Beispiel #1
0
        public async Task TestGetServiceBlockAsync()
        {
            var client = LyraRestClient.Create(networkId, "Windows", "UnitTest", "1.0");

            var aggClient = new LyraAggregatedClient(networkId, false, null);
            await aggClient.InitAsync();

            var svcBlock1 = await client.GetLastServiceBlockAsync();

            var svcBlock2 = await aggClient.GetLastServiceBlockAsync();

            Assert.AreEqual(svcBlock1, svcBlock2);
        }
        internal async Task ProcessMessageAsync(ViewChangeMessage vcm)
        {
            if (ViewId != 0 && vcm.ViewID != ViewId)
            {
                _log.LogInformation($"VC Msgs: view ID {vcm.ViewID} not valid with {ViewId}");
                if (vcm.ViewID > ViewId)
                {
                    // maybe outdated?
                    if (_vcTaskMutex.Wait(1))
                    {
                        _ = Task.Run(async() =>
                        {
                            try
                            {
                                var client = new LyraAggregatedClient(Settings.Default.LyraNode.Lyra.NetworkId, false, _sys.PosWallet.AccountId);
                                await client.InitAsync();
                                var lsb = await client.GetLastServiceBlockAsync();
                                if (lsb.ResultCode == Blocks.APIResultCodes.Success)
                                {
                                    var sb = lsb.GetBlock() as ServiceBlock;
                                    if (sb.Height > ViewId)
                                    {
                                        // outdated
                                        _context.LocalConsolidationFailed(sb.Hash);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                _log.LogError($"In detecting latest view: {ex}");
                            }
                            finally
                            {
                                // throttle
                                await Task.Delay(3000);
                                _vcTaskMutex.Release();
                            }
                        }).ConfigureAwait(false);
                    }
                }
                return;
            }

            if (vcm.TimeStamp < DateTime.UtcNow.AddSeconds(-1 * LyraGlobal.VIEWCHANGE_TIMEOUT))
            {
                return;
            }

            RemoveOutDatedMsgs();

            if (vcm is ViewChangeRequestMessage req)
            {
                await CheckRequestAsync(req);

                return;
            }

            //_log.LogInformation($"ViewChangeHandler ProcessMessage From {vcm.From.Shorten()} with ViewID {vcm.ViewID} My ViewID {_viewId} ");

            if (_context.Board.AllVoters.Contains(vcm.From))      // not the next one
            {
                switch (vcm)
                {
                case ViewChangeReplyMessage reply:
                    await CheckReplyAsync(reply);

                    break;

                case ViewChangeCommitMessage commit:
                    await CheckCommitAsync(commit);

                    break;

                default:
                    _log.LogWarning("Should not happen.");
                    break;
                }
            }
        }