Skip to content

Commit 13badcb

Browse files
committed
feat(trace): introduce new Ipld ExecutionEvent
1 parent cdc261f commit 13badcb

File tree

8 files changed

+70
-36
lines changed

8 files changed

+70
-36
lines changed

fvm/src/call_manager/default.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::eam_actor::EAM_ACTOR_ID;
2424
use crate::engine::Engine;
2525
use crate::gas::{Gas, GasTracker};
2626
use crate::kernel::{
27-
Block, BlockRegistry, ClassifyResult, ExecutionError, Kernel, LogEntry, Result, SyscallError,
27+
Block, BlockRegistry, ClassifyResult, ExecutionError, Kernel, Result, SyscallError,
2828
};
2929
use crate::machine::limiter::MemoryLimiter;
3030
use crate::machine::Machine;
@@ -514,11 +514,12 @@ where
514514
Ok(())
515515
}
516516

517-
fn log(&mut self, entry: LogEntry) {
518-
self.trace(ExecutionEvent::Log(match entry {
519-
LogEntry::Syscall(msg) => format!("SyscallLog({})", msg),
520-
LogEntry::BlockLink(cid) => format!("BlockLink({})", cid),
521-
}));
517+
fn log(&mut self, msg: String) {
518+
self.trace(ExecutionEvent::Log(msg))
519+
}
520+
521+
fn trace_ipld(&mut self, op: crate::trace::IpldOperation, cid: Cid, size: usize) {
522+
self.trace(ExecutionEvent::Ipld { op, cid, size })
522523
}
523524
}
524525

fvm/src/call_manager/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use fvm_shared::{ActorID, MethodNum, METHOD_CONSTRUCTOR};
1010

1111
use crate::engine::Engine;
1212
use crate::gas::{Gas, GasCharge, GasTimer, GasTracker, PriceList};
13-
use crate::kernel::{self, BlockRegistry, ClassifyResult, Context, LogEntry, Result};
13+
use crate::kernel::{self, BlockRegistry, ClassifyResult, Context, Result};
1414
use crate::machine::{Machine, MachineContext};
1515
use crate::state_tree::ActorState;
1616
use crate::Kernel;
@@ -24,7 +24,7 @@ mod default;
2424
pub use default::DefaultCallManager;
2525
use fvm_shared::event::StampedEvent;
2626

27-
use crate::trace::ExecutionTrace;
27+
use crate::trace::{ExecutionTrace, IpldOperation};
2828

2929
/// BlockID representing nil parameters or return data.
3030
pub const NO_DATA_BLOCK_ID: u32 = 0;
@@ -87,6 +87,7 @@ pub trait CallManager: 'static {
8787

8888
/// Returns a reference to the machine.
8989
fn machine(&self) -> &Self::Machine;
90+
9091
/// Returns a mutable reference to the machine.
9192
fn machine_mut(&mut self) -> &mut Self::Machine;
9293

@@ -176,7 +177,9 @@ pub trait CallManager: 'static {
176177
fn append_event(&mut self, evt: StampedEvent);
177178

178179
/// log
179-
fn log(&mut self, entry: LogEntry);
180+
fn log(&mut self, msg: String);
181+
182+
fn trace_ipld(&mut self, op: IpldOperation, cid: Cid, size: usize);
180183
}
181184

182185
/// The result of calling actor's entrypoint

fvm/src/kernel/default.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::gas::GasTimer;
2727
use crate::init_actor::INIT_ACTOR_ID;
2828
use crate::machine::{MachineContext, NetworkConfig, BURNT_FUNDS_ACTOR_ID};
2929
use crate::state_tree::ActorState;
30+
use crate::trace::IpldOperation;
3031
use crate::{ipld, syscall_error};
3132

3233
const BLAKE2B_256: u64 = 0xb220;
@@ -409,6 +410,9 @@ where
409410

410411
t.stop();
411412

413+
self.call_manager
414+
.trace_ipld(IpldOperation::Get, *cid, data.len());
415+
412416
// This can fail because we can run out of gas.
413417
let children = ipld::scan_for_reachable_links(
414418
cid.codec(),
@@ -483,11 +487,13 @@ where
483487
// TODO: This is really "super fatal". It means we failed to store state, and should
484488
// probably abort the entire block.
485489
.or_fatal()?;
490+
let size = block.data().len();
486491
self.blocks.mark_reachable(&k);
487492

488493
t.stop_with(start);
489494

490-
self.call_manager.log(LogEntry::BlockLink(k));
495+
self.call_manager.trace_ipld(IpldOperation::Put, k, size);
496+
491497
Ok(k)
492498
}
493499

