From 54d8136dc2fda48df1035b3aad384e696e646376 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 17 Jul 2024 17:23:30 +0200 Subject: [PATCH] [Enhancement] Macros for State.GetAll* (#4551) --- src/shim/actors/verifreg/state.rs | 255 ++++++------------------------ 1 file changed, 51 insertions(+), 204 deletions(-) diff --git a/src/shim/actors/verifreg/state.rs b/src/shim/actors/verifreg/state.rs index 463e476148a9..36db43be9002 100644 --- a/src/shim/actors/verifreg/state.rs +++ b/src/shim/actors/verifreg/state.rs @@ -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( @@ -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) } @@ -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) }