|
| 1 | +package cosmwasm_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" |
| 9 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | + |
| 12 | + "github.com/osmosis-labs/osmosis/osmoutils/cosmwasm" |
| 13 | + "github.com/osmosis-labs/osmosis/v23/app/apptesting" |
| 14 | +) |
| 15 | + |
| 16 | +type KeeperTestSuite struct { |
| 17 | + apptesting.KeeperTestHelper |
| 18 | +} |
| 19 | + |
| 20 | +func TestKeeperTestSuite(t *testing.T) { |
| 21 | + suite.Run(t, new(KeeperTestSuite)) |
| 22 | +} |
| 23 | + |
| 24 | +func (s *KeeperTestSuite) TestSudoGasLimit() { |
| 25 | + // Skip test if there is system-side incompatibility |
| 26 | + s.SkipIfWSL() |
| 27 | + |
| 28 | + // We use contracts already defined in existing modules to avoid duplicate test contract code. |
| 29 | + // This is a simple counter contract that counts `Amount` times and does a state write on each iteration. |
| 30 | + // Source code can be found in x/concentrated-liquidity/testcontracts/contract-sources |
| 31 | + counterContractPath := "../../x/concentrated-liquidity/testcontracts/compiled-wasm/counter.wasm" |
| 32 | + |
| 33 | + // Message structs for the test CW contract |
| 34 | + type CountMsg struct { |
| 35 | + Amount int64 `json:"amount"` |
| 36 | + } |
| 37 | + type CountMsgResponse struct { |
| 38 | + } |
| 39 | + type CountSudoMsg struct { |
| 40 | + Count CountMsg `json:"count"` |
| 41 | + } |
| 42 | + |
| 43 | + tests := map[string]struct { |
| 44 | + wasmFile string |
| 45 | + msg CountSudoMsg |
| 46 | + noContractSet bool |
| 47 | + |
| 48 | + expectedError error |
| 49 | + }{ |
| 50 | + "contract consumes less than limit": { |
| 51 | + wasmFile: counterContractPath, |
| 52 | + msg: CountSudoMsg{ |
| 53 | + Count: CountMsg{ |
| 54 | + // Consumes roughly 100k gas, which should be comfortably under the limit. |
| 55 | + Amount: 10, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + "contract that consumes more than limit": { |
| 60 | + wasmFile: counterContractPath, |
| 61 | + msg: CountSudoMsg{ |
| 62 | + Count: CountMsg{ |
| 63 | + // Consumes roughly 1B gas, which is well above the 30M limit. |
| 64 | + Amount: 100000, |
| 65 | + }, |
| 66 | + }, |
| 67 | + expectedError: fmt.Errorf("contract call ran out of gas"), |
| 68 | + }, |
| 69 | + } |
| 70 | + for name, tc := range tests { |
| 71 | + s.Run(name, func() { |
| 72 | + s.Setup() |
| 73 | + |
| 74 | + // We use a gov permissioned contract keeper to avoid having to manually set permissions |
| 75 | + contractKeeper := wasmkeeper.NewGovPermissionKeeper(s.App.WasmKeeper) |
| 76 | + |
| 77 | + // Upload and instantiate wasm code |
| 78 | + _, cosmwasmAddressBech32 := s.uploadAndInstantiateContract(contractKeeper, tc.wasmFile) |
| 79 | + |
| 80 | + // System under test |
| 81 | + response, err := cosmwasm.Sudo[CountSudoMsg, CountMsgResponse](s.Ctx, contractKeeper, cosmwasmAddressBech32, tc.msg) |
| 82 | + |
| 83 | + if tc.expectedError != nil { |
| 84 | + s.Require().ErrorContains(err, tc.expectedError.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + s.Require().NoError(err) |
| 89 | + s.Require().Equal(CountMsgResponse{}, response) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// uploadAndInstantiateContract is a helper function to upload and instantiate a contract from a given file path. |
| 95 | +// It calls an empty Instantiate message on the created contract and returns the bech32 address after instantiation. |
| 96 | +func (s *KeeperTestSuite) uploadAndInstantiateContract(contractKeeper *wasmkeeper.PermissionedKeeper, filePath string) (rawCWAddr sdk.AccAddress, bech32CWAddr string) { |
| 97 | + // Upload and instantiate wasm code |
| 98 | + wasmCode, err := os.ReadFile(filePath) |
| 99 | + s.Require().NoError(err) |
| 100 | + codeID, _, err := contractKeeper.Create(s.Ctx, s.TestAccs[0], wasmCode, nil) |
| 101 | + s.Require().NoError(err) |
| 102 | + rawCWAddr, _, err = contractKeeper.Instantiate(s.Ctx, codeID, s.TestAccs[0], s.TestAccs[0], []byte("{}"), "", sdk.NewCoins()) |
| 103 | + s.Require().NoError(err) |
| 104 | + bech32CWAddr, err = sdk.Bech32ifyAddressBytes("osmo", rawCWAddr) |
| 105 | + s.Require().NoError(err) |
| 106 | + |
| 107 | + return rawCWAddr, bech32CWAddr |
| 108 | +} |
0 commit comments