Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat: add Create your messaging protocol content #24

Merged
merged 9 commits into from
Jan 19, 2024
Merged

Conversation

sebastiendan
Copy link
Member

Description

This PR adds new content about creating one's own messaging protocol.

Fixes TOO-414

PR Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added or updated tests that comprehensively prove my change is effective or that my feature works

Copy link

netlify bot commented Jan 15, 2024

Deploy Preview for toposware-docs-platform ready!

Name Link
🔨 Latest commit 44bb7b6
🔍 Latest deploy log https://app.netlify.com/sites/toposware-docs-platform/deploys/65aac3b520ca4e0008f1972b
😎 Deploy Preview https://deploy-preview-24--toposware-docs-platform.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@wyhaines wyhaines left a comment

Choose a reason for hiding this comment

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

I am in favor of getting this merged ASAP. Approved.

@sebastiendan
Copy link
Member Author

I am in favor of getting this merged ASAP. Approved.

@wyhaines This is still WIP, that's why I had not set any reviewer (but I should have created the PR as a draft).

@wyhaines
Copy link
Collaborator

I assumed that it was simply going to be iterative content. Get something released now, and then build on it.

Regardless, what you wrote reads fine to me, so I really am in favor of publishing sooner rather than later.


1. The **ToposCore** smart contract can be used to store certificates on-chain. Although not compulsory, it makes sense for a subnet to have a single instance of such a certificate-storing smart contract.
2. The **ToposMessaging** smart contract makes use of ToposCore. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. It is not meant to be deployed directly, but instead to be used as an inherited smart contract for the messaging protocol proper.
2. The **ToposMessaging** smart contract gathers functionalities that are core to messaging protocols. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. To ease the development of new messaging protocols, these functionalities can be obtained for free by creating a messaging protocol smart contract that inherits ToposMessaging.
Copy link
Contributor

Choose a reason for hiding this comment

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

The two core features mentioned here, inclusion proofs and replay attack resistance, are not very well motivated. Why would I want a "messaging protocol"? Can we emphasize that such protocols are for cross-subnet apps? And that the approach is that each dApp develops their own such protocol. Once motivated, the idea and advantage of using a ToposMessaging contract as the basis is easier to convey imo.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it be fair to say that this is part of a different piece work, a rework of B9's content?

This PR contains changes in the first module because a few things were unclear. Now, adding things that make sense like what you said would require re-working a greater piece of the content, notably the way we introduce messaging protocols in the whole portal, which sounds like something much bigger than a simple PR comment suggestion application, no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is a fair POV. I think it would be reasonable to create a ticket to follow this PR up with some additional work that clarifies those other areas more.

Otherwise, yes, there is the risk that the scope creeps too much on this set of changes. I'd rather see the content live soon, even if there are things that can be adjusted and improved later.


1. The **ToposCore** smart contract can be used to store certificates on-chain. Although not compulsory, it makes sense for a subnet to have a single instance of such a certificate-storing smart contract.
2. The **ToposMessaging** smart contract makes use of ToposCore. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. It is not meant to be deployed directly, but instead to be used as an inherited smart contract for the messaging protocol proper.
2. The **ToposMessaging** smart contract gathers functionalities that are core to messaging protocols. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. To ease the development of new messaging protocols, these functionalities can be obtained for free by creating a messaging protocol smart contract that inherits ToposMessaging.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
2. The **ToposMessaging** smart contract gathers functionalities that are core to messaging protocols. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. To ease the development of new messaging protocols, these functionalities can be obtained for free by creating a messaging protocol smart contract that inherits ToposMessaging.
2. The **ToposMessaging** smart contract gathers features that are core to messaging protocols. It has functions to verify proofs of inclusion and to mark completed messages as used, thereby preventing replay. To ease the development of new messaging protocols, such features can be had for free by creating a messaging protocol smart contract that inherits ToposMessaging.


# Create your messaging protocol

