Skip to content

Commit e1379f3

Browse files
authored
Update validator select balancer (#119)
* Update validator select balancer
1 parent c03edd6 commit e1379f3

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

oracle/networks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import timedelta
22

33
from decouple import Csv, config
4+
from ens.constants import EMPTY_ADDR_HEX
45
from eth_typing import HexStr
56
from web3 import Web3
67

@@ -68,6 +69,9 @@
6869
"0x0100000000000000000000002296e122c1a20fca3cac3371357bdad3be0df079"
6970
),
7071
ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""),
72+
ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress(
73+
"0x5fc60576b92c5ce5c341c43e3b2866eb9e0cddd1"
74+
),
7175
AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-mainnet"),
7276
AWS_REGION=config("AWS_REGION", default="eu-central-1"),
7377
AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""),
@@ -138,6 +142,7 @@
138142
"0x0100000000000000000000005c631621b897f467dd6a91855a0bc97d77b78dc0"
139143
),
140144
ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""),
145+
ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX,
141146
AWS_BUCKET_NAME=config(
142147
"AWS_BUCKET_NAME",
143148
default="oracle-votes-harbour-mainnet",
@@ -211,6 +216,7 @@
211216
"0x010000000000000000000000040f15c6b5bfc5f324ecab5864c38d4e1eef4218"
212217
),
213218
ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""),
219+
ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX,
214220
AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-goerli"),
215221
AWS_REGION=config("AWS_REGION", default="eu-central-1"),
216222
AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""),
@@ -281,6 +287,7 @@
281287
"0x0100000000000000000000006dfc9682e3c3263758ad96e2b2ba9822167f81ee"
282288
),
283289
ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""),
290+
ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX,
284291
AWS_BUCKET_NAME=config(
285292
"AWS_BUCKET_NAME",
286293
default="oracle-votes-perm-goerli",
@@ -354,6 +361,7 @@
354361
"0x010000000000000000000000fc9b67b6034f6b306ea9bd8ec1baf3efa2490394"
355362
),
356363
ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""),
364+
ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX,
357365
AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-gnosis"),
358366
AWS_REGION=config("AWS_REGION", default="eu-north-1"),
359367
AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""),

oracle/oracle/common/graphql_queries.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
operators(
401401
block: { number: $block_number }
402402
where: { committed: true }
403-
orderBy: validatorsCount
403+
orderBy: id
404404
orderDirection: asc
405405
) {
406406
id
@@ -411,6 +411,24 @@
411411
"""
412412
)
413413

414+
LAST_VALIDATORS_QUERY = gql(
415+
"""
416+
query getValidators($block_number: Int) {
417+
validators(
418+
block: { number: $block_number }
419+
orderBy: createdAtBlock
420+
orderDirection: desc
421+
first: 1
422+
) {
423+
operator {
424+
id
425+
}
426+
}
427+
}
428+
"""
429+
)
430+
431+
414432
PARTNERS_QUERY = gql(
415433
"""
416434
query getPartners($block_number: Int) {

oracle/oracle/validators/eth1.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict, Set, Union
22

3+
from ens.constants import EMPTY_ADDR_HEX
34
from eth_typing import HexStr
45
from web3 import Web3
56
from web3.types import BlockNumber
@@ -9,12 +10,13 @@
910
execute_sw_gql_query,
1011
)
1112
from oracle.oracle.common.graphql_queries import (
13+
LAST_VALIDATORS_QUERY,
1214
OPERATORS_QUERY,
1315
VALIDATOR_REGISTRATIONS_LATEST_INDEX_QUERY,
1416
VALIDATOR_REGISTRATIONS_QUERY,
1517
)
1618
from oracle.oracle.common.ipfs import ipfs_fetch
17-
from oracle.settings import NETWORK
19+
from oracle.settings import NETWORK, NETWORK_CONFIG
1820

1921
from .types import ValidatorDepositData
2022

@@ -29,6 +31,21 @@ async def select_validator(
2931
variables=dict(block_number=block_number),
3032
)
3133
operators = result["operators"]
34+
result: Dict = await execute_sw_gql_query(
35+
network=NETWORK,
36+
query=LAST_VALIDATORS_QUERY,
37+
variables=dict(block_number=block_number),
38+
)
39+
40+
last_validators = result["validators"]
41+
if last_validators:
42+
last_operator_id = last_validators[0]["operator"]["id"]
43+
index = _find_operator_index(operators, last_operator_id)
44+
if index is not None and index != len(operators) - 1:
45+
operators = operators[index + 1 :] + [operators[index]] + operators[:index]
46+
47+
_move_to_bottom(operators, NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"])
48+
3249
for operator in operators:
3350
merkle_proofs = operator["depositDataMerkleProofs"]
3451
if not merkle_proofs:
@@ -90,3 +107,22 @@ async def get_validators_deposit_root(block_number: BlockNumber) -> HexStr:
90107
variables=dict(block_number=block_number),
91108
)
92109
return result["validatorRegistrations"][0]["validatorsDepositRoot"]
110+
111+
112+
def _move_to_bottom(operators, operator_id):
113+
if operator_id == EMPTY_ADDR_HEX:
114+
return
115+
116+
index = _find_operator_index(operators, operator_id)
117+
if index is not None:
118+
operators.append(operators.pop(index))
119+
120+
121+
def _find_operator_index(operators, operator_id):
122+
index = None
123+
operator_id = Web3.toChecksumAddress(operator_id)
124+
for i, operator in enumerate(operators):
125+
if Web3.toChecksumAddress(operator["id"]) == operator_id:
126+
index = i
127+
break
128+
return index

oracle/oracle/validators/tests/test_controller.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
block_number = faker.random_int(150000, 250000)
1414

1515

16-
def select_validator(operator, *args, **kwargs):
16+
def select_operators(operator, *args, **kwargs):
1717
return {
1818
"operators": [
1919
{
@@ -25,6 +25,10 @@ def select_validator(operator, *args, **kwargs):
2525
}
2626

2727

28+
def select_validators(*args, **kwargs):
29+
return {"validators": []}
30+
31+
2832
def can_registor_validator(*args, **kwargs):
2933
return {"validatorRegistrations": []}
3034

@@ -71,7 +75,8 @@ def get_validators_deposit_root(validatorsDepositRoot, *args, **kwargs):
7175

7276
def sw_gql_query(operator):
7377
return [
74-
select_validator(operator),
78+
select_operators(operator),
79+
select_validators(),
7580
]
7681

7782

0 commit comments

Comments
 (0)