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

leaf1 to leaf2 migration for sql #767

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions migrations/postgres/V500__leaf2_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE leaf2
(
height BIGINT PRIMARY KEY REFERENCES header (height) ON DELETE CASCADE,
hash VARCHAR NOT NULL UNIQUE,
block_hash VARCHAR NOT NULL REFERENCES header (hash) ON DELETE CASCADE,
leaf JSONB NOT NULL,
qc JSONB NOT NULL
);

CREATE TABLE leaf_migration (
id SERIAL PRIMARY KEY,
completed bool NOT NULL DEFAULT false
);

INSERT INTO leaf_migration ("completed") VALUES (false);
15 changes: 15 additions & 0 deletions migrations/sqlite/V300__leaf2_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE leaf2
(
height BIGINT PRIMARY KEY REFERENCES header (height) ON DELETE CASCADE,
hash VARCHAR NOT NULL UNIQUE,
block_hash VARCHAR NOT NULL REFERENCES header (hash) ON DELETE CASCADE,
leaf JSONB NOT NULL,
qc JSONB NOT NULL
);

CREATE TABLE leaf_migration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
completed bool NOT NULL DEFAULT false
);

INSERT INTO leaf_migration ("completed") VALUES (false);
6 changes: 3 additions & 3 deletions src/availability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ mod test {
use async_lock::RwLock;
use committable::Committable;
use futures::future::FutureExt;
use hotshot_types::{data::Leaf, simple_certificate::QuorumCertificate};
use hotshot_types::{data::Leaf2, simple_certificate::QuorumCertificate2};
use portpicker::pick_unused_port;
use serde::de::DeserializeOwned;
use std::{fmt::Debug, time::Duration};
Expand Down Expand Up @@ -883,9 +883,9 @@ mod test {
);

// mock up some consensus data.
let leaf = Leaf::<MockTypes>::genesis(&Default::default(), &Default::default()).await;
let leaf = Leaf2::<MockTypes>::genesis(&Default::default(), &Default::default()).await;
let qc =
QuorumCertificate::genesis::<TestVersions>(&Default::default(), &Default::default())
QuorumCertificate2::genesis::<TestVersions>(&Default::default(), &Default::default())
.await;
let leaf = LeafQueryData::new(leaf, qc).unwrap();
let block = BlockQueryData::new(leaf.header().clone(), MockPayload::genesis());
Expand Down
28 changes: 14 additions & 14 deletions src/availability/query_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use crate::{types::HeightIndexed, Header, Metadata, Payload, Transaction, VidCommon, VidShare};
use committable::{Commitment, Committable};
use hotshot_types::{
data::Leaf,
simple_certificate::QuorumCertificate,
data::{Leaf, Leaf2},
simple_certificate::QuorumCertificate2,
traits::{
self,
block_contents::{BlockHeader, GENESIS_VID_NUM_STORAGE_NODES},
Expand All @@ -28,8 +28,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use snafu::{ensure, Snafu};
use std::fmt::Debug;

pub type LeafHash<Types> = Commitment<Leaf<Types>>;
pub type QcHash<Types> = Commitment<QuorumCertificate<Types>>;
pub type LeafHash<Types> = Commitment<Leaf2<Types>>;
pub type QcHash<Types> = Commitment<QuorumCertificate2<Types>>;

/// A block hash is the hash of the block header.
///
Expand Down Expand Up @@ -192,8 +192,8 @@ pub trait QueryablePayload<Types: NodeType>: traits::BlockPayload<Types> {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(bound = "")]
pub struct LeafQueryData<Types: NodeType> {
pub(crate) leaf: Leaf<Types>,
pub(crate) qc: QuorumCertificate<Types>,
pub(crate) leaf: Leaf2<Types>,
pub(crate) qc: QuorumCertificate2<Types>,
}

#[derive(Clone, Debug, Snafu)]
Expand All @@ -212,13 +212,13 @@ impl<Types: NodeType> LeafQueryData<Types> {
///
/// Fails with an [`InconsistentLeafError`] if `qc` does not reference `leaf`.
pub fn new(
mut leaf: Leaf<Types>,
qc: QuorumCertificate<Types>,
mut leaf: Leaf2<Types>,
qc: QuorumCertificate2<Types>,
) -> Result<Self, InconsistentLeafError<Types>> {
// TODO: Replace with the new `commit` function in HotShot. Add an `upgrade_lock` parameter
// and a `HsVer: Versions` bound, then call `leaf.commit(upgrade_lock).await`. This will
// require updates in callers and relevant types as well.
let leaf_commit = <Leaf<Types> as Committable>::commit(&leaf);
let leaf_commit = <Leaf2<Types> as Committable>::commit(&leaf);
ensure!(
qc.data.leaf_commit == leaf_commit,
InconsistentLeafSnafu {
Expand All @@ -239,16 +239,16 @@ impl<Types: NodeType> LeafQueryData<Types> {
instance_state: &Types::InstanceState,
) -> Self {
Self {
leaf: Leaf::genesis(validated_state, instance_state).await,
qc: QuorumCertificate::genesis::<HsVer>(validated_state, instance_state).await,
leaf: Leaf2::genesis(validated_state, instance_state).await,
qc: QuorumCertificate2::genesis::<HsVer>(validated_state, instance_state).await,
}
}

pub fn leaf(&self) -> &Leaf<Types> {
pub fn leaf(&self) -> &Leaf2<Types> {
&self.leaf
}

pub fn qc(&self) -> &QuorumCertificate<Types> {
pub fn qc(&self) -> &QuorumCertificate2<Types> {
&self.qc
}

Expand All @@ -260,7 +260,7 @@ impl<Types: NodeType> LeafQueryData<Types> {
// TODO: Replace with the new `commit` function in HotShot. Add an `upgrade_lock` parameter
// and a `HsVer: Versions` bound, then call `leaf.commit(upgrade_lock).await`. This will
// require updates in callers and relevant types as well.
<Leaf<Types> as Committable>::commit(&self.leaf)
<Leaf2<Types> as Committable>::commit(&self.leaf)
}

pub fn block_hash(&self) -> BlockHash<Types> {
Expand Down
27 changes: 13 additions & 14 deletions src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub mod availability_tests {
};
use committable::Committable;
use futures::stream::StreamExt;
use hotshot_types::data::Leaf;
use hotshot_types::data::Leaf2;
use std::collections::HashMap;
use std::fmt::Debug;
use std::ops::{Bound, RangeBounds};
Expand All @@ -148,7 +148,7 @@ pub mod availability_tests {
assert_eq!(leaf.height(), i as u64);
assert_eq!(
leaf.hash(),
<Leaf<MockTypes> as Committable>::commit(&leaf.leaf)
<Leaf2<MockTypes> as Committable>::commit(&leaf.leaf)
);

// Check indices.
Expand Down Expand Up @@ -550,11 +550,10 @@ pub mod persistence_tests {
setup_test,
},
types::HeightIndexed,
Leaf,
};
use committable::Committable;
use hotshot_example_types::state_types::{TestInstanceState, TestValidatedState};
use hotshot_types::simple_certificate::QuorumCertificate;
use hotshot_types::{data::Leaf2, simple_certificate::QuorumCertificate2};

#[tokio::test(flavor = "multi_thread")]
pub async fn test_revert<D: TestableDataSource>()
Expand All @@ -571,20 +570,20 @@ pub mod persistence_tests {
let ds = D::connect(&storage).await;

// Mock up some consensus data.
let mut qc = QuorumCertificate::<MockTypes>::genesis::<TestVersions>(
let mut qc = QuorumCertificate2::<MockTypes>::genesis::<TestVersions>(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
let mut leaf = Leaf::<MockTypes>::genesis(
let mut leaf = Leaf2::<MockTypes>::genesis(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
// Increment the block number, to distinguish this block from the genesis block, which
// already exists.
leaf.block_header_mut().block_number += 1;
qc.data.leaf_commit = <Leaf<MockTypes> as Committable>::commit(&leaf);
qc.data.leaf_commit = <Leaf2<MockTypes> as Committable>::commit(&leaf);

let block = BlockQueryData::new(leaf.block_header().clone(), MockPayload::genesis());
let leaf = LeafQueryData::new(leaf, qc).unwrap();
Expand Down Expand Up @@ -623,20 +622,20 @@ pub mod persistence_tests {
let ds = D::connect(&storage).await;

// Mock up some consensus data.
let mut qc = QuorumCertificate::<MockTypes>::genesis::<TestVersions>(
let mut qc = QuorumCertificate2::<MockTypes>::genesis::<TestVersions>(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
let mut leaf = Leaf::<MockTypes>::genesis(
let mut leaf = Leaf2::<MockTypes>::genesis(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
// Increment the block number, to distinguish this block from the genesis block, which
// already exists.
leaf.block_header_mut().block_number += 1;
qc.data.leaf_commit = <Leaf<MockTypes> as Committable>::commit(&leaf);
qc.data.leaf_commit = <Leaf2<MockTypes> as Committable>::commit(&leaf);

let block = BlockQueryData::new(leaf.block_header().clone(), MockPayload::genesis());
let leaf = LeafQueryData::new(leaf, qc).unwrap();
Expand Down Expand Up @@ -686,20 +685,20 @@ pub mod persistence_tests {
let ds = D::connect(&storage).await;

// Mock up some consensus data.
let mut mock_qc = QuorumCertificate::<MockTypes>::genesis::<TestVersions>(
let mut mock_qc = QuorumCertificate2::<MockTypes>::genesis::<TestVersions>(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
let mut mock_leaf = Leaf::<MockTypes>::genesis(
let mut mock_leaf = Leaf2::<MockTypes>::genesis(
&TestValidatedState::default(),
&TestInstanceState::default(),
)
.await;
// Increment the block number, to distinguish this block from the genesis block, which
// already exists.
mock_leaf.block_header_mut().block_number += 1;
mock_qc.data.leaf_commit = <Leaf<MockTypes> as Committable>::commit(&mock_leaf);
mock_qc.data.leaf_commit = <Leaf2<MockTypes> as Committable>::commit(&mock_leaf);

let block = BlockQueryData::new(mock_leaf.block_header().clone(), MockPayload::genesis());
let leaf = LeafQueryData::new(mock_leaf.clone(), mock_qc.clone()).unwrap();
Expand All @@ -725,7 +724,7 @@ pub mod persistence_tests {

// Get a mutable transaction again, insert different data.
mock_leaf.block_header_mut().block_number += 1;
mock_qc.data.leaf_commit = <Leaf<MockTypes> as Committable>::commit(&mock_leaf);
mock_qc.data.leaf_commit = <Leaf2<MockTypes> as Committable>::commit(&mock_leaf);
let block = BlockQueryData::new(mock_leaf.block_header().clone(), MockPayload::genesis());
let leaf = LeafQueryData::new(mock_leaf, mock_qc).unwrap();

Expand Down
Loading
Loading