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); } }
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 IKey GetTransactionRegistryHashKey(ITransactionRegistryBlock registryRegisterBlock) { IKey key = _transactionHashKey.GetKey(_cryptoService.ComputeTransactionKey(registryRegisterBlock.RawData)); return(key); }