mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 14:16:43 +00:00
[NOD-1541] Add TestPastMedianTime (#1091)
This commit is contained in:
parent
f2df48139f
commit
7050ebeac9
@ -5,6 +5,7 @@ import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
||||
"github.com/kaspanet/kaspad/infrastructure/logger"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -75,6 +76,14 @@ func (bb *testBlockBuilder) buildBlockWithParents(
|
||||
|
||||
defer bb.testConsensus.DiscardAllStores()
|
||||
|
||||
if coinbaseData == nil {
|
||||
scriptPublicKey, _ := testutils.OpTrueScript()
|
||||
coinbaseData = &externalapi.DomainCoinbaseData{
|
||||
ScriptPublicKey: scriptPublicKey,
|
||||
ExtraData: []byte{},
|
||||
}
|
||||
}
|
||||
|
||||
err := bb.blockRelationStore.StageBlockRelation(tempBlockHash, &model.BlockRelations{Parents: parentHashes})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -0,0 +1,79 @@
|
||||
package pastmediantimemanager_test
|
||||
|
||||
import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
||||
"github.com/kaspanet/kaspad/domain/dagconfig"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPastMedianTime(t *testing.T) {
|
||||
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
||||
factory := consensus.NewFactory()
|
||||
tc, tearDown, err := factory.NewTestConsensus(params, "TestUpdateReindexRoot")
|
||||
if err != nil {
|
||||
t.Fatalf("NewTestConsensus: %s", err)
|
||||
}
|
||||
defer tearDown()
|
||||
|
||||
numBlocks := uint32(300)
|
||||
blockHashes := make([]*externalapi.DomainHash, numBlocks)
|
||||
blockHashes[0] = params.GenesisHash
|
||||
blockTime := params.GenesisBlock.Header.TimeInMilliseconds
|
||||
for i := uint32(1); i < numBlocks; i++ {
|
||||
blockTime += 1000
|
||||
block, err := tc.BuildBlockWithParents([]*externalapi.DomainHash{blockHashes[i-1]}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildBlockWithParents: %s", err)
|
||||
}
|
||||
|
||||
block.Header.TimeInMilliseconds = blockTime
|
||||
blockHash, err := tc.SolveAndAddBlock(block)
|
||||
if err != nil {
|
||||
t.Fatalf("SolveAndAddBlock: %s", err)
|
||||
}
|
||||
|
||||
blockHashes[i] = blockHash
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
blockNumber uint32
|
||||
expectedMillisecondsSinceGenesis int64
|
||||
}{
|
||||
{
|
||||
blockNumber: 263,
|
||||
expectedMillisecondsSinceGenesis: 130000,
|
||||
},
|
||||
{
|
||||
blockNumber: 271,
|
||||
expectedMillisecondsSinceGenesis: 138000,
|
||||
},
|
||||
{
|
||||
blockNumber: 241,
|
||||
expectedMillisecondsSinceGenesis: 108000,
|
||||
},
|
||||
{
|
||||
blockNumber: 5,
|
||||
expectedMillisecondsSinceGenesis: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
pastMedianTime, err := tc.PastMedianTimeManager().PastMedianTime(blockHashes[test.blockNumber])
|
||||
if err != nil {
|
||||
t.Fatalf("PastMedianTime: %s", err)
|
||||
}
|
||||
|
||||
millisecondsSinceGenesis := pastMedianTime -
|
||||
params.GenesisBlock.Header.TimeInMilliseconds
|
||||
|
||||
if millisecondsSinceGenesis != test.expectedMillisecondsSinceGenesis {
|
||||
t.Errorf("TestCalcPastMedianTime: expected past median time of block %v to be %v milliseconds "+
|
||||
"from genesis but got %v",
|
||||
test.blockNumber, test.expectedMillisecondsSinceGenesis, millisecondsSinceGenesis)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
@ -6,8 +6,6 @@ import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/consensusserialization"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/mining"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
)
|
||||
@ -38,15 +36,6 @@ func (tc *testConsensus) AddBlock(parentHashes []*externalapi.DomainHash, coinba
|
||||
tc.lock.Lock()
|
||||
defer tc.lock.Unlock()
|
||||
|
||||
scriptPublicKey, _ := testutils.OpTrueScript()
|
||||
|
||||
if coinbaseData == nil {
|
||||
coinbaseData = &externalapi.DomainCoinbaseData{
|
||||
ScriptPublicKey: scriptPublicKey,
|
||||
ExtraData: []byte{},
|
||||
}
|
||||
}
|
||||
|
||||
block, err := tc.testBlockBuilder.BuildBlockWithParents(parentHashes, coinbaseData, transactions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
x
Reference in New Issue
Block a user