Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plugins/RpcClient/WalletAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public async Task<RpcTransaction> WaitTransactionAsync(Transaction transaction,
{
DateTime deadline = DateTime.UtcNow.AddSeconds(timeout);
RpcTransaction rpcTx = null;
var pollDelay = Math.Max(100, (int)rpcClient.protocolSettings.MillisecondsPerBlock / 2);
while (rpcTx == null || rpcTx.Confirmations == null)
{
if (deadline < DateTime.UtcNow)
Expand All @@ -211,10 +212,15 @@ public async Task<RpcTransaction> WaitTransactionAsync(Transaction transaction,
rpcTx = await rpcClient.GetRawTransactionAsync(transaction.Hash.ToString()).ConfigureAwait(false);
if (rpcTx == null || rpcTx.Confirmations == null)
{
await Task.Delay((int)rpcClient.protocolSettings.MillisecondsPerBlock / 2);
await Task.Delay(pollDelay).ConfigureAwait(false);
}
}
catch (Exception) { }
catch (Exception)
{
// Unknown tx and transient RPC errors retry until timeout;
// sleep so a persistent failure cannot busy-loop the CPU.
await Task.Delay(pollDelay).ConfigureAwait(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use finally and do it only once?

}
}
return rpcTx;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Neo.Network.RPC.Tests/UT_WalletAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,30 @@ public async Task TestWaitTransaction()
Assert.AreEqual(VMState.HALT, tx.VMState);
Assert.AreEqual(UInt256.Zero, tx.BlockHash);
}

[TestMethod]
public async Task TestWaitTransaction_RetriesAfterRpcError()
{
Transaction transaction = TestUtils.GetTransaction();
var callCount = 0;
rpcClientMock.Setup(p => p.RpcSendAsync("getrawtransaction", It.Is<JToken[]>(j => j[0].AsString() == transaction.Hash.ToString())))
.Returns(() =>
{
callCount++;
if (callCount == 1)
return Task.FromException<JToken>(new RpcException(-100, "Unknown transaction"));
return Task.FromResult<JToken>(new RpcTransaction
{
Transaction = transaction,
VMState = VMState.HALT,
BlockHash = UInt256.Zero,
BlockTime = 100,
Confirmations = 1
}.ToJson(client.protocolSettings));
});

var tx = await walletAPI.WaitTransactionAsync(transaction, timeout: 20);
Assert.AreEqual(2, callCount);
Assert.AreEqual(VMState.HALT, tx.VMState);
}
}
Loading