forest-filecoin 0.34.1

Rust Filecoin implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::{
    BLOCK_BLOOM_LEN, EthBlockBloomStore, EthMappingsStore, SettingsStore, SettingsStoreExt,
    decode_block_bloom, encode_block_bloom,
};
use crate::blocks::TipsetKey;
use crate::db::PersistentStore;
use crate::libp2p_bitswap::{BitswapStoreRead, BitswapStoreReadWrite};
use crate::prelude::*;
use crate::rpc::eth::types::EthHash;
use crate::shim::clock::ChainEpoch;
use crate::utils::db::car_stream::CarBlock;
use crate::utils::multihash::prelude::*;
use ahash::HashMap;
use indexmap::IndexMap;
use nunny::Vec as NonEmpty;
use parking_lot::RwLock;

#[derive(Debug, Default)]
pub struct MemoryDB {
    blockchain_db: RwLock<HashMap<Cid, Vec<u8>>>,
    blockchain_persistent_db: RwLock<HashMap<Cid, Vec<u8>>>,
    settings_db: RwLock<HashMap<String, Vec<u8>>>,
    pub eth_mappings_db: RwLock<HashMap<EthHash, Vec<u8>>>,
    pub ts_lookup_db: RwLock<HashMap<ChainEpoch, TipsetKey>>,
    pub eth_block_bloom_db: RwLock<HashMap<Cid, Vec<u8>>>,
}

impl MemoryDB {
    pub fn blockstore_len(&self) -> usize {
        self.blockchain_db.read().len() + self.blockchain_persistent_db.read().len()
    }

    pub fn blockstore_size_bytes(&self) -> usize {
        self.blockchain_db
            .read()
            .iter()
            .chain(self.blockchain_persistent_db.read().iter())
            .map(|(k, v)| k.to_bytes().len() + v.len())
            .sum()
    }

    pub async fn export_forest_car<W: tokio::io::AsyncWrite + Unpin>(
        &self,
        writer: &mut W,
    ) -> anyhow::Result<()> {
        let roots =
            SettingsStoreExt::read_obj::<TipsetKey>(self, crate::db::setting_keys::HEAD_KEY)?
                .context("chain head is not tracked and cannot be exported")?
                .into_cids();
        self.export_forest_car_with_roots(roots, writer).await
    }

    pub async fn export_forest_car_with_roots<W: tokio::io::AsyncWrite + Unpin>(
        &self,
        roots: NonEmpty<Cid>,
        writer: &mut W,
    ) -> anyhow::Result<()> {
        let blocks = {
            let blockchain_db = self.blockchain_db.read();
            let blockchain_persistent_db = self.blockchain_persistent_db.read();
            blockchain_db
                .iter()
                .chain(blockchain_persistent_db.iter())
                // Sort to make the result CAR deterministic
                .sorted_by_key(|&(&cid, _)| cid)
                .map(|(&cid, data)| {
                    anyhow::Ok(CarBlock {
                        cid,
                        data: data.clone().into(),
                    })
                })
                .collect_vec()
        };
        let frames =
            crate::db::car::forest::Encoder::compress_stream_default(futures::stream::iter(blocks));
        crate::db::car::forest::Encoder::write(writer, roots, frames).await
    }
}

impl SettingsStore for MemoryDB {
    fn read_bin(&self, key: &str) -> anyhow::Result<Option<Vec<u8>>> {
        Ok(self.settings_db.read().get(key).cloned())
    }

    fn write_bin(&self, key: &str, value: &[u8]) -> anyhow::Result<()> {
        self.settings_db
            .write()
            .insert(key.to_owned(), value.to_vec());
        Ok(())
    }

    fn exists(&self, key: &str) -> anyhow::Result<bool> {
        Ok(self.settings_db.read().contains_key(key))
    }

    fn setting_keys(&self) -> anyhow::Result<Vec<String>> {
        Ok(self.settings_db.read().keys().cloned().collect_vec())
    }
}

impl EthMappingsStore for MemoryDB {
    fn read_bin(&self, key: &EthHash) -> anyhow::Result<Option<Vec<u8>>> {
        Ok(self.eth_mappings_db.read().get(key).cloned())
    }

    fn write_bin(&self, key: &EthHash, value: &[u8]) -> anyhow::Result<()> {
        self.eth_mappings_db
            .write()
            .insert(key.to_owned(), value.to_vec());
        Ok(())
    }

    fn exists(&self, key: &EthHash) -> anyhow::Result<bool> {
        Ok(self.eth_mappings_db.read().contains_key(key))
    }

    fn get_message_cids(&self) -> anyhow::Result<Vec<(Cid, u64)>> {
        let cids = self
            .eth_mappings_db
            .read()
            .values()
            .filter_map(|value| fvm_ipld_encoding::from_slice::<(Cid, u64)>(value).ok())
            .collect();

        Ok(cids)
    }