Developing a custom messaging protocol is in essence about writing a smart contract, in the fashion of **ERC20Messaging**, that 1. inherits **ToposMessaging** to get set and ready with the core functionalities needed for cross-subnet messaging (e.g., emitting a special event to announce a cross-subnet message, verifying a proof of receipt inclusion) on a sending and a receiving subnet, and 2. adds the custom business logic that governs the cross-subnet message execution (e.g., burning and minting an internal ERC20 token in **ERC20Messaging**).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Developing a custom messaging protocol is in essence about writing a smart contract, in the fashion of **ERC20Messaging**, that 1. inherits **ToposMessaging** to get set and ready with the core functionalities needed for cross-subnet messaging (e.g., emitting a special event to announce a cross-subnet message, verifying a proof of receipt inclusion) on a sending and a receiving subnet, and 2. adds the custom business logic that governs the cross-subnet message execution (e.g., burning and minting an internal ERC20 token in **ERC20Messaging**).
Developing a custom cross-subnet messaging protocol is in essence about writing a pair of smart contracts, in the fashion of **ERC20Messaging**, that 1. inherits **ToposMessaging** for the core features needed for cross-subnet messaging (e.g., emitting a special event to announce a message, or verifying a proof of receipt inclusion) on a sending and a receiving subnet, and 2. adds the custom business logic that governs the cross-subnet message execution (e.g., burning and minting an internal ERC20 token in **ERC20Messaging**).

Copy link
Member Author

@sebastiendan sebastiendan Jan 19, 2024

Choose a reason for hiding this comment

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

I will apply it except the "cross-subnet" addition to "messaging protocol" ("messaging protocol" is a term that's been used as is in the rest of the doc), and the "pair of" (there's only one contract to develop)


From start to end, a messaging protocol walks the following path:

1- An arbitrary business logic is executed on the sending subnet
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
1- An arbitrary business logic is executed on the sending subnet
1- An arbitrary piece of business logic is executed on the sending subnet


3- A specific transaction is sent to the receiving subnet in order to validate and execute the cross-subnet message

4- An arbitary business logic is executed on the receiving subnet
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
4- An arbitary business logic is executed on the receiving subnet
4- An arbitrary piece of business logic is executed on the receiving subnet

…or perhaps just "Some business logic is executed…"


1- Burn the amount that the caller requested to send to another subnet

4- Verify that the address of the **ERC20Messaging** contract on the sending subnet corresponds to the current one (i.e., on the receiving subnet), verify that the requested receiving subnet is the correct one, retrieve the requested ERC20 token, and finally mint the right amount to the right recipient
Copy link
Contributor

Choose a reason for hiding this comment

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

Verify that the address…

Is it obvious to readers why the sending and receiving contract addresses must match? I think not?

Copy link
Member Author

Choose a reason for hiding this comment

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

It isn't but I don't think it should as it is introduced as an arbitrary check that ERC20 got its design built upon. But you still think it should come with a reason?


</HighlightBox>

Now, we will detail the ToposMessaging interface and its relevant methods for you to start leveraging its power.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Now, we will detail the ToposMessaging interface and its relevant methods for you to start leveraging its power.
Now, we will detail the ToposMessaging interface and its relevant methods for you to use it.


## ToposMessaging

Like messaging protocol contracts inheriting it, **ToposMessaging** exposes functionalities that relate either to the sending subnet or the receiving subnet in a cross-subnet messaging flow.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Like messaging protocol contracts inheriting it, **ToposMessaging** exposes functionalities that relate either to the sending subnet or the receiving subnet in a cross-subnet messaging flow.
Like messaging protocol contracts inheriting it, **ToposMessaging** exposes features that relate either to the sending subnet or the receiving subnet in a cross-subnet messaging flow.


### Sending subnet (1)

- `_emitMessageSentEvent(SubnetId targetSubnetId)`: This function expects a target subnet id which is the id of the receiving subnet, and takes care of emitting the specific event mentioned in Messaging protocol flow, point 2. This event is **ToposCore**'s **CrossSubnetMessageSent** event, which is not defined in **ToposMessaging**, hence the `_emitMessageSentEvent` helper function.
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to link to the CrossSubnetMessageSent docs&code here.

@wyhaines
Copy link
Collaborator

image

I built locally, and some things aren't rendering right. I haven't gone back through yet to try to figure out what, but I wanted to verify that others are seeing it this way, too?

Copy link
Collaborator

