mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
trie: move trie/nodeset -> trie/trienode/proofset
This commit is contained in:
parent
7cea9d4026
commit
7900aceebd
14 changed files with 56 additions and 49 deletions
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
||||
"github.com/ethereum/go-ethereum/internal/utesting"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
|
|
@ -529,11 +530,11 @@ func (s *Suite) snapGetAccountRange(t *utesting.T, tc *accRangeTest) error {
|
|||
for i, key := range hashes {
|
||||
keys[i] = common.CopyBytes(key[:])
|
||||
}
|
||||
nodes := make(trie.NodeList, len(proof))
|
||||
nodes := make(trienode.NodeList, len(proof))
|
||||
for i, node := range proof {
|
||||
nodes[i] = node
|
||||
}
|
||||
proofdb := nodes.NodeSet()
|
||||
proofdb := nodes.ProofSet()
|
||||
|
||||
var end []byte
|
||||
if len(keys) > 0 {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -320,7 +321,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
|
|||
it.Release()
|
||||
|
||||
// Generate the Merkle proofs for the first and last account
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
if err := tr.Prove(req.Origin[:], proof); err != nil {
|
||||
log.Warn("Failed to prove account range", "origin", req.Origin, "err", err)
|
||||
return nil, nil
|
||||
|
|
@ -426,7 +427,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
|
|||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
if err := stTrie.Prove(origin[:], proof); err != nil {
|
||||
log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err)
|
||||
return nil, nil
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/p2p/msgrate"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
|
|
@ -2393,11 +2394,11 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco
|
|||
for i, key := range hashes {
|
||||
keys[i] = common.CopyBytes(key[:])
|
||||
}
|
||||
nodes := make(trie.NodeList, len(proof))
|
||||
nodes := make(trienode.NodeList, len(proof))
|
||||
for i, node := range proof {
|
||||
nodes[i] = node
|
||||
}
|
||||
proofdb := nodes.NodeSet()
|
||||
proofdb := nodes.ProofSet()
|
||||
|
||||
var end []byte
|
||||
if len(keys) > 0 {
|
||||
|
|
@ -2638,7 +2639,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
|
|||
for j, key := range hashes[i] {
|
||||
keys[j] = common.CopyBytes(key[:])
|
||||
}
|
||||
nodes := make(trie.NodeList, 0, len(proof))
|
||||
nodes := make(trienode.NodeList, 0, len(proof))
|
||||
if i == len(hashes)-1 {
|
||||
for _, node := range proof {
|
||||
nodes = append(nodes, node)
|
||||
|
|
@ -2657,7 +2658,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
|
|||
} else {
|
||||
// A proof was attached, the response is only partial, check that the
|
||||
// returned data is indeed part of the storage trie
|
||||
proofdb := nodes.NodeSet()
|
||||
proofdb := nodes.ProofSet()
|
||||
|
||||
var end []byte
|
||||
if len(keys) > 0 {
|
||||
|
|
|
|||
|
|
@ -272,7 +272,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H
|
|||
// Unless we send the entire trie, we need to supply proofs
|
||||
// Actually, we need to supply proofs either way! This seems to be an implementation
|
||||
// quirk in go-ethereum
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
if err := t.accountTrie.Prove(origin[:], proof); err != nil {
|
||||
t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err)
|
||||
}
|
||||
|
|
@ -352,7 +352,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
|
|||
if originHash != (common.Hash{}) || (abort && len(keys) > 0) {
|
||||
// If we're aborting, we need to prove the first and last item
|
||||
// This terminates the response (and thus the loop)
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
stTrie := t.storageTries[account]
|
||||
|
||||
// Here's a potential gotcha: when constructing the proof, we cannot
|
||||
|
|
@ -410,7 +410,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
|
|||
if exit {
|
||||
// If we're aborting, we need to prove the first and last item
|
||||
// This terminates the response (and thus the loop)
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
stTrie := t.storageTries[account]
|
||||
|
||||
// Here's a potential gotcha: when constructing the proof, we cannot
|
||||
|
|
@ -598,7 +598,7 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
|
|||
vals = append(vals, entry.v)
|
||||
}
|
||||
// The proofs
|
||||
proof := trie.NewNodeSet()
|
||||
proof := trienode.NewProofSet()
|
||||
if err := t.accountTrie.Prove(origin[:], proof); err != nil {
|
||||
t.logger.Error("Could not prove origin", "origin", origin, "error", err)
|
||||
t.logger.Error("Could not prove origin", "origin", origin, "error", err)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/light"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
// clientHandler is responsible for receiving and processing all incoming server
|
||||
|
|
@ -237,7 +237,7 @@ func (h *clientHandler) handleMsg(p *serverPeer) error {
|
|||
p.Log().Trace("Received les/2 proofs response")
|
||||
var resp struct {
|
||||
ReqID, BV uint64
|
||||
Data trie.NodeList
|
||||
Data trienode.NodeList
|
||||
}
|
||||
if err := msg.Decode(&resp); err != nil {
|
||||
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error {
|
||||
|
|
@ -401,7 +402,7 @@ func testGetProofs(t *testing.T, protocol int) {
|
|||
bc := server.handler.blockchain
|
||||
|
||||
var proofreqs []ProofReq
|
||||
proofsV2 := trie.NewNodeSet()
|
||||
proofsV2 := trienode.NewProofSet()
|
||||
|
||||
accounts := []common.Address{bankAddr, userAddr1, userAddr2, signerAddr, {}}
|
||||
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
|
||||
|
|
@ -456,7 +457,7 @@ func testGetStaleProof(t *testing.T, protocol int) {
|
|||
|
||||
var expected []rlp.RawValue
|
||||
if wantOK {
|
||||
proofsV2 := trie.NewNodeSet()
|
||||
proofsV2 := trienode.NewProofSet()
|
||||
t, _ := trie.New(trie.StateTrieID(header.Root), server.backend.Blockchain().TrieDB())
|
||||
t.Prove(account, proofsV2)
|
||||
expected = proofsV2.NodeList()
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -222,9 +223,9 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||
if msg.MsgType != MsgProofsV2 {
|
||||
return errInvalidMessageType
|
||||
}
|
||||
proofs := msg.Obj.(trie.NodeList)
|
||||
proofs := msg.Obj.(trienode.NodeList)
|
||||
// Verify the proof and store if checks out
|
||||
nodeSet := proofs.NodeSet()
|
||||
nodeSet := proofs.ProofSet()
|
||||
reads := &readTraceDB{db: nodeSet}
|
||||
if _, err := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil {
|
||||
return fmt.Errorf("merkle proof verification failed: %v", err)
|
||||
|
|
@ -308,7 +309,7 @@ type HelperTrieReq struct {
|
|||
}
|
||||
|
||||
type HelperTrieResps struct { // describes all responses, not just a single one
|
||||
Proofs trie.NodeList
|
||||
Proofs trienode.NodeList
|
||||
AuxData [][]byte
|
||||
}
|
||||
|
||||
|
|
@ -356,7 +357,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||
if len(resp.AuxData) != 1 {
|
||||
return errInvalidEntryCount
|
||||
}
|
||||
nodeSet := resp.Proofs.NodeSet()
|
||||
nodeSet := resp.Proofs.ProofSet()
|
||||
headerEnc := resp.AuxData[0]
|
||||
if len(headerEnc) == 0 {
|
||||
return errHeaderUnavailable
|
||||
|
|
@ -451,7 +452,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||
}
|
||||
resps := msg.Obj.(HelperTrieResps)
|
||||
proofs := resps.Proofs
|
||||
nodeSet := proofs.NodeSet()
|
||||
nodeSet := proofs.ProofSet()
|
||||
reads := &readTraceDB{db: nodeSet}
|
||||
|
||||
r.BloomBits = make([][]byte, len(r.SectionIndexList))
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -900,7 +900,7 @@ func (p *clientPeer) replyReceiptsRLP(reqID uint64, receipts []rlp.RawValue) *re
|
|||
}
|
||||
|
||||
// replyProofsV2 creates a reply with a batch of merkle proofs, corresponding to the ones requested.
|
||||
func (p *clientPeer) replyProofsV2(reqID uint64, proofs trie.NodeList) *reply {
|
||||
func (p *clientPeer) replyProofsV2(reqID uint64, proofs trienode.NodeList) *reply {
|
||||
data, _ := rlp.EncodeToBytes(proofs)
|
||||
return &reply{p.rw, ProofsV2Msg, reqID, data}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/metrics"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
// serverBackend defines the backend functions needed for serving LES requests
|
||||
|
|
@ -378,7 +379,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
|
|||
err error
|
||||
)
|
||||
bc := backend.BlockChain()
|
||||
nodes := trie.NewNodeSet()
|
||||
nodes := trienode.NewProofSet()
|
||||
|
||||
for i, request := range r.Reqs {
|
||||
if i != 0 && !waitOrStop() {
|
||||
|
|
@ -463,7 +464,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
|
|||
auxData [][]byte
|
||||
)
|
||||
bc := backend.BlockChain()
|
||||
nodes := trie.NewNodeSet()
|
||||
nodes := trienode.NewProofSet()
|
||||
for i, request := range r.Reqs {
|
||||
if i != 0 && !waitOrStop() {
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/txpool"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
// NoOdr is the default context passed to an ODR capable function when the ODR
|
||||
|
|
@ -91,7 +91,7 @@ func StorageTrieID(state *TrieID, address common.Address, root common.Hash) *Tri
|
|||
type TrieRequest struct {
|
||||
Id *TrieID
|
||||
Key []byte
|
||||
Proof *trie.NodeSet
|
||||
Proof *trienode.ProofSet
|
||||
}
|
||||
|
||||
// StoreResult stores the retrieved data in local database
|
||||
|
|
@ -144,7 +144,7 @@ type ChtRequest struct {
|
|||
ChtRoot common.Hash
|
||||
Header *types.Header
|
||||
Td *big.Int
|
||||
Proof *trie.NodeSet
|
||||
Proof *trienode.ProofSet
|
||||
}
|
||||
|
||||
// StoreResult stores the retrieved data in local database
|
||||
|
|
@ -164,7 +164,7 @@ type BloomRequest struct {
|
|||
SectionIndexList []uint64
|
||||
BloomTrieRoot common.Hash
|
||||
BloomBits [][]byte
|
||||
Proofs *trie.NodeSet
|
||||
Proofs *trienode.ProofSet
|
||||
}
|
||||
|
||||
// StoreResult stores the retrieved data in local database
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -95,7 +96,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
nodes := trie.NewNodeSet()
|
||||
nodes := trienode.NewProofSet()
|
||||
t.Prove(req.Key, nodes)
|
||||
req.Proof = nodes
|
||||
case *CodeRequest:
|
||||
|
|
|
|||
|
|
@ -363,7 +363,7 @@ func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uin
|
|||
func (b *BloomTrieIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error {
|
||||
indexCh := make(chan uint, types.BloomBitLength)
|
||||
type res struct {
|
||||
nodes *trie.NodeSet
|
||||
nodes *trienode.ProofSet
|
||||
err error
|
||||
}
|
||||
resCh := make(chan res, types.BloomBitLength)
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ func (set *NodeSet) Merge(owner common.Hash, nodes map[string]*Node) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// AddLeaf adds the provided leaf node into set. TODO(rjl493456442) how can
|
||||
// AddLeaf adds the provided leaf node into set. TODNodeSetO(rjl493456442) how can
|
||||
// we get rid of it?
|
||||
func (set *NodeSet) AddLeaf(parent common.Hash, blob []byte) {
|
||||
set.Leaves = append(set.Leaves, &leaf{Blob: blob, Parent: parent})
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package trie
|
||||
package trienode
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
|
@ -26,9 +26,9 @@ import (
|
|||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
// NodeSet stores a set of trie nodes. It implements trie.Database and can also
|
||||
// ProofSet stores a set of trie nodes. It implements trie.Database and can also
|
||||
// act as a cache for another trie.Database.
|
||||
type NodeSet struct {
|
||||
type ProofSet struct {
|
||||
nodes map[string][]byte
|
||||
order []string
|
||||
|
||||
|
|
@ -36,15 +36,15 @@ type NodeSet struct {
|
|||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// NewNodeSet creates an empty node set
|
||||
func NewNodeSet() *NodeSet {
|
||||
return &NodeSet{
|
||||
// NewProofSet creates an empty node set
|
||||
func NewProofSet() *ProofSet {
|
||||
return &ProofSet{
|
||||
nodes: make(map[string][]byte),
|
||||
}
|
||||
}
|
||||
|
||||
// Put stores a new node in the set
|
||||
func (db *NodeSet) Put(key []byte, value []byte) error {
|
||||
func (db *ProofSet) Put(key []byte, value []byte) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ func (db *NodeSet) Put(key []byte, value []byte) error {
|
|||
}
|
||||
|
||||
// Delete removes a node from the set
|
||||
func (db *NodeSet) Delete(key []byte) error {
|
||||
func (db *ProofSet) Delete(key []byte) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ func (db *NodeSet) Delete(key []byte) error {
|
|||
}
|
||||
|
||||
// Get returns a stored node
|
||||
func (db *NodeSet) Get(key []byte) ([]byte, error) {
|
||||
func (db *ProofSet) Get(key []byte) ([]byte, error) {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
|
|
@ -81,13 +81,13 @@ func (db *NodeSet) Get(key []byte) ([]byte, error) {
|
|||
}
|
||||
|
||||
// Has returns true if the node set contains the given key
|
||||
func (db *NodeSet) Has(key []byte) (bool, error) {
|
||||
func (db *ProofSet) Has(key []byte) (bool, error) {
|
||||
_, err := db.Get(key)
|
||||
return err == nil, nil
|
||||
}
|
||||
|
||||
// KeyCount returns the number of nodes in the set
|
||||
func (db *NodeSet) KeyCount() int {
|
||||
func (db *ProofSet) KeyCount() int {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
|
|
@ -95,7 +95,7 @@ func (db *NodeSet) KeyCount() int {
|
|||
}
|
||||
|
||||
// DataSize returns the aggregated data size of nodes in the set
|
||||
func (db *NodeSet) DataSize() int {
|
||||
func (db *ProofSet) DataSize() int {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ func (db *NodeSet) DataSize() int {
|
|||
}
|
||||
|
||||
// NodeList converts the node set to a NodeList
|
||||
func (db *NodeSet) NodeList() NodeList {
|
||||
func (db *ProofSet) NodeList() NodeList {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
|
|
@ -115,7 +115,7 @@ func (db *NodeSet) NodeList() NodeList {
|
|||
}
|
||||
|
||||
// Store writes the contents of the set to the given database
|
||||
func (db *NodeSet) Store(target ethdb.KeyValueWriter) {
|
||||
func (db *ProofSet) Store(target ethdb.KeyValueWriter) {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
|
|
@ -134,9 +134,9 @@ func (n NodeList) Store(db ethdb.KeyValueWriter) {
|
|||
}
|
||||
}
|
||||
|
||||
// NodeSet converts the node list to a NodeSet
|
||||
func (n NodeList) NodeSet() *NodeSet {
|
||||
db := NewNodeSet()
|
||||
// ProofSet converts the node list to a ProofSet
|
||||
func (n NodeList) ProofSet() *ProofSet {
|
||||
db := NewProofSet()
|
||||
n.Store(db)
|
||||
return db
|
||||
}
|
||||
Loading…
Reference in a new issue