eth/tracers: add disableCode/Storage options for prestate

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2024-10-21 23:28:24 +08:00
parent a5fe7353cf
commit 9456ff2f5b

View file

@ -72,7 +72,9 @@ type prestateTracer struct {
} }
type prestateTracerConfig struct { type prestateTracerConfig struct {
DiffMode bool `json:"diffMode"` // If true, this tracer will return state modifications DiffMode bool `json:"diffMode"` // If true, this tracer will return state modifications
DisableCode bool `json:"disableCode"` // If true, this tracer will not return the contract code
DisableStorage bool `json:"disableStorage"` // If true, this tracer will not return the contract storage
} }
func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage, chainConfig *params.ChainConfig) (*tracers.Tracer, error) { func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage, chainConfig *params.ChainConfig) (*tracers.Tracer, error) {
@ -210,7 +212,6 @@ func (t *prestateTracer) processDiffState() {
postAccount := &account{Storage: make(map[common.Hash]common.Hash)} postAccount := &account{Storage: make(map[common.Hash]common.Hash)}
newBalance := t.env.StateDB.GetBalance(addr).ToBig() newBalance := t.env.StateDB.GetBalance(addr).ToBig()
newNonce := t.env.StateDB.GetNonce(addr) newNonce := t.env.StateDB.GetNonce(addr)
newCode := t.env.StateDB.GetCode(addr)
if newBalance.Cmp(t.pre[addr].Balance) != 0 { if newBalance.Cmp(t.pre[addr].Balance) != 0 {
modified = true modified = true
@ -220,25 +221,30 @@ func (t *prestateTracer) processDiffState() {
modified = true modified = true
postAccount.Nonce = newNonce postAccount.Nonce = newNonce
} }
if !bytes.Equal(newCode, t.pre[addr].Code) { if !t.config.DisableCode {
modified = true newCode := t.env.StateDB.GetCode(addr)
postAccount.Code = newCode if !bytes.Equal(newCode, t.pre[addr].Code) {
modified = true
postAccount.Code = newCode
}
} }
for key, val := range state.Storage { if !t.config.DisableStorage {
// don't include the empty slot for key, val := range state.Storage {
if val == (common.Hash{}) { // don't include the empty slot
delete(t.pre[addr].Storage, key) if val == (common.Hash{}) {
} delete(t.pre[addr].Storage, key)
}
newVal := t.env.StateDB.GetState(addr, key) newVal := t.env.StateDB.GetState(addr, key)
if val == newVal { if val == newVal {
// Omit unchanged slots // Omit unchanged slots
delete(t.pre[addr].Storage, key) delete(t.pre[addr].Storage, key)
} else { } else {
modified = true modified = true
if newVal != (common.Hash{}) { if newVal != (common.Hash{}) {
postAccount.Storage[key] = newVal postAccount.Storage[key] = newVal
}
} }
} }
} }
@ -263,11 +269,17 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
Balance: t.env.StateDB.GetBalance(addr).ToBig(), Balance: t.env.StateDB.GetBalance(addr).ToBig(),
Nonce: t.env.StateDB.GetNonce(addr), Nonce: t.env.StateDB.GetNonce(addr),
Code: t.env.StateDB.GetCode(addr), Code: t.env.StateDB.GetCode(addr),
Storage: make(map[common.Hash]common.Hash),
} }
if !acc.exists() { if !acc.exists() {
acc.empty = true acc.empty = true
} }
if t.config.DisableCode {
acc.Code = nil
}
if !t.config.DisableStorage {
acc.Storage = make(map[common.Hash]common.Hash)
}
t.pre[addr] = acc t.pre[addr] = acc
} }
@ -275,6 +287,9 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
// it to the prestate of the given contract. It assumes `lookupAccount` // it to the prestate of the given contract. It assumes `lookupAccount`
// has been performed on the contract before. // has been performed on the contract before.
func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) { func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) {
if t.config.DisableStorage {
return
}
if _, ok := t.pre[addr].Storage[key]; ok { if _, ok := t.pre[addr].Storage[key]; ok {
return return
} }