github.com/dim4egster/coreth@v0.10.2/plugin/evm/message/leafs_request.go (about) 1 // (c) 2021-2022, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package message 5 6 import ( 7 "context" 8 "fmt" 9 10 "github.com/dim4egster/qmallgo/ids" 11 "github.com/ethereum/go-ethereum/common" 12 "github.com/ethereum/go-ethereum/log" 13 ) 14 15 var _ Request = LeafsRequest{} 16 17 // NodeType outlines the trie that a leaf node belongs to 18 // handlers.LeafsRequestHandler uses this information to determine 19 // which of the two tries (state/atomic) to fetch the information from 20 type NodeType uint8 21 22 const ( 23 // StateTrieNode represents a leaf node that belongs to the coreth State trie 24 StateTrieNode NodeType = iota + 1 25 // AtomicTrieNode represents a leaf node that belongs to the coreth evm.AtomicTrie 26 AtomicTrieNode 27 ) 28 29 func (nt NodeType) String() string { 30 switch nt { 31 case StateTrieNode: 32 return "StateTrie" 33 case AtomicTrieNode: 34 return "AtomicTrie" 35 default: 36 return "Unknown" 37 } 38 } 39 40 // LeafsRequest is a request to receive trie leaves at specified Root within Start and End byte range 41 // Limit outlines maximum number of leaves to returns starting at Start 42 // NodeType outlines which trie to read from state/atomic. 43 type LeafsRequest struct { 44 Root common.Hash `serialize:"true"` 45 Account common.Hash `serialize:"true"` 46 Start []byte `serialize:"true"` 47 End []byte `serialize:"true"` 48 Limit uint16 `serialize:"true"` 49 NodeType NodeType `serialize:"true"` 50 } 51 52 func (l LeafsRequest) String() string { 53 return fmt.Sprintf( 54 "LeafsRequest(Root=%s, Account=%s, Start=%s, End=%s, Limit=%d, NodeType=%s)", 55 l.Root, l.Account, common.Bytes2Hex(l.Start), common.Bytes2Hex(l.End), l.Limit, l.NodeType, 56 ) 57 } 58 59 func (l LeafsRequest) Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, handler RequestHandler) ([]byte, error) { 60 switch l.NodeType { 61 case StateTrieNode: 62 return handler.HandleStateTrieLeafsRequest(ctx, nodeID, requestID, l) 63 case AtomicTrieNode: 64 return handler.HandleAtomicTrieLeafsRequest(ctx, nodeID, requestID, l) 65 } 66 67 log.Debug("node type is not recognised, dropping request", "nodeID", nodeID, "requestID", requestID, "nodeType", l.NodeType) 68 return nil, nil 69 } 70 71 // LeafsResponse is a response to a LeafsRequest 72 // Keys must be within LeafsRequest.Start and LeafsRequest.End and sorted in lexicographical order. 73 // 74 // ProofVals must be non-empty and contain a valid range proof unless the key-value pairs in the 75 // response are the entire trie. 76 // If the key-value pairs make up the entire trie, ProofVals should be empty since the root will be 77 // sufficient to prove that the leaves are included in the trie. 78 // 79 // More is a flag set in the client after verifying the response, which indicates if the last key-value 80 // pair in the response has any more elements to its right within the trie. 81 type LeafsResponse struct { 82 // Keys and Vals provides the key-value pairs in the trie in the response. 83 Keys [][]byte `serialize:"true"` 84 Vals [][]byte `serialize:"true"` 85 86 // More indicates if there are more leaves to the right of the last value in this response. 87 // 88 // This is not serialized since it is set in the client after verifying the response via 89 // VerifyRangeProof and determining if there are in fact more leaves to the right of the 90 // last value in this response. 91 More bool 92 93 // ProofVals contain the edge merkle-proofs for the range of keys included in the response. 94 // The keys for the proof are simply the keccak256 hashes of the values, so they are not included in the response to save bandwidth. 95 ProofVals [][]byte `serialize:"true"` 96 }