Ejemplo n.º 1
0
 private void OnTimeout(object state)
 {
     lock (context)
     {
         if (timer_height != context.BlockIndex || timer_view != context.ViewNumber)
         {
             return;
         }
         Log($"timeout: height={timer_height} view={timer_view} state={context.State}");
         if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent))
         {
             Log($"send perpare request: height={timer_height} view={timer_view}");
             context.State |= ConsensusState.RequestSent;
             if (!context.State.HasFlag(ConsensusState.SignatureSent))
             {
                 context.Timestamp = Math.Max(DateTime.Now.ToTimestamp(), Blockchain.Default.GetHeader(context.PrevHash).Timestamp + 1);
                 context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
             }
             SignAndRelay(context.MakePrepareRequest());
             timer.Change(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (timer_view + 1)), Timeout.InfiniteTimeSpan);
         }
         else if ((context.State.HasFlag(ConsensusState.Primary) && context.State.HasFlag(ConsensusState.RequestSent)) || context.State.HasFlag(ConsensusState.Backup))
         {
             RequestChangeView();
         }
     }
 }
Ejemplo n.º 2
0
 private void OnTimer(Timer timer)
 {
     if (timer.Height != context.BlockIndex || timer.ViewNumber != context.ViewNumber)
     {
         return;
     }
     Log($"timeout: height={timer.Height} view={timer.ViewNumber} state={context.State}");
     if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent))
     {
         Log($"send prepare request: height={timer.Height} view={timer.ViewNumber}");
         context.State |= ConsensusState.RequestSent;
         if (!context.State.HasFlag(ConsensusState.SignatureSent))
         {
             FillContext();
             context.Timestamp = Math.Max(DateTime.UtcNow.ToTimestamp(), context.Snapshot.GetHeader(context.PrevHash).Timestamp + 1);
             context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
         }
         SignAndRelay(context.MakePrepareRequest());
         if (context.TransactionHashes.Length > 1)
         {
             foreach (InvPayload payload in InvPayload.CreateGroup(InventoryType.TX, context.TransactionHashes.Skip(1).ToArray()))
             {
                 system.LocalNode.Tell(Message.Create("inv", payload));
             }
         }
         ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (timer.ViewNumber + 1)));
     }
     else if ((context.State.HasFlag(ConsensusState.Primary) && context.State.HasFlag(ConsensusState.RequestSent)) || context.State.HasFlag(ConsensusState.Backup))
     {
         RequestChangeView();
     }
 }
Ejemplo n.º 3
0
 private void OnTimeout(object state)
 {
     lock (context)
     {
         if (timer_height != context.BlockIndex || timer_view != context.ViewNumber)
         {
             return;
         }
         Log($"timeout: height={timer_height} view={timer_view} state={context.State}");
         if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent))
         {
             Log($"send perpare request: height={timer_height} view={timer_view}");
             context.State |= ConsensusState.RequestSent;
             if (!context.State.HasFlag(ConsensusState.SignatureSent))
             {
                 context.Timestamp = Math.Max(DateTime.Now.ToTimestamp(), Blockchain.Default.GetHeader(context.PrevHash).Timestamp + 1);
                 context.Nonce     = GetNonce();
                 List <Transaction> transactions = LocalNode.GetMemoryPool().Where(p => CheckPolicy(p)).ToList();
                 if (transactions.Count >= Settings.Default.MaxTransactionsPerBlock)
                 {
                     transactions = transactions.OrderByDescending(p => p.NetworkFee / p.Size).Take(Settings.Default.MaxTransactionsPerBlock - 1).ToList();
                 }
                 transactions.Insert(0, CreateMinerTransaction(transactions, context.BlockIndex, context.Nonce));
                 context.TransactionHashes           = transactions.Select(p => p.Hash).ToArray();
                 context.Transactions                = transactions.ToDictionary(p => p.Hash);
                 context.NextConsensus               = Blockchain.GetConsensusAddress(Blockchain.Default.GetValidators(transactions).ToArray());
                 context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
             }
             InvPayload invPayload = InvPayload.Create(InventoryType.TX, context.TransactionHashes);
             foreach (RemoteNode node in localNode.GetRemoteNodes())
             {
                 node.EnqueueMessage("inv", invPayload);
             }
             SignAndRelay(context.MakePrepareRequest());
             timer.Change(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (timer_view + 1)), Timeout.InfiniteTimeSpan);
         }
         else if ((context.State.HasFlag(ConsensusState.Primary) && context.State.HasFlag(ConsensusState.RequestSent)) || context.State.HasFlag(ConsensusState.Backup))
         {
             RequestChangeView();
         }
     }
 }
Ejemplo n.º 4
0
        private void SendPrepareRequest()
        {
            Log($"Sending {nameof(PrepareRequest)}: height={context.Block.Index} view={context.ViewNumber}");
            localNode.Tell(new LocalNode.SendDirectly {
                Inventory = context.MakePrepareRequest()
            });

            if (context.Validators.Length == 1)
            {
                CheckPreparations();
            }

            if (context.TransactionHashes.Length > 0)
            {
                foreach (InvPayload payload in InvPayload.CreateGroup(InventoryType.TX, context.TransactionHashes))
                {
                    localNode.Tell(Message.Create(MessageCommand.Inv, payload));
                }
            }
            ChangeTimer(TimeSpan.FromMilliseconds((neoSystem.Settings.MillisecondsPerBlock << (context.ViewNumber + 1)) - (context.ViewNumber == 0 ? neoSystem.Settings.MillisecondsPerBlock : 0)));
        }
Ejemplo n.º 5
0
 private void OnTimer(Timer timer)
 {
     if (context.State.HasFlag(ConsensusState.BlockSent))
     {
         return;
     }
     if (timer.Height != context.BlockIndex || timer.ViewNumber != context.ViewNumber)
     {
         return;
     }
     Log($"timeout: height={timer.Height} view={timer.ViewNumber} state={context.State}");
     if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent))
     {
         Log($"send prepare request: height={timer.Height} view={timer.ViewNumber}");
         context.State |= ConsensusState.RequestSent;
         if (!context.State.HasFlag(ConsensusState.SignatureSent))
         {
             context.Fill();
             context.SignHeader();
         }
         system.LocalNode.Tell(new LocalNode.SendDirectly {
             Inventory = context.MakePrepareRequest()
         });
         if (context.TransactionHashes.Length > 1)
         {
             foreach (InvPayload payload in InvPayload.CreateGroup(InventoryType.TX, context.TransactionHashes.Skip(1).ToArray()))
             {
                 system.LocalNode.Tell(Message.Create("inv", payload));
             }
         }
         ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (timer.ViewNumber + 1)));
     }
     else if ((context.State.HasFlag(ConsensusState.Primary) && context.State.HasFlag(ConsensusState.RequestSent)) || context.State.HasFlag(ConsensusState.Backup))
     {
         RequestChangeView();
     }
 }