aboutsummaryrefslogtreecommitdiffstats
path: root/core/state
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-06-17 17:24:40 +0800
committerobscuren <geffobscura@gmail.com>2015-06-17 17:24:40 +0800
commit787a61bb27b9f51f4af67c69eb8f1c5869ae3144 (patch)
tree33405ad345b4350940eb163339c5a5de711bfc6e /core/state
parent5721fcf668f8ab798b6602dc6ff88726bf0c8f86 (diff)
downloadgo-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar.gz
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar.bz2
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar.lz
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar.xz
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.tar.zst
go-tangerine-787a61bb27b9f51f4af67c69eb8f1c5869ae3144.zip
core/state, core/vm: reworked storage get / set to use common.Hash
Diffstat (limited to 'core/state')
-rw-r--r--core/state/state_object.go47
-rw-r--r--core/state/statedb.go10
2 files changed, 25 insertions, 32 deletions
diff --git a/core/state/state_object.go b/core/state/state_object.go
index 6d2455d79..42dac632b 100644
--- a/core/state/state_object.go
+++ b/core/state/state_object.go
@@ -19,11 +19,11 @@ func (self Code) String() string {
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) {
for key, value := range self {
- str += fmt.Sprintf("%X : %X\n", key, value.Bytes())
+ str += fmt.Sprintf("%X : %X\n", key, value)
}
return
@@ -32,7 +32,6 @@ func (self Storage) String() (str string) {
func (self Storage) Copy() Storage {
cpy := make(Storage)
for key, value := range self {
- // XXX Do we need a 'value' copy or is this sufficient?
cpy[key] = value
}
@@ -112,7 +111,7 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db common.Data
object.balance = extobject.Balance
object.codeHash = extobject.CodeHash
object.State = New(extobject.Root, db)
- object.storage = make(map[string]*common.Value)
+ object.storage = make(map[string]common.Hash)
object.gasPool = new(big.Int)
object.prepaid = new(big.Int)
object.code, _ = db.Get(extobject.CodeHash)
@@ -129,35 +128,29 @@ func (self *StateObject) MarkForDeletion() {
}
}
-func (c *StateObject) getAddr(addr common.Hash) *common.Value {
- return common.NewValueFromBytes([]byte(c.State.trie.Get(addr[:])))
+func (c *StateObject) getAddr(addr common.Hash) (ret common.Hash) {
+ return common.BytesToHash(common.NewValueFromBytes([]byte(c.State.trie.Get(addr[:]))).Bytes())
}
-func (c *StateObject) setAddr(addr []byte, value interface{}) {
- c.State.trie.Update(addr, common.Encode(value))
-}
-
-func (self *StateObject) GetStorage(key *big.Int) *common.Value {
- fmt.Printf("%v: get %v %v", self.address.Hex(), key)
- return self.GetState(common.BytesToHash(key.Bytes()))
-}
-
-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 (c *StateObject) setAddr(addr []byte, value common.Hash) {
+ 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
+ panic(err)
+ }
+ c.State.trie.Update(addr, v)
}
func (self *StateObject) Storage() 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()
- value := self.storage[strkey]
- if value == nil {
+ value, exists := self.storage[strkey]
+ if !exists {
value = self.getAddr(key)
-
- if !value.IsNil() {
+ if (value != common.Hash{}) {
self.storage[strkey] = value
}
}
@@ -165,14 +158,14 @@ func (self *StateObject) GetState(key common.Hash) *common.Value {
return value
}
-func (self *StateObject) SetState(k common.Hash, value *common.Value) {
- self.storage[k.Str()] = value.Copy()
+func (self *StateObject) SetState(k, value common.Hash) {
+ self.storage[k.Str()] = value
self.dirty = true
}
func (self *StateObject) Sync() {
for key, value := range self.storage {
- if value.Len() == 0 {
+ if (value == common.Hash{}) {
self.State.trie.Delete([]byte(key))
continue
}
@@ -370,7 +363,7 @@ func (c *StateObject) RlpDecode(data []byte) {
c.nonce = decoder.Get(0).Uint()
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.storage = make(map[string]*common.Value)
+ c.storage = make(map[string]common.Hash)
c.gasPool = new(big.Int)
c.codeHash = decoder.Get(3).Bytes()
diff --git a/core/state/statedb.go b/core/state/statedb.go
index 895d9fe8b..1c75ee4db 100644
--- a/core/state/statedb.go
+++ b/core/state/statedb.go
@@ -103,13 +103,13 @@ func (self *StateDB) GetCode(addr common.Address) []byte {
return nil
}
-func (self *StateDB) GetState(a common.Address, b common.Hash) []byte {
+func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
stateObject := self.GetStateObject(a)
if stateObject != nil {
- return stateObject.GetState(b).Bytes()
+ return stateObject.GetState(b)
}
- return nil
+ return common.Hash{}
}
func (self *StateDB) IsDeleted(addr common.Address) bool {
@@ -145,10 +145,10 @@ func (self *StateDB) SetCode(addr common.Address, code []byte) {
}
}
-func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
+func (self *StateDB) SetState(addr common.Address, key common.Hash, value common.Hash) {
stateObject := self.GetOrNewStateObject(addr)
if stateObject != nil {
- stateObject.SetState(key, common.NewValue(value))
+ stateObject.SetState(key, value)
}
}