Skip to content

Commit

Permalink
test: IAccountDiff test
Browse files Browse the repository at this point in the history
  • Loading branch information
riemannulus committed Sep 22, 2023
1 parent 991a0f1 commit 650c176
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,7 @@ public ITrie Trie

public BlockHash? BlockHash
{
get;
set;
get => default;
}

public IValue? GetState(Address address)
Expand Down
42 changes: 36 additions & 6 deletions NineChronicles.Headless/ActionEvaluationPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.IO;
using System.IO.Compression;
Expand Down Expand Up @@ -103,7 +104,7 @@ public async Task AddClient(Address clientAddress)
};

GrpcChannel channel = GrpcChannel.ForAddress($"http://{_host}:{_port}", options);
Client client = await Client.CreateAsync(channel, clientAddress, _context, _sentryTraces);
Client client = await Client.CreateAsync(channel, _blockChainStates, clientAddress, _context, _sentryTraces);
if (_clients.TryAdd(clientAddress, client))
{
if (clientAddress == default)
Expand Down Expand Up @@ -223,6 +224,7 @@ private async void RemoveClient(string clientAddressHex)
private sealed class Client : IAsyncDisposable
{
private readonly IActionEvaluationHub _hub;
private readonly IBlockChainStates _blockChainStates;
private readonly RpcContext _context;
private readonly Address _clientAddress;

Expand All @@ -240,11 +242,13 @@ private sealed class Client : IAsyncDisposable

private Client(
IActionEvaluationHub hub,
IBlockChainStates blockChainStates,
Address clientAddress,
RpcContext context,
ConcurrentDictionary<string, Sentry.ITransaction> sentryTraces)
{
_hub = hub;
_blockChainStates = blockChainStates;
_clientAddress = clientAddress;
_context = context;
TargetAddresses = ImmutableHashSet<Address>.Empty;
Expand All @@ -253,6 +257,7 @@ private Client(

public static async Task<Client> CreateAsync(
GrpcChannel channel,
IBlockChainStates blockChainStates,
Address clientAddress,
RpcContext context,
ConcurrentDictionary<string, Sentry.ITransaction> sentryTraces)
Expand All @@ -263,7 +268,7 @@ public static async Task<Client> CreateAsync(
);
await hub.JoinAsync(clientAddress.ToHex());

return new Client(hub, clientAddress, context, sentryTraces);
return new Client(hub, blockChainStates, clientAddress, context, sentryTraces);
}

public void Subscribe(
Expand Down Expand Up @@ -302,10 +307,20 @@ await _hub.BroadcastRenderBlockAsync(
{
try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
ActionBase? pa = ev.Action is RewardGold
? null
: ev.Action;
var extra = new Dictionary<string, IValue>();
IAccountState output = _blockChainStates.GetAccountState(ev.OutputState);
IAccountState input = _blockChainStates.GetAccountState(ev.PreviousState);
AccountDiff diff = AccountDiff.Create(input, output);
if (!TargetAddresses.Any(diff.StateDiffs.Keys.Append(ev.Signer).Contains))
{
return;
}
var encodeElapsedMilliseconds = stopwatch.ElapsedMilliseconds;

var eval = new NCActionEvaluation(pa, ev.Signer, ev.BlockIndex, ev.OutputState, ev.Exception, ev.PreviousState, ev.RandomSeed, extra);
var encoded = MessagePackSerializer.Serialize(eval);
Expand All @@ -325,6 +340,21 @@ await _hub.BroadcastRenderBlockAsync(
);

await _hub.BroadcastRenderAsync(compressed);
stopwatch.Stop();

var broadcastElapsedMilliseconds = stopwatch.ElapsedMilliseconds - encodeElapsedMilliseconds;
Log
.ForContext("tag", "Metric")
.ForContext("subtag", "ActionEvaluationPublisherElapse")
.Information(
"[{ClientAddress}], #{BlockIndex}, {Action}," +
" {EncodeElapsedMilliseconds}, {BroadcastElapsedMilliseconds}, {TotalElapsedMilliseconds}",
_clientAddress,
ev.BlockIndex,
ev.Action.GetType(),
encodeElapsedMilliseconds,
broadcastElapsedMilliseconds,
encodeElapsedMilliseconds + broadcastElapsedMilliseconds);
}
catch (SerializationException se)
{
Expand Down Expand Up @@ -411,14 +441,14 @@ private bool ContainsAddressToBroadcast(ActionEvaluation<ActionBase> ev)

private bool ContainsAddressToBroadcastLocal(ActionEvaluation<ActionBase> ev)
{
var updatedAddresses = ev.OutputState.Delta.UpdatedAddresses;
return _context.AddressesToSubscribe.Any(updatedAddresses.Add(ev.Signer).Contains);
int t =ev.RandomSeed;
return true;
}

private bool ContainsAddressToBroadcastRemoteClient(ActionEvaluation<ActionBase> ev)
{
var updatedAddresses = ev.OutputState.Delta.UpdatedAddresses;
return TargetAddresses.Any(updatedAddresses.Add(ev.Signer).Contains);
int r = ev.RandomSeed;
return true;
}
}
}
Expand Down

0 comments on commit 650c176

Please sign in to comment.