    fn delete(&self, keys: Vec<EthHash>) -> anyhow::Result<()> {
        let mut lock = self.eth_mappings_db.write();
        for hash in keys.iter() {
            lock.remove(hash);
        }
        Ok(())
    }

    fn tipset_key_by_epoch(&self, epoch: ChainEpoch) -> anyhow::Result<Option<TipsetKey>> {
        Ok(self.ts_lookup_db.read().get(&epoch).cloned())
    }

    fn delete_tipset_key_at_epoch(&self, epoch: ChainEpoch) -> anyhow::Result<()> {
        self.ts_lookup_db.write().remove(&epoch);
        Ok(())
    }

    fn set_tipset_key_at_epoch_raw(
        &self,
        epoch: ChainEpoch,
        tsk: &TipsetKey,
    ) -> anyhow::Result<()> {
        self.ts_lookup_db.write().insert(epoch, tsk.clone());
        Ok(())
    }
}

impl EthBlockBloomStore for MemoryDB {
    fn read_bloom(&self, key: &Cid) -> anyhow::Result<Option<[u8; BLOCK_BLOOM_LEN]>> {
        Ok(self
            .eth_block_bloom_db
            .read()
            .get(key)
            .and_then(|entry| decode_block_bloom(entry).map(|(_, bloom)| *bloom)))
    }

    fn write_bloom(
        &self,
        key: &Cid,
        height: ChainEpoch,
        bloom: &[u8; BLOCK_BLOOM_LEN],
    ) -> anyhow::Result<()> {
        self.eth_block_bloom_db
            .write()
            .insert(*key, encode_block_bloom(height, bloom));
        Ok(())
    }

    fn delete_blooms_before_height(&self, height: ChainEpoch) -> anyhow::Result<()> {
        self.eth_block_bloom_db
            .write()
            .retain(|_, entry| decode_block_bloom(entry).is_some_and(|(h, _)| h >= height));
        Ok(())
    }
}

impl Blockstore for MemoryDB {
    fn get(&self, k: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
        Ok(self.blockchain_db.read().get(k).cloned().or(self
            .blockchain_persistent_db
            .read()
            .get(k)
            .cloned()))
    }

    fn put_keyed(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
        self.blockchain_db.write().insert(*k, block.to_vec());
        Ok(())
    }
}

impl PersistentStore for MemoryDB {
    fn put_keyed_persistent(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
        self.blockchain_persistent_db
            .write()
            .insert(*k, block.to_vec());
        Ok(())
    }
}

impl BitswapStoreRead for MemoryDB {
    fn contains(&self, cid: &Cid) -> anyhow::Result<bool> {
        Ok(self.blockchain_db.read().contains_key(cid))
    }

    fn get(&self, cid: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
        Blockstore::get(self, cid)
    }
}

impl BitswapStoreReadWrite for MemoryDB {
    type Hashes = MultihashCode;

    fn insert(&self, block: &crate::libp2p_bitswap::Block64<Self::Hashes>) -> anyhow::Result<()> {
        self.put_keyed(block.cid(), block.data())
    }
}

impl super::HeaviestTipsetKeyProvider for MemoryDB {
    fn heaviest_tipset_key(&self) -> anyhow::Result<Option<TipsetKey>> {
        SettingsStoreExt::read_obj::<TipsetKey>(self, crate::db::setting_keys::HEAD_KEY)
    }

    fn set_heaviest_tipset_key(&self, tsk: &TipsetKey) -> anyhow::Result<()> {
        SettingsStoreExt::write_obj(self, crate::db::setting_keys::HEAD_KEY, tsk)
    }
}

#[derive(Debug, Default, derive_more::Deref)]
/// A memory blockstore that preserves the insertion order
pub struct IndexMapBlockstore {
    inner: RwLock<IndexMap<Cid, Vec<u8>>>,
}

impl IndexMapBlockstore {
    #[allow(dead_code)]
    pub async fn export_forest_car<W: tokio::io::AsyncWrite + Unpin>(
        &self,
        roots: NonEmpty<Cid>,
        writer: &mut W,
    ) -> anyhow::Result<()> {
        let blocks = {
            let inner = self.inner.read();
            let invalid_roots = roots
                .iter()
                .filter(|&c| !inner.contains_key(c))
                .collect_vec();
            anyhow::ensure!(
                invalid_roots.is_empty(),
                "All roots should present in the blockstore, invalid roots: {invalid_roots:?}"
            );
            inner
                .iter()
                .map(|(&cid, data)| {
                    anyhow::Ok(CarBlock {
                        cid,
                        data: data.clone().into(),
                    })
                })
                .collect_vec()
        };
        let frames =
            crate::db::car::forest::Encoder::compress_stream_default(futures::stream::iter(blocks));
        crate::db::car::forest::Encoder::write(writer, roots, frames).await
    }
}

