eth/tracers, cmd/workload: use standard account for json unmarshal

This commit is contained in:
Gary Rong 2025-12-11 10:51:03 +08:00
parent 59734034d3
commit fbd6024240
3 changed files with 23 additions and 23 deletions

View file

@ -223,7 +223,7 @@ func genStorageProof(cli *client, startBlock uint64, endBlock uint64, number int
log.Error("Failed to marshal data", "err", err) log.Error("Failed to marshal data", "err", err)
continue continue
} }
var accounts map[common.Address]*native.PrestateAccount var accounts map[common.Address]*types.Account
if err := json.Unmarshal(blob, &accounts); err != nil { if err := json.Unmarshal(blob, &accounts); err != nil {
log.Error("Failed to decode trace result", "blockNumber", blockNumber, "hash", tx.Hash(), "err", err) log.Error("Failed to decode trace result", "blockNumber", blockNumber, "hash", tx.Hash(), "err", err)
continue continue

View file

@ -13,50 +13,50 @@ import (
var _ = (*accountMarshaling)(nil) var _ = (*accountMarshaling)(nil)
// MarshalJSON marshals as JSON. // MarshalJSON marshals as JSON.
func (p PrestateAccount) MarshalJSON() ([]byte, error) { func (a account) MarshalJSON() ([]byte, error) {
type PrestateAccount struct { type account struct {
Balance *hexutil.Big `json:"balance,omitempty"` Balance *hexutil.Big `json:"balance,omitempty"`
Code hexutil.Bytes `json:"code,omitempty"` Code hexutil.Bytes `json:"code,omitempty"`
CodeHash *common.Hash `json:"codeHash,omitempty"` CodeHash *common.Hash `json:"codeHash,omitempty"`
Nonce uint64 `json:"nonce,omitempty"` Nonce uint64 `json:"nonce,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
} }
var enc PrestateAccount var enc account
enc.Balance = (*hexutil.Big)(p.Balance) enc.Balance = (*hexutil.Big)(a.Balance)
enc.Code = p.Code enc.Code = a.Code
enc.CodeHash = p.CodeHash enc.CodeHash = a.CodeHash
enc.Nonce = p.Nonce enc.Nonce = a.Nonce
enc.Storage = p.Storage enc.Storage = a.Storage
return json.Marshal(&enc) return json.Marshal(&enc)
} }
// UnmarshalJSON unmarshals from JSON. // UnmarshalJSON unmarshals from JSON.
func (p *PrestateAccount) UnmarshalJSON(input []byte) error { func (a *account) UnmarshalJSON(input []byte) error {
type PrestateAccount struct { type account struct {
Balance *hexutil.Big `json:"balance,omitempty"` Balance *hexutil.Big `json:"balance,omitempty"`
Code *hexutil.Bytes `json:"code,omitempty"` Code *hexutil.Bytes `json:"code,omitempty"`
CodeHash *common.Hash `json:"codeHash,omitempty"` CodeHash *common.Hash `json:"codeHash,omitempty"`
Nonce *uint64 `json:"nonce,omitempty"` Nonce *uint64 `json:"nonce,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
} }
var dec PrestateAccount var dec account
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {
return err return err
} }
if dec.Balance != nil { if dec.Balance != nil {
p.Balance = (*big.Int)(dec.Balance) a.Balance = (*big.Int)(dec.Balance)
} }
if dec.Code != nil { if dec.Code != nil {
p.Code = *dec.Code a.Code = *dec.Code
} }
if dec.CodeHash != nil { if dec.CodeHash != nil {
p.CodeHash = dec.CodeHash a.CodeHash = dec.CodeHash
} }
if dec.Nonce != nil { if dec.Nonce != nil {
p.Nonce = *dec.Nonce a.Nonce = *dec.Nonce
} }
if dec.Storage != nil { if dec.Storage != nil {
p.Storage = dec.Storage a.Storage = dec.Storage
} }
return nil return nil
} }

View file

@ -35,15 +35,15 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
//go:generate go run github.com/fjl/gencodec -type PrestateAccount -field-override accountMarshaling -out gen_account_json.go //go:generate go run github.com/fjl/gencodec -type account -field-override accountMarshaling -out gen_account_json.go
func init() { func init() {
tracers.DefaultDirectory.Register("prestateTracer", newPrestateTracer, false) tracers.DefaultDirectory.Register("prestateTracer", newPrestateTracer, false)
} }
type stateMap = map[common.Address]*PrestateAccount type stateMap = map[common.Address]*account
type PrestateAccount struct { type account struct {
Balance *big.Int `json:"balance,omitempty"` Balance *big.Int `json:"balance,omitempty"`
Code []byte `json:"code,omitempty"` Code []byte `json:"code,omitempty"`
CodeHash *common.Hash `json:"codeHash,omitempty"` CodeHash *common.Hash `json:"codeHash,omitempty"`
@ -52,7 +52,7 @@ type PrestateAccount struct {
empty bool empty bool
} }
func (a *PrestateAccount) exists() bool { func (a *account) exists() bool {
return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0) return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0)
} }
@ -253,7 +253,7 @@ func (t *prestateTracer) processDiffState() {
continue continue
} }
modified := false modified := false
postAccount := &PrestateAccount{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)
newCodeHash := t.env.StateDB.GetCodeHash(addr) newCodeHash := t.env.StateDB.GetCodeHash(addr)
@ -323,7 +323,7 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
return return
} }
acc := &PrestateAccount{ acc := &account{
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),