|
|
@ -19,11 +19,11 @@ func (self Code) String() string { |
|
|
|
return string(self) //strings.Join(Disassemble(self), " ")
|
|
|
|
return string(self) //strings.Join(Disassemble(self), " ")
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Storage map[string]*common.Value |
|
|
|
type Storage map[string]common.Hash |
|
|
|
|
|
|
|
|
|
|
|
func (self Storage) String() (str string) { |
|
|
|
func (self Storage) String() (str string) { |
|
|
|
for key, value := range self { |
|
|
|
for key, value := range self { |
|
|
|
str += fmt.Sprintf("%X : %X\n", key, value.Bytes()) |
|
|
|
str += fmt.Sprintf("%X : %X\n", key, value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
@ -32,7 +32,6 @@ func (self Storage) String() (str string) { |
|
|
|
func (self Storage) Copy() Storage { |
|
|
|
func (self Storage) Copy() Storage { |
|
|
|
cpy := make(Storage) |
|
|
|
cpy := make(Storage) |
|
|
|
for key, value := range self { |
|
|
|
for key, value := range self { |
|
|
|
// XXX Do we need a 'value' copy or is this sufficient?
|
|
|
|
|
|
|
|
cpy[key] = value |
|
|
|
cpy[key] = value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -42,8 +41,7 @@ func (self Storage) Copy() Storage { |
|
|
|
type StateObject struct { |
|
|
|
type StateObject struct { |
|
|
|
// State database for storing state changes
|
|
|
|
// State database for storing state changes
|
|
|
|
db common.Database |
|
|
|
db common.Database |
|
|
|
// The state object
|
|
|
|
trie *trie.SecureTrie |
|
|
|
State *StateDB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Address belonging to this account
|
|
|
|
// Address belonging to this account
|
|
|
|
address common.Address |
|
|
|
address common.Address |
|
|
@ -76,7 +74,6 @@ type StateObject struct { |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Reset() { |
|
|
|
func (self *StateObject) Reset() { |
|
|
|
self.storage = make(Storage) |
|
|
|
self.storage = make(Storage) |
|
|
|
self.State.Reset() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewStateObject(address common.Address, db common.Database) *StateObject { |
|
|
|
func NewStateObject(address common.Address, db common.Database) *StateObject { |
|
|
@ -84,7 +81,7 @@ func NewStateObject(address common.Address, db common.Database) *StateObject { |
|
|
|
//address := common.ToAddress(addr)
|
|
|
|
//address := common.ToAddress(addr)
|
|
|
|
|
|
|
|
|
|
|
|
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true} |
|
|
|
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true} |
|
|
|
object.State = New(common.Hash{}, db) //New(trie.New(common.Config.Db, ""))
|
|
|
|
object.trie = trie.NewSecure((common.Hash{}).Bytes(), db) |
|
|
|
object.storage = make(Storage) |
|
|
|
object.storage = make(Storage) |
|
|
|
object.gasPool = new(big.Int) |
|
|
|
object.gasPool = new(big.Int) |
|
|
|
object.prepaid = new(big.Int) |
|
|
|
object.prepaid = new(big.Int) |
|
|
@ -111,8 +108,8 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db common.Data |
|
|
|
object.nonce = extobject.Nonce |
|
|
|
object.nonce = extobject.Nonce |
|
|
|
object.balance = extobject.Balance |
|
|
|
object.balance = extobject.Balance |
|
|
|
object.codeHash = extobject.CodeHash |
|
|
|
object.codeHash = extobject.CodeHash |
|
|
|
object.State = New(extobject.Root, db) |
|
|
|
object.trie = trie.NewSecure(extobject.Root[:], db) |
|
|
|
object.storage = make(map[string]*common.Value) |
|
|
|
object.storage = make(map[string]common.Hash) |
|
|
|
object.gasPool = new(big.Int) |
|
|
|
object.gasPool = new(big.Int) |
|
|
|
object.prepaid = new(big.Int) |
|
|
|
object.prepaid = new(big.Int) |
|
|
|
object.code, _ = db.Get(extobject.CodeHash) |
|
|
|
object.code, _ = db.Get(extobject.CodeHash) |
|
|
@ -129,35 +126,31 @@ func (self *StateObject) MarkForDeletion() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *StateObject) getAddr(addr common.Hash) *common.Value { |
|
|
|
func (c *StateObject) getAddr(addr common.Hash) common.Hash { |
|
|
|
return common.NewValueFromBytes([]byte(c.State.trie.Get(addr[:]))) |
|
|
|
var ret []byte |
|
|
|
|
|
|
|
rlp.DecodeBytes(c.trie.Get(addr[:]), &ret) |
|
|
|
|
|
|
|
return common.BytesToHash(ret) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *StateObject) setAddr(addr []byte, value interface{}) { |
|
|
|
func (c *StateObject) setAddr(addr []byte, value common.Hash) { |
|
|
|
c.State.trie.Update(addr, common.Encode(value)) |
|
|
|
v, err := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00")) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
|
|
|
|
func (self *StateObject) GetStorage(key *big.Int) *common.Value { |
|
|
|
panic(err) |
|
|
|
fmt.Printf("%v: get %v %v", self.address.Hex(), key) |
|
|
|
|
|
|
|
return self.GetState(common.BytesToHash(key.Bytes())) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
c.trie.Update(addr, v) |
|
|
|
func (self *StateObject) SetStorage(key *big.Int, value *common.Value) { |
|
|
|
|
|
|
|
fmt.Printf("%v: set %v -> %v", self.address.Hex(), key, value) |
|
|
|
|
|
|
|
self.SetState(common.BytesToHash(key.Bytes()), value) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Storage() Storage { |
|
|
|
func (self *StateObject) Storage() Storage { |
|
|
|
return self.storage |
|
|
|
return self.storage |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) GetState(key common.Hash) *common.Value { |
|
|
|
func (self *StateObject) GetState(key common.Hash) common.Hash { |
|
|
|
strkey := key.Str() |
|
|
|
strkey := key.Str() |
|
|
|
value := self.storage[strkey] |
|
|
|
value, exists := self.storage[strkey] |
|
|
|
if value == nil { |
|
|
|
if !exists { |
|
|
|
value = self.getAddr(key) |
|
|
|
value = self.getAddr(key) |
|
|
|
|
|
|
|
if (value != common.Hash{}) { |
|
|
|
if !value.IsNil() { |
|
|
|
|
|
|
|
self.storage[strkey] = value |
|
|
|
self.storage[strkey] = value |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -165,15 +158,16 @@ func (self *StateObject) GetState(key common.Hash) *common.Value { |
|
|
|
return value |
|
|
|
return value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) SetState(k common.Hash, value *common.Value) { |
|
|
|
func (self *StateObject) SetState(k, value common.Hash) { |
|
|
|
self.storage[k.Str()] = value.Copy() |
|
|
|
self.storage[k.Str()] = value |
|
|
|
self.dirty = true |
|
|
|
self.dirty = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Sync() { |
|
|
|
// Update updates the current cached storage to the trie
|
|
|
|
|
|
|
|
func (self *StateObject) Update() { |
|
|
|
for key, value := range self.storage { |
|
|
|
for key, value := range self.storage { |
|
|
|
if value.Len() == 0 { |
|
|
|
if (value == common.Hash{}) { |
|
|
|
self.State.trie.Delete([]byte(key)) |
|
|
|
self.trie.Delete([]byte(key)) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -266,9 +260,7 @@ func (self *StateObject) Copy() *StateObject { |
|
|
|
stateObject.balance.Set(self.balance) |
|
|
|
stateObject.balance.Set(self.balance) |
|
|
|
stateObject.codeHash = common.CopyBytes(self.codeHash) |
|
|
|
stateObject.codeHash = common.CopyBytes(self.codeHash) |
|
|
|
stateObject.nonce = self.nonce |
|
|
|
stateObject.nonce = self.nonce |
|
|
|
if self.State != nil { |
|
|
|
stateObject.trie = self.trie |
|
|
|
stateObject.State = self.State.Copy() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
stateObject.code = common.CopyBytes(self.code) |
|
|
|
stateObject.code = common.CopyBytes(self.code) |
|
|
|
stateObject.initCode = common.CopyBytes(self.initCode) |
|
|
|
stateObject.initCode = common.CopyBytes(self.initCode) |
|
|
|
stateObject.storage = self.storage.Copy() |
|
|
|
stateObject.storage = self.storage.Copy() |
|
|
@ -306,11 +298,11 @@ func (c *StateObject) Init() Code { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Trie() *trie.SecureTrie { |
|
|
|
func (self *StateObject) Trie() *trie.SecureTrie { |
|
|
|
return self.State.trie |
|
|
|
return self.trie |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Root() []byte { |
|
|
|
func (self *StateObject) Root() []byte { |
|
|
|
return self.Trie().Root() |
|
|
|
return self.trie.Root() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateObject) Code() []byte { |
|
|
|
func (self *StateObject) Code() []byte { |
|
|
@ -342,10 +334,10 @@ func (self *StateObject) EachStorage(cb func(key, value []byte)) { |
|
|
|
cb([]byte(h), v.Bytes()) |
|
|
|
cb([]byte(h), v.Bytes()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
it := self.State.trie.Iterator() |
|
|
|
it := self.trie.Iterator() |
|
|
|
for it.Next() { |
|
|
|
for it.Next() { |
|
|
|
// ignore cached values
|
|
|
|
// ignore cached values
|
|
|
|
key := self.State.trie.GetKey(it.Key) |
|
|
|
key := self.trie.GetKey(it.Key) |
|
|
|
if _, ok := self.storage[string(key)]; !ok { |
|
|
|
if _, ok := self.storage[string(key)]; !ok { |
|
|
|
cb(key, it.Value) |
|
|
|
cb(key, it.Value) |
|
|
|
} |
|
|
|
} |
|
|
@ -369,8 +361,8 @@ func (c *StateObject) RlpDecode(data []byte) { |
|
|
|
decoder := common.NewValueFromBytes(data) |
|
|
|
decoder := common.NewValueFromBytes(data) |
|
|
|
c.nonce = decoder.Get(0).Uint() |
|
|
|
c.nonce = decoder.Get(0).Uint() |
|
|
|
c.balance = decoder.Get(1).BigInt() |
|
|
|
c.balance = decoder.Get(1).BigInt() |
|
|
|
c.State = New(common.BytesToHash(decoder.Get(2).Bytes()), c.db) //New(trie.New(common.Config.Db, decoder.Get(2).Interface()))
|
|
|
|
c.trie = trie.NewSecure(decoder.Get(2).Bytes(), c.db) |
|
|
|
c.storage = make(map[string]*common.Value) |
|
|
|
c.storage = make(map[string]common.Hash) |
|
|
|
c.gasPool = new(big.Int) |
|
|
|
c.gasPool = new(big.Int) |
|
|
|
|
|
|
|
|
|
|
|
c.codeHash = decoder.Get(3).Bytes() |
|
|
|
c.codeHash = decoder.Get(3).Bytes() |
|
|
|