forked from noble-assets/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparamauthority_test.go
269 lines (235 loc) · 8.12 KB
/
paramauthority_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package interchaintest_test
import (
"context"
"encoding/json"
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
"github.com/icza/dyno"
"github.com/strangelove-ventures/interchaintest/v3"
"github.com/strangelove-ventures/interchaintest/v3/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v3/ibc"
"github.com/strangelove-ventures/interchaintest/v3/testreporter"
"github.com/strangelove-ventures/noble/cmd"
integration "github.com/strangelove-ventures/noble/interchaintest"
proposaltypes "github.com/strangelove-ventures/paramauthority/x/params/types/proposal"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
type ParamsCase struct {
title string
description string
newAuthority string
msgAuthority string
signer ibc.Wallet
shouldSucceed bool
}
func testParamsCase(t *testing.T, ctx context.Context, broadcaster *cosmos.Broadcaster, testCase ParamsCase) {
t.Logf(
"SIGNER: %s\nMSG AUTHORITY: %s\n",
testCase.signer.Address,
testCase.msgAuthority,
)
msgUpdateParams := proposaltypes.MsgUpdateParams{
ChangeProposal: proposal.NewParameterChangeProposal(
testCase.title,
testCase.description,
[]proposal.ParamChange{
{
Subspace: "params",
Key: "authority",
Value: fmt.Sprintf(`"%s"`, testCase.newAuthority),
},
}),
Authority: testCase.msgAuthority,
}
decoded := sdk.MustAccAddressFromBech32(testCase.signer.Address)
wallet := &ibc.Wallet{
Address: string(decoded),
Mnemonic: testCase.signer.Mnemonic,
KeyName: testCase.signer.KeyName,
CoinType: testCase.signer.CoinType,
}
tx, err := cosmos.BroadcastTx(
ctx,
broadcaster,
wallet,
&msgUpdateParams,
)
require.NoError(t, err, "failed to broadcast tx")
t.Logf("TX: %+v\n", tx)
if testCase.shouldSucceed {
require.Equal(t, uint32(0), tx.Code, "changing authority failed")
} else {
require.NotEqual(t, uint32(0), tx.Code, "changing authority succeeded when it should have failed")
}
}
func TestNobleParamAuthority(t *testing.T) {
if testing.Short() {
t.Skip()
}
t.Parallel()
ctx := context.Background()
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)
client, network := interchaintest.DockerSetup(t)
repo, version := integration.GetDockerImageInfo()
var noble *cosmos.CosmosChain
var roles NobleRoles
var paramauthorityWallet Authority
var extraWallets ExtraWallets
chainCfg := ibc.ChainConfig{
Type: "cosmos",
Name: "noble",
ChainID: "noble-1",
Bin: "nobled",
Denom: "token",
Bech32Prefix: "noble",
CoinType: "118",
GasPrices: "0.0token",
GasAdjustment: 1.1,
TrustingPeriod: "504h",
NoHostMount: false,
Images: []ibc.DockerImage{
{
Repository: repo,
Version: version,
UidGid: "1025:1025",
},
},
EncodingConfig: NobleEncoding(),
PreGenesis: func(cc ibc.ChainConfig) error {
val := noble.Validators[0]
err := createTokenfactoryRoles(ctx, &roles, DenomMetadata_rupee, val, true)
if err != nil {
return err
}
err = createTokenfactoryRoles(ctx, &roles, DenomMetadata_drachma, val, true)
if err != nil {
return err
}
extraWallets, err = createExtraWalletsAtGenesis(ctx, val)
if err != nil {
return err
}
paramauthorityWallet, err = createParamAuthAtGenesis(ctx, val)
return err
},
ModifyGenesis: func(cc ibc.ChainConfig, b []byte) ([]byte, error) {
g := make(map[string]interface{})
if err := json.Unmarshal(b, &g); err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis file: %w", err)
}
err := modifyGenesisTokenfactory(g, "tokenfactory", DenomMetadata_rupee, &roles, true)
if err != nil {
return nil, err
}
err = modifyGenesisTokenfactory(g, "fiat-tokenfactory", DenomMetadata_drachma, &roles, true)
if err != nil {
return nil, err
}
err = modifyGenesisParamAuthority(g, paramauthorityWallet.Authority.Address)
if err != nil {
return nil, err
}
out, err := json.Marshal(&g)
if err != nil {
return nil, fmt.Errorf("failed to marshal genesis bytes to json: %w", err)
}
return out, nil
},
}
nv := 1
nf := 0
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
ChainConfig: chainCfg,
NumValidators: &nv,
NumFullNodes: &nf,
},
})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
noble = chains[0].(*cosmos.CosmosChain)
ic := interchaintest.NewInterchain().
AddChain(noble)
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
})
cmd.SetPrefixes(chainCfg.Bech32Prefix)
broadcaster := cosmos.NewBroadcaster(t, noble)
var orderedTestCases = []ParamsCase{
{
title: "change authority to alice from incorrect msg authority and signer",
description: "change params and upgrade authority to alice's address",
newAuthority: extraWallets.Alice.Address,
msgAuthority: extraWallets.User.Address, // matches signer, but this is not the params authority.
signer: extraWallets.User,
shouldSucceed: false,
},
{
title: "change authority to alice from correct signer but incorrect msg authority",
description: "change params and upgrade authority to alice's address",
newAuthority: extraWallets.Alice.Address,
msgAuthority: extraWallets.User.Address, // this is not the params authority.
signer: paramauthorityWallet.Authority, // this is the params authority, but does not match msgAuthority
shouldSucceed: false,
},
{
title: "change authority to alice from correct msg authority but incorrect signer",
description: "change params and upgrade authority to alice's address",
newAuthority: extraWallets.Alice.Address,
msgAuthority: paramauthorityWallet.Authority.Address, // this is the params authority.
signer: extraWallets.User, // this is not the params authority.
shouldSucceed: false,
},
{
title: "change authority to alice from correct signer and msg authority",
description: "change params and upgrade authority to alice's address",
newAuthority: extraWallets.Alice.Address,
msgAuthority: paramauthorityWallet.Authority.Address, // this is the params authority.
signer: paramauthorityWallet.Authority, // this is the params authority.
shouldSucceed: true,
},
{
title: "change authority to user2 from prior authority",
description: "change params and upgrade authority to user2's address",
newAuthority: extraWallets.User2.Address,
msgAuthority: paramauthorityWallet.Authority.Address, // this account is no longer the params authority.
signer: paramauthorityWallet.Authority, // this account is no longer the params authority.
shouldSucceed: false,
},
{
title: "change authority to user2 from new authority",
description: "change params and upgrade authority to user2's address",
newAuthority: extraWallets.User2.Address,
msgAuthority: extraWallets.Alice.Address, // this account is the new params authority.
signer: extraWallets.Alice, // this account is the new params authority.
shouldSucceed: true,
},
}
for _, testCase := range orderedTestCases {
t.Run(testCase.title, func(t *testing.T) {
testParamsCase(t, ctx, broadcaster, testCase)
})
}
height, err := noble.Height(ctx)
require.NoError(t, err, "failed to get noble height")
err = noble.StopAllNodes(ctx)
require.NoError(t, err, "failed to stop noble chain")
state, err := noble.ExportState(ctx, int64(height))
require.NoError(t, err, "failed to export noble state")
var gs interface{}
err = json.Unmarshal([]byte(state), &gs)
require.NoError(t, err, "failed to unmarshal state export")
authority, err := dyno.Get(gs, "app_state", "params", "params", "authority")
require.NoError(t, err, "failed to get authority from state export")
require.Equal(t, extraWallets.User2.Address, authority, "authority does not match")
}