Esempio 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)));
     }
 }
Esempio 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);
         }
     }
 }
Esempio n. 3
0
        private void InitializeConsensus(byte view_number)
        {
            if (view_number == 0)
            {
                context.Reset(wallet);
            }
            TimeSpan span = DateTime.UtcNow - block_received_time;

            if (span >= Blockchain.TimePerBlock)
            {
                ChangeTimer(TimeSpan.Zero);
            }
            else
            {
                ChangeTimer(Blockchain.TimePerBlock - span);
            }
        }
Esempio n. 4
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. 5
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)));
     }
 }