Skip to content

Conversation

@ckeshava
Copy link
Contributor

@ckeshava ckeshava commented Aug 21, 2025

High Level Overview of Change

This PR aims to comprehensively track all the validated and missing ledgers, as recorded by the VHS. It also exposes suitable API endpoints to query this piece of information.

If you prefer the API endpoints (or) the DB schema differently for your use-cases, let me know.

The validated_ledgers table is pruned daily. I have not set up a pruning schedule for missing_ledgers table yet for debugging purposes. Presently, both these tables only track the mainnet for reducing the storage burden. If deemed necessary, this can be extended to other networks as well.

Context of Change

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Before / After

Here is an example of how I envision the usage of this PR changes:
Debugging: Inspect the missing-ledgers table. Note the ledger_index, previous_ledger and previous_received_at timestamps.
image

Use the information from the contextual rows in the validated_ledgers table to gather more information about the timestamp of the missing ledger. Use this timestamp to correlate events with the logs of vhs-connections pod. (Did the pod restart? Was there a prior unhandled error? etc)

Source of Truth: The missing_ledgers and validated_ledgers provide a comprehensive store of the VHS's data. It can be used to pinpoint erroneous behavior.

If one does not have access to the DB, this can be accomplished with the exposed API endpoints as well.

Test Plan

Unit tests have been added for the DB tasks pertaining to validated_ledgers table. If we are planning to retain the missing_ledgers table in the production VHS instance, suitable tests can be added in the future.

Future Tasks

In a future PR, I'd like to track all the incoming validations received by the VHS.

@ckeshava ckeshava requested a review from Patel-Raj11 August 21, 2025 23:39
@ckeshava ckeshava changed the title Track ledgers Store validated ledgers inside a persistent DB Aug 21, 2025
| `connected` |Boolean denoting websocket connection status. |
| `status_update_time` |Time when the connected column was updated. |

### `validated_ledgers`
Copy link
Collaborator

@Patel-Raj11 Patel-Raj11 Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets update this after all comments are addressed.

* @param networkName - The name of the network.
* @returns A promise that resolves when the insertion is complete.
*/
export async function insertValidations(
Copy link
Collaborator

@Patel-Raj11 Patel-Raj11 Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If there is exactly one entry we update it. If there are 0 entries we log a message that you are already logging and insert the row as well (to account for validation that we receive for which we never had a ledger message). Last else clause should never occur but good to have it for logging purposes.

  2. try catch is required here, otherwise DB operation failure will fail the actual source code flow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will review updated test cases once, we conclude on these changes and other changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are 0 entries we log a message that you are already logging and insert the row as well (to account for validation that we receive for which we never had a ledger message).

Regarding this case, if the VHS has not recieved the ledgerClosed message for a specific ledger, then I cannot insert the row. I do not have the necessary fields of the StreamLedger interface to be able to fill out these values at this stage. Executing a log.error() appears to be the best course of action here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed point-2 in the commit: 84740c0

@ckeshava ckeshava requested a review from Patel-Raj11 September 4, 2025 16:02
ledger_hashes.push(current_ledger.ledger_hash)

if (networks) {
await insertValidatedLedger(networks, current_ledger)
Copy link
Collaborator

@Patel-Raj11 Patel-Raj11 Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are not de-duplicating when hitting database. Not sure if this will lead to deadlock.
try catch in insertValidatedLedger would prevent node.js terminating, but not the deadlock if it ever occurs.

If we want to keep this as it is, I would suggest deploying this branch on staging/dev and then deploying it in production if it works well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

On line 33, I'm checking against for duplicates against the primary-key of the db. Given that these 3 columns make the primary-key, I expect this operation to be optimized by the DB.

Besides, I'm not executing any other instruction in that method. Hence, there is no possibility of ungraceful-closure of the db operation (or) leakage of DB-connections in this method.

Sure, we can observe in dev/stg environment for a few days.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking the PR review: I would recommend testing it in staging/dev for a few days.

@ckeshava ckeshava requested a review from Patel-Raj11 September 4, 2025 22:23
@Patel-Raj11
Copy link
Collaborator

@ckeshava This branch ckeshava:trackLedgers looks in good state to be deployed in staging/dev for testing.

Patel-Raj11
Patel-Raj11 previously approved these changes Sep 4, 2025
@ckeshava
Copy link
Contributor Author

ckeshava commented Sep 5, 2025

@ckeshava This branch ckeshava:trackLedgers looks in good state to be deployed in staging/dev for testing.

Update: I have deployed this branch on dev for observation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants