From e1379f30870ffe4b4560b407f76e2f520f14a0c8 Mon Sep 17 00:00:00 2001 From: Alexander Sysoev Date: Wed, 19 Oct 2022 10:00:23 +0300 Subject: [PATCH] Update validator select balancer (#119) * Update validator select balancer --- oracle/networks.py | 8 ++++ oracle/oracle/common/graphql_queries.py | 20 +++++++++- oracle/oracle/validators/eth1.py | 38 ++++++++++++++++++- .../validators/tests/test_controller.py | 9 ++++- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/oracle/networks.py b/oracle/networks.py index d148fe0..2a21cac 100644 --- a/oracle/networks.py +++ b/oracle/networks.py @@ -1,6 +1,7 @@ from datetime import timedelta from decouple import Csv, config +from ens.constants import EMPTY_ADDR_HEX from eth_typing import HexStr from web3 import Web3 @@ -68,6 +69,9 @@ "0x0100000000000000000000002296e122c1a20fca3cac3371357bdad3be0df079" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0x5fc60576b92c5ce5c341c43e3b2866eb9e0cddd1" + ), AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-mainnet"), AWS_REGION=config("AWS_REGION", default="eu-central-1"), AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""), @@ -138,6 +142,7 @@ "0x0100000000000000000000005c631621b897f467dd6a91855a0bc97d77b78dc0" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-harbour-mainnet", @@ -211,6 +216,7 @@ "0x010000000000000000000000040f15c6b5bfc5f324ecab5864c38d4e1eef4218" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-goerli"), AWS_REGION=config("AWS_REGION", default="eu-central-1"), AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""), @@ -281,6 +287,7 @@ "0x0100000000000000000000006dfc9682e3c3263758ad96e2b2ba9822167f81ee" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-perm-goerli", @@ -354,6 +361,7 @@ "0x010000000000000000000000fc9b67b6034f6b306ea9bd8ec1baf3efa2490394" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config("AWS_BUCKET_NAME", default="oracle-votes-gnosis"), AWS_REGION=config("AWS_REGION", default="eu-north-1"), AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=""), diff --git a/oracle/oracle/common/graphql_queries.py b/oracle/oracle/common/graphql_queries.py index 2c1448a..428fc68 100644 --- a/oracle/oracle/common/graphql_queries.py +++ b/oracle/oracle/common/graphql_queries.py @@ -400,7 +400,7 @@ operators( block: { number: $block_number } where: { committed: true } - orderBy: validatorsCount + orderBy: id orderDirection: asc ) { id @@ -411,6 +411,24 @@ """ ) +LAST_VALIDATORS_QUERY = gql( + """ + query getValidators($block_number: Int) { + validators( + block: { number: $block_number } + orderBy: createdAtBlock + orderDirection: desc + first: 1 + ) { + operator { + id + } + } + } +""" +) + + PARTNERS_QUERY = gql( """ query getPartners($block_number: Int) { diff --git a/oracle/oracle/validators/eth1.py b/oracle/oracle/validators/eth1.py index 2c661db..ab24d84 100644 --- a/oracle/oracle/validators/eth1.py +++ b/oracle/oracle/validators/eth1.py @@ -1,5 +1,6 @@ from typing import Dict, Set, Union +from ens.constants import EMPTY_ADDR_HEX from eth_typing import HexStr from web3 import Web3 from web3.types import BlockNumber @@ -9,12 +10,13 @@ execute_sw_gql_query, ) from oracle.oracle.common.graphql_queries import ( + LAST_VALIDATORS_QUERY, OPERATORS_QUERY, VALIDATOR_REGISTRATIONS_LATEST_INDEX_QUERY, VALIDATOR_REGISTRATIONS_QUERY, ) from oracle.oracle.common.ipfs import ipfs_fetch -from oracle.settings import NETWORK +from oracle.settings import NETWORK, NETWORK_CONFIG from .types import ValidatorDepositData @@ -29,6 +31,21 @@ async def select_validator( variables=dict(block_number=block_number), ) operators = result["operators"] + result: Dict = await execute_sw_gql_query( + network=NETWORK, + query=LAST_VALIDATORS_QUERY, + variables=dict(block_number=block_number), + ) + + last_validators = result["validators"] + if last_validators: + last_operator_id = last_validators[0]["operator"]["id"] + index = _find_operator_index(operators, last_operator_id) + if index is not None and index != len(operators) - 1: + operators = operators[index + 1 :] + [operators[index]] + operators[:index] + + _move_to_bottom(operators, NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"]) + for operator in operators: merkle_proofs = operator["depositDataMerkleProofs"] if not merkle_proofs: @@ -90,3 +107,22 @@ async def get_validators_deposit_root(block_number: BlockNumber) -> HexStr: variables=dict(block_number=block_number), ) return result["validatorRegistrations"][0]["validatorsDepositRoot"] + + +def _move_to_bottom(operators, operator_id): + if operator_id == EMPTY_ADDR_HEX: + return + + index = _find_operator_index(operators, operator_id) + if index is not None: + operators.append(operators.pop(index)) + + +def _find_operator_index(operators, operator_id): + index = None + operator_id = Web3.toChecksumAddress(operator_id) + for i, operator in enumerate(operators): + if Web3.toChecksumAddress(operator["id"]) == operator_id: + index = i + break + return index diff --git a/oracle/oracle/validators/tests/test_controller.py b/oracle/oracle/validators/tests/test_controller.py index 1b974b1..754926d 100644 --- a/oracle/oracle/validators/tests/test_controller.py +++ b/oracle/oracle/validators/tests/test_controller.py @@ -13,7 +13,7 @@ block_number = faker.random_int(150000, 250000) -def select_validator(operator, *args, **kwargs): +def select_operators(operator, *args, **kwargs): return { "operators": [ { @@ -25,6 +25,10 @@ def select_validator(operator, *args, **kwargs): } +def select_validators(*args, **kwargs): + return {"validators": []} + + def can_registor_validator(*args, **kwargs): return {"validatorRegistrations": []} @@ -71,7 +75,8 @@ def get_validators_deposit_root(validatorsDepositRoot, *args, **kwargs): def sw_gql_query(operator): return [ - select_validator(operator), + select_operators(operator), + select_validators(), ]