-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
195 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
) | ||
|
||
func TestInstantiate2(t *testing.T) { | ||
parentCtx, keepers := CreateTestInput(t, false, AvailableCapabilities) | ||
example := StoreHackatomExampleContract(t, parentCtx, keepers) | ||
otherExample := StoreReflectContract(t, parentCtx, keepers) | ||
|
||
verifierAddr := RandomAccountAddress(t) | ||
beneficiaryAddr := RandomAccountAddress(t) | ||
initMsg := mustMarshal(t, HackatomExampleInitMsg{Verifier: verifierAddr, Beneficiary: beneficiaryAddr}) | ||
|
||
otherAddr := keepers.Faucet.NewFundedRandomAccount(parentCtx, sdk.NewInt64Coin("denom", 1_000_000_000)) | ||
|
||
// create instances for duplicate checks | ||
exampleContract := func(t *testing.T, ctx sdk.Context, fixMsg bool) { | ||
_, _, err := keepers.ContractKeeper.Instantiate2( | ||
ctx, | ||
example.CodeID, | ||
example.CreatorAddr, | ||
nil, | ||
initMsg, | ||
"my label", | ||
sdk.NewCoins(sdk.NewInt64Coin("denom", 1)), | ||
[]byte(`my salt`), | ||
fixMsg, | ||
) | ||
require.NoError(t, err) | ||
} | ||
exampleWithFixMsg := func(t *testing.T, ctx sdk.Context) { | ||
exampleContract(t, ctx, true) | ||
} | ||
exampleWithoutFixMsg := func(t *testing.T, ctx sdk.Context) { | ||
exampleContract(t, ctx, false) | ||
} | ||
specs := map[string]struct { | ||
setup func(t *testing.T, ctx sdk.Context) | ||
codeID uint64 | ||
sender sdk.AccAddress | ||
salt []byte | ||
initMsg json.RawMessage | ||
fixMsg bool | ||
expErr error | ||
}{ | ||
"fix msg - generates different address than without fixMsg": { | ||
setup: exampleWithoutFixMsg, | ||
codeID: example.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: initMsg, | ||
fixMsg: true, | ||
}, | ||
"fix msg - different sender": { | ||
setup: exampleWithFixMsg, | ||
codeID: example.CodeID, | ||
sender: otherAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: initMsg, | ||
fixMsg: true, | ||
}, | ||
"fix msg - different code": { | ||
setup: exampleWithFixMsg, | ||
codeID: otherExample.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: []byte(`{}`), | ||
fixMsg: true, | ||
}, | ||
"fix msg - different salt": { | ||
setup: exampleWithFixMsg, | ||
codeID: example.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`other salt`), | ||
initMsg: initMsg, | ||
fixMsg: true, | ||
}, | ||
"fix msg - different init msg": { | ||
setup: exampleWithFixMsg, | ||
codeID: example.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: mustMarshal(t, HackatomExampleInitMsg{Verifier: otherAddr, Beneficiary: beneficiaryAddr}), | ||
fixMsg: true, | ||
}, | ||
"different sender": { | ||
setup: exampleWithoutFixMsg, | ||
codeID: example.CodeID, | ||
sender: otherAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: initMsg, | ||
}, | ||
"different code": { | ||
setup: exampleWithoutFixMsg, | ||
codeID: otherExample.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: []byte(`{}`), | ||
}, | ||
"different salt": { | ||
setup: exampleWithoutFixMsg, | ||
codeID: example.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`other salt`), | ||
initMsg: initMsg, | ||
}, | ||
"different init msg - reject same address": { | ||
setup: exampleWithoutFixMsg, | ||
codeID: example.CodeID, | ||
sender: example.CreatorAddr, | ||
salt: []byte(`my salt`), | ||
initMsg: mustMarshal(t, HackatomExampleInitMsg{Verifier: otherAddr, Beneficiary: beneficiaryAddr}), | ||
expErr: types.ErrDuplicate, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
ctx, _ := parentCtx.CacheContext() | ||
spec.setup(t, ctx) | ||
gotAddr, _, gotErr := keepers.ContractKeeper.Instantiate2( | ||
ctx, | ||
spec.codeID, | ||
spec.sender, | ||
nil, | ||
spec.initMsg, | ||
"my label", | ||
sdk.NewCoins(sdk.NewInt64Coin("denom", 2)), | ||
spec.salt, | ||
spec.fixMsg, | ||
) | ||
if spec.expErr != nil { | ||
assert.ErrorIs(t, gotErr, spec.expErr) | ||
return | ||
} | ||
require.NoError(t, gotErr) | ||
assert.NotEmpty(t, gotAddr) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package wasmtesting | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
type MockCoinTransferrer struct { | ||
TransferCoinsFn func(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error | ||
} | ||
|
||
func (m *MockCoinTransferrer) TransferCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error { | ||
if m.TransferCoinsFn == nil { | ||
panic("not expected to be called") | ||
} | ||
return m.TransferCoinsFn(ctx, fromAddr, toAddr, amt) | ||
} | ||
|
||
type AccountPrunerMock struct { | ||
CleanupExistingAccountFn func(ctx sdk.Context, existingAccount authtypes.AccountI) (handled bool, err error) | ||
} | ||
|
||
func (m AccountPrunerMock) CleanupExistingAccount(ctx sdk.Context, existingAccount authtypes.AccountI) (handled bool, err error) { | ||
if m.CleanupExistingAccountFn == nil { | ||
panic("not expected to be called") | ||
} | ||
return m.CleanupExistingAccountFn(ctx, existingAccount) | ||
} |