Skip to content

Commit fe5a776

Browse files
committed
Add initial log entry during initialization, fixes #100
Previously, an add-chain request would add an entry to the pool, and then during the first sequencing the initial log entry would be added as the _second_ entry in the log. Instead, add the initial entry during initialization to make sure it's actually the initial entry.
1 parent c0b4750 commit fe5a776

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

crates/generic_log_worker/src/log_ops.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,16 @@ impl SequenceState {
467467
})
468468
}
469469

470-
/// Returns the current checkpoint
470+
/// Returns the current checkpoint.
471471
pub(crate) fn checkpoint(&self) -> &[u8] {
472472
&self.checkpoint
473473
}
474474

475+
/// Returns the current tree size.
476+
pub(crate) fn tree_size(&self) -> u64 {
477+
self.tree.size()
478+
}
479+
475480
/// Proves inclusion of the last leaf in the current tree.
476481
#[cfg(test)]
477482
pub(crate) fn prove_inclusion_of_last_elem(&self) -> Proof {
@@ -766,13 +771,6 @@ pub(crate) async fn sequence<L: LogEntry>(
766771
cache: &impl CacheWrite,
767772
metrics: &SequencerMetrics,
768773
) -> Result<(), anyhow::Error> {
769-
// Add the log's initial entry if needed.
770-
if sequence_state.borrow().tree.size() == 0 {
771-
if let Some(entry) = L::initial_entry() {
772-
pool_state.borrow_mut().add(entry.lookup_key(), entry);
773-
}
774-
}
775-
776774
let Some(entries) = pool_state.borrow_mut().take(
777775
sequence_state.borrow().tree.size(),
778776
config.max_sequence_skips,
@@ -882,6 +880,12 @@ async fn sequence_entries<L: LogEntry>(
882880
let mut cache_metadata = Vec::with_capacity(entries.len());
883881

884882
for (entry, sender) in entries {
883+
if n == 0 {
884+
if let Some(initial_entry) = L::initial_entry() {
885+
assert_eq!(entry.lookup_key(), initial_entry.lookup_key());
886+
}
887+
}
888+
885889
// Add the entry and metadata to our lists of things sequenced
886890
let metadata = (n, timestamp);
887891
cache_metadata.push((entry.lookup_key(), metadata));

crates/generic_log_worker/src/sequencer_do.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{cell::RefCell, future::Future, pin::Pin, time::Duration};
77

88
use crate::{
99
deserialize, get_durable_object_stub, load_public_bucket,
10-
log_ops::{self, CreateError, PoolState, SequenceState},
10+
log_ops::{self, add_leaf_to_pool, CreateError, PoolState, SequenceState},
1111
metrics::{millis_diff_as_secs, ObjectMetrics, SequencerMetrics},
1212
serialize,
1313
util::now_millis,
@@ -229,6 +229,13 @@ impl<L: LogEntry> GenericSequencer<L> {
229229
.await
230230
.map_err(|e| e.to_string())?;
231231

232+
// If the tree is empty, add the log's initial entry if it has one.
233+
if self.sequence_state.borrow().tree_size() == 0 {
234+
if let Some(entry) = L::initial_entry() {
235+
add_leaf_to_pool(&self.pool_state, &self.cache, &self.config, entry);
236+
}
237+
}
238+
232239
// Start sequencing loop (OK if alarm is already scheduled).
233240
self.do_state
234241
.storage()

0 commit comments

Comments
 (0)