Exemplo n.º 1
0
        public void Size_Get()
        {
            var test = MerkleBlockPayload.Create(Blockchain.GenesisBlock, new BitArray(1024, false));

            test.Size.Should().Be(270);

            test = MerkleBlockPayload.Create(Blockchain.GenesisBlock, new BitArray(0, false));
            test.Size.Should().Be(142);
        }
Exemplo n.º 2
0
        public void Size_Get()
        {
            var test = MerkleBlockPayload.Create(TestBlockchain.TheNeoSystem.GenesisBlock, new BitArray(1024, false));

            test.Size.Should().Be(247); // 239 + nonce

            test = MerkleBlockPayload.Create(TestBlockchain.TheNeoSystem.GenesisBlock, new BitArray(0, false));
            test.Size.Should().Be(119); // 111 + nonce
        }
Exemplo n.º 3
0
        private void OnGetDataMessageReceived(InvPayload payload)
        {
            foreach (UInt256 hash in payload.Hashes.Distinct())
            {
                IInventory inventory;
                if (!localNode.RelayCache.TryGet(hash, out inventory) && !localNode.ServiceEnabled)
                {
                    continue;
                }
                switch (payload.Type)
                {
                case InventoryType.TX:
                    if (inventory == null)
                    {
                        inventory = LocalNode.GetTransaction(hash);
                    }
                    if (inventory == null && Blockchain.Default != null)
                    {
                        inventory = Blockchain.Default.GetTransaction(hash);
                    }
                    if (inventory != null)
                    {
                        EnqueueMessage("tx", inventory);
                    }
                    break;

                case InventoryType.Block:
                    if (inventory == null && Blockchain.Default != null)
                    {
                        inventory = Blockchain.Default.GetBlock(hash);
                    }
                    if (inventory != null)
                    {
                        BloomFilter filter = bloom_filter;
                        if (filter == null)
                        {
                            EnqueueMessage("block", inventory);
                        }
                        else
                        {
                            Block    block = (Block)inventory;
                            BitArray flags = new BitArray(block.Transactions.Select(p => TestFilter(filter, p)).ToArray());
                            EnqueueMessage("merkleblock", MerkleBlockPayload.Create(block, flags));
                        }
                    }
                    break;

                case InventoryType.Consensus:
                    if (inventory != null)
                    {
                        EnqueueMessage("consensus", inventory);
                    }
                    break;
                }
            }
        }
Exemplo n.º 4
0
        public void DeserializeAndSerialize()
        {
            var test  = MerkleBlockPayload.Create(TestBlockchain.TheNeoSystem.GenesisBlock, new BitArray(2, false));
            var clone = test.ToArray().AsSerializable <MerkleBlockPayload>();

            Assert.AreEqual(test.TxCount, clone.TxCount);
            Assert.AreEqual(test.Hashes.Length, clone.TxCount);
            CollectionAssert.AreEqual(test.Hashes, clone.Hashes);
            CollectionAssert.AreEqual(test.Flags, clone.Flags);
        }
Exemplo n.º 5
0
        private void OnGetDataMessageReceived(InvPayload payload)
        {
            UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray();
            foreach (UInt256 hash in hashes)
            {
                Blockchain.Singleton.RelayCache.TryGet(hash, out IInventory inventory);
                switch (payload.Type)
                {
                case InventoryType.TX:
                    if (inventory == null)
                    {
                        inventory = Blockchain.Singleton.GetTransaction(hash);
                    }
                    if (inventory is Transaction)
                    {
                        Context.Parent.Tell(Message.Create("tx", inventory));
                    }
                    break;

                case InventoryType.Block:
                    if (inventory == null)
                    {
                        inventory = Blockchain.Singleton.GetBlock(hash);
                    }
                    if (inventory is Block block)
                    {
                        if (bloom_filter == null)
                        {
                            Context.Parent.Tell(Message.Create("block", inventory));
                        }
                        else
                        {
                            BitArray flags = new BitArray(block.Transactions.Select(p => bloom_filter.Test(p)).ToArray());
                            Context.Parent.Tell(Message.Create("merkleblock", MerkleBlockPayload.Create(block, flags)));
                        }
                    }
                    break;

                case InventoryType.Consensus:
                    if (inventory != null)
                    {
                        Context.Parent.Tell(Message.Create("consensus", inventory));
                    }
                    break;
                }
            }
        }
Exemplo n.º 6
0
        private bool OnGetBlockData(UInt256 hash)
        {
            Block block = blockchain.GetBlock(hash);

            if (block == null)
            {
                return(false);
            }

            if (bloom_filter == null)
            {
                Context.Parent.Tell(Message.Create(MessageType.Block, block));
            }
            else
            {
                BitArray flags = new BitArray(block.Transactions.Select(p => bloom_filter.Test(p)).ToArray());
                Context.Parent.Tell(Message.Create(MessageType.MerkleBlock, MerkleBlockPayload.Create(block, flags)));
            }
            return(true);
        }
Exemplo n.º 7
0
        private void OnGetBlockDataMessageReceived(GetBlockDataPayload payload)
        {
            for (uint i = payload.IndexStart, max = payload.IndexStart + payload.Count; i < max; i++)
            {
                Block block = Blockchain.Singleton.Store.GetBlock(i);
                if (block == null)
                {
                    break;
                }

                if (bloom_filter == null)
                {
                    Context.Parent.Tell(Message.Create(MessageCommand.Block, block));
                }
                else
                {
                    BitArray flags = new BitArray(block.Transactions.Select(p => bloom_filter.Test(p)).ToArray());
                    Context.Parent.Tell(Message.Create(MessageCommand.MerkleBlock, MerkleBlockPayload.Create(block, flags)));
                }
            }
        }
Exemplo n.º 8
0
        private void OnGetDataMessageReceived(InvPayload payload)
        {
            UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray();
            foreach (UInt256 hash in hashes)
            {
                switch (payload.Type)
                {
                case InventoryType.TX:
                    Transaction tx = Blockchain.Singleton.GetTransaction(hash);
                    if (tx != null)
                    {
                        Context.Parent.Tell(Message.Create(MessageCommand.Transaction, tx));
                    }
                    break;

                case InventoryType.Block:
                    Block block = Blockchain.Singleton.GetBlock(hash);
                    if (block != null)
                    {
                        if (bloom_filter == null)
                        {
                            Context.Parent.Tell(Message.Create(MessageCommand.Block, block));
                        }
                        else
                        {
                            BitArray flags = new BitArray(block.Transactions.Select(p => bloom_filter.Test(p)).ToArray());
                            Context.Parent.Tell(Message.Create(MessageCommand.MerkleBlock, MerkleBlockPayload.Create(block, flags)));
                        }
                    }
                    break;

                case InventoryType.Consensus:
                    if (Blockchain.Singleton.ConsensusRelayCache.TryGet(hash, out IInventory inventoryConsensus))
                    {
                        Context.Parent.Tell(Message.Create(MessageCommand.Consensus, inventoryConsensus));
                    }
                    break;
                }
            }
        }