Skip to content

Commit

Permalink
Merge pull request #2549 from planetarium/release/190
Browse files Browse the repository at this point in the history
Release 190 (for v200220)
  • Loading branch information
U-lis authored Aug 27, 2024
2 parents cfd25c7 + b84c8b8 commit 327c786
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 42 deletions.
12 changes: 9 additions & 3 deletions NineChronicles.Headless.Executable/Commands/ReplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,16 @@ public int RemoteTx(
.Select(ToAction)
.Cast<IAction>()
.ToImmutableList();
var blockIndex = transactionResult.BlockIndex ?? 0;
var blockProtocolVersion = (int)protocolVersion;
var preEvaluationHash = HashDigest<SHA256>.FromString(preEvaluationHashValue);
byte[] preEvaluationHashBytes = preEvaluationHash.ToByteArray();
int seed = ActionEvaluator.GenerateRandomSeed(preEvaluationHashBytes, transaction.Signature, 0);
_console.Out.WriteLine($"BlockIndex: {blockIndex}, BlockProtocolVersion: {blockProtocolVersion}, Miner: {miner}, PreviousState: {previousStateRootHash}, RandomSeed: {seed}, Signer: {transaction.Signer}, TxId: {transactionId}");
var actionEvaluations = EvaluateActions(
preEvaluationHash: HashDigest<SHA256>.FromString(preEvaluationHashValue),
blockIndex: transactionResult.BlockIndex ?? 0,
blockProtocolVersion: (int)protocolVersion,
preEvaluationHash: preEvaluationHash,
blockIndex: blockIndex,
blockProtocolVersion: blockProtocolVersion,
txid: transaction.Id,
previousStates: previousStates,
miner: miner,
Expand Down
5 changes: 4 additions & 1 deletion NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ IActionLoader MakeSingleActionLoader()
.AddSource("Lib9c.Action.HackAndSlash")
.AddSource("Libplanet.Action.State")
.AddSource("Libplanet.Blockchain.BlockChainStates")
//.AddAspNetCoreInstrumentation()
.AddSource("NineChronicles.Headless.GraphTypes.StandaloneQuery")
.AddSource("NineChronicles.Headless.GraphTypes.StandaloneMutation")
.AddSource("NineChronicles.Headless.GraphTypes.TransactionHeadlessQuery")
.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddProcessor(new Pyroscope.OpenTelemetry.PyroscopeSpanProcessor())
.AddOtlpExporter(opt =>
Expand Down
1 change: 0 additions & 1 deletion NineChronicles.Headless/GraphQLService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ public void ConfigureServices(IServiceCollection services)
.AddDataLoader()
.AddGraphTypes(typeof(StandaloneSchema))
.AddLibplanetExplorer()
.AddUserContextBuilder<UserContextBuilder>()
.AddGraphQLAuthorization(
options =>
{
Expand Down
6 changes: 6 additions & 0 deletions NineChronicles.Headless/GraphTypes/StandaloneMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
using Nekoyume.Model.State;
using Serilog;
using System;
using System.Diagnostics;
using Nekoyume.Module;

namespace NineChronicles.Headless.GraphTypes
{
public class StandaloneMutation : ObjectGraphType
{
private static readonly ActivitySource ActivitySource = new ActivitySource("NineChronicles.Headless.GraphTypes.StandaloneMutation");

public StandaloneMutation(
StandaloneContext standaloneContext,
NineChroniclesNodeService nodeService,
Expand Down Expand Up @@ -60,6 +63,7 @@ IConfiguration configuration
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("stageTx");
try
{
byte[] bytes = Convert.FromBase64String(context.GetArgument<string>("payload"));
Expand Down Expand Up @@ -179,6 +183,7 @@ IConfiguration configuration
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("transfer");
if (!(standaloneContext.NineChroniclesNodeService is { } service))
{
throw new InvalidOperationException($"{nameof(NineChroniclesNodeService)} is null.");
Expand Down Expand Up @@ -289,6 +294,7 @@ IConfiguration configuration
{
try
{
using var activity = ActivitySource.StartActivity("stageTransaction");
byte[] bytes = ByteUtil.ParseHex(context.GetArgument<string>("payload"));
Transaction tx = Transaction.Deserialize(bytes);
NineChroniclesNodeService? service = standaloneContext.NineChroniclesNodeService;
Expand Down
69 changes: 62 additions & 7 deletions NineChronicles.Headless/GraphTypes/StandaloneQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Bencodex;
using Bencodex.Types;
Expand Down Expand Up @@ -34,6 +35,8 @@ namespace NineChronicles.Headless.GraphTypes
{
public class StandaloneQuery : ObjectGraphType
{
private static readonly ActivitySource ActivitySource = new ActivitySource("NineChronicles.Headless.GraphTypes.StandaloneQuery");

public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration configuration, ActionEvaluationPublisher publisher, StateMemoryCache stateMemoryCache)
{
bool useSecretToken = configuration[GraphQLService.SecretTokenKey] is { };
Expand All @@ -55,13 +58,15 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
}),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("stateQuery");
BlockHash blockHash = (context.GetArgument<byte[]?>("hash"), context.GetArgument<long?>("index")) switch
{
({ } bytes, null) => new BlockHash(bytes),
(null, { } index) => standaloneContext.BlockChain[index].Hash,
(not null, not null) => throw new ArgumentException("Only one of 'hash' and 'index' must be given."),
(null, null) => standaloneContext.BlockChain.Tip.Hash,
};
activity?.AddTag("BlockHash", blockHash.ToString());

if (!(standaloneContext.BlockChain is { } chain))
{
Expand Down Expand Up @@ -102,6 +107,7 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("diffs");
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
Expand Down Expand Up @@ -194,40 +200,51 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("accountDiffs");
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
$"{nameof(StandaloneContext)}.{nameof(StandaloneContext.BlockChain)} was not set yet!"
);
}

var baseIndex = context.GetArgument<long>("baseIndex");
var changedIndex = context.GetArgument<long>("changedIndex");
var accountAddress = context.GetArgument<Address>("accountAddress");

var blockInterval = Math.Abs(changedIndex - baseIndex);
if (blockInterval >= 30 || blockInterval == 0)
{
throw new ExecutionError(
"Interval between baseIndex and changedIndex should not be greater than 30 or zero"
);
}

var baseBlockStateRootHash = blockChain[baseIndex].StateRootHash.ToString();
var changedBlockStateRootHash = blockChain[changedIndex].StateRootHash.ToString();

var baseStateRootHash = HashDigest<SHA256>.FromString(baseBlockStateRootHash);
var targetStateRootHash = HashDigest<SHA256>.FromString(
changedBlockStateRootHash
);

var stateStore = standaloneContext.StateStore;
var baseTrieModel = stateStore.GetStateRoot(baseStateRootHash);
var targetTrieModel = stateStore.GetStateRoot(targetStateRootHash);

var accountKey = new KeyBytes(ByteUtil.Hex(accountAddress.ByteArray));

Binary GetAccountState(ITrie model, KeyBytes key)
{
return model.Get(key) is Binary state ? state : throw new Exception($"Account state not found.");
}

var baseAccountState = GetAccountState(baseTrieModel, accountKey);
var targetAccountState = GetAccountState(targetTrieModel, accountKey);

var baseSubTrieModel = stateStore.GetStateRoot(new HashDigest<SHA256>(baseAccountState));
var targetSubTrieModel = stateStore.GetStateRoot(new HashDigest<SHA256>(targetAccountState));

var subDiff = baseSubTrieModel
.Diff(targetSubTrieModel)
.Select(diff => new StateDiffType.Value(
Expand All @@ -249,6 +266,7 @@ Binary GetAccountState(ITrie model, KeyBytes key)
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("state");
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
Expand All @@ -266,6 +284,10 @@ Binary GetAccountState(ITrie model, KeyBytes key)
var accountAddress = context.GetArgument<Address>("accountAddress");
var address = context.GetArgument<Address>("address");

activity?
.AddTag("BlockHash", blockHash.ToString())
.AddTag("Address", address.ToString());

var state = blockChain
.GetWorldState(blockHash)
.GetAccountState(accountAddress)
Expand Down Expand Up @@ -293,8 +315,11 @@ Binary GetAccountState(ITrie model, KeyBytes key)
}
), resolve: context =>
{
using var activity = ActivitySource.StartActivity("transferNCGHistories");
BlockHash blockHash = new BlockHash(context.GetArgument<byte[]>("blockHash"));

activity?.AddTag("BlockHash", blockHash.ToString());

if (!(standaloneContext.Store is { } store))
{
throw new InvalidOperationException();
Expand Down Expand Up @@ -349,8 +374,11 @@ Binary GetAccountState(ITrie model, KeyBytes key)

Field<NonNullGraphType<NodeStatusType>>(
name: "nodeStatus",
resolve: _ => new NodeStatusType(standaloneContext)
);
resolve: _ =>
{
using var activity = ActivitySource.StartActivity("nodeStatus");
return new NodeStatusType(standaloneContext);
});

Field<NonNullGraphType<Libplanet.Explorer.Queries.ExplorerQuery>>(
name: "chainQuery",
Expand Down Expand Up @@ -383,6 +411,7 @@ Binary GetAccountState(ITrie model, KeyBytes key)
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("goldBalance");
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
Expand All @@ -398,6 +427,10 @@ Binary GetAccountState(ITrie model, KeyBytes key)
(Dictionary)blockChain.GetWorldState(blockHash).GetLegacyState(GoldCurrencyState.Address)
).Currency;

activity?
.AddTag("BlockHash", blockHash.ToString())
.AddTag("Address", address.ToString());

return blockChain.GetWorldState(blockHash).GetBalance(
address,
currency
Expand All @@ -414,13 +447,15 @@ Binary GetAccountState(ITrie model, KeyBytes key)
),
resolve: context =>
{
using var activity = ActivitySource.StartActivity("nextTxNonce");
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
$"{nameof(StandaloneContext)}.{nameof(StandaloneContext.BlockChain)} was not set yet!");
}

Address address = context.GetArgument<Address>("address");
activity?.AddTag("Address", address.ToString());
return blockChain.GetNextTxNonce(address);
}
);
Expand Down Expand Up @@ -473,6 +508,7 @@ Binary GetAccountState(ITrie model, KeyBytes key)
description: "Get monster collection status by address.",
resolve: context =>
{
using var activity = ActivitySource.StartActivity(nameof(MonsterCollectionStatus));
if (!(standaloneContext.BlockChain is BlockChain blockChain))
{
throw new ExecutionError(
Expand All @@ -498,6 +534,10 @@ Binary GetAccountState(ITrie model, KeyBytes key)


BlockHash offset = blockChain.Tip.Hash;

activity?
.AddTag("BlockHash", offset.ToString())
.AddTag("Address", address.ToString());
IWorldState worldState = blockChain.GetWorldState(offset);
#pragma warning disable S3247
if (worldState.GetAgentState(agentAddress) is { } agentState)
Expand Down Expand Up @@ -537,8 +577,11 @@ Binary GetAccountState(ITrie model, KeyBytes key)
Field<NonNullGraphType<TransactionHeadlessQuery>>(
name: "transaction",
description: "Query for transaction.",
resolve: context => new TransactionHeadlessQuery(standaloneContext)
);
resolve: context =>
{
using var activity = ActivitySource.StartActivity("transaction");
return new TransactionHeadlessQuery(standaloneContext);
});

Field<NonNullGraphType<BooleanGraphType>>(
name: "activated",
Expand Down Expand Up @@ -622,7 +665,11 @@ Binary GetAccountState(ITrie model, KeyBytes key)
Field<NonNullGraphType<ActionQuery>>(
name: "actionQuery",
description: "Query to create action transaction.",
resolve: context => new ActionQuery(standaloneContext));
resolve: context =>
{
using var activity = ActivitySource.StartActivity("actionQuery");
return new ActionQuery(standaloneContext);
});

Field<NonNullGraphType<ActionTxQuery>>(
name: "actionTxQuery",
Expand All @@ -648,12 +695,20 @@ Binary GetAccountState(ITrie model, KeyBytes key)
DefaultValue = 1 * Currencies.Mead
}
),
resolve: context => new ActionTxQuery(standaloneContext));
resolve: context =>
{
using var activity = ActivitySource.StartActivity("actionTxQuery");
return new ActionTxQuery(standaloneContext);
});

Field<NonNullGraphType<AddressQuery>>(
name: "addressQuery",
description: "Query to get derived address.",
resolve: context => new AddressQuery(standaloneContext));
resolve: context =>
{
using var activity = ActivitySource.StartActivity("addressQuery");
return new AddressQuery(standaloneContext);
});
}
}
}
Loading

0 comments on commit 327c786

Please sign in to comment.