go-ethereum/tests/vm_test.go
Daniel Liu 639531aae9
contracts, tests: fix global variable modifications and remove unsafe parallelism (#1882)
Add defer cleanup for global variables and remove t.Parallel() where
global state is modified to prevent race conditions and test pollution.

Changes:

1. tests/vm_test.go:
   - Add defer to restore common.TIPXDCXCancellationFee
   - Remove t.Parallel() since modifying global variable makes concurrent
     execution unsafe, even with defer cleanup
   - Prevents potential race conditions with other tests in the package

2. contracts/tests/Inherited_test.go:
   - Add defer to restore common.TIPXDCXCancellationFee
   - No t.Parallel() present, so safe with defer alone

3. contracts/trc21issuer/trc21issuer_test.go:
   - Add defer to restore common.TRC21IssuerSMC (was missing)
   - Fix existing bug: restore correct variable common.TRC21GasPriceBefore
     instead of common.TIPTRC21Fee
   - Remove unused variables: token, delay

Rationale for removing t.Parallel():
While defer ensures cleanup after test completion, during test execution
the modified global variable is visible to all concurrent tests. Even
though tests/vm_test.go is currently the only test modifying
TIPXDCXCancellationFee, removing t.Parallel() is the safer approach to
ensure complete test isolation and prevent timing-dependent behavior.

This follows the same principles as the txpool MinGasPrice fix: global
variable modifications should not occur during concurrent test execution.
2025-12-22 12:00:39 +05:30

46 lines
1.6 KiB
Go

// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package tests
import (
"math/big"
"testing"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/vm"
)
func TestVM(t *testing.T) {
oldTIPXDCXCancellationFee := new(big.Int).Set(common.TIPXDCXCancellationFee)
defer func() {
common.TIPXDCXCancellationFee = oldTIPXDCXCancellationFee
}()
common.TIPXDCXCancellationFee = big.NewInt(100000000)
vmt := new(testMatcher)
vmt.fails("^vmSystemOperationsTest.json/createNameRegistrator$", "fails without parallel execution")
vmt.skipLoad(`^vmInputLimits(Light)?.json`) // log format broken
vmt.skipShortMode("^vmPerformanceTest.json")
vmt.skipShortMode("^vmInputLimits(Light)?.json")
vmt.walk(t, vmTestDir, func(t *testing.T, name string, test *VMTest) {
withTrace(t, test.json.Exec.GasLimit, func(vmconfig vm.Config) error {
return vmt.checkFailure(t, name, test.Run(vmconfig))
})
})
}