Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support other coin decimals, e.g. Ethermint (backport #866) #878

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ func (tn *ChainNode) AddGenesisAccount(ctx context.Context, address string, gene
if i != 0 {
amount += ","
}
amount += fmt.Sprintf("%d%s", coin.Amount.Int64(), coin.Denom)
amount += fmt.Sprintf("%s%s", coin.Amount.String(), coin.Denom)
}

tn.lock.Lock()
Expand Down Expand Up @@ -732,7 +732,7 @@ func (tn *ChainNode) Gentx(ctx context.Context, name string, genesisSelfDelegati
command = append(command, "genesis")
}

command = append(command, "gentx", valKey, fmt.Sprintf("%d%s", genesisSelfDelegation.Amount.Int64(), genesisSelfDelegation.Denom),
command = append(command, "gentx", valKey, fmt.Sprintf("%s%s", genesisSelfDelegation.Amount.String(), genesisSelfDelegation.Denom),
"--keyring-backend", keyring.BackendTest,
"--chain-id", tn.Chain.Config().ChainID)

Expand Down
17 changes: 10 additions & 7 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"sync"
"time"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -579,20 +580,20 @@ func (c *CosmosChain) ExportState(ctx context.Context, height int64) (string, er

// GetBalance fetches the current balance for a specific account address and denom.
// Implements Chain interface
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (math.Int, error) {
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
params := &bankTypes.QueryBalanceRequest{Address: address, Denom: denom}
grpcAddress := c.getFullNode().hostGRPCPort
conn, err := grpc.Dial(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return math.Int{}, err
return sdkmath.Int{}, err
}
defer conn.Close()

queryClient := bankTypes.NewQueryClient(conn)
res, err := queryClient.Balance(ctx, params)

if err != nil {
return math.Int{}, err
return sdkmath.Int{}, err
}

return res.Balance.Amount, nil
Expand Down Expand Up @@ -866,13 +867,15 @@ type ValidatorWithIntPower struct {
func (c *CosmosChain) Start(testName string, ctx context.Context, additionalGenesisWallets ...ibc.WalletAmount) error {
chainCfg := c.Config()

decimalPow := int64(math.Pow10(int(*chainCfg.CoinDecimals)))

genesisAmount := types.Coin{
Amount: types.NewInt(10_000_000_000_000),
Amount: sdkmath.NewInt(10_000_000).MulRaw(decimalPow),
Denom: chainCfg.Denom,
}

genesisSelfDelegation := types.Coin{
Amount: types.NewInt(5_000_000_000_000),
Amount: sdkmath.NewInt(5_000_000).MulRaw(decimalPow),
Denom: chainCfg.Denom,
}

Expand Down Expand Up @@ -1117,7 +1120,7 @@ func (c *CosmosChain) StartProvider(testName string, ctx context.Context, additi
proposer := ibc.WalletAmount{
Address: proposerAddr,
Denom: c.cfg.Denom,
Amount: math.NewInt(10_000_000_000_000),
Amount: sdkmath.NewInt(10_000_000_000_000),
}

additionalGenesisWallets = append(additionalGenesisWallets, proposer)
Expand Down
17 changes: 17 additions & 0 deletions chainspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ func (s *ChainSpec) applyConfigOverrides(cfg ibc.ChainConfig) (*ibc.ChainConfig,

cfg.UsingChainIDFlagCLI = s.UsingChainIDFlagCLI

if cfg.CoinDecimals == nil {
evm := int64(18)
cosmos := int64(6)

switch cfg.CoinType {
case "60":
cfg.CoinDecimals = &evm
case "118":
cfg.CoinDecimals = &cosmos
case "330":
cfg.CoinDecimals = &cosmos
case "529":
cfg.CoinDecimals = &cosmos

}
}

// Set the version depending on the chain type.
switch cfg.Type {
case "cosmos":
Expand Down
30 changes: 15 additions & 15 deletions conformance/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import (
"golang.org/x/sync/errgroup"
)

const (
userFaucetFund = int64(10_000_000_000)
testCoinAmount = int64(1_000_000)
var (
userFaucetFund = math.NewInt(10_000_000_000)
testCoinAmount = math.NewInt(1_000_000)
pollHeightMax = uint64(50)
)

Expand Down Expand Up @@ -146,12 +146,12 @@ func sendIBCTransfersFromBothChainsWithTimeout(
testCoinSrcToDst := ibc.WalletAmount{
Address: srcUser.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(dstChainCfg.Bech32Prefix),
Denom: srcChainCfg.Denom,
Amount: math.NewInt(testCoinAmount),
Amount: testCoinAmount,
}
testCoinDstToSrc := ibc.WalletAmount{
Address: dstUser.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(srcChainCfg.Bech32Prefix),
Denom: dstChainCfg.Denom,
Amount: math.NewInt(testCoinAmount),
Amount: testCoinAmount,
}

var eg errgroup.Group
Expand Down Expand Up @@ -406,7 +406,7 @@ func testPacketRelaySuccess(
for i, srcTx := range testCase.TxCache.Src {
t.Logf("Asserting %s to %s transfer", srcChainCfg.ChainID, dstChainCfg.ChainID)
// Assuming these values since the ibc transfers were sent in PreRelayerStart, so balances may have already changed by now
srcInitialBalance := math.NewInt(userFaucetFund)
srcInitialBalance := userFaucetFund
dstInitialBalance := math.ZeroInt()

srcAck, err := testutil.PollForAck(ctx, srcChain, srcTx.Height, srcTx.Height+pollHeightMax, srcTx.Packet)
Expand All @@ -424,10 +424,10 @@ func testPacketRelaySuccess(
req.NoError(err, "failed to get balance from dest chain")

totalFees := srcChain.GetGasFeesInNativeDenom(srcTx.GasSpent)
expectedDifference := testCoinAmount + totalFees
expectedDifference := testCoinAmount.AddRaw(totalFees)

req.True(srcFinalBalance.Equal(srcInitialBalance.SubRaw(expectedDifference)))
req.True(dstFinalBalance.Equal(dstInitialBalance.AddRaw(testCoinAmount)))
req.True(srcFinalBalance.Equal(srcInitialBalance.Sub(expectedDifference)))
req.True(dstFinalBalance.Equal(dstInitialBalance.Add(testCoinAmount)))
}

// [END] assert on source to destination transfer
Expand All @@ -439,7 +439,7 @@ func testPacketRelaySuccess(
dstDenom := dstChainCfg.Denom
// Assuming these values since the ibc transfers were sent in PreRelayerStart, so balances may have already changed by now
srcInitialBalance := math.ZeroInt()
dstInitialBalance := math.NewInt(userFaucetFund)
dstInitialBalance := userFaucetFund

dstAck, err := testutil.PollForAck(ctx, dstChain, dstTx.Height, dstTx.Height+pollHeightMax, dstTx.Packet)
req.NoError(err, "failed to get acknowledgement on destination chain")
Expand All @@ -460,10 +460,10 @@ func testPacketRelaySuccess(
req.NoError(err, "failed to get balance from dest chain")

totalFees := dstChain.GetGasFeesInNativeDenom(dstTx.GasSpent)
expectedDifference := testCoinAmount + totalFees
expectedDifference := testCoinAmount.AddRaw(totalFees)

req.True(srcFinalBalance.Equal(srcInitialBalance.AddRaw(testCoinAmount)))
req.True(dstFinalBalance.Equal(dstInitialBalance.SubRaw(expectedDifference)))
req.True(srcFinalBalance.Equal(srcInitialBalance.Add(testCoinAmount)))
req.True(dstFinalBalance.Equal(dstInitialBalance.Sub(expectedDifference)))
}
//[END] assert on destination to source transfer
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func testPacketRelayFail(
// [BEGIN] assert on source to destination transfer
for i, srcTx := range testCase.TxCache.Src {
// Assuming these values since the ibc transfers were sent in PreRelayerStart, so balances may have already changed by now
srcInitialBalance := math.NewInt(userFaucetFund)
srcInitialBalance := userFaucetFund
dstInitialBalance := math.ZeroInt()

timeout, err := testutil.PollForTimeout(ctx, srcChain, srcTx.Height, srcTx.Height+pollHeightMax, srcTx.Packet)
Expand Down Expand Up @@ -523,7 +523,7 @@ func testPacketRelayFail(
for i, dstTx := range testCase.TxCache.Dst {
// Assuming these values since the ibc transfers were sent in PreRelayerStart, so balances may have already changed by now
srcInitialBalance := math.ZeroInt()
dstInitialBalance := math.NewInt(userFaucetFund)
dstInitialBalance := userFaucetFund

timeout, err := testutil.PollForTimeout(ctx, dstChain, dstTx.Height, dstTx.Height+pollHeightMax, dstTx.Packet)
req.NoError(err, "failed to get timeout packet on destination chain")
Expand Down
2 changes: 1 addition & 1 deletion examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
_ = ic.Close()
})

users := interchaintest.GetAndFundTestUsers(t, ctx, "default", int64(10_000_000_000), chain, chain)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", math.NewInt(10_000_000_000), chain, chain)

testBuildDependencies(ctx, t, chain)
testWalletKeys(ctx, t, chain)
Expand Down
3 changes: 2 additions & 1 deletion examples/cosmos/chain_param_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"testing"

"cosmossdk.io/math"
paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand Down Expand Up @@ -43,7 +44,7 @@ func CosmosChainParamChangeTest(t *testing.T, name, version string) {
enableBlockDB := false
ctx, _, _, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

const userFunds = int64(10_000_000_000)
var userFunds = math.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
chainUser := users[0]

Expand Down
4 changes: 3 additions & 1 deletion examples/cosmos/chain_upgrade_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"

interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/conformance"
Expand Down Expand Up @@ -101,7 +103,7 @@ func CosmosChainUpgradeIBCTest(t *testing.T, chainName, initialVersion, upgradeC
_ = ic.Close()
})

const userFunds = int64(10_000_000_000)
var userFunds = sdkmath.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
chainUser := users[0]

Expand Down
Loading
Loading