forked from crypto-org-chain/chain-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_group.py
221 lines (189 loc) · 5.44 KB
/
test_group.py
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
import json
import pytest
pytestmark = pytest.mark.normal
def test_group(cluster, tmp_path):
cli = cluster.cosmos_cli()
admin = cluster.address("community")
signer1 = cluster.address("signer1")
signer2 = cluster.address("signer2")
# create group
members_file = tmp_path / "members.json"
members = {
"members": [
{"address": admin, "weight": "1", "metadata": "admin"},
{"address": signer1, "weight": "1", "metadata": "signer1"},
{"address": signer2, "weight": "1", "metadata": "signer2"},
]
}
with open(members_file, "w") as f:
json.dump(members, f)
rsp = json.loads(
cli.raw(
"tx",
"group",
"create-group",
admin,
"admin",
members_file,
"-y",
_from="community",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
# Get group id from events
group_id = json.loads(
json.loads(rsp["raw_log"])[0]["events"][0]["attributes"][0]["value"]
)
# create group policy
policy_file = tmp_path / "policy.json"
policy = {
"@type": "/cosmos.group.v1.ThresholdDecisionPolicy",
"threshold": "2",
"windows": {"voting_period": "120h", "min_execution_period": "0s"},
}
with open(policy_file, "w") as f:
json.dump(policy, f)
rsp = json.loads(
cli.raw(
"tx",
"group",
"create-group-policy",
admin,
group_id,
"group-policy",
policy_file,
"-y",
_from="community",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
group_policy_address = json.loads(
json.loads(rsp["raw_log"])[0]["events"][0]["attributes"][0]["value"]
)
# submit a proposal
proposal_file = tmp_path / "proposal.json"
proposal = {
"group_policy_address": group_policy_address,
"messages": [
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": group_policy_address,
"to_address": signer1,
"amount": [{"denom": "basecro", "amount": "100000000"}],
}
],
"metadata": "proposal",
"proposers": [admin],
}
with open(proposal_file, "w") as f:
json.dump(proposal, f)
rsp = json.loads(
cli.raw(
"tx",
"group",
"submit-proposal",
proposal_file,
"-y",
_from="community",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
proposal_id = json.loads(
json.loads(rsp["raw_log"])[0]["events"][0]["attributes"][0]["value"]
)
# vote on proposal
rsp = json.loads(
cli.raw(
"tx",
"group",
"vote",
proposal_id,
signer1,
"VOTE_OPTION_YES",
"vote-signer1-yes",
"-y",
_from="signer1",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
rsp = json.loads(
cli.raw(
"tx",
"group",
"vote",
proposal_id,
signer2,
"VOTE_OPTION_YES",
"vote-signer2-yes",
"-y",
_from="signer2",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
# query proposal votes
rsp = json.loads(
cli.raw(
"query",
"group",
"votes-by-proposal",
proposal_id,
home=cli.data_dir,
node=cli.node_rpc,
chain_id=cli.chain_id,
)
)
assert len(rsp["votes"]) == 2, rsp
# transfer some amount to group policy address
cluster.transfer(admin, group_policy_address, "1cro")
group_policy_balance = cluster.balance(group_policy_address)
signer1_balance = cluster.balance(signer1)
# execute proposal
rsp = json.loads(
cli.raw(
"tx",
"group",
"exec",
proposal_id,
"-y",
_from="community",
home=cli.data_dir,
node=cli.node_rpc,
keyring_backend="test",
chain_id=cli.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
# check if the proposal executed successfully
assert (
json.loads(
list(
filter(
lambda attribute: attribute["key"] == "result",
json.loads(rsp["raw_log"])[0]["events"][2]["attributes"],
)
)[0]["value"],
)
== "PROPOSAL_EXECUTOR_RESULT_SUCCESS"
), rsp["raw_log"]
assert group_policy_balance - 100000000 == cluster.balance(group_policy_address)
assert signer1_balance + 100000000 == cluster.balance(signer1)