func NewStateObjectFromBytes(address common.Address, data []byte, db common.Database) *StateObject { // TODO clean me up var extobject struct { Nonce uint64 Balance *big.Int Root common.Hash CodeHash []byte } err := rlp.Decode(bytes.NewReader(data), &extobject) if err != nil { fmt.Println(err) return nil } object := &StateObject{address: address, db: db} object.nonce = extobject.Nonce object.balance = extobject.Balance object.codeHash = extobject.CodeHash object.trie = trie.NewSecure(extobject.Root[:], db) object.storage = make(map[string]common.Hash) object.gasPool = new(big.Int) object.prepaid = new(big.Int) object.code, _ = db.Get(extobject.CodeHash) return object }
func NewStateObjectFromBytes(address common.Address, data []byte, db ethdb.Database) *StateObject { var extobject struct { Nonce uint64 Balance *big.Int Root common.Hash CodeHash []byte } err := rlp.Decode(bytes.NewReader(data), &extobject) if err != nil { glog.Errorf("can't decode state object %x: %v", address, err) return nil } trie, err := trie.NewSecure(extobject.Root, db) if err != nil { // TODO: bubble this up or panic glog.Errorf("can't create account trie with root %x: %v", extobject.Root[:], err) return nil } object := &StateObject{address: address, db: db} object.nonce = extobject.Nonce object.balance = extobject.Balance object.codeHash = extobject.CodeHash object.trie = trie object.storage = make(map[string]common.Hash) object.code, _ = db.Get(extobject.CodeHash) return object }
func NewStateObject(address common.Address, db common.Database) *StateObject { object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true} object.trie = trie.NewSecure((common.Hash{}).Bytes(), db) object.storage = make(Storage) object.gasPool = new(big.Int) return object }
func NewStateObject(address common.Address, db trie.Database) *StateObject { object := &StateObject{ db: db, address: address, balance: new(big.Int), dirty: true, codeHash: emptyCodeHash, storage: make(Storage), } object.trie, _ = trie.NewSecure(common.Hash{}, db) return object }
func NewStateObject(address common.Address, db common.Database) *StateObject { // This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter. //address := common.ToAddress(addr) object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true} object.trie = trie.NewSecure((common.Hash{}).Bytes(), db) object.storage = make(Storage) object.gasPool = new(big.Int) object.prepaid = new(big.Int) return object }
func (c *StateObject) RlpDecode(data []byte) { decoder := common.NewValueFromBytes(data) c.nonce = decoder.Get(0).Uint() c.balance = decoder.Get(1).BigInt() c.trie = trie.NewSecure(decoder.Get(2).Bytes(), c.db) c.storage = make(map[string]common.Hash) c.gasPool = new(big.Int) c.codeHash = decoder.Get(3).Bytes() c.code, _ = c.db.Get(c.codeHash) }
// Update associates key with value in the trie. Subsequent calls to // Get will return value. If value has length zero, any existing value // is deleted from the trie and calls to Get will return nil. // // The value bytes must not be modified by the caller while they are // stored in the trie. func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { t.trie, err = trie.NewSecure(t.originalRoot, t.db) } if err == nil { err = t.trie.TryUpdate(key, value) } return }) return }
// Get returns the value for key stored in the trie. // The value bytes must not be modified by the caller. func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { t.trie, err = trie.NewSecure(t.originalRoot, t.db) } if err == nil { res, err = t.trie.TryGet(key) } return }) return }
// Create a new state from a given trie func New(root common.Hash, db ethdb.Database) (*StateDB, error) { tr, err := trie.NewSecure(root, db) if err != nil { return nil, err } return &StateDB{ db: db, trie: tr, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]vm.Logs), }, nil }
// Create a new state from a given trie func New(root common.Hash, db ethdb.Database) (*StateDB, error) { tr, err := trie.NewSecure(root, db) if err != nil { glog.Errorf("can't create state trie with root %x: %v", root[:], err) return nil, err } return &StateDB{ db: db, trie: tr, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]vm.Logs), }, nil }
// SyncObjects syncs the changed objects to the trie func (self *StateDB) SyncObjects() { self.trie = trie.NewSecure(self.root[:], self.db) self.refund = new(big.Int) for _, stateObject := range self.stateObjects { if stateObject.remove { self.DeleteStateObject(stateObject) } else { stateObject.Update() self.UpdateStateObject(stateObject) } stateObject.dirty = false } }
// FastSyncCommitHead sets the current head block to the one defined by the hash // irrelevant what the chain contents were prior. func (self *BlockChain) FastSyncCommitHead(hash common.Hash) error { // Make sure that both the block as well at its state trie exists block := self.GetBlock(hash) if block == nil { return fmt.Errorf("non existent block [%x…]", hash[:4]) } if _, err := trie.NewSecure(block.Root(), self.chainDb); err != nil { return err } // If all checks out, manually set the head block self.mu.Lock() self.currentBlock = block self.mu.Unlock() glog.V(logger.Info).Infof("committed block #%d [%x…] as new head", block.Number(), hash[:4]) return nil }
// Reset clears out all emphemeral state objects from the state db, but keeps // the underlying state trie to avoid reloading data for the next operations. func (self *StateDB) Reset(root common.Hash) error { var ( err error tr = self.trie ) if self.trie.Hash() != root { if tr, err = trie.NewSecure(root, self.db); err != nil { return err } } *self = StateDB{ db: self.db, trie: tr, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]vm.Logs), } return nil }
// DecodeObject decodes an RLP-encoded state object. func DecodeObject(address common.Address, db trie.Database, data []byte) (*StateObject, error) { var ( obj = &StateObject{address: address, db: db, storage: make(Storage)} ext extStateObject err error ) if err = rlp.DecodeBytes(data, &ext); err != nil { return nil, err } if obj.trie, err = trie.NewSecure(ext.Root, db); err != nil { return nil, err } if !bytes.Equal(ext.CodeHash, emptyCodeHash) { if obj.code, err = db.Get(ext.CodeHash); err != nil { return nil, fmt.Errorf("can't find code for hash %x: %v", ext.CodeHash, err) } } obj.nonce = ext.Nonce obj.balance = ext.Balance obj.codeHash = ext.CodeHash return obj, nil }
// Create a new state from a given trie func New(root common.Hash, db common.Database) *StateDB { trie := trie.NewSecure(root[:], db) return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int), logs: make(map[common.Hash]Logs)} }
func NewStateObject(address common.Address, db ethdb.Database) *StateObject { object := &StateObject{db: db, address: address, balance: new(big.Int), dirty: true} object.trie, _ = trie.NewSecure(common.Hash{}, db) object.storage = make(Storage) return object }