LF-Decentralized-Trust-labs / paladin

Programmable privacy for EVM
https://lf-decentralized-trust-labs.github.io/paladin
Apache License 2.0
17 stars 6 forks source link

Refactor nonce assignment to post-submit of public transactions #441

Closed peterbroadhurst closed 5 days ago

peterbroadhurst commented 1 week ago

The problem

The "2 phase submit" refactor of the public transaction manager DB transaction model in #179, introduced a problem with nonce allocation. Specifically on restart when there is a public transaction in the DB that is not yet submitted to the mempool of the blockchain, the nonce allocation attempts to re-use a nonce already allocated in the DB of the node.

Digging into this showed that we had a significant issue to resolve in the database transaction model, between the Transaction Manager/Private Transaction Manager submitting the transaction, and the Public Transaction Manager attempting to allocate a nonce.

Inherited from previous generations, we were assigning the nonce in-line with the submission, but this is problematic when we have batch database transactions. We need to know in this moment what other nonces are in-flight for that signing from address, across the database, and the eth_getTransactionCount call against the blockchain.

This is further complicated by the fact we might be submitting batches of public transactions covering multiple signing addresses. Two threads racing could hit A-B, B-A deadlocks if we took fine grained locks on an individual signing address. Then course grained locks would be very problematic for transaction submission performance.

The solution

This PR splits nonce assignment out from transaction submission.

DB Migration notes

Due to the early phase of the release cycle, I propose the following compromises to limit the complexity of the DB migrations:

Other fixes