-
Notifications
You must be signed in to change notification settings - Fork 112
fix!: Fix race condition in mempool blockchain impl #656
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d343fdc
Fix race condition in mempool blockchain impl
Eric-Warehime ac1db20
Add test for race condition
Eric-Warehime 880e008
Update changelog
Eric-Warehime 80a7c9f
Fix lint
Eric-Warehime a12c21d
Merge branch 'main' into eric/fix-mempool-race-condition
aljo242 9fd37e7
Update changelog entry
Eric-Warehime 8ef50dd
Merge branch 'main' into eric/fix-mempool-race-condition
Eric-Warehime b6ef7ae
Update test
Eric-Warehime 8412abb
Add timeout to systemtests
Eric-Warehime 2ca080e
Revert "Add timeout to systemtests"
Eric-Warehime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| //go:build test | ||
| // +build test | ||
Eric-Warehime marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| package mempool_test | ||
|
|
||
| import ( | ||
| "math/big" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
| "github.com/cosmos/evm/mempool" | ||
| "github.com/cosmos/evm/testutil/config" | ||
|
|
||
| "cosmossdk.io/log" | ||
| storetypes "cosmossdk.io/store/types" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/cosmos/evm/mempool/mocks" | ||
| "github.com/cosmos/evm/x/vm/statedb" | ||
| vmtypes "github.com/cosmos/evm/x/vm/types" | ||
| ) | ||
|
|
||
| // createMockContext creates a basic mock context for testing | ||
| func createMockContext() sdk.Context { | ||
| return sdk.Context{}. | ||
| WithBlockTime(time.Now()). | ||
| WithBlockHeader(cmtproto.Header{AppHash: []byte("00000000000000000000000000000000")}). | ||
| WithBlockHeight(1) | ||
| } | ||
|
|
||
| // TestBlockchainRaceCondition tests concurrent access to NotifyNewBlock and StateAt | ||
| // to ensure there are no race conditions between these operations. | ||
| func TestBlockchainRaceCondition(t *testing.T) { | ||
| logger := log.NewNopLogger() | ||
|
|
||
| // Create mock keepers using generated mocks | ||
| mockVMKeeper := mocks.NewVmKeeper(t) | ||
| mockFeeMarketKeeper := mocks.NewFeeMarketKeeper(t) | ||
|
|
||
| // Set up mock expectations for methods that will be called | ||
| mockVMKeeper.On("GetBaseFee", mock.Anything).Return(big.NewInt(1000000000)).Maybe() // 1 gwei | ||
| mockFeeMarketKeeper.On("GetBlockGasWanted", mock.Anything).Return(uint64(10000000)).Maybe() // 10M gas | ||
| mockVMKeeper.On("GetParams", mock.Anything).Return(vmtypes.DefaultParams()).Maybe() | ||
| mockVMKeeper.On("GetAccount", mock.Anything, common.Address{}).Return(&statedb.Account{}).Maybe() | ||
| mockVMKeeper.On("GetState", mock.Anything, common.Address{}, common.Hash{}).Return(common.Hash{}).Maybe() | ||
| mockVMKeeper.On("GetCode", mock.Anything, common.Hash{}).Return([]byte{}).Maybe() | ||
| mockVMKeeper.On("GetCodeHash", mock.Anything, common.Address{}).Return(common.Hash{}).Maybe() | ||
| mockVMKeeper.On("ForEachStorage", mock.Anything, common.Address{}, mock.AnythingOfType("func(common.Hash, common.Hash) bool")).Maybe() | ||
| mockVMKeeper.On("KVStoreKeys").Return(make(map[string]*storetypes.KVStoreKey)).Maybe() | ||
|
|
||
| err := vmtypes.NewEVMConfigurator().WithEVMCoinInfo(config.TestChainsCoinInfo[config.EVMChainID]).Configure() | ||
| require.NoError(t, err) | ||
|
|
||
| // Mock context callback that returns a valid context | ||
| getCtxCallback := func(height int64, prove bool) (sdk.Context, error) { | ||
| return createMockContext(), nil | ||
| } | ||
|
|
||
| blockchain := mempool.NewBlockchain( | ||
| getCtxCallback, | ||
| logger, | ||
| mockVMKeeper, | ||
| mockFeeMarketKeeper, | ||
| 21000000, // block gas limit | ||
| ) | ||
|
|
||
| const numIterations = 100 | ||
| var wg sync.WaitGroup | ||
|
|
||
| // Channel to collect any errors from goroutines | ||
| errChan := make(chan error, numIterations*2) | ||
|
|
||
| // Start goroutine that calls NotifyNewBlock repeatedly | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < numIterations; i++ { | ||
| blockchain.NotifyNewBlock() | ||
| // Small delay to allow interleaving | ||
| time.Sleep(time.Microsecond) | ||
| } | ||
| }() | ||
|
|
||
| // Start goroutine that calls StateAt repeatedly | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < numIterations; i++ { | ||
| hash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") | ||
| _, err := blockchain.StateAt(hash) | ||
| if err != nil { | ||
| errChan <- err | ||
| return | ||
| } | ||
| // Small delay to allow interleaving | ||
| time.Sleep(time.Microsecond) | ||
| } | ||
| }() | ||
|
|
||
| // Wait for both goroutines to complete | ||
| wg.Wait() | ||
| close(errChan) | ||
|
|
||
| // Check for any errors | ||
| for err := range errChan { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // Basic validation - ensure blockchain still functions correctly after concurrent access | ||
| header := blockchain.CurrentBlock() | ||
| require.NotNil(t, header) | ||
| require.Equal(t, int64(1), header.Number.Int64()) | ||
|
|
||
| // Ensure StateAt still works after concurrent access | ||
| hash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") | ||
| stateDB, err := blockchain.StateAt(hash) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, stateDB) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.