diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-24 01:35:36 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-24 01:35:36 +0800 |
commit | 780abaec988df302e0c98f1a35e9af35b5623746 (patch) | |
tree | dfd26765ec08d3df756730bb186639edb388b4ee /state | |
parent | 1054c155db8ac59b97b81fa7a7a20f2239eb1e82 (diff) | |
download | go-tangerine-780abaec988df302e0c98f1a35e9af35b5623746.tar.gz go-tangerine-780abaec988df302e0c98f1a35e9af35b5623746.tar.zst go-tangerine-780abaec988df302e0c98f1a35e9af35b5623746.zip |
Switched to new trie
Diffstat (limited to 'state')
-rw-r--r-- | state/dump.go | 26 | ||||
-rw-r--r-- | state/state_object.go | 42 | ||||
-rw-r--r-- | state/statedb.go (renamed from state/state.go) | 37 |
3 files changed, 58 insertions, 47 deletions
diff --git a/state/dump.go b/state/dump.go index c1f5ecf3a..40ecff50c 100644 --- a/state/dump.go +++ b/state/dump.go @@ -22,22 +22,23 @@ type World struct { func (self *StateDB) Dump() []byte { world := World{ - Root: ethutil.Bytes2Hex(self.Trie.GetRoot()), + Root: ethutil.Bytes2Hex(self.trie.Root()), Accounts: make(map[string]Account), } - self.Trie.NewIterator().Each(func(key string, value *ethutil.Value) { - stateObject := NewStateObjectFromBytes([]byte(key), value.Bytes()) + it := self.trie.Iterator() + for it.Next() { + stateObject := NewStateObjectFromBytes(it.Key, it.Value) account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.Nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)} account.Storage = make(map[string]string) - stateObject.EachStorage(func(key string, value *ethutil.Value) { - value.Decode() - account.Storage[ethutil.Bytes2Hex([]byte(key))] = ethutil.Bytes2Hex(value.Bytes()) - }) - world.Accounts[ethutil.Bytes2Hex([]byte(key))] = account - }) + storageIt := stateObject.State.trie.Iterator() + for storageIt.Next() { + account.Storage[ethutil.Bytes2Hex(it.Key)] = ethutil.Bytes2Hex(it.Value) + } + world.Accounts[ethutil.Bytes2Hex(it.Key)] = account + } json, err := json.MarshalIndent(world, "", " ") if err != nil { @@ -50,7 +51,8 @@ func (self *StateDB) Dump() []byte { // Debug stuff func (self *StateObject) CreateOutputForDiff() { fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce) - self.EachStorage(func(addr string, value *ethutil.Value) { - fmt.Printf("%x %x\n", addr, value.Bytes()) - }) + it := self.State.trie.Iterator() + for it.Next() { + fmt.Printf("%x %x\n", it.Key, it.Value) + } } diff --git a/state/state_object.go b/state/state_object.go index b8af4e702..420ad9757 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -6,7 +6,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/ptrie" ) type Code []byte @@ -62,7 +62,7 @@ func NewStateObject(addr []byte) *StateObject { address := ethutil.Address(addr) object := &StateObject{address: address, balance: new(big.Int), gasPool: new(big.Int)} - object.State = New(trie.New(ethutil.Config.Db, "")) + object.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -72,7 +72,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.balance = balance - contract.State = New(trie.New(ethutil.Config.Db, string(root))) + contract.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, string(root))) return contract } @@ -89,12 +89,12 @@ func (self *StateObject) MarkForDeletion() { statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance) } -func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { - return ethutil.NewValueFromBytes([]byte(c.State.Trie.Get(string(addr)))) +func (c *StateObject) getAddr(addr []byte) *ethutil.Value { + return ethutil.NewValueFromBytes([]byte(c.State.trie.Get(addr))) } -func (c *StateObject) SetAddr(addr []byte, value interface{}) { - c.State.Trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) +func (c *StateObject) setAddr(addr []byte, value interface{}) { + c.State.trie.Update(addr, ethutil.Encode(value)) } func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { @@ -113,7 +113,7 @@ func (self *StateObject) GetState(k []byte) *ethutil.Value { value := self.storage[string(key)] if value == nil { - value = self.GetAddr(key) + value = self.getAddr(key) if !value.IsNil() { self.storage[string(key)] = value @@ -128,6 +128,7 @@ func (self *StateObject) SetState(k []byte, value *ethutil.Value) { self.storage[string(key)] = value.Copy() } +/* // Iterate over each storage address and yield callback func (self *StateObject) EachStorage(cb trie.EachCallback) { // First loop over the uncommit/cached values in storage @@ -145,23 +146,26 @@ func (self *StateObject) EachStorage(cb trie.EachCallback) { } }) } +*/ func (self *StateObject) Sync() { for key, value := range self.storage { if value.Len() == 0 { - self.State.Trie.Delete(string(key)) + self.State.trie.Delete([]byte(key)) continue } - self.SetAddr([]byte(key), value) + self.setAddr([]byte(key), value) } - valid, t2 := trie.ParanoiaCheck(self.State.Trie) - if !valid { - statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Root(), t2.GetRoot()) + /* + valid, t2 := ptrie.ParanoiaCheck(self.State.trie, ethutil.Config.Db) + if !valid { + statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Root(), t2.Root()) - self.State.Trie = t2 - } + self.State.trie = t2 + } + */ } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { @@ -276,8 +280,12 @@ func (c *StateObject) Init() Code { return c.InitCode } +func (self *StateObject) Trie() *ptrie.Trie { + return self.State.trie +} + func (self *StateObject) Root() []byte { - return self.State.Trie.GetRoot() + return self.Trie().Root() } func (self *StateObject) SetCode(code []byte) { @@ -302,7 +310,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.balance = decoder.Get(1).BigInt() - c.State = New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = New(ptrie.New(decoder.Get(2).Bytes(), ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) diff --git a/state/state.go b/state/statedb.go index f77da72f0..6a11fc328 100644 --- a/state/state.go +++ b/state/statedb.go @@ -1,11 +1,12 @@ package state import ( + "bytes" "math/big" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/ptrie" ) var statelogger = logger.NewLogger("STATE") @@ -16,8 +17,8 @@ var statelogger = logger.NewLogger("STATE") // * Contracts // * Accounts type StateDB struct { - // The trie for this structure - Trie *trie.Trie + //Trie *trie.Trie + trie *ptrie.Trie stateObjects map[string]*StateObject @@ -29,8 +30,8 @@ type StateDB struct { } // Create a new state from a given trie -func New(trie *trie.Trie) *StateDB { - return &StateDB{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} +func New(trie *ptrie.Trie) *StateDB { + return &StateDB{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} } func (self *StateDB) EmptyLogs() { @@ -140,12 +141,12 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) { ethutil.Config.Db.Put(stateObject.CodeHash(), stateObject.Code) } - self.Trie.Update(string(addr), string(stateObject.RlpEncode())) + self.trie.Update(addr, stateObject.RlpEncode()) } // Delete the given state object and delete it from the state trie func (self *StateDB) DeleteStateObject(stateObject *StateObject) { - self.Trie.Delete(string(stateObject.Address())) + self.trie.Delete(stateObject.Address()) delete(self.stateObjects, string(stateObject.Address())) } @@ -159,7 +160,7 @@ func (self *StateDB) GetStateObject(addr []byte) *StateObject { return stateObject } - data := self.Trie.Get(string(addr)) + data := self.trie.Get(addr) if len(data) == 0 { return nil } @@ -206,12 +207,12 @@ func (self *StateDB) GetAccount(addr []byte) *StateObject { // func (s *StateDB) Cmp(other *StateDB) bool { - return s.Trie.Cmp(other.Trie) + return bytes.Equal(s.trie.Root(), other.trie.Root()) } func (self *StateDB) Copy() *StateDB { - if self.Trie != nil { - state := New(self.Trie.Copy()) + if self.trie != nil { + state := New(self.trie.Copy()) for k, stateObject := range self.stateObjects { state.stateObjects[k] = stateObject.Copy() } @@ -235,19 +236,19 @@ func (self *StateDB) Set(state *StateDB) { panic("Tried setting 'state' to nil through 'Set'") } - self.Trie = state.Trie + self.trie = state.trie self.stateObjects = state.stateObjects self.refund = state.refund self.logs = state.logs } func (s *StateDB) Root() []byte { - return s.Trie.GetRoot() + return s.trie.Root() } // Resets the trie and all siblings func (s *StateDB) Reset() { - s.Trie.Undo() + s.trie.Reset() // Reset all nested states for _, stateObject := range s.stateObjects { @@ -272,7 +273,7 @@ func (s *StateDB) Sync() { stateObject.State.Sync() } - s.Trie.Sync() + s.trie.Commit() s.Empty() } @@ -304,11 +305,11 @@ func (self *StateDB) Update(gasUsed *big.Int) { // FIXME trie delete is broken if deleted { - valid, t2 := trie.ParanoiaCheck(self.Trie) + valid, t2 := ptrie.ParanoiaCheck(self.trie, ethutil.Config.Db) if !valid { - statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.Trie.GetRoot(), t2.GetRoot()) + statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.trie.Root(), t2.Root()) - self.Trie = t2 + self.trie = t2 } } } |