Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): implement debug_executionWitness API #30613

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions eth/api_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/stateless"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/ethapi"
Expand Down Expand Up @@ -443,3 +445,73 @@ func (api *DebugAPI) GetTrieFlushInterval() (string, error) {
}
return api.eth.blockchain.GetTrieFlushInterval().String(), nil
}

func (api *DebugAPI) ExecutionWitness(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*ExecutionWitness, error) {
block, err := api.eth.APIBackend.BlockByNumberOrHash(ctx, blockNrOrHash)
if err != nil {
return nil, fmt.Errorf("failed to retrieve block: %w", err)
}
if block == nil {
return nil, fmt.Errorf("block not found: %s", blockNrOrHash.String())
}

witness, err := generateWitness(api.eth.blockchain, block)
return ToExecutionWitness(witness), err
}

func generateWitness(blockchain *core.BlockChain, block *types.Block) (*stateless.Witness, error) {
witness, err := stateless.NewWitness(block.Header(), blockchain)
if err != nil {
return nil, fmt.Errorf("failed to create witness: %w", err)
}

parentHeader := witness.Headers[0]
statedb, err := blockchain.StateAt(parentHeader.Root)
if err != nil {
return nil, fmt.Errorf("failed to retrieve parent state: %w", err)
}

statedb.StartPrefetcher("debug_execution_witness", witness)
defer statedb.StopPrefetcher()

res, err := blockchain.Processor().Process(block, statedb, *blockchain.GetVMConfig())
if err != nil {
return nil, fmt.Errorf("failed to process block %d: %w", block.Number(), err)
}

if err := blockchain.Validator().ValidateState(block, statedb, res, false); err != nil {
return nil, fmt.Errorf("failed to validate block %d: %w", block.Number(), err)
}

return witness, nil
}

// ExecutionWitness is a witness json encoding for transferring across the network.
// In the future, we'll probably consider using the extWitness format instead for less overhead if performance becomes an issue.
// Currently using this format for ease of reading, parsing and compatibility across clients.
type ExecutionWitness struct {
Headers []*types.Header `json:"headers"`
Codes map[string]string `json:"codes"`
State map[string]string `json:"state"`
}

func transformMap(in map[string]struct{}) map[string]string {
out := make(map[string]string, len(in))
for item := range in {
bytes := []byte(item)
key := crypto.Keccak256Hash(bytes).Hex()
out[key] = hexutil.Encode(bytes)
}
return out
}

// ToExecutionWitness converts a witness to an execution witness format that is compatible with reth.
// keccak(node) => node
// keccak(bytecodes) => bytecodes
func ToExecutionWitness(w *stateless.Witness) *ExecutionWitness {
return &ExecutionWitness{
Headers: w.Headers,
Codes: transformMap(w.Codes),
State: transformMap(w.State),
}
}
33 changes: 33 additions & 0 deletions eth/api_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ package eth
import (
"bytes"
"fmt"
"math/big"
"reflect"
"slices"
"strings"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/triedb"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
)

var dumper = spew.ConfigState{Indent: " "}
Expand Down Expand Up @@ -224,3 +230,30 @@ func TestStorageRangeAt(t *testing.T) {
}
}
}

func TestExecutionWitness(t *testing.T) {
t.Parallel()

// Create a database pre-initialize with a genesis block
db := rawdb.NewMemoryDatabase()
gspec := &core.Genesis{
Config: params.TestChainConfig,
Alloc: types.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}},
}
chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil)

blockNum := 10
_, bs, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), blockNum, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some transactions to the test chain so that we have some more data in the witnesses other than the coinbase payment?

e.g.

tx, err := types.SignTx(types.NewTransaction(0, common.Address{0x00}, new(big.Int), params.TxGas, block.BaseFee(), nil), signer, key)
			if err != nil {
				panic(err)
			}
			block.AddTx(tx)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if _, err := chain.InsertChain(bs); err != nil {
panic(err)
}

block := chain.GetBlockByNumber(uint64(blockNum - 1))
require.NotNil(t, block)

witness, err := generateWitness(chain, block)
require.NoError(t, err)

_, _, err = core.ExecuteStateless(params.TestChainConfig, block, witness)
require.NoError(t, err)
}