Skip to content

Commit

Permalink
feat: add max gas price to action context
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 22, 2024
1 parent 5cbb2c6 commit 5f458fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics.Contracts;
using System.Threading;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

Expand All @@ -22,7 +24,8 @@ public ActionContext(
BlockCommit? lastCommit,
IWorld previousState,
int randomSeed,
long gasLimit)
long gasLimit,
FungibleAssetValue? maxGasPrice)
{
Signer = signer;
TxId = txid;
Expand All @@ -33,6 +36,7 @@ public ActionContext(
PreviousState = previousState;
RandomSeed = randomSeed;
_gasLimit = gasLimit;
MaxGasPrice = maxGasPrice;

GetGasMeter.Value = new GasMeter(_gasLimit);
}
Expand Down Expand Up @@ -64,6 +68,10 @@ public ActionContext(
/// <inheritdoc cref="IActionContext.BlockAction"/>
public bool BlockAction => TxId is null;

/// <inheritdoc cref="IActionContext.MaxGasPrice"/>
[Pure]
public FungibleAssetValue? MaxGasPrice { get; }

/// <inheritdoc cref="IActionContext.UseGas(long)"/>
public void UseGas(long gas) => GetGasMeter.Value?.UseGas(gas);

Expand Down
6 changes: 4 additions & 2 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ IActionContext CreateActionContext(
lastCommit: blockHeader.LastCommit,
previousState: prevState,
randomSeed: randomSeed,
gasLimit: actionGasLimit);
gasLimit: actionGasLimit,
maxGasPrice: tx?.MaxGasPrice);
}

long gasLimit = tx?.GasLimit ?? long.MaxValue;
Expand Down Expand Up @@ -285,7 +286,8 @@ IActionContext CreateActionContext(IWorld newPrevState)
lastCommit: inputContext.LastCommit,
previousState: newPrevState,
randomSeed: inputContext.RandomSeed,
gasLimit: inputContext.GasLimit());
gasLimit: inputContext.GasLimit(),
tx?.MaxGasPrice);
}

try
Expand Down
8 changes: 8 additions & 0 deletions Libplanet.Action/IActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.Contracts;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

Expand Down Expand Up @@ -89,6 +90,13 @@ public interface IActionContext
[Pure]
bool BlockAction { get; }

/// <summary>
/// Max gas price set by the transaction.
/// <see langword="null"/> if the action does not belongs to a transaction.
/// </summary>
[Pure]
FungibleAssetValue? MaxGasPrice { get; }

/// <summary>
/// Consumes the specified amount of gas.
/// </summary>
Expand Down

0 comments on commit 5f458fb

Please sign in to comment.