Skip to content

Commit a501943

Browse files
committed
refactor: Refactor GasTracer
1 parent 2b8189d commit a501943

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

src/Libplanet.Action/ActionContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public ActionContext(
2525
int randomSeed,
2626
bool isPolicyAction,
2727
FungibleAssetValue? maxGasPrice,
28+
long? gasLimit = null,
2829
IReadOnlyList<ITransaction>? txs = null,
2930
IReadOnlyList<EvidenceBase>? evidence = null)
3031
{
@@ -38,6 +39,7 @@ public ActionContext(
3839
RandomSeed = randomSeed;
3940
IsPolicyAction = isPolicyAction;
4041
MaxGasPrice = maxGasPrice;
42+
GasLimit = gasLimit;
4143
_txs = txs ?? ImmutableList<Transaction>.Empty;
4244
Evidence = evidence ?? ImmutableList<EvidenceBase>.Empty;
4345
}
@@ -73,6 +75,10 @@ public ActionContext(
7375
[Pure]
7476
public FungibleAssetValue? MaxGasPrice { get; }
7577

78+
/// <inheritdoc cref="IActionContext.GasLimit"/>
79+
[Pure]
80+
public long? GasLimit { get; }
81+
7682
/// <inheritdoc cref="IActionContext.Txs"/>
7783
public IReadOnlyList<ITransaction> Txs => IsPolicyAction
7884
? _txs

src/Libplanet.Action/ActionEvaluator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ IActionContext CreateActionContext(
221221
isPolicyAction: isPolicyAction,
222222
randomSeed: randomSeed,
223223
maxGasPrice: tx?.MaxGasPrice,
224+
gasLimit: tx?.GasLimit,
224225
evidence: block.Evidence);
225226
}
226227

@@ -285,6 +286,7 @@ IActionContext CreateActionContext(IWorld newPrevState)
285286
randomSeed: inputContext.RandomSeed,
286287
isPolicyAction: isPolicyAction,
287288
maxGasPrice: tx?.MaxGasPrice,
289+
gasLimit: tx?.GasLimit,
288290
txs: inputContext.Txs,
289291
evidence: inputContext.Evidence);
290292
}
@@ -470,8 +472,6 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
470472
ITransaction tx,
471473
IWorld previousState)
472474
{
473-
GasTracer.Initialize(tx.GasLimit ?? long.MaxValue);
474-
GasTracer.StartTrace();
475475
var evaluations = ImmutableList<ActionEvaluation>.Empty;
476476
if (_policyActionsRegistry.BeginTxActions.Length > 0)
477477
{
@@ -500,8 +500,6 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
500500
EvaluatePolicyEndTxActions(block, tx, previousState));
501501
}
502502

503-
GasTracer.EndTrace();
504-
505503
return evaluations;
506504
}
507505

src/Libplanet.Action/GasMeter.cs

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

11-
public long GasAvailable => GasLimit - GasUsed;
16+
public long GasAvailable { get; private set; }
1217

1318
public long GasLimit { get; private set; }
1419

@@ -37,6 +42,12 @@ public void UseGas(long gas)
3742
throw new GasLimitExceededException(GasLimit, newGasUsed);
3843
}
3944

45+
if (newGasUsed > GasAvailable)
46+
{
47+
GasUsed = GasAvailable;
48+
throw new GasLimitExceededException(GasLimit, newGasUsed);
49+
}
50+
4051
GasUsed = newGasUsed;
4152
}
4253

src/Libplanet.Action/GasTracer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,25 @@ public static void UseGas(long gas)
4444
}
4545
}
4646

47-
internal static void Initialize(long gasLimit)
48-
{
49-
GasMeter.Value = new GasMeter(gasLimit);
50-
IsTrace.Value = false;
51-
}
52-
53-
internal static void StartTrace()
47+
public static void Initialize(long gasLimit, long availableGasLimit)
5448
{
49+
GasMeter.Value = new GasMeter(gasLimit, availableGasLimit);
5550
IsTrace.Value = true;
5651
}
5752

58-
internal static void EndTrace()
53+
public static void Release()
5954
{
6055
IsTrace.Value = false;
6156
}
57+
58+
// internal static void StartTrace()
59+
// {
60+
// IsTrace.Value = true;
61+
// }
62+
63+
// internal static void EndTrace()
64+
// {
65+
// IsTrace.Value = false;
66+
// }
6267
}
6368
}

src/Libplanet.Action/IActionContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public interface IActionContext
9999
[Pure]
100100
FungibleAssetValue? MaxGasPrice { get; }
101101

102+
[Pure]
103+
long? GasLimit { get; }
104+
102105
/// <summary>
103106
/// A list of <see cref="ITransaction"/>s that are included in a <see cref="Block"/> as
104107
/// the <see cref="IAction"/> to be evaluated. This information is provided only if

0 commit comments

Comments
 (0)