Skip to content

Commit

Permalink
Add a test for pre-upgrade wasm contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
fastfadingviolets committed Sep 12, 2024
1 parent 8232d7a commit 9455415
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 50 deletions.
148 changes: 98 additions & 50 deletions tests/interchain/cosmwasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,99 @@ import (

type CosmWasmSuite struct {
*chainsuite.Suite
ContractWasm []byte
ContractPath string
PreUpgradeContractCode string
PreUpgradeContractAddr string
}

func (s *CosmWasmSuite) TestPermissionedCosmWasm() {
const (
initState = `{"count": 100}`
query = `{"get_count":{}}`
increment = `{"increment":{}}`
)
const (
initState = `{"count": 100}`
query = `{"get_count":{}}`
increment = `{"increment":{}}`
)

func (s *CosmWasmSuite) SetupSuite() {
s.Suite.SetupSuite()

contractWasm, err := os.ReadFile("testdata/contract.wasm")
s.Require().NoError(err)
s.ContractWasm = contractWasm
s.Require().NoError(s.Chain.GetNode().WriteFile(s.GetContext(), s.ContractWasm, "contract.wasm"))
s.ContractPath = path.Join(s.Chain.GetNode().HomeDir(), "contract.wasm")

s.Require().NoError(s.Chain.GetNode().WriteFile(s.GetContext(), contractWasm, "contract.wasm"))
contractPath := path.Join(s.Chain.GetNode().HomeDir(), "contract.wasm")
code, contractAddr := s.storeInstantiateProposal(initState)
s.PreUpgradeContractCode = code
s.PreUpgradeContractAddr = contractAddr

govAddr, err := s.Chain.GetGovernanceAddress(s.GetContext())
s.Require().NoError(err)
s.UpgradeChain()
}

func (s *CosmWasmSuite) TestPreUpgradeContract() {
count := s.getContractCount(s.PreUpgradeContractAddr)
s.Require().Equal(int64(100), count)

s.executeContractByTx(s.PreUpgradeContractAddr)

count = s.getContractCount(s.PreUpgradeContractAddr)
s.Require().Equal(int64(101), count)

s.executeContractByProposal(s.PreUpgradeContractAddr)

count = s.getContractCount(s.PreUpgradeContractAddr)
s.Require().Equal(int64(102), count)
}

func (s *CosmWasmSuite) TestCantStoreWithoutProp() {
infos, err := s.Chain.QueryJSON(s.GetContext(), "code_infos", "wasm", "list-code")
s.Require().NoError(err)
codeCountBefore := len(infos.Array())

_, err = s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "store", contractPath,
"wasm", "store", s.ContractPath,
)
s.Require().Error(err)

infos, err = s.Chain.QueryJSON(s.GetContext(), "code_infos", "wasm", "list-code")
s.Require().NoError(err)
codeCountAfter := len(infos.Array())
s.Require().Equal(codeCountBefore, codeCountAfter)
}

txhash, err := s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "submit-proposal", "store-instantiate",
contractPath,
initState, "--label", "my-contract",
"--no-admin", "--instantiate-nobody", "true",
"--title", "Store and instantiate template",
"--summary", "Store and instantiate template",
"--deposit", fmt.Sprintf("10000000%s", s.Config.ChainSpec.Denom),
func (s *CosmWasmSuite) TestCantInstantiateWithoutProp() {
_, err := s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "instantiate", s.PreUpgradeContractCode, initState, "--label", "my-contract", "--no-admin",
)
s.Require().NoError(err)
s.Require().Error(err)
}

proposalId, err := s.Chain.GetProposalID(s.GetContext(), txhash)
s.Require().NoError(err)
func (s *CosmWasmSuite) TestCreateNewContract() {

err = s.Chain.PassProposal(s.GetContext(), proposalId)
s.Require().NoError(err)
_, contractAddr := s.storeInstantiateProposal(initState)

codeJSON, err := s.Chain.QueryJSON(s.GetContext(), fmt.Sprintf("code_infos.#(creator=\"%s\").code_id", govAddr), "wasm", "list-code")
s.Require().NoError(err)
code := codeJSON.String()
count := s.getContractCount(contractAddr)
s.Require().Equal(int64(100), count)

contractAddrJSON, err := s.Chain.QueryJSON(s.GetContext(), "contracts.0", "wasm", "list-contract-by-code", code)
s.Require().NoError(err)
contractAddr := contractAddrJSON.String()
s.executeContractByTx(contractAddr)

_, err = s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "instantiate", code, initState, "--label", "my-contract", "--no-admin",
)
s.Require().Error(err)
count = s.getContractCount(contractAddr)
s.Require().Equal(int64(101), count)

countJSON, err := s.Chain.QueryJSON(s.GetContext(), "data.count", "wasm", "contract-state", "smart", contractAddr, query)
s.Require().NoError(err)
count := countJSON.Int()
s.Require().Equal(int64(100), count)
s.executeContractByProposal(contractAddr)

_, err = s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
count = s.getContractCount(contractAddr)
s.Require().Equal(int64(102), count)
}

func (s *CosmWasmSuite) executeContractByTx(contractAddr string) {
_, err := s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "execute", contractAddr, increment,
)
s.Require().NoError(err)
}

