Skip to content

Commit a1ead0b

Browse files
committed
Make evm SSTORE address varying with different contract
1 parent 9ef1c60 commit a1ead0b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

state/contract.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ type (
2929

3030
contract struct {
3131
*Account
32-
dirtyCode bool // contract's code has been set
33-
dirtyState bool // contract's account state has changed
34-
code []byte // contract byte-code
35-
trie trie.Trie // storage trie of the contract
32+
dirtyCode bool // contract's code has been set
33+
dirtyState bool // contract's account state has changed
34+
addr hash.PKHash // contract's address
35+
code []byte // contract byte-code
36+
trie trie.Trie // storage trie of the contract
3637
}
3738
)
3839

3940
// GetState get the value from contract storage
4041
func (c *contract) GetState(key hash.Hash32B) ([]byte, error) {
41-
v, err := c.trie.Get(key[:])
42+
slot := hash.Hash256b(append(key[:], c.addr[:]...))
43+
v, err := c.trie.Get(slot[:])
4244
if err != nil {
4345
return nil, err
4446
}
@@ -48,7 +50,8 @@ func (c *contract) GetState(key hash.Hash32B) ([]byte, error) {
4850
// SetState set the value into contract storage
4951
func (c *contract) SetState(key hash.Hash32B, value []byte) error {
5052
c.dirtyState = true
51-
return c.trie.Upsert(key[:], value)
53+
slot := hash.Hash256b(append(key[:], c.addr[:]...))
54+
return c.trie.Upsert(slot[:], value)
5255
}
5356

5457
// GetCode gets the contract's byte-code
@@ -94,9 +97,10 @@ func (c *contract) RootHash() hash.Hash32B {
9497
}
9598

9699
// newContract returns a Contract instance
97-
func newContract(state *Account, tr trie.Trie) Contract {
100+
func newContract(state *Account, pkhash hash.PKHash, tr trie.Trie) Contract {
98101
c := contract{
99102
Account: state,
103+
addr: pkhash,
100104
trie: tr,
101105
}
102106
c.trie.Start(context.Background())

state/workingset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (ws *workingSet) getContract(addr hash.PKHash) (Contract, error) {
470470
return nil, errors.Wrapf(err, "failed to create storage trie for new contract %x", addr)
471471
}
472472
// add to contract cache
473-
contract := newContract(account, tr)
473+
contract := newContract(account, addr, tr)
474474
ws.cachedContract[addr] = contract
475475
return contract, nil
476476
}

0 commit comments

Comments
 (0)