diff --git a/pow-migration/src/lib.rs b/pow-migration/src/lib.rs index 4d98a1cdab..84b264e74a 100644 --- a/pow-migration/src/lib.rs +++ b/pow-migration/src/lib.rs @@ -22,7 +22,7 @@ use crate::{ check_validators_ready, generate_ready_tx, get_ready_txns, send_tx, types::ValidatorsReadiness, }, - state::{get_stakers, get_validators}, + state::{get_stakers, get_validators, setup_pow_rpc_server}, }; static TESTNET_BLOCK_WINDOWS: &BlockWindows = &BlockWindows { @@ -150,7 +150,10 @@ pub async fn migrate( validator_address: &Address, network_id: NetworkId, ) -> Result, Error> { - // First we obtain the list of registered validators + // First set up the PoW client for accounts migration + setup_pow_rpc_server(pow_client).await?; + + // Now we obtain the list of registered validators let registered_validators = get_validators( pow_client, block_windows.registration_start..block_windows.registration_end, diff --git a/pow-migration/src/state/mod.rs b/pow-migration/src/state/mod.rs index 0de755a070..91fa947aff 100644 --- a/pow-migration/src/state/mod.rs +++ b/pow-migration/src/state/mod.rs @@ -20,9 +20,13 @@ use nimiq_transaction::account::htlc_contract::{AnyHash, AnyHash32, AnyHash64}; use crate::state::types::{Error, GenesisAccounts, GenesisValidator}; -// POW estimated block time in milliseconds +// PoW estimated block time in milliseconds const POW_BLOCK_TIME_MS: u64 = 60 * 1000; // 1 min +// PoW maximum amount of snapshots. This is a constant that needs to be set in the PoW client +// such that we can get accounts snapshots of blocks within [head - `POW_MAX_SNAPSHOTS`, head]. +const POW_MAX_SNAPSHOTS: u64 = 2000; + fn pos_basic_account_from_account(pow_account: &PoWBasicAccount) -> Result { let address = Address::from_user_friendly_address(&pow_account.address)?; let balance = Coin::try_from(pow_account.balance)?; @@ -111,6 +115,18 @@ fn pos_anyhash_from_hash_root(hash_root: &str, algorithm: u8) -> Result Result<(), Error> { + let _ = client + .set_constant("Policy.NUM_SNAPSHOTS_MAX", POW_MAX_SNAPSHOTS) + .await + .map_err(|e| { + log::error!("Could not set `Policy.NUM_SNAPSHOTS_MAX` constant. Check your client or try updating it."); + e + })?; + Ok(()) +} + /// Gets the set of the Genesis Accounts by taking a snapshot of the accounts in /// a specific block number defined by `cutting_block`. pub async fn get_accounts( @@ -125,6 +141,15 @@ pub async fn get_accounts( htlc_accounts: vec![], }; let mut start_prefix = "".to_string(); + + // Check that the PoW client is already set up + if client.get_constant("Policy.NUM_SNAPSHOTS_MAX").await? != POW_MAX_SNAPSHOTS { + log::error!( + "RPC client is not set up for accounts migration. Call `setup_pow_rpc_server` first" + ); + return Err(Error::RPCServerNotReady); + } + loop { let chunk = client .get_accounts_tree_chunk(&cutting_block.hash, &start_prefix) diff --git a/pow-migration/src/state/types.rs b/pow-migration/src/state/types.rs index 205a0d6b3e..4bd8a29592 100644 --- a/pow-migration/src/state/types.rs +++ b/pow-migration/src/state/types.rs @@ -25,6 +25,9 @@ pub enum Error { /// Invalid Genesis timestamp #[error("Invalid genesis timestamp: {0}")] InvalidTimestamp(u64), + /// RPC server not ready + #[error("RPC server is not ready")] + RPCServerNotReady, } /// Genesis accounts for the genesis state