-
Notifications
You must be signed in to change notification settings - Fork 14
RGB KVStore Integration #17
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
dcorral
wants to merge
5
commits into
RGB-Tools:rgb
Choose a base branch
from
dcorral:persistence-layer
base: rgb
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
09b42b1
Implement RGB KVStore Integration
dcorral 5ac1202
Rename rgb_rename_files + restore return type
dcorral 1ad58fe
Move check to method
dcorral c4a126e
Move db ops to update_rgb_channel_info method
dcorral 776fdc0
Rename write_rgb_payment_info_file
dcorral 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
Some comments aren't visible on the classic Files Changed page.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you choose to use
KVStoreSyncinstead ofKVStore(its async version)? I would use the latter if possible, since RLN runs in an async environmentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used KVStoreSync because all the call sites are in synchronous code paths: color_commitment, color_htlc, color_closing, update_rgb_channel_id, handle_funding, etc. are all sync functions inside channel.rs and channelmanager.rs, which are entirely sync.
Using the async KVStore would require making all of these async, which would mean changing a big chunk of LDK's internals. do you think it's worth doing that, or is KVStoreSync ok for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about doing like ldk-node does? Which is implementing an inner store and then implementing for the wrapper store both
KVStoreandKVStoreSync. That way we could use theKVStoreSyncin LDK (where we don't have an async environment) andKVStorein RLN (where we do have an async environment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, sounds like a plan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking into it, KVStoreSyncWrapper already exists and it wraps any KVStoreSync into KVStore by boxing the sync result into a future. So the rust-lightning side can stay as-is with KVStoreSync and on the RLN side we just wrap the sea-orm store with KVStoreSyncWrapper to expose the async KVStore trait where needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But wouldn't this mean we are actually doing sync calls to the DB? What I would like to achieve is to actually do async calls. To me what
SqliteStoreandSqliteStoreInnerin ldk-node are doing is a bit different, but I could be wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, KVStoreSyncWrapper wraps a sync result in Box::pin(async move { res }), so it is still sync under the hood.
Since the rgb_kv_store calls are all sync, I'll keep the bound as Arc<dyn KVStoreSync + Send + Sync> in this PR then on the RLN side I could implement both KVStoreSync and KVStore on SeaOrmKvStore directly (like ldk-node), drop KVStoreSyncWrapper, and .await sea-orm futures directly in the async impl.
Let me know if I'm missing something, this is quite a topic!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems the way to go!
There are some partially related implementation details that I think should be discussed though:
dyn KVStoreSyncforces dynamic dispatch on every read/write/remove/list at runtime. I would instead add aKV: KVStoreSync + Send + Sync + 'staticgeneric parameter and storeArc<KV>, this would give monomorphization (static dispatch at compile time) and be consistent with the existing pattern.KVStoreSynccall does: detect runtime ->thread::scope-> spawn OS thread ->DB_RUNTIME.block_on(future)-> join. That adds a thread lifecycle overhead for each DB call. Moreover, theDB_RUNTIMEis a current thread runtime, which serializes every DB access through a single thread. With the current design, under high load, concurrent calls to the DB would block each other. So even if you raisemax_connections, the dedicated runtime would process futures one at a time. You should instead usetokio::task::block_in_place+Handle::current().block_on()and eliminate the dedicatedDB_RUNTIME.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed on both points 🙏
Conclusions:
for this PR: I'll replace
Arc<dyn KVStoreSync + Send + Sync>with a genericKV: KVStoreSync + Send + Sync + 'staticonChannelContext,ChannelManager,OutboundPayments, and all constructors/deserialization. static dispatch + consistent with existing LDK patterns.on the RLN side: I'll implement both
KVStoreSyncandKVStoredirectly onSeaOrmKvStorelike ldk-node'sSqliteStore. theKVStoreimpl will.awaitsea-orm futures natively (real async, no wrapper). theKVStoreSyncimpl will useblock_in_place+Handle::current().block_on(), dropping the dedicatedDB_RUNTIMEand the per-callthread::scopeoverhead.KVStoreSyncWrappergoes away.Let me know if you have any correction before I can jump to the implementation, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can proceed. Just a couple of notes:
I haven't checked that you added it only where actually necessary, but generally speaking let's add the generic instead of using
dynYes. I'm not sure why we also need
RlnDatabasethough, seems we could keep only that orSeaOrmKvStore.