cuprated: embeddable node library with Node::launch()#592
cuprated: embeddable node library with Node::launch()#592redsh4de wants to merge 20 commits intoCuprate:mainfrom
Node::launch()#592Conversation
c680c1c to
6542272
Compare
bdbba2c to
81d85e1
Compare
81d85e1 to
618b162
Compare
binaries/cuprated/src/lib.rs
Outdated
| /// Panics if the database is corrupt or critical services fail to start. | ||
| pub async fn launch(config: Config) -> Self { | ||
| // Initialize global static `LazyLock` data. | ||
| statics::init_lazylock_statics(); |
There was a problem hiding this comment.
statics::init_lazylock_statics() twice--once in main.rs, once here. What should actually be responsible for this?
There was a problem hiding this comment.
Ideally this should be called by the embedder imo.
However, it's kept twice because if it's called a second time, it's a no-op, and if the embedder doesnt do it the library will take care of it
| let (mut blockchain_read_handle, mut blockchain_write_handle, _) = | ||
| cuprate_blockchain::service::init_with_pool( | ||
| config.blockchain_config(), | ||
| Arc::clone(&db_thread_pool), | ||
| ) | ||
| .inspect_err(|e| error!("Blockchain database error: {e}")) | ||
| .expect(DATABASE_CORRUPT_MSG); |
There was a problem hiding this comment.
won't this panic if the db is corrupt, misconfigured, etc? maybe locked?
inspect_err logs it then panics anyways?
There was a problem hiding this comment.
It will - the graceful shutdown PRs that are dependant on this branch gradually introduce error propagation and get rid of the expects
They are split into parts so that its easier to review, as the changes there are quite verbose
| let (txpool_read_handle, txpool_write_handle, _) = | ||
| cuprate_txpool::service::init_with_pool(&config.txpool_config(), db_thread_pool) | ||
| .inspect_err(|e| error!("Txpool database error: {e}")) | ||
| .expect(DATABASE_CORRUPT_MSG); |
There was a problem hiding this comment.
same thing here, will this panic if the db is corrup tor locked?
There was a problem hiding this comment.
Same as prior, deferred to the other three graceful shutdown PRs
| let context_svc = | ||
| blockchain::init_consensus(blockchain_read_handle.clone(), config.context_config()) | ||
| .await | ||
| .unwrap(); |
There was a problem hiding this comment.
this naked unwrap can cause a panic w no useful msg if consensus init fails
|
you should try and return something like I don't get the difference between or relative purposes of Node and NodeContext. Which should be used for what? with dry_run_check calling std::process::exit(code), won't an embedder calling config.dry_run_check() get their process killed? nit but SyncState renamed unnecessarily imo there's no way to shut it down? I like main.rs after this tho. I like CommandHandle, get_fast_sync_hashes, BlockchainManagerHandle. not an in depth review but these things stood out to me in a once-over and some tangential reading to try and understand the changes |
Deferred this to the shutdown PRs - they depend on this branch so they carry everything over (https://github.com/Cuprate/cuprate/pull/585/changes#diff-d448bb1addb0153e0d62b48a7e6531f9988ea8f1846ab783540eaf9f2c87d87fR159)
Maybe there could be a better name for each struct so its self-documenting?
Yeah, good call
It was a transitive change as i was also planning to add current sync target height to the struct, so the embedder can potentially create progress UIs/bars etc, got sidetracked though
Ty, added shutdown function to #585 (https://github.com/Cuprate/cuprate/pull/585/changes#diff-d448bb1addb0153e0d62b48a7e6531f9988ea8f1846ab783540eaf9f2c87d87fR342-R344) |
Gotcha, OK :) sorry for the incomplete review then, glad to hear most of those issues are resolved by later PRs |
|
cancelling workflow because this is some doc nits, let's think a little about our carbon footprint. |
|
Think this is the last change i want to make here, didn't like how Also added |
What
Extracts
cupratedinto an embeddable library (lib.rs/main.rspattern). Takes inspiration from the API proposed in #554Closes #516
Note to reviewers: This does not implement graceful shutdown / panic removal yet, those have been deferred to #585. For the final state with all changes, skip to #590
Why
To enable embedding the node in other applications
Where
cuprated:Cargo.toml- explicit[lib]+[[bin]]targetslib.rs- new:Nodestruct,NodeContextfor shared state,Node::launch(), public service handlesmain.rs- simplified to CLI wrapper callingNode::launch()commands.rs- actor pattern rewrite:CommandHandlewith mpsc/oneshot,send_command()APIconfig.rs- decoupled from CLI args,find_config()returnsResult<Option<Config>>, removedprocess::exitfrom library codeblockchain/syncer.rs-SyncNotifyrenamed toSyncStateblockchain/interface.rs- free functions + statics (COMMAND_TX,BLOCKS_BEING_HANDLED) converted toBlockchainManagerHandlestruct with owned channel + lockversion.rs- addDefaultimpl (thanks clippy)NodeContextfor multi-instance supportconsensus/fast-sync:set_fast_sync_hashesremoved;fast_sync_stop_heightandvalidate_entriesnow take hashes as a parameter from NodeContextHow
Node::launch(config)inlib.rshandles all initialization and returns aNodewith service handles that serve as the API to the embeder:The previous mutable statics were moved into
NodeContext, making the node safe to run multiple times in a single process.BlockchainManagerHandlereplaces the COMMAND_TX and BLOCKS_BEING_HANDLED statics frominterface.rs, wrapping them into a struct withnew(),is_block_being_handled(), andhandle_incoming_block()methods.The Tor interface arrives via oneshot after sync, similar to the
dandelion.rspattern.