Sample-actor Code Walkthrough - Login Branch
In the login
branch, the majorify of the logic is in the frontend. There are only a few code changes in the backend to handle the login and faucet logic. Let's walk through it all.
We moved the function to handle "say-hello" from lib.rs
to a new file called dfn.rs
. A new handler "faucet" has also been added. This is used to send a faucet txn when the user clicks the "faucet" button in the frontend.
When adding a new handler, make sure to also add the name to the name_list
, because we'll need to let dfn::map_handler dispatch the request to the coresponding handler.
In the future, almost all request handlers will be put here for easy organizing.
Let's take a look at where these functions are called, for example in lib.rs
:
This is the most important function. The base_res
is used to handle default behaviors that can be handled by the tea_sdk::utils
. In this case, it's the login request.
So if the default handler is used and the crate::dfn::map_handler
does not handle it (cur_res is empty), then the base_res is returned to the client.
If you want to override the default handler, you can define the handler function inside of dfn.rs, so that the cur_res is no longer empty and it will be returned to the client.
In this case, we didn't handle the login request, instead the default login handler inside the tea_sdk::utils
is used. That's why we can write almost zero code to get the login feature.
In our TEA SDK, there are many default handlers like login. For more details about the tea_sdk::utils
please go to tea_sdk_utils.
You may also notice that the AddRequest handler is removed given it's no longer used in this and future steps.
A new api.rs is added, and in this step it's mainly for the txn_faucet function.
When the "faucet" request is received by the actor, crate::dfn::map_handler
will dispatch the function call to txn_faucet. Inside this function, we first generate the req:
then use TappstoreTxn::TransferTea
to convert it to a txn. request::send_tappstore_txn
is used to send such a txn. This txn is sent to the state machine and executed async.
Note we don't expect to get the execution result at this moment because all state machine nodes will handle async.
Please read the code carefully and understand each parameter.
Last updated