Skip to content

Commit 6b0cf37

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 70d68d0 commit 6b0cf37

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 {
@@ -740,13 +745,6 @@ pub(crate) async fn sequence<L: LogEntry>(
740745
cache: &impl CacheWrite,
741746
metrics: &SequencerMetrics,
742747
) -> Result<(), anyhow::Error> {
743-
// Add the log's initial entry if needed.
744-
if sequence_state.borrow().tree.size() == 0 {
745-
if let Some(entry) = L::initial_entry() {
746-
pool_state.borrow_mut().add(entry.lookup_key(), entry);
747-
}
748-
}
749-
750748
let Some(entries) = pool_state.borrow_mut().take(
751749
sequence_state.borrow().tree.size(),
752750
config.max_sequence_skips,
@@ -856,6 +854,12 @@ async fn sequence_entries<L: LogEntry>(
856854
let mut cache_metadata = Vec::with_capacity(entries.len());
857855

858856
for (entry, sender) in entries {
857+
if n == 0 {
858+
if let Some(initial_entry) = L::initial_entry() {
859+
assert_eq!(entry.lookup_key(), initial_entry.lookup_key());
860+
}
861+
}
862+
859863
// Add the entry and metadata to our lists of things sequenced
860864
let metadata = (n, timestamp);
861865
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)