示例#1
0
 private void InitializeConsensus(byte viewNumber)
 {
     context.Reset(viewNumber);
     if (viewNumber > 0)
     {
         Log($"changeview: view={viewNumber} primary={context.Validators[context.GetPrimaryIndex((byte)(viewNumber - 1u))]}", LogLevel.Warning);
     }
     Log($"initialize: height={context.BlockIndex} view={viewNumber} index={context.MyIndex} role={(context.IsPrimary() ? "Primary" : context.WatchOnly() ? "WatchOnly" : "Backup")}");
     if (context.WatchOnly())
     {
         return;
     }
     if (context.IsPrimary())
     {
         if (isRecovering)
         {
             ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (viewNumber + 1)));
         }
         else
         {
             TimeSpan span = Blockchain.TimePerBlock;
             if (block_received_index + 1 == context.BlockIndex)
             {
                 var diff = TimeProvider.Current.UtcNow - block_received_time;
                 if (diff >= span)
                 {
                     span = TimeSpan.Zero;
                 }
                 else
                 {
                     span -= diff;
                 }
             }
             ChangeTimer(span);
         }
     }
     else
     {
         ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (viewNumber + 1)));
     }
 }
示例#2
0
 private void InitializeConsensus(ushort 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((ushort)(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 = TimeProvider.Current.UtcNow - block_received_time;
         if (span >= Blockchain.TimePerBlock)
         {
             ChangeTimer(TimeSpan.Zero);
         }
         else
         {
             ChangeTimer(Blockchain.TimePerBlock - span);
         }
     }
     else
     {
         context.State = ConsensusState.Backup;
         SetNextTimer(view_number + 1);
     }
     ProcessUnhandledPayload();
 }