Don't mark block that got rejected because of ruleerrors.ErrPrunedBlock as invalid (#1529)

* Don't mark block that got rejected because of ruleerrors.ErrPrunedBlock as invalid

* Update comment
This commit is contained in:
Ori Newman 2021-02-15 15:34:21 +02:00 committed by GitHub
parent 2a31074fc4
commit e78cdff3d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -53,12 +53,19 @@ func (bp *blockProcessor) validateBlock(block *externalapi.DomainBlock, isPrunin
err = bp.validatePostProofOfWork(block, isPruningPoint)
if err != nil {
if errors.As(err, &ruleerrors.RuleError{}) {
// If we got ErrMissingParents the block shouldn't be considered as invalid
// because it could be added later on when its parents are present, and if
// we get ErrBadMerkleRoot we shouldn't mark the block as invalid because
// later on we can get the block with transactions that fits the merkle
// root.
if !errors.As(err, &ruleerrors.ErrMissingParents{}) && !errors.Is(err, ruleerrors.ErrBadMerkleRoot) {
// We mark invalid blocks with status externalapi.StatusInvalid except in the
// case of the following errors:
// ErrMissingParents - If we got ErrMissingParents the block shouldn't be
// considered as invalid because it could be added later on when its
// parents are present.
// ErrBadMerkleRoot - if we get ErrBadMerkleRoot we shouldn't mark the
// block as invalid because later on we can get the block with
// transactions that fits the merkle root.
// ErrPrunedBlock - ErrPrunedBlock is an error that rejects a block body and
// not the block as a whole, so we shouldn't mark it as invalid.
if !errors.As(err, &ruleerrors.ErrMissingParents{}) &&
!errors.Is(err, ruleerrors.ErrBadMerkleRoot) &&
!errors.Is(err, ruleerrors.ErrPrunedBlock) {
// Discard all changes so we save only the block status
bp.discardAllChanges()
hash := consensushashing.BlockHash(block)

View File

@ -5,6 +5,7 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
"github.com/kaspanet/kaspad/domain/dagconfig"
@ -58,6 +59,17 @@ func TestCheckBlockIsNotPruned(t *testing.T) {
if !errors.Is(err, ruleerrors.ErrPrunedBlock) {
t.Fatalf("Unexpected error: %+v", err)
}
beforePruningBlockBlockStatus, err := tc.BlockStatusStore().Get(tc.DatabaseContext(),
consensushashing.BlockHash(beforePruningBlock))
if err != nil {
t.Fatalf("BlockStatusStore().Get: %+v", err)
}
// Check that the block still has header only status although it got rejected.
if beforePruningBlockBlockStatus != externalapi.StatusHeaderOnly {
t.Fatalf("Unexpected status %s", beforePruningBlockBlockStatus)
}
})
}