git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/p2pool/sidechain/poolblock_test.go (about)

     1  package sidechain
     2  
     3  import (
     4  	"context"
     5  	"git.gammaspectra.live/P2Pool/consensus/v3/monero/client"
     6  	"git.gammaspectra.live/P2Pool/consensus/v3/monero/crypto"
     7  	"git.gammaspectra.live/P2Pool/consensus/v3/monero/randomx"
     8  	types2 "git.gammaspectra.live/P2Pool/consensus/v3/p2pool/types"
     9  	"git.gammaspectra.live/P2Pool/consensus/v3/types"
    10  	unsafeRandom "math/rand/v2"
    11  	"net/netip"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  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) {
    18  	if buf, _ := b.Main.MarshalBinary(); len(buf) != expectedBufferLength {
    19  		t.Fatal()
    20  	}
    21  
    22  	powHash := b.PowHash(ConsensusDefault.GetHasher(), func(height uint64) (hash types.Hash) {
    23  		seedHeight := randomx.SeedHeight(height)
    24  		if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil {
    25  			return types.ZeroHash
    26  		} else {
    27  			return h.BlockHeader.Hash
    28  		}
    29  	})
    30  
    31  	if b.Main.MajorVersion != majorVersion || b.Main.MinorVersion != minorVersion {
    32  		t.Fatal()
    33  	}
    34  
    35  	if b.Side.Height != sideHeight {
    36  		t.Fatal()
    37  	}
    38  
    39  	if b.Main.Nonce != nonce {
    40  		t.Fatal()
    41  	}
    42  
    43  	if b.SideTemplateId(ConsensusDefault) != templateId {
    44  		t.Fatal()
    45  	}
    46  
    47  	if b.FullId(ConsensusDefault).TemplateId() != templateId {
    48  		t.Logf("%s != %s", b.FullId(ConsensusDefault).TemplateId(), templateId)
    49  		t.Fatal()
    50  	}
    51  
    52  	if b.MainId() != mainId {
    53  		t.Fatal()
    54  	}
    55  
    56  	if powHash != expectedPowHash {
    57  		t.Fatal()
    58  	}
    59  
    60  	if !b.IsProofHigherThanDifficulty(ConsensusDefault.GetHasher(), func(height uint64) (hash types.Hash) {
    61  		seedHeight := randomx.SeedHeight(height)
    62  		if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil {
    63  			return types.ZeroHash
    64  		} else {
    65  			return h.BlockHeader.Hash
    66  		}
    67  	}) {
    68  		t.Fatal()
    69  	}
    70  
    71  	if b.Side.CoinbasePrivateKey != privateKey {
    72  		t.Fatal()
    73  	}
    74  
    75  	if b.Side.CoinbasePrivateKeySeed != privateKeySeed {
    76  		t.Fatal()
    77  	}
    78  
    79  	if b.CoinbaseId() != coinbaseId {
    80  		t.Fatal()
    81  	}
    82  }
    83  
    84  func TestPoolBlockMetadata(t *testing.T) {
    85  	meta := PoolBlockReceptionMetadata{
    86  		LocalTime:       time.Now().UTC(),
    87  		AddressPort:     netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 1234),
    88  		PeerId:          unsafeRandom.Uint64(),
    89  		SoftwareId:      uint32(types2.CurrentSoftwareId),
    90  		SoftwareVersion: uint32(types2.CurrentSoftwareVersion),
    91  	}
    92  
    93  	blob, err := meta.MarshalBinary()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	var meta2 PoolBlockReceptionMetadata
    99  	err = meta2.UnmarshalBinary(blob)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	if meta != meta2 {
   105  		t.Errorf("different metadata: %v, %v", meta, meta2)
   106  	}
   107  }
   108  
   109  func TestPoolBlockDecode(t *testing.T) {
   110  
   111  	if buf, err := os.ReadFile("testdata/block.dat"); err != nil {
   112  		t.Fatal(err)
   113  	} else {
   114  		b := &PoolBlock{}
   115  		if err = b.UnmarshalBinary(ConsensusDefault, &NilDerivationCache{}, buf); err != nil {
   116  			t.Fatal(err)
   117  		}
   118  
   119  		testPoolBlock(b, t,
   120  			1757,
   121  			16,
   122  			16,
   123  			4674483,
   124  			1247,
   125  			types.MustHashFromString("15ecd7e99e78e93bf8bffb1f597046cfa2d604decc32070e36e3caca01597c7d"),
   126  			types.MustHashFromString("fc63db007b94b3eba51bbc6e2076b0ac37b49ea554cc310c5e0fa595889960f3"),
   127  			types.MustHashFromString("aa7a3c4a2d67cb6a728e244288219bf038024f3b511b0da197a19ec601000000"),
   128  			types.MustHashFromString("fd7b5f77c79e624e26b939028a15f14b250fdb217cd40b4ce524eab9b17082ca"),
   129  			types.MustHashFromString("b52a9222eb2712c43742ed3a598df34c821bfb5d3b5a25d41bb4bdb014505ca9"),
   130  			crypto.PrivateKeyBytes(types.MustHashFromString("ba757262420e8bfa7c1931c75da051955cd2d4dff35dff7bfff42a1569941405")),
   131  		)
   132  	}
   133  }
   134  
   135  func TestPoolBlockDecodePreFork(t *testing.T) {
   136  
   137  	if buf, err := os.ReadFile("testdata/old_mainnet_test2_block.dat"); err != nil {
   138  		t.Fatal(err)
   139  	} else {
   140  		b := &PoolBlock{}
   141  		if err = b.UnmarshalBinary(ConsensusDefault, &NilDerivationCache{}, buf); err != nil {
   142  			t.Fatal(err)
   143  		}
   144  
   145  		testPoolBlock(b, t,
   146  			5607,
   147  			14,
   148  			14,
   149  			53450,
   150  			2432795907,
   151  			types.MustHashFromString("bd39e2870edfd54838fe690f70178bfe4f433198ae0b3c8b0725bdbbddf7ed57"),
   152  			types.MustHashFromString("5023d36d54141efae5895eae3d51478fe541c5898ad4d783cef33118b67051f2"),
   153  			types.MustHashFromString("f76d731c61c9c9b6c3f46be2e60c9478930b49b4455feecd41ecb9420d000000"),
   154  			types.ZeroHash,
   155  			types.MustHashFromString("09f4ead399cd8357eff7403562e43fe472f79e47deb39148ff3d681fc8f113ea"),
   156  			crypto.PrivateKeyBytes(types.MustHashFromString("fed259c99cb7d21ac94ade82f2909ad1ccabdeafd16eeb60db4ca5eedca86a0a")),
   157  		)
   158  	}
   159  }