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

refactor shard_ids config #137

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config/sample.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
log_format = "json"
id = 1

[fnames]
disable = false
stop_at = 9999999999
start_from = 0
url = "https://fnames.farcaster.xyz/transfers"

[consensus]
num_shards = 1
shard_ids = [1]
24 changes: 6 additions & 18 deletions src/consensus/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl<Ctx: Context + SnapchainContext> From<TimeoutElapsed<Timeout>> for Consensu
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub private_key: String,
pub shard_ids: String,
pub num_shards: u32,
pub shard_ids: Vec<u32>,

#[serde(with = "humantime_serde")]
pub propose_value_delay: Duration,
Expand All @@ -62,25 +63,11 @@ impl Config {
Keypair::from(secret_key.unwrap())
}

pub fn shard_ids(&self) -> Vec<u32> {
self.shard_ids
.split(',')
.map(|s| s.parse().unwrap())
.collect()
}

pub fn num_shards(&self) -> u32 {
self.shard_ids().len() as u32
}

pub fn with_shard_ids(&self, shard_ids: Vec<u32>) -> Self {
Self {
private_key: self.private_key.clone(),
shard_ids: shard_ids
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
.join(","),
num_shards: shard_ids.len() as u32,
shard_ids,
propose_value_delay: self.propose_value_delay,
max_messages_per_block: self.max_messages_per_block,
}
Expand All @@ -91,7 +78,8 @@ impl Default for Config {
fn default() -> Self {
Self {
private_key: hex::encode(SecretKey::generate()),
shard_ids: "1".to_string(),
shard_ids: vec![1],
num_shards: 1,
propose_value_delay: Duration::from_millis(250),
max_messages_per_block: 250, //TODO
}
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Every 5 ticks, re-register the validators so that new nodes can discover each other
if tick_count % 5 == 0 {
let nonce = tick_count as u64;
for i in 0..=app_config.consensus.num_shards() {

let ids = {
// prepend 0 to shard_ids for the following loop
let mut ids = vec![0u32];
ids.extend(&app_config.consensus.shard_ids);
ids
};

for i in ids {
let current_height =
if i == 0 {
block_store.max_block_number().unwrap_or_else(|_| 0)
Expand Down
4 changes: 2 additions & 2 deletions src/node/snapchain_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SnapchainNode {
let mut shard_stores: HashMap<u32, Stores> = HashMap::new();

// Create the shard validators
for shard_id in config.shard_ids() {
for shard_id in config.shard_ids {
if shard_id == 0 {
panic!("Shard ID 0 is reserved for the block shard, created automaticaly");
} else if shard_id > MAX_SHARDS {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl SnapchainNode {
validator_address.clone(),
block_shard.clone(),
shard_decision_rx,
config.num_shards(),
config.num_shards,
block_tx,
engine,
);
Expand Down
Loading