Skip to content

Commit

Permalink
feat: add blockaction flag to action evaluator [skip changelog]
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 22, 2024
1 parent 5cbb2c6 commit a57ed11
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Libplanet.Action.Tests/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void RandomShouldBeDeterministic()
lastCommit: _lastCommit,
previousState: new World(new MockWorldState()),
randomSeed: seed,
blockAction: false,
gasLimit: 0
);
IRandom random = context.GetRandom();
Expand All @@ -79,6 +80,7 @@ public void GuidShouldBeDeterministic()
lastCommit: _lastCommit,
previousState: new World(new MockWorldState()),
randomSeed: 0,
blockAction: false,
gasLimit: 0
);

Expand All @@ -91,6 +93,7 @@ public void GuidShouldBeDeterministic()
lastCommit: _lastCommit,
previousState: new World(new MockWorldState()),
randomSeed: 0,
blockAction: false,
gasLimit: 0
);

Expand All @@ -103,6 +106,7 @@ public void GuidShouldBeDeterministic()
lastCommit: _lastCommit,
previousState: new World(new MockWorldState()),
randomSeed: 1,
blockAction: false,
gasLimit: 0
);

Expand Down Expand Up @@ -143,6 +147,7 @@ public void GuidVersionAndVariant()
lastCommit: _lastCommit,
previousState: new World(new MockWorldState()),
randomSeed: i,
blockAction: false,
gasLimit: 0
);
var guid = context.GetRandom().GenerateRandomGuid().ToString();
Expand Down
1 change: 1 addition & 0 deletions Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void Constructor()
lastCommit,
new World(new MockWorldState()),
123,
false,
0),
world);
var action = (DumbAction)evaluation.Action;
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Action.Tests/Sys/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void Execute()
lastCommit: null,
previousState: prevState,
randomSeed: 123,
blockAction: false,
gasLimit: 0);
var initialize = new Initialize(
states: _states,
Expand Down Expand Up @@ -103,6 +104,7 @@ public void ExecuteInNonGenesis()
lastCommit: lastCommit,
previousState: prevState,
randomSeed: 123,
blockAction: false,
gasLimit: long.MaxValue);
var initialize = new Initialize(
states: _states,
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ActionContext(
BlockCommit? lastCommit,
IWorld previousState,
int randomSeed,
bool blockAction,
long gasLimit)
{
Signer = signer;
Expand All @@ -32,6 +33,7 @@ public ActionContext(
LastCommit = lastCommit;
PreviousState = previousState;
RandomSeed = randomSeed;
BlockAction = blockAction;
_gasLimit = gasLimit;

GetGasMeter.Value = new GasMeter(_gasLimit);
Expand Down Expand Up @@ -62,7 +64,7 @@ public ActionContext(
public int RandomSeed { get; }

/// <inheritdoc cref="IActionContext.BlockAction"/>
public bool BlockAction => TxId is null;
public bool BlockAction { get; }

/// <inheritdoc cref="IActionContext.UseGas(long)"/>
public void UseGas(long gas) => GetGasMeter.Value?.UseGas(gas);
Expand Down
11 changes: 11 additions & 0 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
/// being executed.</param>
/// <param name="actions">Actions to evaluate.</param>
/// <param name="stateStore">An <see cref="IStateStore"/> to use.</param>
/// <param name="blockAction">
/// Flag indicates that whether the action is a block action.</param>
/// <param name="logger">An optional logger.</param>
/// <returns>An enumeration of <see cref="ActionEvaluation"/>s for each
/// <see cref="IAction"/> in <paramref name="actions"/>.
Expand All @@ -205,6 +207,7 @@ internal static IEnumerable<ActionEvaluation> EvaluateActions(
IWorld previousState,
IImmutableList<IAction> actions,
IStateStore stateStore,
bool blockAction,
ILogger? logger = null)
{
IActionContext CreateActionContext(
Expand All @@ -221,6 +224,7 @@ IActionContext CreateActionContext(
lastCommit: blockHeader.LastCommit,
previousState: prevState,
randomSeed: randomSeed,
blockAction: blockAction,
gasLimit: actionGasLimit);
}

Expand All @@ -240,6 +244,7 @@ IActionContext CreateActionContext(
context,
action,
stateStore,
blockAction,
logger);

yield return result.Evaluation;
Expand All @@ -260,6 +265,7 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
IActionContext context,
IAction action,
IStateStore stateStore,
bool blockAction,
ILogger? logger = null)
{
if (!context.PreviousState.Trie.Recorded)
Expand All @@ -285,6 +291,7 @@ IActionContext CreateActionContext(IWorld newPrevState)
lastCommit: inputContext.LastCommit,
previousState: newPrevState,
randomSeed: inputContext.RandomSeed,
blockAction: blockAction,
gasLimit: inputContext.GasLimit());
}

Expand Down Expand Up @@ -501,6 +508,7 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
previousState: previousState,
actions: actions,
stateStore: _stateStore,
blockAction: false,
logger: _logger));

if (_policyActionsRegistry.EndTxActionsGetter(blockHeader) is
Expand Down Expand Up @@ -543,6 +551,7 @@ internal ActionEvaluation[] EvaluatePolicyBeginBlockActions(
previousState: previousState,
actions: _policyActionsRegistry.BeginBlockActionsGetter(blockHeader),
stateStore: _stateStore,
blockAction: true,
logger: _logger).ToArray();
}

Expand Down Expand Up @@ -572,6 +581,7 @@ internal ActionEvaluation[] EvaluatePolicyEndBlockActions(
previousState: previousState,
actions: _policyActionsRegistry.EndBlockActionsGetter(blockHeader),
stateStore: _stateStore,
blockAction: true,
logger: _logger).ToArray();
}

