Esempio n. 1
0
        private static SortedList <ushort, RegistryRegisterBlock> GetTransactionHeaders(ulong syncBlockHeight, ulong blockHeight, uint nonce, ushort expectedCount)
        {
            PacketType expectedReferencedPacketType = PacketType.Transactional;
            ushort     expectedReferencedBlockType  = BlockTypes.Transaction_TransferFunds;

            SortedList <ushort, RegistryRegisterBlock> transactionHeaders = new SortedList <ushort, RegistryRegisterBlock>(expectedCount);

            for (ushort j = 0; j < expectedCount; j++)
            {
                RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock
                {
                    SyncBlockHeight      = syncBlockHeight,
                    Nonce                = nonce + j,
                    PowHash              = BinaryHelper.GetPowHash(1234 + j),
                    BlockHeight          = blockHeight,
                    ReferencedPacketType = expectedReferencedPacketType,
                    ReferencedBlockType  = expectedReferencedBlockType,
                    ReferencedBodyHash   = BinaryHelper.GetDefaultHash(473826643 + j),
                    ReferencedTarget     = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey())
                };

                ISigningService signingService = 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();
                serializer1.Initialize(registryRegisterBlock);
                serializer1.SerializeBody();
                signingService.Sign(registryRegisterBlock);
                serializer1.SerializeFully();

                transactionHeaders.Add(j, registryRegisterBlock);
            }

            return(transactionHeaders);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public void RegistryFullBlockParserTest()
        {
            IBlockParser blockParser = new RegistryRegisterBlockParser(_identityKeyProvidersRegistry);

            _blockParsersRepository.GetInstance(0).ReturnsForAnyArgs(blockParser);

            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryHelper.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = null;

            PacketType expectedReferencedPacketType = PacketType.Transactional;
            ushort     expectedReferencedBlockType  = BlockTypes.Transaction_TransferFunds;

            byte[] body;

            ushort expectedCount = 1;

            byte[] expectedShortBlockHash;

            RegistryRegisterBlock[]            stateWitnesses = new RegistryRegisterBlock[expectedCount];
            RegistryRegisterUtxoConfidential[] utxoWitnesses  = new RegistryRegisterUtxoConfidential[expectedCount];

            for (ushort i = 0; i < expectedCount; i++)
            {
                RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock
                {
                    SyncBlockHeight      = syncBlockHeight,
                    Nonce                = nonce + i,
                    PowHash              = BinaryHelper.GetPowHash(1234 + i),
                    BlockHeight          = blockHeight,
                    ReferencedPacketType = expectedReferencedPacketType,
                    ReferencedBlockType  = expectedReferencedBlockType,
                    ReferencedBodyHash   = BinaryHelper.GetDefaultHash(473826643 + i),
                    ReferencedTarget     = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey())
                };

                RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer();
                serializer1.Initialize(registryRegisterBlock);
                serializer1.SerializeBody();
                _signingService.Sign(registryRegisterBlock);
                serializer1.SerializeFully();

                stateWitnesses[i] = registryRegisterBlock;

                RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = new RegistryRegisterUtxoConfidential
                {
                    SyncBlockHeight      = syncBlockHeight,
                    Nonce                = nonce + i,
                    PowHash              = BinaryHelper.GetPowHash(1234 + i),
                    KeyImage             = new Key32(ConfidentialAssetsHelper.GetRandomSeed()),
                    ReferencedPacketType = expectedReferencedPacketType,
                    ReferencedBlockType  = expectedReferencedBlockType,
                    ReferencedBodyHash   = BinaryHelper.GetDefaultHash(473826643 + i),
                    DestinationKey       = ConfidentialAssetsHelper.GetRandomSeed(),
                    DestinationKey2      = ConfidentialAssetsHelper.GetRandomSeed(),
                    TransactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed(),
                    PublicKeys           = new Key32[] { new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()) },
                    Signatures           = new RingSignature[]
                    {
                        new RingSignature {
                            R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed()
                        },
                        new RingSignature {
                            R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed()
                        },
                        new RingSignature {
                            R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed()
                        }
                    }
                };

                RegistryRegisterUtxoConfidentialBlockSerializer serializer2 = new RegistryRegisterUtxoConfidentialBlockSerializer();
                serializer2.Initialize(registryRegisterUtxoConfidentialBlock);
                serializer2.SerializeFully();

                utxoWitnesses[i] = registryRegisterUtxoConfidentialBlock;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write((ushort)stateWitnesses.Length);
                    bw.Write((ushort)utxoWitnesses.Length);

                    foreach (RegistryRegisterBlock witness in stateWitnesses)
                    {
                        bw.Write(witness.RawData.ToArray());
                    }

                    foreach (RegistryRegisterUtxoConfidential witness in utxoWitnesses)
                    {
                        bw.Write(witness.RawData.ToArray());
                    }

                    expectedShortBlockHash = BinaryHelper.GetDefaultHash(1111);
                    bw.Write(expectedShortBlockHash);
                }

                body = ms.ToArray();
            }

            byte[] packet = BinaryHelper.GetSignedPacket(
                PacketType.Registry,
                syncBlockHeight,
                nonce, powHash, version,
                BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);

            RegistryFullBlockParser registryFullBlockParser = new RegistryFullBlockParser(_identityKeyProvidersRegistry);
            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);

            for (int i = 0; i < expectedCount; i++)
            {
                RegistryRegisterBlock registryRegisterBlock = block.StateWitnesses[i];
                Assert.Equal(stateWitnesses[i].PacketType, registryRegisterBlock.PacketType);
                Assert.Equal(stateWitnesses[i].SyncBlockHeight, registryRegisterBlock.SyncBlockHeight);
                Assert.Equal(stateWitnesses[i].Nonce, registryRegisterBlock.Nonce);
                Assert.Equal(stateWitnesses[i].PowHash, registryRegisterBlock.PowHash);
                Assert.Equal(stateWitnesses[i].BlockHeight, registryRegisterBlock.BlockHeight);
                Assert.Equal(stateWitnesses[i].BlockType, registryRegisterBlock.BlockType);
                Assert.Equal(stateWitnesses[i].ReferencedPacketType, registryRegisterBlock.ReferencedPacketType);
                Assert.Equal(stateWitnesses[i].ReferencedBlockType, registryRegisterBlock.ReferencedBlockType);
                Assert.Equal(stateWitnesses[i].ReferencedBodyHash, registryRegisterBlock.ReferencedBodyHash);
                Assert.Equal(stateWitnesses[i].ReferencedTarget, registryRegisterBlock.ReferencedTarget);
                Assert.Equal(stateWitnesses[i].Signature.ToArray(), registryRegisterBlock.Signature.ToArray());
                Assert.Equal(stateWitnesses[i].Signer, registryRegisterBlock.Signer);

                RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = block.UtxoWitnesses[i];
                Assert.Equal(utxoWitnesses[i].PacketType, registryRegisterUtxoConfidentialBlock.PacketType);
                Assert.Equal(utxoWitnesses[i].SyncBlockHeight, registryRegisterUtxoConfidentialBlock.SyncBlockHeight);
                Assert.Equal(utxoWitnesses[i].Nonce, registryRegisterUtxoConfidentialBlock.Nonce);
                Assert.Equal(utxoWitnesses[i].PowHash, registryRegisterUtxoConfidentialBlock.PowHash);
                Assert.Equal(utxoWitnesses[i].KeyImage, registryRegisterUtxoConfidentialBlock.KeyImage);
                Assert.Equal(utxoWitnesses[i].BlockType, registryRegisterUtxoConfidentialBlock.BlockType);
                Assert.Equal(utxoWitnesses[i].ReferencedPacketType, registryRegisterUtxoConfidentialBlock.ReferencedPacketType);
                Assert.Equal(utxoWitnesses[i].ReferencedBlockType, registryRegisterUtxoConfidentialBlock.ReferencedBlockType);
                Assert.Equal(utxoWitnesses[i].DestinationKey2, registryRegisterUtxoConfidentialBlock.DestinationKey2);
                Assert.Equal(utxoWitnesses[i].ReferencedBodyHash, registryRegisterUtxoConfidentialBlock.ReferencedBodyHash);
                Assert.Equal(utxoWitnesses[i].DestinationKey, registryRegisterUtxoConfidentialBlock.DestinationKey);
            }

            Assert.Equal(expectedShortBlockHash, block.ShortBlockHash);

            Assert.Equal(_publicKey, block.Signer.Value.ToArray());
            Assert.Equal(expectedSignature, block.Signature.ToArray());
        }
