-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor statediff to own crate (#779)
* Refactor statediff out to be used elsewhere * Lint * Fix validation error
- Loading branch information
1 parent
6fb284a
commit 199d7fe
Showing
7 changed files
with
162 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "statediff" | ||
version = "0.1.0" | ||
authors = ["ChainSafe Systems <info@chainsafe.io>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde_json = "1.0" | ||
blockstore = { package = "ipld_blockstore", path = "../../ipld/blockstore/", features = [ | ||
"resolve" | ||
] } | ||
cid = { package = "forest_cid", path = "../../ipld/cid", features = [ | ||
"cbor", | ||
"json" | ||
] } | ||
difference = "2.0" | ||
colored = "2.0" | ||
ipld_hamt = { path = "../../ipld/hamt", features = ["ignore-dead-links"] } | ||
address = { package = "forest_address", path = "../../vm/address", features = [ | ||
"json" | ||
] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
ipld = { package = "forest_ipld", path = "../../ipld", features = ["json"] } | ||
vm = { package = "forest_vm", path = "../../vm" } | ||
fil_types = { path = "../../types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use address::Address; | ||
use blockstore::resolve::resolve_cids_recursive; | ||
use blockstore::BlockStore; | ||
use cid::{json::CidJson, Cid}; | ||
use colored::*; | ||
use difference::{Changeset, Difference}; | ||
use fil_types::HAMT_BIT_WIDTH; | ||
use ipld::json::{IpldJson, IpldJsonRef}; | ||
use ipld::Ipld; | ||
use ipld_hamt::{BytesKey, Hamt}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::BTreeMap; | ||
use std::error::Error as StdError; | ||
use vm::ActorState; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct ActorStateResolved { | ||
code: CidJson, | ||
sequence: u64, | ||
balance: String, | ||
state: IpldJson, | ||
} | ||
|
||
fn root_to_state_map<BS: BlockStore>( | ||
bs: &BS, | ||
root: &Cid, | ||
) -> Result<BTreeMap<String, ActorStateResolved>, Box<dyn StdError>> { | ||
let mut actors = BTreeMap::new(); | ||
let hamt: Hamt<_, _> = Hamt::load_with_bit_width(root, bs, HAMT_BIT_WIDTH)?; | ||
hamt.for_each(|k: &BytesKey, actor: &ActorState| { | ||
let addr = Address::from_bytes(&k.0)?; | ||
|
||
let resolved = resolve_cids_recursive(bs, &actor.state) | ||
.unwrap_or_else(|_| Ipld::Link(actor.state.clone())); | ||
let resolved_state = ActorStateResolved { | ||
state: IpldJson(resolved), | ||
code: CidJson(actor.code.clone()), | ||
balance: actor.balance.to_string(), | ||
sequence: actor.sequence, | ||
}; | ||
|
||
actors.insert(addr.to_string(), resolved_state); | ||
Ok(()) | ||
}) | ||
.unwrap(); | ||
|
||
Ok(actors) | ||
} | ||
|
||
/// Tries to resolve state tree actors, if all data exists in store. | ||
/// The actors hamt is hard to parse in a diff, so this attempts to remedy this. | ||
fn try_resolve_actor_states<BS: BlockStore>( | ||
bs: &BS, | ||
root: &Cid, | ||
expected_root: &Cid, | ||
) -> Result<Changeset, Box<dyn StdError>> { | ||
let e_state = root_to_state_map(bs, expected_root)?; | ||
let c_state = root_to_state_map(bs, root)?; | ||
|
||
let expected_json = serde_json::to_string_pretty(&e_state)?; | ||
let actual_json = serde_json::to_string_pretty(&c_state)?; | ||
|
||
Ok(Changeset::new(&expected_json, &actual_json, "\n")) | ||
} | ||
|
||
/// Prints a diff of the resolved state tree. | ||
/// If the actor's Hamt cannot be loaded, base ipld resolution is given. | ||
pub fn print_state_diff<BS>( | ||
bs: &BS, | ||
root: &Cid, | ||
expected_root: &Cid, | ||
) -> Result<(), Box<dyn StdError>> | ||
where | ||
BS: BlockStore, | ||
{ | ||
let Changeset { diffs, .. } = match try_resolve_actor_states(bs, root, expected_root) { | ||
Ok(cs) => cs, | ||
Err(e) => { | ||
println!( | ||
"Could not resolve actor states: {}\nUsing default resolution:", | ||
e | ||
); | ||
let expected = resolve_cids_recursive(bs, &expected_root)?; | ||
let actual = resolve_cids_recursive(bs, &root)?; | ||
|
||
let expected_json = serde_json::to_string_pretty(&IpldJsonRef(&expected))?; | ||
let actual_json = serde_json::to_string_pretty(&IpldJsonRef(&actual))?; | ||
|
||
Changeset::new(&expected_json, &actual_json, "\n") | ||
} | ||
}; | ||
|
||
for diff in diffs.iter() { | ||
match diff { | ||
Difference::Same(x) => { | ||
println!(" {}", x); | ||
} | ||
Difference::Add(x) => { | ||
println!("{}", format!("+{}", x).green()); | ||
} | ||
Difference::Rem(x) => { | ||
println!("{}", format!("-{}", x).red()); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |