Skip to content

Commit

Permalink
build: Fix build errors in test projects or etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Mar 21, 2024
1 parent 8baee41 commit e8d07a0
Show file tree
Hide file tree
Showing 36 changed files with 530 additions and 142 deletions.
37 changes: 37 additions & 0 deletions Libplanet.Action.Tests/Common/IncrementAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;

namespace Libplanet.Action.Tests.Common
{
public sealed class IncrementAction : IAction
{
public static readonly Address IncrementAddress =
new Address("0000000000000000000000000000000000000123");

public IncrementAction()
{
}

public IValue PlainValue => Bencodex.Types.Dictionary.Empty;

public void LoadPlainValue(IValue plainValue)
{
}

public IWorld Execute(IActionContext ctx)
{
IWorld states = ctx.PreviousState;
IAccount account = states.GetAccount(ReservedAddresses.LegacyAccount);
int value = 0;

if (account.GetState(IncrementAddress) is Integer integer)
{
value = (int)integer.Value + 1;
}

account = account.SetState(IncrementAddress, new Integer(value));
return states.SetAccount(ReservedAddresses.LegacyAccount, account);
}
}
}
4 changes: 3 additions & 1 deletion Libplanet.Benchmarks/AppendBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Libplanet.Types.Blocks;
using Libplanet.Tests;
using Libplanet.Tests.Store;
using System.Collections.Immutable;

