Ejemplo n.º 1
0
 private void InitializeConsensus(byte view_number)
 {
     if (view_number == 0)
     {
         context.Reset(wallet);
     }
     else
     {
         context.ChangeView(view_number);
     }
     if (context.MyIndex < 0)
     {
         return;
     }
     Log($"initialize: height={context.BlockIndex} view={view_number} index={context.MyIndex} role={(context.MyIndex == context.PrimaryIndex ? ConsensusState.Primary : ConsensusState.Backup)}");
     if (context.MyIndex == context.PrimaryIndex)
     {
         context.State |= ConsensusState.Primary;
         TimeSpan span = DateTime.Now - block_received_time;
         if (span >= Blockchain.TimePerBlock)
         {
             ChangeTimer(TimeSpan.Zero);
         }
         else
         {
             ChangeTimer(Blockchain.TimePerBlock - span);
         }
     }
     else
     {
         context.State = ConsensusState.Backup;
         ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (view_number + 1)));
     }
 }
Ejemplo n.º 2
0
 private void InitializeConsensus(byte view_number)
 {
     lock (context)
     {
         if (view_number == 0)
         {
             context.Reset(wallet);
         }
         else
         {
             context.ChangeView(view_number);
         }
         if (context.MyIndex < 0)
         {
             return;
         }
         Log($"initialize: height={context.BlockIndex} view={view_number} index={context.MyIndex} role={(context.MyIndex == context.PrimaryIndex ? ConsensusState.Primary : ConsensusState.Backup)}");
         if (context.MyIndex == context.PrimaryIndex)
         {
             context.State |= ConsensusState.Primary;
             if (!context.State.HasFlag(ConsensusState.SignatureSent))
             {
                 FillContext();
             }
             if (context.TransactionHashes.Length > 1)
             {
                 InvPayload invPayload = InvPayload.Create(InventoryType.TX, context.TransactionHashes.Skip(1).ToArray());
                 foreach (RemoteNode node in localNode.GetRemoteNodes())
                 {
                     node.EnqueueMessage("inv", invPayload);
                 }
             }
             timer_height = context.BlockIndex;
             timer_view   = view_number;
             // 议长发起共识时间控制
             TimeSpan span = DateTime.Now - block_received_time;
             if (span >= Blockchain.TimePerBlock)
             {
                 timer.Change(0, Timeout.Infinite);// 间隔时间大于预订时间则立即发起共识
             }
             else
             {
                 timer.Change(Blockchain.TimePerBlock - span, Timeout.InfiniteTimeSpan);// 定时执行
             }
         }
         else
         {
             context.State = ConsensusState.Backup;
             timer_height  = context.BlockIndex;
             timer_view    = view_number;
             // 议员超时控制 t * 2 ^ (view_number + 1)
             timer.Change(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (view_number + 1)), Timeout.InfiniteTimeSpan);
         }
     }
 }
Ejemplo n.º 3
0
 private void InitializeConsensus(byte view_number)
 {
     if (view_number == 0)
     {
         context.Reset();
     }
     else
     {
         context.ChangeView(view_number);
     }
     if (context.MyIndex < 0)
     {
         return;
     }
     if (view_number > 0)
     {
         Log($"changeview: view={view_number} primary={context.Validators[context.GetPrimaryIndex((byte)(view_number - 1u))]}", LogLevel.Warning);
     }
     Log($"initialize: height={context.BlockIndex} view={view_number} index={context.MyIndex} role={(context.MyIndex == context.PrimaryIndex ? ConsensusState.Primary : ConsensusState.Backup)}");
     if (context.MyIndex == context.PrimaryIndex)
     {
         context.State |= ConsensusState.Primary;
         TimeSpan span = DateTime.UtcNow - block_received_time;
         if (span >= Blockchain.TimePerBlock)
         {
             ChangeTimer(TimeSpan.Zero);
         }
         else
         {
             ChangeTimer(Blockchain.TimePerBlock - span);
         }
     }
     else
     {
         context.State = ConsensusState.Backup;
         ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (view_number + 1)));
     }
 }