Ejemplo n.º 1
0
        public ProtocolHandler(ZoroSystem system, LocalNode localNode, Blockchain blockchain, RemoteNode remoteNode)
        {
            this.system     = system;
            this.localNode  = localNode;
            this.blockchain = blockchain;
            this.remoteNode = remoteNode;

            InitMessageHandlers();

            this.knownHashes = new FIFOSet <UInt256>(blockchain.MemPool.Capacity * 2);
            this.sentHashes  = new FIFOSet <UInt256>(blockchain.MemPool.Capacity * 2);
        }
Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 3
0
        private void OnGetTxnMessageReceived(Message msg)
        {
            InvPayload payload = msg.GetPayload <InvPayload>();

            if (payload.Type != InventoryType.TX)
            {
                throw new InvalidOperationException();
            }

            UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray();
            if (hashes.Length == 0)
            {
                return;
            }

            blockchain.Log($"OnGetTxn begin, count:{payload.Hashes.Length}, [{remoteNode.Remote.Address}]", Plugins.LogLevel.Debug);

            List <Transaction> transactions = new List <Transaction>();

            foreach (UInt256 hash in hashes)
            {
                Transaction tx = blockchain.GetTransaction(hash);
                if (tx != null)
                {
                    transactions.Add(tx);
                }
            }
            int count = transactions.Count;

            if (count > 0)
            {
                if (ProtocolSettings.Default.EnableCompressedRawTxn)
                {
                    foreach (CompressedTransactionPayload ctx_payload in CompressedTransactionPayload.CreateGroup(transactions.ToArray()))
                    {
                        Context.Parent.Tell(Message.Create(MessageType.CompressedTxn, ctx_payload));
                    }
                }
                else
                {
                    foreach (RawTransactionPayload rtx_payload in RawTransactionPayload.CreateGroup(transactions.ToArray()))
                    {
                        Context.Parent.Tell(Message.Create(MessageType.RawTxn, rtx_payload));
                    }
                }

                Context.Parent.Tell(RemoteNode.NewCounterMessage(RemoteNode.CounterType.Send, payload.Type, count));
            }

            blockchain.Log($"OnGetTxn end, count:{hashes.Length}=>{count}, [{remoteNode.Remote.Address}]", Plugins.LogLevel.Debug);
        }
Ejemplo n.º 4
0
 private void OnTaskCompleted(UInt256 hash, InventoryType type)
 {
     knownHashes.Add(hash);
     globalTasks.Remove(hash);
     foreach (TaskSession ms in sessions.Values)
     {
         ms.AvailableTasks.Remove(hash);
     }
     if (sessions.TryGetValue(Sender, out TaskSession session))
     {
         if (type == InventoryType.Block)
         {
             CompleteSyncBlockTask(hash, session);
         }
         Sender.Tell(RemoteNode.NewCounterMessage(RemoteNode.CounterType.Received, type));
         session.Tasks.Remove(hash);
         RequestTasks(session);
     }
 }
Ejemplo n.º 5
0
        private void OnTimer()
        {
            CheckSyncTimeout();

            foreach (TaskSession session in sessions.Values)
            {
                foreach (var task in session.Tasks.ToArray())
                {
                    if (DateTime.UtcNow - task.Value.BeginTime > TaskTimeout)
                    {
                        globalTasks.Remove(task.Key);
                        session.Tasks.Remove(task.Key);
                        session.RemoteNode.Tell(RemoteNode.NewCounterMessage(RemoteNode.CounterType.Timeout, task.Value.Type));
                    }
                }
            }
            foreach (TaskSession session in sessions.Values)
            {
                RequestTasks(session);
            }

            ClearKnownHashes();
        }
Ejemplo n.º 6
0
 protected override Props ProtocolProps(object connection, IPEndPoint remote, IPEndPoint local)
 {
     return(RemoteNode.Props(system, connection, remote, local, this));
 }
Ejemplo n.º 7
0
 public static Props Props(ZoroSystem system, LocalNode localNode, Blockchain blockchain, RemoteNode remoteNode)
 {
     return(Akka.Actor.Props.Create(() => new ProtocolHandler(system, localNode, blockchain, remoteNode)).WithMailbox("protocol-handler-mailbox"));
 }