-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathregister_validators.py
More file actions
189 lines (164 loc) · 6.45 KB
/
Copy pathregister_validators.py
File metadata and controls
189 lines (164 loc) · 6.45 KB
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
import logging
from typing import Sequence
from eth_typing import HexStr
from sw_utils.typings import Bytes32
from web3 import Web3
from web3.exceptions import ContractLogicError
from web3.types import Wei
from src.common.clients import execution_client
from src.common.contracts import VaultContract, validators_registry_contract
from src.common.execution import build_gas_manager, transaction_gas_wrapper
from src.common.typings import HarvestParams, OraclesApproval
from src.common.utils import format_error
from src.config.settings import settings
from src.validators.execution import get_validators_start_index
from src.validators.signing.common import encode_tx_validator_list
from src.validators.typings import Validator
logger = logging.getLogger(__name__)
# pylint: disable=too-many-arguments,too-many-locals
async def tx_register_validators(
approval: OraclesApproval,
validators: Sequence[Validator],
harvest_params: HarvestParams | None,
validators_registry_root: HexStr,
validator_index: int,
validators_manager_signature: HexStr,
) -> HexStr | None:
# Check that validators registry root has not changed
registry_root = await validators_registry_contract.get_registry_root()
if registry_root != validators_registry_root:
logger.info('Validators registry root has changed. Retrying...')
return None
# Check that validator index has not changed
current_validator_index = await get_validators_start_index()
if current_validator_index != validator_index:
logger.info('Validator index has changed. Retrying...')
return None
# Get update state call if harvest params are provided
vault_contract = VaultContract(settings.vault)
if harvest_params is not None:
calls = [vault_contract.get_update_state_call(harvest_params)]
else:
calls = []
# Build keeper approval params
tx_validators = [
Web3.to_bytes(tx_validator)
for tx_validator in encode_tx_validator_list(
validators=validators,
)
]
keeper_approval_params = (
Bytes32(Web3.to_bytes(hexstr=validators_registry_root)),
approval.deadline,
b''.join(tx_validators),
approval.signatures,
approval.ipfs_hash,
)
# add validators registration call
calls.append(
vault_contract.encode_abi(
fn_name='registerValidators',
args=[keeper_approval_params, Web3.to_bytes(hexstr=validators_manager_signature)],
)
)
# Simulate transaction
logger.info('Submitting registration transaction')
try:
await vault_contract.functions.multicall(calls).estimate_gas()
except (ValueError, ContractLogicError) as e:
logger.error(
'Failed to register validator(s): %s. '
'Most likely registry root has changed during validators registration. Retrying...',
format_error(e),
)
if settings.verbose:
logger.exception(e)
return None
# Send transaction
try:
gas_manager = build_gas_manager()
tx_params = await gas_manager.get_high_priority_tx_params()
tx = await vault_contract.functions.multicall(calls).transact(tx_params)
except Exception as e:
logger.error('Failed to register validator(s): %s', format_error(e))
if settings.verbose:
logger.exception(e)
return None
# Wait for transaction confirmation
tx_hash = Web3.to_hex(tx)
logger.info('Waiting for transaction %s confirmation', tx_hash)
tx_receipt = await execution_client.eth.wait_for_transaction_receipt(
tx, timeout=settings.execution_transaction_timeout
)
if not tx_receipt['status']:
logger.error('Registration transaction failed')
return None
return tx_hash
async def tx_fund_validators(
validators: list[Validator],
validators_manager_signature: HexStr,
harvest_params: HarvestParams | None,
) -> HexStr | None:
tx_validators = [
Web3.to_bytes(tx_validator)
for tx_validator in encode_tx_validator_list(
validators=validators,
)
]
calls = []
vault_contract = VaultContract(settings.vault)
if harvest_params is not None:
# add update state calls before validator funding
calls.append(vault_contract.get_update_state_call(harvest_params))
fund_validators_call = vault_contract.encode_abi(
fn_name='fundValidators',
args=[b''.join(tx_validators), Web3.to_bytes(hexstr=validators_manager_signature)],
)
calls.append(fund_validators_call)
logger.info('Submitting fund validators transaction')
try:
tx_function = vault_contract.functions.multicall(calls)
tx = await transaction_gas_wrapper(tx_function)
except Exception as e:
logger.error('Failed to fund validator(s): %s', format_error(e))
if settings.verbose:
logger.exception(e)
return None
tx_hash = Web3.to_hex(tx)
logger.info('Waiting for transaction %s confirmation', tx_hash)
tx_receipt = await execution_client.eth.wait_for_transaction_receipt(
tx, timeout=settings.execution_transaction_timeout
)
if not tx_receipt['status']:
logger.error('Funding transaction failed')
return None
return tx_hash
async def tx_consolidate_validators(
validators: bytes,
oracle_signatures: bytes | None,
tx_fee: Wei,
validators_manager_signature: HexStr,
) -> HexStr | None:
"""Sends consolidate validators transaction to vault contract"""
logger.info('Submitting consolidate validators transaction')
vault_contract = VaultContract(settings.vault)
if oracle_signatures is None:
oracle_signatures = b''
try:
tx_function = vault_contract.functions.consolidateValidators(
validators,
Web3.to_bytes(hexstr=validators_manager_signature),
oracle_signatures,
)
tx = await transaction_gas_wrapper(tx_function, tx_params={'value': tx_fee})
except Exception as e:
logger.info('Failed to submit consolidate validators transaction: %s', format_error(e))
return None
logger.info('Waiting for transaction %s confirmation', Web3.to_hex(tx))
tx_receipt = await execution_client.eth.wait_for_transaction_receipt(
tx, timeout=settings.execution_transaction_timeout
)
if not tx_receipt['status']:
logger.info('Consolidate validators transaction failed')
return None
return Web3.to_hex(tx)