Skip to content

Commit

Permalink
Update meta-transactions.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez authored Sep 2, 2024
1 parent 3193a66 commit 98b5e16
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions docs/2.build/1.chain-abstraction/meta-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ Progress is being made to make this possible in the future.

### High volume parallel processing

When running a relayer that handles a large number of transactions, issues will quickly arise due to each transaction requiring a unique nonce. If multiple transactions are sent from the same access key simultaneously, the process becomes error-prone.
When running a relayer that handles a large number of transactions, you will quickly run into a `nonce` collision problem. At the protocol level, transactions have a unique number that identifies them (nonce) that helps to mitigate reply attacks. Each key on an account has its own nonce, and the nonce is expected to increase with each signature the key creates.

Fortunately, this is easy to resolve. Adding multiple full access keys to the NEAR account used for relaying (up to 20 keys can make a significant difference) will help. Then, relaying transactions using instances of the Account object created from the various keypairs associated with the account.
When multiple transactions are sent from the same access key simultaneously, their nonces might collide. Imagine the relayer creates 3 transactions `Tx1`, `Tx2`, `Tx3` and send them in very short distance from each other, and lets assume that Tx3 has the largest nonce. If Tx3 ends up being processed before Tx1 (because of network delay, or simply because a node picks Tx3 before Tx1), then Tx3 will execute, but Tx1 and Tx2 will fail, because they have smaller nonce!

To add a key, generate a new key pair and use the original account object to call the `addKey` method:
One way to mitigate this is to sign each transaction with a different key. Adding multiple full access keys to the NEAR account used for relaying (up to 20 keys can make a significant difference) will help.

<details>
<summary> Adding keys </summary>

```js
const keyPair = nearAPI.KeyPair.fromRandom("ed25519");
Expand All @@ -122,6 +125,8 @@ const receipt = await account.addKey(keyPair.getPublicKey().toString())

After saving these keys, its possible to rotate the private keys randomly when instantiating accounts before relaying ensuring you won't create a nonce collision.

</details>

### Gating the relayer

In most production applications it's expected that you want to be able to gate the relayer to only be used in certain cases.
Expand Down

0 comments on commit 98b5e16

Please sign in to comment.