mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-24 15:02:32 +00:00

* [NOD-1191] Convert wire protocol to 100% protobuf * [NOD-1191] Simplify wire interface and remove redundant messages * [NOD-1191] Map all proto to wire conversions * [NOD-1203] Create netadapter outside of protocol manager * [NOD-1191] Fix nil errors * [NOD-1191] Fix comments * [NOD-1191] Add converter interface * [NOD-1191] Add missing GetBlockLocator message * [NOD-1191] Change message names that starts with 'get' to 'request' * [NOD-1191] Change message commands values * [NOD-1191] Remove redundant methods * [NOD-1191] Rename message constructors * [NOD-1191] Change message commands to use iota * [NOD-1191] Add missing outputs to protobuf conversion * [NOD-1191] Make block header a required field * [NOD-1191] Rename variables * [NOD-1212] Fix test names * [NOD-1191] Rename flow names * [NOD-1191] Fix infinite loop
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package blockrelay
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/blockdag"
|
|
"github.com/kaspanet/kaspad/netadapter/router"
|
|
peerpkg "github.com/kaspanet/kaspad/protocol/peer"
|
|
"github.com/kaspanet/kaspad/protocol/protocolerrors"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// RelayBlockRequestsContext is the interface for the context needed for the HandleRelayBlockRequests flow.
|
|
type RelayBlockRequestsContext interface {
|
|
DAG() *blockdag.BlockDAG
|
|
}
|
|
|
|
// HandleRelayBlockRequests listens to wire.MsgRequestRelayBlocks messages and sends
|
|
// their corresponding blocks to the requesting peer.
|
|
func HandleRelayBlockRequests(context RelayBlockRequestsContext, incomingRoute *router.Route,
|
|
outgoingRoute *router.Route, peer *peerpkg.Peer) error {
|
|
|
|
for {
|
|
message, err := incomingRoute.Dequeue()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
getRelayBlocksMessage := message.(*wire.MsgRequestRelayBlocks)
|
|
for _, hash := range getRelayBlocksMessage.Hashes {
|
|
// Fetch the block from the database.
|
|
block, err := context.DAG().BlockByHash(hash)
|
|
if blockdag.IsNotInDAGErr(err) {
|
|
return protocolerrors.Errorf(true, "block %s not found", hash)
|
|
} else if err != nil {
|
|
return errors.Wrapf(err, "unable to fetch requested block hash %s", hash)
|
|
}
|
|
msgBlock := block.MsgBlock()
|
|
|
|
// If we are a full node and the peer is a partial node, we must convert
|
|
// the block to a partial block.
|
|
nodeSubnetworkID := context.DAG().SubnetworkID()
|
|
peerSubnetworkID := peer.SubnetworkID()
|
|
|
|
isNodeFull := nodeSubnetworkID == nil
|
|
isPeerFull := peerSubnetworkID == nil
|
|
if isNodeFull && !isPeerFull {
|
|
msgBlock.ConvertToPartial(peerSubnetworkID)
|
|
}
|
|
|
|
err = outgoingRoute.Enqueue(msgBlock)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|