Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor with repository pattern #2540

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions NineChronicles.Headless.Executable.Tests/Commands/TxCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,9 @@ public void Sign_Stake(bool gas)
}

[Theory]
[InlineData(null, null, false)]
[InlineData(0, null, true)]
[InlineData(ClaimStakeReward2.ObsoletedIndex - 1, null, false)]
[InlineData(ClaimStakeReward2.ObsoletedIndex, null, true)]
[InlineData(ClaimStakeReward2.ObsoletedIndex + 1, null, false)]
[InlineData(long.MaxValue, null, true)]
[InlineData(null, 1, false)]
[InlineData(null, 2, true)]
[InlineData(null, 3, false)]
[InlineData(null, 4, true)]
[InlineData(null, 5, false)]
public void Sign_ClaimStakeReward(long? blockIndex, int? actionVersion, bool gas)
[InlineData(true)]
[InlineData(false)]
public void Sign_ClaimStakeReward(bool gas)
{
var filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var actionCommand = new ActionCommand(_console);
Expand Down
1 change: 1 addition & 0 deletions NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ IActionLoader MakeSingleActionLoader()
hostBuilder.ConfigureServices(services =>
{
services.AddSingleton(_ => standaloneContext);
services.AddSingleton<IKeyStore>(standaloneContext.KeyStore);
services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(
serviceName: Assembly.GetEntryAssembly()?.GetName().Name ?? "NineChronicles.Headless",
Expand Down
44 changes: 44 additions & 0 deletions NineChronicles.Headless.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace NineChronicles.Headless.Tests.Action;

public class ActionContext : IActionContext
{
private long UsedGas { get; set; }

public Address Signer { get; init; }
public TxId? TxId { get; init; }
public Address Miner { get; init; }
public long BlockIndex { get; init; }
public int BlockProtocolVersion { get; init; }
public IWorld PreviousState { get; init; }
public int RandomSeed { get; init; }
public bool IsPolicyAction { get; init; }
public IReadOnlyList<ITransaction> Txs { get; init; }
public IReadOnlyList<EvidenceBase> Evidence { get; init; }
public void UseGas(long gas)
{
UsedGas += gas;
}

public IRandom GetRandom()
{
return new Random(RandomSeed);
}

public long GasUsed()
{
return UsedGas;
}

public long GasLimit()
{
return 0L;
}
}
17 changes: 17 additions & 0 deletions NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
using Nekoyume.Action.Loader;
using Nekoyume.Model.State;
using Nekoyume.Module;
using NineChronicles.Headless.Repositories.BlockChain;
using NineChronicles.Headless.Repositories.StateTrie;
using NineChronicles.Headless.Repositories.Transaction;
using NineChronicles.Headless.Repositories.WorldState;
using NineChronicles.Headless.Utils;

namespace NineChronicles.Headless.Tests
Expand Down Expand Up @@ -61,6 +65,10 @@ public static Task<ExecutionResult> ExecuteQueryAsync<TObjectGraphType>(

services.AddLibplanetExplorer();
services.AddSingleton<StateMemoryCache>();
services.AddTransient<IWorldStateRepository, WorldStateRepository>();
services.AddTransient<IBlockChainRepository, BlockChainRepository>();
services.AddSingleton<IStateTrieRepository, StateTrieRepository>();
services.AddSingleton<ITransactionRepository, TransactionRepository>();

var serviceProvider = services.BuildServiceProvider();
return ExecuteQueryAsync<TObjectGraphType>(
Expand All @@ -78,6 +86,15 @@ public static Task<ExecutionResult> ExecuteQueryAsync<TObjectGraphType>(
where TObjectGraphType : IObjectGraphType
{
var graphType = (IObjectGraphType)serviceProvider.GetService(typeof(TObjectGraphType))!;
return ExecuteQueryAsync(graphType, query, userContext, source);
}

public static Task<ExecutionResult> ExecuteQueryAsync(
IObjectGraphType graphType,
string query,
IDictionary<string, object>? userContext = null,
object? source = null)
{
var documentExecutor = new DocumentExecuter();
return documentExecutor.ExecuteAsync(new ExecutionOptions
{
Expand Down
47 changes: 41 additions & 6 deletions NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@
using System.Threading;
using System.Threading.Tasks;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Mocks;
using Libplanet.Types.Tx;
using Moq;
using NineChronicles.Headless.Executable.Tests.KeyStore;
using NineChronicles.Headless.Repositories;
using NineChronicles.Headless.Repositories.BlockChain;
using NineChronicles.Headless.Repositories.StateTrie;
using NineChronicles.Headless.Repositories.Transaction;
using NineChronicles.Headless.Repositories.WorldState;
using Xunit.Abstractions;

namespace NineChronicles.Headless.Tests.GraphTypes
Expand Down Expand Up @@ -86,12 +95,10 @@ public GraphQLTestBase(ITestOutputHelper output)
privateKey: AdminPrivateKey);

var ncService = ServiceBuilder.CreateNineChroniclesNodeService(genesisBlock, ProposerPrivateKey);
var tempKeyStorePath = Path.Join(Path.GetTempPath(), Path.GetRandomFileName());
var keyStore = new Web3KeyStore(tempKeyStorePath);

StandaloneContextFx = new StandaloneContext
{
KeyStore = keyStore,
KeyStore = KeyStore,
DifferentAppProtocolVersionEncounterInterval = TimeSpan.FromSeconds(1),
NotificationInterval = TimeSpan.FromSeconds(1),
NodeExceptionInterval = TimeSpan.FromSeconds(1),
Expand All @@ -117,6 +124,12 @@ public GraphQLTestBase(ITestOutputHelper output)
);
services.AddSingleton(publisher);
services.AddSingleton(StandaloneContextFx);
services.AddTransient(provider => provider.GetService<StandaloneContext>().BlockChain);
services.AddSingleton<IWorldStateRepository>(WorldStateRepository.Object);
services.AddSingleton<IBlockChainRepository>(BlockChainRepository.Object);
services.AddSingleton<IStateTrieRepository>(StateTrieRepository.Object);
services.AddSingleton<ITransactionRepository>(TransactionRepository.Object);
services.AddSingleton<IKeyStore>(KeyStore);
services.AddSingleton<IConfiguration>(configuration);
services.AddGraphTypes();
services.AddLibplanetExplorer();
Expand All @@ -129,6 +142,12 @@ public GraphQLTestBase(ITestOutputHelper output)
DocumentExecutor = new DocumentExecuter();
}

protected Mock<IWorldStateRepository> WorldStateRepository { get; } = new();
protected Mock<IStateTrieRepository> StateTrieRepository { get; } = new();
protected Mock<IBlockChainRepository> BlockChainRepository { get; } = new();
protected Mock<ITransactionRepository> TransactionRepository { get; } = new();
protected IKeyStore KeyStore { get; } = new InMemoryKeyStore();

protected PrivateKey AdminPrivateKey { get; } = new PrivateKey();

protected Address AdminAddress => AdminPrivateKey.Address;
Expand All @@ -150,9 +169,6 @@ protected List<PrivateKey> GenesisValidators
protected BlockChain BlockChain =>
StandaloneContextFx.BlockChain!;

protected IKeyStore KeyStore =>
StandaloneContextFx.KeyStore!;

protected IDocumentExecuter DocumentExecutor { get; }

protected SubscriptionDocumentExecuter SubscriptionDocumentExecuter { get; } = new SubscriptionDocumentExecuter();
Expand Down Expand Up @@ -187,6 +203,25 @@ protected async Task<Task> StartAsync(
return task;
}

protected void SetupStatesOnTip(Func<IWorld, IWorld> func)
{
var worldState = func(new World(MockUtil.MockModernWorldState));
var stateRootHash = worldState.Trie.Hash;
var tip = new Domain.Model.BlockChain.Block(
BlockHash.FromString("613dfa26e104465790625ae7bc03fc27a64947c02a9377565ec190405ef7154b"),
BlockHash.FromString("36456be15af9a5b9b13a02c7ce1e849ae9cba8781ec309010499cdb93e29237d"),
default(Address),
0,
Timestamp: DateTimeOffset.UtcNow,
StateRootHash: stateRootHash,
Transactions: ImmutableArray<Transaction>.Empty
);
BlockChainRepository.Setup(repository => repository.GetTip())
.Returns(tip);
WorldStateRepository.Setup(repository => repository.GetWorldState(stateRootHash))
.Returns(worldState);
}

protected LibplanetNodeService CreateLibplanetNodeService(
Block genesisBlock,
AppProtocolVersion appProtocolVersion,
Expand Down
Loading
Loading