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

Wasm compatible Database for bdk #450

Open
josediegorobles opened this issue Oct 14, 2021 · 9 comments
Open

Wasm compatible Database for bdk #450

josediegorobles opened this issue Oct 14, 2021 · 9 comments

Comments

@josediegorobles
Copy link

Hi, i find that i need to have a database on wasm and i think i'm not the first nor the last. I'm currently working in the browser and it's all great with bdk but it's very cumbersome for the (future) users to wait for the synchronization every time.
So i think in two options:

  • To make a version of MemoryDatabase in which we save the serialized structures on LocalStorage
  • Implement the database tratis for IndexedDB. web_sys has support for IndexedDB so i think it can be a good possibility.
    I really need this feature and I will have time for work on it, so what do you think is the best path and the requirements and niceties for this?
@afilini
Copy link
Member

afilini commented Oct 15, 2021

I haven't tried implementing it yet, but I think the main issue is gonna be the fact that basically all JS APIs are async. And it's not just the fact that they are async, which can be worked around by waiting on the future: some APIs use callbacks to return the data, which probably makes it even trickier to implement.

If I had to pick one method I would go for the one that has the easiest API to integrate: ideally something synchronous, or slightly worse something that uses async so that you can wait on the futures.

Maybe before trying to write my own DB I would try to look for alternatives around, see if somebody has already made some kind of wrapper for the raw browser APIs and try to use those.

@notmandatory
Copy link
Member

Some recommended user friendly DB libraries built on IndexedDB are mentioned here:
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API#see_also

@josediegorobles
Copy link
Author

I can see this
https://www.npmjs.com/package/idb
It uses async/await, is simple and is reasonably maintained.

Another possibility is use localStorage through gloo-storage. We can save and retrieve any serializable object and it's in rust yet. Do you think it can be a good idea?

@afilini
Copy link
Member

afilini commented Oct 18, 2021

Interfacing with JS library is not that easy, so I would just use gloo-storage instead if you need to get something done quickly.

@josediegorobles
Copy link
Author

josediegorobles commented Oct 18, 2021

Do you think it would be something interesting for the project, then? To have a database based on gloo_storage.

@notmandatory
Copy link
Member

I don't have much experience with rust wasm and haven't used any rust wasm storage crates but it looks like gloo_storage has a straight forward key/value API which should be all you need to implement the Database trait. And yes I think it sounds like a good thing to try for your first contribution.

@benthecarman
Copy link
Contributor

I implemented this using gloo_storage here.

This however may not be entirely suitable for production we have found. Any read/write to the database and halt the thread and cause your browser to be slow. If we had an asynchronous database it would much better because we could use indexeddb.

@notmandatory
Copy link
Member

Thank for upstreaming your wasm storage work with #789 ! I hate to pin all our hopes on it but decoupling blockchain updates and database updates so they can run async I believe is one of the goal of the bdk_core work that's underway.

@futurepaul
Copy link
Contributor

yeah I think that would be exactly good for this situation.

in the meantime going to try and use the memory db + a method for "syncing" the memory db to indexed db asynchronously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

5 participants