Skip to content

Commit

Permalink
[Enhancement] Macros for State.GetAll* (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruseinov authored Jul 17, 2024
1 parent c46451b commit 54d8136
Showing 1 changed file with 51 additions and 204 deletions.
255 changes: 51 additions & 204 deletions src/shim/actors/verifreg/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@

use super::*;
use anyhow::Context as _;
macro_rules! list_all_inner_pre_v12 {
($state:ident, $store:ident, $version:ident, $method:ident, $map:ident) => {{
let mut entities = $state.$method($store)?;
let mut actors = vec![];
entities.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::$version::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
entities.for_each(actor_id, |k, v| {
let claim_id = fil_actors_shared::$version::parse_uint_key(k)?;
$map.insert(claim_id, v.into());
Ok(())
})?;
}
}};
}

macro_rules! list_all_inner {
($state:ident, $store:ident, $version:ident, $method:ident, $map:ident) => {{
let mut entities = $state.$method($store)?;
let mut actors = vec![];
entities.for_each(|k, _| {
let actor_id = fil_actors_shared::$version::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
entities.for_each_in(actor_id, |k, v| {
let claim_id = fil_actors_shared::$version::parse_uint_key(k)?;
$map.insert(claim_id, v.into());
Ok(())
})?;
}
}};
}

impl VerifiedRegistryStateExt for State {
fn get_allocations<BS: Blockstore>(
Expand Down Expand Up @@ -73,108 +112,12 @@ impl VerifiedRegistryStateExt for State {
let mut result = HashMap::default();
match self {
State::V8(_) => return Err(anyhow::anyhow!("unsupported in actors v8")),
State::V9(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v9::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v9::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V10(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v10::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v10::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V11(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v11::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v11::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V12(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each(|k, _| {
let actor_id = fil_actors_shared::v12::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each_in(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v12::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V13(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each(|k, _| {
let actor_id = fil_actors_shared::v13::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each_in(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v13::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V14(state) => {
let mut map = state.load_allocs(store)?;
let mut actors = vec![];
map.for_each(|k, _| {
let actor_id = fil_actors_shared::v14::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
map.for_each_in(actor_id, |k, v| {
let allocation_id = fil_actors_shared::v14::parse_uint_key(k)?;
result.insert(allocation_id, v.into());
Ok(())
})?;
}
}
State::V9(state) => list_all_inner_pre_v12!(state, store, v9, load_allocs, result),
State::V10(state) => list_all_inner_pre_v12!(state, store, v10, load_allocs, result),
State::V11(state) => list_all_inner_pre_v12!(state, store, v11, load_allocs, result),
State::V12(state) => list_all_inner!(state, store, v12, load_allocs, result),
State::V13(state) => list_all_inner!(state, store, v13, load_allocs, result),
State::V14(state) => list_all_inner!(state, store, v14, load_allocs, result),
};
Ok(result)
}
Expand Down Expand Up @@ -249,108 +192,12 @@ impl VerifiedRegistryStateExt for State {
let mut result = HashMap::default();
match self {
Self::V8(_) => return Err(anyhow::anyhow!("unsupported in actors v8")),
Self::V9(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v9::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each(actor_id, |k, v| {
let claim_id = fil_actors_shared::v9::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
Self::V10(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v10::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each(actor_id, |k, v| {
let claim_id = fil_actors_shared::v10::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
Self::V11(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each_outer(|k, _| {
let actor_id = fil_actors_shared::v11::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each(actor_id, |k, v| {
let claim_id = fil_actors_shared::v11::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
Self::V12(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each(|k, _| {
let actor_id = fil_actors_shared::v12::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each_in(actor_id, |k, v| {
let claim_id = fil_actors_shared::v12::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
Self::V13(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each(|k, _| {
let actor_id = fil_actors_shared::v13::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each_in(actor_id, |k, v| {
let claim_id = fil_actors_shared::v13::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
Self::V14(s) => {
let mut claims = s.load_claims(store)?;
let mut actors = vec![];
claims.for_each(|k, _| {
let actor_id = fil_actors_shared::v14::parse_uint_key(k)?;
actors.push(actor_id);
Ok(())
})?;

for actor_id in actors {
claims.for_each_in(actor_id, |k, v| {
let claim_id = fil_actors_shared::v14::parse_uint_key(k)?;
result.insert(claim_id, v.into());
Ok(())
})?;
}
}
State::V9(state) => list_all_inner_pre_v12!(state, store, v9, load_claims, result),
State::V10(state) => list_all_inner_pre_v12!(state, store, v10, load_claims, result),
State::V11(state) => list_all_inner_pre_v12!(state, store, v11, load_claims, result),
State::V12(state) => list_all_inner!(state, store, v12, load_claims, result),
State::V13(state) => list_all_inner!(state, store, v13, load_claims, result),
State::V14(state) => list_all_inner!(state, store, v14, load_claims, result),
};
Ok(result)
}
Expand Down

0 comments on commit 54d8136

Please sign in to comment.