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)
continue
}
var accounts map[common.Address]*native.PrestateAccount
var accounts map[common.Address]*types.Account
if err := json.Unmarshal(blob, &accounts); err != nil {
log.Error("Failed to decode trace result", "blockNumber", blockNumber, "hash", tx.Hash(), "err", err)
continue

View file

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

View file

@ -35,15 +35,15 @@ import (
"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() {
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"`
Code []byte `json:"code,omitempty"`
CodeHash *common.Hash `json:"codeHash,omitempty"`
@ -52,7 +52,7 @@ type PrestateAccount struct {
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)
}
@ -253,7 +253,7 @@ func (t *prestateTracer) processDiffState() {
continue
}
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()
newNonce := t.env.StateDB.GetNonce(addr)
newCodeHash := t.env.StateDB.GetCodeHash(addr)
@ -323,7 +323,7 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
return
}
acc := &PrestateAccount{
acc := &account{
Balance: t.env.StateDB.GetBalance(addr).ToBig(),
Nonce: t.env.StateDB.GetNonce(addr),
Code: t.env.StateDB.GetCode(addr),