@@ -3,21 +3,75 @@ package keeper
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ tmbytes "github.com/cometbft/cometbft/libs/bytes"
6
7
"testing"
7
8
8
- tmbytes "github.com/cometbft/cometbft/libs/bytes"
9
9
"github.com/stretchr/testify/require"
10
10
11
11
sdk "github.com/cosmos/cosmos-sdk/types"
12
12
)
13
13
14
- func TestBuildContractAddress (t * testing.T ) {
14
+ func prepareCleanup (t * testing.T ) {
15
+ // preserve current Bech32 settings and restore them after test completion
15
16
x , y := sdk .GetConfig ().GetBech32AccountAddrPrefix (), sdk .GetConfig ().GetBech32AccountPubPrefix ()
17
+ c := sdk .IsAddrCacheEnabled ()
16
18
t .Cleanup (func () {
17
19
sdk .GetConfig ().SetBech32PrefixForAccount (x , y )
20
+ sdk .SetAddrCacheEnabled (c )
18
21
})
22
+ // set custom Bech32 settings
19
23
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
+ }
20
29
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 )
21
75
// test vectors generated via cosmjs: https://github.com/cosmos/cosmjs/pull/1253/files
22
76
type Spec struct {
23
77
In struct {
@@ -37,7 +91,7 @@ func TestBuildContractAddress(t *testing.T) {
37
91
t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
38
92
// when
39
93
gotAddr := BuildContractAddressPredictable (spec .In .Checksum , spec .In .Creator , spec .In .Salt .Bytes (), []byte (spec .In .Msg ))
40
-
94
+ // then
41
95
require .Equal (t , spec .Out .Address .String (), gotAddr .String ())
42
96
require .NoError (t , sdk .VerifyAddressFormat (gotAddr ))
43
97
})
0 commit comments