diff --git a/eth/tracers/native/gen_account_json.go b/eth/tracers/native/gen_account_json.go index 83a54d6e6e..5fec2648b7 100644 --- a/eth/tracers/native/gen_account_json.go +++ b/eth/tracers/native/gen_account_json.go @@ -17,7 +17,7 @@ func (a account) MarshalJSON() ([]byte, error) { type account struct { Balance *hexutil.Big `json:"balance,omitempty"` Code hexutil.Bytes `json:"code,omitempty"` - CodeHash common.Hash `json:"codeHash,omitempty"` + CodeHash *common.Hash `json:"codeHash,omitempty"` Nonce uint64 `json:"nonce,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"` } @@ -50,7 +50,7 @@ func (a *account) UnmarshalJSON(input []byte) error { a.Code = *dec.Code } if dec.CodeHash != nil { - a.CodeHash = *dec.CodeHash + a.CodeHash = dec.CodeHash } if dec.Nonce != nil { a.Nonce = *dec.Nonce diff --git a/eth/tracers/native/prestate.go b/eth/tracers/native/prestate.go index 343f790a05..49679d312f 100644 --- a/eth/tracers/native/prestate.go +++ b/eth/tracers/native/prestate.go @@ -46,7 +46,7 @@ type stateMap = map[common.Address]*account type account struct { Balance *big.Int `json:"balance,omitempty"` Code []byte `json:"code,omitempty"` - CodeHash common.Hash `json:"codeHash,omitempty"` + CodeHash *common.Hash `json:"codeHash,omitempty"` Nonce uint64 `json:"nonce,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"` empty bool @@ -258,9 +258,18 @@ func (t *prestateTracer) processDiffState() { modified = true postAccount.Nonce = newNonce } - if newCodeHash != t.pre[addr].CodeHash { + prevCodeHash := common.Hash{} + if t.pre[addr].CodeHash != nil { + prevCodeHash = *t.pre[addr].CodeHash + } + // Empty code hashes are excluded from the prestate. Normalize + // the empty code hash to a zero hash to make it comparable. + if newCodeHash == types.EmptyCodeHash { + newCodeHash = common.Hash{} + } + if newCodeHash != prevCodeHash { modified = true - postAccount.CodeHash = newCodeHash + postAccount.CodeHash = &newCodeHash } if !t.config.DisableCode { newCode := t.env.StateDB.GetCode(addr) @@ -307,10 +316,14 @@ func (t *prestateTracer) lookupAccount(addr common.Address) { } acc := &account{ - Balance: t.env.StateDB.GetBalance(addr).ToBig(), - Nonce: t.env.StateDB.GetNonce(addr), - Code: t.env.StateDB.GetCode(addr), - CodeHash: t.env.StateDB.GetCodeHash(addr), + Balance: t.env.StateDB.GetBalance(addr).ToBig(), + Nonce: t.env.StateDB.GetNonce(addr), + Code: t.env.StateDB.GetCode(addr), + } + codeHash := t.env.StateDB.GetCodeHash(addr) + // If the code is empty, we don't need to store it in the prestate. + if codeHash != (common.Hash{}) && codeHash != types.EmptyCodeHash { + acc.CodeHash = &codeHash } if !acc.exists() { acc.empty = true