diff --git a/CHANGELOG.md b/CHANGELOG.md index e6932bc62..380d10b2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.1.1 - Babylon Release Date: _unreleased_ -TBA +- Fixed `epoch [+ round]` based ledger state lookups. ## 1.1.0 - Babylon Release Date: _unreleased_ diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/DbQueryExtensions.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/DbQueryExtensions.cs index c616ad08a..8dbdc4b04 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/DbQueryExtensions.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/DbQueryExtensions.cs @@ -162,37 +162,18 @@ public static IQueryable GetFirstLedgerTransactionAfterTimest } /// - /// Returns most recently committed ledger transaction at or before given epoch and round. + /// Returns ledger transaction committed at given epoch and round. /// /// /// A LedgerTransaction row contains large blobs, so you must SELECT the fields you need after using this, and not pull down the whole /// ledger transaction row, to avoid possible performance issues. /// - public static IQueryable GetLatestLedgerTransactionAtEpochRound(this TDbContext dbContext, long epoch, long round) + public static IQueryable GetLedgerTransactionAtEpochAndRound(this TDbContext dbContext, long epoch, long round) where TDbContext : CommonDbContext { return dbContext .LedgerTransactions - .Where(lt => lt.Epoch == epoch && lt.RoundInEpoch >= round && lt.IndexInRound == 0) - .OrderByDescending(lt => lt.StateVersion) - .Take(1) - .AnnotateMetricName(); - } - - /// - /// Returns the first committed ledger transaction at or after given epoch and round. - /// - /// - /// A LedgerTransaction row contains large blobs, so you must SELECT the fields you need after using this, and not pull down the whole - /// ledger transaction row, to avoid possible performance issues. - /// - public static IQueryable GetFirstLedgerTransactionAtEpochRound(this TDbContext dbContext, long epoch, long round) - where TDbContext : CommonDbContext - { - return dbContext - .LedgerTransactions - .Where(lt => lt.Epoch >= epoch && lt.RoundInEpoch >= round && lt.IndexInRound == 0) - .OrderBy(lt => lt.StateVersion) + .Where(lt => lt.Epoch == epoch && lt.RoundInEpoch == round && lt.IndexInRound == 0) .Take(1) .AnnotateMetricName(); } diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/LedgerStateQuerier.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/LedgerStateQuerier.cs index 9b816c126..e7ab18190 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/LedgerStateQuerier.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/LedgerStateQuerier.cs @@ -188,7 +188,7 @@ public LedgerStateQuerier( } else if (fromLedgerStateIdentifier?.HasEpoch() == true) { - ledgerStateReport = await GetLedgerStateAfterEpochAndRound(fromLedgerStateIdentifier.Epoch.Value, fromLedgerStateIdentifier.Round ?? 0, token); + ledgerStateReport = await GetLedgerStateAtEpochAndRound(fromLedgerStateIdentifier.Epoch.Value, fromLedgerStateIdentifier.Round ?? 1, token); } return ledgerStateReport?.LedgerState; @@ -300,7 +300,7 @@ private async Task GetLedgerState(GatewayModel.LedgerStateSel } else if (at?.HasEpoch() == true) { - result = await GetLedgerStateAtEpochAndRound(at.Epoch.Value, at.Round ?? 0, token); + result = await GetLedgerStateAtEpochAndRound(at.Epoch.Value, at.Round ?? 1, token); } else { @@ -372,23 +372,11 @@ private async Task GetLedgerStateAfterTimestamp(DateTime time private async Task GetLedgerStateAtEpochAndRound(long epoch, long round, CancellationToken token) { - var ledgerState = await SelectLedgerStateFromQuery(_dbContext.GetLatestLedgerTransactionAtEpochRound(epoch, round), false, token); + var ledgerState = await SelectLedgerStateFromQuery(_dbContext.GetLedgerTransactionAtEpochAndRound(epoch, round), false, token); if (ledgerState == null) { - throw InvalidRequestException.FromOtherError($"Epoch {epoch} is beyond the end of the known ledger"); - } - - return ledgerState; - } - - private async Task GetLedgerStateAfterEpochAndRound(long epoch, long round, CancellationToken token) - { - var ledgerState = await SelectLedgerStateFromQuery(_dbContext.GetFirstLedgerTransactionAtEpochRound(epoch, round), false, token); - - if (ledgerState == null) - { - throw InvalidRequestException.FromOtherError($"Epoch {epoch} round {round} is beyond the end of the known ledger"); + throw InvalidRequestException.FromOtherError($"Epoch {epoch} round {round} is beyond the end of the known ledger or invalid"); } return ledgerState;