Esempio n. 4
0
        public void RegistryFullBlockSerializerTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryHelper.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = null;

            PacketType expectedReferencedPacketType = PacketType.Transactional;
            ushort     expectedReferencedBlockType  = BlockTypes.Transaction_TransferFunds;

            byte[] expectedReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643);
            byte[] expectedTarget             = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey());

            byte[] body;

            ushort expectedCount = 1000;

            RegistryRegisterBlock[]            stateWitnesses = new RegistryRegisterBlock[expectedCount];
            RegistryRegisterUtxoConfidential[] utxoWitnesses  = new RegistryRegisterUtxoConfidential[expectedCount];
            for (ushort i = 0; i < expectedCount; i++)
            {
                RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock
                {
                    SyncBlockHeight      = syncBlockHeight,
                    Nonce                = nonce + i,
                    PowHash              = BinaryHelper.GetPowHash(1234 + i),
                    BlockHeight          = blockHeight,
                    ReferencedPacketType = expectedReferencedPacketType,
                    ReferencedBlockType  = expectedReferencedBlockType,
                    ReferencedBodyHash   = BinaryHelper.GetDefaultHash(473826643 + i),
                    ReferencedTarget     = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey())
                };

                RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer();
                serializer1.Initialize(registryRegisterBlock);
                serializer1.SerializeBody();
                _signingService.Sign(registryRegisterBlock);
                serializer1.SerializeFully();

                stateWitnesses[i] = registryRegisterBlock;

                RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = new RegistryRegisterUtxoConfidential
                {
                    SyncBlockHeight      = syncBlockHeight,
                    Nonce                = nonce + i,
                    PowHash              = BinaryHelper.GetPowHash(1234 + i),
                    DestinationKey       = ConfidentialAssetsHelper.GetRandomSeed(),
                    ReferencedPacketType = expectedReferencedPacketType,
                    ReferencedBlockType  = expectedReferencedBlockType,
                    DestinationKey2      = ConfidentialAssetsHelper.GetRandomSeed(),
                    ReferencedBodyHash   = BinaryHelper.GetDefaultHash(473826643 + i),
                    KeyImage             = new Key32(ConfidentialAssetsHelper.GetRandomSeed()),
                    TransactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed(),
                    PublicKeys           = new IKey[] { new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()) },
                    Signatures           = new RingSignature[]
                    {
                        new RingSignature {
                            C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed()
                        },
                        new RingSignature {
                            C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed()
                        },
                        new RingSignature {
                            C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed()
                        }
                    }
                };

                RegistryRegisterUtxoConfidentialBlockSerializer serializer2 = new RegistryRegisterUtxoConfidentialBlockSerializer();
                serializer2.Initialize(registryRegisterUtxoConfidentialBlock);
                serializer2.SerializeFully();

                utxoWitnesses[i] = registryRegisterUtxoConfidentialBlock;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write((ushort)stateWitnesses.Length);
                    bw.Write((ushort)utxoWitnesses.Length);

                    foreach (RegistryRegisterBlock witness in stateWitnesses)
                    {
                        bw.Write(witness.RawData.ToArray());
                    }

                    foreach (RegistryRegisterUtxoConfidential witness in utxoWitnesses)
                    {
                        bw.Write(witness.RawData.ToArray());
                    }

                    bw.Write(BinaryHelper.GetDefaultHash(1111));
                }

                body = ms.ToArray();
            }

            byte[] expectedPacket = BinaryHelper.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,
                StateWitnesses  = stateWitnesses,
                UtxoWitnesses   = utxoWitnesses,
                ShortBlockHash  = BinaryHelper.GetDefaultHash(1111)
            };

            RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer();

            serializer.Initialize(block);
            serializer.SerializeBody();
            _signingService.Sign(block);

            byte[] actualPacket = serializer.GetBytes();

            Trace.WriteLine(expectedPacket.ToHexString());
            Trace.WriteLine(actualPacket.ToHexString());

            Assert.Equal(expectedPacket, actualPacket);
        }
