From c1913bb22a074cc7b9b98c9e68042b40cb449faa Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:16 +0800 Subject: [PATCH] accounts/abi/bind/backends: replace NewSimulatedBackend with NewXDCSimulatedBackend --- accounts/abi/bind/backends/simulated.go | 2 +- accounts/abi/bind/backends/simulated_test.go | 33 ++++++++++++----- contracts/randomize/randomize_test.go | 1 + contracts/tests/Inherited_test.go | 11 ++++-- contracts/trc21issuer/trc21issuer_test.go | 38 ++++++++++++-------- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 9fde5ce863..e24636ac68 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -144,7 +144,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi return backend } -// NewSimulatedBackend creates a new binding backend based on the given database +// SimulOldNewSimulatedBackendatedBackend creates a new binding backend based on the given database // and uses a simulated blockchain for testing purposes. // A simulated backend always uses chainID 1337. func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { diff --git a/accounts/abi/bind/backends/simulated_test.go b/accounts/abi/bind/backends/simulated_test.go index 4ffdf7a77f..b8848a6e8d 100644 --- a/accounts/abi/bind/backends/simulated_test.go +++ b/accounts/abi/bind/backends/simulated_test.go @@ -143,8 +143,10 @@ func TestNewSimulatedBackend(t *testing.T) { func TestAdjustTime(t *testing.T) { t.Parallel() - sim := NewSimulatedBackend( - core.GenesisAlloc{}, 10000000, + sim := NewXDCSimulatedBackend( + core.GenesisAlloc{}, + 10000000, + params.TestXDPoSMockChainConfig, ) defer sim.Close() @@ -223,8 +225,10 @@ func TestBalanceAt(t *testing.T) { func TestBlockByHash(t *testing.T) { t.Parallel() - sim := NewSimulatedBackend( - core.GenesisAlloc{}, 10000000, + sim := NewXDCSimulatedBackend( + core.GenesisAlloc{}, + 10000000, + params.TestXDPoSMockChainConfig, ) defer sim.Close() bgCtx := context.Background() @@ -245,8 +249,10 @@ func TestBlockByHash(t *testing.T) { func TestBlockByNumber(t *testing.T) { t.Parallel() - sim := NewSimulatedBackend( - core.GenesisAlloc{}, 10000000, + sim := NewXDCSimulatedBackend( + core.GenesisAlloc{}, + 10000000, + params.TestXDPoSMockChainConfig, ) defer sim.Close() bgCtx := context.Background() @@ -439,7 +445,11 @@ func TestEstimateGas(t *testing.T) { // opts := bind.NewKeyedTransactor(key) opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) - sim := NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether)}}, 10000000, params.TestXDPoSMockChainConfig) + sim := NewXDCSimulatedBackend( + core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether)}}, + 10000000, + params.TestXDPoSMockChainConfig, + ) defer sim.Close() parsed, _ := abi.JSON(strings.NewReader(contractAbi)) @@ -546,7 +556,11 @@ func TestEstimateGasWithPrice(t *testing.T) { key, _ := crypto.GenerateKey() addr := crypto.PubkeyToAddress(key.PublicKey) - sim := NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether*2 + 2e17)}}, 10000000) + sim := NewXDCSimulatedBackend( + core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether*2 + 2e17)}}, + 10000000, + params.TestXDPoSMockChainConfig, +) defer sim.Close() recipient := common.HexToAddress("deadbeef") @@ -893,9 +907,10 @@ func TestTransactionReceipt(t *testing.T) { func TestSuggestGasPrice(t *testing.T) { t.Parallel() - sim := NewSimulatedBackend( + sim := NewXDCSimulatedBackend( core.GenesisAlloc{}, 10000000, + params.TestXDPoSMockChainConfig, ) defer sim.Close() bgCtx := context.Background() diff --git a/contracts/randomize/randomize_test.go b/contracts/randomize/randomize_test.go index fd117681f1..08c38e375d 100644 --- a/contracts/randomize/randomize_test.go +++ b/contracts/randomize/randomize_test.go @@ -73,6 +73,7 @@ func TestRandomize(t *testing.T) { func TestSendTxRandomizeSecretAndOpening(t *testing.T) { genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}} + // TODO(daniel): replace NewSimulatedBackend with NewXDCSimulatedBackend backend := backends.NewSimulatedBackend(genesis, 42000000) backend.Commit() signer := types.HomesteadSigner{} diff --git a/contracts/tests/Inherited_test.go b/contracts/tests/Inherited_test.go index a60fa6344f..da9178ad1b 100644 --- a/contracts/tests/Inherited_test.go +++ b/contracts/tests/Inherited_test.go @@ -12,6 +12,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/log" + "github.com/XinFinOrg/XDPoSChain/params" ) var ( @@ -26,9 +27,13 @@ func TestPriceFeed(t *testing.T) { common.TIPXDCXCancellationFee = big.NewInt(0) // init genesis - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{ - mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))}, - }, 42000000) + contractBackend := backends.NewXDCSimulatedBackend( + core.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) diff --git a/contracts/trc21issuer/trc21issuer_test.go b/contracts/trc21issuer/trc21issuer_test.go index aeebb7b7ad..04d98f41fb 100644 --- a/contracts/trc21issuer/trc21issuer_test.go +++ b/contracts/trc21issuer/trc21issuer_test.go @@ -10,6 +10,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/crypto" + "github.com/XinFinOrg/XDPoSChain/params" ) var ( @@ -29,29 +30,35 @@ var ( ) func TestFeeTxWithTRC21Token(t *testing.T) { - // init genesis - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{ - mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))}, - }, 42000000) + contractBackend := backends.NewXDCSimulatedBackend( + core.GenesisAlloc{ + mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))}, + }, + 42000000, + params.TestXDPoSMockChainConfig, + ) transactOpts := bind.NewKeyedTransactor(mainKey) + // deploy payer swap SMC trc21IssuerAddr, trc21Issuer, err := DeployTRC21Issuer(transactOpts, contractBackend, minApply) - - //set contract address to config - common.TRC21IssuerSMC = trc21IssuerAddr if err != nil { - t.Fatal("can't deploy smart contract: ", err) + t.Fatal("can't deploy TRC21Issuer contract, err:", err) } contractBackend.Commit() + + // set contract address to config + common.TRC21IssuerSMC = trc21IssuerAddr cap := big.NewInt(0).Mul(big.NewInt(10000000), big.NewInt(10000000000000)) TRC21fee := big.NewInt(100) - // deploy a TRC21 SMC + + // deploy a TRC21 SMC trc21TokenAddr, trc21, err := DeployTRC21(transactOpts, contractBackend, "TEST", "XDC", 18, cap, TRC21fee) if err != nil { - t.Fatal("can't deploy smart contract: ", err) + t.Fatal("can't deploy TRC21 contract, err:", err) } contractBackend.Commit() + // add trc21 address to list token trc21Issuer trc21Issuer.TransactOpts.Value = minApply _, err = trc21Issuer.Apply(trc21TokenAddr) @@ -60,19 +67,20 @@ func TestFeeTxWithTRC21Token(t *testing.T) { } contractBackend.Commit() - //check trc21 SMC balance + // check trc21 SMC balance balance, err := contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil) if err != nil || balance.Cmp(minApply) != 0 { t.Fatal("can't get balance in trc21Issuer SMC: ", err, "got", balance, "wanted", minApply) } - //check balance fee + // check balance fee balanceIssuerFee, err := trc21Issuer.GetTokenCapacity(trc21TokenAddr) if err != nil || balanceIssuerFee.Cmp(minApply) != 0 { t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", minApply) } trc21Issuer.TransactOpts.Value = big.NewInt(0) airDropAmount := big.NewInt(1000000000) + // airdrop token trc21 to a address no XDC tx, err := trc21.Transfer(airdropAddr, airDropAmount) if err != nil { @@ -100,7 +108,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) { if balanceIssuerFee.Cmp(remainFee) != 0 { t.Fatal("check balance token fee in smart contract: got", balanceIssuerFee, "wanted", remainFee) } - //check trc21 SMC balance + // check trc21 SMC balance balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil) if err != nil || balance.Cmp(remainFee) != 0 { t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee) @@ -137,12 +145,12 @@ func TestFeeTxWithTRC21Token(t *testing.T) { } fee = common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed) remainFee = big.NewInt(0).Sub(remainFee, fee) - //check balance fee + // check balance fee balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr) if err != nil || balanceIssuerFee.Cmp(remainFee) != 0 { t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee) } - //check trc21 SMC balance + // check trc21 SMC balance balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil) if err != nil || balance.Cmp(remainFee) != 0 { t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee)