Skip to content

Commit

Permalink
Added batching using DB.
Browse files Browse the repository at this point in the history
  • Loading branch information
piohei committed May 9, 2024
1 parent 85466a9 commit ad256dc
Show file tree
Hide file tree
Showing 11 changed files with 912 additions and 125 deletions.
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

0 comments on commit ad256dc

Please sign in to comment.