Skip to content

feat: wrapped block validator token tracing for post october 2023 with saved states#2265

Open
rockysingh wants to merge 13 commits intohiero-ledger:mainfrom
rockysingh:2212-wrapped-block-validator-token-tracing
Open

feat: wrapped block validator token tracing for post october 2023 with saved states#2265
rockysingh wants to merge 13 commits intohiero-ledger:mainfrom
rockysingh:2212-wrapped-block-validator-token-tracing

Conversation

@rockysingh
Copy link
Contributor

@rockysingh rockysingh commented Feb 25, 2026

Summary

  • Add token balance tracing to the wrapped block validator, extending beyond HBAR-only supply checks to per-account, per-token balance verification against signed protobuf balance files from GCP
  • Introduce FetchBalanceCheckpointsCommand (blocks fetchBalanceCheckpoints) to download and bundle weekly/monthly balance snapshots from the mainnet GCP bucket into length-prefixed protobuf checkpoint files
  • Add BalanceCheckpointValidator and BalanceProtobufValidator to compare computed running account/token balances against checkpoint snapshots at configurable intervals (--balance-check-interval-days, default 30)
  • Add BalanceFileBucket utility for downloading midnight-targeted .pb.gz balance files from GCP with signature verification support
  • Add RunningAccountsState to track per-account HBAR and token balances across blocks, processing amendments (crypto transfers, token mints/burns/wipes/transfers)
  • Add bundled checkpoint auto-loading (accountBalances_91019204.pb.gz) so validate-wrapped works out of the box without a separate fetch step
  • Add new CLI options to validate-wrapped: --validate-balances, --balance-checkpoints, --custom-balances-dir, --balance-check-interval-days
  • Add comprehensive unit tests for BalanceCheckpointValidator, BalanceCheckpointsLoader, BalanceProtobufValidator, and BalanceFileBucket
  • Update documentation for fetchBalanceCheckpoints and validate-wrapped commands

Notes

The bundled accountBalances_91019204.pb.gz checkpoint was generated using the standalone [hiero-state-to-balance-file]project, which extracts account/token balances from a consensus node
saved state at block 91,019,204. This file provides a known-good starting point for balance validation. If the saved-state extraction tool or its protobuf format diverges in the future, this checkpoint may need to be regenerated. There is a potential diverge issue we need to raise. The "diverge issue" is that this is a separate standalone project — not part of the hiero-block-node repo — and the generated .pb.gz file was copied over. If that project's protobuf format, extraction logic, or Hiero dependency version changes, the bundled file could become stale or incompatible. Therefore, we will need to generate other files for any diverge.

Test plan

  • Verify blocks validate-wrapped runs with default balance validation enabled (uses bundled checkpoint)
  • Verify blocks fetchBalanceCheckpoints downloads and bundles balance files from GCP
  • Verify --no-validate-balances disables balance checking
  • Verify --balance-check-interval-days controls checkpoint frequency
  • Verify --custom-balances-dir loads balance files from a local directory
  • Run unit tests: BalanceCheckpointValidatorTest, BalanceCheckpointsLoaderTest, BalanceProtobufValidatorTest, BalanceFileBucketTest

Also, rebased using this pr: #2184 as it was needed to work on this issue etc.

This closes: #2173 and #2212

@rockysingh rockysingh force-pushed the 2212-wrapped-block-validator-token-tracing branch from eab9ad7 to 5122146 Compare February 25, 2026 17:27
@rockysingh rockysingh changed the title 2212 wrapped block validator token tracing feat: wrapped block validator token tracing for post october 2023 with saved states Feb 25, 2026
@rockysingh rockysingh self-assigned this Feb 25, 2026
@rockysingh rockysingh added the Block Node Tools Additional tools related to, but not part of, the Block Node label Feb 25, 2026
@rockysingh rockysingh added this to the 0.29.0 milestone Feb 25, 2026
@rockysingh rockysingh force-pushed the 2212-wrapped-block-validator-token-tracing branch from 5122146 to f4cb48b Compare February 25, 2026 17:57
@rockysingh rockysingh marked this pull request as ready for review February 25, 2026 22:11
@rockysingh rockysingh requested review from a team as code owners February 25, 2026 22:11
@jsync-swirlds
Copy link
Contributor

You're pushing a large number of large binary files in this PR that appear to be cached data, not code.

  1. Is that intentional
  2. Can we please not do that, large binary files play havoc with git and create permanent problems for every clone of the repo (because they stay in history, even if later removed).

@rockysingh
Copy link
Contributor Author

