Introduce ProofsWithState for atomic proof state management #1488
No reviewers
Labels
No labels
DB & Storage
Deployment
Error Handling & Logging
Maintenance
Payment Backend
backport
backport v0.13.x
backport v0.14.x
backport v0.15.x
bindings
blocked
bug
cdk-sql
ci
cli
deps
documentation
duplicate
enhancement
good first issue
help wanted
invalid
keep-open
ldk-node-ui
migrations
mint
mutation-testing
needs rebase
needs review
new nut
nut change
question
ready
rust-version
rustfmt
stacked hold
stale
testing
wallet
weekly-report
wontfix
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
cashubtc/cdk!1488
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "feature/mint-acquired-proofs"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
This PR depends on #1490
Replace individual proof state operations with a unified ProofsWithState type that enforces the invariant that all proofs in a set share the same state. This shifts responsibility for state consistency to the database layer and simplifies state transition logic in the saga implementations.
Changes to ProofsTransaction trait:
Benefits:
Notes to the reviewers
Suggested CHANGELOG Updates
CHANGED
ADDED
REMOVED
FIXED
Checklist
just final-checkbefore committingWe should enforce that the number of got proofs is == the number requested. I think this is safer then forcing the application to check this.
We should explicitly roll back the tx before returning the error. As is done on 214, other wise it leads to race conditions in tests.
Can this check be removed now? Since we have the lock on the proofs from the add and then we check the state transition in the
set_new_state?@ -463,2 +422,2 @@).await?;if let Err(err) = tx.add_completed_operation(I think we need to call compensate on the error here since we would expect the error here and not in the
tx.update_proofslike we used to.Should have compensate here as well or input proofs are stuck in pending.
Yes, I'm doing this right now.
Fixed in e5f358df02af5a4d1f9938a8069e8cfd50c7b23a
@ -463,2 +422,2 @@).await?;if let Err(err) = tx.add_completed_operation(Fixed in 0d761ef
Fixed in 0d761ef
@ -463,2 +422,2 @@).await?;if let Err(err) = tx.add_completed_operation(Fixed in 0d761ef
Fixed in 0d761ef
@ -31,6 +32,67 @@ pub enum OperationKind {Melt,}This introduces a third state update pattern to the codebase. Currently we have:
Since proofs follow a state machine (Unspent → Pending → Spent) just like MeltQuote, it makes sense to use the same pattern:
This keeps persistence patterns consistent and eliminates the possibility of in-memory/DB state divergence if update_proofs() fails after set_new_state() succeeds.
@ -451,2 +516,4 @@Ok((proofs, states.into_iter().map(Some).collect()))}This seems a bit weird setting this to unspent, should we just error on an empty query?
@ -31,6 +32,67 @@ pub enum OperationKind {Melt,}@thesimplekid What about the changes in d8bd4fa17ac06d03f7496e5532178bb6227a7b8a ?
@ -31,6 +32,67 @@ pub enum OperationKind {Melt,}There is still a difference here with how we handle melt quotes vs proofs. Melt quotes we check the transition within sql common(
github.com/cashubtc/cdk@44fee4c4d1/crates/cdk-sql-common/src/mint/quotes.rs (L898)). Where the proofs we call check transition in the mint before calling update.@ -31,6 +32,67 @@ pub enum OperationKind {Melt,}Yeah, this is duplicated and will be removed in another PR. I think the consensus was to move all checks off the storage layer. This was missed, I'll fix it later
@ -453,2 +413,4 @@self.compensate_all().await?;return Err(err);}I think we can do better then calling check state and then update. If we have a
set_statefn on theAcquired<ProofsWithState>that takes a db tx. It can check its a valid transition and update the memory state and db state in one fn. Same pattern could be applied to the MeltQuote, and a similar one even to MintQuote.@ -31,6 +32,67 @@ pub enum OperationKind {Melt,}As far as I can tell we only call it there and not in the mint so its not a duplicate.
Yes but we need to make sure they are not missed and the storage and memory are updated together (see review comment).
Can't we remove this check as its done in the Mint::update_proofs_state? We should make sure that method returns the correct error.
@ -0,0 +1,267 @@use cdk_common::database::{Acquired, DynMintTransaction};We should return the correct error based on the current state.
Ex from what we did before
@ -0,0 +40,4 @@#[cfg(test)]mod tests {use cdk_common::mint::Operation;Nice adding tests.
@ -177,54 +177,30 @@ impl<'a> SwapSaga<'a, Initial> {);Can't we remove this check as its done in the
Mint::update_proofs_state? We should make sure that method returns the correct error.Fixed in
36b0dc6e2d@ -0,0 +1,267 @@use cdk_common::database::{Acquired, DynMintTransaction};Fixed in
36b0dc6e2d@ -177,54 +177,30 @@ impl<'a> SwapSaga<'a, Initial> {);Fixed in
36b0dc6e2d