ckb-js / lumos

A full featured dapp framework for Nervos CKB
https://lumos-website.vercel.app/
MIT License
67 stars 54 forks source link

Client-side TX Generation #68

Open tspoff opened 4 years ago

tspoff commented 4 years ago

Tl;dr:

Client-side TX Generation in Lumos

I’m thinking about reducing development work and increasing decentralization for the dApps that are built with Lumos framework. Especially for people just getting familiar with the Nervos platform.

Lumos, as currently designed, encourages each dApp developer to write a custom server that contains the bulk of the logic for their dApp. This is because Lumos is intended such that the entity that handles TX generation needs to be the same as the entity running the indexer.

I’ll show how the flow of a transaction looks currently on lumos, and show a model where things happen in a heavier client, freeing the dApp developer from having to write, run, and maintain a second application (the custom server). The lack of a custom server intermediary also potentially improves decentralization.

Current Flow: Tx Generation on Server

The flow of a user action looks something like this:

current model

Additionally, read requests (such as for cells or balances) are routed through the server.

Alternative: TX Generation on Client

The dApp client could do TX generation on its’ own, if it had access to an indexer to query for cells. A simple, stateless server running the Lumos indexer could provide a source to query directly for cells. In this model, you can think of the indexer as an infrastructure service, akin to a node, that can be run by anyone. This stands in contract to a custom server.

In this model, an action flow would be:

* The user signals an action on the client.
* The client generates an appropriate transaction, using cells queried from the indexer server.
* The client gathers required signatures from the wallet.
* The client sends the transaction directly to a CKB node.
* The client can track the status of the transaction via a subscription to a CKB node.

alternative model

The client could also collect cells for displaying balances from an indexer, or perhaps get this information from the wallet for the active user.

Performance

I don’t expect that bandwidth or compute requirements will be significantly increased for a large percentage of dApps (Compared to a web2 app with similar amounts of data, or the dApp server model). Some applications that deal with a lot of data at once, such as analytics platforms, or that require intensive TX generation, will likely benefit hugely from the dApp server.

Caching information

Derived information is essential for users to make sense of what’s happening on CKB. There should be a client-side caching layer that’s easy to set up with this model.

Open Transactions: A Cavent

For dApps that involve open transactions, there will still need to be some sort of aggregator (custom service) that holds state about the current transaction fragments.

xxuejie commented 4 years ago

I do believe what you explained here could be super useful. It is also why I maintained ckb-js-toolkit as a separate library. However, lumos is designed from day one to only run in node.js environment. There might be some parts that we can refactor and split to a separate module, but lumos here, IMHO, is better suited as a node-only framework.