Skip to content

Commit

Permalink
GetManyUnclaimedGas
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Sep 11, 2023
1 parent 1253942 commit 8d3279b
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions Fairy.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,6 @@ protected virtual JToken ListContracts(JArray _params)
return json;
}

/// <summary>
/// Wait until the transaction is included in blocks
/// </summary>
/// <param name="_params">UInt256String; bool(verbose); waitBlockCount</param>
/// <returns></returns>
/// <exception cref="RpcException"></exception>
[RpcMethod]
protected virtual JToken AwaitConfirmedTransaction(JArray _params)
{
UInt256 hash = UInt256.Parse(_params[0]!.AsString());
bool verbose = _params.Count >= 2 && _params[1]!.AsBoolean();
uint waitBlockCount = _params.Count >= 2 ? uint.Parse(_params[2]!.AsString()) : 2;
JToken? json = GetConfirmedTransaction(hash, verbose);
if (json != null)
return json;
SemaphoreSlim signal = new SemaphoreSlim(0, 1);
uint count = 0;
CommittedHandler getConfirmedTransactionAfterCommitted = delegate(NeoSystem @system, Block @block){ json = GetConfirmedTransaction(hash, verbose); count += 1; signal.Release(); };
Blockchain.Committed += getConfirmedTransactionAfterCommitted;
while (count < waitBlockCount)
{
signal.Wait();
if (json != null)
{
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
return json;
}
}
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
throw new RpcException(-100, $"Transaction not found in {waitBlockCount} blocks");
}

[RpcMethod]
protected virtual JObject PutStorageWithSession(JArray _params)
{
Expand Down Expand Up @@ -283,6 +251,53 @@ protected JObject SetTokenBalance(string session, UInt160 contract, UInt160 acco
}
}

[RpcMethod]
protected virtual JToken GetManyUnclaimedGas(JArray _params)
{
string? session = _params[0]?.AsString();
DataCache snapshot = session == null ? system.StoreView : sessionStringToFairySession[session].engine.Snapshot;
uint nextBlockIndex = NativeContract.Ledger.CurrentIndex(snapshot) + 1;
JObject json = new JObject();
for (int i = 1; i < _params.Count; ++i)
{
UInt160 account = UInt160.Parse(_params[i]!.AsString());
json[account.ToString()] = NativeContract.NEO.UnclaimedGas(snapshot, account, nextBlockIndex).ToString();
}
return json;
}

/// <summary>
/// Wait until the transaction is included in blocks
/// </summary>
/// <param name="_params">UInt256String; bool(verbose); waitBlockCount</param>
/// <returns></returns>
/// <exception cref="RpcException"></exception>
[RpcMethod]
protected virtual JToken AwaitConfirmedTransaction(JArray _params)
{
UInt256 hash = UInt256.Parse(_params[0]!.AsString());
bool verbose = _params.Count >= 2 && _params[1]!.AsBoolean();
uint waitBlockCount = _params.Count >= 2 ? uint.Parse(_params[2]!.AsString()) : 2;
JToken? json = GetConfirmedTransaction(hash, verbose);
if (json != null)
return json;
SemaphoreSlim signal = new SemaphoreSlim(0, 1);
uint count = 0;
CommittedHandler getConfirmedTransactionAfterCommitted = delegate (NeoSystem @system, Block @block) { json = GetConfirmedTransaction(hash, verbose); count += 1; signal.Release(); };
Blockchain.Committed += getConfirmedTransactionAfterCommitted;
while (count < waitBlockCount)
{
signal.Wait();
if (json != null)
{
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
return json;
}
}
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
throw new RpcException(-100, $"Transaction not found in {waitBlockCount} blocks");
}

protected JToken? GetConfirmedTransaction(UInt256 hash, bool verbose)
{
// Do not consider anything in MemPool, because they have not been confirmed
Expand Down

0 comments on commit 8d3279b

Please sign in to comment.