Expand Down Expand Up @@ -630,6 +640,7 @@ internal ActionEvaluation[] EvaluatePolicyEndTxActions(
previousState: previousState,
actions: _policyActionsRegistry.EndTxActionsGetter(blockHeader),
stateStore: _stateStore,
blockAction: true,
logger: _logger).ToArray();
}

Expand Down
1 change: 1 addition & 0 deletions Libplanet.Tests/Action/AccountDiffTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public IActionContext CreateActionContext(Address signer, ITrie trie) =>
trie,
new TrieStateStore(new MemoryKeyValueStore()))),
0,
true,
0);
}
}
1 change: 1 addition & 0 deletions Libplanet.Tests/Action/AccountV0Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public override IActionContext CreateContext(
null,
world,
0,
true,
0);
}

Expand Down
1 change: 1 addition & 0 deletions Libplanet.Tests/Action/AccountV1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override IActionContext CreateContext(IAccount delta, Address signer)
null,
world,
0,
true,
0);
}

Expand Down
65 changes: 62 additions & 3 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,8 @@ public void EvaluateActions()
actions: txA.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
stateStore: fx.StateStore).ToArray();
stateStore: fx.StateStore,
blockAction: false).ToArray();

Assert.Equal(evalsA.Length, deltaA.Count - 1);
Assert.Equal(
Expand Down Expand Up @@ -898,7 +899,8 @@ public void EvaluateActions()
actions: txB.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
stateStore: fx.StateStore).ToArray();
stateStore: fx.StateStore,
blockAction: false).ToArray();

Assert.Equal(evalsB.Length, deltaB.Count - 1);
Assert.Equal(
Expand Down Expand Up @@ -1061,6 +1063,7 @@ public void EvaluatePolicyBeginTxActions()
IWorld previousState = actionEvaluator.PrepareInitialDelta(null);
var evaluations = actionEvaluator.EvaluatePolicyBeginTxActions(
genesis,
txs[0],
previousState);

Assert.Equal<IAction>(
Expand All @@ -1072,11 +1075,13 @@ public void EvaluatePolicyBeginTxActions()
(Integer)evaluations[0].OutputState
.GetAccount(ReservedAddresses.LegacyAccount)
.GetState(_beginTxValueAddress));
Assert.Equal(txs[0].Signer, evaluations[0].InputContext.Signer);
Assert.True(evaluations[0].InputContext.BlockAction);

previousState = evaluations[0].OutputState;
evaluations = actionEvaluator.EvaluatePolicyBeginTxActions(
block,
txs[1],
previousState);

Assert.Equal<IAction>(
Expand All @@ -1088,6 +1093,59 @@ public void EvaluatePolicyBeginTxActions()
(Integer)evaluations[0].OutputState
.GetAccount(ReservedAddresses.LegacyAccount)
.GetState(_beginTxValueAddress));
Assert.Equal(txs[1].Signer, evaluations[0].InputContext.Signer);
Assert.True(evaluations[0].InputContext.BlockAction);
}

[Fact]
public void EvaluatePolicyEndTxActions()
{
var (chain, actionEvaluator) = MakeBlockChainAndActionEvaluator(
policy: _policy,
store: _storeFx.Store,
stateStore: _storeFx.StateStore,
actionLoader: new SingleActionLoader(typeof(DumbAction)),
genesisBlock: _storeFx.GenesisBlock,
privateKey: GenesisProposer);
(_, Transaction[] txs) = MakeFixturesForAppendTests();
var genesis = chain.Genesis;
var block = chain.ProposeBlock(
GenesisProposer, txs.ToImmutableList(), CreateBlockCommit(chain.Tip));

IWorld previousState = actionEvaluator.PrepareInitialDelta(null);
var evaluations = actionEvaluator.EvaluatePolicyEndTxActions(
genesis,
txs[0],
previousState);

Assert.Equal<IAction>(
chain.Policy.EndTxActions,
evaluations.Select(item => item.Action).ToImmutableArray());
Assert.Single(evaluations);
Assert.Equal(
(Integer)1,
(Integer)evaluations[0].OutputState
.GetAccount(ReservedAddresses.LegacyAccount)
.GetState(_endTxValueAddress));
Assert.Equal(txs[0].Signer, evaluations[0].InputContext.Signer);
Assert.True(evaluations[0].InputContext.BlockAction);

previousState = evaluations[0].OutputState;
evaluations = actionEvaluator.EvaluatePolicyEndTxActions(
block,
txs[1],
previousState);

Assert.Equal<IAction>(
chain.Policy.EndTxActions,
evaluations.Select(item => item.Action).ToImmutableArray());
Assert.Single(evaluations);
Assert.Equal(
(Integer)2,
(Integer)evaluations[0].OutputState
.GetAccount(ReservedAddresses.LegacyAccount)
.GetState(_endTxValueAddress));
Assert.Equal(txs[1].Signer, evaluations[0].InputContext.Signer);
Assert.True(evaluations[0].InputContext.BlockAction);
}

Expand Down Expand Up @@ -1519,7 +1577,8 @@ public void CheckRandomSeedInAction()
actions: txA.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
stateStore: fx.StateStore).ToArray();
stateStore: fx.StateStore,
blockAction: false).ToArray();

byte[] preEvaluationHashBytes = blockA.PreEvaluationHash.ToByteArray();
int[] randomSeeds = Enumerable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AnonymousActionRendererTest
null,
_world,
default,
false,
0));

private static Exception _exception = new Exception();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void ActionRenderings(bool error, bool exception)
null,
_world,
default,
false,
0));
Exception actionError = new Exception();
IActionRenderer actionRenderer;
Expand Down

0 comments on commit a57ed11

Please sign in to comment.