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

Added batching using DB. #724

Merged
merged 1 commit into from
May 9, 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
24 changes: 24 additions & 0 deletions schemas/database/013_batches_and_transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Create ENUM for prover type
CREATE TABLE batches
(
id BIGSERIAL UNIQUE PRIMARY KEY,
next_root BYTEA NOT NULL UNIQUE,
prev_root BYTEA UNIQUE,
created_at TIMESTAMPTZ NOT NULL,
batch_type VARCHAR(50) NOT NULL,
data JSON NOT NULL,

FOREIGN KEY (prev_root) REFERENCES batches (next_root)
);

CREATE INDEX idx_batches_prev_root ON batches (prev_root);
CREATE UNIQUE INDEX i_single_null_prev_root ON batches ((batches.prev_root IS NULL)) WHERE batches.prev_root IS NULL;

CREATE TABLE transactions
(
transaction_id VARCHAR(256) NOT NULL UNIQUE PRIMARY KEY,
batch_next_root BYTEA NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL,

FOREIGN KEY (batch_next_root) REFERENCES batches (next_root)
);
22 changes: 21 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::database::{Database, DatabaseExt as _};
use crate::ethereum::Ethereum;
use crate::identity_tree::{
CanonicalTreeBuilder, Hash, InclusionProof, ProcessedStatus, RootItem, Status, TreeState,
TreeUpdate, TreeVersionReadOps, UnprocessedStatus,
TreeUpdate, TreeVersionReadOps, TreeWithNextVersion, UnprocessedStatus,
};
use crate::prover::map::initialize_prover_maps;
use crate::prover::{ProverConfig, ProverType};
Expand Down Expand Up @@ -261,6 +261,9 @@ impl App {

let (processed, batching_builder) = processed_builder.seal_and_continue();
let (batching, mut latest_builder) = batching_builder.seal_and_continue();

// We are duplicating updates here for some commitments that were batched but
// this is an idempotent operation
let pending_items = self
.database
.get_commitments_by_status(ProcessedStatus::Pending)
Expand All @@ -269,6 +272,15 @@ impl App {
latest_builder.update(&update);
}
let latest = latest_builder.seal();

let batch = self.database.get_latest_batch().await?;
if let Some(batch) = batch {
if batching.get_root() != batch.next_root {
batching.apply_updates_up_to(batch.next_root);
}
assert_eq!(batching.get_root(), batch.next_root);
}

Ok(Some(TreeState::new(mined, processed, batching, latest)))
}

Expand Down Expand Up @@ -355,6 +367,14 @@ impl App {

let latest = latest_builder.seal();

let batch = self.database.get_latest_batch().await?;
if let Some(batch) = batch {
if batching.get_root() != batch.next_root {
batching.apply_updates_up_to(batch.next_root);
}
assert_eq!(batching.get_root(), batch.next_root);
}

Ok(TreeState::new(mined, processed, batching, latest))
}

Expand Down
Loading
Loading