@@ -882,6 +888,7 @@ where
882888

883889
fn install_actor(&mut self, code_id: Cid) -> Result<()> {
884890
let start = GasTimer::start();
891+
885892
let size = self
886893
.call_manager
887894
.engine()
@@ -894,6 +901,9 @@ where
894901
.charge_gas(self.call_manager.price_list().on_install_actor(size))?;
895902
t.stop_with(start);
896903

904+
self.call_manager
905+
.trace_ipld(IpldOperation::Get, code_id, size);
906+
897907
Ok(())
898908
}
899909

@@ -953,8 +963,8 @@ impl<C> DebugOps for DefaultKernel<C>
953963
where
954964
C: CallManager,
955965
{
956-
fn log(&mut self, entry: LogEntry) {
957-
self.call_manager.log(entry)
966+
fn log(&mut self, msg: String) {
967+
self.call_manager.log(msg)
958968
}
959969

960970
fn debug_enabled(&self) -> bool {

fvm/src/kernel/mod.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,11 @@ pub trait RandomnessOps {
281281
-> Result<[u8; RANDOMNESS_LENGTH]>;
282282
}
283283

284-
#[derive(PartialEq)]
285-
pub enum LogEntry {
286-
/// A log message originating from a log() syscall.
287-
Syscall(String),
288-
/// A new block was created.
289-
BlockLink(Cid),
290-
}
291-
292284
/// Debugging APIs.
293285
#[delegatable_trait]
294286
pub trait DebugOps {
295287
/// Log a message.
296-
fn log(&mut self, entry: LogEntry);
288+
fn log(&mut self, msg: String);
297289

298290
/// Returns whether debug mode is enabled.
299291
fn debug_enabled(&self) -> bool;
@@ -328,9 +320,7 @@ pub mod prelude {
328320
ActorOps, CryptoOps, DebugOps, EventOps, IpldBlockOps, MessageOps, NetworkOps,
329321
RandomnessOps, SelfOps, SendOps, UpgradeOps,
330322
};
331-
pub use super::{
332-
Block, BlockId, BlockRegistry, BlockStat, CallResult, Kernel, LogEntry, SyscallHandler,
333-
};
323+
pub use super::{Block, BlockId, BlockRegistry, BlockStat, CallResult, Kernel, SyscallHandler};
334324
pub use crate::gas::{Gas, GasTimer, PriceList};
335325
pub use ambassador::Delegate;
336326
pub use fvm_shared::address::Address;

fvm/src/syscalls/debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2021-2023 Protocol Labs
22
// SPDX-License-Identifier: Apache-2.0, MIT
3-
use crate::kernel::{ClassifyResult, DebugOps, LogEntry, Result};
3+
use crate::kernel::{ClassifyResult, DebugOps, Result};
44
use crate::syscalls::context::Context;
55

66
pub fn log(context: Context<'_, impl DebugOps>, msg_off: u32, msg_len: u32) -> Result<()> {
@@ -11,7 +11,7 @@ pub fn log(context: Context<'_, impl DebugOps>, msg_off: u32, msg_len: u32) -> R
1111

1212
let msg = context.memory.try_slice(msg_off, msg_len)?;
1313
let msg = String::from_utf8(msg.to_owned()).or_illegal_argument()?;
14-
context.kernel.log(LogEntry::Syscall(msg));
14+
context.kernel.log(msg);
1515
Ok(())
1616
}
1717

fvm/src/trace/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cid::Cid;
12
// Copyright 2021-2023 Protocol Labs
23
// SPDX-License-Identifier: Apache-2.0, MIT
34
use fvm_ipld_encoding::ipld_block::IpldBlock;
@@ -13,6 +14,13 @@ use crate::kernel::SyscallError;
1314
/// Execution Trace, only for informational and debugging purposes.
1415
pub type ExecutionTrace = Vec<ExecutionEvent>;
1516

17+
/// The type of operation being performed in an Ipld ExecutionEvent.
18+
#[derive(Clone, Debug, PartialEq)]
19+
pub enum IpldOperation {
20+
Get, // open
21+
Put, // link
22+
}
23+
1624
/// An "event" that happened during execution.
1725
///
1826
/// This is marked as `non_exhaustive` so we can introduce additional event types later.
@@ -39,4 +47,9 @@ pub enum ExecutionEvent {
3947
state: ActorState,
4048
},
4149
Log(String),
50+
Ipld {
51+
op: IpldOperation,
52+
cid: Cid,
53+
size: usize,
54+
},
4255
}

fvm/tests/default_kernel/ops.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use super::*;
55
mod ipld {
66

77
use cid::Cid;
8-
use fvm::kernel::{IpldBlockOps, LogEntry, SupportedHashes};
8+
use fvm::kernel::{IpldBlockOps, SupportedHashes};
99
use fvm::machine::Machine;
10+
use fvm::trace::IpldOperation;
1011
use fvm_ipld_blockstore::Blockstore;
1112
use fvm_ipld_encoding::{DAG_CBOR, IPLD_RAW};
1213
use multihash_codetable::MultihashDigest;
@@ -200,10 +201,21 @@ mod ipld {
200201
)
201202
}
202203

203-
assert!(call_manager.logs.contains(&LogEntry::BlockLink(cid)));
204+
assert!(call_manager
205+
.ipld_traces
206+
.iter()
207+
.any(|(op, c, _)| *op == IpldOperation::Put && *c == cid));
208+
204209
let (call_manager1, _) = kern1.into_inner();
205-
assert!(call_manager1.logs.contains(&LogEntry::BlockLink(cid))); // cid1==cid
206-
assert!(call_manager1.logs.contains(&LogEntry::BlockLink(other_cid)));
210+
211+
assert!(call_manager1
212+
.ipld_traces
213+
.iter()
214+
.any(|(op, c, _)| *op == IpldOperation::Put && *c == cid)); // cid1==cid
215+
assert!(call_manager1
216+
.ipld_traces
217+
.iter()
218+
.any(|(op, c, _)| *op == IpldOperation::Put && *c == other_cid));
207219

208220
Ok(())
209221
}

fvm/tests/dummy.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use fvm::gas::{Gas, GasCharge, GasTimer, GasTracker};
1313
use fvm::machine::limiter::MemoryLimiter;
1414
use fvm::machine::{Machine, MachineContext, Manifest, NetworkConfig};
1515
use fvm::state_tree::StateTree;
16+
use fvm::trace::IpldOperation;
1617
use fvm::{kernel, Kernel};
1718
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
1819
use fvm_ipld_encoding::{CborStore, DAG_CBOR};
@@ -191,7 +192,7 @@ pub struct DummyCallManager {
191192
pub origin: ActorID,
192193
pub nonce: u64,
193194
pub test_data: Rc<RefCell<TestData>>,
194-
pub logs: Vec<kernel::LogEntry>,
195+
pub ipld_traces: Vec<(IpldOperation, Cid, usize)>,
195196
limits: DummyLimiter,
196197
}
197198

@@ -217,7 +218,7 @@ impl DummyCallManager {
217218
test_data: rc,
218219
limits: DummyLimiter::default(),
219220
gas_premium: TokenAmount::zero(),
220-
logs: vec![],
221+
ipld_traces: vec![],
221222
},
222223
cell_ref,
223224
)
@@ -237,7 +238,7 @@ impl DummyCallManager {
237238
test_data: rc,
238239
limits: DummyLimiter::default(),
239240
gas_premium: TokenAmount::zero(),
240-
logs: vec![],
241+
ipld_traces: vec![],
241242
},
242243
cell_ref,
243244
)
@@ -270,7 +271,7 @@ impl CallManager for DummyCallManager {
270271
nonce,
271272
test_data: rc,
272273
limits,
273-
logs: vec![],
274+
ipld_traces: vec![],
274275
}
275276
}
276277

@@ -405,7 +406,11 @@ impl CallManager for DummyCallManager {
405406
todo!()
406407
}
407408

408-
fn log(&mut self, entry: kernel::LogEntry) {
409-
self.logs.push(entry);
409+
fn log(&mut self, _msg: String) {
410+
todo!()
411+
}
412+
413+
fn trace_ipld(&mut self, op: IpldOperation, cid: Cid, size: usize) {
414+
self.ipld_traces.push((op, cid, size));
410415
}
411416
}

0 commit comments

Comments
 (0)