MystenLabs / sui

Sui, a next-generation smart contract platform with high throughput, low latency, and an asset-oriented programming model powered by the Move programming language
https://sui.io
Apache License 2.0
6.29k stars 11.22k forks source link

Refactor the clients to have proper names and layers #275

Closed lxfind closed 2 years ago

lxfind commented 2 years ago

First of all, similar to Ethereum, we should be able to talk to the Authorities in a stateless way, through CLI (similar to geth), REST or RPC. The API at this layer should be stateless (i.e. doesn't represent or own an account), and the CLI can be just called fastx.

Secondly, we then can have a ClientService, which encapsulates stateful APIs that a light client will typically need, to maintain a few accounts and persist objects.

Finally, we can then have a client_cli purely for the purpose of demoing the ClientService. Orthogonally, the ClientService can also expose REST APIs.

lxfind commented 2 years ago

The currently implementation is an artifact of FastPay, which binds Client to a specific account

sblackshear commented 2 years ago

An attempt at a diagram to clarify things a bit. Here, the box labeled "client" encompasses several components with distinct functionality. We refer to all of them as "client" today. The colored boxes attempt to give the distinct components descriptive names.

image

The client service does not manage private keys, or ask the wallet(s) it communicates with to pass along a key.

lxfind commented 2 years ago

Nice! I was just trying to draw the same diagram on my iPad. One thing though, if we look at the current REST API design in https://docs.google.com/document/d/1BHnoHu6kf0diXCK0ZgUUBNl6xUkVk6qMTr3Qi_zH6BQ/edit, it doesn't actually make any statefulness assumptions, which means it could go directly to the Relayer. In fact, maybe the REST API interface will be the same between ClientService and Relayer, it's just that requests to ClientService is faster (due to local cache), but requests to Relayer could be more accurate (if there are other transactions that didn't go through the same ClientService).

lxfind commented 2 years ago

I am still a bit fuzzy on the concept of ClientService. Is there anything similar to this in any existing blockchains?

oxade commented 2 years ago

@arun-koshy this might be useful

sblackshear commented 2 years ago

I am still a bit fuzzy on the concept of ClientService. Is there anything similar to this in any existing blockchains?

I think the most similar concept is a (part of) a wallet. "Wallet" is an overloaded term that encompasses too many things, but there is a part of an Eth wallet that maintains the user's sequence number, balances in various coins, NFT's the user owns, etc. This is analogous to the local state maintained by the ClientService.

You might also think of the ClientService as a "sparse" full node that only syncs data relevant to the user's address(es), and but ignores the rest of the data going into the chain.

sblackshear commented 2 years ago

I deleted a duplicate comment that seems to have wiped out some other comments--apologies + will not do that again! Here is (I think) everything that got accidentally deleted:

image

stella3d commented 2 years ago

i was wrong about the client service though - since it's not a full node / full picture of network state, the block explorer wouldn't run the client service, but something else (i think)

sblackshear commented 2 years ago

i was wrong about the client service though - since it's not a full node / full picture of network state, the block explorer wouldn't run the client service, but something else (i think)

I agree with this. Conceptually, the block explorer (at least in my mental model) is a client service that:

But that conceptual explanation doesn't mean that it's helpful to implement it that way. Like Stella, my intuition would be to speak directly to the authorities. This avoids needless coupling/exposing the block explorer to concepts/data it doesn't care about (e.g., anything to do with writes).

stella3d commented 2 years ago

@sblackshear a part i'm unclear on is what the explorer will run to keep up-to-date on network state - are authorities the stand-in for a traditional chain's full node, and we can continually talk to them to get a complete picture of state ?

gdanezis commented 2 years ago

Hey @stella3d - services that need to maintain a full replica of the state on all fastx authorities will probably have to use a sync API beyond the client API. This sync API is also used to ensure authorities keep up to date with each other and new authorities get the state so far before validating new transactions.

gdanezis commented 2 years ago

Here is the task that tracks ideas on how to build sync: https://github.com/MystenLabs/fastnft/issues/194

stella3d commented 2 years ago

perfect, this looks like exactly what it would need.

arun-koshy commented 2 years ago

Thanks for the diagram Sam!

So it sounds like the direct comparison to what we are building from the client side is what Infura/Alchemy are doing for Ethereum

Screen Shot 2022-01-26 at 5 19 18 PM

So would it be fair to say that the end output of our client ecosystem should either provide a way where the customer can manage their own servers & state and use our stateless rest api to call into the network (via the relayer from your diagram) OR they can use our stateful API as a service (client service from your diagram) that maintains all this state for them?

lxfind commented 2 years ago

client_authority

  1. AuthorityClient is a component responsible for serializing and sending those 4 Authority calls over the wire to a specific Authority server through TCP. So it's really just the other side of the Authority on the network port. It is stateless in the sense that once an AuthorityClient is created it won't change. Each AuthorityClient maps to an Authority.
  2. AuthorityAggregator is a component that abstracts away the fact that FastX has a list of authorities, i.e. AuthorityAggregator will make it look like we only need to talk to one place to do any action. process_order takes an Order and does all the multi-step quorum thing to eventually get a committed cert. Both handle_account_info_request and handle_object_info_request will be responsible for deciding which AuthorityClient to talk to (either random, or quorum, or any other approach) to get the accurate information for return. AuthorityAggregator is also stateless, in that once it's created it won't change.
  3. ClientService is the stateful service at the frontline, that manages a bunch of accounts (we can decide whether we actually want ClientService to own private keys). It maintains the list of objects each account owns, and construct Order objects based on the request type. It also should have some mechanism to passively sync client state (or active, on demand).
  4. Then at the far left we can have different client implementations: a CLI Client that takes interactive command and calls into ClientService; a REST Client that listens on REST requests; or RPC Client if we need.
  5. The MassClient is completely independent at the moment
  6. We also want to manage resources in a more efficient way (not marries to the component separation), but has its own mechanism/layer of managing/sharing them.
gdanezis commented 2 years ago

Hey all -- A few thoughts after having slept on our discussion yesterday, and reading @arun-koshy 's REST doc today.

I think the diagram above that has a single "client" dotted box (https://github.com/MystenLabs/fastnft/issues/275#issuecomment-1022590449) is a little simplifying and misleading. Hear me out:

So in effect the REST client is NOT in the same trust boundary as the user client, and its access and interactions with the REST or other services need to be mediated and subject to authentication and authorization.

sblackshear commented 2 years ago

Hey all -- A few thoughts after having slept on our discussion yesterday, and reading @arun-koshy 's REST doc today.

I think the diagram above that has a single "client" dotted box (#275 (comment)) is a little simplifying and misleading. Hear me out:

  • A user runs on their machine a client that interacts with the fastx authorities to read state, and sends signed orders to them.
  • The orders are signed by a different wallet component on the client as well that holds secret signature keys.
  • However, in the context of a web experience the logic that uses the REST API is a webpage from a separate security domain. Thus there is a need to ensure that this webpage is (a) authorized to access the REST API, (b) authorized to talk with the wallet to request signed orders and (c) potentially pre-authorized to perform certain actions (ie get signed orders) without authorization by the user. (In the context of a game it would be really annoying to ask the user all the time to update stuff).
  • I think that in the context of a mobile experience it is similar, but I know too little about mobile to say anything intelligent.

So in effect the REST client is NOT in the same trust boundary as the user client, and its access and interactions with the REST or other services need to be mediated and subject to authentication and authorization.

Totally agree with this. The oversimplified view applies to what we're shipping for GDC (since there are no web pages, wallets, etc.), but not to any realistic production setup.

velvia commented 2 years ago

@sblackshear and others that diagram above is really nice and would be tremendously helpful to those getting started, like in some kind of architectural overview or README somewhere. I don't really know what a "relayer" is or for.

lxfind commented 2 years ago

@velvia https://github.com/MystenLabs/fastnft/issues/275#issuecomment-1023433724 has something that's closer to what we have today. Agree that we should put these to a README

oxade commented 2 years ago

Arch gets updated often but lately it's mostly steady and agreed we need to be on top of documenting

velvia commented 2 years ago

Arch gets updated often but lately it's mostly steady and agreed we need to be on top of documenting

@oxade I like text-based diagramming tools like Mermaid, so that they can be part of the git history, and now it's officially supported by Github!!!!

https://github.blog/2022-02-14-include-diagrams-markdown-files-mermaid/

I volunteer to put diagrams in the README. :D

velvia commented 2 years ago

@oxade @lxfind Put the diagram above into README:

https://github.com/MystenLabs/fastnft/pull/539

lxfind commented 2 years ago

This is all done.