void AssertBlockPayloadsEqual(BlockPayload expected, BlockPayload actual) { Assert.AreEqual(expected.Version, actual.Version); AssertBytesEqual(expected.PreviousBlockHash, actual.PreviousBlockHash); AssertBytesEqual(expected.MerkleRoot, actual.MerkleRoot); Assert.AreEqual(expected.TimeStamp, actual.TimeStamp); Assert.AreEqual(expected.Bits, actual.Bits); Assert.AreEqual(expected.Nonce, actual.Nonce); Assert.AreEqual(expected.PrimeChainMultiplier, actual.PrimeChainMultiplier); }
void HandleNewBestBlock(object sender, NewBestBlockEventArgs a) { lock (DataLock) { var index = Blockchains.IndexOf((Blockchain)sender); BlockchainsBestBlock[index] = a.Block; // Signal as newest block b/c lazy for a second... BestBlock = a.Block; NewBestBlock?.Invoke(this, new NewBestBlockEventArgs(BestBlock)); } }
public void TryDeserialize_DefaultFieldTest() { BlockPayload pl = new BlockPayload(); // field is not set // Block 00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee var stream = new FastStreamReader(Helper.HexToBytes("0100000055bd840a78798ad0da853f68974f3d183e2bd1db6a842c1feecf222a00000000ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d51b96a49ffff001d283e9e700201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0102ffffffff0100f2052a01000000434104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac000000000100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000")); bool b = pl.TryDeserialize(stream, out string error); Assert.True(b, error); Assert.Null(error); Assert.Equal(2, pl.BlockData.TransactionList.Length); }
public void TryDeserialize_FailTest(FastStreamReader stream, MockDeserializableBlock block, string expErr) { BlockPayload pl = new BlockPayload() { BlockData = block }; bool b = pl.TryDeserialize(stream, out string error); Assert.False(b); Assert.Equal(expErr, error); // Mock block has its own tests. }
private void RespondToBlockPayload(Node node, BlockPayload blockPayload) { this.blockSyncHub.ReceiveBlocks.Add(new HubReceiveBlockItem { Payload = blockPayload, Block = blockPayload.Object, Behaviour = this }); RequestCounter requestCounter; if (this.blockSyncHub.RequestCount.TryGetValue(blockPayload.Object.GetHash(), out requestCounter)) { Interlocked.Increment(ref requestCounter.Count); } }
public void TryDeserializeTest() { BlockPayload pl = new BlockPayload() { BlockData = new MockDeserializableBlock(0, 3) }; FastStreamReader stream = new FastStreamReader(new byte[3]); bool b = pl.TryDeserialize(stream, out string error); Assert.True(b, error); Assert.Null(error); // Mock block has its own tests. Assert.Equal(PayloadType.Block, pl.PayloadType); }
public void SerializeTest() { BlockPayload pl = new BlockPayload() { BlockData = new MockSerializableBlock(new byte[] { 1, 2, 3 }) }; FastStream stream = new FastStream(3); pl.Serialize(stream); byte[] actual = pl.Serialize(); byte[] expected = new byte[] { 1, 2, 3 }; Assert.Equal(expected, stream.ToByteArray()); Assert.Equal(expected, actual); }
public void TestAlgorithmsCheckProofOfWork() { // Target Chain Length too small var block = new BlockPayload( version: 2, previousBlockHash: new Byte[] { }, timeStamp: 1, bits: 0x05000000, nonce: 1, primeChainMultiplier: new BigInteger(), txs: null ); var networkConfig = new NetworkConfiguration( defaultPort: 9911, magic: 0xE7E5E7E4, dnsSeed: "seed.primecoin.me", minimumChainLength: 6, maximumChainLength: 99, minimumHeaderHash: new BigInteger(1) << 255, minimumChainOrigin: new BigInteger(1) << 255, maximumChainOrigin: new BigInteger(1) << 2000 - 1 ); Assert.ThrowsException <TargetChainLengthTooSmall>( () => Algorithms.CheckProofOfWork(block, networkConfig) ); // Target Chain Length too big block = new BlockPayload( version: 2, previousBlockHash: new Byte[] { }, timeStamp: 1, bits: 0x64000000, nonce: 1, primeChainMultiplier: new BigInteger(), txs: null ); Assert.ThrowsException <TargetChainLengthTooBig>( () => Algorithms.CheckProofOfWork(block, networkConfig) ); // Header Hash too small var txInputs = new List <TxInputPayload>(); var txOutputs = new List <TxOutputPayload>(); var txs = new List <TransactionPayload>(); txInputs.Add( new TxInputPayload( previousTx: new TxOutPointPayload( txHash: new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, index: 0xFFFFFFFF ), script: new UnknownPayload( bytes: new Byte[] { 0x03, 0x15, 0x86, 0x29, 0x01, 0x4F, 0x00, 0x06, 0x2F, 0x50, 0x32, 0x53, 0x48, 0x2F, } ), sequence: 0xFFFFFFFF ) ); txOutputs.Add( new TxOutputPayload( amount: 740000000, script: new UnknownPayload( bytes: new Byte[] { 0x21, 0x02, 0xF8, 0x2B, 0xD5, 0x4C, 0xF1, 0xFA, 0x3D, 0x07, 0xA1, 0xC3, 0x74, 0xD1, 0x79, 0x02, 0x46, 0xED, 0x82, 0x13, 0x23, 0x52, 0x29, 0xEC, 0x86, 0xA3, 0x95, 0x02, 0xE3, 0x19, 0x0B, 0x07, 0x1A, 0xBB, 0xAC, } ) ) ); txs.Add( new TransactionPayload( version: 1, txInputs: txInputs, txOutputs: txOutputs, lockTime: 0 ) ); block = new BlockPayload( version: 2, previousBlockHash: new Byte[] { 0xAF, 0x20, 0xAC, 0xD1, 0xC5, 0x63, 0x22, 0x2A, 0x94, 0x2F, 0x18, 0xD5, 0x97, 0x59, 0xE5, 0xD1, 0xEC, 0x5B, 0xD1, 0x16, 0xE5, 0x9C, 0x68, 0x11, 0x88, 0x3A, 0x85, 0xB6, 0x6D, 0x2C, 0x4A, 0x6F, }, timeStamp: 0x5B315EF6, bits: 0x0B9D8796, nonce: 0x00000000, primeChainMultiplier: BigInteger.Parse("11450505506960965632"), txs: txs ); Assert.ThrowsException <BlockHeaderHashTooSmall>( () => Algorithms.CheckProofOfWork(block, networkConfig) ); block = new BlockPayload( version: 2, previousBlockHash: new Byte[] { 0xAF, 0x20, 0xAC, 0xD1, 0xC5, 0x63, 0x22, 0x2A, 0x94, 0x2F, 0x18, 0xD5, 0x97, 0x59, 0xE5, 0xD1, 0xEC, 0x5B, 0xD1, 0x16, 0xE5, 0x9C, 0x68, 0x11, 0x88, 0x3A, 0x85, 0xB6, 0x6D, 0x2C, 0x4A, 0x6F, }, timeStamp: 0x5B315EF6, bits: 0x0B9D8796, nonce: 0x5ED830A2, primeChainMultiplier: BigInteger.Parse("0"), txs: txs ); Assert.ThrowsException <ChainOriginTooSmall>( () => Algorithms.CheckProofOfWork(block, networkConfig) ); block = new BlockPayload( version: 2, previousBlockHash: new Byte[] { 0xAF, 0x20, 0xAC, 0xD1, 0xC5, 0x63, 0x22, 0x2A, 0x94, 0x2F, 0x18, 0xD5, 0x97, 0x59, 0xE5, 0xD1, 0xEC, 0x5B, 0xD1, 0x16, 0xE5, 0x9C, 0x68, 0x11, 0x88, 0x3A, 0x85, 0xB6, 0x6D, 0x2C, 0x4A, 0x6F, }, timeStamp: 0x5B315EF6, bits: 0x0B9D8796, nonce: 0x5ED830A2, primeChainMultiplier: new BigInteger(1) << 2000 - 1, txs: txs ); Assert.ThrowsException <ChainOriginTooBig>( () => Algorithms.CheckProofOfWork(block, networkConfig) ); }
public void TestBlockPayload() { BlockPayload payload; byte[] expected; var txInputs = new List <TxInputPayload>(); var txOutputs = new List <TxOutputPayload>(); var txs = new List <TransactionPayload>(); txInputs.Add( new TxInputPayload( previousTx: new TxOutPointPayload( txHash: new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, index: 0xFFFFFFFF ), script: new UnknownPayload( bytes: new Byte[] { 0x03, 0x15, 0x86, 0x29, 0x01, 0x4F, 0x00, 0x06, 0x2F, 0x50, 0x32, 0x53, 0x48, 0x2F, } ), sequence: 0xFFFFFFFF ) ); txOutputs.Add( new TxOutputPayload( amount: 740000000, script: new UnknownPayload( bytes: new Byte[] { 0x21, 0x02, 0xF8, 0x2B, 0xD5, 0x4C, 0xF1, 0xFA, 0x3D, 0x07, 0xA1, 0xC3, 0x74, 0xD1, 0x79, 0x02, 0x46, 0xED, 0x82, 0x13, 0x23, 0x52, 0x29, 0xEC, 0x86, 0xA3, 0x95, 0x02, 0xE3, 0x19, 0x0B, 0x07, 0x1A, 0xBB, 0xAC, } ) ) ); txs.Add( new TransactionPayload( version: 1, txInputs: txInputs, txOutputs: txOutputs, lockTime: 0 ) ); payload = new BlockPayload( version: 2, previousBlockHash: new Byte[] { 0xAF, 0x20, 0xAC, 0xD1, 0xC5, 0x63, 0x22, 0x2A, 0x94, 0x2F, 0x18, 0xD5, 0x97, 0x59, 0xE5, 0xD1, 0xEC, 0x5B, 0xD1, 0x16, 0xE5, 0x9C, 0x68, 0x11, 0x88, 0x3A, 0x85, 0xB6, 0x6D, 0x2C, 0x4A, 0x6F, }, timeStamp: 0x5B315EF6, bits: 0x0B9D8796, nonce: 0x5ED830A2, primeChainMultiplier: BigInteger.Parse("11450505506960965632"), txs: txs ); expected = new byte[] { 0x02, 0x00, 0x00, 0x00, // Version 0xAF, 0x20, 0xAC, 0xD1, 0xC5, 0x63, 0x22, 0x2A, // PreviousBlockHash... 0x94, 0x2F, 0x18, 0xD5, 0x97, 0x59, 0xE5, 0xD1, // ... 0xEC, 0x5B, 0xD1, 0x16, 0xE5, 0x9C, 0x68, 0x11, // ... 0x88, 0x3A, 0x85, 0xB6, 0x6D, 0x2C, 0x4A, 0x6F, // ... 0x30, 0xE5, 0x3D, 0x4F, 0x8B, 0xB1, 0x81, 0xFB, // MerkleRoot... 0x21, 0x2A, 0xB9, 0x6A, 0x4F, 0x68, 0xF2, 0x4E, // ... 0xB6, 0x3A, 0x6F, 0xA0, 0x04, 0x8F, 0x2E, 0x1F, // ... 0x35, 0x75, 0x55, 0x5C, 0x4D, 0xC9, 0x5B, 0x5A, // ... 0xF6, 0x5E, 0x31, 0x5B, // TimeStamp 0x96, 0x87, 0x9D, 0x0B, // Bits 0xA2, 0x30, 0xD8, 0x5E, // Nonce // PrimeChainMultiplier (OpenSSL BIGNUM mpi format) 0x09, // PrimeChainMultiplier byte count 0x00, 0x00, 0x10, 0x59, 0x17, 0x5E, 0xE8, 0x9E, // PrimeChainMultiplier Little Endian... 0x00, // ... 0x01, // Transactions count // Transaction 0x01, 0x00, 0x00, 0x00, // Transaction Version 0x01, // Transaction Inputs count // Transaction Input (coinbase) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PreviousTransactionOutput... 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ... 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ... 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ... 0xFF, 0xFF, 0xFF, 0xFF, 0x0E, // Script length 0x03, 0x15, 0x86, 0x29, 0x01, 0x4F, 0x00, 0x06, // Script... 0x2F, 0x50, 0x32, 0x53, 0x48, 0x2F, // ... 0xFF, 0xFF, 0xFF, 0xFF, // Sequence 0x01, // Transaction Outputs count // Transaction Output 0x00, 0x81, 0x1B, 0x2C, 0x00, 0x00, 0x00, 0x00, // Amount 0x23, // Script length 0x21, 0x02, 0xF8, 0x2B, 0xD5, 0x4C, 0xF1, 0xFA, // Script... 0x3D, 0x07, 0xA1, 0xC3, 0x74, 0xD1, 0x79, 0x02, // ... 0x46, 0xED, 0x82, 0x13, 0x23, 0x52, 0x29, 0xEC, // ... 0x86, 0xA3, 0x95, 0x02, 0xE3, 0x19, 0x0B, 0x07, // ... 0x1A, 0xBB, 0xAC, // ... 0x00, 0x00, 0x00, 0x00 // LockTime }; AssertBytesEqual(expected, payload.ToBytes()); AssertBlockPayloadsEqual(payload, new BlockPayload(expected)); var networkConfig = new NetworkConfiguration( defaultPort: 9911, magic: 0xE7E5E7E4, dnsSeed: "seed.primecoin.me", minimumChainLength: 6, maximumChainLength: 99, minimumHeaderHash: new BigInteger(1) << 255, minimumChainOrigin: new BigInteger(1) << 255, maximumChainOrigin: new BigInteger(1) << 2000 - 1 ); Algorithms.CheckProofOfWork(payload, networkConfig); // Should be a valid block. }
void ProcessBlockPayload(BlockPayload payload) { Algorithms.CheckProofOfWork(payload, Connection.NetworkConfig); // Signal as newest block b/c lazy for a second... NewBestBlock?.Invoke(this, new NewBestBlockEventArgs(payload)); }
public NewBestBlockEventArgs(BlockPayload block) { Block = block; }