Skip to content

Commit

Permalink
feat: add txactions
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 16, 2024
1 parent 024475a commit 6d33ccc
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 60 deletions.
19 changes: 18 additions & 1 deletion Libplanet.Action/PolicyActionsGetterCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

namespace Libplanet.Action
{
Expand All @@ -17,16 +18,32 @@ public class PolicyActionsGetterCollection
/// evaluate at the end for each <see cref="IPreEvaluationBlock"/> that gets evaluated.
/// Note the order of the returned list determines the execution order.
/// </param>
/// <param name="beginTxActionsGetter">A delegator to get policy block actions to
/// evaluate at the beginning for each <see cref="Transaction"/> that gets evaluated.
/// Note the order of the returned list determines the execution order.
/// </param>
/// <param name="endTxActionsGetter">A delegator to get policy block actions to
/// evaluate at the end for each <see cref="Transaction"/> that gets evaluated.
/// Note the order of the returned list determines the execution order.
/// </param>
public PolicyActionsGetterCollection(
PolicyActionsGetter beginBlockActionsGetter,
PolicyActionsGetter endBlockActionsGetter)
PolicyActionsGetter endBlockActionsGetter,
PolicyActionsGetter beginTxActionsGetter,
PolicyActionsGetter endTxActionsGetter)
{
BeginBlockActionsGetter = beginBlockActionsGetter;
EndBlockActionsGetter = endBlockActionsGetter;
BeginTxActionsGetter = beginTxActionsGetter;
EndTxActionsGetter = endTxActionsGetter;
}

public PolicyActionsGetter BeginBlockActionsGetter { get; }

public PolicyActionsGetter EndBlockActionsGetter { get; }

public PolicyActionsGetter BeginTxActionsGetter { get; }

public PolicyActionsGetter EndTxActionsGetter { get; }
}
}
4 changes: 3 additions & 1 deletion Libplanet.Benchmarks/AppendBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public AppendBlock()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
_privateKey = new PrivateKey();
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Benchmarks/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public void SetupChain()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: _fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
var key = new PrivateKey();
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Benchmarks/ProposeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public ProposeBlock()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
_privateKey = new PrivateKey();
Expand Down
8 changes: 7 additions & 1 deletion Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ If omitted (default) explorer only the local blockchain store.")]
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore,
new SingleActionLoader(typeof(NullAction))));
Startup.PreloadedSingleton = false;
Expand Down Expand Up @@ -391,6 +393,10 @@ public DumbBlockPolicy(BlockPolicy blockPolicy)

public ImmutableArray<IAction> EndBlockActions => _impl.EndBlockActions;

public ImmutableArray<IAction> BeginTxActions => _impl.BeginTxActions;

public ImmutableArray<IAction> EndTxActions => _impl.EndTxActions;

