Skip to content

Commit

Permalink
Add zero tracer
Browse files Browse the repository at this point in the history
Zero tracer is a specific type of transaction tracer that records states read and written by a transaction.
  • Loading branch information
cffls committed Feb 15, 2024
1 parent 429a732 commit 5fda835
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 11 deletions.
11 changes: 8 additions & 3 deletions cmd/rpcdaemon/commands/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
txns = append(txns, borTx)
}

cumulativeGas := uint64(0)

for idx, txn := range txns {
stream.WriteObjectStart()
stream.WriteObjectField("result")
Expand All @@ -141,9 +143,12 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}

txCtx := evmtypes.TxContext{
TxHash: txn.Hash(),
Origin: msg.From(),
GasPrice: msg.GasPrice(),
TxHash: txn.Hash(),
Origin: msg.From(),
GasPrice: msg.GasPrice(),
Txn: txn,
CumulativeGasUsed: &cumulativeGas,
BlockNum: block.NumberU64(),
}

if borTx != nil && idx == len(txns)-1 {
Expand Down
56 changes: 56 additions & 0 deletions core/types/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Trace types for sending proof information to a zk prover as defined in https://github.com/0xPolygonZero/proof-protocol-decoder.
package types

import (
"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
)

type HexBytes []byte

func (b HexBytes) MarshalText() ([]byte, error) {
return hexutility.Bytes(b[:]).MarshalText()
}

type ContractCodeUsage struct {
Read *libcommon.Hash `json:"read,omitempty"`
Write HexBytes `json:"write,omitempty"`
}

type TxnTrace struct {
Balance *uint256.Int `json:"balance,omitempty"`
Nonce *uint256.Int `json:"nonce,omitempty"`
StorageRead []libcommon.Hash `json:"storage_read,omitempty"`
StorageWritten map[libcommon.Hash]*uint256.Int `json:"storage_written,omitempty"`
CodeUsage *ContractCodeUsage `json:"code_usage,omitempty"`
SelfDestructed *bool `json:"self_destructed,omitempty"`
StorageReadMap map[libcommon.Hash]struct{} `json:"-"`
}

type TxnMeta struct {
ByteCode HexBytes `json:"byte_code,omitempty"`
NewTxnTrieNode HexBytes `json:"new_txn_trie_node_byte,omitempty"`
NewReceiptTrieNode HexBytes `json:"new_receipt_trie_node_byte,omitempty"`
GasUsed uint64 `json:"gas_used,omitempty"`
}

type TxnInfo struct {
Traces map[libcommon.Address]*TxnTrace `json:"traces,omitempty"`
Meta TxnMeta `json:"meta,omitempty"`
}

type BlockUsedCodeHashes []libcommon.Hash

type CombinedPreImages struct {
Compact HexBytes `json:"compact,omitempty"`
}

type TriePreImage struct {
Combined CombinedPreImages `json:"combined,omitempty"`
}

type BlockTrace struct {
TriePreImage TriePreImage `json:"trie_pre_images,omitempty"`
TxnInfo []TxnInfo `json:"txn_info,omitempty"`
}
12 changes: 8 additions & 4 deletions core/vm/evmtypes/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ type BlockContext struct {
// All fields can change between transactions.
type TxContext struct {
// Message information
TxHash libcommon.Hash
Origin libcommon.Address // Provides information for ORIGIN
GasPrice *uint256.Int // Provides information for GASPRICE
DataHashes []libcommon.Hash // Provides versioned data hashes for DATAHASH
TxHash libcommon.Hash
Origin libcommon.Address // Provides information for ORIGIN
GasPrice *uint256.Int // Provides information for GASPRICE
DataHashes []libcommon.Hash // Provides versioned data hashes for DATAHASH
Txn types.Transaction
CumulativeGasUsed *uint64
BlockNum uint64
}

type (
Expand Down Expand Up @@ -101,4 +104,5 @@ type IntraBlockState interface {
Snapshot() int

AddLog(*types.Log)
GetLogs(hash libcommon.Hash) []*types.Log
}
Loading

0 comments on commit 5fda835

Please sign in to comment.