Add found to GetBlock (#2165)

This commit is contained in:
Ori Newman 2022-11-16 23:48:05 +02:00 committed by GitHub
parent a3387a56b3
commit 7d44275eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 90 additions and 66 deletions

View File

@ -27,18 +27,15 @@ func HandleIBDBlockRequests(context HandleIBDBlockRequestsContext, incomingRoute
log.Debugf("Got request for %d ibd blocks", len(msgRequestIBDBlocks.Hashes))
for i, hash := range msgRequestIBDBlocks.Hashes {
// Fetch the block from the database.
blockInfo, err := context.Domain().Consensus().GetBlockInfo(hash)
if err != nil {
return err
}
if !blockInfo.HasBody() {
return protocolerrors.Errorf(true, "block %s not found (v5)", hash)
}
block, err := context.Domain().Consensus().GetBlock(hash)
block, found, err := context.Domain().Consensus().GetBlock(hash)
if err != nil {
return errors.Wrapf(err, "unable to fetch requested block hash %s", hash)
}
if !found {
return protocolerrors.Errorf(false, "IBD block %s not found", hash)
}
// TODO (Partial nodes): Convert block to partial block if needed
blockMessage := appmessage.DomainBlockToMsgBlock(block)

View File

@ -119,11 +119,15 @@ func HandlePruningPointAndItsAnticoneRequests(context PruningPointAndItsAnticone
}
for i, blockHash := range pointAndItsAnticone {
block, err := context.Domain().Consensus().GetBlock(blockHash)
block, found, err := context.Domain().Consensus().GetBlock(blockHash)
if err != nil {
return err
}
if !found {
return protocolerrors.Errorf(false, "pruning point anticone block %s not found", blockHash)
}
err = outgoingRoute.Enqueue(appmessage.DomainBlockWithTrustedDataToBlockWithTrustedDataV4(block, trustedDataDAABlockIndexes[*blockHash], trustedDataGHOSTDAGDataIndexes[*blockHash]))
if err != nil {
return err

View File

@ -28,18 +28,15 @@ func HandleRelayBlockRequests(context RelayBlockRequestsContext, incomingRoute *
log.Debugf("Got request for relay blocks with hashes %s", getRelayBlocksMessage.Hashes)
for _, hash := range getRelayBlocksMessage.Hashes {
// Fetch the block from the database.
blockInfo, err := context.Domain().Consensus().GetBlockInfo(hash)
if err != nil {
return err
}
if !blockInfo.HasBody() {
return protocolerrors.Errorf(true, "block %s not found", hash)
}
block, err := context.Domain().Consensus().GetBlock(hash)
block, found, err := context.Domain().Consensus().GetBlock(hash)
if err != nil {
return errors.Wrapf(err, "unable to fetch requested block hash %s", hash)
}
if !found {
return protocolerrors.Errorf(false, "Relay block %s not found", hash)
}
// TODO (Partial nodes): Convert block to partial block if needed
err = outgoingRoute.Enqueue(appmessage.DomainBlockToMsgBlock(block))

View File

@ -211,10 +211,14 @@ func (flow *handleRelayInvsFlow) start() error {
continue
}
virtualHasNewParents = true
block, err := flow.Domain().Consensus().GetBlock(parent)
block, found, err := flow.Domain().Consensus().GetBlock(parent)
if err != nil {
return err
}
if !found {
return protocolerrors.Errorf(false, "Virtual parent %s not found", parent)
}
blockHash := consensushashing.BlockHash(block)
log.Debugf("Relaying block %s", blockHash)
err = flow.relayBlock(block)

View File

@ -121,7 +121,7 @@ func testEstimateMassIncreaseForSignaturesSetUp(t *testing.T, consensusConfig *c
t.Fatalf("AddBlock: %+v", err)
}
block1, err := tc.GetBlock(block1Hash)
block1, _, err := tc.GetBlock(block1Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}

View File

@ -84,7 +84,7 @@ func TestMultisig(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block1, err := tc.GetBlock(block1Hash)
block1, _, err := tc.GetBlock(block1Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -245,7 +245,7 @@ func TestP2PK(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block1, err := tc.GetBlock(block1Hash)
block1, _, err := tc.GetBlock(block1Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -377,17 +377,17 @@ func TestMaxSompi(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
fundingBlock2, err := tc.GetBlock(fundingBlock2Hash)
fundingBlock2, _, err := tc.GetBlock(fundingBlock2Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
fundingBlock3, err := tc.GetBlock(fundingBlock3Hash)
fundingBlock3, _, err := tc.GetBlock(fundingBlock3Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
fundingBlock4, err := tc.GetBlock(fundingBlock4Hash)
fundingBlock4, _, err := tc.GetBlock(fundingBlock4Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -397,7 +397,7 @@ func TestMaxSompi(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block1, err := tc.GetBlock(block1Hash)
block1, _, err := tc.GetBlock(block1Hash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}

View File

@ -357,7 +357,7 @@ func (s *consensus) ValidateTransactionAndPopulateWithConsensusData(transaction
stagingArea, transaction, model.VirtualBlockHash)
}
func (s *consensus) GetBlock(blockHash *externalapi.DomainHash) (*externalapi.DomainBlock, error) {
func (s *consensus) GetBlock(blockHash *externalapi.DomainHash) (*externalapi.DomainBlock, bool, error) {
s.lock.Lock()
defer s.lock.Unlock()
@ -366,11 +366,11 @@ func (s *consensus) GetBlock(blockHash *externalapi.DomainHash) (*externalapi.Do
block, err := s.blockStore.Block(s.databaseContext, stagingArea, blockHash)
if err != nil {
if errors.Is(err, database.ErrNotFound) {
return nil, errors.Wrapf(err, "block %s does not exist", blockHash)
return nil, false, nil
}
return nil, err
return nil, false, err
}
return block, nil
return block, true, nil
}
func (s *consensus) GetBlockEvenIfHeaderOnly(blockHash *externalapi.DomainHash) (*externalapi.DomainBlock, error) {

View File

@ -263,7 +263,7 @@ func TestBoundedMergeDepth(t *testing.T) {
continue
}
block, err := tcSyncer.GetBlock(blocksHash)
block, _, err := tcSyncer.GetBlock(blocksHash)
if err != nil {
t.Fatalf("GetBlockHeader: %+v", err)
}

View File

@ -13,7 +13,7 @@ type Consensus interface {
ValidatePruningPointProof(pruningPointProof *PruningPointProof) error
ApplyPruningPointProof(pruningPointProof *PruningPointProof) error
GetBlock(blockHash *DomainHash) (*DomainBlock, error)
GetBlock(blockHash *DomainHash) (*DomainBlock, bool, error)
GetBlockEvenIfHeaderOnly(blockHash *DomainHash) (*DomainBlock, error)
GetBlockHeader(blockHash *DomainHash) (BlockHeader, error)
GetBlockInfo(blockHash *DomainHash) (*BlockInfo, error)

View File

@ -93,7 +93,7 @@ func TestValidateAndInsertImportedPruningPoint(t *testing.T) {
}
for _, blockHash := range pruningPointAndItsAnticone {
block, err := tcSyncer.GetBlock(blockHash)
block, _, err := tcSyncer.GetBlock(blockHash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -268,7 +268,7 @@ func TestValidateAndInsertImportedPruningPoint(t *testing.T) {
}
for _, blocksHash := range missingBlockHashes {
block, err := tcSyncer.GetBlock(blocksHash)
block, _, err := tcSyncer.GetBlock(blocksHash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -294,7 +294,7 @@ func TestValidateAndInsertImportedPruningPoint(t *testing.T) {
}
tipHash := addBlock(tcSyncer, syncerTips, t)
tip, err := tcSyncer.GetBlock(tipHash)
tip, _, err := tcSyncer.GetBlock(tipHash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}
@ -341,7 +341,7 @@ func TestValidateAndInsertImportedPruningPoint(t *testing.T) {
tipHash := consensusConfig.GenesisHash
for i := 0; i < numSharedBlocks; i++ {
tipHash = addBlock(tcSyncer, []*externalapi.DomainHash{tipHash}, t)
block, err := tcSyncer.GetBlock(tipHash)
block, _, err := tcSyncer.GetBlock(tipHash)
if err != nil {
t.Fatalf("GetBlock: %+v", err)
}

View File

@ -41,7 +41,7 @@ func TestCheckBlockIsNotPruned(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
beforePruningBlock, err := tc.GetBlock(tipHash)
beforePruningBlock, _, err := tc.GetBlock(tipHash)
if err != nil {
t.Fatalf("beforePruningBlock: %+v", err)
}
@ -199,7 +199,7 @@ func TestIsFinalizedTransaction(t *testing.T) {
t.Fatalf("Error getting block DAA score : %+v", err)
}
blockParents := block.Header.DirectParents()
parentToSpend, err := tc.GetBlock(blockParents[0])
parentToSpend, _, err := tc.GetBlock(blockParents[0])
if err != nil {
t.Fatalf("Error getting block1: %+v", err)
}

View File

@ -70,7 +70,7 @@ func TestChainedTransactions(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block1, err := tc.GetBlock(block1Hash)
block1, _, err := tc.GetBlock(block1Hash)
if err != nil {
t.Fatalf("Error getting block1: %+v", err)
}
@ -97,7 +97,7 @@ func TestChainedTransactions(t *testing.T) {
t.Fatalf("unexpected error %+v", err)
}
block2, err := tc.GetBlock(block2Hash)
block2, _, err := tc.GetBlock(block2Hash)
if err != nil {
t.Fatalf("Error getting block2: %+v", err)
}

View File

@ -40,7 +40,7 @@ func TestUTXOCommitment(t *testing.T) {
if err != nil {
t.Fatalf("Error creating block B: %+v", err)
}
blockB, err := consensus.GetBlock(blockBHash)
blockB, _, err := consensus.GetBlock(blockBHash)
if err != nil {
t.Fatalf("Error getting block B: %+v", err)
}
@ -73,7 +73,7 @@ func TestUTXOCommitment(t *testing.T) {
}
func checkBlockUTXOCommitment(t *testing.T, consensus testapi.TestConsensus, blockHash *externalapi.DomainHash, blockName string) {
block, err := consensus.GetBlock(blockHash)
block, _, err := consensus.GetBlock(blockHash)
if err != nil {
t.Fatalf("Error getting block %s: %+v", blockName, err)
}

View File

@ -40,7 +40,7 @@ func TestDoubleSpends(t *testing.T) {
if err != nil {
t.Fatalf("Error creating fundingBlock: %+v", err)
}
fundingBlock, err := consensus.GetBlock(fundingBlockHash)
fundingBlock, _, err := consensus.GetBlock(fundingBlockHash)
if err != nil {
t.Fatalf("Error getting fundingBlock: %+v", err)
}
@ -195,7 +195,7 @@ func TestTransactionAcceptance(t *testing.T) {
}
}
lastBlockInChain := chainTipHash
blockC, err := testConsensus.GetBlock(blockHashC)
blockC, _, err := testConsensus.GetBlock(blockHashC)
if err != nil {
t.Fatalf("Error getting blockC: %+v", err)
}
@ -269,22 +269,27 @@ func TestTransactionAcceptance(t *testing.T) {
if err != nil {
t.Fatalf("Error getting acceptance data: %+v", err)
}
blueChildOfRedBlock, err := testConsensus.GetBlock(hashBlueChildOfRedBlock)
blueChildOfRedBlock, _, err := testConsensus.GetBlock(hashBlueChildOfRedBlock)
if err != nil {
t.Fatalf("Error getting blueChildOfRedBlock: %+v", err)
}
blockE, err := testConsensus.GetBlock(blockHashF)
blockE, _, err := testConsensus.GetBlock(blockHashF)
if err != nil {
t.Fatalf("Error getting blockE: %+v", err)
}
redBlock, err := testConsensus.GetBlock(redHash)
redBlock, _, err := testConsensus.GetBlock(redHash)
if err != nil {
t.Fatalf("Error getting redBlock: %+v", err)
}
_, err = testConsensus.GetBlock(blockHashG)
_, found, err := testConsensus.GetBlock(blockHashG)
if err != nil {
t.Fatalf("Error getting blockF: %+v", err)
t.Fatalf("Error getting blockG: %+v", err)
}
if !found {
t.Fatalf("block G is missing")
}
updatedDAAScoreVirtualBlock := consensusConfig.GenesisBlock.Header.DAAScore() + 26
//We expect the second transaction in the "blue block" (blueChildOfRedBlock) to be accepted because the merge set is ordered topologically
//and the red block is ordered topologically before the "blue block" so the input is known in the UTXOSet.

View File

@ -318,12 +318,12 @@ func TestSigningTwoInputs(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block2, err := tc.GetBlock(block2Hash)
block2, _, err := tc.GetBlock(block2Hash)
if err != nil {
t.Fatalf("Error getting block2: %+v", err)
}
block3, err := tc.GetBlock(block3Hash)
block3, _, err := tc.GetBlock(block3Hash)
if err != nil {
t.Fatalf("Error getting block3: %+v", err)
}
@ -444,12 +444,12 @@ func TestSigningTwoInputsECDSA(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}
block2, err := tc.GetBlock(block2Hash)
block2, _, err := tc.GetBlock(block2Hash)
if err != nil {
t.Fatalf("Error getting block2: %+v", err)
}
block3, err := tc.GetBlock(block3Hash)
block3, _, err := tc.GetBlock(block3Hash)
if err != nil {
t.Fatalf("Error getting block3: %+v", err)
}

View File

@ -43,7 +43,7 @@ func TestCheckLockTimeVerifyConditionedByDAAScore(t *testing.T) {
if err != nil {
t.Fatalf("Error creating blockD: %v", err)
}
blockD, err := testConsensus.GetBlock(blockDHash)
blockD, _, err := testConsensus.GetBlock(blockDHash)
if err != nil {
t.Fatalf("Failed getting blockD: %v", err)
}
@ -150,7 +150,7 @@ func TestCheckLockTimeVerifyConditionedByDAAScoreWithWrongLockTime(t *testing.T)
if err != nil {
t.Fatalf("Error creating blockD: %v", err)
}
blockD, err := testConsensus.GetBlock(blockDHash)
blockD, _, err := testConsensus.GetBlock(blockDHash)
if err != nil {
t.Fatalf("Failed getting blockD: %v", err)
}
@ -252,7 +252,7 @@ func TestCheckLockTimeVerifyConditionedByAbsoluteTime(t *testing.T) {
if err != nil {
t.Fatalf("Error creating blockD: %v", err)
}
blockD, err := testConsensus.GetBlock(blockDHash)
blockD, _, err := testConsensus.GetBlock(blockDHash)
if err != nil {
t.Fatalf("Failed getting blockD: %v", err)
}
@ -283,7 +283,7 @@ func TestCheckLockTimeVerifyConditionedByAbsoluteTime(t *testing.T) {
if err != nil {
t.Fatalf("Error creating blockE: %v", err)
}
blockE, err := testConsensus.GetBlock(blockEHash)
blockE, _, err := testConsensus.GetBlock(blockEHash)
if err != nil {
t.Fatalf("Failed getting blockE: %v", err)
}
@ -380,7 +380,7 @@ func TestCheckLockTimeVerifyConditionedByAbsoluteTimeWithWrongLockTime(t *testin
if err != nil {
t.Fatalf("Error creating blockD: %v", err)
}
blockD, err := testConsensus.GetBlock(blockDHash)
blockD, _, err := testConsensus.GetBlock(blockDHash)
if err != nil {
t.Fatalf("Failed getting blockD: %v", err)
}
@ -411,7 +411,7 @@ func TestCheckLockTimeVerifyConditionedByAbsoluteTimeWithWrongLockTime(t *testin
if err != nil {
t.Fatalf("Error creating blockE: %v", err)
}
blockE, err := testConsensus.GetBlock(blockEHash)
blockE, _, err := testConsensus.GetBlock(blockEHash)
if err != nil {
t.Fatalf("Failed getting blockE: %v", err)
}

View File

@ -2,6 +2,7 @@ package domain
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/pkg/errors"
"math"
)
@ -66,11 +67,15 @@ func syncConsensuses(syncer, syncee externalapi.Consensus) error {
}
for _, blockHash := range pruningPointAndItsAnticone {
block, err := syncer.GetBlock(blockHash)
block, found, err := syncer.GetBlock(blockHash)
if err != nil {
return err
}
if !found {
return errors.Errorf("block %s is missing", blockHash)
}
blockDAAWindowHashes, err := syncer.BlockDAAWindowHashes(blockHash)
if err != nil {
return err
@ -156,11 +161,14 @@ func syncConsensuses(syncer, syncee externalapi.Consensus) error {
continue
}
block, err := syncer.GetBlock(blocksHash)
block, found, err := syncer.GetBlock(blocksHash)
if err != nil {
return err
}
if !found {
return errors.Errorf("block %s is missing", blocksHash)
}
err = syncee.ValidateAndInsertBlock(block, false)
if err != nil {
return err

View File

@ -339,7 +339,7 @@ func TestOrphanTransactions(t *testing.T) {
t.Fatalf("AddBlock: %v", err)
}
blockParentsTransactions, err := tc.GetBlock(blockParentsTransactionsHash)
blockParentsTransactions, _, err := tc.GetBlock(blockParentsTransactionsHash)
if err != nil {
t.Fatalf("GetBlock: %v", err)
}
@ -648,7 +648,7 @@ func TestModifyBlockTemplate(t *testing.T) {
t.Fatalf("AddBlock: %v", err)
}
blockParentsTransactions, err := tc.GetBlock(blockParentsTransactionsHash)
blockParentsTransactions, _, err := tc.GetBlock(blockParentsTransactionsHash)
if err != nil {
t.Fatalf("GetBlock: %v", err)
}
@ -924,7 +924,7 @@ func createParentAndChildrenTransactions(tc testapi.TestConsensus) (txParent *ex
if err != nil {
return nil, nil, errors.Wrap(err, "AddBlock: ")
}
fundingBlockForParent, err := tc.GetBlock(fundingBlockHashForParent)
fundingBlockForParent, _, err := tc.GetBlock(fundingBlockHashForParent)
if err != nil {
return nil, nil, errors.Wrap(err, "GetBlock: ")
}
@ -949,7 +949,7 @@ func createChildAndParentTxsAndAddParentToConsensus(tc testapi.TestConsensus) (*
if err != nil {
return nil, errors.Wrap(err, "AddBlock: ")
}
ParentBlock, err := tc.GetBlock(ParentBlockHash)
ParentBlock, _, err := tc.GetBlock(ParentBlockHash)
if err != nil {
return nil, errors.Wrap(err, "GetBlock: ")
}

View File

@ -105,7 +105,16 @@ func mineBlocks(consensusConfig *consensus.Config, rpcClient *rpc.Client, blockC
func mineOrFetchBlock(blockData JSONBlock, mdb *miningDB, testConsensus testapi.TestConsensus) (*externalapi.DomainBlock, error) {
hash := mdb.hashByID(blockData.ID)
if mdb.hashByID(blockData.ID) != nil {
return testConsensus.GetBlock(hash)
block, found, err := testConsensus.GetBlock(hash)
if err != nil {
return nil, err
}
if !found {
return nil, errors.Errorf("block %s is missing", hash)
}
return block, nil
}
parentHashes := make([]*externalapi.DomainHash, len(blockData.Parents))

View File

@ -44,7 +44,7 @@ func TestGenerateFastPruningIBDTest(t *testing.T) {
}
}
tip, err := tc.GetBlock(tipHash)
tip, _, err := tc.GetBlock(tipHash)
if err != nil {
t.Fatal(err)
}