mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-22 14:56:44 +00:00

* Test bounded merge depth * Fix a bug in GetBlockInfo, where trying to use reachability on an invalid block * Add a test to reproduce and test the GetBlockInfo bug
76 lines
2.6 KiB
Go
76 lines
2.6 KiB
Go
package consensus
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensusserialization"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
|
"github.com/pkg/errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestConsensus_GetBlockInfo(t *testing.T) {
|
|
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
|
|
|
factory := NewFactory()
|
|
consensus, teardown, err := factory.NewTestConsensus(params, "TestConsensus_GetBlockInfo")
|
|
if err != nil {
|
|
t.Fatalf("Error setting up consensus: %+v", err)
|
|
}
|
|
defer teardown()
|
|
|
|
invalidBlock, err := consensus.BuildBlockWithParents([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
invalidBlock.Header.TimeInMilliseconds = 0
|
|
err = consensus.ValidateAndInsertBlock(invalidBlock)
|
|
if !errors.Is(err, ruleerrors.ErrTimeTooOld) {
|
|
t.Fatalf("Expected block to be invalid with err: %v, instead found: %v", ruleerrors.ErrTimeTooOld, err)
|
|
}
|
|
|
|
info, err := consensus.GetBlockInfo(consensusserialization.BlockHash(invalidBlock))
|
|
if err != nil {
|
|
t.Fatalf("Failed to get block info: %v", err)
|
|
}
|
|
|
|
if !info.Exists {
|
|
t.Fatal("The block is missing")
|
|
}
|
|
if info.BlockStatus != externalapi.StatusInvalid {
|
|
t.Fatalf("Expected block status: %s, instead got: %s", externalapi.StatusInvalid, info.BlockStatus)
|
|
}
|
|
if info.IsBlockInHeaderPruningPointFuture != false {
|
|
t.Fatalf("Expected IsBlockInHeaderPruningPointFuture=false, instead found: %t", info.IsBlockInHeaderPruningPointFuture)
|
|
}
|
|
|
|
emptyCoinbase := externalapi.DomainCoinbaseData{}
|
|
validBlock, err := consensus.BuildBlock(&emptyCoinbase, nil)
|
|
if err != nil {
|
|
t.Fatalf("consensus.BuildBlock with an empty coinbase shouldn't fail: %v", err)
|
|
}
|
|
|
|
err = consensus.ValidateAndInsertBlock(validBlock)
|
|
if err != nil {
|
|
t.Fatalf("consensus.ValidateAndInsertBlock with a block straight from consensus.BuildBlock should not fail: %v", err)
|
|
}
|
|
|
|
info, err = consensus.GetBlockInfo(consensusserialization.BlockHash(validBlock))
|
|
if err != nil {
|
|
t.Fatalf("Failed to get block info: %v", err)
|
|
}
|
|
|
|
if !info.Exists {
|
|
t.Fatal("The block is missing")
|
|
}
|
|
if info.BlockStatus != externalapi.StatusValid {
|
|
t.Fatalf("Expected block status: %s, instead got: %s", externalapi.StatusValid, info.BlockStatus)
|
|
}
|
|
if info.IsBlockInHeaderPruningPointFuture != true {
|
|
t.Fatalf("Expected IsBlockInHeaderPruningPointFuture=true, instead found: %t", info.IsBlockInHeaderPruningPointFuture)
|
|
}
|
|
|
|
})
|
|
}
|