Skip to content

Commit c001a8c

Browse files
committed
Use lazy_static to evaluate the chain config
This is to make messages network-agnostic and keep the interface of each message clean as they don't really care what network is in use when they are being created. Saving the chain config to a lazily evaluated static variable allows the chain config data to be specified at the start-up but also allows different subsystems to use the values as if they were static variables.
1 parent a9098ec commit c001a8c

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
lazy_static = "1.4.0"
1011
fixed-hash = { version = "0.7.0", default-features = true, features = ["std"] }
1112
bech32 = "0.8.1"
1213
thiserror = "1.0.30"

common/src/chain/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct ChainConfig {
2626
#[allow(dead_code)]
2727
height_checkpoint_data: BTreeMap<BlockHeight, HashType>,
2828
#[allow(dead_code)]
29-
magic_bytes: [u8; 4],
29+
pub magic_bytes: [u8; 4],
3030
}
3131

3232
impl ChainConfig {
@@ -35,6 +35,10 @@ impl ChainConfig {
3535
}
3636
}
3737

38+
lazy_static::lazy_static! {
39+
pub static ref CHAIN_CONFIG: ChainConfig = create_mainnet();
40+
}
41+
3842
#[allow(dead_code)]
3943
pub fn create_mainnet() -> ChainConfig {
4044
ChainConfig {

p2p/src/message.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// limitations under the License.
1515
//
1616
// Author(s): A. Altonen
17-
use common::chain::config::{ChainType, MAGIC_BYTES};
17+
use common::chain::config::CHAIN_CONFIG;
1818
use parity_scale_codec::{Decode, Encode};
1919
use util::Message;
2020

@@ -40,8 +40,6 @@ pub struct Message {
4040
pub struct Hello {
4141
/// Version of the software
4242
version: u32,
43-
/// Network ID
44-
network: ChainType,
4543
/// Services provided by the node
4644
services: u32,
4745
/// Unix timestamp
@@ -52,8 +50,6 @@ pub struct Hello {
5250
pub struct HelloAck {
5351
/// Version of the software
5452
version: u32,
55-
/// Network ID
56-
network: ChainType,
5753
/// Services provided by the node
5854
services: u32,
5955
/// Unix timestamp
@@ -73,7 +69,7 @@ mod tests {
7369
let timestamp: u64 =
7470
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
7571

76-
let hello = Hello::new(version, ChainType::Mainnet, services, timestamp);
72+
let hello = Hello::new(version, services, timestamp);
7773
let msg: Message = hello.clone().into();
7874

7975
// Hello message cannot be converted to HelloAck message
@@ -93,10 +89,10 @@ mod tests {
9389
let timestamp: u64 =
9490
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
9591

96-
let hello_ack = HelloAck::new(version, ChainType::Mainnet, services, timestamp);
92+
let hello_ack = HelloAck::new(version, services, timestamp);
9793
let msg: Message = hello_ack.clone().into();
9894

99-
// Hello message cannot be converted to HelloAck message
95+
// HelloAck message cannot be converted to Hello message
10096
// even if the representation of the messages is exactly the same
10197
assert_eq!(
10298
Hello::try_from(msg.clone()),

p2p/src/util/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn message(input: TokenStream) -> TokenStream {
6363
fn into(self) -> Message {
6464
let encoded = self.encode();
6565
Message {
66-
magic: MAGIC_BYTES,
66+
magic: CHAIN_CONFIG.magic_bytes,
6767
msg_type: MessageType::#ident,
6868
size: encoded.len() as u32,
6969
payload: encoded,

0 commit comments

Comments
 (0)