Skip to content

Commit

Permalink
vochain: remove invalid transactions from mempool
Browse files Browse the repository at this point in the history
On process proposal, if the proposal is rejected, remove the
invalid transactions. This way we do not contribute on propagate
invalid transactions on the network.

Fix prepare proposal invalid transaction deletion by correctly
increasing the failedCount counter.

Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Jan 16, 2024
1 parent d2f3e44 commit 750a842
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
6 changes: 4 additions & 2 deletions vochain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type DeliverTxResponse struct {
Log string
Info string
Data []byte
TxID [32]byte
}

// ExecuteBlockResponse is the response returned by ExecuteBlock after executing the block.
Expand Down Expand Up @@ -174,7 +175,7 @@ func (app *BaseApplication) ExecuteBlock(txs [][]byte, height uint32, blockTime
"data", string(resp.Data),
"info", resp.Info,
"log", resp.Log)
invalidTxs = append(invalidTxs, [32]byte{})
invalidTxs = append(invalidTxs, resp.TxID)
}
result = append(result, resp)
}
Expand Down Expand Up @@ -236,7 +237,7 @@ func (app *BaseApplication) deliverTx(rawTx []byte) *DeliverTxResponse {
response, err := app.TransactionHandler.CheckTx(tx, true)
if err != nil {
log.Errorw(err, "rejected tx")
return &DeliverTxResponse{Code: 1, Data: []byte(err.Error())}
return &DeliverTxResponse{Code: 1, TxID: tx.TxID, Data: []byte(err.Error())}
}
app.txReferences.Delete(tx.TxID)
// call event listeners
Expand All @@ -248,6 +249,7 @@ func (app *BaseApplication) deliverTx(rawTx []byte) *DeliverTxResponse {
Data: response.Data,
Info: fmt.Sprintf("%x", response.TxHash),
Log: response.Log,
TxID: tx.TxID,
}
}

Expand Down
21 changes: 11 additions & 10 deletions vochain/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,13 @@ func (app *BaseApplication) PrepareProposal(ctx context.Context,
log.Debugf("transaction %x has reached max attempts, remove from mempool", txInfo.DecodedTx.TxID)
app.MempoolDeleteTx(txInfo.DecodedTx.TxID)
app.txReferences.Delete(txInfo.DecodedTx.TxID)
} else {
app.txReferences.Store(txInfo.DecodedTx.TxID, val)
}
}
log.Warnw("transaction reference not found on prepare proposal!",
"hash", fmt.Sprintf("%x", txInfo.DecodedTx.TxID),
)
continue
}
validTxs = append(validTxs, txInfo.Data)
Expand Down Expand Up @@ -459,23 +464,19 @@ func (app *BaseApplication) ProcessProposal(_ context.Context,
}

startTime := time.Now()

// TODO: check if we can enable this check without breaking consensus
//
// Check if the time difference is within the allowed threshold
// timeDiff := startTime.Sub(req.Time)
// if timeDiff > allowedConsensusTimeDiff {
// return nil, fmt.Errorf("the time difference for the proposal exceeds the threshold")
// }

resp, err := app.ExecuteBlock(req.Txs, uint32(req.GetHeight()), req.GetTime())
if err != nil {
return nil, fmt.Errorf("cannot execute block on process proposal: %w", err)
}
// invalid txx on a proposed block, should actually never happened if proposer acts honestly
// invalid tx on a proposed block, should actually never happened if proposer acts honestly
if len(resp.InvalidTransactions) > 0 {
log.Warnw("invalid transactions on process proposal", "height", app.Height(), "count", len(resp.InvalidTransactions),
"proposer", hex.EncodeToString(req.ProposerAddress), "action", "reject")
for _, tx := range resp.InvalidTransactions {
// remove transaction from mempool, just in case we have it
app.MempoolDeleteTx(tx)
log.Debugw("remove invalid tx from mempool", "hash", fmt.Sprintf("%x", tx))
}
return &cometabcitypes.ProcessProposalResponse{
Status: cometabcitypes.PROCESS_PROPOSAL_STATUS_REJECT,
}, nil
Expand Down

0 comments on commit 750a842

Please sign in to comment.