mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
Move ExecutionWitness to API
This commit is contained in:
parent
f137c10f33
commit
12fdb1beb5
2 changed files with 32 additions and 34 deletions
|
|
@ -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…
Reference in a new issue