Remove the limit to the returned headers from buildMsgBlockHeaders (#1281)

* Remove the limit to the returned header hashes from buildMsgBlockHeaders.

* Build msgBlockHeaders in batches rather than all at once.
This commit is contained in:
stasatdaglabs 2020-12-24 16:53:26 +02:00 committed by GitHub
parent 05941a76e7
commit bd97075e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 47 deletions

View File

@ -10,7 +10,6 @@ import (
)
const ibdBatchSize = router.DefaultMaxMessages
const maxHeaders = appmessage.MaxInvPerMsg
// RequestIBDBlocksContext is the interface for the context needed for the HandleRequestHeaders flow.
type RequestIBDBlocksContext interface {
@ -39,26 +38,35 @@ func (flow *handleRequestBlocksFlow) start() error {
return err
}
msgHeaders, err := flow.buildMsgBlockHeaders(lowHash, highHash)
blockHashes, err := flow.Domain().Consensus().GetHashesBetween(lowHash, highHash)
if err != nil {
return err
}
for offset := 0; offset < len(msgHeaders); offset += ibdBatchSize {
for offset := 0; offset < len(blockHashes); offset += ibdBatchSize {
end := offset + ibdBatchSize
if end > len(msgHeaders) {
end = len(msgHeaders)
if end > len(blockHashes) {
end = len(blockHashes)
}
blocksToSend := msgHeaders[offset:end]
err = flow.sendHeaders(blocksToSend)
blocksHashesToSend := blockHashes[offset:end]
msgBlockHeadersToSend := make([]*appmessage.MsgBlockHeader, len(blocksHashesToSend))
for i, blockHash := range blocksHashesToSend {
header, err := flow.Domain().Consensus().GetBlockHeader(blockHash)
if err != nil {
return err
}
msgBlockHeadersToSend[i] = appmessage.DomainBlockHeaderToBlockHeader(header)
}
err = flow.sendHeaders(msgBlockHeadersToSend)
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 {
if len(blocksHashesToSend) < ibdBatchSize {
break
}
@ -91,30 +99,6 @@ func receiveRequestHeaders(incomingRoute *router.Route) (lowHash *externalapi.Do
return msgRequestIBDBlocks.LowHash, msgRequestIBDBlocks.HighHash, nil
}
func (flow *handleRequestBlocksFlow) buildMsgBlockHeaders(lowHash *externalapi.DomainHash,
highHash *externalapi.DomainHash) ([]*appmessage.MsgBlockHeader, error) {
blockHashes, err := flow.Domain().Consensus().GetHashesBetween(lowHash, highHash)
if err != nil {
return nil, err
}
if len(blockHashes) > maxHeaders {
blockHashes = blockHashes[:maxHeaders]
}
msgBlockHeaders := make([]*appmessage.MsgBlockHeader, len(blockHashes))
for i, blockHash := range blockHashes {
header, err := flow.Domain().Consensus().GetBlockHeader(blockHash)
if err != nil {
return nil, err
}
msgBlockHeaders[i] = appmessage.DomainBlockHeaderToBlockHeader(header)
}
return msgBlockHeaders, nil
}
func (flow *handleRequestBlocksFlow) sendHeaders(headers []*appmessage.MsgBlockHeader) error {
for _, msgBlockHeader := range headers {
err := flow.outgoingRoute.Enqueue(msgBlockHeader)

View File

@ -1,15 +0,0 @@
package blockrelay
import (
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
"github.com/kaspanet/kaspad/domain/dagconfig"
"testing"
)
func TestMaxHeaders(t *testing.T) {
testutils.ForAllNets(t, false, func(t *testing.T, params *dagconfig.Params) {
if params.FinalityDepth() > maxHeaders {
t.Errorf("FinalityDepth() in %s should be lower or equal to appmessage.MaxInvPerMsg", params.Name)
}
})
}