Skip to content

Commit

Permalink
fix: work around to get permissionless for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Nov 29, 2024
1 parent a1629f2 commit bdd9aa5
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 77 deletions.
135 changes: 88 additions & 47 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
Expand All @@ -35,15 +34,13 @@ import (
volumetypes "github.com/docker/docker/api/types/volume"
dockerclient "github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/tidwall/sjson"
"go.uber.org/zap"
"golang.org/x/mod/semver"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
ccvclient "github.com/cosmos/interchain-security/v6/x/ccv/provider/client"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
"github.com/strangelove-ventures/interchaintest/v8/blockdb"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
Expand Down Expand Up @@ -884,63 +881,107 @@ func (tn *ChainNode) SendIBCTransfer(
return tn.ExecTx(ctx, keyName, command...)
}

func (tn *ChainNode) ConsumerAdditionProposal(ctx context.Context, keyName string, prop ccvclient.ConsumerAdditionProposalJSON) (string, error) {
type CreateConsumerResponse struct {
// ConsumerID can be empty if submited via a gov proposal.
ConsumerID string `json:"consumer_id"`
TxHash string `json:"tx_hash"`
}

func (tn *ChainNode) ConsumerAdditionProposal(ctx context.Context, keyName string, cc providertypes.MsgCreateConsumer, deposit string) (CreateConsumerResponse, error) {
cosmosChain := (tn.Chain).(*CosmosChain)
if !tn.HasCommand(ctx, "tx", "gov", "submit-legacy-proposal", "consumer-addition") {
authority, err := cosmosChain.GetGovernanceAddress(ctx)

// authority, err := cosmosChain.GetGovernanceAddress(ctx)
// if err != nil {
// return "", err
// }

// permissionless
if tn.HasCommand(ctx, "tx", "provider", "create-consumer") {
fmt.Println("Using new command")
content, err := json.Marshal(cc)
if err != nil {
return "", err
return CreateConsumerResponse{}, err
}
ccvProp := &providertypes.MsgConsumerAddition{
ChainId: prop.ChainId,
InitialHeight: prop.InitialHeight,
GenesisHash: prop.GenesisHash,
BinaryHash: prop.BinaryHash,
SpawnTime: prop.SpawnTime,
UnbondingPeriod: prop.UnbondingPeriod,
CcvTimeoutPeriod: prop.CcvTimeoutPeriod,
TransferTimeoutPeriod: prop.TransferTimeoutPeriod,
ConsumerRedistributionFraction: prop.ConsumerRedistributionFraction,
BlocksPerDistributionTransmission: prop.BlocksPerDistributionTransmission,
HistoricalEntries: prop.HistoricalEntries,
DistributionTransmissionChannel: prop.DistributionTransmissionChannel,
Top_N: prop.TopN,
ValidatorsPowerCap: prop.ValidatorsPowerCap,
ValidatorSetCap: prop.ValidatorSetCap,
Allowlist: prop.Allowlist,
Denylist: prop.Denylist,
Authority: authority,
AllowInactiveVals: prop.AllowInactiveVals,
MinStake: prop.MinStake,
jsonFile := "create-consumer.json"
if err = tn.WriteFile(ctx, content, jsonFile); err != nil {
return CreateConsumerResponse{}, err
}
propObj, err := cosmosChain.BuildProposal([]ProtoMessage{ccvProp}, prop.Title, prop.Summary, "ipfs://CID", prop.Deposit, "", false)
filePath := path.Join(tn.HomeDir(), jsonFile)
txHash, err := tn.ExecTx(ctx, keyName, "provider", "create-consumer", filePath)
if err != nil {
return "", err
return CreateConsumerResponse{}, err
}
return tn.SubmitProposal(ctx, keyName, propObj)
} else {
propBz, err := json.Marshal(prop)

response, err := tn.TxHashToResponse(ctx, txHash)
if err != nil {
return "", err
return CreateConsumerResponse{}, err
}
propBz, err = sjson.SetBytes(propBz, "metadata", "ipfs://CID")

consumerId, found := getEvtAttribute(response.Events, providertypes.EventTypeCreateConsumer, providertypes.AttributeConsumerId)
if !found {
return CreateConsumerResponse{}, fmt.Errorf("consumer id is not found")
}
fmt.Println("Consumer ID: ", consumerId)

return CreateConsumerResponse{
ConsumerID: consumerId,
TxHash: txHash,
}, nil

// TODO: remove support for this? or as default for v1 non permissionless chains
} else if !tn.HasCommand(ctx, "tx", "gov", "submit-legacy-proposal", "consumer-addition") {
ccvProp := &providertypes.MsgCreateConsumer{
ChainId: cc.ChainId,
Submitter: cc.Submitter,
Metadata: providertypes.ConsumerMetadata{
Name: cc.ChainId,
Description: "description",
Metadata: "github.com/meta/data",
},
InitializationParameters: nil,
PowerShapingParameters: nil,
AllowlistedRewardDenoms: nil,
}
text := "consumer addition " + cc.ChainId
propObj, err := cosmosChain.BuildProposal([]ProtoMessage{ccvProp}, text, text, "ipfs://CID", deposit, "", false)
if err != nil {
return "", err
return CreateConsumerResponse{}, err
}

fileName := "proposal_" + dockerutil.RandLowerCaseLetterString(4) + ".json"

fw := dockerutil.NewFileWriter(tn.logger(), tn.DockerClient, tn.TestName)
if err := fw.WriteFile(ctx, tn.VolumeName, fileName, propBz); err != nil {
return "", fmt.Errorf("failure writing proposal json: %w", err)
resp, err := tn.SubmitProposal(ctx, keyName, propObj)
if err != nil {
return CreateConsumerResponse{}, err
}

filePath := filepath.Join(tn.HomeDir(), fileName)
// Gov
return CreateConsumerResponse{
ConsumerID: "",
TxHash: resp,
}, nil

return tn.ExecTx(ctx, keyName,
"gov", "submit-legacy-proposal", "consumer-addition", filePath,
"--gas", "auto",
)
} else {
// propBz, err := json.Marshal(cc)
// if err != nil {
// return "", err
// }
// propBz, err = sjson.SetBytes(propBz, "metadata", "ipfs://CID")
// if err != nil {
// return "", err
// }

// fileName := "proposal_" + dockerutil.RandLowerCaseLetterString(4) + ".json"

// fw := dockerutil.NewFileWriter(tn.logger(), tn.DockerClient, tn.TestName)
// if err := fw.WriteFile(ctx, tn.VolumeName, fileName, propBz); err != nil {
// return "", fmt.Errorf("failure writing proposal json: %w", err)
// }

// filePath := filepath.Join(tn.HomeDir(), fileName)

// return tn.ExecTx(ctx, keyName,
// "gov", "submit-legacy-proposal", "consumer-addition", filePath,
// "--gas", "auto",
// )
panic("ICT has not implemented non govv1 or permissionless. You can no longer use submit-legacy-proposal for ICS additions.")
}
}

Expand Down
10 changes: 5 additions & 5 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" // nolint:staticcheck
chanTypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ccvclient "github.com/cosmos/interchain-security/v6/x/ccv/provider/client"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
dockertypes "github.com/docker/docker/api/types"
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -509,13 +509,13 @@ func (c *CosmosChain) QueryBankMetadata(ctx context.Context, denom string) (*Ban
return c.getFullNode().QueryBankMetadata(ctx, denom)
}

// ConsumerAdditionProposal submits a legacy governance proposal to add a consumer to the chain.
func (c *CosmosChain) ConsumerAdditionProposal(ctx context.Context, keyName string, prop ccvclient.ConsumerAdditionProposalJSON) (tx TxProposal, _ error) {
txHash, err := c.getFullNode().ConsumerAdditionProposal(ctx, keyName, prop)
// CreateConsumerAction submits a consumer to the chain.
func (c *CosmosChain) CreateConsumerAction(ctx context.Context, keyName string, cc providertypes.MsgCreateConsumer, deposit string) (tx TxProposal, _ error) {
ca, err := c.getFullNode().ConsumerAdditionProposal(ctx, keyName, cc, deposit)
if err != nil {
return tx, fmt.Errorf("failed to submit consumer addition proposal: %w", err)
}
return c.txProposal(txHash)
return c.txProposal(ca.TxHash)
}

func (c *CosmosChain) txProposal(txHash string) (tx TxProposal, _ error) {
Expand Down
80 changes: 59 additions & 21 deletions chain/cosmos/ics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"time"

sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types" // nolint:staticcheck
"github.com/cosmos/cosmos-sdk/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" // nolint:staticcheck
ccvclient "github.com/cosmos/interchain-security/v6/x/ccv/provider/client"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
"github.com/icza/dyno"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -148,7 +149,6 @@ func (c *CosmosChain) StartProvider(testName string, ctx context.Context, additi
if err != nil {
return fmt.Errorf("failed to parse trusting period in 'StartProvider': %w", err)
}

spawnTimeWait := os.Getenv("ICS_SPAWN_TIME_WAIT")
if spawnTimeWait == "" {
spawnTimeWait = "45s"
Expand All @@ -157,32 +157,55 @@ func (c *CosmosChain) StartProvider(testName string, ctx context.Context, additi
if err != nil {
return fmt.Errorf("invalid ICS_SPAWN_TIME_WAIT %s: %w", spawnTimeWait, err)
}

for _, consumer := range c.Consumers {
prop := ccvclient.ConsumerAdditionProposalJSON{
Title: fmt.Sprintf("Addition of %s consumer chain", consumer.cfg.Name),
Summary: "Proposal to add new consumer chain",
ChainId: consumer.cfg.ChainID,
InitialHeight: clienttypes.Height{RevisionNumber: clienttypes.ParseChainID(consumer.cfg.ChainID), RevisionHeight: 1},
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: time.Now().Add(spawnTimeWaitDuration),

// TODO fetch or default variables
BlocksPerDistributionTransmission: 1000,
CcvTimeoutPeriod: trustingPeriod * 2,
TransferTimeoutPeriod: trustingPeriod,
ConsumerRedistributionFraction: "0.75",
HistoricalEntries: 10000,
UnbondingPeriod: trustingPeriod,
Deposit: "100000000" + c.cfg.Denom,
prop := providertypes.MsgCreateConsumer{
// Title: fmt.Sprintf("Addition of %s consumer chain", consumer.cfg.Name),
// Summary: "Proposal to add new consumer chain",
// ChainId: consumer.cfg.ChainID,
// InitialHeight: clienttypes.Height{RevisionNumber: clienttypes.ParseChainID(consumer.cfg.ChainID), RevisionHeight: 1},
// GenesisHash: []byte("gen_hash"),
// BinaryHash: []byte("bin_hash"),
// SpawnTime: time.Now().Add(spawnTimeWaitDuration),

// // TODO fetch or default variables
// BlocksPerDistributionTransmission: 1000,
// CcvTimeoutPeriod: trustingPeriod * 2,
// TransferTimeoutPeriod: trustingPeriod,
// ConsumerRedistributionFraction: "0.75",
// HistoricalEntries: 10000,
// UnbondingPeriod: trustingPeriod,
// Deposit: "100000000" + c.cfg.Denom,
Submitter: proposerAddr,
ChainId: consumer.cfg.ChainID,
Metadata: providertypes.ConsumerMetadata{
Name: consumer.cfg.Name,
Description: fmt.Sprintf("Consumer chain %s", consumer.cfg.Name),
Metadata: "metadata",
},
InitializationParameters: &providertypes.ConsumerInitializationParameters{
InitialHeight: clienttypes.Height{RevisionNumber: clienttypes.ParseChainID(consumer.cfg.ChainID), RevisionHeight: 1},
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: time.Now().Add(spawnTimeWaitDuration),
UnbondingPeriod: trustingPeriod,
CcvTimeoutPeriod: trustingPeriod * 2,
TransferTimeoutPeriod: trustingPeriod,
ConsumerRedistributionFraction: "0.75",
BlocksPerDistributionTransmission: 1000,
HistoricalEntries: 10000,
DistributionTransmissionChannel: "distribution",
},
PowerShapingParameters: nil,
AllowlistedRewardDenoms: nil,
}

height, err := c.Height(ctx)
if err != nil {
return fmt.Errorf("failed to query provider height before consumer addition proposal: %w", err)
}

propTx, err := c.ConsumerAdditionProposal(ctx, proposerKeyName, prop)
propTx, err := c.CreateConsumerAction(ctx, proposerKeyName, prop, "")
if err != nil {
return err
}
Expand Down Expand Up @@ -479,3 +502,18 @@ func (c *CosmosChain) transformCCVState(ctx context.Context, ccvState []byte, co
}
return res.Stdout, nil
}

// https://github.com/cosmos/interchain-security/blob/deea6fb8a98bbce447a1b2dfd6a60806141efbfe/tests/interchain/chainsuite/chain.go#L442
func getEvtAttribute(events []abci.Event, evtType string, key string) (string, bool) {
for _, evt := range events {
if evt.GetType() == evtType {
for _, attr := range evt.Attributes {
if attr.Key == key {
return attr.Value, true
}
}
}
}

return "", false
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/tyler-smith/go-bip32 v1.0.0
github.com/tyler-smith/go-bip39 v1.1.0
go.uber.org/multierr v1.11.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1171,15 +1171,12 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
Expand Down

0 comments on commit bdd9aa5

Please sign in to comment.