Skip to content

Commit

Permalink
Summonerd: implement root fetching from database
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Oct 5, 2023
1 parent 0b7ef0c commit 0cc1471
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 30 deletions.
12 changes: 6 additions & 6 deletions crates/crypto/proof-setup/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ impl From<Phase2CeremonyCRS> for Phase2RawCeremonyCRS {
}
}

impl TryInto<pb::CeremonyCrs> for Phase2CeremonyCRS {
impl TryFrom<Phase2CeremonyCRS> for pb::CeremonyCrs {
type Error = anyhow::Error;

fn try_into(self) -> Result<pb::CeremonyCrs> {
Phase2RawCeremonyCRS::from(self).try_into()
fn try_from(data: Phase2CeremonyCRS) -> Result<pb::CeremonyCrs> {
Phase2RawCeremonyCRS::from(data).try_into()
}
}

Expand Down Expand Up @@ -413,11 +413,11 @@ impl From<Phase2CeremonyContribution> for Phase2RawCeremonyContribution {
}
}

impl TryInto<pb::participate_request::Contribution> for Phase2CeremonyContribution {
impl TryFrom<Phase2CeremonyContribution> for pb::participate_request::Contribution {
type Error = anyhow::Error;

fn try_into(self) -> Result<pb::participate_request::Contribution> {
Phase2RawCeremonyContribution::from(self).try_into()
fn try_from(data: Phase2CeremonyContribution) -> Result<pb::participate_request::Contribution> {
Phase2RawCeremonyContribution::from(data).try_into()
}
}

Expand Down
6 changes: 2 additions & 4 deletions tools/summonerd/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ impl Coordinator {
return Ok(());
}
};
//if let Some(contribution) = contribution.validate(&mut OsRng, &self.storage.root().await?) {
if true {
let contribution = contribution.assume_valid();
if let Some(contribution) = contribution.validate(&mut OsRng, &self.storage.root().await?) {
if contribution.is_linked_to(&parent) {
self.storage
.commit_contribution(contributor, &contribution)
.commit_contribution(contributor, contribution)
.await?;
participant
.confirm(self.storage.current_slot().await?)
Expand Down
76 changes: 57 additions & 19 deletions tools/summonerd/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use std::sync::Arc;

use anyhow::Result;
use camino::Utf8Path;
use penumbra_keys::Address;
use penumbra_proof_setup::all::{Phase2CeremonyCRS, Phase2CeremonyContribution};
use penumbra_proof_setup::all::{
Phase2CeremonyCRS, Phase2CeremonyContribution, Phase2RawCeremonyCRS,
Phase2RawCeremonyContribution,
};
use penumbra_proto::{
penumbra::tools::summoning::v1alpha1::{
self as pb, participate_request::Contribution as PBContribution,
},
Message,
};
use r2d2_sqlite::{rusqlite::OpenFlags, SqliteConnectionManager};
use tokio::sync::Mutex;
use tokio::task::spawn_blocking;

#[derive(Clone)]
pub struct Storage {
pool: r2d2::Pool<SqliteConnectionManager>,
crs: Arc<Mutex<Phase2CeremonyCRS>>,
}

impl Storage {
Expand All @@ -35,15 +40,16 @@ impl Storage {

// Create the tables
tx.execute_batch(include_str!("storage/schema.sql"))?;
// TODO: Remove this in favor of a specific command for initializing root
let root = Phase2CeremonyCRS::root()?;
tx.execute(
"INSERT INTO phase2_contributions VALUES (0, 1, ?1, NULL)",
[pb::CeremonyCrs::try_from(root)?.encode_to_vec()],
)?;

tx.commit()?;
drop(conn);

let root = Phase2CeremonyCRS::root()?;
Ok(Storage {
pool,
crs: Arc::new(Mutex::new(root.clone())),
})
Ok(Storage { pool })
})
.await?
}
Expand All @@ -65,10 +71,8 @@ impl Storage {
}

pub async fn load(path: impl AsRef<Utf8Path>) -> anyhow::Result<Self> {
let root: Phase2CeremonyCRS = Phase2CeremonyCRS::root()?;
let storage = Self {
pool: Self::connect(path)?,
crs: Arc::new(Mutex::new(root.clone())),
};

Ok(storage)
Expand All @@ -83,22 +87,42 @@ impl Storage {
}

pub async fn current_crs(&self) -> Result<Phase2CeremonyCRS> {
Ok(self.crs.lock().await.clone())
let mut conn = self.pool.get()?;
let tx = conn.transaction()?;
let (is_root, contribution_or_crs) = tx.query_row(
"SELECT is_root, contribution_or_crs FROM phase2_contributions ORDER BY slot DESC LIMIT 1",
[],
|row| Ok((row.get::<usize, bool>(0)?, row.get::<usize, Vec<u8>>(1)?)),
)?;
let crs = if is_root {
Phase2RawCeremonyCRS::try_from(pb::CeremonyCrs::decode(
contribution_or_crs.as_slice(),
)?)?
.assume_valid()
} else {
Phase2RawCeremonyContribution::try_from(PBContribution::decode(
contribution_or_crs.as_slice(),
)?)?
.assume_valid()
.new_elements()
};
Ok(crs)
}

// TODO: Add other stuff here
pub async fn commit_contribution(
&self,
contributor: Address,
contribution: &Phase2CeremonyContribution,
contribution: Phase2CeremonyContribution,
) -> Result<()> {
*self.crs.lock().await = contribution.new_elements();
let mut conn = self.pool.get()?;
let tx = conn.transaction()?;
let contributor_bytes = contributor.to_vec();
tx.execute(
"INSERT INTO phase2_contributions VALUES(NULL, ?1)",
[contributor_bytes],
"INSERT INTO phase2_contributions VALUES(NULL, 0, ?1, ?2)",
[
PBContribution::try_from(contribution)?.encode_to_vec(),
contributor_bytes,
],
)?;
tx.commit()?;
Ok(())
Expand All @@ -114,4 +138,18 @@ impl Storage {
.unwrap_or(0);
Ok(out)
}

pub async fn root(&self) -> Result<Phase2CeremonyCRS> {
let mut conn = self.pool.get()?;
let tx = conn.transaction()?;
let data = tx.query_row(
"SELECT contribution_or_crs FROM phase2_contributions WHERE is_root LIMIT 1",
[],
|row| row.get::<usize, Vec<u8>>(0),
)?;
Ok(Phase2RawCeremonyCRS::try_from(pb::CeremonyCrs::decode(
data.as_slice(),
)?)?
.assume_valid())
}
}
7 changes: 6 additions & 1 deletion tools/summonerd/src/storage/schema.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
-- used for storing phase 2 contribution information
CREATE TABLE phase2_contributions (
slot INTEGER PRIMARY KEY,
address BLOB NOT NULL
-- 1 if this is a root
is_root INTEGER NOT NULL,
-- If this is the root, will be just the elements, and not a full contribution
contribution_or_crs BLOB NOT NULL,
-- NULL in the specific case that this is the root
address BLOB
);

0 comments on commit 0cc1471

Please sign in to comment.