-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoven-registry.py
319 lines (254 loc) · 11.8 KB
/
oven-registry.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import smartpy as sp
Addresses = sp.import_script_from_url("file:test-helpers/addresses.py")
Errors = sp.import_script_from_url("file:common/errors.py")
################################################################
# Contract
################################################################
class OvenRegistryContract(sp.Contract):
def __init__(
self,
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS,
governorContractAddress = Addresses.GOVERNOR_ADDRESS,
):
self.exception_optimization_level = "default-unit"
self.init(
governorContractAddress = governorContractAddress,
ovenFactoryContractAddress = ovenFactoryContractAddress,
ovenMap = sp.big_map(
l = {},
tkey=sp.TAddress,
tvalue=sp.TAddress
)
)
################################################################
# Public Interface
################################################################
@sp.entry_point
def isOven(self, maybeOvenAddress):
sp.verify(self.data.ovenMap.contains(maybeOvenAddress), message = Errors.NOT_OVEN)
# Verify the call did not contain a balance.
sp.verify(sp.amount == sp.mutez(0), message = Errors.AMOUNT_NOT_ALLOWED)
# Disallow direct transfers.
@sp.entry_point
def default(self, param):
sp.set_type(param, sp.TUnit)
sp.failwith(Errors.CANNOT_RECEIVE_FUNDS)
################################################################
# OvenFactory Interface
################################################################
# (oven address, owner)
@sp.entry_point
def addOven(self, param):
sp.set_type(param, sp.TPair(sp.TAddress, sp.TAddress))
sp.verify(sp.sender == self.data.ovenFactoryContractAddress, message = Errors.NOT_OVEN_FACTORY)
ovenAddress = sp.fst(param)
owner = sp.snd(param)
self.data.ovenMap[ovenAddress] = owner
################################################################
# Governance
################################################################
# Update the governor contract.
@sp.entry_point
def setGovernorContract(self, newGovernorContractAddress):
sp.set_type(newGovernorContractAddress, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.governorContractAddress = newGovernorContractAddress
# Update the oven factory contract address.
@sp.entry_point
def setOvenFactoryContract(self, newOvenFactoryContract):
sp.set_type(newOvenFactoryContract, sp.TAddress)
sp.verify(sp.sender == self.data.governorContractAddress, message = Errors.NOT_GOVERNOR)
self.data.ovenFactoryContractAddress = newOvenFactoryContract
# Only run tests if this file is main.
if __name__ == "__main__":
################################################################
################################################################
# Tests
################################################################
################################################################
################################################################
# isOven
################################################################
@sp.add_test(name="isOven - fails with unknown oven")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS
ovenRegistry = OvenRegistryContract(
ovenFactoryContractAddress = ovenFactoryContractAddress
)
scenario += ovenRegistry
# AND an oven that is registered in the OvenRegistry
ovenAddress = Addresses.OVEN_ADDRESS
ownerAddress = Addresses.OVEN_OWNER_ADDRESS
addOvenParameter = (ovenAddress, ownerAddress)
scenario += ovenRegistry.addOven(addOvenParameter).run(
sender = ovenFactoryContractAddress
)
# WHEN isOven is called for an unregistered oven THEN the invocation fails.
notOven = Addresses.NULL_ADDRESS
scenario += ovenRegistry.isOven(notOven).run(
valid = False
)
@sp.add_test(name="isOven - succeeds with known oven")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS
ovenRegistry = OvenRegistryContract(
ovenFactoryContractAddress = ovenFactoryContractAddress
)
scenario += ovenRegistry
# AND an oven that is registered in the OvenRegistry
ovenAddress = Addresses.OVEN_ADDRESS
ownerAddress = Addresses.OVEN_OWNER_ADDRESS
addOvenParameter = (ovenAddress, ownerAddress)
scenario += ovenRegistry.addOven(addOvenParameter).run(
sender = ovenFactoryContractAddress
)
# WHEN isOven is called for the oven THEN the invocation does not fail
scenario += ovenRegistry.isOven(ovenAddress)
@sp.add_test(name="isOven - fails with known oven and an amount")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS
ovenRegistry = OvenRegistryContract(
ovenFactoryContractAddress = ovenFactoryContractAddress
)
scenario += ovenRegistry
# AND an oven that is registered in the OvenRegistry
ovenAddress = Addresses.OVEN_ADDRESS
ownerAddress = Addresses.OVEN_OWNER_ADDRESS
addOvenParameter = (ovenAddress, ownerAddress)
scenario += ovenRegistry.addOven(addOvenParameter).run(
sender = ovenFactoryContractAddress
)
# WHEN isOven is called for the oven with an amount THEN the invocation does not fail
scenario += ovenRegistry.isOven(ovenAddress).run(
amount = sp.mutez(1),
valid = False
)
################################################################
# addOven
################################################################
@sp.add_test(name="addOven - fails when not called by OvenFactory")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS
ovenRegistry = OvenRegistryContract(
ovenFactoryContractAddress = ovenFactoryContractAddress
)
scenario += ovenRegistry
# WHEN an oven is added to the contract by another contract THEN the invocation fails.
notOvenFactory = Addresses.NULL_ADDRESS
ovenAddress = Addresses.OVEN_ADDRESS
ownerAddress = Addresses.OVEN_OWNER_ADDRESS
addOvenParameter = (ovenAddress, ownerAddress)
scenario += ovenRegistry.addOven(addOvenParameter).run(
sender = notOvenFactory,
valid = False
)
@sp.add_test(name="addOven - succeeds")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenFactoryContractAddress = Addresses.OVEN_FACTORY_ADDRESS
ovenRegistry = OvenRegistryContract(
ovenFactoryContractAddress = ovenFactoryContractAddress
)
scenario += ovenRegistry
# WHEN an oven is added to the contract
ovenAddress = Addresses.OVEN_ADDRESS
ownerAddress = Addresses.OVEN_OWNER_ADDRESS
addOvenParameter = (ovenAddress, ownerAddress)
scenario += ovenRegistry.addOven(addOvenParameter).run(
sender = ovenFactoryContractAddress,
)
# THEN the OvenRegistry is registered in the registry's map.
scenario.verify(ovenRegistry.data.ovenMap[ovenAddress] == ownerAddress)
# AND future calls to isOven report the oven as registered.
scenario += ovenRegistry.isOven(ovenAddress)
################################################################
# default
################################################################
@sp.add_test(name="default - fails with calls to the default entrypoint")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
ovenRegistry = OvenRegistryContract()
scenario += ovenRegistry
# WHEN a transfer is made THEN the invocation fails.
scenario += ovenRegistry.default().run(
amount = sp.mutez(0),
valid = False
)
################################################################
# setGovernorContract
################################################################
@sp.add_test(name="setGovernorContract - succeeds when called by governor")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
governorContractAddress = Addresses.GOVERNOR_ADDRESS
ovenRegistry = OvenRegistryContract(
governorContractAddress = governorContractAddress
)
scenario += ovenRegistry
# WHEN the setGovernorContract is called with a new governor contract
newGovernorContractAddress = Addresses.ROTATED_ADDRESS
scenario += ovenRegistry.setGovernorContract(newGovernorContractAddress).run(
sender = governorContractAddress,
)
# THEN the governor contract is updated.
scenario.verify(ovenRegistry.data.governorContractAddress == newGovernorContractAddress)
@sp.add_test(name="setGovernorContract - fails when not called by governor")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
governorContractAddress = Addresses.GOVERNOR_ADDRESS
ovenRegistry = OvenRegistryContract(
governorContractAddress = governorContractAddress
)
scenario += ovenRegistry
# WHEN the setGovernorContract is called by someone who isn't the governor THEN the call fails
newGovernorContractAddress = Addresses.ROTATED_ADDRESS
scenario += ovenRegistry.setGovernorContract(newGovernorContractAddress).run(
sender = Addresses.NULL_ADDRESS,
valid = False
)
################################################################
# setOvenFactoryContract
################################################################
@sp.add_test(name="setOvenFactoryContract - succeeds when called by governor")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
governorContractAddress = Addresses.GOVERNOR_ADDRESS
ovenRegistry = OvenRegistryContract(
governorContractAddress = governorContractAddress
)
scenario += ovenRegistry
# WHEN the setOvenFactoryContract is called with a new governor contract
newOvenFactoryContractAddress = Addresses.ROTATED_ADDRESS
scenario += ovenRegistry.setOvenFactoryContract(newOvenFactoryContractAddress).run(
sender = governorContractAddress,
)
# THEN the governor contract is updated.
scenario.verify(ovenRegistry.data.ovenFactoryContractAddress == newOvenFactoryContractAddress)
@sp.add_test(name="setOvenFactoryContract - fails when not called by governor")
def test():
# GIVEN an OvenRegistry contract
scenario = sp.test_scenario()
governorContractAddress = Addresses.GOVERNOR_ADDRESS
ovenRegistry = OvenRegistryContract(
governorContractAddress = governorContractAddress
)
scenario += ovenRegistry
# WHEN the setOvenFactoryContract is called by someone who isn't the governor THEN the call fails
newOvenFactoryContractAddress = Addresses.ROTATED_ADDRESS
scenario += ovenRegistry.setOvenFactoryContract(newOvenFactoryContractAddress).run(
sender = Addresses.NULL_ADDRESS,
valid = False
)