-
Notifications
You must be signed in to change notification settings - Fork 35
/
ethereum_system.py
282 lines (224 loc) · 9.11 KB
/
ethereum_system.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
"""
# Ethereum System
General Ethereum mechanisms, such as managing the system upgrade process,
the EIP-1559 transaction pricing mechanism, and updating the ETH price and ETH supply.
"""
import typing
import datetime
from model import constants as constants
from model.types import ETH, USD_per_ETH, Gwei, Stage
def policy_upgrade_stages(params, substep, state_history, previous_state):
"""
## Upgrade Stages Policy
Transitions the model from one stage in the Ethereum network
upgrade process to the next at different milestones.
This is essentially a finite-state machine: https://en.wikipedia.org/wiki/Finite-state_machine
"""
# Parameters
dt = params["dt"]
stage: Stage = params["stage"]
date_start = params["date_start"]
date_eip1559 = params["date_eip1559"]
date_pos = params["date_pos"]
# State Variables
current_stage = previous_state["stage"]
timestep = previous_state["timestep"]
# Calculate current timestamp from timestep
timestamp = date_start + datetime.timedelta(
days=(timestep * dt / constants.epochs_per_day)
)
# Initialize stage State Variable at start of simulation
if current_stage is None:
current_stage = stage
else:
# Convert Stage enum value (int) to Stage enum
current_stage = Stage(current_stage)
# Stage finite-state machine
if stage == Stage.ALL:
# If Stage ALL selected, transition through all stages
# at different timestamps
if (
current_stage in [Stage.ALL, Stage.BEACON_CHAIN]
and timestamp < date_eip1559
):
current_stage = Stage.BEACON_CHAIN
elif (
current_stage in [Stage.ALL, Stage.BEACON_CHAIN, Stage.EIP1559]
and timestamp < date_pos
):
current_stage = Stage.EIP1559
else:
current_stage = Stage.PROOF_OF_STAKE
elif stage == Stage.BEACON_CHAIN:
# If Stage BEACON_CHAIN selected, only execute single stage
current_stage = Stage.BEACON_CHAIN
elif stage == Stage.EIP1559:
# If Stage EIP-1559 selected, only execute single stage
current_stage = Stage.EIP1559
elif stage == Stage.PROOF_OF_STAKE:
# If Stage PROOF_OF_STAKE selected, only execute single stage
current_stage = Stage.PROOF_OF_STAKE
else:
# Else, raise exception if invalid Stage
raise Exception("Invalid Stage selected")
return {
"stage": current_stage.value,
"timestamp": timestamp,
}
def policy_network_issuance(
params, substep, state_history, previous_state
) -> typing.Dict[str, ETH]:
"""
## Network Issuance Policy Function
Calculate the total network issuance and issuance from Proof of Work block rewards.
"""
# Parameters
dt = params["dt"]
daily_pow_issuance = params["daily_pow_issuance"]
# State Variables
stage = previous_state["stage"]
amount_slashed = previous_state["amount_slashed"]
total_base_fee = previous_state["total_base_fee"]
total_priority_fee_to_validators = previous_state[
"total_priority_fee_to_validators"
]
total_online_validator_rewards = previous_state["total_online_validator_rewards"]
# Calculate network issuance in ETH
network_issuance = (
# Remove priority fee to validators which is not issuance (ETH transferred rather than minted)
(total_online_validator_rewards - total_priority_fee_to_validators)
- amount_slashed
- total_base_fee
) / constants.gwei
# Calculate Proof of Work issuance
pow_issuance = (
daily_pow_issuance / constants.epochs_per_day
if Stage(stage) in [Stage.BEACON_CHAIN, Stage.EIP1559]
else 0
)
network_issuance += pow_issuance * dt
return {
"network_issuance": network_issuance,
"pow_issuance": pow_issuance,
}
def policy_mev(params, substep, state_history, previous_state) -> typing.Dict[str, ETH]:
"""
## Maximum Extractable Value (MEV) Policy
MEV is allocated to miners pre Proof-of-Stake and validators post Proof-of-Stake,
using the `mev_per_block` System Parameter.
By default `mev_per_block` is set zero, to only consider the
influence of Proof-of-Stake (PoS) incentives on validator yields.
See [ASSUMPTIONS.md](ASSUMPTIONS.md) document for further details.
"""
# Parameters
dt = params["dt"]
mev_per_block = params["mev_per_block"]
# State Variables
stage = Stage(previous_state["stage"])
if stage in [Stage.PROOF_OF_STAKE]:
total_realized_mev_to_miners = 0
# Allocate realized MEV to validators post Proof-of-Stake
total_realized_mev_to_validators = (
mev_per_block * constants.slots_per_epoch * dt
)
else: # Stage is pre Proof-of-Stake
# Allocate realized MEV to miners pre Proof-of-Stake
total_realized_mev_to_miners = (
mev_per_block * constants.pow_blocks_per_epoch * dt
)
total_realized_mev_to_validators = 0
return {
"total_realized_mev_to_miners": total_realized_mev_to_miners,
"total_realized_mev_to_validators": total_realized_mev_to_validators,
}
def policy_eip1559_transaction_pricing(
params, substep, state_history, previous_state
) -> typing.Dict[str, Gwei]:
"""
## EIP-1559 Transaction Pricing Policy
A transaction pricing mechanism that includes fixed-per-block network fee
that is burned and dynamically expands/contracts block sizes to deal with transient congestion.
See:
* https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md
* https://eips.ethereum.org/EIPS/eip-1559
"""
stage = Stage(previous_state["stage"])
if stage not in [Stage.EIP1559, Stage.PROOF_OF_STAKE]:
return {
"base_fee_per_gas": 0,
"total_base_fee": 0,
"total_priority_fee_to_miners": 0,
"total_priority_fee_to_validators": 0,
}
# Parameters
dt = params["dt"]
gas_target_process = params["gas_target_process"] # Gas
ELASTICITY_MULTIPLIER = params["ELASTICITY_MULTIPLIER"]
base_fee_process = params["base_fee_process"]
priority_fee_process = params["priority_fee_process"]
# State Variables
run = previous_state["run"]
timestep = previous_state["timestep"]
# Get samples for current run and timestep from base fee, priority fee, and transaction processes
base_fee_per_gas = base_fee_process(run, timestep * dt) # Gwei per Gas
gas_target = gas_target_process(run, timestep * dt) # Gas
# Ensure basefee changes by no more than 1 / BASE_FEE_MAX_CHANGE_DENOMINATOR %
_BASE_FEE_MAX_CHANGE_DENOMINATOR = params["BASE_FEE_MAX_CHANGE_DENOMINATOR"]
# assert (
# abs(basefee - previous_basefee) / previous_basefee
# <= constants.slots_per_epoch / BASE_FEE_MAX_CHANGE_DENOMINATOR
# if timestep > 1
# else True
# ), "basefee changed by more than 1 / BASE_FEE_MAX_CHANGE_DENOMINATOR %"
avg_priority_fee_per_gas = priority_fee_process(run, timestep * dt) # Gwei per Gas
if stage in [Stage.EIP1559]:
gas_used = constants.pow_blocks_per_epoch * gas_target # Gas
else: # stage is Stage.PROOF_OF_STAKE
gas_used = constants.slots_per_epoch * gas_target # Gas
# Calculate the total base fee and priority fee
total_base_fee = gas_used * base_fee_per_gas # Gwei
total_priority_fee = gas_used * avg_priority_fee_per_gas # Gwei
if stage in [Stage.PROOF_OF_STAKE]:
total_priority_fee_to_miners = 0
total_priority_fee_to_validators = total_priority_fee
else:
total_priority_fee_to_miners = total_priority_fee
total_priority_fee_to_validators = 0
# Check if the block used too much gas
assert (
gas_used <= gas_target * ELASTICITY_MULTIPLIER * constants.slots_per_epoch
), "invalid block: too much gas used"
return {
"base_fee_per_gas": base_fee_per_gas,
"total_base_fee": total_base_fee * dt,
"total_priority_fee_to_miners": total_priority_fee_to_miners * dt,
"total_priority_fee_to_validators": total_priority_fee_to_validators * dt,
}
def update_eth_price(
params, substep, state_history, previous_state, policy_input
) -> typing.Tuple[str, USD_per_ETH]:
"""
## ETH Price State Update Function
Update the ETH price from the `eth_price_process`.
"""
# Parameters
dt = params["dt"]
eth_price_process = params["eth_price_process"]
# State Variables
run = previous_state["run"]
timestep = previous_state["timestep"]
# Get the ETH price sample for the current run and timestep
eth_price_sample = eth_price_process(run, timestep * dt)
return "eth_price", eth_price_sample
def update_eth_supply(
params, substep, state_history, previous_state, policy_input
) -> typing.Tuple[str, ETH]:
"""
## ETH Supply State Update Function
Update the ETH supply from the Network Issuance Policy Function.
"""
# Policy Inputs
network_issuance = policy_input["network_issuance"]
# State variables
eth_supply = previous_state["eth_supply"]
return "eth_supply", eth_supply + network_issuance