private void CheckAuthorizedAllOk(AuthState state) { if (state.GetIsAuthoringSuccess(_board)) { if (state.Saving) { return; } state.Saving = true; _ = Task.Run(() => { // do commit var block = state.InputMsg.Block; block.Authorizations = state.OutputMsgs.Select(a => a.AuthSign).ToList(); if (block.BlockType != BlockTypes.Consolidation) { // pickup UIndex try { block.UIndex = state.ConsensusUIndex; } catch (Exception ex) { _log.LogError("Can't get UIndex. System fail: " + ex.Message); return; } } if (block.UIndex != _UIndexSeed - 1) { // local node out of sync _UIndexSeed = block.UIndex + 1; Mode = ConsensusWorkingMode.OutofSyncWaiting; LyraSystem.Singleton.TheBlockchain.Tell(new BlockChain.NeedSync { ToUIndex = block.UIndex }); } block.UHash = SignableObject.CalculateHash($"{block.UIndex}|{block.Index}|{block.Hash}"); BlockChain.Singleton.AddBlock(block); var msg = new AuthorizerCommitMsg { From = NodeService.Instance.PosWallet.AccountId, MsgType = ChatMessageType.AuthorizerCommit, BlockHash = state.InputMsg.Block.Hash, BlockIndex = block.UIndex, Commited = true }; state.AddCommitedResult(msg); Send2P2pNetwork(msg); _log.LogInformation($"Consensus: OnPrepare Commited: BlockUIndex: {msg.BlockHash}"); }); } }
private AuthState CreateAuthringState(AuthorizingMsg item) { _log.LogInformation($"Consensus: CreateAuthringState Called: BlockUIndex: {item.Block.UIndex}"); var ukey = item.Block.Hash; if (_activeConsensus.ContainsKey(ukey)) { return(_activeConsensus[ukey]); } var state = new AuthState { HashOfFirstBlock = ukey, InputMsg = item, }; // add possible out of ordered messages belong to the block if (_outOfOrderedMessages.ContainsKey(item.Block.Hash)) { var msgs = _outOfOrderedMessages[item.Block.Hash]; _outOfOrderedMessages.Remove(item.Block.Hash); foreach (var msg in msgs) { switch (msg) { case AuthorizedMsg authorized: state.AddAuthResult(authorized); break; case AuthorizerCommitMsg committed: state.AddCommitedResult(committed); break; } } } _activeConsensus.Add(ukey, state); return(state); }