mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-25 15:32: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
73 lines
2.0 KiB
Go
73 lines
2.0 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"
|
|
)
|
|
|
|
// RequestBlockLocatorContext is the interface for the context needed for the HandleRequestBlockLocator flow.
|
|
type RequestBlockLocatorContext interface {
|
|
DAG() *blockdag.BlockDAG
|
|
}
|
|
|
|
type handleRequestBlockLocatorFlow struct {
|
|
RequestBlockLocatorContext
|
|
incomingRoute, outgoingRoute *router.Route
|
|
}
|
|
|
|
// HandleRequestBlockLocator handles getBlockLocator messages
|
|
func HandleRequestBlockLocator(context RequestBlockLocatorContext, incomingRoute *router.Route,
|
|
outgoingRoute *router.Route) error {
|
|
|
|
flow := &handleRequestBlockLocatorFlow{
|
|
RequestBlockLocatorContext: context,
|
|
incomingRoute: incomingRoute,
|
|
outgoingRoute: outgoingRoute,
|
|
}
|
|
return flow.start()
|
|
}
|
|
|
|
func (flow *handleRequestBlockLocatorFlow) start() error {
|
|
for {
|
|
lowHash, highHash, err := flow.receiveGetBlockLocator()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
locator, err := flow.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 = flow.sendBlockLocator(locator)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
func (flow *handleRequestBlockLocatorFlow) receiveGetBlockLocator() (lowHash *daghash.Hash,
|
|
highHash *daghash.Hash, err error) {
|
|
|
|
message, err := flow.incomingRoute.Dequeue()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
msgGetBlockLocator := message.(*wire.MsgRequestBlockLocator)
|
|
|
|
return msgGetBlockLocator.LowHash, msgGetBlockLocator.HighHash, nil
|
|
}
|
|
|
|
func (flow *handleRequestBlockLocatorFlow) sendBlockLocator(locator blockdag.BlockLocator) error {
|
|
msgBlockLocator := wire.NewMsgBlockLocator(locator)
|
|
err := flow.outgoingRoute.Enqueue(msgBlockLocator)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|