Skip to content

Commit

Permalink
Add firehose instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gldeng committed Jan 8, 2025
1 parent 17c523c commit 397cb33
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/AElf.Kernel.Core/Blockchain/Events/BlockAttachedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AElf.Kernel.Blockchain.Events;

public class BlockAttachedEvent
{
public long Height { get; set; }
public Hash Hash { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Threading.Tasks;
using AElf.Kernel.Blockchain.Application;
using AElf.Kernel.Blockchain.Domain;
using AElf.Kernel.Blockchain.Events;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Local;

namespace AElf.Kernel.SmartContractExecution.Application;

Expand All @@ -21,6 +23,7 @@ public class BlockAttachService : IBlockAttachService, ITransientDependency
private readonly IBlockExecutionResultProcessingService _blockExecutionResultProcessingService;
private readonly IChainBlockLinkService _chainBlockLinkService;

public ILocalEventBus LocalEventBus { get; set; }
public BlockAttachService(IBlockchainService blockchainService,
IBlockchainExecutingService blockchainExecutingService,
IChainBlockLinkService chainBlockLinkService,
Expand All @@ -30,6 +33,7 @@ public BlockAttachService(IBlockchainService blockchainService,
_blockchainExecutingService = blockchainExecutingService;
_chainBlockLinkService = chainBlockLinkService;
_blockExecutionResultProcessingService = blockExecutionResultProcessingService;
LocalEventBus = NullLocalEventBus.Instance;

Logger = NullLogger<BlockAttachService>.Instance;
}
Expand Down Expand Up @@ -65,6 +69,7 @@ public async Task AttachBlockAsync(Block block)
finally
{
await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, executionResult);
await LocalEventBus.PublishAsync(new BlockAttachedEvent() { });
}
}
}
13 changes: 13 additions & 0 deletions src/AElf.WebApp.Firehose/AElf.WebApp.Firehose.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AElf.Kernel.Core\AElf.Kernel.Core.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions src/AElf.WebApp.Firehose/BlockAcceptedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using AElf.Kernel;
using AElf.Kernel.Blockchain.Application;
using AElf.Kernel.Blockchain.Events;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Google.Protobuf; // Ensure you have this using directive for protobuf serialization

namespace AElf.WebApp.Firehose;

public class BlockAcceptedEventHandler : ILocalEventHandler<BlockAcceptedEvent>, ILocalEventHandler<BlockAttachedEvent>,
ITransientDependency
{
private BlockAcceptedEvent? _acceptedEvent = null;
private readonly IBlockchainService _blockchainService;

public BlockAcceptedEventHandler(IBlockchainService blockchainService)
{
_blockchainService = blockchainService;
Console.WriteLine($"FIRE INIT 3.0 {Block.Descriptor.FullName}");
}

public Task HandleEventAsync(BlockAcceptedEvent? eventData)
{
_acceptedEvent = eventData;
return Task.CompletedTask;
}

public async Task HandleEventAsync(BlockAttachedEvent eventData)
{
if (_acceptedEvent == null)
{
Console.WriteLine("FIRE EXCEPTION NO_ACCEPTED_EVENT");
return;
}

if (
// ReSharper disable once ComplexConditionExpression
_acceptedEvent.Block.Header.Height != eventData.Height ||
_acceptedEvent.Block.Header.GetHash() != eventData.Hash
)
{
_acceptedEvent = null;
Console.WriteLine("FIRE EXCEPTION DISCREPANCY");
return;
}

var blockPayloadBase64 = Convert.ToBase64String(_acceptedEvent.Block.ToByteArray());

// Convert DateTime to Unix time in nanoseconds
var unixTimeNanos = (
_acceptedEvent.Block.Header.Time.ToDateTime() -
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
).TotalNanoseconds;
var chain = await _blockchainService.GetChainAsync();

var blockLine = string.Format(
"FIRE BLOCK {0} {1} {2} {3} {4} {5} {6}",
_acceptedEvent.Block.Height,
_acceptedEvent.Block.GetHash().ToHex(),
_acceptedEvent.Block.Height - 1,
_acceptedEvent.Block.Header.PreviousBlockHash.ToHex(),
chain.LastIrreversibleBlockHeight,
(long)unixTimeNanos, // Cast to long for formatting
blockPayloadBase64
);
Console.WriteLine(blockLine);
}
}

0 comments on commit 397cb33

Please sign in to comment.