aboutsummaryrefslogtreecommitdiffstats
path: root/core/state
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-10-01 20:44:53 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-10-01 21:01:58 +0800
commitcb84e3f02953f2df166ae69369d222dcbbd7d78d (patch)
tree68eb52c91a136e820f3656e394847c4e695afa63 /core/state
parentd8715fba1a366944a069397775fc52a30358eff3 (diff)
downloadgo-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar.gz
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar.bz2
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar.lz
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar.xz
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.tar.zst
go-tangerine-cb84e3f02953f2df166ae69369d222dcbbd7d78d.zip
cmd, core, internal, light, tests: avoid hashing the code in the VM
Diffstat (limited to 'core/state')
-rw-r--r--core/state/state_object.go4
-rw-r--r--core/state/state_test.go7
-rw-r--r--core/state/statedb.go11
-rw-r--r--core/state/statedb_test.go9
-rw-r--r--core/state/sync_test.go2
5 files changed, 22 insertions, 11 deletions
diff --git a/core/state/state_object.go b/core/state/state_object.go
index a54620d55..121a2ec5c 100644
--- a/core/state/state_object.go
+++ b/core/state/state_object.go
@@ -273,9 +273,9 @@ func (self *StateObject) Code(db trie.Database) []byte {
return code
}
-func (self *StateObject) SetCode(code []byte) {
+func (self *StateObject) SetCode(codeHash common.Hash, code []byte) {
self.code = code
- self.data.CodeHash = crypto.Keccak256(code)
+ self.data.CodeHash = codeHash[:]
self.dirtyCode = true
if self.onDirty != nil {
self.onDirty(self.Address())
diff --git a/core/state/state_test.go b/core/state/state_test.go
index fcdc38588..5fe98939b 100644
--- a/core/state/state_test.go
+++ b/core/state/state_test.go
@@ -24,6 +24,7 @@ import (
checker "gopkg.in/check.v1"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
)
@@ -40,7 +41,7 @@ func (s *StateSuite) TestDump(c *checker.C) {
obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
obj1.AddBalance(big.NewInt(22))
obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
- obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3})
+ obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
obj3.SetBalance(big.NewInt(44))
@@ -148,7 +149,7 @@ func TestSnapshot2(t *testing.T) {
so0 := state.GetStateObject(stateobjaddr0)
so0.SetBalance(big.NewInt(42))
so0.SetNonce(43)
- so0.SetCode([]byte{'c', 'a', 'f', 'e'})
+ so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
so0.remove = false
so0.deleted = false
state.SetStateObject(so0)
@@ -160,7 +161,7 @@ func TestSnapshot2(t *testing.T) {
so1 := state.GetStateObject(stateobjaddr1)
so1.SetBalance(big.NewInt(52))
so1.SetNonce(53)
- so1.SetCode([]byte{'c', 'a', 'f', 'e', '2'})
+ so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'})
so1.remove = true
so1.deleted = true
state.SetStateObject(so1)
diff --git a/core/state/statedb.go b/core/state/statedb.go
index 5c51e3b59..4204c456e 100644
--- a/core/state/statedb.go
+++ b/core/state/statedb.go
@@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
@@ -246,6 +247,14 @@ func (self *StateDB) GetCodeSize(addr common.Address) int {
return size
}
+func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
+ stateObject := self.GetStateObject(addr)
+ if stateObject == nil {
+ return common.Hash{}
+ }
+ return common.BytesToHash(stateObject.CodeHash())
+}
+
func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
stateObject := self.GetStateObject(a)
if stateObject != nil {
@@ -283,7 +292,7 @@ func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
func (self *StateDB) SetCode(addr common.Address, code []byte) {
stateObject := self.GetOrNewStateObject(addr)
if stateObject != nil {
- stateObject.SetCode(code)
+ stateObject.SetCode(crypto.Keccak256Hash(code), code)
}
}
diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go
index 928333459..7930b620d 100644
--- a/core/state/statedb_test.go
+++ b/core/state/statedb_test.go
@@ -21,6 +21,7 @@ import (
"testing"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
)
@@ -40,7 +41,7 @@ func TestUpdateLeaks(t *testing.T) {
obj.SetState(common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
}
if i%3 == 0 {
- obj.SetCode([]byte{i, i, i, i, i})
+ obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
}
state.UpdateStateObject(obj)
}
@@ -70,7 +71,7 @@ func TestIntermediateLeaks(t *testing.T) {
obj.SetState(common.BytesToHash([]byte{i, i, i, 0}), common.BytesToHash([]byte{i, i, i, i, 0}))
}
if i%3 == 0 {
- obj.SetCode([]byte{i, i, i, i, i, 0})
+ obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i, 0}), []byte{i, i, i, i, i, 0})
}
transState.UpdateStateObject(obj)
@@ -82,7 +83,7 @@ func TestIntermediateLeaks(t *testing.T) {
obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
}
if i%3 == 0 {
- obj.SetCode([]byte{i, i, i, i, i, 1})
+ obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i, 1}), []byte{i, i, i, i, i, 1})
}
transState.UpdateStateObject(obj)
@@ -94,7 +95,7 @@ func TestIntermediateLeaks(t *testing.T) {
obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
}
if i%3 == 0 {
- obj.SetCode([]byte{i, i, i, i, i, 1})
+ obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i, 1}), []byte{i, i, i, i, i, 1})
}
finalState.UpdateStateObject(obj)
}
diff --git a/core/state/sync_test.go b/core/state/sync_test.go
index c768781a4..670e1fb1b 100644
--- a/core/state/sync_test.go
+++ b/core/state/sync_test.go
@@ -54,7 +54,7 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
acc.nonce = uint64(42 * i)
if i%3 == 0 {
- obj.SetCode([]byte{i, i, i, i, i})
+ obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
acc.code = []byte{i, i, i, i, i}
}
state.UpdateStateObject(obj)