-
Notifications
You must be signed in to change notification settings - Fork 351
/
test_feegrant.py
284 lines (233 loc) · 8.8 KB
/
test_feegrant.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import datetime
import pytest
from dateutil.parser import isoparse
from .utils import (
BASECRO_DENOM,
SUCCESS_CODE,
grant_fee_allowance,
query_block_info,
revoke_fee_grant,
transfer,
wait_for_block,
wait_for_block_time,
)
pytestmark = pytest.mark.normal
def test_basic_fee_allowance(cluster):
"""
check basic fee allowance with no limit or grant expiry
"""
transaction_coins = 100
fee_coins = 10
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
fee_granter_balance = cluster.balance(fee_granter_address)
fee_grantee_balance = cluster.balance(fee_grantee_address)
receiver_balance = cluster.balance(receiver_address)
grant_fee_allowance(cluster, fee_granter_address, fee_grantee_address)
transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
assert cluster.balance(fee_granter_address) == fee_granter_balance - fee_coins
assert (
cluster.balance(fee_grantee_address) == fee_grantee_balance - transaction_coins
)
assert cluster.balance(receiver_address) == receiver_balance + transaction_coins
def test_tx_failed_when_exceeds_grant_fee(cluster):
"""
check transaction should fail when tx fee exceeds fee limit in basic fee allowance
"""
transaction_coins = 100
fee_coins = 10
fee_grant_spend_limit = 5
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
fee_granter_balance = cluster.balance(fee_granter_address)
fee_grantee_balance = cluster.balance(fee_grantee_address)
receiver_balance = cluster.balance(receiver_address)
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
grant_fee_allowance(
cluster,
fee_granter_address,
fee_grantee_address,
spend_limit="%s%s" % (fee_grant_spend_limit, BASECRO_DENOM),
)
tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
assert tx["code"] != SUCCESS_CODE, "should fail as fee limit exceeded"
assert cluster.balance(fee_granter_address) == fee_granter_balance
assert cluster.balance(fee_grantee_address) == fee_grantee_balance
assert cluster.balance(receiver_address) == receiver_balance
def test_tx_failed_after_grant_expiration(cluster):
"""
check transaction should fail when tx happens after grant expiry
"""
transaction_coins = 100
fee_coins = 10
# RFC 3339 timestamp
grant_expiration = datetime.datetime.utcnow().isoformat() + "Z"
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
fee_granter_balance = cluster.balance(fee_granter_address)
fee_grantee_balance = cluster.balance(fee_grantee_address)
receiver_balance = cluster.balance(receiver_address)
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
grant_fee_allowance(
cluster, fee_granter_address, fee_grantee_address, expiration=grant_expiration
)
tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
assert tx["code"] != SUCCESS_CODE, "should fail as fee allowance expired"
assert cluster.balance(fee_granter_address) == fee_granter_balance
assert cluster.balance(fee_grantee_address) == fee_grantee_balance
assert cluster.balance(receiver_address) == receiver_balance
def test_periodic_fee_allowance(cluster):
"""
check periodic fee allowance with no expiration
"""
transaction_coins = 100
fee_coins = 10
period = 5
period_limit = 11
number_of_periods = 3
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
fee_granter_balance = cluster.balance(fee_granter_address)
fee_grantee_balance = cluster.balance(fee_grantee_address)
receiver_balance = cluster.balance(receiver_address)
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
grant_fee_allowance(
cluster,
fee_granter_address,
fee_grantee_address,
period_limit="%s%s" % (period_limit, BASECRO_DENOM),
period=period,
)
for _ in range(number_of_periods):
tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
wait_for_block(cluster, int(tx["height"]) + 1) # wait for next block
block_info = query_block_info(cluster, tx["height"])
wait_for_block_time(
cluster,
isoparse(block_info["header"]["time"]) + datetime.timedelta(seconds=period),
)
assert (
cluster.balance(fee_granter_address)
== fee_granter_balance - fee_coins * number_of_periods
)
assert (
cluster.balance(fee_grantee_address)
== fee_grantee_balance - transaction_coins * number_of_periods
)
assert (
cluster.balance(receiver_address)
== receiver_balance + transaction_coins * number_of_periods
)
def test_exceed_period_limit_should_not_affect_the_next_period(cluster):
"""
check exceeding periodic fee should not affect next period
"""
transaction_coins = 100
fee_coins = 10
period = 5
period_limit = 11
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
fee_granter_balance = cluster.balance(fee_granter_address)
fee_grantee_balance = cluster.balance(fee_grantee_address)
receiver_balance = cluster.balance(receiver_address)
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
grant_fee_allowance(
cluster,
fee_granter_address,
fee_grantee_address,
period_limit="%s%s" % (period_limit, BASECRO_DENOM),
period=period,
)
tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
failed_tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
assert failed_tx["code"] != SUCCESS_CODE, "should fail as fee exceeds period limit"
wait_for_block(cluster, int(tx["height"]))
block_info = query_block_info(cluster, tx["height"])
wait_for_block_time(
cluster,
isoparse(block_info["header"]["time"]) + datetime.timedelta(seconds=period),
)
transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
# transaction only happened two times
assert cluster.balance(fee_granter_address) == fee_granter_balance - fee_coins * 2
assert (
cluster.balance(fee_grantee_address)
== fee_grantee_balance - transaction_coins * 2
)
assert cluster.balance(receiver_address) == receiver_balance + transaction_coins * 2
def test_revoke_fee_grant(cluster):
"""
check tx should fail after fee grant is revoked
"""
transaction_coins = 100
fee_coins = 10
fee_granter_address = cluster.address("community")
fee_grantee_address = cluster.address("ecosystem")
receiver_address = cluster.address("reserve")
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
grant_fee_allowance(cluster, fee_granter_address, fee_grantee_address)
revoke_fee_grant(cluster, fee_granter_address, fee_grantee_address)
failed_tx = transfer(
cluster,
fee_grantee_address,
receiver_address,
"%s%s" % (transaction_coins, BASECRO_DENOM),
fees="%s%s" % (fee_coins, BASECRO_DENOM),
fee_granter=fee_granter_address,
)
assert failed_tx["code"] != SUCCESS_CODE, "should fail as grant is revoked"