Move ExecutionWitness to API

pull/30613/head
Francis Li 4 months ago
parent f137c10f33
commit 12fdb1beb5
No known key found for this signature in database
GPG Key ID: 39F1A72C987F3F88
  1. 32
      core/stateless/encoding.go
  2. 34
      eth/api_debug.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),
}
}

@ -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),
}
}

Loading…
Cancel
Save