Skip to content

Commit

Permalink
Merge branch 'cbw-1248/implementation-smart-contract-aggregate' into …
Browse files Browse the repository at this point in the history
…cbw-1249/add-observability
  • Loading branch information
schwartz-concordium committed Sep 4, 2023
2 parents bc4440a + f143293 commit 4ec857d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public void Configure(EntityTypeBuilder<ModuleReferenceContractLinkEvent> builde
.HasColumnName("contract_address_sub_index");
builder.Property(x => x.Source)
.HasColumnName("source");
builder.Property(x => x.LinkAction)
.HasColumnName("link_action");
builder.Property(x => x.CreatedAt)
.HasColumnName("created_at");
}
Expand Down
27 changes: 20 additions & 7 deletions backend/Application/Aggregates/Contract/ContractAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ internal async Task NodeImportJob(IContractNodeClient client, CancellationToken
{
try
{
var lastHeight = await GetNextBlockHeight();
var nextBlockHeight = await GetNextBlockHeight();
while (!token.IsCancellationRequested)
{
var consensusInfo = await client.GetConsensusInfoAsync(token);
var newLastHeight = consensusInfo.LastFinalizedBlockHeight;
if (lastHeight == newLastHeight)
var lastFinalizedHeight = consensusInfo.LastFinalizedBlockHeight;
if (nextBlockHeight > lastFinalizedHeight)
{
await Task.Delay(_options.DelayBetweenRetries, token);
continue;
}

for (var height = lastHeight; height <= newLastHeight; height++)
for (var height = nextBlockHeight; height <= lastFinalizedHeight; height++)
{
using var durationMetric = new ContractMetrics.DurationMetric(ImportSource.NodeImport);
try
Expand All @@ -68,7 +68,7 @@ internal async Task NodeImportJob(IContractNodeClient client, CancellationToken
throw;
}
}
lastHeight = newLastHeight;
nextBlockHeight = lastFinalizedHeight + 1;
retryCount = 0;
}
break;
Expand Down Expand Up @@ -198,7 +198,8 @@ await repository
eventIndex,
contractInitialized.ModuleRef,
contractInitialized.ContractAddress,
source
source,
ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Added
));
break;
case ContractInterrupted contractInterrupted:
Expand Down Expand Up @@ -256,7 +257,19 @@ await repository
eventIndex,
contractUpgraded.To,
contractUpgraded.ContractAddress,
source
source,
ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Added
));
await repository
.AddAsync(new ModuleReferenceContractLinkEvent(
blockHeight,
transactionHash,
transactionIndex,
eventIndex,
contractUpgraded.From,
contractUpgraded.ContractAddress,
source,
ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Removed
));
break;
case Transferred transferred:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public sealed class ModuleReferenceContractLinkEvent
public ulong ContractAddressIndex { get; init; }
public ulong ContractAddressSubIndex { get; init; }
public ImportSource Source { get; init; }
public ModuleReferenceContractLinkAction LinkAction { get; init; }
public DateTime CreatedAt { get; init; } = DateTime.UtcNow;

/// <summary>
/// Needed for EF Core
/// </summary>
Expand All @@ -35,7 +36,8 @@ public ModuleReferenceContractLinkEvent(
uint eventIndex,
string moduleReference,
ContractAddress contractAddress,
ImportSource source
ImportSource source,
ModuleReferenceContractLinkAction linkAction
)
{
BlockHeight = blockHeight;
Expand All @@ -46,5 +48,16 @@ ImportSource source
ContractAddressIndex = contractAddress.Index;
ContractAddressSubIndex = contractAddress.SubIndex;
Source = source;
LinkAction = linkAction;
}

/// <summary>
/// Identifies if the event add- or removes a link between a <see cref="Contract"/>
/// and <see cref="ModuleReferenceEvent"/>.
/// </summary>
public enum ModuleReferenceContractLinkAction
{
Added,
Removed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ create table graphql_module_reference_contract_link_events
module_reference text not null,
contract_address_index bigint not null,
contract_address_sub_index bigint not null,
source int not null,
source int not null,
link_action int not null,
created_at TIMESTAMPTZ not null,
PRIMARY KEY (
block_height,
Expand Down
16 changes: 12 additions & 4 deletions backend/Tests/Aggregates/Contract/ContractAggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public async Task GivenContractInitialization_WhenNodeImport_ThenStoreContractEv
var link = moduleReferenceContractLinkEvents[0];
link.ModuleReference.Should().Be(moduleTo);
link.ContractAddressIndex.Should().Be(contractIndex);
link.LinkAction.Should().Be(ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Added);

contracts.Count.Should().Be(1);
var contract = contracts[0];
Expand Down Expand Up @@ -144,10 +145,17 @@ public async Task GivenContractUpgraded_WhenNodeImport_ThenStoreEventAndModuleLi
contractUpgraded.From.Should().Be(moduleFrom);
contractUpgraded.To.Should().Be(moduleTo);

moduleReferenceContractLinkEvents.Count.Should().Be(1);
var link = moduleReferenceContractLinkEvents[0];
link.ModuleReference.Should().Be(moduleTo);
link.ContractAddressIndex.Should().Be(contractIndex);
moduleReferenceContractLinkEvents.Count.Should().Be(2);

var to = moduleReferenceContractLinkEvents[0];
to.ModuleReference.Should().Be(moduleTo);
to.LinkAction.Should().Be(ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Added);
to.ContractAddressIndex.Should().Be(contractIndex);

var from = moduleReferenceContractLinkEvents[1];
from.ModuleReference.Should().Be(moduleFrom);
from.LinkAction.Should().Be(ModuleReferenceContractLinkEvent.ModuleReferenceContractLinkAction.Removed);
from.ContractAddressIndex.Should().Be(contractIndex);
}

private static byte[] ArrayFilledWith(byte fill, ushort size)
Expand Down

0 comments on commit 4ec857d

Please sign in to comment.