Skip to content

[sql-49] account + session: Add determintistic sorting of maps during the KVDB to SQL migration. #1132

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ViktorTigerstrom
Copy link
Contributor

Addresses comment #1129 (review).

Based on #1129.

This PR adds deterministic sorting of the maps in the accounts and sessions during the KVDB to SQL migration.
Will remove from draft once I've done more testing.

In the kvdb to sql migration, if there have been caveats set for the
MacaroonRecipe, the order of the postgres db caveats will in very rare
cases differ from the kv store caveats. Therefore, we sort both the kv
and sql caveats by their ID, so that we can compare them in a
deterministic way.
Similar to the previous commit, we also sort the
`MacaroonRecipe.Permissions` slice to ensure it can be compared in a
deterministic manner during migrations.
This commit adds a deterministic comparison for the maps in accounts
during the KVDB to SQL migration.
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @ViktorTigerstrom, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the reliability of the KVDB to SQL migration process for account and session data. The primary goal is to introduce deterministic comparison mechanisms for data structures that inherently lack order, such as maps. By converting these unordered data types into deterministically sorted lists or structs before comparison, the changes ensure that migration validation accurately reflects data integrity, preventing false mismatches due to varying internal representations. This is crucial for robust data migration and validation workflows.

Highlights

  • Deterministic Account Comparison: To ensure reliable validation during the KVDB to SQL migration, new deterministicPayment and deterministicAccount structs have been introduced. These structs convert map-based data within OffChainBalanceAccount into sorted slices, allowing for consistent reflect.DeepEqual comparisons.
  • Deterministic Session Comparison: Similar to accounts, featureConfigEntry and deterministicSession structs were added to handle session data. This change ensures that session information, particularly map-like structures, can be deterministically sorted and compared during the migration validation process.
  • Macaroon Recipe Sorting for Deterministic Comparison: The overrideMacaroonRecipe function has been updated to sort the caveats and permissions within MacaroonRecipe objects. This addresses potential non-deterministic ordering issues that could arise from the KV store versus the SQL database, ensuring accurate deep equality checks post-migration.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces deterministic sorting for maps in accounts and sessions during the KVDB to SQL migration, which is a good approach for ensuring reliable comparisons. The implementation for accounts is solid, though there's a small opportunity to make the code more idiomatic. However, the session migration has a critical issue in newDeterministicSession that could lead to a panic and incorrect comparisons due to missing data and sorting. I've provided detailed comments and suggestions to address these points.

Comment on lines +90 to +104
i := 0
for hash := range acct.Invoices {
invoices[i] = hash
i++
}

i = 0
for hash, paymentEntry := range acct.Payments {
payments[i] = &deterministicPayment{
paymentHash: hash,
paymentEntry: paymentEntry,
}

i++
}

Choose a reason for hiding this comment

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

medium

The loops for populating invoices and payments slices can be written more idiomatically in Go. Instead of manual index management, you could use append with slices initialized with a capacity. This would make the code more concise and less error-prone.

For example:

	invoices := make([]lntypes.Hash, 0, len(acct.Invoices))
	for hash := range acct.Invoices {
		invoices = append(invoices, hash)
	}

	payments := make([]*deterministicPayment, 0, len(acct.Payments))
	for hash, paymentEntry := range acct.Payments {
		payments = append(payments, &deterministicPayment{
			paymentHash:  hash,
			paymentEntry: paymentEntry,
		})
	}

Comment on lines +514 to +552
if kvPerms != nil {
sort.Slice(kvPerms, func(i, j int) bool {
if kvPerms[i].Entity == kvPerms[j].Entity {
return kvPerms[i].Action <
kvPerms[j].Action
}

return kvPerms[i].Entity < kvPerms[j].Entity
})

sort.Slice(sqlPerms, func(i, j int) bool {
if sqlPerms[i].Entity == sqlPerms[j].Entity {
return sqlPerms[i].Action <
sqlPerms[j].Action
}

return sqlPerms[i].Entity < sqlPerms[j].Entity
})
}

Choose a reason for hiding this comment

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

medium

The sorting logic for macaroon permissions is duplicated for kvPerms and sqlPerms. You can extract this logic into a helper function to avoid repetition and improve readability. The same applies to sorting caveats (lines 497-509).

This commit adds a deterministic comparison for the maps in sessions
during the KVDB to SQL migration.
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2025-08-session-migration-ceavat-order-fix-with-maps branch from e1253ce to 0e3eba8 Compare August 18, 2025 11:01
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.

1 participant