-
Notifications
You must be signed in to change notification settings - Fork 144
tapgarden: hardening phase two (batch invariants) #2203
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
Open
jtobin
wants to merge
5
commits into
lightninglabs:main
Choose a base branch
from
jtobin:tapgarden-batch-invariant
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86a5d62
tapgarden+tapdb: collapse batch-state two-truth dualism
jtobin 835008f
tapgarden: split fundBatch into create vs. apply
jtobin a4c279a
tapgarden: find the unique anchor seedling deterministically
jtobin e1fd2ee
tapdb: enforce singleton pre-broadcast minting batch invariant
jtobin cec1069
docs: add release note
jtobin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package tapcfg | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "github.com/btcsuite/btclog/v2" | ||
| "github.com/lightninglabs/taproot-assets/tapdb" | ||
| "github.com/lightninglabs/taproot-assets/tapgarden" | ||
| "github.com/lightningnetwork/lnd/signal" | ||
| ) | ||
|
|
||
| // RunRepairTool inspects the configured database for batches that | ||
| // violate the singleton "≤ 1 in {Pending, Frozen}" invariant added | ||
| // in migration 000060, and cancels all but the most recent. The | ||
| // preserved batch is the one with the latest CreationTime; cancelled | ||
| // batches transition to BatchStateSeedlingCancelled, leaving their | ||
| // row and seedlings on disk for later inspection. | ||
| // | ||
| // The function opens the database with migrations skipped, so it can | ||
| // run against a legacy database whose state would otherwise fail the | ||
| // migration. | ||
| // | ||
| // NOTE: With migration 000061's self-heal in place, restarting tapd | ||
| // normally will cancel the duplicates as part of applying the | ||
| // migration. This tool is retained as a diagnostic that surfaces the | ||
| // same repair outside the migration stream (e.g. after operator | ||
| // intervention that re-introduces duplicates). | ||
| func RunRepairTool(cfg *Config, cfgLogger btclog.Logger, | ||
| shutdownInterceptor signal.Interceptor) error { | ||
|
|
||
| // Derive a cancellable context that trips on shutdown. Without | ||
| // this, a Ctrl+C mid-repair would leave partial state behind | ||
| // (some batches cancelled, others not). | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go func() { | ||
| select { | ||
| case <-shutdownInterceptor.ShutdownChannel(): | ||
| cancel() | ||
| case <-ctx.Done(): | ||
| } | ||
| }() | ||
|
|
||
| // Open the database with migrations skipped. We want to inspect | ||
| // and repair a database whose state would otherwise prevent | ||
| // migration 000060 from applying; running migrations as part of | ||
| // opening the DB would defeat the purpose. | ||
| var ( | ||
| db tapdb.DatabaseBackend | ||
| err error | ||
| ) | ||
| switch cfg.DatabaseBackend { | ||
| case DatabaseBackendSqlite: | ||
| sqliteCfg := *cfg.Sqlite | ||
| sqliteCfg.SkipMigrations = true | ||
| cfgLogger.Infof("repair: opening sqlite3 database at %v "+ | ||
| "(migrations skipped)", | ||
| sqliteCfg.DatabaseFileName) | ||
| db, err = tapdb.NewSqliteStore(&sqliteCfg) | ||
|
|
||
| case DatabaseBackendPostgres: | ||
| pgCfg := *cfg.Postgres | ||
| pgCfg.SkipMigrations = true | ||
| cfgLogger.Infof("repair: opening postgres database " + | ||
| "(migrations skipped)") | ||
| db, err = tapdb.NewPostgresStore(&pgCfg) | ||
|
|
||
| default: | ||
| return fmt.Errorf("unknown database backend: %s", | ||
| cfg.DatabaseBackend) | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("repair: unable to open database: %w", err) | ||
| } | ||
|
|
||
| mintingExec := tapdb.NewTransactionExecutor( | ||
| db, func(tx *sql.Tx) tapdb.PendingAssetStore { | ||
| return db.WithTx(tx) | ||
| }, | ||
| ) | ||
| store := tapdb.NewAssetMintingStore(mintingExec) | ||
|
|
||
| nonFinal, err := store.FetchNonFinalBatches(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("repair: unable to fetch non-final "+ | ||
| "batches: %w", err) | ||
| } | ||
|
|
||
| var preBroadcast []*tapgarden.MintingBatch | ||
| for _, batch := range nonFinal { | ||
| switch batch.State() { | ||
| case tapgarden.BatchStatePending, | ||
| tapgarden.BatchStateFrozen: | ||
|
|
||
| preBroadcast = append(preBroadcast, batch) | ||
|
|
||
| default: | ||
| // Only pre-broadcast states are constrained by | ||
| // the singleton index; ignore everything else. | ||
| } | ||
| } | ||
|
|
||
| if len(preBroadcast) <= 1 { | ||
| cfgLogger.Infof("repair: nothing to do; found %d batches "+ | ||
| "in pre-broadcast state", len(preBroadcast)) | ||
| return nil | ||
| } | ||
|
|
||
| // Sort newest-first by CreationTime; preserve [0], cancel the | ||
| // rest. SliceStable gives a deterministic winner when two | ||
| // batches share a timestamp -- the input order (from | ||
| // FetchNonFinalBatches) then acts as the tiebreak. | ||
| sort.SliceStable(preBroadcast, func(i, j int) bool { | ||
| return preBroadcast[i].CreationTime.After( | ||
| preBroadcast[j].CreationTime, | ||
| ) | ||
| }) | ||
|
|
||
| preserved := preBroadcast[0] | ||
| cfgLogger.Infof("repair: preserving most recent pre-broadcast "+ | ||
| "batch %x (state=%v, created=%s)", | ||
| preserved.BatchKey.PubKey.SerializeCompressed(), | ||
| preserved.State(), | ||
| preserved.CreationTime.Format(time.RFC3339)) | ||
|
|
||
| for _, batch := range preBroadcast[1:] { | ||
| cfgLogger.Warnf("repair: cancelling pre-broadcast batch "+ | ||
| "%x (state=%v, created=%s)", | ||
| batch.BatchKey.PubKey.SerializeCompressed(), | ||
| batch.State(), | ||
| batch.CreationTime.Format(time.RFC3339)) | ||
|
|
||
| err := store.UpdateBatchState( | ||
| ctx, batch, tapgarden.BatchStateSeedlingCancelled, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("repair: unable to cancel batch "+ | ||
| "%x: %w", | ||
| batch.BatchKey.PubKey.SerializeCompressed(), | ||
| err) | ||
| } | ||
| } | ||
|
|
||
| cfgLogger.Infof("repair: complete; cancelled %d duplicate "+ | ||
| "batches, preserved 1.", len(preBroadcast)-1) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.