-
Notifications
You must be signed in to change notification settings - Fork 2.2k
channeldb+migration: add type-prefixed waiting proof records #10633
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
ellemouton
wants to merge
2
commits into
lightningnetwork:master
Choose a base branch
from
ellemouton:waitingproof-type-migration
base: master
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.
+863
−26
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package migration35 | ||
|
|
||
| import ( | ||
| "github.com/btcsuite/btclog/v2" | ||
| ) | ||
|
|
||
| // log is a logger that is initialized as disabled. This means the package will | ||
| // not perform any logging by default until a logger is set. | ||
| var log = btclog.Disabled | ||
|
|
||
| // UseLogger uses a specified logger to output package logging info. | ||
| func UseLogger(logger btclog.Logger) { | ||
| log = logger | ||
| } |
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,197 @@ | ||
| package migration35 | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "fmt" | ||
|
|
||
| lnwire "github.com/lightningnetwork/lnd/channeldb/migration/lnwire21" | ||
| "github.com/lightningnetwork/lnd/kvdb" | ||
| ) | ||
|
|
||
| var ( | ||
| // waitingProofsBucketKey is the top-level bucket that stores waiting | ||
| // proofs. | ||
| waitingProofsBucketKey = []byte("waitingproofs") | ||
|
|
||
| // byteOrder is the preferred DB byte order. | ||
| byteOrder = binary.BigEndian | ||
| ) | ||
|
|
||
| // waitingProofType represents the type of a waiting proof record. | ||
| type waitingProofType uint8 | ||
|
|
||
| const ( | ||
| // waitingProofTypeV1 represents AnnounceSignatures1 proofs (gossip v1). | ||
| waitingProofTypeV1 waitingProofType = 0 | ||
| ) | ||
|
|
||
| // legacyWaitingProofKey is the key format used by legacy waiting proof | ||
| // records: [scid(8) || isRemote(1)]. | ||
| type legacyWaitingProofKey [9]byte | ||
|
|
||
| // waitingProofKey is the updated key format used by waiting proof records: | ||
| // [proofType(1) || scid(8) || isRemote(1)]. | ||
| type waitingProofKey [10]byte | ||
|
|
||
| // waitingProof is a migration-only representation of a waiting proof record. | ||
| type waitingProof struct { | ||
| announceSignatures *lnwire.AnnounceSignatures | ||
| isRemote bool | ||
| } | ||
|
|
||
| // LegacyKey computes the legacy waiting proof store key. | ||
| func (p *waitingProof) LegacyKey() legacyWaitingProofKey { | ||
| var key legacyWaitingProofKey | ||
| binary.BigEndian.PutUint64( | ||
| key[:8], p.announceSignatures.ShortChannelID.ToUint64(), | ||
| ) | ||
|
|
||
| if p.isRemote { | ||
| key[8] = 1 | ||
| } | ||
|
|
||
| return key | ||
| } | ||
|
|
||
| // Key computes the updated waiting proof store key. | ||
| func (p *waitingProof) Key() waitingProofKey { | ||
| var key waitingProofKey | ||
| key[0] = byte(waitingProofTypeV1) | ||
|
|
||
| binary.BigEndian.PutUint64( | ||
| key[1:9], p.announceSignatures.ShortChannelID.ToUint64(), | ||
| ) | ||
|
|
||
| if p.isRemote { | ||
| key[9] = 1 | ||
| } | ||
|
|
||
| return key | ||
| } | ||
|
|
||
| // decodeLegacyWaitingProof decodes a pre-migration waiting proof in the | ||
| // legacy format: isRemote + raw AnnounceSignatures payload. | ||
| func decodeLegacyWaitingProof(v []byte) (*waitingProof, error) { | ||
| r := bytes.NewReader(v) | ||
|
|
||
| // Decode the legacy side bit first. | ||
| var isRemote bool | ||
| if err := binary.Read(r, byteOrder, &isRemote); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Decode the legacy AnnounceSignatures payload. | ||
| ann := &lnwire.AnnounceSignatures{} | ||
| if err := ann.Decode(r, 0); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Reconstruct the migration-local waiting proof representation. | ||
| return &waitingProof{ | ||
| announceSignatures: ann, | ||
| isRemote: isRemote, | ||
| }, nil | ||
| } | ||
|
|
||
| // encodeUpdatedWaitingProof encodes a waiting proof in the new format: | ||
| // type byte + isRemote + raw AnnounceSignatures payload. | ||
| func encodeUpdatedWaitingProof(p *waitingProof) ([]byte, error) { | ||
| var b bytes.Buffer | ||
|
|
||
| // Prefix the payload with the explicit waiting proof type. | ||
| if err := binary.Write(&b, byteOrder, waitingProofTypeV1); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Preserve the side bit after the type prefix. | ||
| if err := binary.Write(&b, byteOrder, p.isRemote); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Encode the existing AnnounceSignatures payload unchanged. | ||
| if err := p.announceSignatures.Encode(&b, 0); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return b.Bytes(), nil | ||
ellemouton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // MigrateWaitingProofStore migrates waiting proofs to include a leading proof | ||
| // type byte and rewrites record keys to include proof type as well. | ||
| func MigrateWaitingProofStore(tx kvdb.RwTx) error { | ||
| log.Info("Migrating waiting proof store") | ||
|
|
||
| bucket := tx.ReadWriteBucket(waitingProofsBucketKey) | ||
|
|
||
| // If the bucket doesn't exist there is no data to migrate. | ||
| if bucket == nil { | ||
| return nil | ||
| } | ||
|
|
||
| type migratedProof struct { | ||
| oldKey []byte | ||
| newKey waitingProofKey | ||
| value []byte | ||
| } | ||
|
|
||
| var migratedProofs []migratedProof | ||
|
|
||
| err := bucket.ForEach(func(k, v []byte) error { | ||
| // Skip nested bucket references. | ||
| if v == nil { | ||
| return nil | ||
| } | ||
|
|
||
| proof, err := decodeLegacyWaitingProof(v) | ||
| if err != nil { | ||
| return fmt.Errorf("decode waiting proof for key %x: %w", | ||
| k, err) | ||
| } | ||
|
|
||
| // Sanity check: the key should match the proof content. | ||
| legacyKey := proof.LegacyKey() | ||
| if !bytes.Equal(k, legacyKey[:]) { | ||
| return fmt.Errorf("proof key (%x) does not "+ | ||
| "match bucket key (%x)", legacyKey, k) | ||
| } | ||
|
|
||
| updatedProofValue, err := encodeUpdatedWaitingProof(proof) | ||
| if err != nil { | ||
| return fmt.Errorf("encode updated waiting "+ | ||
| "proof for key %x: %w", k, err) | ||
| } | ||
|
|
||
| oldKey := make([]byte, len(k)) | ||
| copy(oldKey, k) | ||
|
|
||
| migratedProofs = append(migratedProofs, migratedProof{ | ||
| oldKey: oldKey, | ||
| newKey: proof.Key(), | ||
| value: updatedProofValue, | ||
| }) | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, proof := range migratedProofs { | ||
| if err := bucket.Delete(proof.oldKey); err != nil { | ||
| return fmt.Errorf( | ||
| "delete legacy waiting proof key %x: %w", | ||
| proof.oldKey, err, | ||
| ) | ||
| } | ||
|
|
||
| if err := bucket.Put(proof.newKey[:], proof.value); err != nil { | ||
| return fmt.Errorf( | ||
| "put updated waiting proof key %x: %w", | ||
| proof.newKey, err, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.