@wyhaines wyhaines left a comment

Choose a reason for hiding this comment

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

Overall, it is looking pretty good!

@@ -1,6 +1,7 @@
.highlightbox {
* {
@apply text-inherit;
overflow: scroll;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is causing a poor visual experience with the highlight boxes:

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, additional context, I see why you did this.

image

This (without the overflow: scroll) is being caused by an interaction with the highlight box. The code boxes, either the flavor that one gets with the markdown syntax, or by explicitly using a CodeBox component, do not behave properly with regard to scroll and their overflow boundaries when they are inside of a HighlightBox.

The best thing here would be to solve why the highlightbox is breaking things. I can try to figure that out, as it has repercussions across multiple sites, and update here.


You now should have a deep understanding of what messaging protocols are in the Topos ecosystem and how they work. You got to see what **ToposMessaging** is and what functionalities it offers to messaging protocol developers ready to jump into the world of cross-subnet messaging. **ERC20Messaging**, the messaging protocol that we developed and deployed on our different networks, was mentioned many times to illustrate with a concrete example the steps that compose the development and use of a messaging protocol.

The next steps after writing your messaging protocol will be to create a dapp-frontend, if needed, to let your users start using your dApp, and start, if needed too, interacting with the Executor Service to delegate that `execute` function call to it (and prevent your users from paying transaction fees twice).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
The next steps after writing your messaging protocol will be to create a dapp-frontend, if needed, to let your users start using your dApp, and start, if needed too, interacting with the Executor Service to delegate that `execute` function call to it (and prevent your users from paying transaction fees twice).
The next steps after writing your messaging protocol will be to create a dapp-frontend, if needed, to let your users start using your dApp, and to start, if needed, interacting with the Executor Service to delegate the `execute` function call to it (and prevent your users from paying transaction fees twice).


**ERC20Messaging** emits, on a sending subnet, the following `TokenSent` event to detail which cross-subnet ERC20 transfer was requested:

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

The syntax highlighter that we use handles Solidity.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice, I didn't expect the syntax highlighter to work on the dev portal (thought this was github only).


3- Emits the **CrossSubnetMessageSent** core event to announce the cross-subnet message

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity


Only the last line that emits the **CrossSubnetMessageEvent** is a requirement for all messaging protocols. So if we were to name the first function of **CustomMessaging** `doSomething`, `doSomething` would have the following structure:

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity


Now that you have retrieved the right log index, you can retrieve the rest of the log's data you are interested in:

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

<br/>
As we can see, it starts by retrieving the expected index of the `TokenSent` event.

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

<br/>
Then, from the right index, the address of the contract that emitted the `TokenSent` event is retrieved and compared to the current address (the address of the **ERC20Messaging** smart contract instance being used). This is because **ERC20Messaging** expects all instances of its contract to be deployed at the same address on all subnets.

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

<br/>
Next, from the right index and the corresponding array of topics, the target subnet id is retrieved. Index `1` is used because the first topic of an EVM event is the event signature, and the next are the indexed arguments of the event and `targetSubnetId` is the first and only indexed argument of the `TokenSent` event.

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

<br/>
Finally, the data field is decoded and the data to be used to execute the cross-subnet ERC20 transfer is retrieved (token symbol, receiver address, and amount), before the transfer gets executed by minting the token.

```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```
```solidity

sebastiendan and others added 5 commits January 19, 2024 18:39
Co-authored-by: Jawad <38837406+JDawg287@users.noreply.github.com>
Co-authored-by: Jawad <38837406+JDawg287@users.noreply.github.com>
@sebastiendan
Copy link
Member Author

sebastiendan commented Jan 19, 2024

image

I built locally, and some things aren't rendering right. I haven't gone back through yet to try to figure out what, but I wanted to verify that others are seeing it this way, too?

@wyhaines I think this is windows only (overflow scroll shows invisible scrolling bars on mac). I will find another fix to replace the overflow: scroll; on all highlight boxes.

@sebastiendan sebastiendan merged commit 3936d8e into main Jan 19, 2024
4 checks passed
@sebastiendan sebastiendan deleted the seb/too-414 branch January 19, 2024 18:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants