Пример #1
0
        public void TestExceptWith()
        {
            var a = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01
            });
            var b = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x02
            });
            var c = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x03
            });

            var set = new FIFOSet <UInt256>(10);

            set.Add(a);
            set.Add(b);
            set.Add(c);
            set.ExceptWith(new UInt256[] { b, c });
            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a });
        }
Пример #2
0
        private void OnGetDataMessageReceived(Message msg)
        {
            InvPayload payload = msg.GetPayload <InvPayload>();

            if (sentHashes.Add(payload.Hashes[0]))
            {
                if (OnGetInventoryData(payload.Hashes[0], payload.Type))
                {
                    Context.Parent.Tell(RemoteNode.NewCounterMessage(RemoteNode.CounterType.Send, payload.Type, 1));
                }
            }
        }
Пример #3
0
        private void OnInvMessageReceived(InvPayload payload)
        {
            UInt256[] hashes = payload.Hashes.Where(p => knownHashes.Add(p) && !sentHashes.Contains(p)).ToArray();
            if (hashes.Length == 0)
            {
                return;
            }
            switch (payload.Type)
            {
            case InventoryType.Block:
                using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
                    hashes = hashes.Where(p => !snapshot.ContainsBlock(p)).ToArray();
                break;

            case InventoryType.TX:
                using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
                    hashes = hashes.Where(p => !snapshot.ContainsTransaction(p)).ToArray();
                break;
            }
            if (hashes.Length == 0)
            {
                return;
            }
            system.TaskManager.Tell(new TaskManager.NewTasks {
                Payload = InvPayload.Create(payload.Type, hashes)
            }, Context.Parent);
        }
Пример #4
0
        //private void OnGetBlocksMessageReceived(GetBlocksPayload payload)
        //{
        //    UInt256 hash = payload.HashStart;
        //    int count = payload.Count < 0 ? InvPayload.MaxHashesCount : payload.Count;
        //    TrimmedBlock state = Blockchain.Singleton.Store.GetBlocks().TryGet(hash);
        //    if (state == null) return;
        //    List<UInt256> hashes = new List<UInt256>();
        //    for (uint i = 1; i <= count; i++)
        //    {
        //        uint index = state.Index + i;
        //        if (index > Blockchain.Singleton.Height)
        //            break;
        //        hash = Blockchain.Singleton.GetBlockHash(index);
        //        if (hash == null) break;
        //        hashes.Add(hash);
        //    }
        //    if (hashes.Count == 0) return;
        //    Context.Parent.Tell(Message.Create(MessageCommand.Inv, InvPayload.Create(InventoryType.Block, hashes.ToArray())));
        //}

        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;
            //    }
            //}
        }
Пример #5
0
        public void TestAdd()
        {
            var a = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01
            });
            var b = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x02
            });
            var set = new FIFOSet <UInt256>(1, 1);

            set.Add(a);
            set.Add(b);
            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { b });
        }
Пример #6
0
        public void FifoSetTest()
        {
            var a = UInt256.Zero;
            var b = new UInt256();
            var c = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01
            });

            var set = new FIFOSet <UInt256>(3);

            Assert.IsTrue(set.Add(a));
            Assert.IsFalse(set.Add(a));
            Assert.IsFalse(set.Add(b));
            Assert.IsTrue(set.Add(c));

            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a, c });
        }
Пример #7
0
 private void OnTaskCompleted(UInt256 hash)
 {
     knownHashes.Add(hash);
     globalTasks.Remove(hash);
     foreach (TaskSession ms in sessions.Values)
     {
         ms.AvailableTasks.Remove(hash);
     }
     if (sessions.TryGetValue(Sender, out TaskSession session))
     {
         session.Tasks.Remove(hash);
         RequestTasks(session);
     }
 }
Пример #8
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;
                }
            }
        }
Пример #9
0
        public void FIFOSetTest()
        {
            var a = UInt256.Zero;
            var b = new UInt256();
            var c = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01
            });

            var set = new FIFOSet <UInt256>(3);

            Assert.IsTrue(set.Add(a));
            Assert.IsFalse(set.Add(a));
            Assert.IsFalse(set.Add(b));
            Assert.IsTrue(set.Add(c));

            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a, c });

            var d = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x02
            });

            // Testing Fifo max size
            Assert.IsTrue(set.Add(d));
            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a, c, d });

            var e = new UInt256(new byte[32] {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x03
            });

            Assert.IsTrue(set.Add(e));
            Assert.IsFalse(set.Add(e));
            CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { c, d, e });
        }