namespace Libplanet.Benchmarks
{
Expand All @@ -29,7 +30,8 @@ public AppendBlock()
fx.StateStore,
fx.GenesisBlock,
new ActionEvaluator(
policyBlockActionGetter: _ => null,
policyBeginBlockActionGetter: _ => ImmutableArray<IAction>.Empty,
policyEndBlockActionGetter: _ => 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
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using Libplanet.Action;
using Libplanet.Action.Loader;
Expand Down Expand Up @@ -35,7 +36,8 @@ public void SetupChain()
_fx.StateStore,
_fx.GenesisBlock,
new ActionEvaluator(
policyBlockActionGetter: _ => null,
policyBeginBlockActionGetter: _ => ImmutableArray<IAction>.Empty,
policyEndBlockActionGetter: _ => 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 @@ -8,6 +8,7 @@
using Libplanet.Types.Blocks;
using Libplanet.Tests;
using Libplanet.Tests.Store;
using System.Collections.Immutable;

namespace Libplanet.Benchmarks
{
Expand All @@ -28,7 +29,8 @@ public ProposeBlock()
fx.StateStore,
fx.GenesisBlock,
new ActionEvaluator(
policyBlockActionGetter: _ => null,
policyBeginBlockActionGetter: _ => ImmutableArray<IAction>.Empty,
policyEndBlockActionGetter: _ => ImmutableArray<IAction>.Empty,
stateStore: fx.StateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
_privateKey = new PrivateKey();
Expand Down
10 changes: 7 additions & 3 deletions Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ If omitted (default) explorer only the local blockchain store.")]
options.GetGenesisBlock(policy),
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
stateStore,
new SingleActionLoader(typeof(NullAction))));
Startup.PreloadedSingleton = false;
Expand Down Expand Up @@ -341,7 +342,8 @@ private static IStore LoadStore(Options options)
private static BlockPolicy LoadBlockPolicy(Options options)
{
return new BlockPolicy(
blockAction: null,
beginBlockActions: ImmutableArray<IAction>.Empty,
endBlockActions: ImmutableArray<IAction>.Empty,
blockInterval: TimeSpan.FromMilliseconds(options.BlockIntervalMilliseconds),
getMaxTransactionsBytes: i => i > 0
? options.MaxTransactionsBytes
Expand Down Expand Up @@ -384,7 +386,9 @@ public DumbBlockPolicy(BlockPolicy blockPolicy)
_impl = blockPolicy;
}

public IAction BlockAction => _impl.BlockAction;
public ImmutableArray<IAction> BeginBlockActions => _impl.BeginBlockActions;

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

public int GetMinTransactionsPerBlock(long index) =>
_impl.GetMinTransactionsPerBlock(index);
Expand Down
3 changes: 2 additions & 1 deletion Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public GeneratedBlockChainFixture(
getMaxTransactionsPerBlock: _ => int.MaxValue,
getMaxTransactionsBytes: _ => long.MaxValue);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
stateStore,
TypedActionLoader.Create(typeof(SimpleAction).Assembly, typeof(SimpleAction)));
Block genesisBlock = BlockChain.ProposeGenesisBlock(
Expand Down
32 changes: 26 additions & 6 deletions Libplanet.Extensions.Cocona.Tests/BlockPolicyParamsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public void DefaultState()
{
var blockPolicyParams = new BlockPolicyParams();
Assert.Null(blockPolicyParams.GetBlockPolicy());
Assert.Null(blockPolicyParams.GetBlockAction());
Assert.Empty(blockPolicyParams.GetBeginBlockActions());
Assert.Empty(blockPolicyParams.GetEndBlockActions());
}

[Fact]
Expand All @@ -26,7 +27,10 @@ public void GetBlockPolicy()
BlockPolicy blockPolicy = Assert.IsType<BlockPolicy>(
blockPolicyParams.GetBlockPolicy(new[] { GetType().Assembly })
);
Assert.IsType<NullAction>(blockPolicy.BlockAction);
Assert.Single(blockPolicy.BeginBlockActions);
Assert.IsType<NullAction>(blockPolicy.BeginBlockActions[0]);
Assert.Single(blockPolicy.EndBlockActions);
Assert.IsType<NullAction>(blockPolicy.EndBlockActions[0]);
}

[Fact]
Expand Down Expand Up @@ -123,18 +127,34 @@ public void GetBlockPolicy_FactoryReturningNull()
}

[Fact]
public void GetBlockAction()
public void GetBeginBlockActions()
{
var blockPolicyParams = new BlockPolicyParams
{
PolicyFactory = $"{GetType().FullName}.{nameof(BlockPolicyFactory)}",
};
var blockAction = blockPolicyParams.GetBlockAction(new[] { GetType().Assembly });
Assert.IsType<NullAction>(blockAction);
var blockActions = blockPolicyParams.GetBeginBlockActions(new[] { GetType().Assembly });
Assert.Single(blockActions);
Assert.IsType<NullAction>(blockActions[0]);
}

[Fact]
public void GetEndBlockActions()
{
var blockPolicyParams = new BlockPolicyParams
{
PolicyFactory = $"{GetType().FullName}.{nameof(BlockPolicyFactory)}",
};
var blockActions = blockPolicyParams.GetEndBlockActions(new[] { GetType().Assembly });
Assert.Single(blockActions);
Assert.IsType<NullAction>(blockActions[0]);
}

internal static BlockPolicy BlockPolicyFactory() =>
new BlockPolicy(blockAction: new NullAction());
new BlockPolicy(
beginBlockActions: new IAction[] { new NullAction() }.ToImmutableArray(),
endBlockActions: new IAction[] { new NullAction() }.ToImmutableArray()
);

internal static BlockPolicy BlockPolicyFactoryWithParams(bool param) =>
new BlockPolicy();
Expand Down
65 changes: 58 additions & 7 deletions Libplanet.Extensions.Cocona/BlockPolicyParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public Assembly[] LoadAssemblies()
public object? GetBlockPolicy() =>
GetBlockPolicy(LoadAssemblies());

public IAction? GetBlockAction() =>
GetBlockAction(LoadAssemblies());
public ImmutableArray<IAction> GetBeginBlockActions() =>
GetBeginBlockActions(LoadAssemblies());

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

[SuppressMessage(
"Major Code Smell",
Expand Down Expand Up @@ -132,17 +135,65 @@ public Assembly[] LoadAssemblies()
);
}

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

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

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

return (ImmutableArray<IAction>)value;
}

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

PropertyInfo? propertyInfo = policy
.GetType()
.GetProperty(nameof(IBlockPolicy.EndBlockActions));
if (propertyInfo is null)
{
var message = $"The policy type "
+ $"'{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.";
throw new InvalidOperationException(message);
}

return (ImmutableArray<IAction>)value;
}
}
6 changes: 4 additions & 2 deletions Libplanet.Extensions.Cocona/Commands/BlockCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ public void GenerateGenesis(
}.Select(x => x.PlainValue)))
.ToImmutableList();

