paritytech / capi

[WIP] A framework for crafting interactions with Substrate chains
https://docs.capi.dev
Apache License 2.0
104 stars 10 forks source link

safeguard against same nonces in parallelized txs #660

Open harrysolovay opened 1 year ago

harrysolovay commented 1 year ago

In some cases, two txs will trigger two system_accountNextIndex calls, which are parallelized, and produce the same nonce. This in turn invalidates one of the two txs. I believe we need some kind of nonce-management system––perhaps a dedicated Rune.

harrysolovay commented 1 year ago

Ideally we could somehow encode the parallelism restriction in the Rune type.

Rune.tuple([txA, txB])
//               ~~~
//               ^
//               Argument of type 'ExtrinsicRune<U, Chain>' is not assignable to parameter of type 'NonExtrinsicRune'.

On the flip side: if we could pull this off without too much overhead, it might be worth it.

EDIT

After reflection with @tjjfvi, it became clear this approach would...

harrysolovay commented 1 year ago

I believe we should create a new error type DuplicateAccountNonceError which we could return and unhandle from SignedExtrinsicRune's sent in the case that someone fires off a new sent with a duplicate nonce.

Rune
  .tuple([sent1, sent2])
  .rehandle(DuplicateAccountNonceError, () => Rune.constant(...))
  .run()