Skip to content

Commit

Permalink
Throw exception when tip not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Aug 28, 2024
1 parent ec0ebd2 commit 4e625a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
13 changes: 11 additions & 2 deletions MarketService/ProductWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions MarketService/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MarketService;
public class Receiver : IActionEvaluationHubReceiver
{
public Block Tip;
public Block PreviousTip;
private readonly ILogger<Receiver> _logger;
private readonly Codec _codec = new Codec();

Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions MarketService/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class RpcClient

public bool Ready => _ready;
public Block Tip => _receiver.Tip;
public Block PreviousTip => _receiver.PreviousTip;


public RpcClient(IOptions<RpcConfigOptions> options, ILogger<RpcClient> logger, Receiver receiver,
Expand Down
14 changes: 12 additions & 2 deletions MarketService/ShopWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4e625a1

Please sign in to comment.