Skip to content

Commit

Permalink
feat: support other coin decimals, e.g. Ethermint (#866)
Browse files Browse the repository at this point in the history
* move to math.NewInt

* mod tidy

* ethermint support + chain example

* remove c.CoinDecimals from isFullyConfigured check

* revert accidental hyperspace finalStakeBal change

* use heighliner image

* remove unused docker state export

---------

Co-authored-by: Dan Kanefsky <[email protected]>
(cherry picked from commit a033ba4)

# Conflicts:
#	chain/cosmos/cosmos_chain.go
#	examples/cosmos/chain_upgrade_ibc_test.go
#	examples/cosmwasm/rust-optimizer/rust_optimizer_test.go
#	examples/cosmwasm/workspace-optimizer/workspace_optimizer_test.go
#	examples/hyperspace/hyperspace_test.go
#	examples/penumbra/penumbra_chain_test.go
#	go.sum
#	go.work.sum
  • Loading branch information
Reecepbcups authored and mergify[bot] committed Nov 19, 2023
1 parent 79f1a55 commit 4340111
Show file tree
Hide file tree
Showing 22 changed files with 744 additions and 49 deletions.
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
10 changes: 10 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,13 +866,23 @@ 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{
<<<<<<< HEAD

Check failure on line 872 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: unexpected <<, expecting expression

Check failure on line 872 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: unexpected <<, expecting expression

Check failure on line 872 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: unexpected <<, expecting expression

Check failure on line 872 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '<<' (typecheck)
Amount: types.NewInt(10_000_000_000_000),

Check failure on line 873 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: unexpected ) in composite literal; possibly missing comma or }

Check failure on line 873 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: unexpected ) in composite literal; possibly missing comma or }

Check failure on line 873 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: unexpected ) in composite literal; possibly missing comma or }
=======
Amount: sdkmath.NewInt(10_000_000).MulRaw(decimalPow),
>>>>>>> a033ba4 (feat: support other coin decimals, e.g. Ethermint (#866))

Check failure on line 876 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

invalid character U+0023 '#'

Check failure on line 876 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

invalid character U+0023 '#'

Check failure on line 876 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

invalid character U+0023 '#'

Check failure on line 876 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / lint

illegal character U+0023 '#' (typecheck)
Denom: chainCfg.Denom,
}

Check failure on line 878 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: unexpected }, expecting expression

Check failure on line 878 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: unexpected }, expecting expression

Check failure on line 878 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: unexpected }, expecting expression

