From f2959156257f4a63df187b01ea589187c88b2c7d Mon Sep 17 00:00:00 2001 From: Sander Pick Date: Mon, 10 Jun 2024 16:15:05 -0700 Subject: [PATCH] acc: return optional with get_leaf_at Signed-off-by: Sander Pick --- fendermint/actors/accumulator/src/actor.rs | 2 +- fendermint/actors/accumulator/src/shared.rs | 28 +++++++++------------ fendermint/rpc/src/response.rs | 6 ++--- fendermint/rpc/src/tx.rs | 4 +-- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/fendermint/actors/accumulator/src/actor.rs b/fendermint/actors/accumulator/src/actor.rs index 9e6ddd8f..20b67a8f 100644 --- a/fendermint/actors/accumulator/src/actor.rs +++ b/fendermint/actors/accumulator/src/actor.rs @@ -42,7 +42,7 @@ impl Actor { }) } - fn get_leaf_at(rt: &impl Runtime, index: u64) -> Result, ActorError> { + fn get_leaf_at(rt: &impl Runtime, index: u64) -> Result>, ActorError> { rt.validate_immediate_caller_accept_any()?; let st: State = rt.state()?; st.get_obj(rt.store(), index) diff --git a/fendermint/actors/accumulator/src/shared.rs b/fendermint/actors/accumulator/src/shared.rs index 536c176e..58f02913 100644 --- a/fendermint/actors/accumulator/src/shared.rs +++ b/fendermint/actors/accumulator/src/shared.rs @@ -158,7 +158,7 @@ fn get_at( leaf_index: u64, leaf_count: u64, peaks: &Amt, -) -> anyhow::Result { +) -> anyhow::Result> { let (path, eigen_index) = path_for_eigen_root(leaf_index, leaf_count); let cid = match peaks.get(eigen_index)? { Some(cid) => cid, @@ -172,8 +172,8 @@ fn get_at( // Special case where eigentree has height of one if path == 1 { return match store.get_cbor::(cid)? { - Some(value) => Ok(value), - None => return Err(anyhow::anyhow!("failed to get leaf for cid {}", cid)), + Some(value) => Ok(Some(value)), + None => Err(anyhow::anyhow!("failed to get leaf for cid {}", cid)), }; } @@ -207,11 +207,7 @@ fn get_at( let bit = (path & 1) as usize; let cid = &pair[bit]; - let leaf = match store.get_cbor::(cid)? { - Some(root) => root, - None => return Err(anyhow::anyhow!("failed to get leaf for cid {}", cid)), - }; - Ok(leaf) + store.get_cbor::(cid) } /// The state represents an MMR with peaks stored in an AMT @@ -307,7 +303,7 @@ impl State { &self, store: &BS, index: u64, - ) -> anyhow::Result { + ) -> anyhow::Result> { let amt = Amt::::load(&self.peaks, store)?; get_at::(store, index, self.leaf_count, &amt) } @@ -422,23 +418,23 @@ mod tests { state.push(&store, vec![0]).unwrap(); assert_eq!(state.peak_count(), 1); assert_eq!(state.leaf_count(), 1); - let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap(); + let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap().unwrap(); assert_eq!(item0, vec![0]); state.push(&store, vec![1]).unwrap(); assert_eq!(state.peak_count(), 1); assert_eq!(state.leaf_count(), 2); - let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap(); - let item1 = state.get_obj::<_, Vec>(&store, 1u64).unwrap(); + let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap().unwrap(); + let item1 = state.get_obj::<_, Vec>(&store, 1u64).unwrap().unwrap(); assert_eq!(item0, vec![0]); assert_eq!(item1, vec![1]); state.push(&store, vec![2]).unwrap(); assert_eq!(state.peak_count(), 2); assert_eq!(state.leaf_count(), 3); - let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap(); - let item1 = state.get_obj::<_, Vec>(&store, 1u64).unwrap(); - let item2 = state.get_obj::<_, Vec>(&store, 2u64).unwrap(); + let item0 = state.get_obj::<_, Vec>(&store, 0u64).unwrap().unwrap(); + let item1 = state.get_obj::<_, Vec>(&store, 1u64).unwrap().unwrap(); + let item2 = state.get_obj::<_, Vec>(&store, 2u64).unwrap().unwrap(); assert_eq!(item0, vec![0]); assert_eq!(item1, vec![1]); assert_eq!(item2, vec![2]); @@ -455,7 +451,7 @@ mod tests { // As more items are added to the accumulator, ensure each item remains gettable at // each phase of the growth of the inner tree structures. for j in 0..i { - let item = state.get_obj::<_, Vec>(&store, j).unwrap(); + let item = state.get_obj::<_, Vec>(&store, j).unwrap().unwrap(); assert_eq!(item, vec![j]); } } diff --git a/fendermint/rpc/src/response.rs b/fendermint/rpc/src/response.rs index 1574e65e..d08b05fe 100644 --- a/fendermint/rpc/src/response.rs +++ b/fendermint/rpc/src/response.rs @@ -78,10 +78,10 @@ pub fn decode_acc_push_return(deliver_tx: &DeliverTx) -> anyhow::Result anyhow::Result> { +pub fn decode_acc_get_at(deliver_tx: &DeliverTx) -> anyhow::Result>> { let data = decode_data(&deliver_tx.data)?; - fvm_ipld_encoding::from_slice(data.as_slice()) - .map_err(|e| anyhow!("error parsing as Vec: {e}")) + fvm_ipld_encoding::from_slice::>>(&data) + .map_err(|e| anyhow!("error parsing as Option>: {e}")) } /// Parse what Tendermint returns in the `data` field of [`DeliverTx`] as [`CreateReturn`]. diff --git a/fendermint/rpc/src/tx.rs b/fendermint/rpc/src/tx.rs index 623772ac..f31ce14e 100644 --- a/fendermint/rpc/src/tx.rs +++ b/fendermint/rpc/src/tx.rs @@ -319,12 +319,12 @@ pub trait CallClient: QueryClient + BoundClient { return Err(anyhow!("{}", response.value.info)); } - let res = decode_acc_get_at(&response.value) + let return_data = decode_acc_get_at(&response.value) .context("error decoding data from deliver_tx in call")?; let response = CallResponse { response, - return_data: Some(res), + return_data, }; Ok(response)