Skip to content

Commit

Permalink
fix db
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed May 6, 2024
1 parent 2b3736f commit 490abfc
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 71 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build_binaries.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name": "linux-x86_64",
"runs-on": "ubuntu-22.04",
"rust": "stable",
"rust": "1.77",
"target": "x86_64-unknown-linux-gnu",
"cross": false,
"target_cpu": "x86-64",
Expand All @@ -11,7 +11,7 @@
{
"name": "linux-arm64",
"runs-on": "ubuntu-20.04",
"rust": "stable",
"rust": "1.77",
"target": "aarch64-unknown-linux-gnu",
"cross": true,
"target_cpu": "generic",
Expand All @@ -21,7 +21,7 @@
{
"name": "macos-x86_64",
"runs-on": "macos-11",
"rust": "stable",
"rust": "1.77",
"target": "x86_64-apple-darwin",
"cross": false,
"target_cpu": "x86-64",
Expand All @@ -30,7 +30,7 @@
{
"name": "macos-arm64",
"runs-on": "macos-12",
"rust": "stable",
"rust": "1.77",
"target": "aarch64-apple-darwin",
"cross": false,
"target_cpu": "generic",
Expand All @@ -40,7 +40,7 @@
{
"name": "windows-x64",
"runs-on": "windows-2019",
"rust": "stable",
"rust": "1.77",
"target": "x86_64-pc-windows-msvc",
"cross": false,
"target_cpu": "x86-64",
Expand All @@ -49,7 +49,7 @@
{
"name": "windows-arm64",
"runs-on": "windows-latest",
"rust": "stable",
"rust": "1.77",
"target": "aarch64-pc-windows-msvc",
"cross": false,
"target_cpu": "generic",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
TBN_BUNDLEID_BASE: "com.tari.network.pkg"
TARI_TARGET_NETWORK: igor
TARI_NETWORK: igor
toolchain: stable
toolchain: 1.77
matrix-json-file: ".github/workflows/build_binaries.json"

jobs:
Expand Down
38 changes: 27 additions & 11 deletions dan_layer/epoch_manager/src/base_layer/base_layer_epoch_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,19 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let mut tx = self.global_db.create_transaction()?;
let mut validator_nodes = self.global_db.validator_nodes(&mut tx);

let vns = validator_nodes.get_all_within_epochs(start_epoch, end_epoch)?;
let vns = validator_nodes.get_all_within_epochs(
start_epoch,
end_epoch,
self.config.validator_node_sidechain_id.as_ref(),
)?;

let num_committees = calculate_num_committees(vns.len() as u64, self.config.committee_size);

for vn in &vns {
validator_nodes.set_committee_bucket(
vn.shard_key,
vn.shard_key.to_committee_shard(num_committees),
self.config.validator_node_sidechain_id.as_ref(),
self.current_epoch,
)?;
}
Expand Down Expand Up @@ -500,7 +505,7 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let (start_epoch, end_epoch) = self.get_epoch_range(epoch)?;
let mut tx = self.global_db.create_transaction()?;
let mut vn_db = self.global_db.validator_nodes(&mut tx);
let num_vns = vn_db.count(start_epoch, end_epoch)?;
let num_vns = vn_db.count(start_epoch, end_epoch, self.config.validator_node_sidechain_id.as_ref())?;
let vn = vn_db.get_by_address(start_epoch, end_epoch, identity)?;
let num_committees = calculate_num_committees(num_vns, self.config.committee_size);
let shard = substate_address.to_committee_shard(num_committees);
Expand All @@ -514,7 +519,11 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let (start_epoch, end_epoch) = self.get_epoch_range(epoch)?;

let mut tx = self.global_db.create_transaction()?;
let num_vns = self.global_db.validator_nodes(&mut tx).count(start_epoch, end_epoch)?;
let num_vns = self.global_db.validator_nodes(&mut tx).count(
start_epoch,
end_epoch,
self.config.validator_node_sidechain_id.as_ref(),
)?;
Ok(calculate_num_committees(num_vns, self.config.committee_size))
}

Expand All @@ -532,10 +541,11 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let (start_epoch, end_epoch) = self.get_epoch_range(epoch)?;

let mut tx = self.global_db.create_transaction()?;
let db_vns = self
.global_db
.validator_nodes(&mut tx)
.get_all_within_epochs(start_epoch, end_epoch)?;
let db_vns = self.global_db.validator_nodes(&mut tx).get_all_within_epochs(
start_epoch,
end_epoch,
self.config.validator_node_sidechain_id.as_ref(),
)?;
let vns = db_vns.into_iter().map(Into::into).collect();
Ok(vns)
}
Expand Down Expand Up @@ -609,8 +619,12 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let mut tx = self.global_db.create_transaction()?;
let mut validator_node_db = self.global_db.validator_nodes(&mut tx);
let (start_epoch, end_epoch) = self.get_epoch_range(epoch)?;
let validators =
validator_node_db.get_by_shard_range(start_epoch, end_epoch, rounded_substate_address_range)?;
let validators = validator_node_db.get_by_shard_range(
start_epoch,
end_epoch,
self.config.validator_node_sidechain_id.as_ref(),
rounded_substate_address_range,
)?;
Ok(Committee::new(
validators.into_iter().map(|v| (v.address, v.public_key)).collect(),
))
Expand All @@ -632,7 +646,8 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let mut tx = self.global_db.create_transaction()?;
let mut validator_node_db = self.global_db.validator_nodes(&mut tx);
let (start_epoch, end_epoch) = self.get_epoch_range(epoch)?;
let num_validators = validator_node_db.count(start_epoch, end_epoch)?;
let num_validators =
validator_node_db.count(start_epoch, end_epoch, self.config.validator_node_sidechain_id.as_ref())?;
Ok(num_validators)
}

Expand All @@ -652,7 +667,8 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let shard = substate_address.to_committee_shard(num_committees);
let mut tx = self.global_db.create_transaction()?;
let mut validator_node_db = self.global_db.validator_nodes(&mut tx);
let num_validators = validator_node_db.count_in_bucket(epoch, shard)?;
let num_validators =
validator_node_db.count_in_bucket(epoch, self.config.validator_node_sidechain_id.as_ref(), shard)?;
let num_validators = u32::try_from(num_validators).map_err(|_| EpochManagerError::IntegerOverflow {
func: "get_committee_shard",
})?;
Expand Down
5 changes: 5 additions & 0 deletions dan_layer/storage/src/global/backend_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone {
tx: &mut Self::DbTransaction<'_>,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
) -> Result<Vec<ValidatorNode<Self::Addr>>, Self::Error>;
fn get_validator_node_by_address(
&self,
Expand All @@ -115,11 +116,13 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone {
tx: &mut Self::DbTransaction<'_>,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
) -> Result<u64, Self::Error>;
fn validator_nodes_count_for_bucket(
&self,
tx: &mut Self::DbTransaction<'_>,
epoch: Epoch,
sidechain_id: Option<&PublicKey>,
bucket: Shard,
) -> Result<u64, Self::Error>;

Expand All @@ -128,6 +131,7 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone {
tx: &mut Self::DbTransaction<'_>,
shard_key: SubstateAddress,
bucket: Shard,
sidechain_id: Option<&PublicKey>,
epoch: Epoch,
) -> Result<(), Self::Error>;

Expand All @@ -136,6 +140,7 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone {
tx: &mut Self::DbTransaction<'_>,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
shard_range: RangeInclusive<SubstateAddress>,
) -> Result<Vec<ValidatorNode<Self::Addr>>, Self::Error>;

Expand Down
27 changes: 20 additions & 7 deletions dan_layer/storage/src/global/validator_node_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,25 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba
.map_err(TGlobalDbAdapter::Error::into)
}

pub fn count(&mut self, start_epoch: Epoch, end_epoch: Epoch) -> Result<u64, TGlobalDbAdapter::Error> {
pub fn count(
&mut self,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
) -> Result<u64, TGlobalDbAdapter::Error> {
self.backend
.validator_nodes_count(self.tx, start_epoch, end_epoch)
.validator_nodes_count(self.tx, start_epoch, end_epoch, sidechain_id)
.map_err(TGlobalDbAdapter::Error::into)
}

pub fn count_in_bucket(&mut self, epoch: Epoch, bucket: Shard) -> Result<u64, TGlobalDbAdapter::Error> {
pub fn count_in_bucket(
&mut self,
epoch: Epoch,
sidechain_id: Option<&PublicKey>,
bucket: Shard,
) -> Result<u64, TGlobalDbAdapter::Error> {
self.backend
.validator_nodes_count_for_bucket(self.tx, epoch, bucket)
.validator_nodes_count_for_bucket(self.tx, epoch, sidechain_id, bucket)
.map_err(TGlobalDbAdapter::Error::into)
}

Expand Down Expand Up @@ -101,20 +111,22 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba
&mut self,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
) -> Result<Vec<ValidatorNode<TGlobalDbAdapter::Addr>>, TGlobalDbAdapter::Error> {
self.backend
.get_validator_nodes_within_epochs(self.tx, start_epoch, end_epoch)
.get_validator_nodes_within_epochs(self.tx, start_epoch, end_epoch, sidechain_id)
.map_err(TGlobalDbAdapter::Error::into)
}

pub fn get_by_shard_range(
&mut self,
start_epoch: Epoch,
end_epoch: Epoch,
sidechain_id: Option<&PublicKey>,
shard_range: RangeInclusive<SubstateAddress>,
) -> Result<Vec<ValidatorNode<TGlobalDbAdapter::Addr>>, TGlobalDbAdapter::Error> {
self.backend
.validator_nodes_get_by_shard_range(self.tx, start_epoch, end_epoch, shard_range)
.validator_nodes_get_by_shard_range(self.tx, start_epoch, end_epoch, sidechain_id, shard_range)
.map_err(TGlobalDbAdapter::Error::into)
}

Expand All @@ -133,10 +145,11 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba
&mut self,
substate_address: SubstateAddress,
committee_bucket: Shard,
sidechain_id: Option<&PublicKey>,
epoch: Epoch,
) -> Result<(), TGlobalDbAdapter::Error> {
self.backend
.validator_nodes_set_committee_bucket(self.tx, substate_address, committee_bucket, epoch)
.validator_nodes_set_committee_bucket(self.tx, substate_address, committee_bucket, sidechain_id, epoch)
.map_err(TGlobalDbAdapter::Error::into)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Your SQL goes here
ALTER TABLE validator_nodes
ADD COLUMN sidechain_id BLOB NULL;
ADD COLUMN sidechain_id BLOB NOT NULL;

drop index validator_nodes_public_key_uniq_idx;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CREATE TABLE committees
(
id INTEGER PRIMARY KEY autoincrement NOT NULL,
public_key BLOB NOT NULL,
validator_node_id INTEGER NOT NULL,
epoch BIGINT NOT NULL,
committee_bucket BIGINT NOT NULL,
FOREIGN KEY (public_key) REFERENCES validator_nodes (public_key)
FOREIGN KEY (validator_node_id) REFERENCES validator_nodes (id)
);

CREATE INDEX committees_epoch_index ON committees (epoch);
Loading

0 comments on commit 490abfc

Please sign in to comment.