Skip to content

Add converting ledger with test #17428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged

Conversation

glyh
Copy link
Member

@glyh glyh commented Jun 17, 2025

Continuation of #17379

Status: waiting for next round of review.

@glyh glyh requested a review from a team as a code owner June 17, 2025 09:41
if Unstable_db.num_accounts db2 = 0 then
Or_error.return @@ Converting_ledger.create_with_migration db1 db2
else if not (Db.num_accounts db1 = Unstable_db.num_accounts db2) then (
(* TODO: ensure 2 dbs have exact same accounts *)
Copy link
Member Author

Choose a reason for hiding this comment

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

Noted

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, we have test coverage for this so it's not that fatal, though

Copy link
Member

Choose a reason for hiding this comment

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

To respond to #17379 (comment) here - yes, this is insufficient as a desync check. A converting merkle tree is two databases and we write to them separately, so it should be possible for the daemon to crash between database updates, which would cause a desync. I think you're right, that that is only detectable with a full equality check (up to account conversion) between the databases.

Copy link
Member

Choose a reason for hiding this comment

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

I think part of the slowness of migration is all the hashing that needs to be done to populate the database, not necessarily the actual account conversion. If the Unstable.of_stable conversion function is expected to be fast, then we might be able to do this integrity check right here. @georgeee do you know how heavy the real conversion function is likely to be?

Copy link
Member

Choose a reason for hiding this comment

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

I think part of the slowness of migration is all the hashing that needs to be done to populate the database, not necessarily the actual account conversion.

This is my expectation as well

do you know how heavy the real conversion function is likely to be?

We expect conversion to be trivial, i.e. not using any cryptographic operations at all.

if Unstable_db.num_accounts db2 = 0 then
Or_error.return @@ Converting_ledger.create_with_migration db1 db2
else if not (Db.num_accounts db1 = Unstable_db.num_accounts db2) then (
(* TODO: ensure 2 dbs have exact same accounts *)
Copy link
Member

Choose a reason for hiding this comment

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

To respond to #17379 (comment) here - yes, this is insufficient as a desync check. A converting merkle tree is two databases and we write to them separately, so it should be possible for the daemon to crash between database updates, which would cause a desync. I think you're right, that that is only detectable with a full equality check (up to account conversion) between the databases.

(* TODO: ensure 2 dbs have exact same accounts *)
Db.close db1 ;
Unstable_db.close db2 ;
Or_error.error_string
Copy link
Member

Choose a reason for hiding this comment

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

I think if a desync is detected then we might want to clean up the secondary database and re-migrate, and just issue a warning/error log message. I'm not sure if that cleanup should happen here or if it should be the responsibility of the caller of this method, though. I'd have to check to see how the existing databases are cleaned up.

(The issue I see is similar to the crash back-off loop with the empty PID file - if the invalid resource isn't cleaned up automatically somehow then the working directory of the daemon will become unusable).

if Unstable_db.num_accounts db2 = 0 then
Or_error.return @@ Converting_ledger.create_with_migration db1 db2
else if not (Db.num_accounts db1 = Unstable_db.num_accounts db2) then (
(* TODO: ensure 2 dbs have exact same accounts *)
Copy link
Member

Choose a reason for hiding this comment

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

I think part of the slowness of migration is all the hashing that needs to be done to populate the database, not necessarily the actual account conversion. If the Unstable.of_stable conversion function is expected to be fast, then we might be able to do this integrity check right here. @georgeee do you know how heavy the real conversion function is likely to be?

@cjjdespres
Copy link
Member

I think my comments are showing up twice because I made them "request changes"?

else
let%bind db1_accounts = Db.accounts db1 in
let%bind db2_accounts = Unstable_db.accounts db2 in
(* TODO: check if relying on identifier is enough for equality *)
Copy link
Member Author

Choose a reason for hiding this comment

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

noted

Copy link
Member

Choose a reason for hiding this comment

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

You could do something similar to what's done in the merkle ledger tests for database equality:

Db.iteri primary ~f:(fun idx primary_account ->
let stored_migrated_account =
Db_migrated.get_at_index_exn migrated idx
in
[%test_eq: Migrated.Account.t] stored_migrated_account
(Db_converting.convert primary_account) ) ) ) )

Synced ledgers should have accounts at identical indices.

Copy link
Member

Choose a reason for hiding this comment

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

Because of #17428 (comment) I don't think we need to worry about the speed of account conversion. Hopefully.

@glyh
Copy link
Member Author

glyh commented Jul 15, 2025

!ci-build-me

@@ -43,7 +42,17 @@ module Make (Inputs : Intf.Inputs.DATABASE) = struct

let depth t = t.depth

let create ?directory_name ~depth () =
let rec remove_dir_recursively path =
Copy link
Member

Choose a reason for hiding this comment

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

There is Mina_stdlib_unix.File_system.rmrf for this, potentially

@glyh
Copy link
Member Author

glyh commented Jul 16, 2025

rebased atop compatible

@glyh glyh force-pushed the lyh/converting-ledger-and-tests branch from 95d8adc to 6a0c5de Compare July 16, 2025 00:50
Copy link
Member

@cjjdespres cjjdespres left a comment

Choose a reason for hiding this comment

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

I think that's the last thing. Could you clean up the commits as well? They could probably be condensed down to the first one.

@@ -43,7 +44,7 @@ module Make (Inputs : Intf.Inputs.DATABASE) = struct

let depth t = t.depth

let create ?directory_name ~depth () =
let create ?directory_name ?fresh ~depth () =
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I missed this before - could you declare the fresh with a default to avoid having to deal with the Some later on here?

The new Converting_ledger ledger type in mina_ledger is backed by a
converting_merkle_tree - this will keep a secondary ledger database in
an unstable format synced with a primary ledger database in a stable
format.
@glyh glyh force-pushed the lyh/converting-ledger-and-tests branch from 119cfac to e806e3d Compare July 16, 2025 02:00
@glyh
Copy link
Member Author

glyh commented Jul 16, 2025

!ci-build-me

@amc-ie
Copy link
Member

amc-ie commented Jul 16, 2025

!ci-bypass-changelog

@glyh glyh merged commit 2ea4285 into compatible Jul 16, 2025
71 checks passed
@glyh glyh deleted the lyh/converting-ledger-and-tests branch July 16, 2025 06:18
@glyh glyh mentioned this pull request Jul 22, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants