Exemplo n.º 1
0
        public void RegistryShortBlockParserTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

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

            byte[] prevHash = null;

            byte[] body;

            ushort expectedCount = 10;

            Random random = new Random();

            WitnessStateKey[] witnessStateKeys = new WitnessStateKey[expectedCount];
            WitnessUtxoKey[]  witnessUtxoKeys  = new WitnessUtxoKey[expectedCount];
            for (ushort i = 0; i < expectedCount; i++)
            {
                witnessStateKeys[i] = new WitnessStateKey {
                    PublicKey = new Key32(ConfidentialAssetsHelper.GetRandomSeed()), Height = (ulong)random.Next(0, int.MaxValue)
                };
                witnessUtxoKeys[i] = new WitnessUtxoKey {
                    KeyImage = new Key32(ConfidentialAssetsHelper.GetRandomSeed())
                };
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write((ushort)witnessStateKeys.Length);
                    bw.Write((ushort)witnessUtxoKeys.Length);

                    foreach (WitnessStateKey witnessStateKey in witnessStateKeys)
                    {
                        bw.Write(witnessStateKey.PublicKey.Value.ToArray());
                        bw.Write(witnessStateKey.Height);
                    }

                    foreach (WitnessUtxoKey witnessUtxoKey in witnessUtxoKeys)
                    {
                        bw.Write(witnessUtxoKey.KeyImage.Value.ToArray());
                    }
                }

                body = ms.ToArray();
            }

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

            RegistryShortBlockParser registryFullBlockParser = new RegistryShortBlockParser(_identityKeyProvidersRegistry);
            RegistryShortBlock       block = (RegistryShortBlock)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 < witnessStateKeys.Length; i++)
            {
                Assert.Equal(witnessStateKeys[i].PublicKey, block.WitnessStateKeys[i].PublicKey);
                Assert.Equal(witnessStateKeys[i].Height, block.WitnessStateKeys[i].Height);
            }

            for (int i = 0; i < witnessUtxoKeys.Length; i++)
            {
                Assert.Equal(witnessUtxoKeys[i].KeyImage, block.WitnessUtxoKeys[i].KeyImage);
            }

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

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

            byte[] prevHash = null;

            byte[] body;

            ushort expectedCount = 10;

            SortedList <ushort, IKey> transactionHeaders = new SortedList <ushort, IKey>();

            for (ushort i = 0; i < expectedCount; i++)
            {
                transactionHeaders.Add(i, new Key16(BinaryBuilder.GetTransactionKeyHash(i)));
            }

            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].Value.ToArray());
                    }
                }

                body = ms.ToArray();
            }

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

            RegistryShortBlockParser registryFullBlockParser = new RegistryShortBlockParser(_identityKeyProvidersRegistry, _hashCalculationRepository);
            RegistryShortBlock       block = (RegistryShortBlock)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)
            {
                Assert.True(block.TransactionHeaderHashes.ContainsKey(item.Key));
                Assert.Equal(item.Value, block.TransactionHeaderHashes[item.Key]);
            }

            Assert.Equal(_publicKey, block.Signer.Value.ToArray());
            Assert.Equal(expectedSignature, block.Signature.ToArray());
        }