Skip to content

Commit

Permalink
Refactor keeper health server
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi committed Dec 14, 2021
1 parent 5860dc9 commit 076def2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ docker-compose -f docker-compose.yml up -d
| ORACLE_PRIVATE_KEY | The ETH1 private key of the oracle | Yes | - |
| KEEPER_PROCESS_INTERVAL | How long to wait before processing again (in seconds) | No | 180 |
| ETH1_CONFIRMATION_BLOCKS | The required number of ETH1 confirmation blocks used to fetch the data | No | 15 |
| KEEPER_MIN_BALANCE_WEI | The minimum balance keeper must have for votes submission | No | 0.1 ETH |
| LOG_LEVEL | The log level of the keeper | No | INFO |
3 changes: 2 additions & 1 deletion deploy/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ORACLE_PROCESS_INTERVAL=180
WEB3_ENDPOINT=""
KEEPER_PROCESS_INTERVAL=180
TRANSACTION_TIMEOUT=900

# 0.1 ETH
KEEPER_MIN_BALANCE_WEI=100000000000000000

# GRAPH
postgres_host=postgres
Expand Down
9 changes: 9 additions & 0 deletions oracle/keeper/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def get_oracles_contract() -> Contract:
"stateMutability": "view",
"type": "function",
},
{
"inputs": [
{"internalType": "address", "name": "account", "type": "address"}
],
"name": "isOracle",
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
"stateMutability": "view",
"type": "function",
},
{
"inputs": [
{
Expand Down
72 changes: 17 additions & 55 deletions oracle/keeper/health_server.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,38 @@
from collections import Counter

from aiohttp import web

from oracle.keeper.utils import can_submit, get_keeper_params, get_oracles_votes
from oracle.keeper.contracts import get_oracles_contract
from oracle.keeper.settings import KEEPER_MIN_BALANCE_WEI
from oracle.keeper.utils import get_keeper_params, get_oracles_votes

keeper_routes = web.RouteTableDef()


@keeper_routes.get("/")
async def health(request):
try:
# 1. Fetch current nonces of the validators, rewards and the total number of oracles
oracles = get_oracles_contract()
oracle = oracles.web3.eth.default_account

# Check ETH1 node connection and oracle is part of the set
assert oracles.functions.isOracle(oracles.web3.eth.default_account).call()

# Check oracle has enough balance
balance = oracles.web3.eth.get_balance(oracle)
assert balance > KEEPER_MIN_BALANCE_WEI

# Can fetch oracle votes and is not paused
params = get_keeper_params()
if params.paused:
return web.Response(text="keeper 0")

# 2. Resolve and fetch latest votes of the oracles for validators and rewards
votes = get_oracles_votes(
# Can resolve and fetch latest votes of the oracles
get_oracles_votes(
rewards_nonce=params.rewards_nonce,
validators_nonce=params.validators_nonce,
oracles=params.oracles,
)

# 3. Check whether there are no submitted votes
counter = Counter(
[
(vote["total_rewards"], vote["activated_validators"])
for vote in votes.rewards
]
)
most_voted_rewards = counter.most_common(1)

counter = Counter(
[(vote["merkle_root"], vote["merkle_proofs"]) for vote in votes.distributor]
)
most_voted_distributor = counter.most_common(1)

counter = Counter(
[
(vote["public_key"], vote["operator"])
for vote in votes.initialize_validator
]
)
most_voted_init_validator = counter.most_common(1)

counter = Counter(
[
(vote["public_key"], vote["operator"])
for vote in votes.finalize_validator
]
)
most_voted_finalize_validator = counter.most_common(1)

if not (
(
most_voted_rewards
and can_submit(most_voted_rewards[0][1], len(params.oracles))
)
or (
most_voted_distributor
and can_submit(most_voted_distributor[0][1], len(params.oracles))
)
or (
most_voted_init_validator
and can_submit(most_voted_init_validator[0][1], len(params.oracles))
)
or (
most_voted_finalize_validator
and can_submit(most_voted_finalize_validator[0][1], len(params.oracles))
)
):
return web.Response(text="keeper 1")
return web.Response(text="keeper 1")
except: # noqa: E722
pass

Expand Down
4 changes: 4 additions & 0 deletions oracle/keeper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

KEEPER_PROCESS_INTERVAL = config("KEEPER_PROCESS_INTERVAL", default=180, cast=int)

KEEPER_MIN_BALANCE_WEI = config(
"KEEPER_MIN_BALANCE_WEI", default=Web3.toWei(0.1, "ether"), cast=int
)

TRANSACTION_TIMEOUT = config("TRANSACTION_TIMEOUT", default=900, cast=int)

if NETWORK == MAINNET:
Expand Down

0 comments on commit 076def2

Please sign in to comment.