-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrounds.py
393 lines (382 loc) · 14.7 KB
/
rounds.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2025 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------
"""This module contains the rounds for the decision-making."""
from typing import Dict, Set
from packages.valory.skills.abstract_round_abci.base import (
AbciApp,
AbciAppTransitionFunction,
AppState,
get_name,
)
from packages.valory.skills.decision_maker_abci.states.base import (
Event,
SynchronizedData,
)
from packages.valory.skills.decision_maker_abci.states.bet_placement import (
BetPlacementRound,
)
from packages.valory.skills.decision_maker_abci.states.blacklisting import (
BlacklistingRound,
)
from packages.valory.skills.decision_maker_abci.states.check_benchmarking import (
CheckBenchmarkingModeRound,
)
from packages.valory.skills.decision_maker_abci.states.claim_subscription import (
ClaimRound,
)
from packages.valory.skills.decision_maker_abci.states.decision_receive import (
DecisionReceiveRound,
)
from packages.valory.skills.decision_maker_abci.states.decision_request import (
DecisionRequestRound,
)
from packages.valory.skills.decision_maker_abci.states.final_states import (
BenchmarkingDoneRound,
BenchmarkingModeDisabledRound,
FinishedDecisionMakerRound,
FinishedDecisionRequestRound,
FinishedSubscriptionRound,
FinishedWithoutDecisionRound,
FinishedWithoutRedeemingRound,
ImpossibleRound,
RefillRequiredRound,
)
from packages.valory.skills.decision_maker_abci.states.handle_failed_tx import (
HandleFailedTxRound,
)
from packages.valory.skills.decision_maker_abci.states.order_subscription import (
SubscriptionRound,
)
from packages.valory.skills.decision_maker_abci.states.randomness import (
BenchmarkingRandomnessRound,
RandomnessRound,
)
from packages.valory.skills.decision_maker_abci.states.redeem import RedeemRound
from packages.valory.skills.decision_maker_abci.states.sampling import SamplingRound
from packages.valory.skills.decision_maker_abci.states.tool_selection import (
ToolSelectionRound,
)
from packages.valory.skills.market_manager_abci.rounds import (
Event as MarketManagerEvent,
)
class DecisionMakerAbciApp(AbciApp[Event]):
"""DecisionMakerAbciApp
Initial round: CheckBenchmarkingModeRound
Initial states: {CheckBenchmarkingModeRound, ClaimRound, DecisionReceiveRound, HandleFailedTxRound, RandomnessRound, RedeemRound}
Transition states:
0. CheckBenchmarkingModeRound
- benchmarking enabled: 1.
- benchmarking disabled: 14.
- no majority: 0.
- round timeout: 0.
- none: 20.
- done: 20.
- subscription error: 20.
1. BenchmarkingRandomnessRound
- done: 3.
- round timeout: 1.
- no majority: 1.
- none: 20.
2. RandomnessRound
- done: 3.
- round timeout: 2.
- no majority: 2.
- none: 20.
3. SamplingRound
- done: 4.
- none: 16.
- no majority: 3.
- round timeout: 3.
- new simulated resample: 3.
- benchmarking enabled: 6.
- benchmarking finished: 21.
- fetch error: 20.
4. SubscriptionRound
- done: 18.
- mock tx: 6.
- no subscription: 6.
- none: 4.
- subscription error: 4.
- no majority: 4.
- round timeout: 4.
5. ClaimRound
- done: 6.
- subscription error: 5.
- no majority: 5.
- round timeout: 5.
6. ToolSelectionRound
- done: 7.
- none: 6.
- no majority: 6.
- round timeout: 6.
7. DecisionRequestRound
- done: 15.
- mock mech request: 8.
- slots unsupported error: 9.
- no majority: 7.
- round timeout: 7.
8. DecisionReceiveRound
- done: 10.
- mech response error: 9.
- no majority: 8.
- tie: 9.
- unprofitable: 9.
- round timeout: 8.
9. BlacklistingRound
- done: 16.
- mock tx: 16.
- none: 20.
- no majority: 9.
- round timeout: 9.
- fetch error: 20.
10. BetPlacementRound
- done: 13.
- mock tx: 11.
- insufficient balance: 19.
- calc buy amount failed: 12.
- no majority: 10.
- round timeout: 10.
- none: 20.
11. RedeemRound
- done: 13.
- mock tx: 3.
- no redeeming: 17.
- no majority: 11.
- redeem round timeout: 17.
- none: 20.
12. HandleFailedTxRound
- blacklist: 9.
- no op: 11.
- no majority: 12.
13. FinishedDecisionMakerRound
14. BenchmarkingModeDisabledRound
15. FinishedDecisionRequestRound
16. FinishedWithoutDecisionRound
17. FinishedWithoutRedeemingRound
18. FinishedSubscriptionRound
19. RefillRequiredRound
20. ImpossibleRound
21. BenchmarkingDoneRound
Final states: {BenchmarkingDoneRound, BenchmarkingModeDisabledRound, FinishedDecisionMakerRound, FinishedDecisionRequestRound, FinishedSubscriptionRound, FinishedWithoutDecisionRound, FinishedWithoutRedeemingRound, ImpossibleRound, RefillRequiredRound}
Timeouts:
round timeout: 30.0
redeem round timeout: 3600.0
"""
initial_round_cls: AppState = CheckBenchmarkingModeRound
initial_states: Set[AppState] = {
CheckBenchmarkingModeRound,
RandomnessRound,
HandleFailedTxRound,
DecisionReceiveRound,
RedeemRound,
ClaimRound,
}
transition_function: AbciAppTransitionFunction = {
CheckBenchmarkingModeRound: {
Event.BENCHMARKING_ENABLED: BenchmarkingRandomnessRound,
Event.BENCHMARKING_DISABLED: BenchmarkingModeDisabledRound,
Event.NO_MAJORITY: CheckBenchmarkingModeRound,
Event.ROUND_TIMEOUT: CheckBenchmarkingModeRound,
# added because of `autonomy analyse fsm-specs`
# falsely reporting them as missing from the transition
Event.NONE: ImpossibleRound,
Event.DONE: ImpossibleRound,
Event.SUBSCRIPTION_ERROR: ImpossibleRound,
},
BenchmarkingRandomnessRound: {
Event.DONE: SamplingRound,
Event.ROUND_TIMEOUT: BenchmarkingRandomnessRound,
Event.NO_MAJORITY: BenchmarkingRandomnessRound,
# added because of `autonomy analyse fsm-specs`
# falsely reporting this as missing from the transition
Event.NONE: ImpossibleRound,
},
RandomnessRound: {
Event.DONE: SamplingRound,
Event.ROUND_TIMEOUT: RandomnessRound,
Event.NO_MAJORITY: RandomnessRound,
# added because of `autonomy analyse fsm-specs`
# falsely reporting this as missing from the transition
Event.NONE: ImpossibleRound,
},
SamplingRound: {
Event.DONE: SubscriptionRound,
Event.NONE: FinishedWithoutDecisionRound,
Event.NO_MAJORITY: SamplingRound,
Event.ROUND_TIMEOUT: SamplingRound,
Event.NEW_SIMULATED_RESAMPLE: SamplingRound,
Event.BENCHMARKING_ENABLED: ToolSelectionRound,
Event.BENCHMARKING_FINISHED: BenchmarkingDoneRound,
# this is here because of `autonomy analyse fsm-specs`
# falsely reporting it as missing from the transition
MarketManagerEvent.FETCH_ERROR: ImpossibleRound,
},
SubscriptionRound: {
Event.DONE: FinishedSubscriptionRound,
# skip placing the subscription tx and the claiming round
Event.MOCK_TX: ToolSelectionRound,
Event.NO_SUBSCRIPTION: ToolSelectionRound,
Event.NONE: SubscriptionRound,
Event.SUBSCRIPTION_ERROR: SubscriptionRound,
Event.NO_MAJORITY: SubscriptionRound,
Event.ROUND_TIMEOUT: SubscriptionRound,
},
ClaimRound: {
Event.DONE: ToolSelectionRound,
Event.SUBSCRIPTION_ERROR: ClaimRound,
Event.NO_MAJORITY: ClaimRound,
Event.ROUND_TIMEOUT: ClaimRound,
},
ToolSelectionRound: {
Event.DONE: DecisionRequestRound,
Event.NONE: ToolSelectionRound,
Event.NO_MAJORITY: ToolSelectionRound,
Event.ROUND_TIMEOUT: ToolSelectionRound,
},
DecisionRequestRound: {
Event.DONE: FinishedDecisionRequestRound,
# skip the request to the mech
Event.MOCK_MECH_REQUEST: DecisionReceiveRound,
Event.SLOTS_UNSUPPORTED_ERROR: BlacklistingRound,
Event.NO_MAJORITY: DecisionRequestRound,
Event.ROUND_TIMEOUT: DecisionRequestRound,
},
DecisionReceiveRound: {
Event.DONE: BetPlacementRound,
Event.MECH_RESPONSE_ERROR: BlacklistingRound,
Event.NO_MAJORITY: DecisionReceiveRound,
Event.TIE: BlacklistingRound,
Event.UNPROFITABLE: BlacklistingRound,
# loop on the same state until Mech deliver is received
Event.ROUND_TIMEOUT: DecisionReceiveRound,
},
BlacklistingRound: {
Event.DONE: FinishedWithoutDecisionRound,
Event.MOCK_TX: FinishedWithoutDecisionRound,
# degenerate round on purpose, should never have reached here
Event.NONE: ImpossibleRound,
Event.NO_MAJORITY: BlacklistingRound,
Event.ROUND_TIMEOUT: BlacklistingRound,
# this is here because of `autonomy analyse fsm-specs`
# falsely reporting it as missing from the transition
MarketManagerEvent.FETCH_ERROR: ImpossibleRound,
},
BetPlacementRound: {
Event.DONE: FinishedDecisionMakerRound,
# skip the bet placement tx
Event.MOCK_TX: RedeemRound,
# degenerate round on purpose, owner must refill the safe
Event.INSUFFICIENT_BALANCE: RefillRequiredRound,
Event.CALC_BUY_AMOUNT_FAILED: HandleFailedTxRound,
Event.NO_MAJORITY: BetPlacementRound,
Event.ROUND_TIMEOUT: BetPlacementRound,
# this is here because of `autonomy analyse fsm-specs`
# falsely reporting it as missing from the transition
Event.NONE: ImpossibleRound,
},
RedeemRound: {
Event.DONE: FinishedDecisionMakerRound,
Event.MOCK_TX: SamplingRound,
Event.NO_REDEEMING: FinishedWithoutRedeemingRound,
Event.NO_MAJORITY: RedeemRound,
# in case of a round timeout, there likely is something wrong with redeeming
# it could be the RPC, or some other issue.
# We don't want to be stuck trying to redeem.
Event.REDEEM_ROUND_TIMEOUT: FinishedWithoutRedeemingRound,
# this is here because of `autonomy analyse fsm-specs` falsely
# reporting it as missing from the transition
Event.NONE: ImpossibleRound,
},
HandleFailedTxRound: {
Event.BLACKLIST: BlacklistingRound,
Event.NO_OP: RedeemRound,
Event.NO_MAJORITY: HandleFailedTxRound,
},
FinishedDecisionMakerRound: {},
BenchmarkingModeDisabledRound: {},
FinishedDecisionRequestRound: {},
FinishedWithoutDecisionRound: {},
FinishedWithoutRedeemingRound: {},
FinishedSubscriptionRound: {},
RefillRequiredRound: {},
ImpossibleRound: {},
BenchmarkingDoneRound: {},
}
cross_period_persisted_keys = frozenset(
{
get_name(SynchronizedData.available_mech_tools),
get_name(SynchronizedData.policy),
get_name(SynchronizedData.utilized_tools),
get_name(SynchronizedData.redeemed_condition_ids),
get_name(SynchronizedData.payout_so_far),
get_name(SynchronizedData.mech_price),
get_name(SynchronizedData.mocking_mode),
get_name(SynchronizedData.next_mock_data_row),
get_name(SynchronizedData.agreement_id),
}
)
final_states: Set[AppState] = {
FinishedDecisionMakerRound,
BenchmarkingModeDisabledRound,
FinishedDecisionRequestRound,
FinishedSubscriptionRound,
FinishedWithoutDecisionRound,
FinishedWithoutRedeemingRound,
RefillRequiredRound,
ImpossibleRound,
BenchmarkingDoneRound,
}
event_to_timeout: Dict[Event, float] = {
Event.ROUND_TIMEOUT: 30.0,
Event.REDEEM_ROUND_TIMEOUT: 3600.0,
}
db_pre_conditions: Dict[AppState, Set[str]] = {
RedeemRound: set(),
ClaimRound: set(),
DecisionReceiveRound: {
get_name(SynchronizedData.final_tx_hash),
},
HandleFailedTxRound: {
get_name(SynchronizedData.bets_hash),
},
RandomnessRound: set(),
CheckBenchmarkingModeRound: set(),
}
db_post_conditions: Dict[AppState, Set[str]] = {
FinishedDecisionMakerRound: {
get_name(SynchronizedData.sampled_bet_index),
get_name(SynchronizedData.tx_submitter),
get_name(SynchronizedData.most_voted_tx_hash),
},
BenchmarkingModeDisabledRound: set(),
FinishedDecisionRequestRound: set(),
FinishedSubscriptionRound: {
get_name(SynchronizedData.tx_submitter),
get_name(SynchronizedData.most_voted_tx_hash),
get_name(SynchronizedData.agreement_id),
},
FinishedWithoutDecisionRound: {get_name(SynchronizedData.sampled_bet_index)},
FinishedWithoutRedeemingRound: set(),
RefillRequiredRound: set(),
ImpossibleRound: set(),
BenchmarkingDoneRound: {
get_name(SynchronizedData.mocking_mode),
get_name(SynchronizedData.next_mock_data_row),
},
}