genesisSelfDelegation := types.Coin{

Check failure on line 880 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: non-declaration statement outside function body

Check failure on line 880 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: non-declaration statement outside function body

Check failure on line 880 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body
<<<<<<< HEAD
Amount: types.NewInt(5_000_000_000_000),
=======
Amount: sdkmath.NewInt(5_000_000).MulRaw(decimalPow),
>>>>>>> a033ba4 (feat: support other coin decimals, e.g. Ethermint (#866))

Check failure on line 885 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

invalid character U+0023 '#'

Check failure on line 885 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / test-conformance

invalid character U+0023 '#'

Check failure on line 885 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / unit-tests

invalid character U+0023 '#'

Check failure on line 885 in chain/cosmos/cosmos_chain.go

View workflow job for this annotation

GitHub Actions / lint

illegal character U+0023 '#' (typecheck)
Denom: chainCfg.Denom,
}

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
13 changes: 12 additions & 1 deletion examples/cosmos/chain_upgrade_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import (
"testing"
"time"

<<<<<<< HEAD

Check failure on line 8 in examples/cosmos/chain_upgrade_ibc_test.go

View workflow job for this annotation

GitHub Actions / test-cosmos-examples

expected 'STRING', found '<<'

Check failure on line 8 in examples/cosmos/chain_upgrade_ibc_test.go

View workflow job for this annotation

GitHub Actions / unit-tests

expected 'STRING', found '<<'
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/conformance"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/relayer"
"github.com/strangelove-ventures/interchaintest/v7/testreporter"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
=======
"cosmossdk.io/math"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/conformance"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/relayer"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
>>>>>>> a033ba4 (feat: support other coin decimals, e.g. Ethermint (#866))
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
Expand Down Expand Up @@ -101,7 +112,7 @@ func CosmosChainUpgradeIBCTest(t *testing.T, chainName, initialVersion, upgradeC
_ = ic.Close()
})

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
158 changes: 158 additions & 0 deletions examples/cosmos/ethermint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package cosmos_test

import (
"context"
"testing"
"time"

sdkmath "cosmossdk.io/math"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

const (
wallet = "dym"
denom = "udym"
display = "DYM"
decimals = 18
)

func TestEthermintChain(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}

numVals := 1
numFullNodes := 0

cosmos.SetSDKConfig(wallet)

genesis := []cosmos.GenesisKV{
cosmos.NewGenesisKV("app_state.gov.voting_params.voting_period", "1m"),
cosmos.NewGenesisKV("app_state.gov.deposit_params.min_deposit.0.denom", denom),
cosmos.NewGenesisKV("app_state.gov.deposit_params.min_deposit.0.amount", "1"),

cosmos.NewGenesisKV("app_state.rollapp.params.dispute_period_in_blocks", "2"),

cosmos.NewGenesisKV("app_state.staking.params.max_validators", 110),

cosmos.NewGenesisKV("consensus_params.block.max_gas", "40000000"),
cosmos.NewGenesisKV("app_state.feemarket.params.no_base_fee", true),
cosmos.NewGenesisKV("app_state.evm.params.evm_denom", denom),
cosmos.NewGenesisKV("app_state.evm.params.enable_create", false),

cosmos.NewGenesisKV("app_state.epochs.epochs", []evmEpoch{
newEvmEpoch("week", "604800s"),
newEvmEpoch("day", "86400s"),
newEvmEpoch("hour", "3600s"),
newEvmEpoch("minute", "60s"),
}),

cosmos.NewGenesisKV("app_state.incentives.params.distr_epoch_identifier", "minute"),
cosmos.NewGenesisKV("app_state.poolincentives.params.minted_denom", denom),
cosmos.NewGenesisKV("app_state.poolincentives.lockable_durations", []string{"3600s"}),

cosmos.NewGenesisKV("app_state.crisis.constant_fee.denom", denom),
cosmos.NewGenesisKV("app_state.poolmanager.params.pool_creation_fee.0.denom", denom),

cosmos.NewGenesisKV("app_state.bank.denom_metadata", []banktypes.Metadata{
{
Description: "Denom metadata",
DenomUnits: []*banktypes.DenomUnit{
{
Denom: denom,
Exponent: 0,
},
{
Denom: display,
Exponent: decimals,
},
},
Base: denom,
Display: display,
Name: display,
Symbol: display,
URI: "",
URIHash: "",
},
}),
}

decimals := int64(decimals)
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "dymension",
ChainConfig: ibc.ChainConfig{
Type: "cosmos",
ChainID: "dymension_100-1",
Images: []ibc.DockerImage{{Repository: "ghcr.io/strangelove-ventures/heighliner/dymension", Version: "854ef84", UidGid: "1025:1025"}},
Bin: "dymd",
Bech32Prefix: wallet,
Denom: denom,
CoinType: "60",
GasPrices: "0" + denom,
GasAdjustment: 1.5,
TrustingPeriod: "168h0m0s",
ModifyGenesis: cosmos.ModifyGenesis(genesis),
CoinDecimals: &decimals,
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

chain := chains[0].(*cosmos.CosmosChain)

ic := interchaintest.NewInterchain().
AddChain(chain)

ctx := context.Background()
client, network := interchaintest.DockerSetup(t)

require.NoError(t, ic.Build(ctx, nil, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
}))
t.Cleanup(func() {
_ = ic.Close()
})

users := interchaintest.GetAndFundTestUsers(t, ctx, "default", sdkmath.NewInt(10_000_000_000), chain, chain)
user := users[0]

balance, err := chain.GetNode().Chain.GetBalance(ctx, user.FormattedAddress(), denom)
require.NoError(t, err)
require.Equal(t, "10000000000", balance.String())
}

type evmEpoch struct {
Identifier string `json:"identifier"`
StartTime time.Time `json:"start_time"`
Duration string `json:"duration"`
CurrentEpoch string `json:"current_epoch"`
CurrentEpochStartTime time.Time `json:"current_epoch_start_time"`
EpochCountingStarted bool `json:"epoch_counting_started"`
CurrentEpochStartHeight string `json:"current_epoch_start_height"`
}

func newEvmEpoch(identifier string, duration string) evmEpoch {
return evmEpoch{
Identifier: identifier,
StartTime: time.Time{},
Duration: duration,
CurrentEpoch: "0",
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
CurrentEpochStartHeight: "0",
}
}
Loading

0 comments on commit 4340111

Please sign in to comment.