Skip to content

Commit

Permalink
Merge pull request #94 from amazingdatamachine/sander/fix-acc-get-at
Browse files Browse the repository at this point in the history
Return optional with get_leaf_at
  • Loading branch information
sanderpick authored Jun 11, 2024
2 parents 588813f + f295915 commit 0e62386
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion fendermint/actors/accumulator/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Actor {
})
}

fn get_leaf_at(rt: &impl Runtime, index: u64) -> Result<Vec<u8>, ActorError> {
fn get_leaf_at(rt: &impl Runtime, index: u64) -> Result<Option<Vec<u8>>, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let st: State = rt.state()?;
st.get_obj(rt.store(), index)
Expand Down
28 changes: 12 additions & 16 deletions fendermint/actors/accumulator/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn get_at<BS: Blockstore, S: DeserializeOwned + Serialize>(
leaf_index: u64,
leaf_count: u64,
peaks: &Amt<Cid, &BS>,
) -> anyhow::Result<S> {
) -> anyhow::Result<Option<S>> {
let (path, eigen_index) = path_for_eigen_root(leaf_index, leaf_count);
let cid = match peaks.get(eigen_index)? {
Some(cid) => cid,
Expand All @@ -172,8 +172,8 @@ fn get_at<BS: Blockstore, S: DeserializeOwned + Serialize>(
// Special case where eigentree has height of one
if path == 1 {
return match store.get_cbor::<S>(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)),
};
}

Expand Down Expand Up @@ -207,11 +207,7 @@ fn get_at<BS: Blockstore, S: DeserializeOwned + Serialize>(

let bit = (path & 1) as usize;
let cid = &pair[bit];
let leaf = match store.get_cbor::<S>(cid)? {
Some(root) => root,
None => return Err(anyhow::anyhow!("failed to get leaf for cid {}", cid)),
};
Ok(leaf)
store.get_cbor::<S>(cid)
}

/// The state represents an MMR with peaks stored in an AMT
Expand Down Expand Up @@ -307,7 +303,7 @@ impl State {
&self,
store: &BS,
index: u64,
) -> anyhow::Result<S> {
) -> anyhow::Result<Option<S>> {
let amt = Amt::<Cid, &BS>::load(&self.peaks, store)?;
get_at::<BS, S>(store, index, self.leaf_count, &amt)
}
Expand Down Expand Up @@ -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<i32>>(&store, 0u64).unwrap();
let item0 = state.get_obj::<_, Vec<i32>>(&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<i32>>(&store, 0u64).unwrap();
let item1 = state.get_obj::<_, Vec<i32>>(&store, 1u64).unwrap();
let item0 = state.get_obj::<_, Vec<i32>>(&store, 0u64).unwrap().unwrap();
let item1 = state.get_obj::<_, Vec<i32>>(&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<i32>>(&store, 0u64).unwrap();
let item1 = state.get_obj::<_, Vec<i32>>(&store, 1u64).unwrap();
let item2 = state.get_obj::<_, Vec<i32>>(&store, 2u64).unwrap();
let item0 = state.get_obj::<_, Vec<i32>>(&store, 0u64).unwrap().unwrap();
let item1 = state.get_obj::<_, Vec<i32>>(&store, 1u64).unwrap().unwrap();
let item2 = state.get_obj::<_, Vec<i32>>(&store, 2u64).unwrap().unwrap();
assert_eq!(item0, vec![0]);
assert_eq!(item1, vec![1]);
assert_eq!(item2, vec![2]);
Expand All @@ -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<u64>>(&store, j).unwrap();
let item = state.get_obj::<_, Vec<u64>>(&store, j).unwrap().unwrap();
assert_eq!(item, vec![j]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions fendermint/rpc/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ pub fn decode_acc_push_return(deliver_tx: &DeliverTx) -> anyhow::Result<PushRetu
.map_err(|e| anyhow!("error parsing as PushReturn: {e}"))
}

pub fn decode_acc_get_at(deliver_tx: &DeliverTx) -> anyhow::Result<Vec<u8>> {
pub fn decode_acc_get_at(deliver_tx: &DeliverTx) -> anyhow::Result<Option<Vec<u8>>> {
let data = decode_data(&deliver_tx.data)?;
fvm_ipld_encoding::from_slice(data.as_slice())
.map_err(|e| anyhow!("error parsing as Vec<u8>: {e}"))
fvm_ipld_encoding::from_slice::<Option<Vec<u8>>>(&data)
.map_err(|e| anyhow!("error parsing as Option<Vec<u8>>: {e}"))
}

/// Parse what Tendermint returns in the `data` field of [`DeliverTx`] as [`CreateReturn`].
Expand Down
4 changes: 2 additions & 2 deletions fendermint/rpc/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0e62386

Please sign in to comment.