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

feat: add unique receipts for cross-subnet transactions #125

Merged
merged 6 commits into from
Feb 22, 2024
Merged

Conversation

JDawg287
Copy link
Member

@JDawg287 JDawg287 commented Feb 15, 2024

Description

This PR resolves the issue with the receiptHash not being unique for two or more cross-subnet transactions.

Problem

Let's try to understand the issue with the following example. For two identical sendToken transactions from the same user on the same subnet, i.e. two transactions where the allowance amount and the arguments for the sendToken transaction are kept the same, we would end up in a situation where the receiptHash would also be identical for these two transactions. This is because there is no nonce for receipts as opposed to transactions where a combination of from and nonce is kept track of to prevent replay attacks. If the two transactions end up in different blocks (let's assume there are no other transactions in both blocks), then the receiptsRoot for both blocks would also be identical.

This isn't a problem on the source subnet, as it is normal to have two blocks with the same receiptsRoot but, it is an issue on the target subnet when we try to execute those transactions. The first transaction would be executed normally but the second transaction would be rejected as it has the same receiptHash as the first transaction. We were using a combination of the receiptsRoot and the receiptHash to identify different transactions.
So, in a nutshell, we need a method to identify these particular transactions uniquely.

Solution

As a solution, we emit an incrementing nonce and the sourceSubnetId as part of the CrossSubnetMessageSent event from the ToposCore contract. This would mean that the receipt data would contain an incrementing nonce for a particular subnet for each sendToken transaction. Since the receipt's trie keeps a record of the receipt's data it would result in a unique receiptsRoot for a block. Furthermore, since the receiptHash is calculated from the receipt's data it would result in a unique receiptHash. As a result, this allows us to uniquely identify different valid transactions with similar arguments.

Fixes TP-864

PR Checklist:

  • I have performed a self-review of my code
  • I have commented on 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

@sebastiendan
Copy link
Member

I recommend renaming receiptRaw and receiptHash to receiptTrieNodeRaw and receiptTrieNodeHash to avoid future confusion

Signed-off-by: Jawad Tariq <[email protected]>
@JDawg287 JDawg287 self-assigned this Feb 19, 2024
Copy link

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

lgtm

contracts/topos-core/ToposCore.sol Show resolved Hide resolved
contracts/topos-core/ToposMessaging.sol Show resolved Hide resolved
@JDawg287 JDawg287 marked this pull request as ready for review February 20, 2024 15:10
Signed-off-by: Jawad Tariq <[email protected]>
Signed-off-by: Jawad Tariq <[email protected]>
@sebastiendan
Copy link
Member

@JDawg287 Let's rename the PR to something not ERC20Messaging-specific? (the problem and the fix concern all messaging protocols)

@JDawg287 JDawg287 changed the title feat: add unique receipts for sendToken transactions feat: add unique receipts for cross-subnet transactions Feb 20, 2024
@JDawg287
Copy link
Member Author

@JDawg287 Let's rename the PR to something not ERC20Messaging-specific? (the problem and the fix concern all messaging protocols)

Done

@dvdplm
Copy link

dvdplm commented Feb 21, 2024

@JDawg287 CI is angry. Seems like a legit problem. :/

@JDawg287
Copy link
Member Author

JDawg287 commented Feb 21, 2024

@JDawg287 CI is angry. Seems like a legit problem. :/

We'll have to change the ToposCoreProxy contract address for that. Plus, we also need to adapt the CrossSubnetMessageSent event on the sequencer side. Tasks/PRs were created respectively. Once the they are merged and CI tests are passing we can merge.

@JDawg287 JDawg287 merged commit 4b819f4 into main Feb 22, 2024
16 of 18 checks passed
@JDawg287 JDawg287 deleted the feat/tp-864 branch February 22, 2024 16:24
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.

3 participants