-
Notifications
You must be signed in to change notification settings - Fork 2.3k
channeldb: add persist nodeannounment config in db #8690
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
Closed
Abdulkbk
wants to merge
9
commits into
lightningnetwork:master
from
Abdulkbk:persist-nodeannouncement-config
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0815a1
channeldb: add persist nodeannounment config in db
Abdulkbk d7719d8
channeldb: check for persisted node announcement alias
Abdulkbk 815e069
channeldb+lnd: check for persisted alias in disk
Abdulkbk 75ffcaf
channeldb: remove migration for nodeannouncemnet bucket
Abdulkbk 03e2ac6
channeldb+peersrpc+lnd: save node announcement config whenever updated
Abdulkbk 31820dc
lnd: determine source for alias and color
Abdulkbk 9365f95
test: test persisting and retrieving node announcement
Abdulkbk 371c12e
channeldb+peersrpc+lnd: save features and addresses to disk
Abdulkbk 5899f80
channeldb: add test to compare local and persisted node announcement …
Abdulkbk 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,6 +286,12 @@ var ( | |
| number: 31, | ||
| migration: migration31.DeleteLastPublishedTxTLB, | ||
| }, | ||
| { | ||
| // Create a top level bucket which holds information | ||
| // about our node announcement. | ||
| number: 32, | ||
| migration: mig.CreateTLB(nodeAnnouncementBucket), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you dont need a migration to add a new bucket.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed it in another commit. |
||
| }, | ||
| } | ||
|
|
||
| // optionalVersions stores all optional migrations that are applied | ||
|
|
@@ -447,6 +453,7 @@ var dbTopLevelBuckets = [][]byte{ | |
| outpointBucket, | ||
| chanIDBucket, | ||
| historicalChannelBucket, | ||
| nodeAnnouncementBucket, | ||
| } | ||
|
|
||
| // Wipe completely deletes all saved state within all used buckets within the | ||
|
|
||
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
|
Abdulkbk marked this conversation as resolved.
|
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,155 @@ | ||
| package channeldb | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/lightningnetwork/lnd/kvdb" | ||
| ) | ||
|
|
||
| var ( | ||
| // nodeAnnouncementBucket stores announcement config pertaining to node. | ||
| // This bucket allows one to query for persisted node announcement | ||
| // config and use it when starting orrestarting a node | ||
| nodeAnnouncementBucket = []byte("nab") | ||
| ) | ||
|
|
||
| type NodeAnnouncement struct { | ||
| // Alias indicate the human readable name that a node operator can | ||
| // assign to their node for better readability and easier identification | ||
| Alias string | ||
|
|
||
| // Color represent the hexadecimal value that node operators can assign | ||
| // to their nodes. It's represented as a hex string. | ||
| Color string | ||
|
|
||
| // IdentityPub is the node's current identity public key. Any | ||
| // channel/topology related information received by this node MUST be | ||
| // signed by this public key. | ||
| IdentityPub *btcec.PublicKey | ||
| } | ||
|
|
||
| // FetchNodeAnnouncement attempts to lookup the data for NodeAnnouncement based | ||
| // on a target identity public key. If a particular NodeAnnouncement for the | ||
| // passed identity public key cannot be found, then returns ErrNodeAnnNotFound | ||
| func (d *DB) FetchNodeAnnouncement(identity *btcec.PublicKey) (*NodeAnnouncement, error) { | ||
| var nodeAnnouncement *NodeAnnouncement | ||
| err := kvdb.View(d, func(tx kvdb.RTx) error { | ||
| nodeAnn, err := fetchNodeAnnouncement(tx, identity) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| nodeAnnouncement = nodeAnn | ||
| return nil | ||
| }, func() { | ||
| nodeAnnouncement = nil | ||
| }) | ||
|
|
||
| return nodeAnnouncement, err | ||
| } | ||
|
|
||
| func fetchNodeAnnouncement(tx kvdb.RTx, targetPub *btcec.PublicKey) (*NodeAnnouncement, error) { | ||
| // First fetch the bucket for storing node announcement, bailing out | ||
| // early if it hasn't been created yet. | ||
| nodeAnnBucket := tx.ReadBucket(nodeAnnouncementBucket) | ||
| if nodeAnnBucket == nil { | ||
| return nil, ErrNodeAnnBucketNotFound | ||
| } | ||
|
|
||
| // If a node announcement for that particular public key cannot be | ||
| // located, then exit early with ErrNodeAnnNotFound | ||
| pubkey := targetPub.SerializeCompressed() | ||
| nodeAnnBytes := nodeAnnBucket.Get(pubkey) | ||
| if nodeAnnBytes == nil { | ||
| return nil, ErrNodeAnnNotFound | ||
| } | ||
|
|
||
| // FInally, decode and allocate a fresh NodeAnnouncement object to be | ||
| // returned to the caller | ||
| nodeAnnReader := bytes.NewReader(nodeAnnBytes) | ||
| return deserializeNodeAnnouncement(nodeAnnReader) | ||
|
|
||
| } | ||
|
|
||
| func (d *DB) PutNodeAnnouncement(pubkey *btcec.PublicKey, alias, color string) error { | ||
| nodeAnn := &NodeAnnouncement{ | ||
| Alias: alias, | ||
| IdentityPub: pubkey, | ||
| Color: color, | ||
| } | ||
|
|
||
| return kvdb.Update(d, func(tx kvdb.RwTx) error { | ||
| nodeAnnouncements := tx.ReadWriteBucket(nodeAnnouncementBucket) | ||
|
|
||
| nodeAnnBucket, err := nodeAnnouncements.CreateBucketIfNotExists(pubkey.SerializeCompressed()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var b bytes.Buffer | ||
| if err := serializeNodeAnnouncement(&b, nodeAnn); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = nodeAnnBucket.Put(pubkey.SerializeCompressed(), b.Bytes()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
| }, func() {}) | ||
| } | ||
|
|
||
| func serializeNodeAnnouncement(w io.Writer, n *NodeAnnouncement) error { | ||
| // Serialize Alias | ||
| if _, err := w.Write([]byte(n.Alias + "\x00")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Serialize Color | ||
| if _, err := w.Write([]byte(n.Color + "\x00")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Serialize IdentityPub | ||
| serializedID := n.IdentityPub.SerializeCompressed() | ||
| if _, err := w.Write(serializedID); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func deserializeNodeAnnouncement(r io.Reader) (*NodeAnnouncement, error) { | ||
| var err error | ||
| nodeAnn := &NodeAnnouncement{} | ||
|
|
||
| // Read Alias | ||
| aliasBuf := make([]byte, 32) | ||
| if _, err := io.ReadFull(r, aliasBuf); err != nil { | ||
| return nil, err | ||
| } | ||
| nodeAnn.Alias = string(bytes.TrimRight(aliasBuf, "\x00")) | ||
|
|
||
| // Read Color | ||
| colorBuf := make([]byte, 8) | ||
| if _, err := io.ReadFull(r, colorBuf); err != nil { | ||
| return nil, err | ||
| } | ||
| nodeAnn.Color = string(bytes.TrimRight(colorBuf, "\x00")) | ||
|
|
||
| var pub [33]byte | ||
| if _, err := io.ReadFull(r, pub[:]); err != nil { | ||
| return nil, err | ||
| } | ||
| nodeAnn.IdentityPub, err = btcec.ParsePubKey(pub[:]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return nodeAnn, err | ||
|
|
||
| } |
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
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.