Commit 04f37a0d authored by QM's avatar QM

uptdata test.go

parent 4d38b10e
......@@ -144,5 +144,5 @@ func bindOrResumePush(cfg *relayerTypes.SyncTxReceiptConfig) {
panic("bindOrResumePush client failed due to:" + string(res.Msg))
}
log.Info("bindOrResumePush", "Succeed to AddSubscribeTxReceipt for rpc address:", cfg.Chain33Host)
fmt.Println("Succeed to AddSubscribeTxReceipt")
fmt.Println("Succeed to AddPushSubscribe")
}
......@@ -4,20 +4,20 @@ import (
"context"
"encoding/hex"
"flag"
"fmt"
"github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/util/testnode"
//"github.com/ethereum/go-ethereum/core/types"
"github.com/influxdata/influxdb/pkg/testing/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"math/big"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/util/testnode"
"github.com/stretchr/testify/mock"
dbm "github.com/33cn/chain33/common/db"
_ "github.com/33cn/chain33/system"
chain33Types "github.com/33cn/chain33/types"
......@@ -32,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"
)
var (
......@@ -49,318 +48,339 @@ var (
ethValidatorAddrKeyB = "a5f3063552f4483cfc20ac4f40f45b798791379862219de9e915c64722c1d400"
ethValidatorAddrKeyC = "bbf5e65539e9af0eb0cfac30bad475111054b09c11d668fc0731d54ea777471e"
ethValidatorAddrKeyD = "c9fa31d7984edf81b8ef3b40c761f1847f6fcd5711ab2462da97dc458f1f896b"
)
type suiteEthRelayer struct {
suite.Suite
ethRelayer *Relayer4Ethereum
sim *ethinterface.SimExtend
x2EthContracts *ethtxs.X2EthContracts
x2EthDeployInfo *ethtxs.X2EthDeployInfo
para *ethtxs.DeployPara
backend bind.ContractBackend
}
func TestRunSuiteX2Ethereum(t *testing.T) {
var tx chain33Types.Transaction
var ret chain33Types.Reply
ret.IsOk = true
)
mockapi := &mocks.QueueProtocolAPI{}
// 这里对需要mock的方法打桩,Close是必须的,其它方法根据需要
mockapi.On("Close").Return()
mockapi.On("CreateTransaction", mock.Anything).Return(&tx, nil)
mockapi.On("SendTx", mock.Anything).Return(&ret, nil)
mockapi.On("SendTransaction", mock.Anything).Return(&ret, nil)
mockapi.On("GetConfig", mock.Anything).Return(chainTestCfg, nil)
func init() {
deployContracts()
ethRelayer = newEthRelayer()
_ = ethRelayer.ImportChain33PrivateKey(passphrase, chain33PrivateKeyStr)
time.Sleep(time.Second)
}
mock33 := testnode.New("", mockapi)
defer mock33.Close()
rpcCfg := mock33.GetCfg().RPC
// 这里必须设置监听端口,默认的是无效值
rpcCfg.JrpcBindAddr = "127.0.0.1:8801"
mock33.GetRPC().Listen()
func Test_GetValidatorAddr(t *testing.T) {
_, _, err := ethRelayer.NewAccount("123")
assert.NoError(t, err)
log := new(suiteEthRelayer)
suite.Run(t, log)
}
privateKey, addr, err := ethRelayer.GetAccount("123")
assert.NoError(t, err)
assert.NotEqual(t, privateKey, chain33PrivateKeyStr)
func (r *suiteEthRelayer) SetupSuite() {
r.deployContracts()
r.ethRelayer = r.newEthRelayer()
privateKey, addr, err = ethRelayer.GetAccount(passphrase)
assert.NoError(t, err)
assert.Equal(t, privateKey, chain33PrivateKeyStr)
assert.Equal(t, addr, chain33AccountAddr)
_, err := r.ethRelayer.DeployContrcts()
r.Error(err)
validators, err := ethRelayer.GetValidatorAddr()
assert.NoError(t, err)
assert.Equal(t, validators.Chain33Validator, chain33AccountAddr)
}
func (r *suiteEthRelayer) Test_1_ImportPrivateKey() {
validators, err := r.ethRelayer.GetValidatorAddr()
r.Error(err)
r.Empty(validators)
func Test_IsValidatorActive(t *testing.T) {
ethRelayer.x2EthContracts = x2EthContracts
ethRelayer.x2EthDeployInfo = x2EthDeployInfo
_, _, err = r.ethRelayer.NewAccount("123")
r.NoError(err)
fmt.Println("ethRelayer.x2EthContracts.Valset", ethRelayer.x2EthContracts.Valset)
err = r.ethRelayer.ImportChain33PrivateKey(passphrase, chain33PrivateKeyStr)
r.NoError(err)
is, err := ethRelayer.IsValidatorActive("0x92c8b16afd6d423652559c6e266cbe1c29bfd84f")
assert.Equal(t, is, true)
assert.NoError(t, err)
privateKey, addr, err := r.ethRelayer.GetAccount("123")
r.NoError(err)
r.NotEqual(privateKey, chain33PrivateKeyStr)
is, err = ethRelayer.IsValidatorActive("0x0C05bA5c230fDaA503b53702aF1962e08D0C60BF")
assert.Equal(t, is, false)
assert.NoError(t, err)
privateKey, addr, err = r.ethRelayer.GetAccount(passphrase)
r.NoError(err)
r.Equal(privateKey, chain33PrivateKeyStr)
r.Equal(addr, chain33AccountAddr)
validators, err = r.ethRelayer.GetValidatorAddr()
r.NoError(err)
r.Equal(validators.Chain33Validator, chain33AccountAddr)
is, err = ethRelayer.IsValidatorActive("123")
require.Error(t, err)
}
func (r *suiteEthRelayer) Test_2_IsValidatorActive() {
r.ethRelayer.x2EthContracts = r.x2EthContracts
r.ethRelayer.x2EthDeployInfo = r.x2EthDeployInfo
func Test_ShowAddr(t *testing.T) {
ethRelayer.prePareSubscribeEvent()
is, err := r.ethRelayer.IsValidatorActive("0x92c8b16afd6d423652559c6e266cbe1c29bfd84f")
r.Equal(is, true)
r.NoError(err)
addr, err := ethRelayer.ShowBridgeBankAddr()
assert.NoError(t, err)
assert.Equal(t, addr, x2EthDeployInfo.BridgeBank.Address.String())
is, err = r.ethRelayer.IsValidatorActive("0x0C05bA5c230fDaA503b53702aF1962e08D0C60BF")
r.Equal(is, false)
r.NoError(err)
addr, err = ethRelayer.ShowBridgeRegistryAddr()
assert.NoError(t, err)
assert.Equal(t, addr, x2EthDeployInfo.BridgeRegistry.Address.String())
is, err = r.ethRelayer.IsValidatorActive("123")
r.Error(err)
addr, err = ethRelayer.ShowOperator()
assert.NoError(t, err)
assert.Equal(t, addr, para.Operator.String())
}
func (r *suiteEthRelayer) Test_3_Token() {
r.isChain33KeyImport()
r.ethRelayer.prePareSubscribeEvent()
addr, err := r.ethRelayer.ShowBridgeBankAddr()
r.NoError(err)
r.Equal(addr, r.x2EthDeployInfo.BridgeBank.Address.String())
func Test_CreateBridgeToken(t *testing.T) {
balance, err := ethRelayer.GetBalance("", para.InitValidators[0].String())
assert.NoError(t, err)
assert.Equal(t, balance, "10000000000")
addr, err = r.ethRelayer.ShowBridgeRegistryAddr()
r.NoError(err)
r.Equal(addr, r.x2EthDeployInfo.BridgeRegistry.Address.String())
tokenAddrbty, err := ethRelayer.CreateBridgeToken("BTY")
assert.NoError(t, err)
require.NotEmpty(t, tokenAddrbty)
sim.Commit()
addr, err = r.ethRelayer.ShowOperator()
r.NoError(err)
r.Equal(addr, r.para.Operator.String())
addr, err := ethRelayer.ShowTokenAddrBySymbol("BTY")
assert.NoError(t, err)
assert.Equal(t, addr, tokenAddrbty)
balance, err := r.ethRelayer.GetBalance("", r.para.InitValidators[0].String())
r.NoError(err)
r.Equal(balance, "10000000000")
decimals, err := ethRelayer.GetDecimals(tokenAddrbty)
assert.NoError(t, err)
assert.Equal(t, decimals, uint8(8))
tokenAddrbty, err := r.ethRelayer.CreateBridgeToken("bty")
r.NoError(err)
r.NotEmpty(tokenAddrbty)
r.sim.Commit()
_, err = ethRelayer.Burn(ethValidatorAddrKeyA, tokenAddrbty, chain33AccountAddr, "10")
require.Error(t, err)
addr, err = r.ethRelayer.ShowTokenAddrBySymbol("bty")
r.NoError(err)
r.Equal(addr, tokenAddrbty)
_, err = ethRelayer.BurnAsync(ethValidatorAddrKeyA, tokenAddrbty, chain33AccountAddr, "10")
require.Error(t, err)
}
tokenErc20Addr, err := r.ethRelayer.CreateERC20Token("testcc")
r.NoError(err)
r.NotEmpty(tokenErc20Addr)
r.sim.Commit()
func Test_CreateERC20Token(t *testing.T) {
tokenErc20Addr, err := ethRelayer.CreateERC20Token("testcc")
assert.NoError(t, err)
require.NotEmpty(t, tokenErc20Addr)
sim.Commit()
_, err = r.ethRelayer.MintERC20Token(tokenErc20Addr, r.para.Deployer.String(), "20000000000000")
r.NoError(err)
r.sim.Commit()
_, err = ethRelayer.MintERC20Token(tokenErc20Addr, para.Deployer.String(), "20000000000000")
assert.NoError(t, err)
sim.Commit()
balance, err = r.ethRelayer.ShowDepositStatics(tokenErc20Addr)
r.NoError(err)
r.Equal(balance, "20000000000000")
balance, err := ethRelayer.ShowDepositStatics(tokenErc20Addr)
assert.NoError(t, err)
assert.Equal(t, balance, "20000000000000")
//claimID := crypto.Keccak256Hash(big.NewInt(50).Bytes())
//ret, err = r.ethRelayer.IsProphecyPending(claimID)
//r.NoError(err)
//r.Equal(ret, false)
//ret, err = ethRelayer.IsProphecyPending(claimID)
//assert.NoError(t, err)
//assert.Equal(t,ret, false)
decimals, err := r.ethRelayer.GetDecimals(tokenAddrbty)
r.NoError(err)
r.Equal(decimals, uint8(8))
txhash, err := ethRelayer.TransferToken(tokenErc20Addr, hexutil.Encode(crypto.FromECDSA(para.DeployPrivateKey)), ethRelayer.deployInfo.ValidatorsAddr[0], "100")
assert.NoError(t, err)
sim.Commit()
_, err = r.ethRelayer.Burn(ethValidatorAddrKeyA, tokenAddrbty, chain33AccountAddr, "10")
r.Error(err)
_, err = ethRelayer.ShowTxReceipt(txhash)
assert.NoError(t, err)
_, err = r.ethRelayer.BurnAsync(ethValidatorAddrKeyA, tokenAddrbty, chain33AccountAddr, "10")
r.Error(err)
balance, err = ethRelayer.GetBalance(tokenErc20Addr, ethRelayer.deployInfo.ValidatorsAddr[0])
assert.NoError(t, err)
assert.Equal(t, balance, "100")
txhash, err := r.ethRelayer.TransferToken(tokenErc20Addr, hexutil.Encode(crypto.FromECDSA(r.para.DeployPrivateKey)), r.ethRelayer.deployInfo.ValidatorsAddr[0], "100")
r.NoError(err)
r.sim.Commit()
balance, err = ethRelayer.GetBalance(tokenErc20Addr, para.Deployer.String())
assert.NoError(t, err)
assert.Equal(t, balance, "19999999999900")
_, err = r.ethRelayer.ShowTxReceipt(txhash)
r.NoError(err)
tx1 := ethRelayer.QueryTxhashRelay2Eth()
require.Empty(t, tx1)
balance, err = r.ethRelayer.GetBalance(tokenErc20Addr, r.ethRelayer.deployInfo.ValidatorsAddr[0])
r.NoError(err)
r.Equal(balance, "100")
balance, err = r.ethRelayer.GetBalance(tokenErc20Addr, r.para.Deployer.String())
r.NoError(err)
r.Equal(balance, "19999999999900")
tx2 := ethRelayer.QueryTxhashRelay2Chain33()
require.Empty(t, tx2)
}
tx1 := r.ethRelayer.QueryTxhashRelay2Eth()
r.Empty(tx1)
func Test_LockEth(t *testing.T) {
var tx chain33Types.Transaction
var ret chain33Types.Reply
ret.IsOk = true
tx2 := r.ethRelayer.QueryTxhashRelay2Chain33()
r.Empty(tx2)
}
mockapi := &mocks.QueueProtocolAPI{}
// 这里对需要mock的方法打桩,Close是必须的,其它方法根据需要
mockapi.On("Close").Return()
mockapi.On("AddPushSubscribe", mock.Anything).Return(&ret, nil)
mockapi.On("CreateTransaction", mock.Anything).Return(&tx, nil)
mockapi.On("SendTx", mock.Anything).Return(&ret, nil)
mockapi.On("SendTransaction", mock.Anything).Return(&ret, nil)
mockapi.On("GetConfig", mock.Anything).Return(chainTestCfg, nil)
func (r *suiteEthRelayer) Test_4_LockEth() {
r.isChain33KeyImport()
mock33 := testnode.New("", mockapi)
defer mock33.Close()
rpcCfg := mock33.GetCfg().RPC
// 这里必须设置监听端口,默认的是无效值
rpcCfg.JrpcBindAddr = "127.0.0.1:8801"
mock33.GetRPC().Listen()
ctx := context.Background()
bridgeBankBalance, err := r.sim.BalanceAt(ctx, r.x2EthDeployInfo.BridgeBank.Address, nil)
r.NoError(err)
r.Equal(bridgeBankBalance.Int64(), int64(0))
bridgeBankBalance, err := sim.BalanceAt(ctx, x2EthDeployInfo.BridgeBank.Address, nil)
assert.NoError(t, err)
assert.Equal(t, bridgeBankBalance.Int64(), int64(0))
userOneAuth, err := ethtxs.PrepareAuth(r.sim, r.para.ValidatorPriKey[0], r.para.InitValidators[0])
r.NoError(err)
userOneAuth, err := ethtxs.PrepareAuth(sim, para.ValidatorPriKey[0], para.InitValidators[0])
assert.NoError(t, err)
//lock 50 eth
chain33Sender := []byte("14KEKbYtKKQm4wMthSK9J4La4nAiidGozt")
ethAmount := big.NewInt(50)
userOneAuth.Value = ethAmount
_, err = r.x2EthContracts.BridgeBank.Lock(userOneAuth, chain33Sender, common.Address{}, ethAmount)
r.NoError(err)
r.sim.Commit()
_, err = x2EthContracts.BridgeBank.Lock(userOneAuth, chain33Sender, common.Address{}, ethAmount)
assert.NoError(t, err)
sim.Commit()
bridgeBankBalance, err = r.sim.BalanceAt(ctx, r.x2EthDeployInfo.BridgeBank.Address, nil)
r.NoError(err)
r.Equal(bridgeBankBalance.Int64(), ethAmount.Int64())
bridgeBankBalance, err = sim.BalanceAt(ctx, x2EthDeployInfo.BridgeBank.Address, nil)
assert.NoError(t, err)
assert.Equal(t, bridgeBankBalance.Int64(), ethAmount.Int64())
for i := 0; i < 11; i++ {
r.sim.Commit()
sim.Commit()
}
time.Sleep(time.Duration(r.ethRelayer.fetchHeightPeriodMs) * time.Millisecond)
time.Sleep(time.Duration(ethRelayer.fetchHeightPeriodMs) * time.Millisecond)
balance, err := ethRelayer.ShowLockStatics("")
assert.NoError(t, err)
assert.Equal(t, balance, "50")
balance, err := r.ethRelayer.ShowLockStatics("")
r.NoError(err)
r.Equal(balance, "50")
time.Sleep(time.Second)
}
//func (r *suiteEthRelayer) Test_5_LockErc20() {
// r.isChain33KeyImport()
//func Test_LockErc20(t *testing.T) {
// var tx chain33Types.Transaction
// var ret chain33Types.Reply
// ret.IsOk = true
//
// tokenErc20Addr, err := r.ethRelayer.CreateERC20Token("testc")
// r.NoError(err)
// r.NotEmpty(tokenErc20Addr)
// r.sim.Commit()
// mockapi := &mocks.QueueProtocolAPI{}
// // 这里对需要mock的方法打桩,Close是必须的,其它方法根据需要
// mockapi.On("Close").Return()
// mockapi.On("AddPushSubscribe", mock.Anything).Return(&ret, nil)
// mockapi.On("CreateTransaction", mock.Anything).Return(&tx, nil)
// mockapi.On("SendTx", mock.Anything).Return(&ret, nil)
// mockapi.On("SendTransaction", mock.Anything).Return(&ret, nil)
// mockapi.On("GetConfig", mock.Anything).Return(chainTestCfg, nil)
//
// _, err = r.ethRelayer.MintERC20Token(tokenErc20Addr, r.para.Deployer.String(), "10000000000000")
// r.NoError(err)
// r.sim.Commit()
// mock33 := testnode.New("", mockapi)
// defer mock33.Close()
// rpcCfg := mock33.GetCfg().RPC
// // 这里必须设置监听端口,默认的是无效值
// rpcCfg.JrpcBindAddr = "127.0.0.1:8801"
// mock33.GetRPC().Listen()
//
// balance, err := r.ethRelayer.ShowDepositStatics(tokenErc20Addr)
// r.NoError(err)
// r.Equal(balance, "10000000000000")
// tokenErc20Addr, err := ethRelayer.CreateERC20Token("testc")
// assert.NoError(t, err)
// require.NotEmpty(t, tokenErc20Addr)
// sim.Commit()
//
// chain33Receiver := "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
// _, err = r.ethRelayer.LockEthErc20Asset(hexutil.Encode(crypto.FromECDSA(r.para.DeployPrivateKey)), tokenErc20Addr, "100", chain33Receiver)
// r.NoError(err)
// r.sim.Commit()
// fmt.Println("tokenErc20Addr**************", tokenErc20Addr)
//
// balance, err = r.ethRelayer.GetBalance(tokenErc20Addr, r.para.Deployer.String())
// r.NoError(err)
// r.Equal(balance, "9999999999900")
// _, err = ethRelayer.MintERC20Token(tokenErc20Addr, para.Deployer.String(), "10000000000000")
// assert.NoError(t, err)
// sim.Commit()
//
// _, err = r.ethRelayer.ApproveAllowance(hexutil.Encode(crypto.FromECDSA(r.para.DeployPrivateKey)), tokenErc20Addr, "500")
// r.NoError(err)
// r.sim.Commit()
// balance, err := ethRelayer.ShowDepositStatics(tokenErc20Addr)
// assert.NoError(t, err)
// assert.Equal(t, balance, "10000000000000")
//
// _, err = r.ethRelayer.LockEthErc20AssetAsync(hexutil.Encode(crypto.FromECDSA(r.para.DeployPrivateKey)), tokenErc20Addr, "100", chain33Receiver)
// r.NoError(err)
// r.sim.Commit()
// chain33Receiver := "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
// _, err = ethRelayer.LockEthErc20Asset(hexutil.Encode(crypto.FromECDSA(para.DeployPrivateKey)), tokenErc20Addr, "100", chain33Receiver)
// assert.NoError(t, err)
// sim.Commit()
//
// balance, err = r.ethRelayer.GetBalance(tokenErc20Addr, r.para.Deployer.String())
// r.NoError(err)
// r.Equal(balance, "9999999999800")
// balance, err = ethRelayer.GetBalance(tokenErc20Addr, para.Deployer.String())
// assert.NoError(t, err)
// assert.Equal(t, balance, "9999999999900")
//
// //ret, err := r.ethRelayer.ApproveAllowance(hexutil.Encode(crypto.FromECDSA(r.para.DeployPrivateKey)), tokenErc20Addr, "20000000000000")
// //fmt.Println("?????", ret, err)
// //r.Error(err)
// _, err = ethRelayer.ApproveAllowance(hexutil.Encode(crypto.FromECDSA(para.DeployPrivateKey)), tokenErc20Addr, "500")
// assert.NoError(t, err)
// sim.Commit()
//
// //ctx := context.Background()
// //bridgeBankBalance, err := r.sim.BalanceAt(ctx, r.x2EthDeployInfo.BridgeBank.Address, nil)
// //r.NoError(err)
// //r.Equal(bridgeBankBalance.Int64(), int64(0))
// //
// //userOneAuth, err := ethtxs.PrepareAuth(r.sim, r.para.DeployPrivateKey, r.para.Deployer)
// //r.NoError(err)
// //
// //allowAmount := int64(100)
// //erc20TokenInstance, err := generated.NewBridgeToken(common.HexToAddress(tokenErc20Addr), r.ethRelayer.clientSpec)
// //r.NoError(err)
// //
// //_, err = erc20TokenInstance.Approve(userOneAuth, r.ethRelayer.x2EthDeployInfo.BridgeBank.Address, big.NewInt(allowAmount))
// //r.NoError(err)
// //r.sim.Commit()
// //
// ////lock 100 testc
// //chain33Receiver := []byte("14KEKbYtKKQm4wMthSK9J4La4nAiidGozt")
// //lockAmount := big.NewInt(100)
// //userOneAuth, err = ethtxs.PrepareAuth(r.sim, r.para.DeployPrivateKey, r.para.Deployer)
// //r.NoError(err)
// //
// //_, err = r.x2EthContracts.BridgeBank.Lock(userOneAuth, chain33Receiver, common.HexToAddress(tokenErc20Addr), lockAmount)
// //r.NoError(err)
// //r.sim.Commit()
// _, err = ethRelayer.LockEthErc20AssetAsync(hexutil.Encode(crypto.FromECDSA(para.DeployPrivateKey)), tokenErc20Addr, "100", chain33Receiver)
// assert.NoError(t, err)
// sim.Commit()
//
// balance, err = ethRelayer.GetBalance(tokenErc20Addr, para.Deployer.String())
// assert.NoError(t, err)
// assert.Equal(t, balance, "9999999999800")
// //
// //balance, err = r.ethRelayer.ShowLockStatics(tokenErc20Addr)
// //r.NoError(err)
// //r.Equal(balance, "100")
// //for i := 0; i < 11; i++ {
// // sim.Commit()
// //}
// //time.Sleep(time.Duration(ethRelayer.fetchHeightPeriodMs) * time.Millisecond)
//}
//func Test_BurnBty(t *testing.T) {
// tokenAddrbty, err := ethRelayer.CreateBridgeToken("bty")
// assert.NoError(t, err)
// require.NotEmpty(t, tokenAddrbty)
// sim.Commit()
//
// chain33Sender := []byte("14KEKbYtKKQm4wMthSK9J4La4nAiidGozt")
// amount := int64(100)
// ethReceiver := para.InitValidators[2]
// claimID := crypto.Keccak256Hash(chain33Sender, ethReceiver.Bytes(), big.NewInt(amount).Bytes())
//
// authOracle, err := ethtxs.PrepareAuth(ethRelayer.clientSpec, para.ValidatorPriKey[0], para.InitValidators[0])
// require.Nil(t, err)
// signature, err := ethtxs.SignClaim4Eth(claimID, para.ValidatorPriKey[0])
// require.Nil(t, err)
//
// _, err = x2EthContracts.Oracle.NewOracleClaim(
// authOracle,
// events.ClaimTypeLock,
// chain33Sender,
// ethReceiver,
// common.HexToAddress(tokenAddrbty),
// "bty",
// big.NewInt(amount),
// claimID,
// signature)
// require.Nil(t, err)
// sim.Commit()
//
// balanceNew, err := ethRelayer.GetBalance(tokenAddrbty, ethReceiver.String())
// require.Nil(t, err)
// require.Equal(t, balanceNew, "100")
//
// _, err = ethRelayer.Burn(hexutil.Encode(crypto.FromECDSA(para.ValidatorPriKey[2])), tokenAddrbty, chain33AccountAddr, "10")
// require.NoError(t, err)
// sim.Commit()
//
// balanceNew, err = ethRelayer.GetBalance(tokenAddrbty, ethReceiver.String())
// require.Nil(t, err)
// require.Equal(t, balanceNew, "90")
//
// for i := 0; i < 11; i++ {
// r.sim.Commit()
// }
// time.Sleep(time.Duration(r.ethRelayer.fetchHeightPeriodMs) * time.Millisecond)
// _, err = ethRelayer.ApproveAllowance(hexutil.Encode(crypto.FromECDSA(para.ValidatorPriKey[2])), tokenAddrbty, "10")
// assert.NoError(t, err)
// sim.Commit()
//
// _, err = ethRelayer.BurnAsync(hexutil.Encode(crypto.FromECDSA(para.ValidatorPriKey[2])), tokenAddrbty, chain33AccountAddr, "10")
// require.NoError(t, err)
// sim.Commit()
//
// balanceNew, err = ethRelayer.GetBalance(tokenAddrbty, ethReceiver.String())
// require.Nil(t, err)
// require.Equal(t, balanceNew, "80")
//}
func (r *suiteEthRelayer) Test_6_RestorePrivateKeys() {
func Test_RestorePrivateKeys(t *testing.T) {
go func() {
for range r.ethRelayer.unlockchan {
for range ethRelayer.unlockchan {
}
}()
temp := r.ethRelayer.privateKey4Chain33
temp := ethRelayer.privateKey4Chain33
err := r.ethRelayer.RestorePrivateKeys("123")
r.NotEqual(hex.EncodeToString(temp.Bytes()), hex.EncodeToString(r.ethRelayer.privateKey4Chain33.Bytes()))
r.NoError(err)
err := ethRelayer.RestorePrivateKeys("123")
assert.NotEqual(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
assert.NoError(t, err)
err = r.ethRelayer.RestorePrivateKeys(passphrase)
r.Equal(hex.EncodeToString(temp.Bytes()), hex.EncodeToString(r.ethRelayer.privateKey4Chain33.Bytes()))
r.NoError(err)
err = ethRelayer.RestorePrivateKeys(passphrase)
assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
assert.NoError(t, err)
err = r.ethRelayer.StoreAccountWithNewPassphase("new123", passphrase)
r.NoError(err)
err = ethRelayer.StoreAccountWithNewPassphase("new123", passphrase)
assert.NoError(t, err)
err = r.ethRelayer.RestorePrivateKeys("new123")
r.Equal(hex.EncodeToString(temp.Bytes()), hex.EncodeToString(r.ethRelayer.privateKey4Chain33.Bytes()))
r.NoError(err)
err = ethRelayer.RestorePrivateKeys("new123")
assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
assert.NoError(t, err)
time.Sleep(time.Second)
}
func (r *suiteEthRelayer) isChain33KeyImport() {
_, err := r.ethRelayer.GetValidatorAddr()
if err != nil {
err = r.ethRelayer.ImportChain33PrivateKey(passphrase, chain33PrivateKeyStr)
r.NoError(err)
}
}
func (r *suiteEthRelayer) newEthRelayer() *Relayer4Ethereum {
func newEthRelayer() *Relayer4Ethereum {
cfg := initCfg(*configPath)
cfg.SyncTxConfig.Chain33Host = "http://127.0.0.1:8801"
cfg.BridgeRegistry = r.x2EthDeployInfo.BridgeRegistry.Address.String()
cfg.BridgeRegistry = x2EthDeployInfo.BridgeRegistry.Address.String()
cfg.SyncTxConfig.PushBind = "127.0.0.1:60000"
cfg.SyncTxConfig.FetchHeightPeriodMs = 50
cfg.SyncTxConfig.Dbdriver = "memdb"
......@@ -373,48 +393,37 @@ func (r *suiteEthRelayer) newEthRelayer() *Relayer4Ethereum {
db: db,
unlockchan: make(chan int, 2),
rpcURL2Chain33: cfg.SyncTxConfig.Chain33Host,
bridgeRegistryAddr: r.x2EthDeployInfo.BridgeRegistry.Address,
bridgeRegistryAddr: x2EthDeployInfo.BridgeRegistry.Address,
maturityDegree: cfg.EthMaturityDegree,
fetchHeightPeriodMs: cfg.EthBlockFetchPeriod,
deployInfo: cfg.Deploy,
}
_ = relayer.setBridgeRegistryAddr(cfg.BridgeRegistry)
registrAddrInDB, err := relayer.getBridgeRegistryAddr()
r.NoError(err)
r.Equal(registrAddrInDB, cfg.BridgeRegistry)
//_ = relayer.setBridgeRegistryAddr(cfg.BridgeRegistry)
//registrAddrInDB, err := relayer.getBridgeRegistryAddr()
//assert.NoError(t, err)
//assert.Equal(t, registrAddrInDB, cfg.BridgeRegistry)
relayer.eventLogIndex = relayer.getLastBridgeBankProcessedHeight()
relayer.initBridgeBankTx()
relayer.clientSpec = r.sim
relayer.clientSpec = sim
relayer.clientChainID = big.NewInt(1)
deployPrivateKey, err := crypto.ToECDSA(common.FromHex(relayer.deployInfo.DeployerPrivateKey))
deployPrivateKey, _ := crypto.ToECDSA(common.FromHex(relayer.deployInfo.DeployerPrivateKey))
deployerAddr := crypto.PubkeyToAddress(deployPrivateKey.PublicKey)
relayer.operatorInfo = &ethtxs.OperatorInfo{
PrivateKey: deployPrivateKey,
Address: deployerAddr,
}
relayer.deployPara = r.para
relayer.x2EthContracts = r.x2EthContracts
relayer.x2EthDeployInfo = r.x2EthDeployInfo
relayer.deployPara = para
relayer.x2EthContracts = x2EthContracts
relayer.x2EthDeployInfo = x2EthDeployInfo
go relayer.proc()
var wg sync.WaitGroup
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
go func() {
<-ch
//cancel()
wg.Wait()
os.Exit(0)
}()
return relayer
}
func (r *suiteEthRelayer) deployContracts() {
func deployContracts() {
ethValidatorAddrKeys := make([]string, 0)
ethValidatorAddrKeys = append(ethValidatorAddrKeys, ethValidatorAddrKeyA)
ethValidatorAddrKeys = append(ethValidatorAddrKeys, ethValidatorAddrKeyB)
......@@ -422,24 +431,25 @@ func (r *suiteEthRelayer) deployContracts() {
ethValidatorAddrKeys = append(ethValidatorAddrKeys, ethValidatorAddrKeyD)
ctx := context.Background()
r.backend, r.para = setup.PrepareTestEnvironment(deployerPrivateKey, ethValidatorAddrKeys)
r.sim = new(ethinterface.SimExtend)
r.sim.SimulatedBackend = r.backend.(*backends.SimulatedBackend)
var backend bind.ContractBackend
backend, para = setup.PrepareTestEnvironment(deployerPrivateKey, ethValidatorAddrKeys)
sim = new(ethinterface.SimExtend)
sim.SimulatedBackend = backend.(*backends.SimulatedBackend)
balance, _ := r.sim.BalanceAt(ctx, r.para.Deployer, nil)
r.Equal(balance.Int64(), int64(10000000000*10000))
//balance, _ := sim.BalanceAt(ctx, para.Deployer, nil)
//assert.Equal(t, balance.Int64(), int64(10000000000*10000))
callMsg := ethereum.CallMsg{
From: r.para.Deployer,
From: para.Deployer,
Data: common.FromHex(generated.BridgeBankBin),
}
_, err := r.sim.EstimateGas(ctx, callMsg)
r.NoError(err)
_, _ = sim.EstimateGas(ctx, callMsg)
//assert.NoError(t, err)
r.x2EthContracts, r.x2EthDeployInfo, err = ethtxs.DeployAndInit(r.sim, r.para)
r.NoError(err)
r.sim.Commit()
x2EthContracts, x2EthDeployInfo, _ = ethtxs.DeployAndInit(sim, para)
//assert.NoError(t, err)
sim.Commit()
}
func initCfg(path string) *relayerTypes.RelayerConfig {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment