Skip to content

Commit

Permalink
address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
creativcoder committed May 17, 2021
1 parent 0d973e9 commit ed470cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
17 changes: 8 additions & 9 deletions vm/state_migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ use clock::ChainEpoch;
use ipld_blockstore::BlockStore;
use std::error::Error as StdError;
use std::rc::Rc;
use vm::ActorState;
use vm::TokenAmount;
use vm::{ActorState, TokenAmount};

pub mod nv12;

pub type MigrationResult<T> = Result<T, MigrationErr>;
pub type MigrationResult<T> = Result<T, MigrationError>;

#[derive(thiserror::Error, Debug)]
pub enum MigrationErr {
pub enum MigrationError {
#[error("Failed creating job for state migration")]
MigrationJobCreate(Box<dyn StdError>),
#[error("Failed running job for state migration: {0}")]
Expand All @@ -42,13 +41,13 @@ pub enum MigrationErr {
}

pub(crate) struct ActorMigrationInput {
/// actor's address
/// Actor's address
address: Address,
/// actor's balance
/// Actor's balance
balance: TokenAmount,
/// actor's state head CID
/// Actor's state head CID
head: Cid,
// epoch of last state transition prior to migration
/// Epoch of last state transition prior to migration
prior_epoch: ChainEpoch,
}

Expand Down Expand Up @@ -85,7 +84,7 @@ impl<'db, BS: BlockStore> MigrationJob<'db, BS> {
},
)
.map_err(|e| {
MigrationErr::MigrationJobRun(format!(
MigrationError::MigrationJobRun(format!(
"state migration failed for {} actor, addr {}:{}",
self.actor_state.code,
self.address,
Expand Down
14 changes: 6 additions & 8 deletions vm/state_migration/src/nv12/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

//! This module implements the miner actor state migration for network version 12 upgrade.
use crate::ActorMigration;
use crate::ActorMigrationInput;
use crate::MigrationErr;
use crate::MigrationOutput;
use crate::{ActorMigration, ActorMigrationInput};
use crate::{MigrationError, MigrationOutput, MigrationResult};
use actor_interface::actorv3::miner::State as V3State;
use actor_interface::actorv4::miner::State as V4State;
use cid::Cid;
Expand All @@ -26,11 +24,11 @@ impl<'db, BS: BlockStore> ActorMigration<'db, BS> for MinerMigrator {
&self,
store: &'db BS,
input: ActorMigrationInput,
) -> Result<MigrationOutput, MigrationErr> {
) -> MigrationResult<MigrationOutput> {
let v3_state: Option<V3State> = store
.get(&input.head)
.map_err(MigrationErr::BlockStoreRead)?;
let in_state: V3State = v3_state.ok_or(MigrationErr::BlockStoreRead(
.map_err(MigrationError::BlockStoreRead)?;
let in_state: V3State = v3_state.ok_or(MigrationError::BlockStoreRead(
Error::new(ErrorKind::Other, "Miner actor: could not read v3 state").into(),
))?;

Expand All @@ -54,7 +52,7 @@ impl<'db, BS: BlockStore> ActorMigration<'db, BS> for MinerMigrator {

let new_head = store
.put(&out_state, Blake2b256)
.map_err(MigrationErr::BlockStoreWrite)?;
.map_err(MigrationError::BlockStoreWrite)?;

Ok(MigrationOutput {
new_code_cid: self.0,
Expand Down
22 changes: 9 additions & 13 deletions vm/state_migration/src/nv12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
pub mod miner;

use crate::nil_migrator_v4;
use crate::MigrationErr;
use crate::MigrationResult;
use crate::{ActorMigration, MigrationJob};
use actor_interface::actorv3;
use actor_interface::actorv4;
use crate::{ActorMigration, MigrationError, MigrationJob, MigrationResult};
use actor_interface::{actorv3, actorv4};
use async_std::task;
use cid::Cid;
use clock::ChainEpoch;
Expand All @@ -24,8 +21,7 @@ use futures::StreamExt;
use ipld_blockstore::BlockStore;
use miner::miner_migrator_v4;
use state_tree::StateTree;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

const ACTORS_COUNT: usize = 11;
Expand Down Expand Up @@ -89,12 +85,12 @@ pub fn migrate_state_tree<'db, BS: BlockStore>(
let deferred_code_ids = HashSet::<Cid>::new(); // None in this migration

if migrations.len() + deferred_code_ids.len() != ACTORS_COUNT {
return Err(MigrationErr::IncompleteMigrationSpec(migrations.len()));
return Err(MigrationError::IncompleteMigrationSpec(migrations.len()));
}

let actors_in = StateTree::new_from_root(store, &actors_root_in).unwrap();
let mut actors_out =
StateTree::new(store, StateTreeVersion::V3).map_err(MigrationErr::StateTreeCreation)?;
StateTree::new(store, StateTreeVersion::V3).map_err(MigrationError::StateTreeCreation)?;

actors_in
.for_each(|addr, state| {
Expand All @@ -108,27 +104,27 @@ pub fn migrate_state_tree<'db, BS: BlockStore>(
actor_migration: migrations
.get(&state.code)
.cloned()
.ok_or(MigrationErr::MigratorNotFound(state.code))?,
.ok_or(MigrationError::MigratorNotFound(state.code))?,
};

jobs.push(async move { next_input.run(store, prior_epoch) });

Ok(())
})
.map_err(MigrationErr::MigrationJobCreate)?;
.map_err(MigrationError::MigrationJobCreate)?;

task::block_on(async {
while let Some(job_result) = jobs.next().await {
let result = job_result?;
actors_out
.set_actor(&result.address, result.actor_state)
.map_err(MigrationErr::SetActorState)?;
.map_err(MigrationError::SetActorState)?;
}

Ok(())
})?;

let root_cid = actors_out.flush().map_err(MigrationErr::FlushFailed);
let root_cid = actors_out.flush().map_err(MigrationError::FlushFailed);

root_cid
}

0 comments on commit ed470cc

Please sign in to comment.