go-ethereum/contracts/tests/Inherited_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

54 lines
1.5 KiB
Go

package tests
import (
"fmt"
"math/big"
"os"
"testing"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
)
var (
mainKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
mainAddr = crypto.PubkeyToAddress(mainKey.PublicKey)
)
func TestPriceFeed(t *testing.T) {
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
glogger.Verbosity(log.LevelTrace)
log.SetDefault(log.NewLogger(glogger))
oldTIPXDCXCancellationFee := new(big.Int).Set(common.TIPXDCXCancellationFee)
defer func() {
common.TIPXDCXCancellationFee = oldTIPXDCXCancellationFee
}()
common.TIPXDCXCancellationFee = big.NewInt(0)
// init genesis
contractBackend := backends.NewXDCSimulatedBackend(
types.GenesisAlloc{
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
},
42000000,
params.TestXDPoSMockChainConfig,
)
transactOpts := bind.NewKeyedTransactor(mainKey)
// deploy payer swap SMC
addr, contract, err := DeployMyInherited(transactOpts, contractBackend)
if err != nil {
t.Fatal("can't deploy smart contract: ", err)
}
fmt.Println("addr", addr.Hex())
tx, err := contract.Foo()
if err != nil {
t.Fatal("can't run function Foo() in smart contract: ", err)
}
fmt.Println("tx", tx)
}