From a1ead0b8ede310e7b8086104fda8a2f8653bcdc1 Mon Sep 17 00:00:00 2001 From: Dustin Xie Date: Tue, 29 Jan 2019 11:07:47 -0800 Subject: [PATCH] Make evm SSTORE address varying with different contract --- state/contract.go | 18 +++++++++++------- state/workingset.go | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/state/contract.go b/state/contract.go index eeaa235109..da98b7a48a 100644 --- a/state/contract.go +++ b/state/contract.go @@ -29,16 +29,18 @@ type ( contract struct { *Account - dirtyCode bool // contract's code has been set - dirtyState bool // contract's account state has changed - code []byte // contract byte-code - trie trie.Trie // storage trie of the contract + dirtyCode bool // contract's code has been set + dirtyState bool // contract's account state has changed + addr hash.PKHash // contract's address + code []byte // contract byte-code + trie trie.Trie // storage trie of the contract } ) // GetState get the value from contract storage func (c *contract) GetState(key hash.Hash32B) ([]byte, error) { - v, err := c.trie.Get(key[:]) + slot := hash.Hash256b(append(key[:], c.addr[:]...)) + v, err := c.trie.Get(slot[:]) if err != nil { return nil, err } @@ -48,7 +50,8 @@ func (c *contract) GetState(key hash.Hash32B) ([]byte, error) { // SetState set the value into contract storage func (c *contract) SetState(key hash.Hash32B, value []byte) error { c.dirtyState = true - return c.trie.Upsert(key[:], value) + slot := hash.Hash256b(append(key[:], c.addr[:]...)) + return c.trie.Upsert(slot[:], value) } // GetCode gets the contract's byte-code @@ -94,9 +97,10 @@ func (c *contract) RootHash() hash.Hash32B { } // newContract returns a Contract instance -func newContract(state *Account, tr trie.Trie) Contract { +func newContract(state *Account, pkhash hash.PKHash, tr trie.Trie) Contract { c := contract{ Account: state, + addr: pkhash, trie: tr, } c.trie.Start(context.Background()) diff --git a/state/workingset.go b/state/workingset.go index ea9aaab0cf..7c01462516 100644 --- a/state/workingset.go +++ b/state/workingset.go @@ -470,7 +470,7 @@ func (ws *workingSet) getContract(addr hash.PKHash) (Contract, error) { return nil, errors.Wrapf(err, "failed to create storage trie for new contract %x", addr) } // add to contract cache - contract := newContract(account, tr) + contract := newContract(account, addr, tr) ws.cachedContract[addr] = contract return contract, nil }