Skip to content

Commit

Permalink
Remove duplicate function between the blockchains
Browse files Browse the repository at this point in the history
  • Loading branch information
viquezclaudio committed May 28, 2024
1 parent 1cf1c3f commit 9e3c822
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 64 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions blockchain-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ thiserror = "1.0"
tokio-stream = { version = "0.1", features = ["sync"] }

nimiq-block = { workspace = true }
nimiq-collections = { workspace = true }
nimiq-database-value = { workspace = true }
nimiq-hash = { workspace = true }
nimiq-primitives = { workspace = true, features = ["coin", "key-nibbles", "policy"] }
nimiq-serde = { workspace = true }
nimiq-transaction = { workspace = true }
nimiq-vrf = { workspace = true }
31 changes: 31 additions & 0 deletions blockchain-interface/src/abstract_blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use futures::stream::BoxStream;
use nimiq_block::{Block, BlockType, MacroBlock};
use nimiq_collections::BitSet;
use nimiq_hash::Blake2bHash;
use nimiq_primitives::{
networks::NetworkId,
policy::Policy,
slots_allocation::{Slot, Validators},
};
use nimiq_vrf::{Rng, VrfEntropy, VrfUseCase};

use crate::{
error::{BlockchainError, BlockchainEvent, Direction},
Expand Down Expand Up @@ -115,8 +117,37 @@ pub trait AbstractBlockchain {
/// block number and offset.
fn get_proposer_at(&self, block_number: u32, offset: u32) -> Result<Slot, BlockchainError>;

/// Obtains the slow owner at a given block hash.
fn get_proposer_of(&self, block_hash: &Blake2bHash) -> Result<Slot, BlockchainError>;

/// Obtains the slot number, given an offset and vrf entropy, not considering the ones in the disable_slots bitmap.
fn compute_slot_number(offset: u32, vrf_entropy: VrfEntropy, disabled_slots: BitSet) -> u16 {
// RNG for slot selection
let mut rng = vrf_entropy.rng(VrfUseCase::ViewSlotSelection);

// Create a list of viable slots.
let mut slots: Vec<u16> = if disabled_slots.len() == Policy::SLOTS as usize {
// If all slots are disabled, we will accept any slot, since we want the
// chain to progress.
(0..Policy::SLOTS).collect()
} else {
// Otherwise, we will only accept slots that are not disabled.
(0..Policy::SLOTS)
.filter(|slot| !disabled_slots.contains(*slot as usize))
.collect()
};

// Shuffle the slots vector using the Fisher–Yates shuffle.
for i in (1..slots.len()).rev() {
let r = rng.next_u64_below((i + 1) as u64) as usize;
slots.swap(r, i);
}

// Now simply take the offset modulo the number of viable slots and that will give us
// the chosen slot.
slots[offset as usize % slots.len()]
}

/// Fetches a given number of macro blocks, starting at a specific block (by its hash).
/// It can fetch only election macro blocks if desired.
fn get_macro_blocks(
Expand Down
38 changes: 7 additions & 31 deletions blockchain/src/blockchain/slots.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use nimiq_blockchain_interface::BlockchainError;
use nimiq_collections::BitSet;
use nimiq_blockchain_interface::{AbstractBlockchain, BlockchainError};
use nimiq_database::TransactionProxy;
use nimiq_primitives::{
policy::Policy,
slots_allocation::{Slot, Validators},
};
use nimiq_vrf::{Rng, VrfEntropy, VrfSeed, VrfUseCase};
use nimiq_vrf::{VrfEntropy, VrfSeed};

use crate::Blockchain;

Expand Down Expand Up @@ -72,7 +71,11 @@ impl Blockchain {
.next_batch_initial_punished_set;

// Compute the slot number of the next proposer.
let slot_number = Self::compute_slot_number(offset, vrf_entropy, disabled_slots);
let slot_number = <Blockchain as AbstractBlockchain>::compute_slot_number(
offset,
vrf_entropy,
disabled_slots,
);

// Fetch the validators that are active in given block's epoch.
let epoch_number = Policy::epoch_at(block_number);
Expand All @@ -90,31 +93,4 @@ impl Blockchain {
validator: validator.clone(),
})
}

fn compute_slot_number(offset: u32, vrf_entropy: VrfEntropy, disabled_slots: BitSet) -> u16 {
// RNG for slot selection
let mut rng = vrf_entropy.rng(VrfUseCase::ViewSlotSelection);

// Create a list of viable slots.
let mut slots: Vec<u16> = if disabled_slots.len() == Policy::SLOTS as usize {
// If all slots are disabled, we will accept any slot, since we want the
// chain to progress.
(0..Policy::SLOTS).collect()
} else {
// Otherwise, we will only accept slots that are not disabled.
(0..Policy::SLOTS)
.filter(|slot| !disabled_slots.contains(*slot as usize))
.collect()
};

// Shuffle the slots vector using the Fisher–Yates shuffle.
for i in (1..slots.len()).rev() {
let r = rng.next_u64_below((i + 1) as u64) as usize;
slots.swap(r, i);
}

// Now simply take the offset modulo the number of viable slots and that will give us
// the chosen slot.
slots[offset as usize % slots.len()]
}
}
1 change: 0 additions & 1 deletion light-blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ tokio-stream = { version = "0.1", features = ["sync"] }

nimiq-block = { workspace = true }
nimiq-blockchain-interface = { workspace = true }
nimiq-collections = { workspace = true }
nimiq-genesis = { workspace = true, default-features = false }
nimiq-hash = { workspace = true }
nimiq-keys = { workspace = true }
Expand Down
33 changes: 2 additions & 31 deletions light-blockchain/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use nimiq_block::{Block, MacroBlock};
use nimiq_blockchain_interface::{
AbstractBlockchain, BlockchainError, BlockchainEvent, ChainInfo, ForkEvent,
};
use nimiq_collections::BitSet;
use nimiq_genesis::NetworkInfo;
use nimiq_primitives::{
networks::NetworkId,
policy::Policy,
slots_allocation::{Slot, Validators},
};
use nimiq_utils::time::OffsetTime;
use nimiq_vrf::{Rng, VrfEntropy, VrfUseCase};
use nimiq_vrf::VrfEntropy;
use tokio::sync::broadcast::{channel as broadcast, Sender as BroadcastSender};

use crate::chain_store::ChainStore;
Expand Down Expand Up @@ -101,7 +100,7 @@ impl LightBlockchain {
}
}

