Skip to content

Commit 9897b90

Browse files
committed
feat: Add CancelTrace method to the GasTracer
1 parent 625e617 commit 9897b90

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/Libplanet.Action/ActionEvaluator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,14 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
471471
IWorld previousState)
472472
{
473473
GasTracer.Initialize(tx.GasLimit ?? long.MaxValue);
474-
GasTracer.StartTrace();
475474
var evaluations = ImmutableList<ActionEvaluation>.Empty;
476475
if (_policyActionsRegistry.BeginTxActions.Length > 0)
477476
{
477+
GasTracer.IsTxAction = true;
478478
evaluations = evaluations.AddRange(
479479
EvaluatePolicyBeginTxActions(block, tx, previousState));
480480
previousState = evaluations.Last().OutputState;
481+
GasTracer.IsTxAction = false;
481482
}
482483

483484
ImmutableList<IAction> actions =
@@ -500,7 +501,7 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
500501
EvaluatePolicyEndTxActions(block, tx, previousState));
501502
}
502503

503-
GasTracer.EndTrace();
504+
GasTracer.Release();
504505

505506
return evaluations;
506507
}

src/Libplanet.Action/GasMeter.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ namespace Libplanet.Action
22
{
33
internal class GasMeter : IGasMeter
44
{
5-
public GasMeter(long gasLimit, long gasUsed = 0)
5+
public GasMeter(long gasLimit)
66
{
7-
SetGasLimit(gasLimit);
8-
GasUsed = gasUsed;
7+
if (gasLimit < 0)
8+
{
9+
throw new GasLimitNegativeException();
10+
}
11+
12+
GasLimit = gasLimit;
913
}
1014

1115
public long GasAvailable => GasLimit - GasUsed;
@@ -39,15 +43,5 @@ public void UseGas(long gas)
3943

4044
GasUsed = newGasUsed;
4145
}
42-
43-
private void SetGasLimit(long gasLimit)
44-
{
45-
if (gasLimit < 0)
46-
{
47-
throw new GasLimitNegativeException();
48-
}
49-
50-
GasLimit = gasLimit;
51-
}
5246
}
5347
}

src/Libplanet.Action/GasTracer.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public static class GasTracer
1616

1717
private static readonly AsyncLocal<bool> IsTrace = new AsyncLocal<bool>();
1818

19+
private static readonly AsyncLocal<bool> IsTraceCancelled = new AsyncLocal<bool>();
20+
1921
/// <summary>
2022
/// The amount of gas used so far.
2123
/// </summary>
@@ -26,6 +28,8 @@ public static class GasTracer
2628
/// </summary>
2729
public static long GasAvailable => GasMeterValue.GasAvailable;
2830

31+
internal static bool IsTxAction { get; set; }
32+
2933
private static GasMeter GasMeterValue
3034
=> GasMeter.Value ?? throw new InvalidOperationException(
3135
"GasTracer is not initialized.");
@@ -41,23 +45,39 @@ public static void UseGas(long gas)
4145
if (IsTrace.Value)
4246
{
4347
GasMeterValue.UseGas(gas);
48+
if (IsTraceCancelled.Value)
49+
{
50+
throw new InvalidOperationException("GasTracing was canceled.");
51+
}
4452
}
4553
}
4654

47-
internal static void Initialize(long gasLimit)
55+
public static void CancelTrace()
4856
{
49-
GasMeter.Value = new GasMeter(gasLimit);
50-
IsTrace.Value = false;
57+
if (!IsTxAction)
58+
{
59+
throw new InvalidOperationException("CancelTrace can only be called in TxAction.");
60+
}
61+
62+
if (IsTraceCancelled.Value)
63+
{
64+
throw new InvalidOperationException("GasTracing is already canceled.");
65+
}
66+
67+
IsTraceCancelled.Value = true;
5168
}
5269

53-
internal static void StartTrace()
70+
internal static void Initialize(long gasLimit)
5471
{
72+
GasMeter.Value = new GasMeter(gasLimit);
5573
IsTrace.Value = true;
74+
IsTraceCancelled.Value = false;
5675
}
5776

58-
internal static void EndTrace()
77+
internal static void Release()
5978
{
6079
IsTrace.Value = false;
80+
IsTraceCancelled.Value = false;
6181
}
6282
}
6383
}

0 commit comments

Comments
 (0)