diff --git a/MarketService/ProductWorker.cs b/MarketService/ProductWorker.cs index 02e4e6a..59a6d36 100644 --- a/MarketService/ProductWorker.cs +++ b/MarketService/ProductWorker.cs @@ -24,7 +24,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) var stopWatch = new Stopwatch(); _logger.LogInformation("[ProductWorker]Start sync product"); - while (_rpcClient.Tip is null) await Task.Delay(100, stoppingToken); + var retry = 0; + while (_rpcClient.Tip?.Index == _rpcClient.PreviousTip?.Index) + { + await Task.Delay((5 - retry) * 1000, stoppingToken); + retry++; + if (retry >= 3) + { + throw new InvalidOperationException(); + } + } stopWatch.Start(); @@ -44,8 +53,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) stopWatch.Stop(); var ts = stopWatch.Elapsed; - await Task.Delay(1000, stoppingToken); _logger.LogInformation("[ProductWorker]Complete sync product on {BlockIndex}. {TotalElapsed}", _rpcClient.Tip.Index, ts); + await Task.Delay(8000, stoppingToken); } } } diff --git a/MarketService/Receiver.cs b/MarketService/Receiver.cs index 682fdce..2514d46 100644 --- a/MarketService/Receiver.cs +++ b/MarketService/Receiver.cs @@ -8,6 +8,7 @@ namespace MarketService; public class Receiver : IActionEvaluationHubReceiver { public Block Tip; + public Block PreviousTip; private readonly ILogger _logger; private readonly Codec _codec = new Codec(); @@ -28,6 +29,7 @@ public void OnRenderBlock(byte[] oldTip, byte[] newTip) { var dict = (Dictionary)_codec.Decode(newTip); var newTipBlock = BlockMarshaler.UnmarshalBlock(dict); + PreviousTip = Tip; Tip = newTipBlock; } diff --git a/MarketService/RpcClient.cs b/MarketService/RpcClient.cs index 82f9fbd..7a3e71c 100644 --- a/MarketService/RpcClient.cs +++ b/MarketService/RpcClient.cs @@ -59,6 +59,7 @@ public class RpcClient public bool Ready => _ready; public Block Tip => _receiver.Tip; + public Block PreviousTip => _receiver.PreviousTip; public RpcClient(IOptions options, ILogger logger, Receiver receiver, diff --git a/MarketService/ShopWorker.cs b/MarketService/ShopWorker.cs index a747841..6ee2fd9 100644 --- a/MarketService/ShopWorker.cs +++ b/MarketService/ShopWorker.cs @@ -27,9 +27,19 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) if (stoppingToken.IsCancellationRequested) stoppingToken.ThrowIfCancellationRequested(); var stopWatch = new Stopwatch(); - - while (_rpcClient.Tip is null) await Task.Delay(100, stoppingToken); _logger.LogInformation("Start sync shop"); + + var retry = 0; + while (_rpcClient.Tip?.Index == _rpcClient.PreviousTip?.Index) + { + await Task.Delay((5 - retry) * 1000, stoppingToken); + retry++; + if (retry >= 3) + { + throw new InvalidOperationException(); + } + } + stopWatch.Start(); var hashBytes = await _rpcClient.GetBlockStateRootHashBytes();