Skip to content
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

peer-store, wasm: use path as database name in wasm peer-store implementation #4760

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions network/src/peer_store/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub async fn get_db<P: AsRef<Path>>(path: P) -> &'static Storage {
DB.get_or_init(|| Storage::new(path)).await
}

const STORE_NAME: &str = "main-store";

#[derive(Clone)]
pub struct Storage {
chan: tokio::sync::mpsc::Sender<Request>,
Expand All @@ -67,15 +69,14 @@ pub struct Storage {
impl Storage {
pub async fn new<P: AsRef<Path>>(path: P) -> Self {
Copy link
Collaborator

@driftluo driftluo Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path needs to be added with the network name. No matter what network is passed in, the same path is used now.

By the way,if we use network with DB name, a different path as the object store name, is it ok?(I think the current problem is that the path is the same value regardless of the network.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • For the first question, the user can customize store path in network configuration to make different network use different databases
  • For the second, IndexedDB limits store creation only to the time of upgrading database, if we want to use different stores for different configurations, we'll need a suitable way to determine database version

let factory = Factory::new().unwrap();
let mut open_request = factory.open("network", Some(1)).unwrap();
let store_name = path.as_ref().to_str().unwrap().to_owned();
let store_name_clone = store_name.clone();
let database_name = path.as_ref().to_str().unwrap().to_owned();
let mut open_request = factory.open(&database_name, Some(1)).unwrap();
Copy link
Collaborator

@eval-exec eval-exec Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a secret_key file in network dir. If the secret key in CKB's network directory changes, it may cause the NodeId to change as well. Is this expected? Cc. @driftluo

Copy link
Collaborator

@driftluo driftluo Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, wasm target‘s secret_key isn't stored on this path. The user manually passes it to ckb

open_request.on_upgrade_needed(move |event| {
let database = event.database().unwrap();
let store_params = ObjectStoreParams::new();

let store = database
.create_object_store(&store_name_clone, store_params)
.create_object_store(STORE_NAME, store_params)
.unwrap();
let mut index_params = IndexParams::new();
index_params.unique(true);
Expand All @@ -92,9 +93,9 @@ impl Storage {
match request.cmd {
CommandRequest::Read { key } => {
let tran = db
.transaction(&[&store_name], TransactionMode::ReadOnly)
.transaction(&[STORE_NAME], TransactionMode::ReadOnly)
.unwrap();
let store = tran.object_store(&store_name).unwrap();
let store = tran.object_store(STORE_NAME).unwrap();
let key = serde_wasm_bindgen::to_value(&key).unwrap();
let value = store
.get(key)
Expand All @@ -107,9 +108,9 @@ impl Storage {
}
CommandRequest::Put { kv } => {
let tran = db
.transaction(&[&store_name], TransactionMode::ReadWrite)
.transaction(&[STORE_NAME], TransactionMode::ReadWrite)
.unwrap();
let store = tran.object_store(&store_name).unwrap();
let store = tran.object_store(STORE_NAME).unwrap();

let key = serde_wasm_bindgen::to_value(&kv.key).unwrap();
let value = serde_wasm_bindgen::to_value(&kv).unwrap();
Expand Down
Loading