Esempio n. 1
0
 private void InitializeConsensus(byte viewNumber)
 {
     context.Reset(viewNumber);
     if (viewNumber > 0)
     {
         Log($"View changed: view={viewNumber} primary={context.Validators[context.GetPrimaryIndex((byte)(viewNumber - 1u))]}", LogLevel.Warning);
     }
     Log($"Initialize: height={context.Block.Index} view={viewNumber} index={context.MyIndex} role={(context.IsPrimary ? "Primary" : context.WatchOnly ? "WatchOnly" : "Backup")}");
     if (context.WatchOnly)
     {
         return;
     }
     if (context.IsPrimary)
     {
         if (isRecovering)
         {
             ChangeTimer(TimeSpan.FromMilliseconds(Blockchain.MillisecondsPerBlock << (viewNumber + 1)));
         }
         else
         {
             TimeSpan span = Blockchain.TimePerBlock;
             if (block_received_index + 1 == context.Block.Index)
             {
                 var diff = TimeProvider.Current.UtcNow - block_received_time;
                 if (diff >= span)
                 {
                     span = TimeSpan.Zero;
                 }
                 else
                 {
                     span -= diff;
                 }
             }
             ChangeTimer(span);
         }
     }
     else
     {
         ChangeTimer(TimeSpan.FromMilliseconds(Blockchain.MillisecondsPerBlock << (viewNumber + 1)));
     }
 }
Esempio n. 2
0
 private void InitializeConsensus(byte view_number)
 {
     if (view_number == 0)
     {
         context.Reset(wallet);
     }
     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)));
     }
 }