Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
implement GetTrieNode on responder side
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 committed Feb 28, 2024
1 parent 6b82b52 commit bbdf86d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 1 addition & 2 deletions p2p/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/dominant-strategies/go-quai/p2p"
quaiprotocol "github.com/dominant-strategies/go-quai/p2p/protocol"
"github.com/dominant-strategies/go-quai/quai"
"github.com/dominant-strategies/go-quai/trie"

"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -252,7 +251,7 @@ func (p *P2PNode) GetHeader(hash common.Hash, location common.Location) *types.H
panic("TODO: implement")
}

func (p *P2PNode) GetTrieNode(hash common.Hash, location common.Location) *trie.TrieNodeResponse {
func (p *P2PNode) GetTrieNode(hash common.Hash, location common.Location) ([]byte, error) {
return p.consensus.GetTrieNode(hash, location)
}

Expand Down
35 changes: 22 additions & 13 deletions p2p/protocol/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package protocol

import (
"errors"
"io"
"math/big"

Expand All @@ -12,6 +11,7 @@ import (
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/pb"
"github.com/dominant-strategies/go-quai/trie"
"github.com/pkg/errors"
)

// QuaiProtocolHandler handles all the incoming requests and responds with corresponding data
Expand Down Expand Up @@ -69,19 +69,19 @@ func handleRequest(quaiMsg *pb.QuaiRequestMessage, stream network.Stream, node Q
switch query.(type) {
case *common.Hash:
log.Global.WithFields(log.Fields{
"requestID": id,
"requestID": id,
"decodedType": decodedType,
"location": loc,
"hash": query,
"peer": stream.Conn().RemotePeer(),
"location": loc,
"hash": query,
"peer": stream.Conn().RemotePeer(),
}).Debug("Received request by hash to handle")
case *big.Int:
log.Global.WithFields(log.Fields{
"requestID": id,
"requestID": id,
"decodedType": decodedType,
"location": loc,
"hash": query,
"peer": stream.Conn().RemotePeer(),
"location": loc,
"hash": query,
"peer": stream.Conn().RemotePeer(),
}).Debug("Received request by number to handle")
default:
log.Global.Errorf("unsupported request input data field type: %T", query)
Expand Down Expand Up @@ -228,13 +228,22 @@ func handleBlockNumberRequest(id uint32, loc common.Location, number *big.Int, s
}

func handleTrieNodeRequest(id uint32, loc common.Location, hash common.Hash, stream network.Stream, node QuaiP2PNode) error {
trieNode := node.GetTrieNode(hash, loc)
trieNode, err := node.GetTrieNode(hash, loc)
if err != nil {
return errors.Wrapf(err, "error getting trie node for hash %s", hash)
}

if trieNode == nil {
log.Global.Tracef("trie node not found")
return nil
return errors.Errorf("trie node not found for hash %s", hash)
}

trieNodeResp := &trie.TrieNodeResponse{
NodeHash: hash,
NodeData: trieNode,
}

log.Global.Tracef("trie node found")
data, err := pb.EncodeQuaiResponse(id, loc, trieNode)
data, err := pb.EncodeQuaiResponse(id, loc, trieNodeResp)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions p2p/protocol/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"math/big"

"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/p2p/requestManager"
"github.com/dominant-strategies/go-quai/trie"
)

// interface required to join the quai protocol network
Expand All @@ -21,7 +20,7 @@ type QuaiP2PNode interface {
GetBlock(hash common.Hash, location common.Location) *types.Block
GetHeader(hash common.Hash, location common.Location) *types.Header
GetBlockHashByNumber(number *big.Int, location common.Location) *common.Hash
GetTrieNode(hash common.Hash, location common.Location) *trie.TrieNodeResponse
GetTrieNode(hash common.Hash, location common.Location) ([]byte, error)
GetRequestManager() requestManager.RequestManager
GetHostBackend() host.Host

Expand Down
3 changes: 1 addition & 2 deletions quai/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"

"github.com/dominant-strategies/go-quai/trie"
"github.com/libp2p/go-libp2p/core"
)

Expand All @@ -28,7 +27,7 @@ type ConsensusAPI interface {

// Asks the consensus backend to lookup a trie node by hash and location,
// and return the data in the trie node.
GetTrieNode(hash common.Hash, location common.Location) *trie.TrieNodeResponse
GetTrieNode(hash common.Hash, location common.Location) ([]byte, error)
}

// The networking backend will implement the following interface to enable consensus to communicate with other nodes.
Expand Down
17 changes: 12 additions & 5 deletions quai/p2p_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p"
"github.com/dominant-strategies/go-quai/rpc"
"github.com/dominant-strategies/go-quai/trie"
"github.com/pkg/errors"
)

// QuaiBackend implements the quai consensus protocol
Expand Down Expand Up @@ -102,10 +102,17 @@ func (qbe *QuaiBackend) OnNewBroadcast(sourcePeer p2p.PeerID, data interface{},
return true
}

// GetTrieNode returns the TrieNodeResponse for a given hash
func (qbe *QuaiBackend) GetTrieNode(hash common.Hash, location common.Location) *trie.TrieNodeResponse {
// Example/mock implementation
panic("todo")
// GetTrieNode returns the encoded RLP trie node for a given hash
func (qbe *QuaiBackend) GetTrieNode(hash common.Hash, location common.Location) ([]byte, error) {
backend := *qbe.GetBackend(location)
if backend == nil {
return nil, errors.Errorf("no backend found")
}
data, err := backend.ChainDb().Get(hash.Bytes())
if err != nil {
return nil, err
}
return data, nil
}

// Returns the current block height for the given location
Expand Down

0 comments on commit bbdf86d

Please sign in to comment.