Skip to content

Commit 2b8189d

Browse files
authored
Merge pull request planetarium#3912 from planetarium/exp/dpos-sloth
Preparation for PoS
2 parents 68d8f42 + d65ff91 commit 2b8189d

33 files changed

+510
-482
lines changed

CHANGES.md

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ To be released.
1010

1111
### Backward-incompatible API changes
1212

13+
- (Libplanet.Action) Added `MaxGasPrice` property to `IActionContext`
14+
interface and its implementations. [[#3912]]
15+
- (Libplanet.Explorer) Added `self` field to `NoteStateType`. [[#3912]]
16+
- (Libplanet.Action) Removed `IFeeCollector` interface
17+
and its implementations. [[#3912]]
18+
- (Libplanet.Action) Removed following methods from the
19+
`IActionContext` interface. [[#3912]]
20+
- Removed `IActionContext.UseGas(long)`.
21+
- Removed `IActionContext.GasUsed()`.
22+
- Removed `IActionContext.GasLimit()`.
23+
- (Libplanet.Action) Added `GasTracer` static class. [[#3912]]
24+
- (Libplanet.Action) Added `LastCommit` property to `IActionContext`
25+
interface and its implementations. [[#3912]]
26+
27+
1328
### Backward-incompatible network protocol changes
1429

1530
### Backward-incompatible storage format changes
@@ -24,6 +39,8 @@ To be released.
2439

2540
### CLI tools
2641

42+
[#3912]: https://github.com/planetarium/libplanet/pull/3912
43+
2744

2845
Version 5.3.1
2946
-------------

src/Libplanet.Action/ActionContext.cs

+14-43
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Collections.Immutable;
4-
using System.Threading;
3+
using System.Diagnostics.Contracts;
54
using Libplanet.Action.State;
65
using Libplanet.Crypto;
6+
using Libplanet.Types.Assets;
7+
using Libplanet.Types.Blocks;
78
using Libplanet.Types.Evidence;
89
using Libplanet.Types.Tx;
910

1011
namespace Libplanet.Action
1112
{
1213
internal class ActionContext : IActionContext
1314
{
14-
public static readonly AsyncLocal<GasMeter> GetGasMeter = new AsyncLocal<GasMeter>();
15-
16-
private readonly long _gasLimit;
17-
1815
private readonly IReadOnlyList<ITransaction> _txs;
1916

2017
public ActionContext(
@@ -23,10 +20,11 @@ public ActionContext(
2320
Address miner,
2421
long blockIndex,
2522
int blockProtocolVersion,
23+
BlockCommit? lastCommit,
2624
IWorld previousState,
2725
int randomSeed,
2826
bool isPolicyAction,
29-
long gasLimit,
27+
FungibleAssetValue? maxGasPrice,
3028
IReadOnlyList<ITransaction>? txs = null,
3129
IReadOnlyList<EvidenceBase>? evidence = null)
3230
{
@@ -35,14 +33,13 @@ public ActionContext(
3533
Miner = miner;
3634
BlockIndex = blockIndex;
3735
BlockProtocolVersion = blockProtocolVersion;
36+
LastCommit = lastCommit;
3837
PreviousState = previousState;
3938
RandomSeed = randomSeed;
4039
IsPolicyAction = isPolicyAction;
41-
_gasLimit = gasLimit;
40+
MaxGasPrice = maxGasPrice;
4241
_txs = txs ?? ImmutableList<Transaction>.Empty;
4342
Evidence = evidence ?? ImmutableList<EvidenceBase>.Empty;
44-
45-
GetGasMeter.Value = new GasMeter(_gasLimit);
4643
}
4744

4845
/// <inheritdoc cref="IActionContext.Signer"/>
@@ -60,6 +57,9 @@ public ActionContext(
6057
/// <inheritdoc cref="IActionContext.BlockProtocolVersion"/>
6158
public int BlockProtocolVersion { get; }
6259

60+
/// <inheritdoc cref="IActionContext.LastCommit"/>
61+
public BlockCommit? LastCommit { get; }
62+
6363
/// <inheritdoc cref="IActionContext.PreviousState"/>
6464
public IWorld PreviousState { get; }
6565

@@ -69,6 +69,10 @@ public ActionContext(
6969
/// <inheritdoc cref="IActionContext.IsPolicyAction"/>
7070
public bool IsPolicyAction { get; }
7171

72+
/// <inheritdoc cref="IActionContext.MaxGasPrice"/>
73+
[Pure]
74+
public FungibleAssetValue? MaxGasPrice { get; }
75+
7276
/// <inheritdoc cref="IActionContext.Txs"/>
7377
public IReadOnlyList<ITransaction> Txs => IsPolicyAction
7478
? _txs
@@ -77,40 +81,7 @@ public ActionContext(
7781
/// <inheritdoc cref="IActionContext.Evidence"/>
7882
public IReadOnlyList<EvidenceBase> Evidence { get; }
7983

80-
/// <inheritdoc cref="IActionContext.UseGas(long)"/>
81-
public void UseGas(long gas) => GetGasMeter.Value?.UseGas(gas);
82-
8384
/// <inheritdoc cref="IActionContext.GetRandom"/>
8485
public IRandom GetRandom() => new Random(RandomSeed);
85-
86-
/// <summary>
87-
/// Returns the elapsed gas of the current action.
88-
/// </summary>
89-
/// <returns>
90-
/// The elapsed gas of the current action.
91-
/// </returns>
92-
/// <exception cref="InvalidOperationException">
93-
/// Thrown when <see cref="GetGasMeter"/> is not initialized.
94-
/// </exception>
95-
public long GasUsed()
96-
{
97-
if (GetGasMeter.Value is { } gasMeter)
98-
{
99-
return gasMeter.GasUsed;
100-
}
101-
102-
throw new InvalidOperationException($"{nameof(GetGasMeter)} is not initialized.");
103-
}
104-
105-
/// <summary>
106-
/// Returns the gas limit of the current action.
107-
/// </summary>
108-
/// <returns>
109-
/// The gas limit of the current action.
110-
/// </returns>
111-
public long GasLimit()
112-
{
113-
return _gasLimit;
114-
}
11586
}
11687
}

src/Libplanet.Action/ActionEvaluator.cs

+16-21
Original file line numberDiff line numberDiff line change
@@ -207,34 +207,32 @@ internal static IEnumerable<ActionEvaluation> EvaluateActions(
207207
{
208208
IActionContext CreateActionContext(
209209
IWorld prevState,
210-
int randomSeed,
211-
long actionGasLimit)
210+
int randomSeed)
212211
{
213212
return new ActionContext(
214213
signer: tx?.Signer ?? block.Miner,
215214
txid: tx?.Id ?? null,
216215
miner: block.Miner,
217216
blockIndex: block.Index,
218217
blockProtocolVersion: block.ProtocolVersion,
218+
lastCommit: block.LastCommit,
219219
txs: block.Transactions,
220220
previousState: prevState,
221221
isPolicyAction: isPolicyAction,
222222
randomSeed: randomSeed,
223-
gasLimit: actionGasLimit,
223+
maxGasPrice: tx?.MaxGasPrice,
224224
evidence: block.Evidence);
225225
}
226226

227-
long gasLimit = tx?.GasLimit ?? long.MaxValue;
228-
229227
byte[] preEvaluationHashBytes = block.PreEvaluationHash.ToByteArray();
230228
byte[] signature = tx?.Signature ?? Array.Empty<byte>();
231229
int seed = GenerateRandomSeed(preEvaluationHashBytes, signature, 0);
232230

233231
IWorld state = previousState;
234232
foreach (IAction action in actions)
235233
{
236-
IActionContext context = CreateActionContext(state, seed, gasLimit);
237-
(ActionEvaluation Evaluation, long NextGasLimit) result = EvaluateAction(
234+
IActionContext context = CreateActionContext(state, seed);
235+
ActionEvaluation evaluation = EvaluateAction(
238236
block,
239237
tx,
240238
context,
@@ -243,10 +241,9 @@ IActionContext CreateActionContext(
243241
isPolicyAction,
244242
logger);
245243

246-
yield return result.Evaluation;
244+
yield return evaluation;
247245

248-
state = result.Evaluation.OutputState;
249-
gasLimit = result.NextGasLimit;
246+
state = evaluation.OutputState;
250247

251248
unchecked
252249
{
@@ -255,7 +252,7 @@ IActionContext CreateActionContext(
255252
}
256253
}
257254

258-
internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
255+
internal static ActionEvaluation EvaluateAction(
259256
IPreEvaluationBlock block,
260257
ITransaction? tx,
261258
IActionContext context,
@@ -274,7 +271,6 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
274271
IActionContext inputContext = context;
275272
IWorld state = inputContext.PreviousState;
276273
Exception? exc = null;
277-
IFeeCollector feeCollector = new FeeCollector(context, tx?.MaxGasPrice);
278274

279275
IActionContext CreateActionContext(IWorld newPrevState)
280276
{
@@ -284,10 +280,11 @@ IActionContext CreateActionContext(IWorld newPrevState)
284280
miner: inputContext.Miner,
285281
blockIndex: inputContext.BlockIndex,
286282
blockProtocolVersion: inputContext.BlockProtocolVersion,
283+
lastCommit: inputContext.LastCommit,
287284
previousState: newPrevState,
288285
randomSeed: inputContext.RandomSeed,
289286
isPolicyAction: isPolicyAction,
290-
gasLimit: inputContext.GasLimit(),
287+
maxGasPrice: tx?.MaxGasPrice,
291288
txs: inputContext.Txs,
292289
evidence: inputContext.Evidence);
293290
}
@@ -297,9 +294,7 @@ IActionContext CreateActionContext(IWorld newPrevState)
297294
Stopwatch stopwatch = new Stopwatch();
298295
stopwatch.Start();
299296
AccountMetrics.Initialize();
300-
state = feeCollector.Mortgage(state);
301297
context = CreateActionContext(state);
302-
feeCollector = feeCollector.Next(context);
303298
state = action.Execute(context);
304299
logger?
305300
.ForContext("Tag", "Metric")
@@ -361,8 +356,6 @@ IActionContext CreateActionContext(IWorld newPrevState)
361356
e);
362357
}
363358

364-
state = feeCollector.Refund(state);
365-
state = feeCollector.Reward(state);
366359
state = stateStore.CommitWorld(state);
367360

368361
if (!state.Trie.Recorded)
@@ -371,13 +364,11 @@ IActionContext CreateActionContext(IWorld newPrevState)
371364
$"Failed to record {nameof(IAccount)}'s {nameof(ITrie)}.");
372365
}
373366

374-
return (
375-
new ActionEvaluation(
367+
return new ActionEvaluation(
376368
action: action,
377369
inputContext: inputContext,
378370
outputState: state,
379-
exception: exc),
380-
context.GasLimit() - context.GasUsed());
371+
exception: exc);
381372
}
382373

383374
/// <summary>
@@ -479,6 +470,8 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
479470
ITransaction tx,
480471
IWorld previousState)
481472
{
473+
GasTracer.Initialize(tx.GasLimit ?? long.MaxValue);
474+
GasTracer.StartTrace();
482475
var evaluations = ImmutableList<ActionEvaluation>.Empty;
483476
if (_policyActionsRegistry.BeginTxActions.Length > 0)
484477
{
@@ -507,6 +500,8 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
507500
EvaluatePolicyEndTxActions(block, tx, previousState));
508501
}
509502

503+
GasTracer.EndTrace();
504+
510505
return evaluations;
511506
}
512507

0 commit comments

Comments
 (0)