Merge pull request #1666 from CosmWasm/classic-address-tests

Classic address tests
This commit is contained in:
Dariusz Depta
2023-10-13 10:01:22 +02:00
committed by GitHub
2 changed files with 58 additions and 4 deletions

View File

@@ -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)

View File

@@ -3,21 +3,75 @@ package keeper
import (
"encoding/json"
"fmt"
tmbytes "github.com/cometbft/cometbft/libs/bytes"
"testing"
tmbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestBuildContractAddress(t *testing.T) {
func prepareCleanup(t *testing.T) {
// preserve current Bech32 settings and restore them after test completion
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
c := sdk.IsAddrCacheEnabled()
t.Cleanup(func() {
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
sdk.SetAddrCacheEnabled(c)
})
// set custom Bech32 settings
sdk.GetConfig().SetBech32PrefixForAccount("purple", "purple")
// disable address cache
// AccAddress -> String conversion is then slower, but does not lead to errors like this:
// runtime error: invalid memory address or nil pointer dereference
sdk.SetAddrCacheEnabled(false)
}
func TestBuildContractAddressClassic(t *testing.T) {
// set cleanup function
prepareCleanup(t)
// prepare test data
specs := []struct {
codeId uint64
instanceId uint64
expAddress string
}{
{
codeId: 0,
instanceId: 0,
expAddress: "purple1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qfplul7",
},
{
codeId: 0,
instanceId: 1,
expAddress: "purple156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04s5cr8hj",
},
{
codeId: 1,
instanceId: 0,
expAddress: "purple1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfs5efvjk",
},
{
codeId: 1,
instanceId: 1,
expAddress: "purple14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9smc2vxm",
},
}
// run tests
for i, spec := range specs {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
// when
gotAddr := BuildContractAddressClassic(spec.codeId, spec.instanceId)
// then
require.Equal(t, spec.expAddress, gotAddr.String())
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
})
}
}
func TestBuildContractAddressPredictable(t *testing.T) {
// set cleanup function
prepareCleanup(t)
// test vectors generated via cosmjs: https://github.com/cosmos/cosmjs/pull/1253/files
type Spec struct {
In struct {
@@ -37,7 +91,7 @@ func TestBuildContractAddress(t *testing.T) {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
// when
gotAddr := BuildContractAddressPredictable(spec.In.Checksum, spec.In.Creator, spec.In.Salt.Bytes(), []byte(spec.In.Msg))
// then
require.Equal(t, spec.Out.Address.String(), gotAddr.String())
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
})