mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-02 02:42:31 +00:00

* [NOD-1500] Added Domain type and Constructor * [NOD-1500] Replaced dag+txpool with domain in flowContext * [NOD-1500] Replaced dag+txpool with domain in flowContext * [NOD-1500] Converters: domain objects from/to appmessage * [NOD-1500] Convert hashes to DomainHashes in appmessages * [NOD-1500] Remove references to daghash in dagconfig * [NOD-1500] Fixed all appmessage usages of hashes * [NOD-1500] Update all RPC to use domain * [NOD-1500] Big chunk of protocol flows re-wired to domain * [NOD-1500] Finished re-wiring all protocol flows to new Domain * [NOD-1500] Fix some mempool and kaspaminer compilation errors * [NOD-1500] Deleted util/{block,tx,daghash} and dbaccess * [NOD-1500] util.CoinbaseTransactionIndex -> transactionhelper.CoinbaseTransactionIndex * [NOD-1500] Fix txsigner * [NOD-1500] Removed all references to util/subnetworkid * [NOD-1500] Update RpcGetBlock related messages * [NOD-1500] Many more compilation fixes * [NOD-1500] Return full list of missing blocks for orphan resolution * [NOD-1500] Fixed handshake * [NOD-1500] Fixed flowcontext compilation * [NOD-1500] Update users of StartIBDIfRequired to handle error * [NOD-1500] Removed some more fields from RPC * [NOD-1500] Fix the getBlockTemplate flow * [NOD-1500] Fix HandleGetCurrentNetwork * [NOD-1500] Remove redundant code * [NOD-1500] Remove obsolete notifications * [NOD-1500] Split MiningManager and Consensus to separate fields in Domain * [NOD-1500] Update two wrong references to location of txscript * [NOD-1500] Added comments * [NOD-1500] Fix some tests * [NOD-1500] Removed serialization logic from appmessage * [NOD-1500] Rename database/serialization/messages.proto to dbobjects.proto * [NOD-1500] Delete integration tests * [NOD-1500] Remove txsort * [NOD-1500] Fix tiny bug * [NOD-1500] Remove rogue dependancy on bchd * [NOD-1500] Some stylistic fixes
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package ibd
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/app/protocol/protocolerrors"
|
|
"github.com/kaspanet/kaspad/domain"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
|
|
)
|
|
|
|
const ibdBatchSize = router.DefaultMaxMessages
|
|
|
|
// RequestIBDBlocksContext is the interface for the context needed for the HandleRequestIBDBlocks flow.
|
|
type RequestIBDBlocksContext interface {
|
|
Domain() domain.Domain
|
|
}
|
|
|
|
type handleRequestBlocksFlow struct {
|
|
RequestIBDBlocksContext
|
|
incomingRoute, outgoingRoute *router.Route
|
|
}
|
|
|
|
// HandleRequestIBDBlocks handles getBlocks messages
|
|
func HandleRequestIBDBlocks(context RequestIBDBlocksContext, incomingRoute *router.Route, outgoingRoute *router.Route) error {
|
|
flow := &handleRequestBlocksFlow{
|
|
RequestIBDBlocksContext: context,
|
|
incomingRoute: incomingRoute,
|
|
outgoingRoute: outgoingRoute,
|
|
}
|
|
return flow.start()
|
|
}
|
|
|
|
func (flow *handleRequestBlocksFlow) start() error {
|
|
for {
|
|
lowHash, highHash, err := receiveRequestIBDBlocks(flow.incomingRoute)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
msgIBDBlocks, err := flow.buildMsgIBDBlocks(lowHash, highHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for offset := 0; offset < len(msgIBDBlocks); offset += ibdBatchSize {
|
|
end := offset + ibdBatchSize
|
|
if end > len(msgIBDBlocks) {
|
|
end = len(msgIBDBlocks)
|
|
}
|
|
|
|
blocksToSend := msgIBDBlocks[offset:end]
|
|
err = flow.sendMsgIBDBlocks(blocksToSend)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
// Exit the loop and don't wait for the GetNextIBDBlocks message if the last batch was
|
|
// less than ibdBatchSize.
|
|
if len(blocksToSend) < ibdBatchSize {
|
|
break
|
|
}
|
|
|
|
message, err := flow.incomingRoute.Dequeue()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, ok := message.(*appmessage.MsgRequestNextIBDBlocks); !ok {
|
|
return protocolerrors.Errorf(true, "received unexpected message type. "+
|
|
"expected: %s, got: %s", appmessage.CmdRequestNextIBDBlocks, message.Command())
|
|
}
|
|
}
|
|
err = flow.outgoingRoute.Enqueue(appmessage.NewMsgDoneIBDBlocks())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
func receiveRequestIBDBlocks(incomingRoute *router.Route) (lowHash *externalapi.DomainHash,
|
|
highHash *externalapi.DomainHash, err error) {
|
|
|
|
message, err := incomingRoute.Dequeue()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
msgRequestIBDBlocks := message.(*appmessage.MsgRequestIBDBlocks)
|
|
|
|
return msgRequestIBDBlocks.LowHash, msgRequestIBDBlocks.HighHash, nil
|
|
}
|
|
|
|
func (flow *handleRequestBlocksFlow) buildMsgIBDBlocks(lowHash *externalapi.DomainHash,
|
|
highHash *externalapi.DomainHash) ([]*appmessage.MsgIBDBlock, error) {
|
|
|
|
blockHashes, err := flow.Domain().Consensus().GetHashesBetween(lowHash, highHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
const maxHashesInMsgIBDBlocks = appmessage.MaxInvPerMsg
|
|
if len(blockHashes) > maxHashesInMsgIBDBlocks {
|
|
blockHashes = blockHashes[:maxHashesInMsgIBDBlocks]
|
|
}
|
|
|
|
msgIBDBlocks := make([]*appmessage.MsgIBDBlock, len(blockHashes))
|
|
for i, blockHash := range blockHashes {
|
|
block, err := flow.Domain().Consensus().GetBlock(blockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
msgIBDBlocks[i] = appmessage.NewMsgIBDBlock(appmessage.DomainBlockToMsgBlock(block))
|
|
}
|
|
|
|
return msgIBDBlocks, nil
|
|
}
|
|
|
|
func (flow *handleRequestBlocksFlow) sendMsgIBDBlocks(msgIBDBlocks []*appmessage.MsgIBDBlock) error {
|
|
for _, msgIBDBlock := range msgIBDBlocks {
|
|
err := flow.outgoingRoute.Enqueue(msgIBDBlock)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|