// FIXME Duplicated function (also exists in full blockchain)
// A similar function exists in full blockchain, however the light blockchain does not use any database txn.
pub fn get_proposer(
&self,
block_number: u32,
Expand Down Expand Up @@ -136,32 +135,4 @@ impl LightBlockchain {
validator: validator.clone(),
})
}

// FIXME Duplicated function (also exists in full blockchain)
fn compute_slot_number(offset: u32, vrf_entropy: VrfEntropy, disabled_slots: BitSet) -> u16 {
// RNG for slot selection
let mut rng = vrf_entropy.rng(VrfUseCase::ViewSlotSelection);

// Create a list of viable slots.
let mut slots: Vec<u16> = if disabled_slots.len() == Policy::SLOTS as usize {
// If all slots are disabled, we will accept any slot, since we want the
// chain to progress.
(0..Policy::SLOTS).collect()
} else {
// Otherwise, we will only accept slots that are not disabled.
(0..Policy::SLOTS)
.filter(|slot| !disabled_slots.contains(*slot as usize))
.collect()
};

// Shuffle the slots vector using the Fisher–Yates shuffle.
for i in (1..slots.len()).rev() {
let r = rng.next_u64_below((i + 1) as u64) as usize;
slots.swap(r, i);
}

// Now simply take the offset modulo the number of viable slots and that will give us
// the chosen slot.
slots[offset as usize % slots.len()]
}
}

0 comments on commit 9e3c822

Please sign in to comment.