var blockAction = blockPolicyParams.GetBlockAction();
var beginBlockActions = blockPolicyParams.GetBeginBlockActions();
var endBlockActions = blockPolicyParams.GetEndBlockActions();
var actionEvaluator = new ActionEvaluator(
_ => blockAction,
_ => beginBlockActions,
_ => endBlockActions,
new TrieStateStore(new DefaultKeyValueStore(null)),
new SingleActionLoader(typeof(NullAction)));
Block genesis = BlockChain.ProposeGenesisBlock(
Expand Down
8 changes: 6 additions & 2 deletions Libplanet.Net.Tests/Consensus/ConsensusReactorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public async void StartAsync()
var consensusReactors = new ConsensusReactor[4];
var stores = new IStore[4];
var blockChains = new BlockChain[4];
var fx = new MemoryStoreFixture(TestUtils.Policy.BlockAction);
var fx = new MemoryStoreFixture(
TestUtils.Policy.BeginBlockActions,
TestUtils.Policy.EndBlockActions
);
var validatorPeers = new List<BoundPeer>();
var cancellationTokenSource = new CancellationTokenSource();

Expand All @@ -66,7 +69,8 @@ public async void StartAsync()
stateStore,
fx.GenesisBlock,
new ActionEvaluator(
policyBlockActionGetter: _ => TestUtils.Policy.BlockAction,
policyBeginBlockActionGetter: _ => TestUtils.Policy.BeginBlockActions,
policyEndBlockActionGetter: _ => TestUtils.Policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new SingleActionLoader(typeof(DumbAction))));
}
Expand Down
17 changes: 13 additions & 4 deletions Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading.Tasks;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.Tests.Common;
using Libplanet.Blockchain;
Expand Down Expand Up @@ -259,7 +261,8 @@ public async Task EnterPreVoteNilOnInvalidBlockContent()
var nilPreVoteSent = new AsyncAutoResetEvent();
var invalidKey = new PrivateKey();
var policy = new BlockPolicy(
blockAction: new MinerReward(1),
beginBlockActions: ImmutableArray<IAction>.Empty,
endBlockActions: ImmutableArray.Create<IAction>(new MinerReward(1)),
getMaxTransactionsBytes: _ => 50 * 1024,
validateNextBlockTx: IsSignerValid);

Expand Down Expand Up @@ -294,7 +297,10 @@ public async Task EnterPreVoteNilOnInvalidBlockContent()
}
};

using var fx = new MemoryStoreFixture(policy.BlockAction);
using var fx = new MemoryStoreFixture(
policy.BeginBlockActions,
policy.EndBlockActions
);
var diffPolicyBlockChain =
TestUtils.CreateDummyBlockChain(
fx, policy, new SingleActionLoader(typeof(DumbAction)), blockChain.Genesis);
Expand Down Expand Up @@ -332,7 +338,8 @@ public async Task EnterPreVoteNilOnInvalidAction()
var nilPreCommitSent = new AsyncAutoResetEvent();
var txSigner = new PrivateKey();
var policy = new BlockPolicy(
blockAction: new MinerReward(1),
beginBlockActions: ImmutableArray<IAction>.Empty,
endBlockActions: ImmutableArray.Create<IAction>(new MinerReward(1)),
getMaxTransactionsBytes: _ => 50 * 1024);

var (blockChain, context) = TestUtils.CreateDummyContext(
Expand Down Expand Up @@ -363,7 +370,9 @@ message is ConsensusPreCommitMsg commit &&
}
};

using var fx = new MemoryStoreFixture(policy.BlockAction);
using var fx = new MemoryStoreFixture(
policy.BeginBlockActions,
policy.EndBlockActions);

var unsignedInvalidTx = new UnsignedTx(
new TxInvoice(
Expand Down
8 changes: 6 additions & 2 deletions Libplanet.Net.Tests/Consensus/ContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -291,9 +292,12 @@ public async Task CanPreCommitOnEndCommit()
TimeSpan newHeightDelay = TimeSpan.FromSeconds(1);

var policy = new BlockPolicy(
blockAction: new MinerReward(1),
beginBlockActions: ImmutableArray<IAction>.Empty,
endBlockActions: ImmutableArray.Create<IAction>(new MinerReward(1)),
getMaxTransactionsBytes: _ => 50 * 1024);
var fx = new MemoryStoreFixture(policy.BlockAction);
var fx = new MemoryStoreFixture(
policy.BeginBlockActions,
policy.EndBlockActions);
var blockChain = Libplanet.Tests.TestUtils.MakeBlockChain(
policy,
fx.Store,
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class HeightVoteSetTest
public HeightVoteSetTest()
{
_blockChain = TestUtils.CreateDummyBlockChain(
new MemoryStoreFixture(TestUtils.Policy.BlockAction));
new MemoryStoreFixture(
TestUtils.Policy.BeginBlockActions,
TestUtils.Policy.EndBlockActions));
var block = _blockChain.ProposeBlock(TestUtils.PrivateKeys[1]);
_lastCommit = TestUtils.CreateBlockCommit(block);
_heightVoteSet = new HeightVoteSet(2, TestUtils.ValidatorSet);
Expand Down
Loading

0 comments on commit e8d07a0

Please sign in to comment.