Skip to content

Commit c038a4a

Browse files
committed
feat: add flush_all_blocks option to also write intermediate blocks
1 parent e2c2ff7 commit c038a4a

File tree

10 files changed

+22
-51
lines changed

10 files changed

+22
-51
lines changed

fvm/src/blockstore/buffered.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ where
3535
self.base
3636
}
3737

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<()> {
38+
/// Flushes all blocks from the write cache to the provided blockstore,
39+
/// regardless of whether they're reachable from any state root.
40+
/// Unlike the standard flush() operation which only writes blocks connected
41+
/// to the final state tree, this writes every block created during execution.
42+
pub fn flush_all(&self) -> Result<()> {
4243
log::info!(
43-
"Dumping {} cache blocks to blockstore",
44+
"Flushing all ({}) cache blocks to blockstore",
4445
self.write.borrow().len()
4546
);
46-
bs.put_many_keyed(self.write.borrow().iter().map(|(k, v)| (*k, v.as_slice())))
47+
self.base
48+
.put_many_keyed(self.write.borrow().iter().map(|(k, v)| (*k, v.as_slice())))
4749
}
4850
}
4951

fvm/src/executor/default.rs

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

66
use anyhow::{anyhow, Result};
77
use cid::Cid;
8-
use fvm_ipld_blockstore::Blockstore;
98
use fvm_ipld_encoding::{RawBytes, CBOR};
109
use fvm_shared::address::Payload;
1110
use fvm_shared::econ::TokenAmount;
@@ -290,12 +289,6 @@ where
290289
let k = (**self).flush()?;
291290
Ok(k)
292291
}
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-
}
299292
}
300293

301294
impl<K> DefaultExecutor<K>

fvm/src/executor/mod.rs

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

88
use cid::Cid;
99
pub use default::DefaultExecutor;
10-
use fvm_ipld_blockstore::Blockstore;
1110
use fvm_ipld_encoding::RawBytes;
1211
use fvm_shared::econ::TokenAmount;
1312
use fvm_shared::error::ExitCode;
@@ -45,9 +44,6 @@ pub trait Executor {
4544

4645
/// Flushes the state-tree, returning the new root CID.
4746
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<()>;
5147
}
5248

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

fvm/src/executor/threaded.rs

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

@@ -51,9 +50,4 @@ where
5150
fn flush(&mut self) -> anyhow::Result<Cid> {
5251
self.0.flush()
5352
}
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-
}
5953
}

fvm/src/kernel/default.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,6 @@ 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-
491488
t.stop_with(start);
492489
Ok(k)
493490
}

fvm/src/machine/boxed.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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;
98

109
type Type = MachineContext;
1110

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

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

fvm/src/machine/default.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,19 @@ where
157157
/// This method also flushes all new blocks (reachable from this new root CID) from the write
158158
/// buffer into the underlying blockstore (the blockstore with which the machine was
159159
/// constructed).
160+
///
161+
/// If an intermediate blockstore was provided, all intermediate blocks created during message
162+
/// execution are flushed to it.
160163
fn flush(&mut self) -> Result<Cid> {
161164
let root = self.state_tree_mut().flush()?;
162-
self.blockstore().flush(&root).or_fatal()?;
165+
if self.context.flush_all_blocks {
166+
self.blockstore().flush_all().or_fatal()?;
167+
} else {
168+
self.blockstore().flush(&root).or_fatal()?;
169+
}
163170
Ok(root)
164171
}
165172

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-
171173
fn into_store(self) -> Self::Blockstore {
172174
self.state_tree.into_store()
173175
}

fvm/src/machine/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ 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-
7976
/// Consumes the machine and returns the owned blockstore.
8077
fn into_store(self) -> Self::Blockstore;
8178

@@ -197,6 +194,7 @@ impl NetworkConfig {
197194
initial_state_root: initial_state,
198195
circ_supply: fvm_shared::TOTAL_FILECOIN.clone(),
199196
tracing: false,
197+
flush_all_blocks: false,
200198
}
201199
}
202200

@@ -243,6 +241,10 @@ pub struct MachineContext {
243241
/// Whether or not to produce execution traces in the returned result.
244242
/// Not consensus-critical, but has a performance impact.
245243
pub tracing: bool,
244+
245+
/// When true, flush() will write all blocks created during execution to the
246+
/// blockstore, not just those reachable from the final state root.
247+
pub flush_all_blocks: bool,
246248
}
247249

248250
impl MachineContext {

fvm/tests/dummy.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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;
1413
use fvm::machine::limiter::MemoryLimiter;
1514
use fvm::machine::{Machine, MachineContext, Manifest, NetworkConfig};
1615
use fvm::state_tree::StateTree;
@@ -182,10 +181,6 @@ impl Machine for DummyMachine {
182181
fn new_limiter(&self) -> Self::Limiter {
183182
DummyLimiter::default()
184183
}
185-
186-
fn dump_cache<S: Blockstore>(&mut self, _bs: S) -> anyhow::Result<(), ExecutionError> {
187-
Ok(())
188-
}
189184
}
190185

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

testing/conformance/src/vm.rs

Lines changed: 1 addition & 5 deletions
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::{Blockstore, MemoryBlockstore};
13+
use fvm_ipld_blockstore::MemoryBlockstore;
1414
use fvm_shared::consensus::ConsensusFault;
1515
use fvm_shared::piece::PieceInfo;
1616
use fvm_shared::sector::{
@@ -137,10 +137,6 @@ 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-
144140
fn machine_id(&self) -> &str {
145141
self.machine.machine_id()
146142
}

0 commit comments

Comments
 (0)