countJSON, err = s.Chain.QueryJSON(s.GetContext(), "data.count", "wasm", "contract-state", "smart", contractAddr, query)
s.Require().NoError(err)
count = countJSON.Int()
s.Require().Equal(int64(101), count)

txhash, err = s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
func (s *CosmWasmSuite) executeContractByProposal(contractAddr string) {
txhash, err := s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "submit-proposal", "execute-contract",
contractAddr, increment,
"--title", "Increment count",
Expand All @@ -99,21 +114,54 @@ func (s *CosmWasmSuite) TestPermissionedCosmWasm() {
)
s.Require().NoError(err)

proposalId, err = s.Chain.GetProposalID(s.GetContext(), txhash)
proposalId, err := s.Chain.GetProposalID(s.GetContext(), txhash)
s.Require().NoError(err)

err = s.Chain.PassProposal(s.GetContext(), proposalId)
s.Require().NoError(err)
}

countJSON, err = s.Chain.QueryJSON(s.GetContext(), "data.count", "wasm", "contract-state", "smart", contractAddr, query)
func (s *CosmWasmSuite) getContractCount(contractAddr string) int64 {
countJSON, err := s.Chain.QueryJSON(s.GetContext(), "data.count", "wasm", "contract-state", "smart", contractAddr, query)
s.Require().NoError(err)
count = countJSON.Int()
s.Require().Equal(int64(102), count)
count := countJSON.Int()
return count
}

func (s *CosmWasmSuite) storeInstantiateProposal(initState string) (string, string) {
govAddr, err := s.Chain.GetGovernanceAddress(s.GetContext())
s.Require().NoError(err)

txhash, err := s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"wasm", "submit-proposal", "store-instantiate",
s.ContractPath,
initState, "--label", "my-contract",
"--no-admin", "--instantiate-nobody", "true",
"--title", "Store and instantiate template",
"--summary", "Store and instantiate template",
"--deposit", fmt.Sprintf("10000000%s", s.Config.ChainSpec.Denom),
)
s.Require().NoError(err)

proposalId, err := s.Chain.GetProposalID(s.GetContext(), txhash)
s.Require().NoError(err)

err = s.Chain.PassProposal(s.GetContext(), proposalId)
s.Require().NoError(err)

codeJSON, err := s.Chain.QueryJSON(s.GetContext(), fmt.Sprintf("code_infos.@reverse.#(creator=\"%s\").code_id", govAddr), "wasm", "list-code")
s.Require().NoError(err)
code := codeJSON.String()

contractAddrJSON, err := s.Chain.QueryJSON(s.GetContext(), "contracts.0", "wasm", "list-contract-by-code", code)
s.Require().NoError(err)
contractAddr := contractAddrJSON.String()
return code, contractAddr
}

func TestCosmWasm(t *testing.T) {
s := &CosmWasmSuite{
Suite: chainsuite.NewSuite(chainsuite.SuiteConfig{UpgradeOnSetup: true}),
Suite: chainsuite.NewSuite(chainsuite.SuiteConfig{UpgradeOnSetup: false}),
}
suite.Run(t, s)
}
35 changes: 35 additions & 0 deletions tests/interchain/permissionless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package interchain_test
import (
"encoding/json"
"fmt"
"path"
"path/filepath"
"strconv"
"testing"
Expand Down Expand Up @@ -221,6 +222,40 @@ func (s *ConsumerPropMigrationSuite) TestPassedProposalsDontChange() {
s.Require().Equal("CONSUMER_PHASE_STOPPED", chain.Get("phase").String())
}

func (s *ConsumerPropMigrationSuite) TestChangeOwner() {
s.UpgradeChain()

cfg := s.consumerCfg
cfg.TopN = 0
consumer, err := s.Chain.AddConsumerChain(s.GetContext(), s.Relayer, cfg)
s.Require().NoError(err)
err = s.Chain.CheckCCV(s.GetContext(), consumer, s.Relayer, 1_000_000, 0, 1)
s.Require().NoError(err)

govAddress, err := s.Chain.GetGovernanceAddress(s.GetContext())
s.Require().NoError(err)
consumerID, err := s.Chain.QueryJSON(s.GetContext(), fmt.Sprintf("chains.#(chain_id=%q).consumer_id",
consumer.Config().ChainID), "provider", "list-consumer-chains")
s.Require().NoError(err)
update := &providertypes.MsgUpdateConsumer{
ConsumerId: consumerID.String(),
NewOwnerAddress: govAddress,
Metadata: &providertypes.ConsumerMetadata{
Name: consumer.Config().Name,
Description: "Consumer chain",
Metadata: "ipfs://",
},
}
updateBz, err := json.Marshal(update)
s.Require().NoError(err)
err = s.Chain.GetNode().WriteFile(s.GetContext(), updateBz, "consumer-update.json")
s.Require().NoError(err)
_, err = s.Chain.GetNode().ExecTx(s.GetContext(), interchaintest.FaucetAccountKeyName,
"provider", "update-consumer", path.Join(s.Chain.GetNode().HomeDir(), "consumer-update.json"))
s.Require().NoError(err)

}

func TestConsumerPropMigration(t *testing.T) {
genesis := chainsuite.DefaultGenesis()
genesis = append(genesis,
Expand Down

0 comments on commit 9455415

Please sign in to comment.