Skip to content

Commit

Permalink
fix double registration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 27, 2024
1 parent 680f4f6 commit 4491a9f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
10 changes: 8 additions & 2 deletions base_layer/core/src/chain_storage/blockchain_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,14 @@ pub trait BlockchainBackend: Send + Sync {
sidechain_pk: Option<&PublicKey>,
epoch: VnEpoch,
) -> Result<Vec<ValidatorNodeRegistrationInfo>, ChainStorageError>;

/// Returns true if the validator node is registered
/// Returns true if the validator node registration UTXO exists
fn validator_node_exists(
&self,
sidechain_pk: Option<&PublicKey>,
height: u64,
validator_node_pk: &PublicKey,
) -> Result<bool, ChainStorageError>;
/// Returns true if the validator node is registered and currently active
fn validator_node_is_active(
&self,
sidechain_pk: Option<&PublicKey>,
Expand Down
23 changes: 23 additions & 0 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,13 @@ impl LMDBDatabase {
let constants = self.get_consensus_constants(header.height);
let epoch = constants.block_height_to_epoch(header.height);
let sidechain_pk = sidechain_feature.sidechain_id().map(|id| id.public_key());
info!(
target: LOG_TARGET,
"Evicting ValidatorNode in {}: public_key: {}, sidechain_public_key: {:?}",
epoch,
evict_node,
sidechain_pk.map(|pk| pk.to_hex()),
);
store.evict(sidechain_pk, evict_node, epoch)?;
},
}
Expand Down Expand Up @@ -2699,6 +2706,22 @@ impl BlockchainBackend for LMDBDatabase {
Ok(nodes)
}

fn validator_node_exists(
&self,
sidechain_pk: Option<&PublicKey>,
height: u64,
validator_node_pk: &PublicKey,
) -> Result<bool, ChainStorageError> {
let txn = self.read_transaction()?;
let vn_store = self.validator_node_store(&txn);
let constants = self.consensus_manager.consensus_constants(height);

// Get the current epoch for the height
let end_epoch = constants.block_height_to_epoch(height);
let is_active = vn_store.vn_exists(sidechain_pk, validator_node_pk, end_epoch)?;
Ok(is_active)
}

fn validator_node_is_active(
&self,
sidechain_pk: Option<&PublicKey>,
Expand Down
16 changes: 16 additions & 0 deletions base_layer/core/src/chain_storage/lmdb_db/validator_node_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ impl<'a, Txn: Deref<Target = ConstTransaction<'a>>> ValidatorNodeStore<'a, Txn>
Ok(cursor)
}

/// Checks if the given validator node (by its public key and side chain ID)
/// exists until a given `end_epoch`.
pub fn vn_exists(
&self,
sidechain_id: Option<&PublicKey>,
public_key: &PublicKey,
end_epoch: VnEpoch,
) -> Result<bool, ChainStorageError> {
let key = create_vn_key(sidechain_id, public_key);
let Some(vn) = lmdb_get::<_, ValidatorNodeEntry>(self.txn, &self.db_validator_nodes, &key)? else {
return Ok(false);
};

Ok(vn.registration_epoch <= end_epoch)
}

/// Checks if the given validator node (by its public key and side chain ID)
/// exists until a given `end_epoch`.
pub fn is_vn_active(
Expand Down
12 changes: 12 additions & 0 deletions base_layer/core/src/test_helpers/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ impl BlockchainBackend for TempDatabase {
.fetch_validators_exiting_in_epoch(sidechain_pk, epoch)
}

fn validator_node_exists(
&self,
sidechain_pk: Option<&PublicKey>,
height: u64,
validator_node_pk: &PublicKey,
) -> Result<bool, ChainStorageError> {
self.db
.as_ref()
.unwrap()
.validator_node_exists(sidechain_pk, height, validator_node_pk)
}

fn validator_node_is_active(
&self,
sidechain_pk: Option<&PublicKey>,
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ pub fn check_validator_node_registration<B: BlockchainBackend>(
return Ok(());
};

if db.validator_node_is_active(sidechain_features.sidechain_public_key(), height, vn_reg.public_key())? {
if db.validator_node_exists(sidechain_features.sidechain_public_key(), height, vn_reg.public_key())? {
return Err(ValidationError::ValidatorNodeAlreadyRegistered {
public_key: vn_reg.public_key().clone(),
});
Expand Down

0 comments on commit 4491a9f

Please sign in to comment.