Skip to content

Commit

Permalink
Make evm SSTORE address varying with different contract
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jan 29, 2019
1 parent 9ef1c60 commit a1ead0b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions state/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion state/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a1ead0b

Please sign in to comment.