|
|
|
@ -10,12 +10,12 @@ import ( |
|
|
|
|
|
|
|
|
|
var statelogger = logger.NewLogger("STATE") |
|
|
|
|
|
|
|
|
|
// States within the ethereum protocol are used to store anything
|
|
|
|
|
// within the merkle trie. States take care of caching and storing
|
|
|
|
|
// StateDBs within the ethereum protocol are used to store anything
|
|
|
|
|
// within the merkle trie. StateDBs take care of caching and storing
|
|
|
|
|
// nested states. It's the general query interface to retrieve:
|
|
|
|
|
// * Contracts
|
|
|
|
|
// * Accounts
|
|
|
|
|
type State struct { |
|
|
|
|
type StateDB struct { |
|
|
|
|
// The trie for this structure
|
|
|
|
|
Trie *trie.Trie |
|
|
|
|
|
|
|
|
@ -29,24 +29,24 @@ type State struct { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a new state from a given trie
|
|
|
|
|
func New(trie *trie.Trie) *State { |
|
|
|
|
return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string][]refund)} |
|
|
|
|
func New(trie *trie.Trie) *StateDB { |
|
|
|
|
return &StateDB{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string][]refund)} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) EmptyLogs() { |
|
|
|
|
func (self *StateDB) EmptyLogs() { |
|
|
|
|
self.logs = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) AddLog(log *Log) { |
|
|
|
|
func (self *StateDB) AddLog(log *Log) { |
|
|
|
|
self.logs = append(self.logs, log) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Logs() Logs { |
|
|
|
|
func (self *StateDB) Logs() Logs { |
|
|
|
|
return self.logs |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Retrieve the balance from the given address or 0 if object not found
|
|
|
|
|
func (self *State) GetBalance(addr []byte) *big.Int { |
|
|
|
|
func (self *StateDB) GetBalance(addr []byte) *big.Int { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
return stateObject.balance |
|
|
|
@ -59,18 +59,18 @@ type refund struct { |
|
|
|
|
gas, price *big.Int |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Refund(addr []byte, gas, price *big.Int) { |
|
|
|
|
func (self *StateDB) Refund(addr []byte, gas, price *big.Int) { |
|
|
|
|
self.refund[string(addr)] = append(self.refund[string(addr)], refund{gas, price}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) AddBalance(addr []byte, amount *big.Int) { |
|
|
|
|
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
stateObject.AddBalance(amount) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) GetNonce(addr []byte) uint64 { |
|
|
|
|
func (self *StateDB) GetNonce(addr []byte) uint64 { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
return stateObject.Nonce |
|
|
|
@ -79,14 +79,14 @@ func (self *State) GetNonce(addr []byte) uint64 { |
|
|
|
|
return 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) SetNonce(addr []byte, nonce uint64) { |
|
|
|
|
func (self *StateDB) SetNonce(addr []byte, nonce uint64) { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
stateObject.Nonce = nonce |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) GetCode(addr []byte) []byte { |
|
|
|
|
func (self *StateDB) GetCode(addr []byte) []byte { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
return stateObject.Code |
|
|
|
@ -95,7 +95,7 @@ func (self *State) GetCode(addr []byte) []byte { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) GetState(a, b []byte) []byte { |
|
|
|
|
func (self *StateDB) GetState(a, b []byte) []byte { |
|
|
|
|
stateObject := self.GetStateObject(a) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
return stateObject.GetState(b).Bytes() |
|
|
|
@ -104,14 +104,14 @@ func (self *State) GetState(a, b []byte) []byte { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) SetState(addr, key []byte, value interface{}) { |
|
|
|
|
func (self *StateDB) SetState(addr, key []byte, value interface{}) { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
stateObject.SetState(key, ethutil.NewValue(value)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Delete(addr []byte) bool { |
|
|
|
|
func (self *StateDB) Delete(addr []byte) bool { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject != nil { |
|
|
|
|
stateObject.MarkForDeletion() |
|
|
|
@ -127,7 +127,7 @@ func (self *State) Delete(addr []byte) bool { |
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Update the given state object and apply it to state trie
|
|
|
|
|
func (self *State) UpdateStateObject(stateObject *StateObject) { |
|
|
|
|
func (self *StateDB) UpdateStateObject(stateObject *StateObject) { |
|
|
|
|
addr := stateObject.Address() |
|
|
|
|
|
|
|
|
|
if len(stateObject.CodeHash()) > 0 { |
|
|
|
@ -138,14 +138,14 @@ func (self *State) UpdateStateObject(stateObject *StateObject) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Delete the given state object and delete it from the state trie
|
|
|
|
|
func (self *State) DeleteStateObject(stateObject *StateObject) { |
|
|
|
|
func (self *StateDB) DeleteStateObject(stateObject *StateObject) { |
|
|
|
|
self.Trie.Delete(string(stateObject.Address())) |
|
|
|
|
|
|
|
|
|
delete(self.stateObjects, string(stateObject.Address())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Retrieve a state object given my the address. Nil if not found
|
|
|
|
|
func (self *State) GetStateObject(addr []byte) *StateObject { |
|
|
|
|
func (self *StateDB) GetStateObject(addr []byte) *StateObject { |
|
|
|
|
addr = ethutil.Address(addr) |
|
|
|
|
|
|
|
|
|
stateObject := self.stateObjects[string(addr)] |
|
|
|
@ -164,12 +164,12 @@ func (self *State) GetStateObject(addr []byte) *StateObject { |
|
|
|
|
return stateObject |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) SetStateObject(object *StateObject) { |
|
|
|
|
func (self *StateDB) SetStateObject(object *StateObject) { |
|
|
|
|
self.stateObjects[string(object.address)] = object |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Retrieve a state object or create a new state object if nil
|
|
|
|
|
func (self *State) GetOrNewStateObject(addr []byte) *StateObject { |
|
|
|
|
func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject { |
|
|
|
|
stateObject := self.GetStateObject(addr) |
|
|
|
|
if stateObject == nil { |
|
|
|
|
stateObject = self.NewStateObject(addr) |
|
|
|
@ -179,7 +179,7 @@ func (self *State) GetOrNewStateObject(addr []byte) *StateObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a state object whether it exist in the trie or not
|
|
|
|
|
func (self *State) NewStateObject(addr []byte) *StateObject { |
|
|
|
|
func (self *StateDB) NewStateObject(addr []byte) *StateObject { |
|
|
|
|
addr = ethutil.Address(addr) |
|
|
|
|
|
|
|
|
|
statelogger.Debugf("(+) %x\n", addr) |
|
|
|
@ -191,7 +191,7 @@ func (self *State) NewStateObject(addr []byte) *StateObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
|
func (self *State) GetAccount(addr []byte) *StateObject { |
|
|
|
|
func (self *StateDB) GetAccount(addr []byte) *StateObject { |
|
|
|
|
return self.GetOrNewStateObject(addr) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -199,11 +199,11 @@ func (self *State) GetAccount(addr []byte) *StateObject { |
|
|
|
|
// Setting, copying of the state methods
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
func (s *State) Cmp(other *State) bool { |
|
|
|
|
func (s *StateDB) Cmp(other *StateDB) bool { |
|
|
|
|
return s.Trie.Cmp(other.Trie) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Copy() *State { |
|
|
|
|
func (self *StateDB) Copy() *StateDB { |
|
|
|
|
if self.Trie != nil { |
|
|
|
|
state := New(self.Trie.Copy()) |
|
|
|
|
for k, stateObject := range self.stateObjects { |
|
|
|
@ -224,7 +224,7 @@ func (self *State) Copy() *State { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Set(state *State) { |
|
|
|
|
func (self *StateDB) Set(state *StateDB) { |
|
|
|
|
if state == nil { |
|
|
|
|
panic("Tried setting 'state' to nil through 'Set'") |
|
|
|
|
} |
|
|
|
@ -235,12 +235,12 @@ func (self *State) Set(state *State) { |
|
|
|
|
self.logs = state.logs |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *State) Root() []byte { |
|
|
|
|
func (s *StateDB) Root() []byte { |
|
|
|
|
return s.Trie.GetRoot() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Resets the trie and all siblings
|
|
|
|
|
func (s *State) Reset() { |
|
|
|
|
func (s *StateDB) Reset() { |
|
|
|
|
s.Trie.Undo() |
|
|
|
|
|
|
|
|
|
// Reset all nested states
|
|
|
|
@ -256,7 +256,7 @@ func (s *State) Reset() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Syncs the trie and all siblings
|
|
|
|
|
func (s *State) Sync() { |
|
|
|
|
func (s *StateDB) Sync() { |
|
|
|
|
// Sync all nested states
|
|
|
|
|
for _, stateObject := range s.stateObjects { |
|
|
|
|
if stateObject.State == nil { |
|
|
|
@ -271,12 +271,12 @@ func (s *State) Sync() { |
|
|
|
|
s.Empty() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Empty() { |
|
|
|
|
func (self *StateDB) Empty() { |
|
|
|
|
self.stateObjects = make(map[string]*StateObject) |
|
|
|
|
self.refund = make(map[string][]refund) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Update(gasUsed *big.Int) { |
|
|
|
|
func (self *StateDB) Update(gasUsed *big.Int) { |
|
|
|
|
var deleted bool |
|
|
|
|
|
|
|
|
|
// Refund any gas that's left
|
|
|
|
@ -313,12 +313,12 @@ func (self *State) Update(gasUsed *big.Int) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *State) Manifest() *Manifest { |
|
|
|
|
func (self *StateDB) Manifest() *Manifest { |
|
|
|
|
return self.manifest |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debug stuff
|
|
|
|
|
func (self *State) CreateOutputForDiff() { |
|
|
|
|
func (self *StateDB) CreateOutputForDiff() { |
|
|
|
|
for _, stateObject := range self.stateObjects { |
|
|
|
|
stateObject.CreateOutputForDiff() |
|
|
|
|
} |
|
|
|
|