Skip to content

Commit 7f15f27

Browse files
Merge branch 'master' into joshua/wasm-sync
2 parents b9ee0cc + 1a2f111 commit 7f15f27

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

crates/client-api-messages/src/energy.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ pub struct EnergyQuanta {
1616
impl EnergyQuanta {
1717
pub const ZERO: Self = EnergyQuanta { quanta: 0 };
1818

19-
// per the comment on [`FunctionBudget::DEFAULT_BUDGET`]: 1 second of wasm runtime is roughtly 2 TeV
20-
pub const PER_EXECUTION_SEC: Self = Self::new(2_000_000_000_000);
21-
pub const PER_EXECUTION_NANOSEC: Self = Self::new(Self::PER_EXECUTION_SEC.get() / 1_000_000_000);
22-
2319
#[inline]
24-
pub const fn new(quanta: u128) -> Self {
20+
pub fn new(quanta: u128) -> Self {
2521
Self { quanta }
2622
}
2723

2824
#[inline]
29-
pub const fn get(&self) -> u128 {
25+
pub fn get(&self) -> u128 {
3026
self.quanta
3127
}
3228

crates/commitlog/src/commit.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ impl Header {
6767
match &mut hdr.as_slice() {
6868
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] => Ok(None),
6969
buf => {
70-
let min_tx_offset = buf.get_u64().map_err(decode_error)?;
71-
let n = buf.get_u16().map_err(decode_error)?;
72-
let len = buf.get_u32().map_err(decode_error)?;
70+
let min_tx_offset = buf.get_u64().map_err(|e| decode_header_error(e, "min_tx_offset"))?;
71+
let n = buf.get_u16().map_err(|e| decode_header_error(e, "n"))?;
72+
let len = buf.get_u32().map_err(|e| decode_header_error(e, "len"))?;
7373

7474
Ok(Some(Self {
7575
min_tx_offset,
@@ -88,15 +88,15 @@ impl Header {
8888
return Ok(None);
8989
}
9090

91-
return Err(e);
91+
return Err(io::Error::new(e.kind(), format!("error reading commit header: {e}")));
9292
}
9393
match &mut hdr.as_slice() {
9494
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] => Ok(None),
9595
buf => {
96-
let min_tx_offset = buf.get_u64().map_err(decode_error)?;
97-
let epoch = buf.get_u64().map_err(decode_error)?;
98-
let n = buf.get_u16().map_err(decode_error)?;
99-
let len = buf.get_u32().map_err(decode_error)?;
96+
let min_tx_offset = buf.get_u64().map_err(|e| decode_header_error(e, "min_tx_offset"))?;
97+
let epoch = buf.get_u64().map_err(|e| decode_header_error(e, "epoch"))?;
98+
let n = buf.get_u16().map_err(|e| decode_header_error(e, "n"))?;
99+
let len = buf.get_u32().map_err(|e| decode_header_error(e, "len"))?;
100100

101101
Ok(Some(Self {
102102
min_tx_offset,
@@ -310,10 +310,16 @@ impl StoredCommit {
310310
return Ok(None);
311311
};
312312
let mut records = vec![0; hdr.len as usize];
313-
reader.read_exact(&mut records)?;
313+
reader.read_exact(&mut records).map_err(|e| {
314+
io::Error::new(
315+
e.kind(),
316+
format!("failed to read {} bytes of commit payload: {}", hdr.len, e),
317+
)
318+
})?;
314319

315320
let chk = reader.crc32c();
316-
let crc = decode_u32(reader.into_inner())?;
321+
let crc = decode_u32(reader.into_inner())
322+
.map_err(|e| io::Error::new(e.kind(), format!("failed to read checksum: {e}")))?;
317323

318324
if chk != crc {
319325
return Err(invalid_data(ChecksumMismatch));
@@ -385,8 +391,8 @@ fn decode_u32<R: Read>(mut read: R) -> io::Result<u32> {
385391
Ok(u32::from_le_bytes(buf))
386392
}
387393

388-
fn decode_error(e: DecodeError) -> io::Error {
389-
invalid_data(e)
394+
fn decode_header_error(e: DecodeError, field: &str) -> io::Error {
395+
invalid_data(format!("failed to decode commit header field '{field}': {e}"))
390396
}
391397

392398
fn invalid_data<E>(e: E) -> io::Error

crates/core/src/host/v8/budget.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::ptr;
1313
use core::sync::atomic::Ordering;
1414
use core::time::Duration;
1515
use core::{ffi::c_void, sync::atomic::AtomicBool};
16-
use spacetimedb_client_api_messages::energy::{EnergyQuanta, FunctionBudget};
16+
use spacetimedb_client_api_messages::energy::FunctionBudget;
1717
use std::sync::Arc;
1818
use v8::{Isolate, IsolateHandle};
1919

@@ -118,12 +118,7 @@ fn budget_to_duration(_budget: FunctionBudget) -> Duration {
118118
/// Returns [`EnergyStats`] for a reducer given its `budget`
119119
/// and the `duration` it took to execute.
120120
pub(super) fn energy_from_elapsed(budget: FunctionBudget, duration: Duration) -> EnergyStats {
121-
let used = duration.as_nanos() * EnergyQuanta::PER_EXECUTION_NANOSEC.get();
122-
// in order for duration_nanos * ev_per_ns >= u64::MAX:
123-
// duration_nanos >= u64::MAX / ev_per_ns
124-
// duration_nanos >= (9223372036854775 ns = 106.75 days)
125-
// so it's unlikely we'll have to worry about it
126-
let used = FunctionBudget::new(u64::try_from(used).unwrap_or(u64::MAX));
121+
let used = duration_to_budget(duration);
127122
let remaining = budget - used;
128123
EnergyStats { budget, remaining }
129124
}

0 commit comments

Comments
 (0)