Skip to content

Commit

Permalink
Do not load entire leaf just to get anchor view (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbearer authored Oct 24, 2024
1 parent 4144ba4 commit 3d46e44
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions sequencer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,8 @@ async fn fetch_proposal_chain<N, P, V>(

async fn load_anchor_view(persistence: &impl SequencerPersistence) -> ViewNumber {
loop {
match persistence.load_anchor_leaf().await {
Ok(Some((leaf, _))) => break leaf.view_number(),
Ok(None) => break ViewNumber::genesis(),
match persistence.load_anchor_view().await {
Ok(view) => break view,
Err(err) => {
tracing::warn!("error loading anchor view: {err:#}");
sleep(Duration::from_secs(1)).await;
Expand Down
25 changes: 25 additions & 0 deletions sequencer/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ mod persistence_tests {
final_qc,
];

assert_eq!(
storage.load_anchor_view().await.unwrap(),
ViewNumber::genesis()
);

let consumer = EventCollector::default();
let leaf_chain = leaves
.iter()
Expand All @@ -381,6 +386,10 @@ mod persistence_tests {
)
.await
.unwrap();
assert_eq!(
storage.load_anchor_view().await.unwrap(),
ViewNumber::new(2)
);

for i in 0..=2 {
assert_eq!(
Expand Down Expand Up @@ -430,6 +439,10 @@ mod persistence_tests {
storage.load_anchor_leaf().await.unwrap(),
Some((leaves[2].clone(), qcs[2].clone()))
);
assert_eq!(
storage.load_anchor_view().await.unwrap(),
leaves[2].view_number()
);

// Process a second decide event.
let consumer = EventCollector::default();
Expand All @@ -442,6 +455,10 @@ mod persistence_tests {
)
.await
.unwrap();
assert_eq!(
storage.load_anchor_view().await.unwrap(),
ViewNumber::new(3)
);

// A decide event should have been processed.
let events = consumer.events.read().await;
Expand Down Expand Up @@ -643,6 +660,10 @@ mod persistence_tests {
.view_number(),
ViewNumber::new(1)
);
assert_eq!(
storage.load_anchor_view().await.unwrap(),
ViewNumber::new(1)
);

// Now decide remaining leaves successfully. We should now garbage collect and process a
// decide event for all the leaves.
Expand Down Expand Up @@ -686,6 +707,10 @@ mod persistence_tests {
.view_number(),
ViewNumber::new(3)
);
assert_eq!(
storage.load_anchor_view().await.unwrap(),
ViewNumber::new(3)
);

// Check decide event.
tracing::info!("check decide event");
Expand Down
8 changes: 8 additions & 0 deletions sequencer/src/persistence/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ impl SequencerPersistence for Persistence {
Ok(Some((leaf, qc)))
}

async fn load_anchor_view(&self) -> anyhow::Result<ViewNumber> {
let mut tx = self.db.read().await?;
let (view,) = query_as::<(i64,)>("SELECT coalesce(max(view), 0) FROM anchor_leaf")
.fetch_one(tx.as_mut())
.await?;
Ok(ViewNumber::new(view as u64))
}

async fn load_undecided_state(
&self,
) -> anyhow::Result<Option<(CommitmentMap<Leaf>, BTreeMap<ViewNumber, View<SeqTypes>>)>> {
Expand Down
7 changes: 7 additions & 0 deletions types/src/v0/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ pub trait SequencerPersistence: Sized + Send + Sync + 'static {
&self,
decided_upgrade_certificate: Option<UpgradeCertificate<SeqTypes>>,
) -> anyhow::Result<()>;

async fn load_anchor_view(&self) -> anyhow::Result<ViewNumber> {
match self.load_anchor_leaf().await? {
Some((leaf, _)) => Ok(leaf.view_number()),
None => Ok(ViewNumber::genesis()),
}
}
}

#[async_trait]
Expand Down

0 comments on commit 3d46e44

Please sign in to comment.