From 4ce13cc301276dbde29eea8b466f8a85af9534e6 Mon Sep 17 00:00:00 2001 From: qima Date: Wed, 13 Nov 2024 23:33:00 +0800 Subject: [PATCH] fix(node): initialize records_by_bucket properly --- sn_networking/src/record_store.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sn_networking/src/record_store.rs b/sn_networking/src/record_store.rs index e3eb672d6c..5d3ce39b61 100644 --- a/sn_networking/src/record_store.rs +++ b/sn_networking/src/record_store.rs @@ -371,13 +371,25 @@ impl NodeRecordStore { }; let records = Self::update_records_from_an_existing_store(&config, &encryption_details); + let local_address = NetworkAddress::from_peer(local_id); + + // Initialize records_by_bucket + let mut records_by_bucket: HashMap> = HashMap::new(); + for (key, (addr, _record_type)) in records.iter() { + let distance = local_address.distance(&addr); + let bucket = distance.ilog2().unwrap_or_default(); + records_by_bucket + .entry(bucket) + .or_default() + .insert(key.clone()); + } let cache_size = config.records_cache_size; let mut record_store = NodeRecordStore { - local_address: NetworkAddress::from_peer(local_id), + local_address, config, records, - records_by_bucket: HashMap::new(), + records_by_bucket, records_cache: RecordCache::new(cache_size), network_event_sender, local_swarm_cmd_sender: swarm_cmd_sender,