private void BroadCastBillBoard()
        {
            var msg = new ChatMsg(NodeService.Instance.PosWallet.AccountId, JsonConvert.SerializeObject(_board));

            msg.MsgType = ChatMessageType.StakingChanges;
            Send2P2pNetwork(msg);
        }
 private void OnBillBoardBroadcast(ChatMsg msg)
 {
     if (!IsThisNodeSeed0)
     {
         _board = JsonConvert.DeserializeObject <BillBoard>(msg.Text);
         _log.LogInformation("BillBoard updated!");
     }
 }
 private void OnBlockConsolication(ChatMsg msg)
 {
     if (!IsThisNodeSeed0)
     {
         var block = JsonConvert.DeserializeObject <ConsolidationBlock>(msg.Text);
         BlockChain.Singleton.AddBlock(block);
         _log.LogInformation($"Receive and store ConsolidateBlock of UIndex: {block.UIndex}");
     }
 }
        private void OnNodeUp(ChatMsg chat)
        {
            var node = _board.Add(chat.From);

            if (IsThisNodeSeed0)
            {
                // broadcast billboard
                BroadCastBillBoard();
            }

            if (node.Balance < LyraGlobal.MinimalAuthorizerBalance)
            {
                _log.LogInformation("Node {0} has not enough balance: {1}.", node.AccountID, node.Balance);
            }
        }
 public virtual async Task SendMessage(ChatMsg msg)
 {
 }
        public ConsensusService(IActorRef localNode)
        {
            _localNode = localNode;
            _log       = new SimpleLogger("ConsensusService").Logger;

            _outOfOrderedMessages = new Dictionary <string, List <SourceSignedMessage> >();
            _activeConsensus      = new Dictionary <string, AuthState>();

            _authorizers = new AuthorizersFactory();
            while (BlockChain.Singleton == null)
            {
                Task.Delay(100).Wait();
            }

            _UIndexSeed = BlockChain.Singleton.GetBlockCount() + 1;
            Mode        = ConsensusWorkingMode.OutofSyncWaiting;

            Receive <Consolidate>((_) =>
            {
                _log.LogInformation("Doing Consolidate");
                OnNodeActive(NodeService.Instance.PosWallet.AccountId);     // update billboard

                if (Mode == ConsensusWorkingMode.Normal)
                {
                    BroadCastBillBoard();
                    GenerateConsolidateBlock();
                }
            });

            Receive <BillBoard>((bb) => {
                _board = bb;
            });

            Receive <AskForBillboard>((_) => Sender.Tell(_board));

            Receive <AuthorizingMsg>(async msg => {
                if (msg.Version != LyraGlobal.ProtocolVersion)
                {
                    Sender.Tell(null);
                }

                OnNodeActive(NodeService.Instance.PosWallet.AccountId);     // update billboard

                // first try auth locally
                var state           = CreateAuthringState(msg);
                var localAuthResult = LocalAuthorizingAsync(msg);
                state.AddAuthResult(localAuthResult);

                if (!localAuthResult.IsSuccess)
                {
                    state.Done.Set();
                    Sender.Tell(state);
                }
                else
                {
                    Send2P2pNetwork(msg);
                    Send2P2pNetwork(localAuthResult);

                    var sender = Context.Sender;

                    await Task.Run(() =>
                    {
                        _ = state.Done.WaitOne();
                    }).ConfigureAwait(false);

                    sender.Tell(state);
                }
            });

            Receive <SignedMessageRelay>(relayMsg =>
            {
                if (relayMsg.signedMessage.Version == LyraGlobal.ProtocolVersion)
                {
                    OnNextConsensusMessage(relayMsg.signedMessage);
                }
                else
                {
                    _log.LogWarning("Protocol Version Mismatch. Do nothing.");
                }
            });

            Receive <BlockChainSynced>(_ =>
            {
                Mode        = ConsensusWorkingMode.Normal;
                _UIndexSeed = BlockChain.Singleton.GetBlockCount() + 1;

                // declare to the network
                var msg = new ChatMsg
                {
                    From    = NodeService.Instance.PosWallet.AccountId,
                    MsgType = ChatMessageType.NodeUp,
                    Text    = "Staking with () Lyra"
                };

                Send2P2pNetwork(msg);
            });

            Task.Run(async() => {
                while (true)
                {
                    StateClean();
                    await Task.Delay(1000).ConfigureAwait(false);
                }
            });
        }