Skip to content

Commit

Permalink
refactor: Refactor GasTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 25, 2024
1 parent 2b8189d commit a501943
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public ActionContext(
int randomSeed,
bool isPolicyAction,
FungibleAssetValue? maxGasPrice,
long? gasLimit = null,
IReadOnlyList<ITransaction>? txs = null,
IReadOnlyList<EvidenceBase>? evidence = null)
{
Expand All @@ -38,6 +39,7 @@ public ActionContext(
RandomSeed = randomSeed;
IsPolicyAction = isPolicyAction;
MaxGasPrice = maxGasPrice;
GasLimit = gasLimit;
_txs = txs ?? ImmutableList<Transaction>.Empty;
Evidence = evidence ?? ImmutableList<EvidenceBase>.Empty;
}
Expand Down Expand Up @@ -73,6 +75,10 @@ public ActionContext(
[Pure]
public FungibleAssetValue? MaxGasPrice { get; }

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

/// <inheritdoc cref="IActionContext.Txs"/>
public IReadOnlyList<ITransaction> Txs => IsPolicyAction
? _txs
Expand Down
6 changes: 2 additions & 4 deletions src/Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ IActionContext CreateActionContext(
isPolicyAction: isPolicyAction,
randomSeed: randomSeed,
maxGasPrice: tx?.MaxGasPrice,
gasLimit: tx?.GasLimit,
evidence: block.Evidence);
}

Expand Down Expand Up @@ -285,6 +286,7 @@ IActionContext CreateActionContext(IWorld newPrevState)
randomSeed: inputContext.RandomSeed,
isPolicyAction: isPolicyAction,
maxGasPrice: tx?.MaxGasPrice,
gasLimit: tx?.GasLimit,
txs: inputContext.Txs,
evidence: inputContext.Evidence);
}
Expand Down Expand Up @@ -470,8 +472,6 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
ITransaction tx,
IWorld previousState)
{
GasTracer.Initialize(tx.GasLimit ?? long.MaxValue);
GasTracer.StartTrace();
var evaluations = ImmutableList<ActionEvaluation>.Empty;
if (_policyActionsRegistry.BeginTxActions.Length > 0)
{
Expand Down Expand Up @@ -500,8 +500,6 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
EvaluatePolicyEndTxActions(block, tx, previousState));
}

GasTracer.EndTrace();

return evaluations;
}

Expand Down
19 changes: 15 additions & 4 deletions src/Libplanet.Action/GasMeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ namespace Libplanet.Action
{
internal class GasMeter : IGasMeter
{
public GasMeter(long gasLimit, long gasUsed = 0)
public GasMeter(long gasLimit, long gasAvailable)
{
SetGasLimit(gasLimit);
GasUsed = gasUsed;
if (gasLimit < 0)
{
throw new GasLimitNegativeException();
}

GasLimit = gasLimit;
GasAvailable = gasAvailable;
}

public long GasAvailable => GasLimit - GasUsed;
public long GasAvailable { get; private set; }

public long GasLimit { get; private set; }

Expand Down Expand Up @@ -37,6 +42,12 @@ public void UseGas(long gas)
throw new GasLimitExceededException(GasLimit, newGasUsed);
}

if (newGasUsed > GasAvailable)
{
GasUsed = GasAvailable;
throw new GasLimitExceededException(GasLimit, newGasUsed);
}

GasUsed = newGasUsed;
}

Expand Down
21 changes: 13 additions & 8 deletions src/Libplanet.Action/GasTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,25 @@ public static void UseGas(long gas)
}
}

internal static void Initialize(long gasLimit)
{
GasMeter.Value = new GasMeter(gasLimit);
IsTrace.Value = false;
}

internal static void StartTrace()
public static void Initialize(long gasLimit, long availableGasLimit)
{
GasMeter.Value = new GasMeter(gasLimit, availableGasLimit);
IsTrace.Value = true;
}

internal static void EndTrace()
public static void Release()
{
IsTrace.Value = false;
}

// internal static void StartTrace()
// {
// IsTrace.Value = true;
// }

// internal static void EndTrace()
// {
// IsTrace.Value = false;
// }
}
}
3 changes: 3 additions & 0 deletions src/Libplanet.Action/IActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public interface IActionContext
[Pure]
FungibleAssetValue? MaxGasPrice { get; }

[Pure]
long? GasLimit { get; }

/// <summary>
/// A list of <see cref="ITransaction"/>s that are included in a <see cref="Block"/> as
/// the <see cref="IAction"/> to be evaluated. This information is provided only if
Expand Down

0 comments on commit a501943

Please sign in to comment.