Skip to content

Commit 748ef08

Browse files
start patching through errors in ipld
1 parent 54b4283 commit 748ef08

File tree

9 files changed

+61
-76
lines changed

9 files changed

+61
-76
lines changed

ipld/blockstore/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ repository = "https://github.com/filecoin-project/ref-fvm"
99

1010
[dependencies]
1111
cid = { version = "0.8.2", default-features = false, features = ["serde-codec", "std"] }
12-
anyhow = "1.0.51"
1312

1413

1514
[dev-dependencies]

ipld/blockstore/src/lib.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::rc::Rc;
22

3-
use anyhow::Result;
43
use cid::{multihash, Cid};
54

65
pub mod tracking;
@@ -15,26 +14,28 @@ pub use block::*;
1514
///
1615
/// The cgo blockstore adapter implements this trait.
1716
pub trait Blockstore {
17+
type Error: std::error::Error + std::fmt::Debug;
18+
1819
/// Gets the block from the blockstore.
19-
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>>;
20+
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error>;
2021

2122
/// Put a block with a pre-computed cid.
2223
///
2324
/// If you don't yet know the CID, use put. Some blockstores will re-compute the CID internally
2425
/// even if you provide it.
2526
///
2627
/// If you _do_ already know the CID, use this method as some blockstores _won't_ recompute it.
27-
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()>;
28+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error>;
2829

2930
/// Checks if the blockstore has the specified block.
30-
fn has(&self, k: &Cid) -> Result<bool> {
31+
fn has(&self, k: &Cid) -> Result<bool, Self::Error> {
3132
Ok(self.get(k)?.is_some())
3233
}
3334

3435
/// Puts the block into the blockstore, computing the hash with the specified multicodec.
3536
///
3637
/// By default, this defers to put.
37-
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid>
38+
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid, Self::Error>
3839
where
3940
Self: Sized,
4041
D: AsRef<[u8]>,
@@ -55,7 +56,7 @@ pub trait Blockstore {
5556
/// let blocks = vec![Block::new(0x55, vec![0, 1, 2])];
5657
/// bs.put_many(blocks.iter().map(|b| (Blake2b256, b.into()))).unwrap();
5758
/// ```
58-
fn put_many<D, I>(&self, blocks: I) -> Result<()>
59+
fn put_many<D, I>(&self, blocks: I) -> Result<(), Self::Error>
5960
where
6061
Self: Sized,
6162
D: AsRef<[u8]>,
@@ -68,7 +69,7 @@ pub trait Blockstore {
6869
/// Bulk-put pre-keyed blocks into the blockstore.
6970
///
7071
/// By default, this defers to put_keyed.
71-
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<()>
72+
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<(), Self::Error>
7273
where
7374
Self: Sized,
7475
D: AsRef<[u8]>,
@@ -82,34 +83,36 @@ pub trait Blockstore {
8283
}
8384

8485
pub trait Buffered: Blockstore {
85-
fn flush(&self, root: &Cid) -> Result<()>;
86+
fn flush(&self, root: &Cid) -> Result<(), Self::Error>;
8687
}
8788

8889
impl<BS> Blockstore for &BS
8990
where
9091
BS: Blockstore,
9192
{
92-
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>> {
93+
type Error = BS::Error;
94+
95+
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error> {
9396
(*self).get(k)
9497
}
9598

96-
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> {
99+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error> {
97100
(*self).put_keyed(k, block)
98101
}
99102

100-
fn has(&self, k: &Cid) -> Result<bool> {
103+
fn has(&self, k: &Cid) -> Result<bool, Self::Error> {
101104
(*self).has(k)
102105
}
103106

104-
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid>
107+
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid, Self::Error>
105108
where
106109
Self: Sized,
107110
D: AsRef<[u8]>,
108111
{
109112
(*self).put(mh_code, block)
110113
}
111114

112-
fn put_many<D, I>(&self, blocks: I) -> Result<()>
115+
fn put_many<D, I>(&self, blocks: I) -> Result<(), Self::Error>
113116
where
114117
Self: Sized,
115118
D: AsRef<[u8]>,
@@ -118,7 +121,7 @@ where
118121
(*self).put_many(blocks)
119122
}
120123

121-
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<()>
124+
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<(), Self::Error>
122125
where
123126
Self: Sized,
124127
D: AsRef<[u8]>,
@@ -132,27 +135,29 @@ impl<BS> Blockstore for Rc<BS>
132135
where
133136
BS: Blockstore,
134137
{
135-
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>> {
138+
type Error = BS::Error;
139+
140+
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error> {
136141
(**self).get(k)
137142
}
138143

139-
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> {
144+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error> {
140145
(**self).put_keyed(k, block)
141146
}
142147

143-
fn has(&self, k: &Cid) -> Result<bool> {
148+
fn has(&self, k: &Cid) -> Result<bool, Self::Error> {
144149
(**self).has(k)
145150
}
146151

147-
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid>
152+
fn put<D>(&self, mh_code: multihash::Code, block: &Block<D>) -> Result<Cid, Self::Error>
148153
where
149154
Self: Sized,
150155
D: AsRef<[u8]>,
151156
{
152157
(**self).put(mh_code, block)
153158
}
154159

155-
fn put_many<D, I>(&self, blocks: I) -> Result<()>
160+
fn put_many<D, I>(&self, blocks: I) -> Result<(), Self::Error>
156161
where
157162
Self: Sized,
158163
D: AsRef<[u8]>,
@@ -161,7 +166,7 @@ where
161166
(**self).put_many(blocks)
162167
}
163168

164-
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<()>
169+
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<(), Self::Error>
165170
where
166171
Self: Sized,
167172
D: AsRef<[u8]>,

ipld/blockstore/src/memory.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::RefCell;
22
use std::collections::HashMap;
3+
use std::convert::Infallible;
34

4-
use anyhow::Result;
55
use cid::Cid;
66

77
use super::Blockstore;
@@ -18,15 +18,17 @@ impl MemoryBlockstore {
1818
}
1919

2020
impl Blockstore for MemoryBlockstore {
21-
fn has(&self, k: &Cid) -> Result<bool> {
21+
type Error = Infallible;
22+
23+
fn has(&self, k: &Cid) -> Result<bool, Self::Error> {
2224
Ok(self.blocks.borrow().contains_key(k))
2325
}
2426

25-
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>> {
27+
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error> {
2628
Ok(self.blocks.borrow().get(k).cloned())
2729
}
2830

29-
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> {
31+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error> {
3032
self.blocks.borrow_mut().insert(*k, block.into());
3133
Ok(())
3234
}

ipld/blockstore/src/tracking.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::cell::RefCell;
44

5-
use anyhow::Result;
65
use cid::multihash::{self, Code};
76
use cid::Cid;
87

@@ -46,7 +45,9 @@ impl<BS> Blockstore for TrackingBlockstore<BS>
4645
where
4746
BS: Blockstore,
4847
{
49-
fn get(&self, cid: &Cid) -> Result<Option<Vec<u8>>> {
48+
type Error = BS::Error;
49+
50+
fn get(&self, cid: &Cid) -> Result<Option<Vec<u8>>, Self::Error> {
5051
let mut stats = self.stats.borrow_mut();
5152
stats.r += 1;
5253
let bytes = self.base.get(cid)?;
@@ -55,12 +56,13 @@ where
5556
}
5657
Ok(bytes)
5758
}
58-
fn has(&self, cid: &Cid) -> Result<bool> {
59+
60+
fn has(&self, cid: &Cid) -> Result<bool, Self::Error> {
5961
self.stats.borrow_mut().r += 1;
6062
self.base.has(cid)
6163
}
6264

63-
fn put<D>(&self, code: Code, block: &Block<D>) -> Result<Cid>
65+
fn put<D>(&self, code: Code, block: &Block<D>) -> Result<Cid, Self::Error>
6466
where
6567
D: AsRef<[u8]>,
6668
{
@@ -70,14 +72,14 @@ where
7072
self.base.put(code, block)
7173
}
7274

73-
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<()> {
75+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> Result<(), Self::Error> {
7476
let mut stats = self.stats.borrow_mut();
7577
stats.w += 1;
7678
stats.bw += block.len();
7779
self.base.put_keyed(k, block)
7880
}
7981

80-
fn put_many<D, I>(&self, blocks: I) -> Result<()>
82+
fn put_many<D, I>(&self, blocks: I) -> Result<(), Self::Error>
8183
where
8284
Self: Sized,
8385
D: AsRef<[u8]>,
@@ -91,7 +93,7 @@ where
9193
Ok(())
9294
}
9395

94-
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<()>
96+
fn put_many_keyed<D, I>(&self, blocks: I) -> Result<(), Self::Error>
9597
where
9698
Self: Sized,
9799
D: AsRef<[u8]>,

ipld/encoding/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ serde_ipld_dagcbor = "0.1.2"
1515
serde_tuple = "0.5"
1616
serde_repr = "0.1"
1717
cid = { version = "0.8.2", default-features = false, features = ["serde-codec", "std"] }
18-
thiserror = "1.0"
19-
anyhow = "1.0.56"
18+
thiserror = "1.0.30"
2019
fvm_ipld_blockstore = { version = "0.1", path = "../blockstore" }
2120

2221
[features]

ipld/encoding/src/cbor_store.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ use serde::{de, ser};
44

55
use crate::DAG_CBOR;
66

7+
#[derive(thiserror::Error, Debug)]
8+
pub enum Error<BS: Blockstore> {
9+
#[error("blockstore: {0}")]
10+
Blockstore(BS::Error),
11+
#[error("encoding: {0}")]
12+
Encoding(#[from] crate::errors::Error),
13+
}
14+
715
/// Wrapper for database to handle inserting and retrieving ipld data with Cids
816
pub trait CborStore: Blockstore + Sized {
917
/// Get typed object from block store by Cid.
10-
fn get_cbor<T>(&self, cid: &Cid) -> anyhow::Result<Option<T>>
18+
fn get_cbor<T>(&self, cid: &Cid) -> Result<Option<T>, Error<Self>>
1119
where
1220
T: de::DeserializeOwned,
1321
{
14-
match self.get(cid)? {
22+
match self.get(cid).map_err(Error::Blockstore)? {
1523
Some(bz) => {
1624
let res = crate::from_slice(&bz)?;
1725
Ok(Some(res))
@@ -21,7 +29,7 @@ pub trait CborStore: Blockstore + Sized {
2129
}
2230

2331
/// Put an object in the block store and return the Cid identifier.
24-
fn put_cbor<S>(&self, obj: &S, code: multihash::Code) -> anyhow::Result<Cid>
32+
fn put_cbor<S>(&self, obj: &S, code: multihash::Code) -> Result<Cid, Error<Self>>
2533
where
2634
S: ser::Serialize,
2735
{
@@ -33,6 +41,7 @@ pub trait CborStore: Blockstore + Sized {
3341
data: &bytes,
3442
},
3543
)
44+
.map_err(Error::Blockstore)
3645
}
3746
}
3847

ipld/encoding/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use serde_bytes;
1313

1414
pub use self::bytes::*;
1515
pub use self::cbor::*;
16-
pub use self::cbor_store::CborStore;
16+
pub use self::cbor_store::{CborStore, Error as CborStoreError};
1717
pub use self::errors::*;
1818
pub use self::vec::*;
1919

ipld/hamt/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ thiserror = "1.0"
1717
sha2 = "0.10"
1818
once_cell = "1.5"
1919
forest_hash_utils = "0.1"
20-
anyhow = "1.0.51"
2120
libipld-core = { version = "0.13.1", features = ["serde-codec"] }
2221
fvm_ipld_encoding = { version = "0.1", path = "../encoding" }
2322
fvm_ipld_blockstore = { version = "0.1", path = "../blockstore" }

ipld/hamt/src/error.rs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
use std::error::Error as StdError;
55

6-
use fvm_ipld_encoding::Error as EncodingError;
6+
use fvm_ipld_blockstore::Blockstore;
7+
use fvm_ipld_encoding::{CborStore, CborStoreError, Error as EncodingError};
78
use thiserror::Error;
89

910
/// HAMT Error
1011
#[derive(Debug, Error)]
11-
pub enum Error {
12+
pub enum Error<BS: Blockstore> {
1213
/// Maximum depth error
1314
#[error("Maximum depth reached")]
1415
MaxDepth,
@@ -21,39 +22,8 @@ pub enum Error {
2122
/// Cid not found in store error
2223
#[error("Cid ({0}) did not match any in database")]
2324
CidNotFound(String),
24-
// TODO: This should be something like "internal" or "io". And we shouldn't have both this and
25-
// "other"; they serve the same purpose.
26-
/// Dynamic error for when the error needs to be forwarded as is.
27-
#[error("{0}")]
28-
Dynamic(anyhow::Error),
29-
}
30-
31-
impl From<String> for Error {
32-
fn from(e: String) -> Self {
33-
Self::Dynamic(anyhow::anyhow!(e))
34-
}
35-
}
36-
37-
impl From<&'static str> for Error {
38-
fn from(e: &'static str) -> Self {
39-
Self::Dynamic(anyhow::anyhow!(e))
40-
}
41-
}
42-
43-
impl From<anyhow::Error> for Error {
44-
fn from(e: anyhow::Error) -> Self {
45-
e.downcast::<Error>().unwrap_or_else(Self::Dynamic)
46-
}
47-
}
48-
49-
impl From<EncodingError> for Error {
50-
fn from(e: EncodingError) -> Self {
51-
Self::Dynamic(anyhow::anyhow!(e))
52-
}
53-
}
54-
55-
impl From<Box<dyn StdError + Send + Sync>> for Error {
56-
fn from(e: Box<dyn StdError + Send + Sync>) -> Self {
57-
Self::Dynamic(anyhow::anyhow!(e))
58-
}
25+
#[error("blockstore {0}")]
26+
Blockstore(BS::Error),
27+
#[error("encoding error {0}")]
28+
Encoding(#[from] EncodingError),
5929
}

0 commit comments

Comments
 (0)