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

@ -73,6 +73,8 @@ 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,11 +221,15 @@ func (t *prestateTracer) processDiffState() {
modified = true modified = true
postAccount.Nonce = newNonce postAccount.Nonce = newNonce
} }
if !t.config.DisableCode {
newCode := t.env.StateDB.GetCode(addr)
if !bytes.Equal(newCode, t.pre[addr].Code) { if !bytes.Equal(newCode, t.pre[addr].Code) {
modified = true modified = true
postAccount.Code = newCode postAccount.Code = newCode
} }
}
if !t.config.DisableStorage {
for key, val := range state.Storage { for key, val := range state.Storage {
// don't include the empty slot // don't include the empty slot
if val == (common.Hash{}) { if val == (common.Hash{}) {
@ -242,6 +247,7 @@ func (t *prestateTracer) processDiffState() {
} }
} }
} }
}
if modified { if modified {
t.post[addr] = postAccount t.post[addr] = postAccount
@ -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
} }