Skip to content

Commit

Permalink
chore: Apply comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Jan 2, 2025
1 parent 5ec7fc9 commit d5e1897
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 0 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,6 @@ func (app *BaseApp) CheckTxSync(req *abci.RequestCheckTx) (*abci.ResponseCheckTx
return &abci.ResponseCheckTx{
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
// Log: result.Log,
// Data: result.Data,
// Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
}, nil
}

Expand Down
14 changes: 14 additions & 0 deletions baseapp/accountwgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@ func NewAccountWGs() *AccountWGs {
}
}

// Register registers transaction signers and returns necessary WaitGroups.
// It ensures that transactions from the same signer are processed sequentially.
//
// Parameters:
// - cdc: Codec for decoding transaction messages
// - tx: Transaction to be registered
//
// Returns:
// - waits: WaitGroups that need to be waited on (from previous transactions of the same signers)
// - signals: WaitGroups that should be signaled when this transaction completes
func (aw *AccountWGs) Register(cdc codec.Codec, tx sdk.Tx) (waits []*sync.WaitGroup, signals []*AccountWG) {
// Get unique signers from the tx
signers := getUniqSigners(cdc, tx)

aw.mtx.Lock()
defer aw.mtx.Unlock()
for _, signer := range signers {
// If there's an existing WaitGroup for this signer,
// we need to wait for it before processing the current transaction
if wg := aw.wgs[signer]; wg != nil {
waits = append(waits, wg)
}
// Create a new WaitGroup for the current transaction
sig := waitGroup1()
aw.wgs[signer] = sig
signals = append(signals, NewAccountWG(signer, sig))
Expand Down
9 changes: 9 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ func (app *BaseApp) setState(mode execMode, h cmtproto.Header) {

switch mode {
case execModeCheck:
// Acquire a lock to ensure thread-safe access to `checkState` during state updates.
// Since `CheckTxAsync` runs checkTx concurrently, this lock is necessary to
// prevent data races and ensure that updates to `checkState` remain consistent
// across all goroutines. Without this lock, concurrent modifications could
// lead to inconsistent application states or runtime errors.
app.checkStateMtx.Lock()
defer app.checkStateMtx.Unlock()
baseState.SetContext(baseState.Context().WithIsCheckTx(true).WithMinGasPrices(app.minGasPrices))
Expand Down Expand Up @@ -690,6 +695,10 @@ func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter {
// retrieve the context for the tx w/ txBytes and other memoized values.
func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte, txIndex int) sdk.Context {
if mode == execModeCheck || mode == execModeReCheck {
// By introducing Parallel CheckTx, now getContextForTx can be called by multiple goroutines.
// Without this lock, multiple goroutines could attempt to modify or read `checkState` simultaneously,
// leading to inconsistent or corrupted state.
// The lock guarantees a consistent context is maintained across all CheckTx operations.
app.checkStateMtx.Lock()
defer app.checkStateMtx.Unlock()
}
Expand Down

0 comments on commit d5e1897

Please sign in to comment.