git.gammaspectra.live/P2Pool/consensus@v0.0.0-20240403173234-a039820b20c9/p2pool/sidechain/poolblock_test.go (about) 1 package sidechain 2 3 import ( 4 "context" 5 "git.gammaspectra.live/P2Pool/consensus/monero/client" 6 "git.gammaspectra.live/P2Pool/consensus/monero/crypto" 7 "git.gammaspectra.live/P2Pool/consensus/monero/randomx" 8 "git.gammaspectra.live/P2Pool/consensus/types" 9 "os" 10 "testing" 11 ) 12 13 func testPoolBlock(b *PoolBlock, t *testing.T, expectedBufferLength int, majorVersion, minorVersion uint8, sideHeight uint64, nonce uint32, templateId, mainId, expectedPowHash, privateKeySeed, coinbaseId types.Hash, privateKey crypto.PrivateKeyBytes) { 14 if buf, _ := b.Main.MarshalBinary(); len(buf) != expectedBufferLength { 15 t.Fatal() 16 } 17 18 powHash := b.PowHash(ConsensusDefault.GetHasher(), func(height uint64) (hash types.Hash) { 19 seedHeight := randomx.SeedHeight(height) 20 if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil { 21 return types.ZeroHash 22 } else { 23 return types.MustHashFromString(h.BlockHeader.Hash) 24 } 25 }) 26 27 if b.Main.MajorVersion != majorVersion || b.Main.MinorVersion != minorVersion { 28 t.Fatal() 29 } 30 31 if b.Side.Height != sideHeight { 32 t.Fatal() 33 } 34 35 if b.Main.Nonce != nonce { 36 t.Fatal() 37 } 38 39 if b.SideTemplateId(ConsensusDefault) != templateId { 40 t.Fatal() 41 } 42 43 if b.FullId().TemplateId() != templateId { 44 t.Logf("%s != %s", b.FullId().TemplateId(), templateId) 45 t.Fatal() 46 } 47 48 if b.MainId() != mainId { 49 t.Fatal() 50 } 51 52 if powHash != expectedPowHash { 53 t.Fatal() 54 } 55 56 if !b.IsProofHigherThanDifficulty(ConsensusDefault.GetHasher(), func(height uint64) (hash types.Hash) { 57 seedHeight := randomx.SeedHeight(height) 58 if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil { 59 return types.ZeroHash 60 } else { 61 return types.MustHashFromString(h.BlockHeader.Hash) 62 } 63 }) { 64 t.Fatal() 65 } 66 67 if b.Side.CoinbasePrivateKey != privateKey { 68 t.Fatal() 69 } 70 71 if b.Side.CoinbasePrivateKeySeed != privateKeySeed { 72 t.Fatal() 73 } 74 75 if b.CoinbaseId() != coinbaseId { 76 t.Fatal() 77 } 78 } 79 80 func TestPoolBlockDecode(t *testing.T) { 81 82 if buf, err := os.ReadFile("testdata/block.dat"); err != nil { 83 t.Fatal(err) 84 } else { 85 b := &PoolBlock{} 86 if err = b.UnmarshalBinary(ConsensusDefault, &NilDerivationCache{}, buf); err != nil { 87 t.Fatal(err) 88 } 89 90 testPoolBlock(b, t, 91 1757, 92 16, 93 16, 94 4674483, 95 1247, 96 types.MustHashFromString("15ecd7e99e78e93bf8bffb1f597046cfa2d604decc32070e36e3caca01597c7d"), 97 types.MustHashFromString("fc63db007b94b3eba51bbc6e2076b0ac37b49ea554cc310c5e0fa595889960f3"), 98 types.MustHashFromString("aa7a3c4a2d67cb6a728e244288219bf038024f3b511b0da197a19ec601000000"), 99 types.MustHashFromString("fd7b5f77c79e624e26b939028a15f14b250fdb217cd40b4ce524eab9b17082ca"), 100 types.MustHashFromString("b52a9222eb2712c43742ed3a598df34c821bfb5d3b5a25d41bb4bdb014505ca9"), 101 crypto.PrivateKeyBytes(types.MustHashFromString("ba757262420e8bfa7c1931c75da051955cd2d4dff35dff7bfff42a1569941405")), 102 ) 103 } 104 } 105 106 func TestPoolBlockDecodePreFork(t *testing.T) { 107 108 if buf, err := os.ReadFile("testdata/old_mainnet_test2_block.dat"); err != nil { 109 t.Fatal(err) 110 } else { 111 b := &PoolBlock{} 112 if err = b.UnmarshalBinary(ConsensusDefault, &NilDerivationCache{}, buf); err != nil { 113 t.Fatal(err) 114 } 115 116 testPoolBlock(b, t, 117 5607, 118 14, 119 14, 120 53450, 121 2432795907, 122 types.MustHashFromString("bd39e2870edfd54838fe690f70178bfe4f433198ae0b3c8b0725bdbbddf7ed57"), 123 types.MustHashFromString("5023d36d54141efae5895eae3d51478fe541c5898ad4d783cef33118b67051f2"), 124 types.MustHashFromString("f76d731c61c9c9b6c3f46be2e60c9478930b49b4455feecd41ecb9420d000000"), 125 types.ZeroHash, 126 types.MustHashFromString("09f4ead399cd8357eff7403562e43fe472f79e47deb39148ff3d681fc8f113ea"), 127 crypto.PrivateKeyBytes(types.MustHashFromString("fed259c99cb7d21ac94ade82f2909ad1ccabdeafd16eeb60db4ca5eedca86a0a")), 128 ) 129 } 130 }