mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* Reject SubmitBlock if the node is in IBD * Add comments * Don't use iota for RejectReason constants, since in .proto those are hard-coded
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package integration
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/pow"
|
|
"github.com/kaspanet/kaspad/util/difficulty"
|
|
)
|
|
|
|
func solveBlock(block *externalapi.DomainBlock) *externalapi.DomainBlock {
|
|
targetDifficulty := difficulty.CompactToBig(block.Header.Bits())
|
|
headerForMining := block.Header.ToMutable()
|
|
initialNonce := rand.Uint64()
|
|
for i := initialNonce; i != initialNonce-1; i++ {
|
|
headerForMining.SetNonce(i)
|
|
if pow.CheckProofOfWorkWithTarget(headerForMining, targetDifficulty) {
|
|
block.Header = headerForMining.ToImmutable()
|
|
return block
|
|
}
|
|
}
|
|
|
|
panic("Failed to solve block! This should never happen")
|
|
}
|
|
|
|
func mineNextBlock(t *testing.T, harness *appHarness) *externalapi.DomainBlock {
|
|
blockTemplate, err := harness.rpcClient.GetBlockTemplate(harness.miningAddress)
|
|
if err != nil {
|
|
t.Fatalf("Error getting block template: %+v", err)
|
|
}
|
|
|
|
block := appmessage.MsgBlockToDomainBlock(blockTemplate.MsgBlock)
|
|
|
|
solveBlock(block)
|
|
|
|
_, err = harness.rpcClient.SubmitBlock(block)
|
|
if err != nil {
|
|
t.Fatalf("Error submitting block: %s", err)
|
|
}
|
|
|
|
return block
|
|
}
|