Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        public void DeferredBroadcast(ushort round, Action onBroadcasted)
        {
            if (_synchronizationContext == null)
            {
                return;
            }

            //if (_synchronizationContext.LastBlockDescriptor != null && _synchronizationContext.LastBlockDescriptor.BlockHeight - 1 > _lastLaunchedSyncBlockOrder)
            //{
            //    _syncProducingCancellation?.Cancel();
            //    _syncProducingCancellation = null;
            //}
            int delay = 1;

            if (_syncProducingCancellation != null)
            {
                delay = Math.Max((int)(60000 * round - (DateTime.Now - (_synchronizationContext.LastBlockDescriptor?.UpdateTime ?? DateTime.Now)).TotalMilliseconds), 0);
            }

            _syncProducingCancellation = new CancellationTokenSource();

            //if (delay > 0)
            {
                Task.Delay(delay, _syncProducingCancellation.Token)
                .ContinueWith(t =>
                {
                    try
                    {
                        SynchronizationDescriptor synchronizationDescriptor = _synchronizationContext.LastBlockDescriptor;

                        SynchronizationProducingBlock synchronizationBlock = new SynchronizationProducingBlock
                        {
                            SyncBlockHeight = synchronizationDescriptor?.BlockHeight ?? 0,
                            BlockHeight     = (synchronizationDescriptor?.BlockHeight ?? 0) + 1,
                            ReportedTime    = synchronizationDescriptor?.MedianTime.AddMinutes(1) ?? DateTime.Now,
                            Round           = round,
                            HashPrev        = synchronizationDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE],
                            PowHash         = _proofOfWorkCalculation.CalculateHash(synchronizationDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE])
                        };

                        using (ISerializer serializer = _serializersFactory.Create(synchronizationBlock))
                        {
                            serializer.SerializeBody();
                            _nodeContext.SigningService.Sign(synchronizationBlock);
                            _communicationService.PostMessage(_synchronizationGroupState.GetAllNeighbors(), serializer);
                            _lastLaunchedSyncBlockOrder = synchronizationBlock.BlockHeight;
                        }
                    }
                    finally
                    {
                        onBroadcasted.Invoke();
                    }
                }, _syncProducingCancellation.Token, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Current);
            }
        }
Exemplo n.º 3
0
        private RegistryConfidenceBlock GetConfidence(RegistryShortBlock transactionsShortBlock)
        {
            _registryMemPool.EnqueueTransactionsShortBlock(transactionsShortBlock);

            byte[] proof = _registryMemPool.GetConfidenceMask(transactionsShortBlock, out byte[] bitMask);

            SynchronizationDescriptor synchronizationDescriptor           = _synchronizationContext.LastBlockDescriptor;
            RegistryConfidenceBlock   transactionsRegistryConfidenceBlock = new RegistryConfidenceBlock()
            {
                SyncBlockHeight     = transactionsShortBlock.SyncBlockHeight,
                Nonce               = transactionsShortBlock.Nonce,
                PowHash             = transactionsShortBlock.PowHash,
                BlockHeight         = transactionsShortBlock.BlockHeight,
                ReferencedBlockHash = _defaulHashCalculation.CalculateHash(transactionsShortBlock.RawData),
                BitMask             = bitMask,
                ConfidenceProof     = proof
            };

            _logger.Debug($"BitMask of Confidence for RegistryShortBlock at round {transactionsShortBlock.BlockHeight} with {transactionsShortBlock.TransactionHeaderHashes.Count} hashes is {transactionsRegistryConfidenceBlock.BitMask.ToHexString()}");

            return(transactionsRegistryConfidenceBlock);
        }
Exemplo n.º 4
0
        private RegistryFullBlock ProduceTransactionsFullBlock(SortedList <ushort, ITransactionRegistryBlock> transactionRegisterBlocks)
        {
            SynchronizationDescriptor synchronizationDescriptor = _synchronizationContext.LastBlockDescriptor;
            ulong syncBlockHeight = synchronizationDescriptor?.BlockHeight ?? 0;

            byte[] hash = synchronizationDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE];
            byte[] pow  = _powCalculation.CalculateHash(hash);

            _logger.Debug($"ProduceTransactionsFullBlock synchronizationDescriptor[{syncBlockHeight}].Hash = {hash.ToHexString()}; POW = {pow.ToHexString()}");

            RegistryFullBlock transactionsFullBlock = new RegistryFullBlock
            {
                SyncBlockHeight    = syncBlockHeight,
                PowHash            = pow,
                BlockHeight        = (ulong)_registryGroupState.Round,
                TransactionHeaders = transactionRegisterBlocks
            };

            //_nodeCountersService.RegistryBlockLastSize.RawValue = transactionRegisterBlocks.Count;
            //_nodeCountersService.RegistryBlockLastSize.NextSample();

            return(transactionsFullBlock);
        }
Exemplo n.º 5
0
        public void MemPool_AddedNonUniqueTransactions_NotAllContained()
        {
            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>();
            ISigningService               signingService               = Substitute.For <ISigningService>();
            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);
            //identityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16() { Value = c.Arg<Memory<byte>>() });

            statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext);

            byte[] privateKey = ConfidentialAssetsHelper.GetRandomSeed();
            Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey);

            signingService.WhenForAnyArgs(s => s.Sign(null, null)).Do(c =>
            {
                ((SignedPacketBase)c.ArgAt <IPacket>(0)).Signer    = new Key32(publicKey);
                ((SignedPacketBase)c.ArgAt <IPacket>(0)).Signature = Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey);
            });
            signingService.PublicKeys.ReturnsForAnyArgs(new IKey[] { new Key32(publicKey) });

            TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, statesRepository, 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();
                serializer.Initialize(transactionRegisterBlock);
                serializer.SerializeBody();
                signingService.Sign(transactionRegisterBlock);
                serializer.SerializeFully();

                if (!addedHeights.Contains(heights[i]))
                {
                    expectedBlocks.Add(order++, transactionRegisterBlock);
                    addedHeights.Add(heights[i]);
                }

                transactionRegistryMemPool.EnqueueTransactionWitness(transactionRegisterBlock);
            }

            SortedList <ushort, RegistryRegisterBlock> actualBlocks = transactionRegistryMemPool.DequeueStateWitnessBulk();

            Assert.Equal(expectedBlocks.Count, actualBlocks.Count);
            for (ushort i = 0; i < (ushort)expectedBlocks.Count; i++)
            {
                Assert.Equal(expectedBlocks[i].BlockHeight, actualBlocks[i].BlockHeight);
            }
        }
Exemplo n.º 6
0
 public SyncCycleDescriptor(SynchronizationDescriptor synchronizationDescriptor)
 {
     SynchronizationDescriptor = synchronizationDescriptor;
     CancellationTokenSource   = new CancellationTokenSource();
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        private RegistryFullBlock ProduceTransactionsFullBlock(SortedList <ushort, RegistryRegisterBlock> transactionStateWitnesses, SortedList <ushort, RegistryRegisterUtxoConfidential> transactionUtxoWitnesses, SynchronizationDescriptor synchronizationDescriptor, int round)
        {
            ulong syncBlockHeight = synchronizationDescriptor?.BlockHeight ?? 0;

            byte[] hash = synchronizationDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE];
            byte[] pow  = _powCalculation.CalculateHash(hash);

            _logger.Debug($"ProduceTransactionsFullBlock synchronizationDescriptor[{syncBlockHeight}].Hash = {hash.ToHexString()}; POW = {pow.ToHexString()}");

            RegistryFullBlock transactionsFullBlock = new RegistryFullBlock
            {
                SyncBlockHeight = syncBlockHeight,
                PowHash         = pow,
                BlockHeight     = (ulong)(round * _registryConfiguration.TotalNodes + _registryConfiguration.Position + 1),
                StateWitnesses  = transactionStateWitnesses.Select(t => t.Value).ToArray(),
                UtxoWitnesses   = transactionUtxoWitnesses.Select(t => t.Value).ToArray()
            };

            return(transactionsFullBlock);
        }