Skip to content

Commit

Permalink
update unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed May 3, 2024
1 parent e378ebf commit c719df1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
14 changes: 12 additions & 2 deletions dan_layer/epoch_manager/src/base_layer/base_layer_epoch_manager.rs
Expand Up @@ -349,7 +349,12 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let vn = self
.global_db
.validator_nodes(&mut tx)
.get_by_public_key(start_epoch, end_epoch, public_key)
.get_by_public_key(
start_epoch,
end_epoch,
public_key,
self.config.validator_node_sidechain_id.as_ref(),
)
.optional()?;

Ok(vn)
Expand Down Expand Up @@ -388,7 +393,12 @@ impl<TAddr: NodeAddressable + DerivableFromPublicKey>
let vn = self
.global_db
.validator_nodes(&mut tx)
.get_by_public_key(start_epoch, end_epoch, &public_key)
.get_by_public_key(
start_epoch,
end_epoch,
&public_key,
self.config.validator_node_sidechain_id.as_ref(),
)
.optional()?
.ok_or_else(|| EpochManagerError::ValidatorNodeNotRegistered {
address: public_key.to_string(),
Expand Down
1 change: 1 addition & 0 deletions dan_layer/storage/src/global/backend_adapter.rs
Expand Up @@ -108,6 +108,7 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone {
start_epoch: Epoch,
end_epoch: Epoch,
public_key: &PublicKey,
sidechain_id: Option<&PublicKey>,
) -> Result<ValidatorNode<Self::Addr>, Self::Error>;
fn validator_nodes_count(
&self,
Expand Down
3 changes: 2 additions & 1 deletion dan_layer/storage/src/global/validator_node_db.rs
Expand Up @@ -79,9 +79,10 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba
start_epoch: Epoch,
end_epoch: Epoch,
public_key: &PublicKey,
sidechain_id: Option<&PublicKey>,
) -> Result<ValidatorNode<TGlobalDbAdapter::Addr>, TGlobalDbAdapter::Error> {
self.backend
.get_validator_node_by_public_key(self.tx, start_epoch, end_epoch, public_key)
.get_validator_node_by_public_key(self.tx, start_epoch, end_epoch, public_key, sidechain_id)
.map_err(TGlobalDbAdapter::Error::into)
}

Expand Down
10 changes: 7 additions & 3 deletions dan_layer/storage_sqlite/src/global/backend_adapter.rs
Expand Up @@ -30,7 +30,7 @@ use std::{

use diesel::{
sql_query,
sql_types::{BigInt, Bigint},
sql_types::{BigInt, Bigint, Binary},
ExpressionMethods,
JoinOnDsl,
NullableExpressionMethods,
Expand Down Expand Up @@ -378,9 +378,9 @@ impl<TAddr: NodeAddressable> GlobalDbAdapter for SqliteGlobalDbAdapter<TAddr> {
validator_nodes::shard_key.eq(shard_key.as_bytes()),
validator_nodes::epoch.eq(epoch.as_u64() as i64),
validator_nodes::fee_claim_public_key.eq(ByteArray::as_bytes(&fee_claim_public_key)),
validator_nodes::sidechain_id.eq(sidechain_id.as_ref().map(|id| id.as_bytes())),
validator_nodes::sidechain_id.eq(sidechain_id.as_ref().map(|id| id.as_bytes()).unwrap_or(&[0u8; 32])),
))
.on_conflict(validator_nodes::public_key)
.on_conflict((validator_nodes::public_key, validator_nodes::sidechain_id))
.do_update()
.set((
validator_nodes::address.eq(&addr),
Expand All @@ -404,6 +404,7 @@ impl<TAddr: NodeAddressable> GlobalDbAdapter for SqliteGlobalDbAdapter<TAddr> {
start_epoch: Epoch,
end_epoch: Epoch,
public_key: &PublicKey,
sidechain_id: Option<&PublicKey>,
) -> Result<ValidatorNode<Self::Addr>, Self::Error> {
use crate::global::schema::{committees, validator_nodes};

Expand All @@ -424,6 +425,9 @@ impl<TAddr: NodeAddressable> GlobalDbAdapter for SqliteGlobalDbAdapter<TAddr> {
)
.filter(coalesce_bigint(committees::epoch.nullable(), validator_nodes::epoch).le(end_epoch.as_u64() as i64))
.filter(validator_nodes::public_key.eq(ByteArray::as_bytes(public_key)))
.filter(
validator_nodes::sidechain_id.eq(sidechain_id.map(|id| ByteArray::as_bytes(id)).unwrap_or(&[0u8; 32])),
)
.order_by(committees::epoch.desc())
.first::<DbValidatorNode>(tx.connection())
.map_err(|source| SqliteStorageError::DieselError {
Expand Down

0 comments on commit c719df1

Please sign in to comment.