public void TestSetup() { TestBlockchain.InitializeMockNeoSystem(); var rand = new Random(); var mockWallet = new Mock <Wallet>(); mockWallet.Setup(p => p.GetAccount(It.IsAny <UInt160>())).Returns <UInt160>(p => new TestWalletAccount(p)); // Create dummy validators _validatorKeys = new KeyPair[7]; for (int x = 0; x < _validatorKeys.Length; x++) { var pk = new byte[32]; rand.NextBytes(pk); _validatorKeys[x] = new KeyPair(pk); } _context = new ConsensusContext(mockWallet.Object, Blockchain.Singleton.Store) { Validators = _validatorKeys.Select(u => u.PublicKey).ToArray() }; _context.Reset(0); }
private void InitializeConsensus(byte view_number) { lock (context) { if (view_number == 0) { context.Reset(wallet); } else { context.ChangeView(view_number); } if (context.MinerIndex < 0) { return; } Log($"{nameof(InitializeConsensus)} h:{context.Height} v:{view_number} i:{context.MinerIndex} s:{(context.MinerIndex == context.PrimaryIndex ? ConsensusState.Primary : ConsensusState.Backup)}"); if (context.MinerIndex == context.PrimaryIndex) { context.State |= ConsensusState.Primary; timer_height = context.Height; 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.Height; timer_view = view_number; timer.Change(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (view_number + 1)), Timeout.InfiniteTimeSpan); } } }
Block RunConsensus() { if (chain.ConsensusNodes.Count == 1) { var ctx = new ConsensusContext(nodeWallet, Blockchain.Singleton.Store); ctx.Reset(0); ctx.MakePrepareRequest(); ctx.MakeCommit(); return(ctx.CreateBlock()); } // create ConsensusContext for each ConsensusNode var contexts = new ConsensusContext[chain.ConsensusNodes.Count]; for (int x = 0; x < contexts.Length; x++) { contexts[x] = new ConsensusContext(DevWallet.FromExpressWallet(chain.ConsensusNodes[x].Wallet), Blockchain.Singleton.Store); contexts[x].Reset(0); } // find the primary node for this consensus round var primary = contexts.Single(c => c.IsPrimary); var prepareRequestPayload = primary.MakePrepareRequest(); for (int x = 0; x < contexts.Length; x++) { var context = contexts[x]; if (context.MyIndex == primary.MyIndex) { continue; } var prepareRequestMessage = context.GetMessage <PrepareRequest>(prepareRequestPayload); OnPrepareRequestReceived(context, prepareRequestPayload, prepareRequestMessage); var commitPayload = context.MakeCommit(); var commitMessage = primary.GetMessage <Commit>(commitPayload); OnCommitReceived(primary, commitPayload, commitMessage); } return(primary.CreateBlock()); }