impl Blockstore for IndexMapBlockstore {
    fn get(&self, k: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
        Ok(self.read().get(k).cloned())
    }

    fn put_keyed(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
        self.write().insert(*k, block.to_vec());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        db::{car::ForestCar, setting_keys::HEAD_KEY},
        utils::cid::CidCborExt as _,
    };
    use fil_actors_shared::fvm_ipld_hamt::Hamt;
    use fvm_ipld_encoding::DAG_CBOR;
    use multihash_codetable::Code::Blake2b256;
    use nunny::vec as nonempty;

    #[tokio::test]
    async fn test_export_forest_car() {
        let db = MemoryDB::default();
        let record1 = b"non-persistent";
        let key1 = Cid::new_v1(DAG_CBOR, Blake2b256.digest(record1.as_slice()));
        db.put_keyed(&key1, record1.as_slice()).unwrap();

        let record2 = b"persistent";
        let key2 = Cid::new_v1(DAG_CBOR, Blake2b256.digest(record2.as_slice()));
        db.put_keyed_persistent(&key2, record2.as_slice()).unwrap();

        let mut car_db_bytes = vec![];
        assert!(
            db.export_forest_car(&mut car_db_bytes)
                .await
                .unwrap_err()
                .to_string()
                .contains("chain head is not tracked and cannot be exported")
        );

        db.write_obj(HEAD_KEY, &TipsetKey::from(nonempty![key1]))
            .unwrap();

        car_db_bytes.clear();
        db.export_forest_car(&mut car_db_bytes).await.unwrap();

        let car = ForestCar::new(car_db_bytes).unwrap();
        assert_eq!(car.head_tipset_key(), &nonempty![key1]);
        assert!(car.has(&key1).unwrap());
        assert!(car.has(&key2).unwrap());
    }

    #[test]
    fn block_bloom_encode_decode() {
        let bloom = [0xab; 256];
        let entry = encode_block_bloom(42, &bloom);
        let (height, decoded) = decode_block_bloom(&entry).unwrap();
        assert_eq!(height, 42);
        assert_eq!(decoded, &bloom);
        assert!(decode_block_bloom(&[0, 1, 2]).is_none());
    }

    #[tokio::test]
    async fn test_index_map_blockstore() {
        const BIT_WIDTH: u32 = 5;

        let db = IndexMapBlockstore::default();
        // similate tipset lookup table
        let mut hamt: Hamt<_, TipsetKey, ChainEpoch> = Hamt::new_with_bit_width(&db, BIT_WIDTH);
        let checkpoints = [
            (
                0,
                TipsetKey::from(nunny::vec![Cid::from_cbor_blake2b256(&"1").unwrap()]),
            ),
            (
                5,
                TipsetKey::from(nunny::vec![Cid::from_cbor_blake2b256(&"5").unwrap()]),
            ),
            (
                10,
                TipsetKey::from(nunny::vec![Cid::from_cbor_blake2b256(&"10").unwrap()]),
            ),
        ];
        for (epoch, tsk) in checkpoints.iter().cloned() {
            hamt.set(epoch, tsk).unwrap();
        }
        let hamt_root = hamt.flush().unwrap();
        assert!(db.has(&hamt_root).unwrap(), "hamt root should present");

        // export with invalid roots should fail
        let mut car = vec![];
        db.export_forest_car(
            nunny::vec![Cid::from_cbor_blake2b256(&"invalid").unwrap()],
            &mut car,
        )
        .await
        .unwrap_err();

        // export with hamt root should succeed
        let mut car_bytes = vec![];
        let car_roots = nunny::vec![hamt_root];
        db.export_forest_car(car_roots.clone(), &mut car_bytes)
            .await
            .unwrap();

        let car: ForestCar<Vec<u8>> = ForestCar::new(car_bytes).unwrap();
        let hamt_from_car: Hamt<_, TipsetKey, ChainEpoch> =
            Hamt::load_with_bit_width(&hamt_root, &car, BIT_WIDTH).unwrap();

        let checkpoints_from_memdb_hamt = {
            let mut v = vec![];
            hamt.for_each_cacheless(|epoch, tsk| {
                v.push((*epoch, tsk.clone()));
                anyhow::Ok(())
            })
            .unwrap();
            v
        };
        let checkpoints_from_car_hamt = {
            let mut v = vec![];
            hamt_from_car
                .for_each_cacheless(|epoch, tsk| {
                    v.push((*epoch, tsk.clone()));
                    anyhow::Ok(())
                })
                .unwrap();
            v
        };
        // Cannot compare the 2 hamts as they use different DB types
        assert_eq!(checkpoints_from_memdb_hamt, checkpoints_from_car_hamt);
        // The hamt iteration order is different from the insertion order
        assert_eq!(
            HashMap::from_iter(checkpoints),
            HashMap::from_iter(checkpoints_from_car_hamt)
        );
    }
}