Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block include nonce mismatch tx issue #16

Merged
merged 6 commits into from
Jul 18, 2023

Conversation

j75689
Copy link
Collaborator

@j75689 j75689 commented Jul 17, 2023

Description

fix block include nonce mismatch tx issue

Rationale

Currently, there are two possible concurrent invocations of the CheckTx method. The first one is an RPC invocation, and the second one is the simultaneous invocation through different P2P connections.

However, in reality, CheckTx first calls ABCI for basic validation within Cosmos, and upon receiving the cometbft response, it writes to the mempool. The process is illustrated as follows:

  1. mem.proxyAppConn.CheckTxAsync call ABCI
  2. reqRes.SetCallback(mem.reqResCb(tx, txInfo.SenderID, txInfo.SenderP2PID, cb)) write Tx into mempool
image

However, this creates a problem. When the CPU is particularly busy, if concurrent operations occur here, the temporary scheduling of Goroutines may cause the order of writing to the mempool after ABCI check to be incorrect. Ultimately, this results in incorrect transaction order when packaging blocks.

Therefore, we must ensure that all transaction "checking" and "writing" are executed sequentially. In this PR modification, a channel is introduced to execute transaction requests received from both P2P and RPC in a sequential manner, guaranteeing the correct order of writing to the mempool.

Example

add an example CLI or API response...

Changes

Notable changes:

  • rpc, p2p, mempool

@@ -122,12 +124,14 @@ func (memR *Reactor) OnStart() error {
memR.Logger.Info("Tx broadcasting is disabled")
}
go memR.receiveRoutine()
go memR.checkTxRoutine()
return nil
}

// OnStop implements p2p.BaseReactor.
func (memR *Reactor) OnStop() {
close(memR.recvCh)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we dont have to close these two channel.

Because the CI check is failed now: https://github.com/bnb-chain/greenfield-cometbft/actions/runs/5574353577/jobs/10189203589?pr=16.

It is ok leaving there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

resolved

if errors.Is(err, mempool.ErrTxInCache) {
memR.Logger.Debug("Tx already exists in cache", "tx", ntx.String())
} else if err != nil {
memR.Logger.Info("Could not check tx", "tx", ntx.String(), "err", err)
}
}
close(errChan)
Copy link
Contributor

Choose a reason for hiding this comment

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

errChan can be reused.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

resolved

@j75689 j75689 force-pushed the fix/nonce_mismatch_issue branch from 9866402 to c844fee Compare July 18, 2023 02:41
@j75689 j75689 force-pushed the fix/nonce_mismatch_issue branch from c844fee to e926db4 Compare July 18, 2023 02:52
@unclezoro unclezoro merged commit f9c9a7e into bnb-chain:develop Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants