Skip to content

Commit

Permalink
Added tests for classic addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Oct 12, 2023
1 parent 90d4368 commit 1445baa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion x/wasm/keeper/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func PredicableAddressGenerator(creator sdk.AccAddress, salt, msg []byte, fixMsg
}
}

// BuildContractAddressClassic builds an sdk account address for a contract.
// BuildContractAddressClassic builds an address for a contract.
func BuildContractAddressClassic(codeID, instanceID uint64) sdk.AccAddress {
contractID := make([]byte, 16)
binary.BigEndian.PutUint64(contractID[:8], codeID)
Expand Down
74 changes: 73 additions & 1 deletion x/wasm/keeper/addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,79 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestBuildContractAddress(t *testing.T) {
func TestBuildContractAddressClassic(t *testing.T) {
// preserve current Bech32 settings and make sure they will be restores when this test finishes
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
t.Cleanup(func() {
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
})
// set custom Bech32 settings
sdk.GetConfig().SetBech32PrefixForAccount("juno", "juno")
// prepare test data
type Spec struct {
In struct {
CodeId uint64 `json:"codeId"`
InstanceId uint64 `json:"InstanceId"`
} `json:"in"`
Out struct {
Address sdk.AccAddress `json:"address"`
} `json:"out"`
}
var specs []Spec
require.NoError(t, json.Unmarshal([]byte(goldenMasterClassicContractAddr), &specs))
require.NotEmpty(t, specs)
// run test on prepared test data
for i, spec := range specs {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
// when
gotAddr := BuildContractAddressClassic(spec.In.CodeId, spec.In.InstanceId)
// then
require.Equal(t, spec.Out.Address.String(), gotAddr.String())
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
})
}
}

const goldenMasterClassicContractAddr = `[
{
"in": {
"codeId": 0,
"instanceId": 0
},
"out": {
"address": "juno1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qytf73z"
}
},
{
"in": {
"codeId": 0,
"instanceId": 1
},
"out": {
"address": "juno156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04sej49ew"
}
},
{
"in": {
"codeId": 1,
"instanceId": 0
},
"out": {
"address": "juno1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfsenlwu2"
}
},
{
"in": {
"codeId": 1,
"instanceId": 1
},
"out": {
"address": "juno14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9skjuwg8"
}
}
]`

func TestBuildContractAddressPredictable(t *testing.T) {
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
t.Cleanup(func() {
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
Expand Down

0 comments on commit 1445baa

Please sign in to comment.