Skip to content

Commit

Permalink
Merge pull request #3533 from greymistcube/revert/context-scoping
Browse files Browse the repository at this point in the history
⏪ Revert context scoping
  • Loading branch information
greymistcube authored Dec 4, 2023
2 parents e5fd96b + 4b0b11c commit 5ffe61d
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 258 deletions.
10 changes: 0 additions & 10 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,13 @@ be compatible with this version, specifically, those that ran with
- Changed `TxInvoice` to no longer allow having the null-ness of
`MaxGasPrice` and `GasLimit` to be different, i.e. either both should be
null or both should not be null at the same time. [[#3529]]
- (Libplanet.Action) Changed `IActionContext` to inherit `ITxContext`.
The following properties of `IActionContext` have moved to `ITxContext`:
`Signer`, `TxId`, `Miner`, `BlockIndex`, and `BlockProtocolVersion`.
[[#3530]]
- (Libplanet.Action) Changed `IAccount.Mint()`, `IAccount.Burn()`, and
`IAccount.Transfer()` to accept `ITxContext` instead of `IActionContext`
as its parameter. [[#3530]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes

### Added APIs

- (Libplanet.Action) Added `ITxContext` interface. [[#3530]]

### Behavioral changes

### Bug fixes
Expand All @@ -46,7 +37,6 @@ be compatible with this version, specifically, those that ran with

[#3523]: https://github.com/planetarium/libplanet/pull/3523
[#3529]: https://github.com/planetarium/libplanet/pull/3529
[#3530]: https://github.com/planetarium/libplanet/pull/3530
[Libplanet 2.0.0]: https://www.nuget.org/packages/Libplanet/2.0.0


Expand Down
58 changes: 27 additions & 31 deletions Libplanet.Action.Tests/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ public void RandomShouldBeDeterministic()
foreach (var (seed, expected) in testCases)
{
var context = new ActionContext(
new TxContext(
signer: _address,
txId: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: _address,
txid: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
randomSeed: seed,
gasLimit: 0
Expand All @@ -50,35 +49,33 @@ public void RandomShouldBeDeterministic()
public void GuidShouldBeDeterministic()
{
var context1 = new ActionContext(
new TxContext(
signer: _address,
txId: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: _address,
txid: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
randomSeed: 0,
gasLimit: 0);
gasLimit: 0
);

var context2 = new ActionContext(
new TxContext(
signer: _address,
txId: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: _address,
txid: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
randomSeed: 0,
gasLimit: 0
);

var context3 = new ActionContext(
new TxContext(
signer: _address,
txId: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: _address,
txid: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
randomSeed: 1,
gasLimit: 0
Expand Down Expand Up @@ -113,12 +110,11 @@ public void GuidVersionAndVariant()
for (var i = 0; i < 100; i++)
{
var context = new ActionContext(
new TxContext(
signer: _address,
txId: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: _address,
txid: _txid,
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
randomSeed: i,
gasLimit: 0
Expand Down
11 changes: 5 additions & 6 deletions Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public void Constructor()
var evaluation = new ActionEvaluation(
new DumbAction(address, "item"),
new ActionContext(
new TxContext(
address,
txid,
address,
1,
Block.CurrentProtocolVersion),
address,
txid,
address,
1,
Block.CurrentProtocolVersion,
new Account(MockAccountState.Empty),
123,
0),
Expand Down
22 changes: 10 additions & 12 deletions Libplanet.Action.Tests/Sys/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ public void Execute()
var prevState = new Account(MockAccountState.Empty);
BlockHash genesisHash = random.NextBlockHash();
var context = new ActionContext(
new TxContext(
signer: signer,
txId: random.NextTxId(),
miner: random.NextAddress(),
blockIndex: 0,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: signer,
txid: random.NextTxId(),
miner: random.NextAddress(),
blockIndex: 0,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: prevState,
randomSeed: 123,
gasLimit: 0);
Expand All @@ -73,12 +72,11 @@ public void ExecuteInNonGenesis()
var prevState = new Account(MockAccountState.Empty);
BlockHash genesisHash = random.NextBlockHash();
var context = new ActionContext(
new TxContext(
signer: signer,
txId: random.NextTxId(),
miner: random.NextAddress(),
blockIndex: 10,
blockProtocolVersion: Block.CurrentProtocolVersion),
signer: signer,
txid: random.NextTxId(),
miner: random.NextAddress(),
blockIndex: 10,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: prevState,
randomSeed: 123,
gasLimit: long.MaxValue);
Expand Down
28 changes: 16 additions & 12 deletions Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System;
using System.Diagnostics.Contracts;
using System.Threading;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Tx;

namespace Libplanet.Action
{
/// <summary>
/// Implements <see cref="IActionContext"/>.
/// </summary>
internal class ActionContext : IActionContext
{
public static readonly AsyncLocal<GasMeter> GetGasMeter = new AsyncLocal<GasMeter>();

private readonly ITxContext _txContext;

private readonly long _gasLimit;

public ActionContext(
ITxContext txContext,
Address signer,
TxId? txid,
Address miner,
long blockIndex,
int blockProtocolVersion,
IAccount previousState,
int randomSeed,
long gasLimit)
{
_txContext = txContext;
Signer = signer;
TxId = txid;
Miner = miner;
BlockIndex = blockIndex;
BlockProtocolVersion = blockProtocolVersion;
PreviousState = previousState;
RandomSeed = randomSeed;
_gasLimit = gasLimit;
Expand All @@ -32,19 +36,19 @@ public ActionContext(
}

/// <inheritdoc cref="IActionContext.Signer"/>
public Address Signer => _txContext.Signer;
public Address Signer { get; }

/// <inheritdoc cref="IActionContext.TxId"/>
public TxId? TxId => _txContext.TxId;
public TxId? TxId { get; }

/// <inheritdoc cref="IActionContext.Miner"/>
public Address Miner => _txContext.Miner;
public Address Miner { get; }

/// <inheritdoc cref="IActionContext.BlockIndex"/>
public long BlockIndex => _txContext.BlockIndex;
public long BlockIndex { get; }

/// <inheritdoc cref="IActionContext.BlockProtocolVersion"/>
public int BlockProtocolVersion => _txContext.BlockProtocolVersion;
public int BlockProtocolVersion { get; }

/// <inheritdoc cref="IActionContext.PreviousState"/>
public IAccount PreviousState { get; }
Expand Down
75 changes: 41 additions & 34 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,28 +183,36 @@ internal static IEnumerable<ActionEvaluation> EvaluateActions(
IImmutableList<IAction> actions,
ILogger? logger = null)
{
ITxContext txContext = new TxContext(
signer: tx?.Signer ?? blockHeader.Miner,
txId: tx?.Id ?? null,
miner: blockHeader.Miner,
blockIndex: blockHeader.Index,
blockProtocolVersion: blockHeader.ProtocolVersion);
IActionContext CreateActionContext(
IAccount prevState,
int randomSeed,
long actionGasLimit)
{
return new ActionContext(
signer: tx?.Signer ?? blockHeader.Miner,
txid: tx?.Id ?? null,
miner: blockHeader.Miner,
blockIndex: blockHeader.Index,
blockProtocolVersion: blockHeader.ProtocolVersion,
previousState: prevState,
randomSeed: randomSeed,
gasLimit: actionGasLimit);
}

long gasLimit = tx?.GasLimit ?? long.MaxValue;

byte[] preEvaluationHashBytes = blockHeader.PreEvaluationHash.ToByteArray();
byte[] signature = tx?.Signature ?? Array.Empty<byte>();
int seed = GenerateRandomSeed(preEvaluationHashBytes, signature, 0);
long gasLimit = tx?.GasLimit ?? long.MaxValue;

IAccount state = previousState;
foreach (IAction action in actions)
{
IActionContext context = CreateActionContext(state, seed, gasLimit);
(ActionEvaluation Evaluation, long NextGasLimit) result = EvaluateAction(
blockHeader,
tx,
txContext,
state,
seed,
gasLimit,
context,
action,
logger);

Expand All @@ -220,39 +228,38 @@ internal static IEnumerable<ActionEvaluation> EvaluateActions(
}
}

// FIXME: Argument blockHeader is passed on purely for logging.
internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
IPreEvaluationBlockHeader blockHeader,
ITransaction? tx,
ITxContext txContext,
IAccount prevState,
int randomSeed,
long gasLimit,
IActionContext context,
IAction action,
ILogger? logger = null)
{
IActionContext inputContext = new ActionContext(
txContext,
prevState,
randomSeed,
gasLimit);
IActionContext context = inputContext;

IActionContext inputContext = context;
IAccount state = inputContext.PreviousState;
Exception? exc = null;
IFeeCollector feeCollector = new FeeCollector(context, tx?.MaxGasPrice);

IActionContext CreateActionContext(IAccount newPrevState)
{
return new ActionContext(
signer: inputContext.Signer,
txid: inputContext.TxId,
miner: inputContext.Miner,
blockIndex: inputContext.BlockIndex,
blockProtocolVersion: inputContext.BlockProtocolVersion,
previousState: newPrevState,
randomSeed: inputContext.RandomSeed,
gasLimit: inputContext.GasLimit());
}

try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
AccountMetrics.Initialize();
state = feeCollector.Mortgage(state);
context = new ActionContext(
txContext: txContext,
previousState: state,
randomSeed: randomSeed,
gasLimit: gasLimit);
context = CreateActionContext(state);
feeCollector = feeCollector.Next(context);
state = action.Execute(context);
logger?
Expand All @@ -279,8 +286,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
e,
message,
action,
txContext.TxId,
txContext.BlockIndex,
tx?.Id,
blockHeader.Index,
ByteUtil.Hex(blockHeader.PreEvaluationHash.ByteArray));
throw;
}
Expand All @@ -294,8 +301,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
e,
message,
action,
txContext.TxId,
txContext.BlockIndex,
tx?.Id,
blockHeader.Index,
ByteUtil.Hex(blockHeader.PreEvaluationHash.ByteArray));
var innerMessage =
$"The action {action} (block #{blockHeader.Index}, " +
Expand All @@ -308,8 +315,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
exc = new UnexpectedlyTerminatedActionException(
innerMessage,
blockHeader.PreEvaluationHash,
txContext.BlockIndex,
txContext.TxId,
blockHeader.Index,
tx?.Id,
null,
action,
e);
Expand Down
Loading

0 comments on commit 5ffe61d

Please sign in to comment.