Skip to content

Commit

Permalink
refactor(test): eth address to tron address (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Feb 28, 2024
1 parent 5883b33 commit 3b5f94a
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 79 deletions.
19 changes: 4 additions & 15 deletions tests/crosschain_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
gethcommon "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
tronaddress "github.com/fbsobreira/gotron-sdk/pkg/address"

"github.com/functionx/fx-core/v7/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v7/types"
Expand Down Expand Up @@ -60,10 +59,8 @@ func (suite *CrosschainTestSuite) OracleAddr() sdk.AccAddress {
}

func (suite *CrosschainTestSuite) ExternalAddr() string {
if suite.chainName == trontypes.ModuleName {
return tronaddress.PubkeyToAddress(suite.externalPrivKey.PublicKey).String()
}
return ethcrypto.PubkeyToAddress(suite.externalPrivKey.PublicKey).String()
address := ethcrypto.PubkeyToAddress(suite.externalPrivKey.PublicKey)
return fxtypes.AddressToStr(address.Bytes(), suite.chainName)
}

func (suite *CrosschainTestSuite) BridgerAddr() sdk.AccAddress {
Expand All @@ -79,11 +76,7 @@ func (suite *CrosschainTestSuite) HexAddress() gethcommon.Address {
}

func (suite *CrosschainTestSuite) HexAddressString() string {
hexAddr := suite.HexAddress()
if suite.chainName == trontypes.ModuleName {
return trontypes.AddressFromHex(hexAddr.String())
}
return hexAddr.String()
return fxtypes.AddressToStr(suite.HexAddress().Bytes(), suite.chainName)
}

func (suite *CrosschainTestSuite) CrosschainQuery() crosschaintypes.QueryClient {
Expand Down Expand Up @@ -531,9 +524,5 @@ func (suite *CrosschainTestSuite) AddBridgeToken(md banktypes.Metadata) (string,
}

func (suite *CrosschainTestSuite) FormatAddress(address gethcommon.Address) string {
receive := address.String()
if suite.chainName == trontypes.ModuleName {
receive = trontypes.AddressFromHex(receive)
}
return receive
return fxtypes.AddressToStr(address.Bytes(), suite.chainName)
}
17 changes: 8 additions & 9 deletions testutil/helpers/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ func GenerateAddress() common.Address {
// GenerateAddressByModule generates an Ethereum or Tron address.
func GenerateAddressByModule(module string) string {
addr := GenerateAddress()
if module == "tron" {
return tronaddress.Address(append([]byte{tronaddress.TronBytePrefix}, addr.Bytes()...)).String()
}
return addr.String()
return fxtypes.AddressToStr(addr.Bytes(), module)
}

func AddressToBytesByModule(addr, module string) ([]byte, error) {
Expand All @@ -121,11 +118,13 @@ func AddressToBytesByModule(addr, module string) ([]byte, error) {

// GenerateZeroAddressByModule generates an Ethereum or Tron zero address.
func GenerateZeroAddressByModule(module string) string {
addr := common.HexToAddress(common.Address{}.Hex())
if module == "tron" {
return tronaddress.Address(append([]byte{tronaddress.TronBytePrefix}, addr.Bytes()...)).String()
}
return addr.String()
addr := common.Address{}
return fxtypes.AddressToStr(addr.Bytes(), module)
}

func HexAddrToTronAddr(str string) string {
addr := common.FromHex(str)
return tronaddress.Address(append([]byte{tronaddress.TronBytePrefix}, addr...)).String()
}

// NewPubKeyFromHex returns a PubKey from a hex string.
Expand Down
20 changes: 4 additions & 16 deletions x/crosschain/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
tronaddress "github.com/fbsobreira/gotron-sdk/pkg/address"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/functionx/fx-core/v7/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v7/types"
"github.com/functionx/fx-core/v7/x/crosschain"
"github.com/functionx/fx-core/v7/x/crosschain/types"
trontypes "github.com/functionx/fx-core/v7/x/tron/types"
)

func (suite *KeeperTestSuite) TestABCIEndBlockDepositClaim() {
Expand All @@ -35,12 +33,8 @@ func (suite *KeeperTestSuite) TestABCIEndBlockDepositClaim() {

suite.app.EndBlock(abci.RequestEndBlock{Height: suite.ctx.BlockHeight()})

bridgeToken := helpers.GenerateAddress().String()
sendToFxSendAddr := helpers.GenerateAddress().String()
if trontypes.ModuleName == suite.chainName {
bridgeToken = trontypes.AddressFromHex(bridgeToken)
sendToFxSendAddr = tronaddress.PubkeyToAddress(suite.externalPris[0].PublicKey).String()
}
bridgeToken := helpers.GenerateAddressByModule(suite.chainName)
sendToFxSendAddr := helpers.GenerateAddressByModule(suite.chainName)
addBridgeTokenClaim := &types.MsgBridgeTokenClaim{
EventNonce: 1,
BlockHeight: 1000,
Expand Down Expand Up @@ -111,10 +105,7 @@ func (suite *KeeperTestSuite) TestOracleUpdate() {
require.True(suite.T(), expectPower.Equal(power))
}

bridgeToken := helpers.GenerateAddress().String()
if trontypes.ModuleName == suite.chainName {
bridgeToken = trontypes.AddressFromHex(bridgeToken)
}
bridgeToken := helpers.GenerateAddressByModule(suite.chainName)

for i := 0; i < 6; i++ {
addBridgeTokenClaim := &types.MsgBridgeTokenClaim{
Expand Down Expand Up @@ -225,10 +216,7 @@ func (suite *KeeperTestSuite) TestAttestationAfterOracleUpdate() {
require.True(suite.T(), expectPower.Equal(power))
}

bridgeToken := helpers.GenerateAddress().String()
if trontypes.ModuleName == suite.chainName {
bridgeToken = trontypes.AddressFromHex(bridgeToken)
}
bridgeToken := helpers.GenerateAddressByModule(suite.chainName)

{
firstBridgeTokenClaim := &types.MsgBridgeTokenClaim{
Expand Down
6 changes: 2 additions & 4 deletions x/crosschain/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ func (suite *KeeperTestSuite) SetupTest() {
}

func (suite *KeeperTestSuite) PubKeyToExternalAddr(publicKey ecdsa.PublicKey) string {
if trontypes.ModuleName == suite.chainName {
return tronaddress.PubkeyToAddress(publicKey).String()
}
return crypto.PubkeyToAddress(publicKey).Hex()
address := crypto.PubkeyToAddress(publicKey)
return fxtypes.AddressToStr(address.Bytes(), suite.chainName)
}

func (suite *KeeperTestSuite) Commit(block ...int64) {
Expand Down
31 changes: 15 additions & 16 deletions x/tron/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/functionx/fx-core/v7/testutil/helpers"
"github.com/functionx/fx-core/v7/x/crosschain/types"
trontypes "github.com/functionx/fx-core/v7/x/tron/types"
)

func (suite *KeeperTestSuite) TestKeeper_BatchFees() {
Expand All @@ -27,7 +26,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchFees() {
request = &types.QueryBatchFeeRequest{
MinBatchFees: []types.MinBatchFee{
{
TokenContract: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
TokenContract: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
BaseFee: sdkmath.NewInt(-1),
},
},
Expand Down Expand Up @@ -62,7 +61,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchFees() {
_, err := suite.app.TronKeeper.AddToOutgoingPool(
suite.ctx,
suite.signer.AccAddress(),
trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))))
suite.Require().NoError(err)
Expand All @@ -71,7 +70,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchFees() {
_, err := suite.app.TronKeeper.AddToOutgoingPool(
suite.ctx,
suite.signer.AccAddress(),
trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(10)))))
suite.Require().NoError(err)
Expand Down Expand Up @@ -108,23 +107,23 @@ func (suite *KeeperTestSuite) TestKeeper_BatchFees() {
_, err := suite.app.TronKeeper.AddToOutgoingPool(
suite.ctx,
suite.signer.AccAddress(),
trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(10)))))
suite.Require().NoError(err)
}
_, err := suite.app.TronKeeper.AddToOutgoingPool(
suite.ctx,
suite.signer.AccAddress(),
trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))),
sdk.NewCoin(bridgeToken[0].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(100)))))
suite.Require().NoError(err)
for i := 1; i <= 3; i++ {
_, err = suite.app.TronKeeper.AddToOutgoingPool(
suite.ctx,
suite.signer.AccAddress(),
trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
sdk.NewCoin(bridgeToken[1].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e18), big.NewInt(100)))),
sdk.NewCoin(bridgeToken[1].Denom, sdkmath.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(1e18), big.NewInt(100)))))
suite.Require().NoError(err)
Expand Down Expand Up @@ -180,7 +179,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchRequestByNonce() {
name: "store normal batch",
malleate: func() {
bridgeToken := suite.NewBridgeToken(helpers.GenerateAddress().Bytes())
feeReceive := trontypes.AddressFromHex(helpers.GenerateAddress().Hex())
feeReceive := helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex())
request = &types.QueryBatchRequestByNonceRequest{
TokenContract: bridgeToken[0].Token,
Nonce: 3,
Expand Down Expand Up @@ -231,7 +230,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchRequestByNonce() {
name: "request error nonce",
malleate: func() {
request = &types.QueryBatchRequestByNonceRequest{
TokenContract: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
TokenContract: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Nonce: 0,
}
},
Expand Down Expand Up @@ -262,7 +261,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchRequestByNonce() {
name: "request nonexistent token",
malleate: func() {
request = &types.QueryBatchRequestByNonceRequest{
TokenContract: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
TokenContract: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Nonce: 8,
}
},
Expand Down Expand Up @@ -307,7 +306,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchConfirms() {
"token nonce is zero",
func() {
request = &types.QueryBatchConfirmsRequest{
TokenContract: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
TokenContract: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Nonce: 0,
}
},
Expand Down Expand Up @@ -341,7 +340,7 @@ func (suite *KeeperTestSuite) TestKeeper_BatchConfirms() {
Nonce: 1,
TokenContract: bridgeToken[0].Token,
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
Signature: helpers.GenerateAddress().Hex(),
}
suite.app.TronKeeper.SetBatchConfirm(suite.ctx, suite.signer.AccAddress(), newConfirmBatch)
Expand Down Expand Up @@ -392,7 +391,7 @@ func (suite *KeeperTestSuite) TestKeeper_TokenToDenom() {
name: "token that does not exist",
malleate: func() {
request = &types.QueryTokenToDenomRequest{
Token: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
Token: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
}
},
expPass: false,
Expand Down Expand Up @@ -466,7 +465,7 @@ func (suite *KeeperTestSuite) TestKeeper_GetOracleByExternalAddr() {
name: "nonexistent external address",
malleate: func() {
request = &types.QueryOracleByExternalAddrRequest{
ExternalAddress: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
ExternalAddress: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
}
},
expPass: false,
Expand All @@ -476,12 +475,12 @@ func (suite *KeeperTestSuite) TestKeeper_GetOracleByExternalAddr() {
malleate: func() {
oracle, bridger, externalKey := suite.NewOracleByBridger()
request = &types.QueryOracleByExternalAddrRequest{
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
}
response = &types.QueryOracleResponse{Oracle: &types.Oracle{
OracleAddress: oracle.String(),
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
DelegateAmount: sdkmath.ZeroInt(),
}}
},
Expand Down
13 changes: 6 additions & 7 deletions x/tron/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
fxtypes "github.com/functionx/fx-core/v7/types"
crosschaintypes "github.com/functionx/fx-core/v7/x/crosschain/types"
tronkeeper "github.com/functionx/fx-core/v7/x/tron/keeper"
trontypes "github.com/functionx/fx-core/v7/x/tron/types"
)

type KeeperTestSuite struct {
Expand Down Expand Up @@ -67,13 +66,13 @@ func (suite *KeeperTestSuite) SetupTest() {

func (suite *KeeperTestSuite) NewOutgoingTxBatch() *crosschaintypes.OutgoingTxBatch {
batchNonce := tmrand.Uint64()
tokenContract := trontypes.AddressFromHex(helpers.GenerateAddress().Hex())
tokenContract := helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex())
newOutgoingTx := &crosschaintypes.OutgoingTxBatch{
BatchNonce: batchNonce,
Transactions: []*crosschaintypes.OutgoingTransferTx{
{
Sender: suite.signer.AccAddress().String(),
DestAddress: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
DestAddress: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Token: crosschaintypes.ERC20Token{
Contract: tokenContract,
Amount: sdkmath.NewIntFromBigInt(big.NewInt(1e18)),
Expand All @@ -85,7 +84,7 @@ func (suite *KeeperTestSuite) NewOutgoingTxBatch() *crosschaintypes.OutgoingTxBa
},
},
TokenContract: tokenContract,
FeeReceive: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
FeeReceive: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Block: batchNonce,
}
err := suite.app.TronKeeper.StoreBatch(suite.ctx, newOutgoingTx)
Expand All @@ -97,7 +96,7 @@ func (suite *KeeperTestSuite) NewOracleByBridger() (sdk.AccAddress, sdk.AccAddre
oracle := sdk.AccAddress(helpers.GenerateAddress().Bytes())
bridger := sdk.AccAddress(helpers.GenerateAddress().Bytes())
externalKey := helpers.NewEthPrivKey()
externalAddress := trontypes.AddressFromHex(externalKey.PubKey().Address().String())
externalAddress := helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String())
newOracle := crosschaintypes.Oracle{
OracleAddress: oracle.String(),
BridgerAddress: bridger.String(),
Expand All @@ -116,7 +115,7 @@ func (suite *KeeperTestSuite) NewOracleSet(externalKey cryptotypes.PrivKey) *cro
newOracleSet := crosschaintypes.NewOracleSet(tmrand.Uint64(), tmrand.Uint64(), crosschaintypes.BridgeValidators{
{
Power: tmrand.Uint64(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
},
})
suite.app.TronKeeper.StoreOracleSet(suite.ctx, newOracleSet)
Expand All @@ -126,7 +125,7 @@ func (suite *KeeperTestSuite) NewOracleSet(externalKey cryptotypes.PrivKey) *cro
func (suite *KeeperTestSuite) NewBridgeToken(bridger sdk.AccAddress) []crosschaintypes.BridgeToken {
bridgeTokens := make([]crosschaintypes.BridgeToken, 0)
for i := 0; i < 3; i++ {
bridgeTokens = append(bridgeTokens, crosschaintypes.BridgeToken{Token: trontypes.AddressFromHex(helpers.GenerateAddress().Hex())})
bridgeTokens = append(bridgeTokens, crosschaintypes.BridgeToken{Token: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex())})
channelIBC := ""
if i == 2 {
channelIBC = hex.EncodeToString([]byte("transfer/channel-0"))
Expand Down
12 changes: 6 additions & 6 deletions x/tron/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (suite *KeeperTestSuite) Test_msgServer_ConfirmBatch() {
malleate: func() {
msg = &crosschaintypes.MsgConfirmBatch{
Nonce: tmrand.Uint64(),
TokenContract: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
TokenContract: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
BridgerAddress: sdk.AccAddress(helpers.GenerateAddress().Bytes()).String(),
}
},
Expand All @@ -60,7 +60,7 @@ func (suite *KeeperTestSuite) Test_msgServer_ConfirmBatch() {
Nonce: newOutgoingTx.BatchNonce,
TokenContract: newOutgoingTx.TokenContract,
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
Signature: helpers.GenerateAddress().Hex(),
}
},
Expand All @@ -83,7 +83,7 @@ func (suite *KeeperTestSuite) Test_msgServer_ConfirmBatch() {
Nonce: newOutgoingTx.BatchNonce,
TokenContract: newOutgoingTx.TokenContract,
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
Signature: hex.EncodeToString(signature),
}
},
Expand Down Expand Up @@ -136,7 +136,7 @@ func (suite *KeeperTestSuite) Test_msgServer_OracleSetConfirm() {
msg = &crosschaintypes.MsgOracleSetConfirm{
Nonce: newOracleSet.Nonce,
BridgerAddress: sdk.AccAddress(helpers.GenerateAddress().Bytes()).String(),
ExternalAddress: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
ExternalAddress: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
}
},
expPass: false,
Expand All @@ -149,7 +149,7 @@ func (suite *KeeperTestSuite) Test_msgServer_OracleSetConfirm() {
msg = &crosschaintypes.MsgOracleSetConfirm{
Nonce: newOracleSet.Nonce,
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(helpers.GenerateAddress().Hex()),
ExternalAddress: helpers.HexAddrToTronAddr(helpers.GenerateAddress().Hex()),
Signature: helpers.GenerateAddress().Hex(),
}
},
Expand All @@ -171,7 +171,7 @@ func (suite *KeeperTestSuite) Test_msgServer_OracleSetConfirm() {
msg = &crosschaintypes.MsgOracleSetConfirm{
Nonce: newOracleSet.Nonce,
BridgerAddress: bridger.String(),
ExternalAddress: trontypes.AddressFromHex(externalKey.PubKey().Address().String()),
ExternalAddress: helpers.HexAddrToTronAddr(externalKey.PubKey().Address().String()),
Signature: hex.EncodeToString(signature),
}
},
Expand Down
Loading

0 comments on commit 3b5f94a

Please sign in to comment.