Esempio n. 5
0
        public void RegistryRegisterBlockSerializerTransitionalTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryHelper.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = null;

            PacketType expectedReferencedPacketType = PacketType.Transactional;
            ushort     expectedReferencedBlockType  = BlockTypes.Transaction_TransferAssetToUtxo;

            byte[] expectedReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643);
            byte[] expectedTarget             = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey());
            byte[] transactionKey             = ConfidentialAssetsHelper.GetRandomSeed();

            byte[] body;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write((ushort)expectedReferencedPacketType);
                    bw.Write(expectedReferencedBlockType);
                    bw.Write(expectedReferencedBodyHash);
                    bw.Write(expectedTarget);
                    bw.Write(transactionKey);
                }

                body = ms.ToArray();
            }

            byte[] expectedPacket = BinaryHelper.GetSignedPacket(
                PacketType.Registry,
                syncBlockHeight,
                nonce, powHash, version,
                BlockTypes.Registry_Register, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);

            RegistryRegisterBlock block = new RegistryRegisterBlock
            {
                SyncBlockHeight          = syncBlockHeight,
                Nonce                    = nonce,
                PowHash                  = powHash,
                BlockHeight              = blockHeight,
                ReferencedPacketType     = expectedReferencedPacketType,
                ReferencedBlockType      = expectedReferencedBlockType,
                ReferencedBodyHash       = expectedReferencedBodyHash,
                ReferencedTarget         = expectedTarget,
                ReferencedTransactionKey = transactionKey
            };

            RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer();

            serializer.Initialize(block);
            serializer.SerializeBody();
            _signingService.Sign(block);

            byte[] actualPacket = serializer.GetBytes();

            Trace.WriteLine(expectedPacket.ToHexString());
            Trace.WriteLine(actualPacket.ToHexString());

            Assert.Equal(expectedPacket, actualPacket);
        }