Skip to content

Commit

Permalink
pow-migration: Fix accounts migration
Browse files Browse the repository at this point in the history
Fix accounts migration by setting the `Policy.NUM_SNAPSHOTS_MAX`
constant that allows to get accounts tree chunks for blocks that are
this value below the head of the chain.
  • Loading branch information
jsdanielh committed Apr 22, 2024
1 parent 7484b4c commit eaa8fda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pow-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -150,7 +150,10 @@ pub async fn migrate(
validator_address: &Address,
network_id: NetworkId,
) -> Result<Option<GenesisConfig>, 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,
Expand Down
27 changes: 26 additions & 1 deletion pow-migration/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenesisAccount, Error> {
let address = Address::from_user_friendly_address(&pow_account.address)?;
let balance = Coin::try_from(pow_account.balance)?;
Expand Down Expand Up @@ -111,6 +115,18 @@ fn pos_anyhash_from_hash_root(hash_root: &str, algorithm: u8) -> Result<AnyHash,
}
}

/// Sets up the POW RPC server for migrating accounts
pub async fn setup_pow_rpc_server(client: &Client) -> 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(
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pow-migration/src/state/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eaa8fda

Please sign in to comment.