Пример #1
0
        public void RegistryRegisterBlockSerializerTest()
        {
            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;

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

                body = ms.ToArray();
            }

            byte[] expectedPacket = BinaryBuilder.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
            };

            RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository);

            serializer.Initialize(block);

            byte[] actualPacket = serializer.GetBytes();

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

            Assert.Equal(expectedPacket, actualPacket);
        }
Пример #2
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);
        }