Skip to content

Commit

Permalink
Add flake8-newspaper (#441)
Browse files Browse the repository at this point in the history
* Add flake8-newspaper

* Fix flake8-newspaper in hashi vault
  • Loading branch information
evgeny-stakewise authored Dec 20, 2024
1 parent 8a79226 commit 18fc6f9
Show file tree
Hide file tree
Showing 20 changed files with 839 additions and 780 deletions.
870 changes: 462 additions & 408 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ coverage = "==7.3.2"
aioresponses = "^0.7.4"
types-requests = "==2.31.0.20240406"
types-setuptools = "==70.0.0.20240524"
flake8-newspaper-style = "==0.4.3"

[tool.poetry.group.build.dependencies]
pyinstaller = "==5.13.2"
Expand Down
36 changes: 18 additions & 18 deletions src/common/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ def amount(self) -> int:
def withdrawal_credentials(self) -> Bytes32:
return get_eth1_withdrawal_credentials(self.vault)

def encrypt_signing_keystore(self, password: str) -> Keystore:
return ScryptKeystore.encrypt(
secret=self.private_key_bytes,
password=password,
path=self.path,
kdf_salt=randbits(256).to_bytes(32, 'big'),
aes_iv=randbits(128).to_bytes(16, 'big'),
)

def save_signing_keystore(
self, password: str, folder: str, per_keystore_password: bool = False
) -> str:
Expand All @@ -86,6 +77,15 @@ def save_signing_keystore(
keystore.save(file_path)
return file_path

def encrypt_signing_keystore(self, password: str) -> Keystore:
return ScryptKeystore.encrypt(
secret=self.private_key_bytes,
password=password,
path=self.path,
kdf_salt=randbits(256).to_bytes(32, 'big'),
aes_iv=randbits(128).to_bytes(16, 'big'),
)

@property
def deposit_message(self) -> DepositMessage:
return DepositMessage(
Expand Down Expand Up @@ -180,6 +180,15 @@ def _generate_credentials_chunk(
credentials.append(credential)
return credentials

@staticmethod
def generate_credential_first_public_key(network: str, vault: HexAddress, mnemonic: str) -> str:
return CredentialManager.generate_credential(
network=network,
vault=vault,
mnemonic=mnemonic,
index=0,
).public_key

@staticmethod
def generate_credential(
network: str, vault: HexAddress, mnemonic: str, index: int
Expand All @@ -196,12 +205,3 @@ def generate_credential(
return Credential(
private_key=private_key, path=signing_key_path, network=network, vault=vault
)

@staticmethod
def generate_credential_first_public_key(network: str, vault: HexAddress, mnemonic: str) -> str:
return CredentialManager.generate_credential(
network=network,
vault=vault,
mnemonic=mnemonic,
index=0,
).public_key
100 changes: 50 additions & 50 deletions src/common/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,24 @@
logger = logging.getLogger(__name__)


async def get_hot_wallet_balance() -> Wei:
return await execution_client.eth.get_balance(hot_wallet.address)


async def check_vault_address() -> None:
try:
await Vault().get_validators_root()
except BadFunctionCallOutput as e:
raise click.ClickException('Invalid vault contract address') from e


async def check_hot_wallet_balance() -> None:
hot_wallet_min_balance = settings.network_config.HOT_WALLET_MIN_BALANCE
symbol = settings.network_config.WALLET_BALANCE_SYMBOL

if hot_wallet_min_balance <= 0:
return

hot_wallet_balance = await get_hot_wallet_balance()

metrics.wallet_balance.set(hot_wallet_balance)
async def get_protocol_config() -> ProtocolConfig:
await update_oracles_cache()
app_state = AppState()

if hot_wallet_balance < hot_wallet_min_balance:
logger.warning(
'Wallet %s balance is too low. At least %s %s is recommended.',
hot_wallet.address,
Web3.from_wei(hot_wallet_min_balance, 'ether'),
symbol,
)
oracles_cache = cast(OraclesCache, app_state.oracles_cache)
pc = build_protocol_config(
config_data=oracles_cache.config,
rewards_threshold=oracles_cache.rewards_threshold,
validators_threshold=oracles_cache.validators_threshold,
)
return pc


async def update_oracles_cache() -> None:
Expand Down Expand Up @@ -100,17 +89,24 @@ async def update_oracles_cache() -> None:
)


async def get_protocol_config() -> ProtocolConfig:
await update_oracles_cache()
app_state = AppState()
async def check_gas_price(high_priority: bool = False) -> bool:
if high_priority:
tx_params = await get_high_priority_tx_params()
max_fee_per_gas = Wei(int(tx_params['maxFeePerGas']))
else:
# fallback to logic from web3
max_fee_per_gas = await _max_fee_per_gas(execution_client, {})

oracles_cache = cast(OraclesCache, app_state.oracles_cache)
pc = build_protocol_config(
config_data=oracles_cache.config,
rewards_threshold=oracles_cache.rewards_threshold,
validators_threshold=oracles_cache.validators_threshold,
)
return pc
if max_fee_per_gas >= Web3.to_wei(settings.max_fee_per_gas_gwei, 'gwei'):
logging.warning(
'Current gas price (%s gwei) is too high. '
'Will try to submit transaction on the next block if the gas '
'price is acceptable.',
Web3.from_wei(max_fee_per_gas, 'gwei'),
)
return False

return True


async def get_high_priority_tx_params() -> TxParams:
Expand Down Expand Up @@ -152,26 +148,30 @@ async def _calc_high_priority_fee() -> Wei:
return Wei(mean_reward)


async def check_gas_price(high_priority: bool = False) -> bool:
if high_priority:
tx_params = await get_high_priority_tx_params()
max_fee_per_gas = Wei(int(tx_params['maxFeePerGas']))
else:
# fallback to logic from web3
max_fee_per_gas = await _max_fee_per_gas(execution_client, {})
class WalletTask(BaseTask):
async def process_block(self, interrupt_handler: InterruptHandler) -> None:
await check_hot_wallet_balance()

if max_fee_per_gas >= Web3.to_wei(settings.max_fee_per_gas_gwei, 'gwei'):
logging.warning(
'Current gas price (%s gwei) is too high. '
'Will try to submit transaction on the next block if the gas '
'price is acceptable.',
Web3.from_wei(max_fee_per_gas, 'gwei'),
)
return False

return True
async def check_hot_wallet_balance() -> None:
hot_wallet_min_balance = settings.network_config.HOT_WALLET_MIN_BALANCE
symbol = settings.network_config.WALLET_BALANCE_SYMBOL

if hot_wallet_min_balance <= 0:
return

class WalletTask(BaseTask):
async def process_block(self, interrupt_handler: InterruptHandler) -> None:
await check_hot_wallet_balance()
hot_wallet_balance = await get_hot_wallet_balance()

metrics.wallet_balance.set(hot_wallet_balance)

if hot_wallet_balance < hot_wallet_min_balance:
logger.warning(
'Wallet %s balance is too low. At least %s %s is recommended.',
hot_wallet.address,
Web3.from_wei(hot_wallet_min_balance, 'ether'),
symbol,
)


async def get_hot_wallet_balance() -> Wei:
return await execution_client.eth.get_balance(hot_wallet.address)
10 changes: 5 additions & 5 deletions src/common/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ def __init__(self) -> None:
)
self.gql_client = Client(transport=transport)

@retry_gql_errors(delay=DEFAULT_RETRY_TIME)
async def run_query(self, query: DocumentNode, params: dict | None = None) -> dict:
result = await self.gql_client.execute_async(query, variable_values=params)
return result

async def get_vault_validators(self, vault: str) -> list[str]:
query = gql(
"""
Expand Down Expand Up @@ -55,3 +50,8 @@ async def get_vault_validators(self, vault: str) -> list[str]:
variables['skip'] += GRAPH_PAGE_SIZE

return all_validators

@retry_gql_errors(delay=DEFAULT_RETRY_TIME)
async def run_query(self, query: DocumentNode, params: dict | None = None) -> dict:
result = await self.gql_client.execute_async(query, variable_values=params)
return result
26 changes: 13 additions & 13 deletions src/common/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
SPECIAL_CHARS = '!@#$%^&*()_'


def get_or_create_password_file(password_file: Path) -> str:
try:
with open(password_file, 'r', encoding='utf-8') as file:
password = file.readline()
except FileNotFoundError:
password = generate_password()
password_file.parent.mkdir(parents=True, exist_ok=True)
with open(password_file, 'w', encoding='utf-8') as file:
file.write(password)

return password


def generate_password() -> str:
alphabet = string.ascii_letters + string.digits + SPECIAL_CHARS
lower_set = set(string.ascii_lowercase)
Expand All @@ -21,16 +34,3 @@ def generate_password() -> str:
and digits_set.intersection(password_set)
):
return ''.join(password)


def get_or_create_password_file(password_file: Path) -> str:
try:
with open(password_file, 'r', encoding='utf-8') as file:
password = file.readline()
except FileNotFoundError:
password = generate_password()
password_file.parent.mkdir(parents=True, exist_ok=True)
with open(password_file, 'w', encoding='utf-8') as file:
file.write(password)

return password
Loading

0 comments on commit 18fc6f9

Please sign in to comment.