public void MemPool_AddedXUniqueTransactions_AllContained() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ICryptoService cryptoService = Substitute.For <ICryptoService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetInstance(null).ReturnsForAnyArgs(identityKeyProvider); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = CryptoHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); cryptoService.ComputeTransactionKey(new Memory <byte>()).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <Memory <byte> >().ToArray()).GetBytes()); cryptoService.ComputeTransactionKey(new byte[0]).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <byte[]>()).GetBytes()); cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey)); cryptoService.PublicKey.ReturnsForAnyArgs(new Key32(publicKey)); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, cryptoService, statesRepository, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry), hashCalculationsRepository); ulong expectedCount = 10; SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); for (ulong i = 0; i < expectedCount; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, i, PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository); serializer.Initialize(transactionRegisterBlock); serializer.FillBodyAndRowBytes(); expectedBlocks.Add((ushort)i, transactionRegisterBlock); transactionRegistryMemPool.EnqueueTransactionRegisterBlock(transactionRegisterBlock); } SortedList <ushort, ITransactionRegistryBlock> actualBlocks = transactionRegistryMemPool.DequeueBulk(-1); Assert.Equal(expectedCount, (ushort)actualBlocks.Count); for (ushort i = 0; i < (ushort)expectedCount; i++) { Assert.Equal(expectedBlocks[i].BlockHeight, ((RegistryRegisterBlock)actualBlocks[i]).BlockHeight); } }
private static SortedList <ushort, ITransactionRegistryBlock> GetTransactionHeaders(ulong syncBlockHeight, ulong blockHeight, uint nonce, ushort expectedCount) { PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; SortedList <ushort, ITransactionRegistryBlock> transactionHeaders = new SortedList <ushort, ITransactionRegistryBlock>(expectedCount); for (ushort j = 0; j < expectedCount; j++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + j, PowHash = BinaryBuilder.GetPowHash(1234 + j), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643 + j), ReferencedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()) }; ICryptoService cryptoService = GetRandomCryptoService(); IIdentityKeyProvider transactionIdentityKeyProvider = Substitute.For <IIdentityKeyProvider>(); transactionIdentityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16(c.ArgAt <Memory <byte> >(0))); IIdentityKeyProvidersRegistry transactionIdentityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); transactionIdentityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().Returns(transactionIdentityKeyProvider); IHashCalculationsRepository transactionHashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); transactionHashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(cryptoService, transactionIdentityKeyProvidersRegistry, transactionHashCalculationsRepository); serializer1.Initialize(registryRegisterBlock); serializer1.FillBodyAndRowBytes(); transactionHeaders.Add(j, registryRegisterBlock); } return(transactionHeaders); }
public void RegistryFullBlockSerializerTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] expectedReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643); byte[] expectedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()); byte[] body; ushort expectedCount = 1000; SortedList <ushort, ITransactionRegistryBlock> transactionHeaders = new SortedList <ushort, ITransactionRegistryBlock>(); for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryBuilder.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer1.Initialize(registryRegisterBlock); serializer1.FillBodyAndRowBytes(); transactionHeaders.Add(i, registryRegisterBlock); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)transactionHeaders.Count); foreach (ushort order in transactionHeaders.Keys) { bw.Write(order); bw.Write(transactionHeaders[order].RawData.ToArray()); } bw.Write(BinaryBuilder.GetDefaultHash(1111)); } body = ms.ToArray(); } byte[] expectedPacket = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlock block = new RegistryFullBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, TransactionHeaders = transactionHeaders, ShortBlockHash = BinaryBuilder.GetDefaultHash(1111) }; RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer.Initialize(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }
public void MemPool_ContainsXItems_ConfidenceLevelOnAll() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ICryptoService cryptoService = Substitute.For <ICryptoService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().ReturnsForAnyArgs(identityKeyProvider); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = CryptoHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); cryptoService.ComputeTransactionKey(new Memory <byte>()).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <Memory <byte> >().ToArray()).GetBytes()); cryptoService.ComputeTransactionKey(new byte[0]).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <byte[]>()).GetBytes()); cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey)); cryptoService.PublicKey.ReturnsForAnyArgs(new Key32(publicKey)); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, cryptoService, statesRepository, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry), hashCalculationsRepository); SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); ulong[] heights = new ulong[] { 1, 2, 2, 5, 4, 3, 4, 3, 4, 5, 3 }; HashSet <ulong> addedHeights = new HashSet <ulong>(); ushort order = 0; for (ulong i = 0; i < (ulong)heights.Length; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, heights[i], PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository); serializer.Initialize(transactionRegisterBlock); serializer.FillBodyAndRowBytes(); if (!addedHeights.Contains(heights[i])) { expectedBlocks.Add(order++, transactionRegisterBlock); addedHeights.Add(heights[i]); } transactionRegistryMemPool.EnqueueTransactionRegisterBlock(transactionRegisterBlock); } RegistryShortBlock transactionsShortBlockAll = PacketsBuilder.GetTransactionsShortBlock(1, 1, null, 1, 1, expectedBlocks.Values, privateKey, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry)); RegistryShortBlock transactionsShortBlockOneLess = PacketsBuilder.GetTransactionsShortBlock(1, 1, null, 1, 1, expectedBlocks.Values.Skip(1), privateKey, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry)); byte[] bitMaskAll, bitMaskOneLess; byte[] confidenceProofAll = transactionRegistryMemPool.GetConfidenceMask(transactionsShortBlockAll, out bitMaskAll); byte[] confidenceProofOneLess = transactionRegistryMemPool.GetConfidenceMask(transactionsShortBlockOneLess, out bitMaskOneLess); long expectedConfidenceAll = expectedBlocks.Count; long expectedConfidenceOneLess = expectedBlocks.Count - 1; long actualConfidenceAll = GetConfidence(bitMaskAll); long actualConfidenceOneLess = GetConfidence(bitMaskOneLess); Assert.Equal(expectedConfidenceAll, actualConfidenceAll); Assert.Equal(expectedConfidenceOneLess, actualConfidenceOneLess); }
public void RegistryFullBlockParserTest() { IBlockParser blockParser = new RegistryRegisterBlockParser(_identityKeyProvidersRegistry, _hashCalculationRepository); _blockParsersRepository.GetInstance(0).ReturnsForAnyArgs(blockParser); ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] body; ushort expectedCount = 1000; byte[] expectedShortBlockHash; SortedList <ushort, RegistryRegisterBlock> transactionHeaders = new SortedList <ushort, RegistryRegisterBlock>(); for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryBuilder.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer1.Initialize(registryRegisterBlock); serializer1.FillBodyAndRowBytes(); transactionHeaders.Add(i, registryRegisterBlock); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)transactionHeaders.Count); foreach (ushort order in transactionHeaders.Keys) { bw.Write(order); bw.Write(transactionHeaders[order].RawData.ToArray()); } expectedShortBlockHash = BinaryBuilder.GetDefaultHash(1111); bw.Write(expectedShortBlockHash); } body = ms.ToArray(); } byte[] packet = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlockParser registryFullBlockParser = new RegistryFullBlockParser(_identityKeyProvidersRegistry, _hashCalculationRepository); RegistryFullBlock block = (RegistryFullBlock)registryFullBlockParser.Parse(packet); Assert.Equal(syncBlockHeight, block.SyncBlockHeight); Assert.Equal(nonce, block.Nonce); Assert.Equal(powHash, block.PowHash); Assert.Equal(version, block.Version); Assert.Equal(blockHeight, block.BlockHeight); foreach (var item in transactionHeaders) { RegistryRegisterBlock registryRegisterBlock = (RegistryRegisterBlock)block.TransactionHeaders[item.Key]; Assert.True(block.TransactionHeaders.ContainsKey(item.Key)); Assert.Equal(item.Value.PacketType, registryRegisterBlock.PacketType); Assert.Equal(item.Value.SyncBlockHeight, registryRegisterBlock.SyncBlockHeight); Assert.Equal(item.Value.Nonce, registryRegisterBlock.Nonce); Assert.Equal(item.Value.PowHash, registryRegisterBlock.PowHash); Assert.Equal(item.Value.BlockHeight, registryRegisterBlock.BlockHeight); Assert.Equal(item.Value.BlockType, registryRegisterBlock.BlockType); Assert.Equal(item.Value.ReferencedPacketType, registryRegisterBlock.ReferencedPacketType); Assert.Equal(item.Value.ReferencedBlockType, registryRegisterBlock.ReferencedBlockType); Assert.Equal(item.Value.ReferencedBodyHash, registryRegisterBlock.ReferencedBodyHash); Assert.Equal(item.Value.ReferencedTarget, registryRegisterBlock.ReferencedTarget); Assert.Equal(item.Value.Signature.ToArray(), registryRegisterBlock.Signature.ToArray()); Assert.Equal(item.Value.Signer, registryRegisterBlock.Signer); } Assert.Equal(expectedShortBlockHash, block.ShortBlockHash); Assert.Equal(_publicKey, block.Signer.Value.ToArray()); Assert.Equal(expectedSignature, block.Signature.ToArray()); }