@@ -11,75 +11,67 @@ import (
11
11
sdk "github.com/cosmos/cosmos-sdk/types"
12
12
)
13
13
14
- func TestBuildContractAddressClassic (t * testing.T ) {
14
+ func PrepareCleanup (t * testing.T ) {
15
15
// preserve current Bech32 settings and restore them after test completion
16
16
x , y := sdk .GetConfig ().GetBech32AccountAddrPrefix (), sdk .GetConfig ().GetBech32AccountPubPrefix ()
17
17
c := sdk .IsAddrCacheEnabled ()
18
18
t .Cleanup (func () {
19
19
sdk .GetConfig ().SetBech32PrefixForAccount (x , y )
20
20
sdk .SetAddrCacheEnabled (c )
21
21
})
22
-
23
22
// set custom Bech32 settings
24
23
sdk .GetConfig ().SetBech32PrefixForAccount ("purple" , "purple" )
25
24
// disable address cache
26
25
// AccAddress -> String conversion is then slower, but does not lead to errors like this:
27
26
// runtime error: invalid memory address or nil pointer dereference
28
27
sdk .SetAddrCacheEnabled (false )
28
+ }
29
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
+ }
30
60
// run tests
31
- for i , spec := range classicContractAddr {
61
+ for i , spec := range specs {
32
62
t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
33
63
// when
34
64
gotAddr := BuildContractAddressClassic (spec .codeId , spec .instanceId )
35
65
// then
36
- require .Equal (t , spec .address , gotAddr .String ())
66
+ require .Equal (t , spec .expAddress , gotAddr .String ())
37
67
require .NoError (t , sdk .VerifyAddressFormat (gotAddr ))
38
68
})
39
69
}
40
70
}
41
71
42
- var classicContractAddr = []struct {
43
- codeId uint64
44
- instanceId uint64
45
- address string
46
- }{
47
- {
48
- codeId : 0 ,
49
- instanceId : 0 ,
50
- address : "purple1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qfplul7" ,
51
- },
52
- {
53
- codeId : 0 ,
54
- instanceId : 1 ,
55
- address : "purple156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04s5cr8hj" ,
56
- },
57
- {
58
- codeId : 1 ,
59
- instanceId : 0 ,
60
- address : "purple1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfs5efvjk" ,
61
- },
62
- {
63
- codeId : 1 ,
64
- instanceId : 1 ,
65
- address : "purple14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9smc2vxm" ,
66
- },
67
- }
68
-
69
72
func TestBuildContractAddressPredictable (t * testing.T ) {
70
- x , y := sdk .GetConfig ().GetBech32AccountAddrPrefix (), sdk .GetConfig ().GetBech32AccountPubPrefix ()
71
- c := sdk .IsAddrCacheEnabled ()
72
- t .Cleanup (func () {
73
- sdk .GetConfig ().SetBech32PrefixForAccount (x , y )
74
- sdk .SetAddrCacheEnabled (c )
75
- })
76
- // set custom Bech32 settings
77
- sdk .GetConfig ().SetBech32PrefixForAccount ("purple" , "purple" )
78
- // disable address cache
79
- // AccAddress -> String conversion is then slower, but does not lead to errors like this:
80
- // runtime error: invalid memory address or nil pointer dereference
81
- sdk .SetAddrCacheEnabled (false )
82
-
73
+ // set cleanup function
74
+ PrepareCleanup (t )
83
75
// test vectors generated via cosmjs: https://github.com/cosmos/cosmjs/pull/1253/files
84
76
type Spec struct {
85
77
In struct {
0 commit comments