Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
F4ever committed Apr 19, 2024
1 parent f4340ed commit 7fa669e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 77 deletions.
12 changes: 12 additions & 0 deletions src/blockchain/contracts/deposit_security_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def get_max_deposits(self, block_identifier: BlockIdentifier = 'latest'):
})
return response

def version(self, block_identifier: BlockIdentifier = 'latest'):
return 1


class DepositSecurityModuleContractV2(DepositSecurityModuleContract):
abi_path = './interfaces/DepositSecurityModuleV2.json'
Expand Down Expand Up @@ -217,3 +220,12 @@ def get_is_deposits_paused(self, block_identifier: BlockIdentifier = 'latest'):
'block_identifier': block_identifier.__repr__(),
})
return response

def version(self, block_identifier: BlockIdentifier = 'latest'):
response = self.functions.VERSION().call(block_identifier=block_identifier)
logger.info({
'msg': f'Call `VERSION()`.',
'value': response,
'block_identifier': block_identifier.__repr__(),
})
return response
2 changes: 1 addition & 1 deletion src/blockchain/web3_extentions/lido_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _load_contracts(self):
))

try:
self.deposit_security_module.functions.VERSION().call()
self.deposit_security_module.version()
except ContractLogicError:
logger.info({'msg': 'Use deposit security module V1.'})
self.deposit_security_module = cast(DepositSecurityModuleContract, self.w3.eth.contract(
Expand Down
24 changes: 24 additions & 0 deletions src/bots/depositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import defaultdict
from typing import Optional, Callable

from eth_account import Account
from eth_typing import Hash32
from schema import Or, Schema
from web3.types import BlockData
Expand All @@ -10,7 +11,9 @@
from blockchain.deposit_strategy.curated_module import CuratedModuleDepositStrategy
from blockchain.deposit_strategy.interface import ModuleDepositStrategyInterface
from blockchain.deposit_strategy.prefered_module_to_deposit import get_preferred_to_deposit_modules
from blockchain.executor import Executor
from blockchain.typings import Web3
from blockchain.web3_extentions.bundle import activate_relay
from cryptography.verify_signature import compute_vs
from metrics.metrics import (
ACCOUNT_BALANCE,
Expand All @@ -29,6 +32,27 @@
logger = logging.getLogger(__name__)


def run_depositor(w3):
if variables.AUCTION_BUNDLER_PRIVATE_KEY and variables.AUCTION_BUNDLER_URIS:
logger.info({'msg': 'Add private relays.'})
activate_relay(w3, Account.from_key(variables.AUCTION_BUNDLER_PRIVATE_KEY), variables.AUCTION_BUNDLER_URIS)
else:
logger.info({'msg': 'No flashbots available for this network.'})

logger.info({'msg': 'Initialize Depositor bot.'})
depositor_bot = DepositorBot(w3)

e = Executor(
w3,
depositor_bot.execute,
5,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute depositor as daemon.'})
e.execute_as_daemon()



class ModuleNotSupportedError(Exception):
pass

Expand Down
14 changes: 14 additions & 0 deletions src/bots/pauser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from web3.types import BlockData

import variables
from blockchain.executor import Executor
from blockchain.typings import Web3
from cryptography.verify_signature import compute_vs
from metrics.metrics import UNEXPECTED_EXCEPTIONS
Expand All @@ -20,6 +21,19 @@
logger = logging.getLogger(__name__)


def run_pauser(w3: Web3):
pause = PauserBot(w3)
e = Executor(
w3,
pause.execute,
1,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute pauser as daemon.'})
e.execute_as_daemon()



class PauserBot:
def __init__(self, w3: Web3):
self.w3 = w3
Expand Down
38 changes: 24 additions & 14 deletions src/bots/unvetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from web3.types import BlockData

import variables
from blockchain.executor import Executor
from blockchain.typings import Web3
from cryptography.verify_signature import compute_vs
from metrics.metrics import UNEXPECTED_EXCEPTIONS
Expand All @@ -20,18 +21,29 @@
logger = logging.getLogger(__name__)


class UnvetterBot:
def run_unvetter(w3: Web3):
unvetter = UnvetterBot(w3)
e = Executor(
w3,
unvetter.execute,
1,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute unvetter as daemon.'})
e.execute_as_daemon()


fully_initialized = False

class UnvetterBot:
message_storage: MessageStorage

def __init__(self, w3: Web3):
self.w3 = w3

if self.w3.lido.deposit_security_module.__class__.__name__ == 'DepositSecurityModuleContractV2':
self.finalize_init()
fully_initialized = True
def prepare_transport_bus(self):
if self.message_storage:
return

def finalize_init(self):
transports = []

if TransportType.RABBIT in variables.MESSAGE_TRANSPORTS:
Expand Down Expand Up @@ -62,12 +74,11 @@ def finalize_init(self):
)

def execute(self, block: BlockData) -> bool:
if not self.fully_initialized:
if self.w3.lido.deposit_security_module.__class__.__name__ == 'DepositSecurityModuleContractV2':
self.finalize_init()
self.fully_initialized = True
else:
return True
if self.w3.lido.deposit_security_module.version() == 1:
logger.warning({'msg': 'DSM version is not supported.'})
return True
else:
self.prepare_transport_bus()

messages = self.receive_unvet_messages()
logger.info({'msg': f'Received {len(messages)} unvet messages.'})
Expand Down Expand Up @@ -108,8 +119,7 @@ def _send_unvet_message(self, message: UnvetMessage) -> bool:

actual_nonce = self.w3.lido.staking_router.get_staking_module_nonce(module_id)

if message['nonce'] < actual_nonce:
self._clear_outdated_messages_for_module(module_id, actual_nonce)
self._clear_outdated_messages_for_module(module_id, actual_nonce)

unvet_tx = self.w3.lido.deposit_security_module.unvet_signing_keys(
message['blockNumber'],
Expand Down
27 changes: 3 additions & 24 deletions src/depositor.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
from eth_account import Account

import variables
from blockchain.executor import Executor
from blockchain.web3_extentions.bundle import activate_relay
from bots.depositor import DepositorBot
from main import main
from metrics.logging import logging


logger = logging.getLogger(__name__)


def run_depositor(w3):
if variables.AUCTION_BUNDLER_PRIVATE_KEY and variables.AUCTION_BUNDLER_URIS:
logger.info({'msg': 'Add private relays.'})
activate_relay(w3, Account.from_key(variables.AUCTION_BUNDLER_PRIVATE_KEY), variables.AUCTION_BUNDLER_URIS)
else:
logger.info({'msg': 'No flashbots available for this network.'})

logger.info({'msg': 'Initialize Depositor bot.'})
depositor_bot = DepositorBot(w3)

e = Executor(
w3,
depositor_bot.execute,
5,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute depositor as daemon.'})
e.execute_as_daemon()
if __name__ == '__main__':
main('depositor')
14 changes: 8 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from blockchain.web3_extentions.lido_contracts import LidoContracts
from blockchain.web3_extentions.requests_metric_middleware import add_requests_metric_middleware
from blockchain.web3_extentions.transaction import TransactionUtils
from depositor import run_depositor
from bots.depositor import run_depositor
from bots.pauser import run_pauser
from bots.unvetter import run_unvetter
from metrics.healthcheck_pulse import start_pulse_server
from metrics.logging import logging
from metrics.metrics import BUILD_INFO
from pauser import run_pauser
from unvetter import run_unvetter


logger = logging.getLogger(__name__)
Expand All @@ -26,9 +26,7 @@ class BotModule(StrEnum):
UNVETTER = 'unvetter'


if __name__ == '__main__':
bot_name = sys.argv[-1]

def main(bot_name: str):
if bot_name not in iter(BotModule):
msg = f'Last arg should be one of {[str(item) for item in BotModule]}, received {BotModule}.'
logger.error({'msg': msg})
Expand Down Expand Up @@ -74,3 +72,7 @@ class BotModule(StrEnum):
run_pauser(w3)
elif bot_name == BotModule.UNVETTER:
run_unvetter(w3)


if __name__ == '__main__':
main(sys.argv[-1])
18 changes: 3 additions & 15 deletions src/pauser.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
from web3 import Web3

import variables
from blockchain.executor import Executor
from bots.pauser import PauserBot
from main import main
from metrics.logging import logging


logger = logging.getLogger(__name__)


def run_pauser(w3: Web3):
pause = PauserBot(w3)
e = Executor(
w3,
pause.execute,
1,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute pauser as daemon.'})
e.execute_as_daemon()
if __name__ == '__main__':
main('pauser')
20 changes: 3 additions & 17 deletions src/unvetter.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import time

import variables
from blockchain.executor import Executor
from blockchain.typings import Web3
from bots.unvetter import UnvetterBot
from metrics.healthcheck_pulse import pulse
from main import main
from metrics.logging import logging


logger = logging.getLogger(__name__)


def run_unvetter(w3: Web3):
unvetter = UnvetterBot(w3)
e = Executor(
w3,
unvetter.execute,
1,
variables.MAX_CYCLE_LIFETIME_IN_SECONDS,
)
logger.info({'msg': 'Execute unvetter as daemon.'})
e.execute_as_daemon()
if __name__ == '__main__':
main('unvetter')

0 comments on commit 7fa669e

Please sign in to comment.