public int GetMinTransactionsPerBlock(long index) =>
_impl.GetMinTransactionsPerBlock(index);

Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public GeneratedBlockChainFixture(
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore,
TypedActionLoader.Create(typeof(SimpleAction).Assembly, typeof(SimpleAction)));
Block genesisBlock = BlockChain.ProposeGenesisBlock(
Expand Down
76 changes: 72 additions & 4 deletions Libplanet.Extensions.Cocona/BlockPolicyParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public ImmutableArray<IAction> GetBeginBlockActions() =>
public ImmutableArray<IAction> GetEndBlockActions() =>
GetEndBlockActions(LoadAssemblies());

public ImmutableArray<IAction> GetBeginTxActions() =>
GetBeginTxActions(LoadAssemblies());

public ImmutableArray<IAction> GetEndTxActions() =>
GetEndTxActions(LoadAssemblies());

[SuppressMessage(
"Major Code Smell",
"S3011:Reflection should not be used to increase accessibility of classes, methods, " +
Expand Down Expand Up @@ -180,17 +186,79 @@ internal ImmutableArray<IAction> GetEndBlockActions(Assembly[] assemblies)
if (propertyInfo is null)
{
var message = $"The policy type "
+ $"'{policy.GetType().FullName}' does not have a "
+ $"'{nameof(IBlockPolicy.EndBlockActions)}' property.";
+ $"'{policy.GetType().FullName}' does not have a "
+ $"'{nameof(IBlockPolicy.EndBlockActions)}' property.";
throw new InvalidOperationException(message);
}

var value = propertyInfo.GetValue(policy);
if (value is null)
{
var message = $"The value of property "
+ $"'{nameof(IBlockPolicy.EndBlockActions)}' of type "
+ $"'{policy.GetType().FullName}' cannot be null.";
+ $"'{nameof(IBlockPolicy.EndBlockActions)}' of type "
+ $"'{policy.GetType().FullName}' cannot be null.";
throw new InvalidOperationException(message);
}

return (ImmutableArray<IAction>)value;
}

internal ImmutableArray<IAction> GetBeginTxActions(Assembly[] assemblies)
{
object? policy = GetBlockPolicy(assemblies);
if (policy is null)
{
return ImmutableArray<IAction>.Empty;
}

PropertyInfo? propertyInfo = policy
.GetType()
.GetProperty(nameof(IBlockPolicy.BeginTxActions));
if (propertyInfo is null)
{
var message = $"The policy type "
+ $"'{policy.GetType().FullName}' does not have a "
+ $"'{nameof(IBlockPolicy.BeginTxActions)}' property.";
throw new InvalidOperationException(message);
}

var value = propertyInfo.GetValue(policy);
if (value is null)
{
var message = $"The value of property "
+ $"'{nameof(IBlockPolicy.BeginTxActions)}' of type "
+ $"'{policy.GetType().FullName}' cannot be null.";
throw new InvalidOperationException(message);
}

return (ImmutableArray<IAction>)value;
}

internal ImmutableArray<IAction> GetEndTxActions(Assembly[] assemblies)
{
object? policy = GetBlockPolicy(assemblies);
if (policy is null)
{
return ImmutableArray<IAction>.Empty;
}

PropertyInfo? propertyInfo = policy
.GetType()
.GetProperty(nameof(IBlockPolicy.EndTxActions));
if (propertyInfo is null)
{
var message = $"The policy type "
+ $"'{policy.GetType().FullName}' does not have a "
+ $"'{nameof(IBlockPolicy.EndTxActions)}' property.";
throw new InvalidOperationException(message);
}

var value = propertyInfo.GetValue(policy);
if (value is null)
{
var message = $"The value of property "
+ $"'{nameof(IBlockPolicy.EndTxActions)}' of type "
+ $"'{policy.GetType().FullName}' cannot be null.";
throw new InvalidOperationException(message);
}

Expand Down
6 changes: 5 additions & 1 deletion Libplanet.Extensions.Cocona/Commands/BlockCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,14 @@ public void GenerateGenesis(

var beginBlockActions = blockPolicyParams.GetBeginBlockActions();
var endBlockActions = blockPolicyParams.GetEndBlockActions();
var beginTxActions = blockPolicyParams.GetEndBlockActions();
var endTxActions = blockPolicyParams.GetEndBlockActions();
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => beginBlockActions,
_ => endBlockActions),
_ => endBlockActions,
_ => beginTxActions,
_ => endTxActions),
new TrieStateStore(new DefaultKeyValueStore(null)),
new SingleActionLoader(typeof(NullAction)));
Block genesis = BlockChain.ProposeGenesisBlock(
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Net.Tests/Consensus/ConsensusReactorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public async void StartAsync()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => TestUtils.Policy.BeginBlockActions,
endBlockActionsGetter: _ => TestUtils.Policy.EndBlockActions),
endBlockActionsGetter: _ => TestUtils.Policy.EndBlockActions,
beginTxActionsGetter: _ => TestUtils.Policy.BeginTxActions,
endTxActionsGetter: _ => TestUtils.Policy.EndTxActions),
stateStore: stateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
}
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Net.Tests/SwarmTest.Broadcast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ public async Task BroadcastTxAsyncMany()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => policy.BeginBlockActions,
endBlockActionsGetter_ => policy.EndBlockActions),
endBlockActionsGetter: _ => policy.EndBlockActions,
beginTxActionsGetter: _ => policy.BeginTxActions,
endTxActionsGetter: _ => policy.EndTxActions),
stateStore: fxs[i].StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
swarms[i] = await CreateSwarm(blockChains[i]).ConfigureAwait(false);
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.RocksDBStore.Tests/RocksDBStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public void ReopenStoreAfterDispose()
new ActionEvaluator(
policyActionsGetterCollection: new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: stateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
store.Dispose();
Expand Down
16 changes: 12 additions & 4 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public void Idempotent()
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore,
new SingleActionLoader(typeof(RandomAction)));
Block stateRootBlock = noStateRootBlock.Sign(
Expand Down Expand Up @@ -300,7 +302,9 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
actionTypeLoader: new SingleActionLoader(typeof(DumbAction)));

Expand Down Expand Up @@ -584,7 +588,9 @@ public void EvaluateTx()
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
actionTypeLoader: new SingleActionLoader(typeof(DumbAction)));

Expand Down Expand Up @@ -687,7 +693,9 @@ public void EvaluateTxResultThrowingException()
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
beginBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty),
endBlockActionsGetter: _ => ImmutableArray<IAction>.Empty,
beginTxActionsGetter: _ => ImmutableArray<IAction>.Empty,
endTxActionsGetter: _ => ImmutableArray<IAction>.Empty),
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
actionTypeLoader: new SingleActionLoader(typeof(ThrowException))
);
Expand Down
12 changes: 9 additions & 3 deletions Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ TxPolicyViolationException IsSignerValid(
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));

Expand Down Expand Up @@ -596,7 +598,9 @@ public void AppendValidatesBlock()
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
_fx.StateStore,
new SingleActionLoader(typeof(DumbAction))));
Assert.Throws<BlockPolicyViolationException>(
Expand Down Expand Up @@ -773,7 +777,9 @@ public void DoesNotMigrateStateWithoutAction()
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction)));

Expand Down
16 changes: 12 additions & 4 deletions Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ public void CanProposeInvalidGenesisBlock()
var actionEvaluator = new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
fx.StateStore,
new SingleActionLoader(typeof(DumbAction)));
var genesis = BlockChain.ProposeGenesisBlock(
Expand Down Expand Up @@ -185,7 +187,9 @@ public void CanProposeInvalidBlock()
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
var txs = new[]
Expand Down Expand Up @@ -411,7 +415,9 @@ TxPolicyViolationException IsSignerValid(
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));

Expand Down Expand Up @@ -533,7 +539,9 @@ public void ProposeBlockWithBlockAction()
new ActionEvaluator(
new PolicyActionsGetterCollection(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions),
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
_fx.StateStore,
new SingleActionLoader(typeof(DumbAction))));

Expand Down
Loading

0 comments on commit 6d33ccc

Please sign in to comment.