kaspad/protocol/flows/ibd/handle_get_block_locator.go
Ori Newman 8e1958c20b
[NOD-1168] Add context interfaces for flows (#808)
* [NOD-1168] Add context interfaces to flows

* [NOD-1168] Move IBD state to protocol manager

* [NOD-1168] Move ready peers to protocol manager

* [NOD-1168] Add comments

* [NOD-1168] Separate context interfaces for send and receive pings

* [NOD-1168] Add protocol shared state to FlowContext

* [NOD-1168] Fix comment

* [NOD-1168] Rename Context->HandleHandshakeContext

* [NOD-1168] Initialize readyPeers and transactionsToRebroadcast

* [NOD-1168] Rename readyPeers -> peers
2020-07-21 18:02:33 +03:00

59 lines
1.6 KiB
Go

package ibd
import (
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/netadapter/router"
"github.com/kaspanet/kaspad/protocol/protocolerrors"
"github.com/kaspanet/kaspad/util/daghash"
"github.com/kaspanet/kaspad/wire"
)
// GetBlockLocatorContext is the interface for the context needed for the HandleGetBlockLocator flow.
type GetBlockLocatorContext interface {
DAG() *blockdag.BlockDAG
}
// HandleGetBlockLocator handles getBlockLocator messages
func HandleGetBlockLocator(context GetBlockLocatorContext, incomingRoute *router.Route,
outgoingRoute *router.Route) error {
for {
lowHash, highHash, err := receiveGetBlockLocator(incomingRoute)
if err != nil {
return err
}
locator, err := context.DAG().BlockLocatorFromHashes(highHash, lowHash)
if err != nil || len(locator) == 0 {
return protocolerrors.Errorf(true, "couldn't build a block "+
"locator between blocks %s and %s", lowHash, highHash)
}
err = sendBlockLocator(outgoingRoute, locator)
if err != nil {
return err
}
}
}
func receiveGetBlockLocator(incomingRoute *router.Route) (lowHash *daghash.Hash,
highHash *daghash.Hash, err error) {
message, err := incomingRoute.Dequeue()
if err != nil {
return nil, nil, err
}
msgGetBlockLocator := message.(*wire.MsgGetBlockLocator)
return msgGetBlockLocator.LowHash, msgGetBlockLocator.HighHash, nil
}
func sendBlockLocator(outgoingRoute *router.Route, locator blockdag.BlockLocator) error {
msgBlockLocator := wire.NewMsgBlockLocator(locator)
err := outgoingRoute.Enqueue(msgBlockLocator)
if err != nil {
return err
}
return nil
}