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

* Add mutable and immutable header interfaces * Fix ShouldMine() * Remove false comment * Fix Equal signature * Fix Equal implementation
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package integration
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/pow"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/util"
|
|
)
|
|
|
|
func solveBlock(block *externalapi.DomainBlock) *externalapi.DomainBlock {
|
|
targetDifficulty := util.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
|
|
}
|