accounts/abi/bind/backends: replace NewSimulatedBackend with NewXDCSimulatedBackend

This commit is contained in:
Daniel Liu 2025-01-14 10:56:16 +08:00
parent b5f6104d5d
commit c1913bb22a
5 changed files with 57 additions and 28 deletions

View file

@ -144,7 +144,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi
return backend 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. // and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337. // A simulated backend always uses chainID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {

View file

@ -143,8 +143,10 @@ func TestNewSimulatedBackend(t *testing.T) {
func TestAdjustTime(t *testing.T) { func TestAdjustTime(t *testing.T) {
t.Parallel() t.Parallel()
sim := NewSimulatedBackend( sim := NewXDCSimulatedBackend(
core.GenesisAlloc{}, 10000000, core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
) )
defer sim.Close() defer sim.Close()
@ -223,8 +225,10 @@ func TestBalanceAt(t *testing.T) {
func TestBlockByHash(t *testing.T) { func TestBlockByHash(t *testing.T) {
t.Parallel() t.Parallel()
sim := NewSimulatedBackend( sim := NewXDCSimulatedBackend(
core.GenesisAlloc{}, 10000000, core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
) )
defer sim.Close() defer sim.Close()
bgCtx := context.Background() bgCtx := context.Background()
@ -245,8 +249,10 @@ func TestBlockByHash(t *testing.T) {
func TestBlockByNumber(t *testing.T) { func TestBlockByNumber(t *testing.T) {
t.Parallel() t.Parallel()
sim := NewSimulatedBackend( sim := NewXDCSimulatedBackend(
core.GenesisAlloc{}, 10000000, core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
) )
defer sim.Close() defer sim.Close()
bgCtx := context.Background() bgCtx := context.Background()
@ -439,7 +445,11 @@ func TestEstimateGas(t *testing.T) {
// opts := bind.NewKeyedTransactor(key) // opts := bind.NewKeyedTransactor(key)
opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) 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() defer sim.Close()
parsed, _ := abi.JSON(strings.NewReader(contractAbi)) parsed, _ := abi.JSON(strings.NewReader(contractAbi))
@ -546,7 +556,11 @@ func TestEstimateGasWithPrice(t *testing.T) {
key, _ := crypto.GenerateKey() key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey) 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() defer sim.Close()
recipient := common.HexToAddress("deadbeef") recipient := common.HexToAddress("deadbeef")
@ -893,9 +907,10 @@ func TestTransactionReceipt(t *testing.T) {
func TestSuggestGasPrice(t *testing.T) { func TestSuggestGasPrice(t *testing.T) {
t.Parallel() t.Parallel()
sim := NewSimulatedBackend( sim := NewXDCSimulatedBackend(
core.GenesisAlloc{}, core.GenesisAlloc{},
10000000, 10000000,
params.TestXDPoSMockChainConfig,
) )
defer sim.Close() defer sim.Close()
bgCtx := context.Background() bgCtx := context.Background()

View file

@ -73,6 +73,7 @@ func TestRandomize(t *testing.T) {
func TestSendTxRandomizeSecretAndOpening(t *testing.T) { func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}} genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
// TODO(daniel): replace NewSimulatedBackend with NewXDCSimulatedBackend
backend := backends.NewSimulatedBackend(genesis, 42000000) backend := backends.NewSimulatedBackend(genesis, 42000000)
backend.Commit() backend.Commit()
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}

View file

@ -12,6 +12,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
) )
var ( var (
@ -26,9 +27,13 @@ func TestPriceFeed(t *testing.T) {
common.TIPXDCXCancellationFee = big.NewInt(0) common.TIPXDCXCancellationFee = big.NewInt(0)
// init genesis // init genesis
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{ contractBackend := backends.NewXDCSimulatedBackend(
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))}, core.GenesisAlloc{
}, 42000000) mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
},
42000000,
params.TestXDPoSMockChainConfig,
)
transactOpts := bind.NewKeyedTransactor(mainKey) transactOpts := bind.NewKeyedTransactor(mainKey)
// deploy payer swap SMC // deploy payer swap SMC
addr, contract, err := DeployMyInherited(transactOpts, contractBackend) addr, contract, err := DeployMyInherited(transactOpts, contractBackend)

View file

@ -10,6 +10,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/params"
) )
var ( var (
@ -29,29 +30,35 @@ var (
) )
func TestFeeTxWithTRC21Token(t *testing.T) { func TestFeeTxWithTRC21Token(t *testing.T) {
// init genesis // init genesis
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{ contractBackend := backends.NewXDCSimulatedBackend(
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))}, core.GenesisAlloc{
}, 42000000) mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
},
42000000,
params.TestXDPoSMockChainConfig,
)
transactOpts := bind.NewKeyedTransactor(mainKey) transactOpts := bind.NewKeyedTransactor(mainKey)
// deploy payer swap SMC // deploy payer swap SMC
trc21IssuerAddr, trc21Issuer, err := DeployTRC21Issuer(transactOpts, contractBackend, minApply) trc21IssuerAddr, trc21Issuer, err := DeployTRC21Issuer(transactOpts, contractBackend, minApply)
//set contract address to config
common.TRC21IssuerSMC = trc21IssuerAddr
if err != nil { if err != nil {
t.Fatal("can't deploy smart contract: ", err) t.Fatal("can't deploy TRC21Issuer contract, err:", err)
} }
contractBackend.Commit() contractBackend.Commit()
// set contract address to config
common.TRC21IssuerSMC = trc21IssuerAddr
cap := big.NewInt(0).Mul(big.NewInt(10000000), big.NewInt(10000000000000)) cap := big.NewInt(0).Mul(big.NewInt(10000000), big.NewInt(10000000000000))
TRC21fee := big.NewInt(100) TRC21fee := big.NewInt(100)
// deploy a TRC21 SMC
// deploy a TRC21 SMC
trc21TokenAddr, trc21, err := DeployTRC21(transactOpts, contractBackend, "TEST", "XDC", 18, cap, TRC21fee) trc21TokenAddr, trc21, err := DeployTRC21(transactOpts, contractBackend, "TEST", "XDC", 18, cap, TRC21fee)
if err != nil { if err != nil {
t.Fatal("can't deploy smart contract: ", err) t.Fatal("can't deploy TRC21 contract, err:", err)
} }
contractBackend.Commit() contractBackend.Commit()
// add trc21 address to list token trc21Issuer // add trc21 address to list token trc21Issuer
trc21Issuer.TransactOpts.Value = minApply trc21Issuer.TransactOpts.Value = minApply
_, err = trc21Issuer.Apply(trc21TokenAddr) _, err = trc21Issuer.Apply(trc21TokenAddr)
@ -60,19 +67,20 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
} }
contractBackend.Commit() contractBackend.Commit()
//check trc21 SMC balance // check trc21 SMC balance
balance, err := contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil) balance, err := contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(minApply) != 0 { if err != nil || balance.Cmp(minApply) != 0 {
t.Fatal("can't get balance in trc21Issuer SMC: ", err, "got", balance, "wanted", minApply) 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) balanceIssuerFee, err := trc21Issuer.GetTokenCapacity(trc21TokenAddr)
if err != nil || balanceIssuerFee.Cmp(minApply) != 0 { if err != nil || balanceIssuerFee.Cmp(minApply) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", minApply) t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", minApply)
} }
trc21Issuer.TransactOpts.Value = big.NewInt(0) trc21Issuer.TransactOpts.Value = big.NewInt(0)
airDropAmount := big.NewInt(1000000000) airDropAmount := big.NewInt(1000000000)
// airdrop token trc21 to a address no XDC // airdrop token trc21 to a address no XDC
tx, err := trc21.Transfer(airdropAddr, airDropAmount) tx, err := trc21.Transfer(airdropAddr, airDropAmount)
if err != nil { if err != nil {
@ -100,7 +108,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
if balanceIssuerFee.Cmp(remainFee) != 0 { if balanceIssuerFee.Cmp(remainFee) != 0 {
t.Fatal("check balance token fee in smart contract: got", balanceIssuerFee, "wanted", remainFee) 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) balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(remainFee) != 0 { if err != nil || balance.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee) 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) fee = common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed)
remainFee = big.NewInt(0).Sub(remainFee, fee) remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee // check balance fee
balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr) balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr)
if err != nil || balanceIssuerFee.Cmp(remainFee) != 0 { if err != nil || balanceIssuerFee.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee) 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) balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(remainFee) != 0 { if err != nil || balance.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee) t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee)