Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-rogue] Do not load entire leaf just to get anchor view #2216

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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