From 0297b5ceb52c56a84130b683b86bce3dd2aafbef Mon Sep 17 00:00:00 2001 From: cyc60 Date: Fri, 14 Oct 2022 13:25:22 +0300 Subject: [PATCH 1/5] Update validator select balancer Signed-off-by: cyc60 --- oracle/networks.py | 40 +++++++++++++++++++ oracle/oracle/common/graphql_queries.py | 19 +++++++++ oracle/oracle/validators/eth1.py | 34 +++++++++++++++- .../validators/tests/test_controller.py | 9 ++++- 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/oracle/networks.py b/oracle/networks.py index d148fe0..c4829e9 100644 --- a/oracle/networks.py +++ b/oracle/networks.py @@ -68,6 +68,14 @@ "0x0100000000000000000000002296e122c1a20fca3cac3371357bdad3be0df079" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0x5fc60576b92c5ce5c341c43e3b2866eb9e0cddd1" + ), + ORACLE_IGNORE_STAKEWISE_OPERATOR=config( + "ORACLE_IGNORE_STAKEWISE_OPERATOR", + cast=bool, + default=True, + ), 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 +146,14 @@ "0x0100000000000000000000005c631621b897f467dd6a91855a0bc97d77b78dc0" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0xa11c358c20b2baa682956ea2c7a659f6e9583f96" + ), + ORACLE_IGNORE_STAKEWISE_OPERATOR=config( + "ORACLE_IGNORE_STAKEWISE_OPERATOR", + cast=bool, + default=False, + ), AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-harbour-mainnet", @@ -211,6 +227,14 @@ "0x010000000000000000000000040f15c6b5bfc5f324ecab5864c38d4e1eef4218" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0x15c4bd6fe01f6cd2eaa6bf77d976a6a879b1d5bd" + ), + ORACLE_IGNORE_STAKEWISE_OPERATOR=config( + "ORACLE_IGNORE_STAKEWISE_OPERATOR", + cast=bool, + default=False, + ), 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 +305,14 @@ "0x0100000000000000000000006dfc9682e3c3263758ad96e2b2ba9822167f81ee" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0xca445ecf3c53e1f8bc0dcfec6ff1e9544b3dc5b8" + ), + ORACLE_IGNORE_STAKEWISE_OPERATOR=config( + "ORACLE_IGNORE_STAKEWISE_OPERATOR", + cast=bool, + default=False, + ), AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-perm-goerli", @@ -354,6 +386,14 @@ "0x010000000000000000000000fc9b67b6034f6b306ea9bd8ec1baf3efa2490394" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), + ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( + "0xe11e57383312be71b0589e1a3529fec2a524975c" + ), + ORACLE_IGNORE_STAKEWISE_OPERATOR=config( + "ORACLE_IGNORE_STAKEWISE_OPERATOR", + cast=bool, + default=True, + ), 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..98c8db3 100644 --- a/oracle/oracle/common/graphql_queries.py +++ b/oracle/oracle/common/graphql_queries.py @@ -411,6 +411,25 @@ """ ) +LAST_VALIDATORS_QUERY = gql( + """ + query getValidators($block_number: Int) { + validators( + block: { number: $block_number } + orderBy: createdAtBlock + orderDirection: desc + first: 1 + ) { + id + 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..14d4ee7 100644 --- a/oracle/oracle/validators/eth1.py +++ b/oracle/oracle/validators/eth1.py @@ -9,12 +9,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 @@ -28,7 +29,36 @@ async def select_validator( query=OPERATORS_QUERY, variables=dict(block_number=block_number), ) - operators = result["operators"] + operators = result["operators"][:1] + 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 = None + for i, operator in enumerate(operators): + if operator["id"] == last_operator_id: + index = i + break + if index is not None: + operators.append(operators.pop(index)) + + if NETWORK_CONFIG["ORACLE_IGNORE_STAKEWISE_OPERATOR"]: + index = None + for i, operator in enumerate(operators): + if ( + Web3.toChecksumAddress(operator["id"]) + == NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"] + ): + index = i + break + if index is not None: + operators.append(operators.pop(index)) + for operator in operators: merkle_proofs = operator["depositDataMerkleProofs"] if not merkle_proofs: 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(), ] From 92d07fe883462c6bbbb7ebfbfa91e086ad028d31 Mon Sep 17 00:00:00 2001 From: cyc60 Date: Sun, 16 Oct 2022 12:25:56 +0300 Subject: [PATCH 2/5] Refactor operators select Signed-off-by: cyc60 --- oracle/networks.py | 8 ++++---- oracle/oracle/validators/eth1.py | 34 ++++++++++++++------------------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/oracle/networks.py b/oracle/networks.py index c4829e9..138617f 100644 --- a/oracle/networks.py +++ b/oracle/networks.py @@ -147,7 +147,7 @@ ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0xa11c358c20b2baa682956ea2c7a659f6e9583f96" + "0x0000000000000000000000000000000000000000" ), ORACLE_IGNORE_STAKEWISE_OPERATOR=config( "ORACLE_IGNORE_STAKEWISE_OPERATOR", @@ -228,7 +228,7 @@ ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0x15c4bd6fe01f6cd2eaa6bf77d976a6a879b1d5bd" + "0x0000000000000000000000000000000000000000" ), ORACLE_IGNORE_STAKEWISE_OPERATOR=config( "ORACLE_IGNORE_STAKEWISE_OPERATOR", @@ -306,7 +306,7 @@ ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0xca445ecf3c53e1f8bc0dcfec6ff1e9544b3dc5b8" + "0x0000000000000000000000000000000000000000" ), ORACLE_IGNORE_STAKEWISE_OPERATOR=config( "ORACLE_IGNORE_STAKEWISE_OPERATOR", @@ -387,7 +387,7 @@ ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0xe11e57383312be71b0589e1a3529fec2a524975c" + "0x0000000000000000000000000000000000000000" ), ORACLE_IGNORE_STAKEWISE_OPERATOR=config( "ORACLE_IGNORE_STAKEWISE_OPERATOR", diff --git a/oracle/oracle/validators/eth1.py b/oracle/oracle/validators/eth1.py index 14d4ee7..7c665d3 100644 --- a/oracle/oracle/validators/eth1.py +++ b/oracle/oracle/validators/eth1.py @@ -29,35 +29,19 @@ async def select_validator( query=OPERATORS_QUERY, variables=dict(block_number=block_number), ) - operators = result["operators"][:1] + 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 = None - for i, operator in enumerate(operators): - if operator["id"] == last_operator_id: - index = i - break - if index is not None: - operators.append(operators.pop(index)) + _move_to_bottom(operators, last_operator_id) if NETWORK_CONFIG["ORACLE_IGNORE_STAKEWISE_OPERATOR"]: - index = None - for i, operator in enumerate(operators): - if ( - Web3.toChecksumAddress(operator["id"]) - == NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"] - ): - index = i - break - if index is not None: - operators.append(operators.pop(index)) + _move_to_bottom(operators, NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"]) for operator in operators: merkle_proofs = operator["depositDataMerkleProofs"] @@ -120,3 +104,15 @@ 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): + index = None + for i, operator in enumerate(operators): + if Web3.toChecksumAddress(operator["id"]) == Web3.toChecksumAddress( + operator_id + ): + index = i + break + if index is not None: + operators.append(operators.pop(index)) From a6a9e811597a7fdf1daae11f2d5bd85f2a7f792a Mon Sep 17 00:00:00 2001 From: cyc60 Date: Mon, 17 Oct 2022 19:46:52 +0300 Subject: [PATCH 3/5] Operators select: review fixes Signed-off-by: cyc60 --- oracle/networks.py | 25 ------------------------- oracle/oracle/common/graphql_queries.py | 2 +- oracle/oracle/validators/eth1.py | 22 +++++++++++++++++----- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/oracle/networks.py b/oracle/networks.py index 138617f..da8dc36 100644 --- a/oracle/networks.py +++ b/oracle/networks.py @@ -71,11 +71,6 @@ ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( "0x5fc60576b92c5ce5c341c43e3b2866eb9e0cddd1" ), - ORACLE_IGNORE_STAKEWISE_OPERATOR=config( - "ORACLE_IGNORE_STAKEWISE_OPERATOR", - cast=bool, - default=True, - ), 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=""), @@ -149,11 +144,6 @@ ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( "0x0000000000000000000000000000000000000000" ), - ORACLE_IGNORE_STAKEWISE_OPERATOR=config( - "ORACLE_IGNORE_STAKEWISE_OPERATOR", - cast=bool, - default=False, - ), AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-harbour-mainnet", @@ -230,11 +220,6 @@ ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( "0x0000000000000000000000000000000000000000" ), - ORACLE_IGNORE_STAKEWISE_OPERATOR=config( - "ORACLE_IGNORE_STAKEWISE_OPERATOR", - cast=bool, - default=False, - ), 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=""), @@ -308,11 +293,6 @@ ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( "0x0000000000000000000000000000000000000000" ), - ORACLE_IGNORE_STAKEWISE_OPERATOR=config( - "ORACLE_IGNORE_STAKEWISE_OPERATOR", - cast=bool, - default=False, - ), AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-perm-goerli", @@ -389,11 +369,6 @@ ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( "0x0000000000000000000000000000000000000000" ), - ORACLE_IGNORE_STAKEWISE_OPERATOR=config( - "ORACLE_IGNORE_STAKEWISE_OPERATOR", - cast=bool, - default=True, - ), 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 98c8db3..e7ed181 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 diff --git a/oracle/oracle/validators/eth1.py b/oracle/oracle/validators/eth1.py index 7c665d3..cbf55fa 100644 --- a/oracle/oracle/validators/eth1.py +++ b/oracle/oracle/validators/eth1.py @@ -35,13 +35,15 @@ async def select_validator( 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"] - _move_to_bottom(operators, last_operator_id) + index = _find_operator_index(operators, last_operator_id) + if index != len(operators) - 1: + operators = operators[index + 1 :] + [operators[index]] + operators[:index] - if NETWORK_CONFIG["ORACLE_IGNORE_STAKEWISE_OPERATOR"]: - _move_to_bottom(operators, NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"]) + _move_to_bottom(operators, NETWORK_CONFIG["ORACLE_STAKEWISE_OPERATOR"]) for operator in operators: merkle_proofs = operator["depositDataMerkleProofs"] @@ -107,6 +109,17 @@ async def get_validators_deposit_root(block_number: BlockNumber) -> HexStr: def _move_to_bottom(operators, operator_id): + if operator_id == Web3.toChecksumAddress( + "0x0000000000000000000000000000000000000000" + ): + 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 for i, operator in enumerate(operators): if Web3.toChecksumAddress(operator["id"]) == Web3.toChecksumAddress( @@ -114,5 +127,4 @@ def _move_to_bottom(operators, operator_id): ): index = i break - if index is not None: - operators.append(operators.pop(index)) + return index From 353cb60957126bd0b4ae7de6c1dc2951c3a5c001 Mon Sep 17 00:00:00 2001 From: cyc60 Date: Tue, 18 Oct 2022 15:28:18 +0300 Subject: [PATCH 4/5] Operators select: review fixes Signed-off-by: cyc60 --- oracle/oracle/validators/eth1.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/oracle/oracle/validators/eth1.py b/oracle/oracle/validators/eth1.py index cbf55fa..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 @@ -40,7 +41,7 @@ async def select_validator( if last_validators: last_operator_id = last_validators[0]["operator"]["id"] index = _find_operator_index(operators, last_operator_id) - if index != len(operators) - 1: + 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"]) @@ -109,9 +110,7 @@ async def get_validators_deposit_root(block_number: BlockNumber) -> HexStr: def _move_to_bottom(operators, operator_id): - if operator_id == Web3.toChecksumAddress( - "0x0000000000000000000000000000000000000000" - ): + if operator_id == EMPTY_ADDR_HEX: return index = _find_operator_index(operators, operator_id) @@ -121,10 +120,9 @@ def _move_to_bottom(operators, operator_id): 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"]) == Web3.toChecksumAddress( - operator_id - ): + if Web3.toChecksumAddress(operator["id"]) == operator_id: index = i break return index From 8f0b6616f01fbe6a23c24594d59e986e002b6959 Mon Sep 17 00:00:00 2001 From: cyc60 Date: Tue, 18 Oct 2022 16:15:38 +0300 Subject: [PATCH 5/5] Operators select: review fixes Signed-off-by: cyc60 --- oracle/networks.py | 17 +++++------------ oracle/oracle/common/graphql_queries.py | 1 - 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/oracle/networks.py b/oracle/networks.py index da8dc36..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 @@ -141,9 +142,7 @@ "0x0100000000000000000000005c631621b897f467dd6a91855a0bc97d77b78dc0" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), - ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0x0000000000000000000000000000000000000000" - ), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-harbour-mainnet", @@ -217,9 +216,7 @@ "0x010000000000000000000000040f15c6b5bfc5f324ecab5864c38d4e1eef4218" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), - ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0x0000000000000000000000000000000000000000" - ), + 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=""), @@ -290,9 +287,7 @@ "0x0100000000000000000000006dfc9682e3c3263758ad96e2b2ba9822167f81ee" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), - ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0x0000000000000000000000000000000000000000" - ), + ORACLE_STAKEWISE_OPERATOR=EMPTY_ADDR_HEX, AWS_BUCKET_NAME=config( "AWS_BUCKET_NAME", default="oracle-votes-perm-goerli", @@ -366,9 +361,7 @@ "0x010000000000000000000000fc9b67b6034f6b306ea9bd8ec1baf3efa2490394" ), ORACLE_PRIVATE_KEY=config("ORACLE_PRIVATE_KEY", default=""), - ORACLE_STAKEWISE_OPERATOR=Web3.toChecksumAddress( - "0x0000000000000000000000000000000000000000" - ), + 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 e7ed181..428fc68 100644 --- a/oracle/oracle/common/graphql_queries.py +++ b/oracle/oracle/common/graphql_queries.py @@ -420,7 +420,6 @@ orderDirection: desc first: 1 ) { - id operator { id }