Skip to content

Commit c8a6594

Browse files
authored
Merge pull request #1666 from CosmWasm/classic-address-tests
Classic address tests
2 parents 90d4368 + 257cd7e commit c8a6594

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

x/wasm/keeper/addresses.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func PredicableAddressGenerator(creator sdk.AccAddress, salt, msg []byte, fixMsg
3232
}
3333
}
3434

35-
// BuildContractAddressClassic builds an sdk account address for a contract.
35+
// BuildContractAddressClassic builds an address for a contract.
3636
func BuildContractAddressClassic(codeID, instanceID uint64) sdk.AccAddress {
3737
contractID := make([]byte, 16)
3838
binary.BigEndian.PutUint64(contractID[:8], codeID)

x/wasm/keeper/addresses_test.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,75 @@ package keeper
33
import (
44
"encoding/json"
55
"fmt"
6+
tmbytes "github.com/cometbft/cometbft/libs/bytes"
67
"testing"
78

8-
tmbytes "github.com/cometbft/cometbft/libs/bytes"
99
"github.com/stretchr/testify/require"
1010

1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212
)
1313

14-
func TestBuildContractAddress(t *testing.T) {
14+
func prepareCleanup(t *testing.T) {
15+
// preserve current Bech32 settings and restore them after test completion
1516
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
17+
c := sdk.IsAddrCacheEnabled()
1618
t.Cleanup(func() {
1719
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
20+
sdk.SetAddrCacheEnabled(c)
1821
})
22+
// set custom Bech32 settings
1923
sdk.GetConfig().SetBech32PrefixForAccount("purple", "purple")
24+
// disable address cache
25+
// AccAddress -> String conversion is then slower, but does not lead to errors like this:
26+
// runtime error: invalid memory address or nil pointer dereference
27+
sdk.SetAddrCacheEnabled(false)
28+
}
2029

30+
func TestBuildContractAddressClassic(t *testing.T) {
31+
// set cleanup function
32+
prepareCleanup(t)
33+
// prepare test data
34+
specs := []struct {
35+
codeId uint64
36+
instanceId uint64
37+
expAddress string
38+
}{
39+
{
40+
codeId: 0,
41+
instanceId: 0,
42+
expAddress: "purple1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qfplul7",
43+
},
44+
{
45+
codeId: 0,
46+
instanceId: 1,
47+
expAddress: "purple156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04s5cr8hj",
48+
},
49+
{
50+
codeId: 1,
51+
instanceId: 0,
52+
expAddress: "purple1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfs5efvjk",
53+
},
54+
{
55+
codeId: 1,
56+
instanceId: 1,
57+
expAddress: "purple14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9smc2vxm",
58+
},
59+
}
60+
// run tests
61+
for i, spec := range specs {
62+
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
63+
// when
64+
gotAddr := BuildContractAddressClassic(spec.codeId, spec.instanceId)
65+
// then
66+
require.Equal(t, spec.expAddress, gotAddr.String())
67+
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
68+
})
69+
}
70+
}
71+
72+
func TestBuildContractAddressPredictable(t *testing.T) {
73+
// set cleanup function
74+
prepareCleanup(t)
2175
// test vectors generated via cosmjs: https://github.com/cosmos/cosmjs/pull/1253/files
2276
type Spec struct {
2377
In struct {
@@ -37,7 +91,7 @@ func TestBuildContractAddress(t *testing.T) {
3791
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
3892
// when
3993
gotAddr := BuildContractAddressPredictable(spec.In.Checksum, spec.In.Creator, spec.In.Salt.Bytes(), []byte(spec.In.Msg))
40-
94+
// then
4195
require.Equal(t, spec.Out.Address.String(), gotAddr.String())
4296
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
4397
})

0 commit comments

Comments
 (0)