This commit is contained in:
Jędrzej Stuczyński
2023-12-18 15:40:01 +00:00
committed by Jon Häggblad
parent defd148d73
commit 6c1d14a4bc
8 changed files with 45 additions and 36 deletions
+8 -1
View File
@@ -4,6 +4,7 @@
use crate::cli::ConfigOverridableArgs;
use crate::config::{default_config_directory, default_data_directory, Config};
use crate::error::NymRewarderError;
use nym_network_defaults::NymNetworkDetails;
use std::path::PathBuf;
use std::{fs, io};
@@ -42,7 +43,13 @@ pub(crate) fn execute(args: Args) -> Result<(), NymRewarderError> {
init_paths().map_err(|source| NymRewarderError::PathInitialisationFailure { source })?;
let config = Config::new(args.mnemonic).with_override(args.config_override);
let network = NymNetworkDetails::new_from_env();
let nyxd = network.endpoints[0].nyxd_url();
let Some(websocket) = network.endpoints[0].websocket_url() else {
return Err(NymRewarderError::UnavailableWebsocketUrl);
};
let config = Config::new(args.mnemonic, websocket, nyxd).with_override(args.config_override);
config.ensure_is_valid()?;
config
+3 -15
View File
@@ -9,8 +9,6 @@ use nym_config::{
must_get_home, read_config_from_toml_file, save_formatted_config_to_file, NymConfigTemplate,
DEFAULT_CONFIG_DIR, DEFAULT_CONFIG_FILENAME, DEFAULT_DATA_DIR, NYM_DIR,
};
use nym_network_defaults::NymNetworkDetails;
use nym_validator_client::nyxd;
use nym_validator_client::nyxd::Coin;
use serde::{Deserialize, Serialize};
use std::io;
@@ -92,9 +90,7 @@ impl NymConfigTemplate for Config {
}
impl Config {
pub fn new(mnemonic: bip39::Mnemonic) -> Self {
let network = NymNetworkDetails::new_from_env();
pub fn new(mnemonic: bip39::Mnemonic, websocket_url: Url, nyxd_url: Url) -> Self {
Config {
save_path: None,
rewarding: Rewarding::default(),
@@ -102,12 +98,10 @@ impl Config {
issuance_monitor: IssuanceMonitor::default(),
nyxd_scraper: NyxdScraper {
enabled: true,
websocket_url: network.endpoints[0]
.websocket_url()
.expect("TODO: hardcoded websocket url is not available"),
websocket_url,
},
base: Base {
upstream_nyxd: network.endpoints[0].nyxd_url(),
upstream_nyxd: nyxd_url,
mnemonic,
},
storage_paths: Default::default(),
@@ -122,12 +116,6 @@ impl Config {
}
}
pub fn rpc_client_config(&self) -> nyxd::Config {
// TEMP
nyxd::Config::try_from_nym_network_details(&NymNetworkDetails::new_from_env())
.expect("failed to create nyxd client config")
}
pub fn ensure_is_valid(&self) -> Result<(), NymRewarderError> {
if self.block_signing.enabled && !self.nyxd_scraper.enabled {
return Err(NymRewarderError::BlockSigningRewardWithoutScraper);
+12 -6
View File
@@ -154,10 +154,16 @@ pub enum NymRewarderError {
#[error("can't enable block signing rewarding without the block scraper")]
BlockSigningRewardWithoutScraper,
#[error("the current rewarder balance is insufficient to start the process. The epoch budget is: {epoch_budget} while we currently have {balance}. (the minimum is set to {minimum})")]
InsufficientRewarderBalance {
epoch_budget: Coin,
balance: Coin,
minimum: Coin,
},
#[error("the current rewarder balance is insufficient to start the process. The epoch budget is: {} while we currently have {}. (the minimum is set to {})", .0.epoch_budget, .0.balance, .0.minimum)]
InsufficientRewarderBalance(Box<InsufficientBalance>),
#[error("the scraper websocket endpoint hasn't been provided")]
UnavailableWebsocketUrl,
}
#[derive(Debug)]
pub struct InsufficientBalance {
pub epoch_budget: Coin,
pub balance: Coin,
pub minimum: Coin,
}
@@ -109,6 +109,7 @@ impl MonitoringResults {
let next_epoch = guard.epoch.next();
// safety: whenever the monitor results are constructed, we always have at least a single value there
#[allow(clippy::unwrap_used)]
let latest_dkg_epoch = guard.dkg_epochs.pop().unwrap();
// only keep results from the latest dkg epoch (but after resetting the counters)
+9 -7
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::config::Config;
use crate::error::NymRewarderError;
use crate::error::{InsufficientBalance, NymRewarderError};
use crate::rewarder::block_signing::types::EpochSigningResults;
use crate::rewarder::block_signing::EpochSigning;
use crate::rewarder::credential_issuance::types::CredentialIssuanceResults;
@@ -83,7 +83,7 @@ impl Rewarder {
None
};
let nyxd_client = NyxdClient::new(&config);
let nyxd_client = NyxdClient::new(&config)?;
let storage = RewarderStorage::init(&config.storage_paths.reward_history).await?;
let current_epoch = if let Some(last_epoch) = storage.load_last_rewarding_epoch().await? {
last_epoch.next()
@@ -119,11 +119,13 @@ impl Rewarder {
);
if balance.amount < minimum.amount {
return Err(NymRewarderError::InsufficientRewarderBalance {
epoch_budget: config.rewarding.epoch_budget.clone(),
balance,
minimum,
});
return Err(NymRewarderError::InsufficientRewarderBalance(Box::new(
InsufficientBalance {
epoch_budget: config.rewarding.epoch_budget.clone(),
balance,
minimum,
},
)));
}
}
@@ -8,6 +8,7 @@ use nym_coconut_bandwidth_contract_common::events::{
COSMWASM_DEPOSITED_FUNDS_EVENT_TYPE, DEPOSIT_INFO, DEPOSIT_VALUE,
};
use nym_coconut_dkg_common::types::Epoch;
use nym_network_defaults::NymNetworkDetails;
use nym_validator_client::nyxd::contract_traits::{DkgQueryClient, PagedDkgQueryClient};
use nym_validator_client::nyxd::helpers::find_tx_attribute;
use nym_validator_client::nyxd::module_traits::staking::{
@@ -16,7 +17,7 @@ use nym_validator_client::nyxd::module_traits::staking::{
use nym_validator_client::nyxd::{
AccountId, Coin, CosmWasmClient, Hash, PageRequest, StakingQueryClient,
};
use nym_validator_client::DirectSigningHttpRpcNyxdClient;
use nym_validator_client::{nyxd, DirectSigningHttpRpcNyxdClient};
use std::ops::Deref;
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -27,8 +28,9 @@ pub struct NyxdClient {
}
impl NyxdClient {
pub(crate) fn new(config: &Config) -> Self {
let client_config = config.rpc_client_config();
pub(crate) fn new(config: &Config) -> Result<Self, NymRewarderError> {
let client_config =
nyxd::Config::try_from_nym_network_details(&NymNetworkDetails::new_from_env())?;
let nyxd_url = config.base.upstream_nyxd.as_str();
let mnemonic = config.base.mnemonic.clone();
@@ -37,12 +39,11 @@ impl NyxdClient {
client_config,
nyxd_url,
mnemonic,
)
.expect("Failed to connect to nyxd!");
)?;
NyxdClient {
Ok(NyxdClient {
inner: Arc::new(RwLock::new(inner)),
}
})
}
pub(crate) async fn address(&self) -> AccountId {
@@ -68,6 +68,7 @@ impl StorageManager {
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn insert_rewarding_epoch_block_signing_reward(
&self,
epoch: i64,
@@ -138,6 +139,7 @@ impl StorageManager {
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn insert_rewarding_epoch_credential_issuance_reward(
&self,
epoch: i64,
@@ -114,11 +114,13 @@ impl RewarderStorage {
}
// safety: we must have at least a single value here
#[allow(clippy::unwrap_used)]
let dkg_epoch_start = reward
.credentials
.as_ref()
.map(|c| *c.dkg_epochs.first().unwrap())
.unwrap_or_default();
#[allow(clippy::unwrap_used)]
let dkg_epoch_end = reward
.credentials
.as_ref()