func (c *StateObject) getTrie(db trie.Database) *trie.SecureTrie { if c.trie == nil { var err error c.trie, err = trie.NewSecure(c.data.Root, db) if err != nil { c.trie, _ = trie.NewSecure(common.Hash{}, db) c.setError(fmt.Errorf("can't create storage trie: %v", err)) } } return c.trie }
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 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.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 }
// openTrie creates a trie. It uses an existing trie if one is available // from the journal if available. func (self *StateDB) openTrie(root common.Hash) (*trie.SecureTrie, error) { for i := len(self.pastTries) - 1; i >= 0; i-- { if self.pastTries[i].Hash() == root { tr := *self.pastTries[i] return &tr, nil } } return trie.NewSecure(root, self.db) }
// 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 }
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) }
// 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 { return nil, err } csc, _ := lru.New(codeSizeCacheSize) return &StateDB{ db: db, trie: tr, codeSizeCache: csc, stateObjects: make(map[common.Address]*StateObject), stateObjectsDirty: make(map[common.Address]struct{}), 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 }
// Create a new state from a given trie func New(root common.Hash, db common.Database) *StateDB { trie := trie.NewSecure(root[:], db) return &StateDB{root: root, db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: new(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 }