kaspad/util/difficulty/difficulty_test.go
Elichai Turkel dbf18d8052
Hard fork - new genesis with the utxo set of the last block (#1856)
* UTXO dump of block 0fca37ca667c2d550a6c4416dad9717e50927128c424fa4edbebc436ab13aeef

* Activate HF immediately and change reward to 1000

* Change protocol version and datadir location

* Delete comments

* Fix zero hash to muhash zero hash in genesis utxo dump check

* Don't omit genesis as direct parent

* Fix tests

* Change subsidy to 500

* Dont assume genesis multiset is empty

* Fix BlockReward test

* Fix TestValidateAndInsertImportedPruningPoint test

* Fix pruning point genesis utxo set

* Fix tests related to mainnet utxo set

* Dont change the difficulty before you have a full window

* Fix TestBlockWindow tests

* Remove global utxo set variable, and persist mainnetnet utxo deserialization between runs

* Fix last tests

* Make peer banning opt-in

* small fix for a test

* Fix go lint

* Fix Ori's review comments

* Change DAA score of genesis to checkpoint DAA score and fix all tests

* Fix the BlockLevel bits counting

* Fix some tests and make them run a little faster

* Change datadir name back to kaspa-mainnet and change db path from /data to /datadir

* Last changes for the release and change the version to 0.11.5

Co-authored-by: Ori Newman <orinewman1@gmail.com>
Co-authored-by: Ori Newman <>
Co-authored-by: msutton <mikisiton2@gmail.com>
2021-11-25 20:18:43 +02:00

115 lines
5.6 KiB
Go

package difficulty_test
import (
"fmt"
"github.com/kaspanet/kaspad/domain/consensus"
"math"
"math/big"
"testing"
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
"github.com/kaspanet/kaspad/domain/dagconfig"
"github.com/kaspanet/kaspad/util/difficulty"
)
func TestGetHashrateString(t *testing.T) {
var results = map[string]string{
dagconfig.MainnetParams.Name: "1.53 GH/s",
dagconfig.TestnetParams.Name: "131.07 KH/s",
dagconfig.DevnetParams.Name: "131.07 KH/s",
dagconfig.SimnetParams.Name: "2.00 KH/s",
}
testutils.ForAllNets(t, false, func(t *testing.T, consensusConfig *consensus.Config) {
targetGenesis := difficulty.CompactToBig(consensusConfig.GenesisBlock.Header.Bits())
hashrate := difficulty.GetHashrateString(targetGenesis, consensusConfig.TargetTimePerBlock)
if hashrate != results[consensusConfig.Name] {
t.Errorf("Expected %s, found %s", results[consensusConfig.Name], hashrate)
}
})
}
// TestBigToCompact ensures BigToCompact converts big integers to the expected
// compact representation.
func TestBigToCompact(t *testing.T) {
tests := []struct {
in string
out uint32
}{
{"0000000000000000000000000000000000000000000000000000000000000000", 0},
{"-1", 25231360},
{"9223372036854775807", 142606335},
{"922337203685477580712312312123487", 237861256},
{"128", 0x02008000},
}
for x, test := range tests {
n := new(big.Int)
n.SetString(test.in, 10)
r := difficulty.BigToCompact(n)
if r != test.out {
t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n",
x, r, test.out)
return
}
}
}
// TestCompactToBig ensures CompactToBig converts numbers using the compact
// representation to the expected big integers.
func TestCompactToBig(t *testing.T) {
tests := []struct {
before uint32
intHex string
after uint32
}{
{math.MaxUint32, "-7fffff000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000", math.MaxUint32},
{0x00000000, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x0989680, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x87fffff, "0000000000000000000000000000000000000000000000007fffff0000000000", 0x87fffff},
{0x1810000, "-000000000000000000000000000000000000000000000000000000000000001", 0x1810000},
{0xe2d7988, "0000000000000000000000000000000000002d79880000000000000000000000", 0xe2d7988},
{0x00123456, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x01003456, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x02000056, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x03000000, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x04000000, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x00923456, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x01803456, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x02800056, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x03800000, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x04800000, "0000000000000000000000000000000000000000000000000000000000000000", 0x00000000},
{0x01123456, "0000000000000000000000000000000000000000000000000000000000000012", 0x01120000},
{0x02008000, "0000000000000000000000000000000000000000000000000000000000000080", 0x02008000},
{0x01fedcba, "-00000000000000000000000000000000000000000000000000000000000007e", 0x01fe0000},
{0x02123456, "0000000000000000000000000000000000000000000000000000000000001234", 0x02123400},
{0x03123456, "0000000000000000000000000000000000000000000000000000000000123456", 0x03123456},
{0x04123456, "0000000000000000000000000000000000000000000000000000000012345600", 0x04123456},
{0x04923456, "-000000000000000000000000000000000000000000000000000000012345600", 0x04923456},
{0x05009234, "0000000000000000000000000000000000000000000000000000000092340000", 0x05009234},
{0x20123456, "1234560000000000000000000000000000000000000000000000000000000000", 0x20123456},
{0xff123456, "123456000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 0xff123456},
}
for i, test := range tests {
n := difficulty.CompactToBig(test.before)
convertBack := difficulty.BigToCompact(n)
got := fmt.Sprintf("%064x", n)
if got != test.intHex {
t.Errorf("TestCompactToBig test #%d failed: got %s want %s, input: 0x%08x",
i, got, test.intHex, test.before)
}
if convertBack != test.after {
t.Errorf("TestCompactToBig test #%d failed: got: 0x%08x want 0x%08x input: 0x%08x", i, convertBack, test.after, test.before)
}
}
}