Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Sep 5, 2024
1 parent d332132 commit a6e6abc
Show file tree
Hide file tree
Showing 23 changed files with 651 additions and 505 deletions.
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
26 changes: 20 additions & 6 deletions NineChronicles.Headless.Tests/GraphTypes/GraphQLTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
using System.Threading.Tasks;
using Bencodex.Types;
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 +93,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 +122,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 +140,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 +167,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
Loading

0 comments on commit a6e6abc

Please sign in to comment.