Repository navigation
Description
When a node's Tenderdash mempool cache still holds the hash of a state transition that left the mempool without being committed, every re-broadcast of those bytes to that node is answered with gRPC AlreadyExists: tx already exists in cache. rs-dapi has a handler meant to turn this into "already in mempool", "already in chain" or the real CheckTx error. As far as we can tell, that handler never runs for this error. dashmate's keep-invalid-txs-in-cache = true keeps such hashes cached, and rs-sdk sends the next retry to whichever node its rotation picks, which can be the same one. A client can't tell a dropped transition from a pending one.
We hit this building Dash Forge, a git forge that runs on Platform.
Expected Behavior
Re-broadcasting the exact bytes of a transition tells the client which case it is in: still pending in the mempool, already committed, or no longer accepted (with the CheckTx error). This is what rs-dapi's handle_duplicate_transaction is written to do. JS DAPI did it from #2270 on.
Current Behavior
Three things combine. The first is our reading of the code; the log excerpt further down shows what a client sees.
1. rs-dapi's duplicate check is not reached for this error.
- Tenderdash checks its cache before CheckTx and returns
ErrTxInCache(mempool.go L229-L237).broadcast_tx_syncreturns that as a JSON-RPC error, code -32000 "Tx already exists in cache" (rpc/core/mempool.go L71-L84, types.go L177-L180). - rs-dapi's Tenderdash client turns any JSON-RPC error into
DapiError::TenderdashClientError(tenderdash_client.rs L364-L367).broadcast_to_tenderdashpasses that error through unchanged (L140). Only a CheckTx result with a non-zero code goes throughmap_tenderdash_message, so only such a result can reachDapiError::AlreadyExists(_) => self.handle_duplicate_transaction(..)(L151-L154). - The client error is then converted by
TenderdashStatus::to_status, which maps the message to a bareAlreadyExists("tx already exists in cache")(error_mapping.rs L41-L57, L384-L388). - JS DAPI matched this case on the JSON-RPC error's
datafield (broadcastStateTransitionHandlerFactory.js L71-L73), so this looks like it was lost in the Rust port. - Our logs agree. Every answer carried the literal message
tx already exists in cache. We never sawstate transition already in mempool,already in chainor a CheckTx error. - A second problem would show up once the routing is fixed. Step 2 of the handler only treats
DapiError::NotFoundas "not in chain" (L200-L214). The client returns Tenderdash'stx (…) not foundas aTenderdashClientError, though (tenderdash rpc/core/tx.go L28-L31), so the handler would likely return that error instead of reaching CheckTx. Step 1 already handles the same shape by matching "not found" in the message (L179-L198).
2. dashmate keeps the hashes of invalid transitions cached.
- The Tenderdash template hardcodes
keep-invalid-txs-in-cache = truefor every network (config.toml.dot L352-L355). Its own comment says to set it to true only "if it's not possible for any invalid transaction to become valid again in the future". Tenderdash's default is false (config.go L901-L904). - Here is our reading of Tenderdash v1.8.1 with this setting. A hash stays cached when the transition fails its first CheckTx (mempool.go L530-L534), fails recheck (L742-L755) or is committed with a non-zero code (L446-L458). After that it stays until it falls out of the LRU cache (
cacheSize: 15000, getBaseConfigFactory.js L408) or the node restarts. - On Platform, a transition that one node rejects can be valid later or on another node. Examples are a node that is a block behind, or the chainlock-height race in Drive accepts invalid Identity create transition in mempool (chainlock race condition) #3552. So the precondition in that comment doesn't hold.
- The mempool TTL does not cover this case:
ttl-durationonly purges transactions that are still in the mempool (L850-L874). It is0sin the base config (L416) and24hon testnet and mainnet.
3. rs-sdk does not move off the node.
AlreadyExistsis not retryable (rs-dapi-client grpc.rs L111-L126, rs-sdk error.rs L364-L369).broadcast()therefore returns it at once (retries: 0in our logs) and does not exclude the node (sync.rs L139-L146). The caller's next send may go to the same node.- rs-sdk already handles this for one DPNS case: "A node can retain rejected transaction hashes. Never resend this rejection to the same node" (sync.rs L163-L189).
What we observed. We ran a bulk import of document batch creates, 8 at a time, each followed by a bounded wait and a re-send of the same bytes. Two runs died the same way: one after 50 documents, one after 359. Each time, a transition was accepted and its wait heard nothing. Every re-send was then answered by one node:
00:06:10Z WARN dash_sdk::platform::transition::broadcast: broadcast: request failed error=ExecutionError { inner: AlreadyExists("tx already exists in cache"), retries: 0, address: Some(<node A>) }
00:06:32Z ... same, <node A>
00:06:57Z ... same, <node A>
00:07:25Z ... same, <node A>
00:07:45Z WARN write not confirmed; giving up
The second run looked the same: four re-sends, all to one (different) node. In both cases the identity-contract nonce stayed unspent and the transition never appeared on chain. It had left the mempool without landing, but its hash was still cached. We could not tell from the client which path dropped it.
Possible Solution
Suggestions, roughly in order of effort:
- rs-dapi: route the JSON-RPC "tx already exists in cache" error (code -32000) into
handle_duplicate_transaction. Let step 2 treat Tenderdash'stx (…) not foundthe way step 1 already does. A client would then get "in mempool", "in chain", the real CheckTx error, or the existing Internal "create a new state transition with different hash" message, and each of those is actionable. This also bears on rs-sdk: put_to_platform_and_wait_* fails with AlreadyExists when a timed-out broadcast was already accepted #4998, which proposes treatingAlreadyExistsas "accepted". That is safe only ifAlreadyExistsreally means "in the mempool or in a block", and this fix would make it so. - dashmate: consider
keep-invalid-txs-in-cache = false(Tenderdash's default), or make it a config option. We can't judge the cost of more repeated CheckTx calls for known-invalid bytes. - Tenderdash: remove a hash from the cache when its transition leaves the mempool without being committed. Note that the same-sender rejection path returns before touching the cache whatever the setting is (mempool.go L548-L566).
- rs-sdk / wasm-sdk: when
AlreadyExistscomes without "in mempool" or "in chain", briefly exclude that node and try another, as the DPNS failover already does.
A call that reports a transition's status by hash without re-broadcasting would also help. We filed that separately as a feature request, #5269.
Steps to Reproduce (for bugs)
The failure we hit depends on a node dropping a transition, which is timing-dependent. From the code, we expect this simpler sequence to show the same behaviour, but we have not run it on its own:
- On a network set up with dashmate defaults, broadcast a state transition that fails CheckTx, for example a document batch with a stale identity-contract nonce. The first answer is the consensus error.
- Broadcast the identical bytes to the same node again.
- Expected: the same consensus error, through
handle_duplicate_transaction's CheckTx step. Actual (expected from the code):AlreadyExists: tx already exists in cache.
Context
Our importer writes about 74,000 documents for one large repository. Before our workaround, each of these failures killed the run, and each restart cost us about 1 h 45 min.
Our workaround (forge-core drive_write) works like this:
- After an
AlreadyExistsand a silent wait with the nonce still free, every later send goes to a node not yet tried. - If that also stays silent, it re-signs with a fresh nonce. That is only safe for document types where a unique index lets just one copy land. Elsewhere a re-sign could create a duplicate if the original turned up later.
With the workaround, the full run of 73,541 documents completed. 50 broadcasts were answered tx already exists in cache, and 4 transitions never landed and had to be re-signed.
Related: #5269 (status lookup by hash), #4998 (AlreadyExists when the transition was accepted), #3552 (a transition stuck in the cache after a proposal rejects it), #2270 (where JS DAPI added the duplicate check).
Your Environment
- Version used: Platform 5.0.0-beta.1 on a devnet (sakura) set up with dashmate defaults; Tenderdash from dashmate's default
dashpay/tenderdash:1.8image (we read the v1.8.1 source). - Client: rs-sdk (
dash-sdk) at git tagv5.0.0-beta.1, in a Rust CLI. - Operating System and version: macOS client.
- Link to your project: https://github.com/PastaPastaPasta/dash-forge
Activity