Skip to content

Commit e2c2ff7

Browse files
committed
add dump_cache() to write out intermediate blocks to a blockstore
1 parent 7728aae commit e2c2ff7

File tree

10 files changed

+55
-1
lines changed

10 files changed

+55
-1
lines changed

fvm/src/blockstore/buffered.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ where
3434
pub fn into_inner(self) -> BS {
3535
self.base
3636
}
37+
38+
/// Dumps all cached state blocks to the provided blockstore. Blocks in the cache are not
39+
/// guaranteed to be connected to the final state tree and may be intermediate state blocks
40+
/// that are not reachable from the eventual state root.
41+
pub fn dump_cache<S: Blockstore>(&self, bs: S) -> Result<()> {
42+
log::info!(
43+
"Dumping {} cache blocks to blockstore",
44+
self.write.borrow().len()
45+
);
46+
bs.put_many_keyed(self.write.borrow().iter().map(|(k, v)| (*k, v.as_slice())))
47+
}
3748
}
3849

3950
impl<BS> Buffered for BufferedBlockstore<BS>

fvm/src/executor/default.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::result::Result as StdResult;
55

66
use anyhow::{anyhow, Result};
77
use cid::Cid;
8+
use fvm_ipld_blockstore::Blockstore;
89
use fvm_ipld_encoding::{RawBytes, CBOR};
910
use fvm_shared::address::Payload;
1011
use fvm_shared::econ::TokenAmount;
@@ -289,6 +290,12 @@ where
289290
let k = (**self).flush()?;
290291
Ok(k)
291292
}
293+
294+
/// Dumps all cached state blocks (intermediate and final) to the provided blockstore.
295+
fn dump_cache<B: Blockstore>(&mut self, bs: B) -> anyhow::Result<()> {
296+
(**self).dump_cache(bs)?;
297+
Ok(())
298+
}
292299
}
293300

294301
impl<K> DefaultExecutor<K>

fvm/src/executor/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::Display;
77

88
use cid::Cid;
99
pub use default::DefaultExecutor;
10+
use fvm_ipld_blockstore::Blockstore;
1011
use fvm_ipld_encoding::RawBytes;
1112
use fvm_shared::econ::TokenAmount;
1213
use fvm_shared::error::ExitCode;
@@ -44,6 +45,9 @@ pub trait Executor {
4445

4546
/// Flushes the state-tree, returning the new root CID.
4647
fn flush(&mut self) -> anyhow::Result<Cid>;
48+
49+
/// Dumps all cached state blocks (intermediate and final) to the provided blockstore.
50+
fn dump_cache<S: Blockstore>(&mut self, bs: S) -> anyhow::Result<()>;
4751
}
4852

4953
/// A description of some failure encountered when applying a message.

fvm/src/executor/threaded.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33
use anyhow::anyhow;
44
use cid::Cid;
5+
use fvm_ipld_blockstore::Blockstore;
56
use fvm_shared::message::Message;
67
use lazy_static::lazy_static;
78

@@ -50,4 +51,9 @@ where
5051
fn flush(&mut self) -> anyhow::Result<Cid> {
5152
self.0.flush()
5253
}
54+
55+
/// Dumps all cached state blocks (intermediate and final) to the provided blockstore.
56+
fn dump_cache<S: Blockstore>(&mut self, bs: S) -> anyhow::Result<()> {
57+
self.0.dump_cache(bs)
58+
}
5359
}

fvm/src/kernel/default.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ where
485485
.or_fatal()?;
486486
self.blocks.mark_reachable(&k);
487487

488+
// TODO: remove this, it's just for debugging.
489+
self.call_manager.log(format!("block_link({})", k));
490+
488491
t.stop_with(start);
489492
Ok(k)
490493
}

fvm/src/machine/boxed.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cid::Cid;
55
use super::{Machine, MachineContext, Manifest};
66
use crate::kernel::Result;
77
use crate::state_tree::StateTree;
8+
use fvm_ipld_blockstore::Blockstore;
89

910
type Type = MachineContext;
1011

@@ -48,6 +49,11 @@ impl<M: Machine> Machine for Box<M> {
4849
(**self).flush()
4950
}
5051

52+
#[inline(always)]
53+
fn dump_cache<B: Blockstore>(&mut self, bs: B) -> Result<()> {
54+
(**self).dump_cache(bs)
55+
}
56+
5157
#[inline(always)]
5258
fn into_store(self) -> Self::Blockstore {
5359
(*self).into_store()

fvm/src/machine/default.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ where
163163
Ok(root)
164164
}
165165

166+
/// Dumps all cached state blocks (intermediate and final) to the provided blockstore.
167+
fn dump_cache<S: Blockstore>(&mut self, bs: S) -> Result<()> {
168+
self.blockstore().dump_cache(bs).or_fatal()
169+
}
170+
166171
fn into_store(self) -> Self::Blockstore {
167172
self.state_tree.into_store()
168173
}

fvm/src/machine/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ pub trait Machine: 'static {
7373
self.state_tree_mut().flush()
7474
}
7575

76+
/// Dumps all cached state blocks (intermediate and final) to the provided blockstore.
77+
fn dump_cache<S: Blockstore>(&mut self, bs: S) -> Result<()>;
78+
7679
/// Consumes the machine and returns the owned blockstore.
7780
fn into_store(self) -> Self::Blockstore;
7881

fvm/tests/dummy.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use fvm::call_manager::{Backtrace, CallManager, Entrypoint, FinishRet, Invocatio
1010
use fvm::engine::Engine;
1111
use fvm::externs::{Chain, Consensus, Externs, Rand};
1212
use fvm::gas::{Gas, GasCharge, GasTimer, GasTracker};
13+
use fvm::kernel::ExecutionError;
1314
use fvm::machine::limiter::MemoryLimiter;
1415
use fvm::machine::{Machine, MachineContext, Manifest, NetworkConfig};
1516
use fvm::state_tree::StateTree;
@@ -181,6 +182,10 @@ impl Machine for DummyMachine {
181182
fn new_limiter(&self) -> Self::Limiter {
182183
DummyLimiter::default()
183184
}
185+
186+
fn dump_cache<S: Blockstore>(&mut self, _bs: S) -> anyhow::Result<(), ExecutionError> {
187+
Ok(())
188+
}
184189
}
185190

186191
/// Minimal *pseudo-functional* implementation CallManager

testing/conformance/src/vm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use fvm::machine::limiter::MemoryLimiter;
1010
use fvm::machine::{DefaultMachine, Machine, MachineContext, Manifest, NetworkConfig};
1111
use fvm::state_tree::StateTree;
1212
use fvm::syscalls::Linker;
13-
use fvm_ipld_blockstore::MemoryBlockstore;
13+
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
1414
use fvm_shared::consensus::ConsensusFault;
1515
use fvm_shared::piece::PieceInfo;
1616
use fvm_shared::sector::{
@@ -137,6 +137,10 @@ where
137137
self.machine.flush()
138138
}
139139

140+
fn dump_cache<S: Blockstore>(&mut self, bs: S) -> Result<()> {
141+
self.machine.dump_cache(bs)
142+
}
143+
140144
fn machine_id(&self) -> &str {
141145
self.machine.machine_id()
142146
}

0 commit comments

Comments
 (0)