private async Task <AuthState> PostToConsensusAsync(TransactionBlock block) { _log.LogInformation($"ApiService: PostToConsensusAsync Called: {block.BlockType}"); //AuthorizingMsg msg = new AuthorizingMsg //{ // From = NodeService.Instance.PosWallet.AccountId, // Block = block, // MsgType = ChatMessageType.AuthorizerPrePrepare //}; AuthorizingMsg msg = new AuthorizingMsg { From = NodeService.Instance.PosWallet.AccountId, Block = block, MsgType = ChatMessageType.AuthorizerPrePrepare }; var state = new AuthState(true); state.SetView(await BlockChain.Singleton.GetLastServiceBlockAsync()); state.InputMsg = msg; DagSystem.Singleton.Consensus.Tell(state); await state.Done.AsTask(); state.Done.Close(); state.Done = null; var ts1 = state.T1 == null ? "" : ((int)(DateTime.Now - state.T1).TotalMilliseconds).ToString(); var ts2 = state.T2 == null ? "" : ((int)(DateTime.Now - state.T2).TotalMilliseconds).ToString(); var ts3 = state.T3 == null ? "" : ((int)(DateTime.Now - state.T3).TotalMilliseconds).ToString(); var ts4 = state.T4 == null ? "" : ((int)(DateTime.Now - state.T4).TotalMilliseconds).ToString(); var ts5 = state.T5 == null ? "" : ((int)(DateTime.Now - state.T5).TotalMilliseconds).ToString(); _log.LogInformation($"ApiService Timing:\n{ts1}\n{ts2}\n{ts3}\n{ts4}\n{ts5}\n"); var resultMsg = state.OutputMsgs.Count > 0 ? state.OutputMsgs.First().Result.ToString() : "Unknown"; _log.LogInformation($"ApiService: PostToConsensusAsync Exited: IsAuthoringSuccess: {state?.CommitConsensus == ConsensusResult.Yay} with {resultMsg}"); if (state.CommitConsensus == ConsensusResult.Yay) { return(state); } else { return(null); } }
internal async Task <AuthorizationAPIResult> Pre_PrepareAsync(TransactionBlock block1, Func <TransactionBlock, Task <TransactionBlock> > OnBlockSucceed = null) { bool IsSuccess; AuthState state2 = null; var state1 = await PostToConsensusAsync(block1).ConfigureAwait(false); if (state1 != null && state1.CommitConsensus == ConsensusResult.Yay) { IsSuccess = true; //fee is the bottle neck!!! must do lazy fee collection by consolidation if (OnBlockSucceed != null) { var block2 = await OnBlockSucceed(state1.InputMsg.Block as TransactionBlock).ConfigureAwait(false); if (block2 != null) { state2 = await PostToConsensusAsync(block2).ConfigureAwait(false); } } } else { IsSuccess = false; } var result = new AuthorizationAPIResult(); if (IsSuccess) { result.ResultCode = APIResultCodes.Success; } else if (state1 == null) { result.ResultCode = APIResultCodes.BlockFailedToBeAuthorized; } else { result.ResultCode = state1.OutputMsgs.Count > 0 ? state1.OutputMsgs.First().Result : APIResultCodes.BlockFailedToBeAuthorized; } return(result); }
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); }