From 12fdb1beb5cd7fee4b1e6caacff1480945e92557 Mon Sep 17 00:00:00 2001 From: Francis Li Date: Thu, 17 Oct 2024 14:48:04 -0700 Subject: [PATCH] Move ExecutionWitness to API --- core/stateless/encoding.go | 32 -------------------------------- eth/api_debug.go | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/core/stateless/encoding.go b/core/stateless/encoding.go index e689769546..5f4cb0ea3c 100644 --- a/core/stateless/encoding.go +++ b/core/stateless/encoding.go @@ -19,9 +19,7 @@ package stateless import ( "io" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) @@ -76,33 +74,3 @@ type extWitness struct { Codes [][]byte State [][]byte } - -// 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 (w *Witness) ToExecutionWitness() *ExecutionWitness { - return &ExecutionWitness{ - Headers: w.Headers, - Codes: transformMap(w.Codes), - State: transformMap(w.State), - } -} diff --git a/eth/api_debug.go b/eth/api_debug.go index dce5bbf1ba..b8cd6409ef 100644 --- a/eth/api_debug.go +++ b/eth/api_debug.go @@ -446,7 +446,7 @@ func (api *DebugAPI) GetTrieFlushInterval() (string, error) { return api.eth.blockchain.GetTrieFlushInterval().String(), nil } -func (api *DebugAPI) ExecutionWitness(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*stateless.ExecutionWitness, error) { +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) @@ -456,7 +456,7 @@ func (api *DebugAPI) ExecutionWitness(ctx context.Context, blockNrOrHash rpc.Blo } witness, err := generateWitness(api.eth.blockchain, block) - return witness.ToExecutionWitness(), err + return ToExecutionWitness(witness), err } func generateWitness(blockchain *core.BlockChain, block *types.Block) (*stateless.Witness, error) { @@ -485,3 +485,33 @@ func generateWitness(blockchain *core.BlockChain, block *types.Block) (*stateles 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), + } +}