rockysingh commented Feb 26, 2026

You're pushing a large number of large binary files in this PR that appear to be cached data, not code.

  1. Is that intentional
  2. Can we please not do that, large binary files play havoc with git and create permanent problems for every clone of the repo (because they stay in history, even if later removed).

That is not intentional. I had them removed. I guess I did not stage the changes correctly. I've removed them now.

   Add optional balance file validation that compares computed account
   balances against signed protobuf balance files from GCP mainnet bucket.
   This provides per-account verification in addition to the existing
   50 billion HBAR supply check.

   - Add BalanceFileBucket to download balance files from GCP
   - Add BalanceCsvValidator to compare balances at checkpoints
   - Add signature verification for balance files using address book
   - Update WrappedBlockValidator to process amendments in balance tracking
   - Add CLI options: --validate-balances, --balance-start-day,
     --balance-end-day, --address-book, --verify-signatures, --gcp-project

   Balance files are available from September 2019 through October 23, 2023

Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
    (default: 30 days/monthly) to control how often balance checkpoints
    are validated
  - Add setCheckIntervalDays() to BalanceCheckpointValidator with
    filtering logic based on ~20,000 blocks/day
  - Update README.md command tree with fetchBalanceCheckpoints and
    validate-wrapped commands
  - Add comprehensive documentation for fetchBalanceCheckpoints command
    including options, prerequisites, output format, and examples
  - Update validate-wrapped documentation with new balance validation
    options and clarify relationship between fetch and validation intervals

Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
  Address PR review comments:

  - Rename BalanceCsvValidator to BalanceProtobufValidator (downloads .pb.gz files)
  - Change checkpoint file format to length-prefixed protobufs:
    [blockNumber (8 bytes)][length (4 bytes)][raw protobuf bytes]
  - Add token balance support via BalanceProtobufParser.parseWithTokens()
  - Add direct midnight targeting in BalanceFileBucket.downloadMidnightBalanceFile()
  - Update mergeRecordStreamItems to handle replacements (same timestamp = replace)
  - Remove unused BalanceProtobufParser.parseAndWrite() method

  Note: Existing .zstd checkpoint files need regeneration with new format.

Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
Remove validateCheckpoint(HBAR-only) overloads and parseProtobufBalances
method that are no longer called after token balance support was added.

Signed-off-by: Rocky Thind <[email protected]>
jasperpotts and others added 3 commits February 25, 2026 18:21
…ests

  Add loadFromGzippedStream to BalanceCheckpointsLoader and
  BalanceCheckpointValidator to support loading individual gzipped protobuf
  balance files. Extend ValidateWrappedBlocksCommand to auto-load bundled
  checkpoint resources (accountBalances_91019204.pb.gz) at startup. Add
  unit tests for both loader and validator covering HBAR/token balance
  loading, checkpoint querying, interval filtering, range checks, and
  validation pass/fail paths.

Signed-off-by: Rocky Thind <[email protected]>
Signed-off-by: Rocky Thind <[email protected]>
@rockysingh rockysingh force-pushed the 2212-wrapped-block-validator-token-tracing branch from 7221f30 to 0dc0943 Compare February 26, 2026 02:23
Copy link
Contributor

@jsync-swirlds jsync-swirlds left a comment

Choose a reason for hiding this comment

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

Just a few more large (20-50M) files to double check if we really need them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same question.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is created using a command but it can be very flakey to run due to GCP but gets even worse as its at a weekly granularity. This is why I've pushed them to the repo as a source of truth.

Copy link
Contributor

@jsync-swirlds jsync-swirlds Feb 28, 2026

Choose a reason for hiding this comment

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

Could we store it separately and load/download as needed?
It is a lot of data to store in git, and that data gets copied to every clone forever.
I understand if it's actually needed, and we can perhaps fix later if necessary, but it would be nice to not store these binaries if we can reasonably avoid it.

@jsync-swirlds jsync-swirlds moved this from Todo to In Progress in Hiero CLI Feb 28, 2026
@codecov
Copy link

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@             Coverage Diff              @@
##               main    #2265      +/-   ##
============================================
- Coverage     80.96%   80.89%   -0.08%     
- Complexity     1445     1448       +3     
============================================
  Files           139      139              
  Lines          6730     6730              
  Branches        726      726              
============================================
- Hits           5449     5444       -5     
- Misses          962      964       +2     
- Partials        319      322       +3     

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

Block Node Tools Additional tools related to, but not part of, the Block Node

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Add balance checking to wrapped block validator

3 participants