Skip to content

Commit

Permalink
Oracle: Optimize the filter payment (#1610)
Browse files Browse the repository at this point in the history
* Optimize the payment of the filter

* Add comment

* Remove foreach

* Remove foreach in wallet
  • Loading branch information
shargon committed Apr 25, 2020
1 parent 04d4578 commit d9c032b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
20 changes: 0 additions & 20 deletions src/neo/Oracle/OracleResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Neo.Oracle
public class OracleResponse : ISerializable
{
private UInt160 _hash;
private bool _alreadyPayed;

/// <summary>
/// Request hash
Expand Down Expand Up @@ -49,25 +48,6 @@ public UInt160 Hash

public int Size => UInt160.Length + sizeof(byte) + Result.GetVarSize() + sizeof(long);

/// <summary>
/// Filter cost that it could be consumed only once
/// </summary>
public long FilterCostOnce()
{
if (_alreadyPayed) return 0;

_alreadyPayed = true;
return FilterCost;
}

/// <summary>
/// Reset the _alreadyPayed flag
/// </summary>
public void ResetFilterCostOnce()
{
_alreadyPayed = false;
}

/// <summary>
/// Create error result
/// </summary>
Expand Down
17 changes: 10 additions & 7 deletions src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,24 @@ public bool ContainsResponse(StoreView snapshot, UInt256 txHash)
/// <summary>
/// Consume Oracle Response
/// </summary>
/// <param name="snapshot">Snapshot</param>
/// <param name="engine">Engine</param>
/// <param name="txHash">Transaction Hash</param>
public OracleExecutionCache ConsumeOracleResponse(StoreView snapshot, UInt256 txHash)
public OracleExecutionCache ConsumeOracleResponse(ApplicationEngine engine, UInt256 txHash)
{
StorageKey key = CreateStorageKey(Prefix_OracleResponse, txHash.ToArray());
StorageItem storage = snapshot.Storages.TryGet(key);
StorageItem storage = engine.Snapshot.Storages.TryGet(key);
if (storage == null) return null;

OracleExecutionCache ret = storage.Value.AsSerializable<OracleExecutionCache>();

// It should be cached by the ApplicationEngine so we can save space removing it

snapshot.Storages.Delete(key);
engine.Snapshot.Storages.Delete(key);

// Pay for the filter

if (!engine.AddGas(ret.FilterCost)) throw new ArgumentException("OutOfGas");

return ret;
}

Expand Down Expand Up @@ -348,7 +353,7 @@ private StackItem Get(ApplicationEngine engine, Array args)
{
// Read Oracle Response

engine.OracleCache = Oracle.ConsumeOracleResponse(engine.Snapshot, tx.Hash);
engine.OracleCache = Oracle.ConsumeOracleResponse(engine, tx.Hash);

// If it doesn't exist, fault

Expand Down Expand Up @@ -415,8 +420,6 @@ private StackItem Get(ApplicationEngine engine, Array args)
{
// Add the gas filter cost

if (!engine.AddGas(response.FilterCostOnce())) return false;

return response.Result ?? StackItem.Null;
}

Expand Down
14 changes: 3 additions & 11 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,6 @@ private Transaction MakeTransaction(StoreView snapshot, byte[] script, Transacti
Cosigners = cosigners
};

if (oracleCache != null)
{
// We need to reset if we already consumed the fiter price
// otherwise we can have a wrong gas computation

foreach (var entry in oracleCache.Responses)
{
entry.ResetFilterCostOnce();
}
}

// will try to execute 'transfer' script to check if it works
using (ApplicationEngine engine = ApplicationEngine.Run(script, snapshot.Clone(), tx, testMode: true, oracle: oracleCache))
{
Expand All @@ -357,6 +346,9 @@ private Transaction MakeTransaction(StoreView snapshot, byte[] script, Transacti

if (oracleRequests?.Count > 0)
{
// Increase filter cost

tx.SystemFee += oracleCache.FilterCost;
tx.Version = TransactionVersion.OracleRequest;

if (oracle == OracleWalletBehaviour.OracleWithAssert)
Expand Down

0 comments on commit d9c032b

Please sign in to comment.