Skip to content

Commit

Permalink
feat(versioning): include protocol versions in Version message
Browse files Browse the repository at this point in the history
  • Loading branch information
drcpu-github committed Dec 4, 2024
1 parent 200cb90 commit 8d6d619
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 32 deletions.
15 changes: 14 additions & 1 deletion data_structures/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use crate::{
SuperBlockVote,
},
error::BuildersError,
get_protocol_version_activation_epoch, get_protocol_version_period,
proto::versioning::ProtocolVersion,
strum::IntoEnumIterator,
transaction::Transaction,
types::{
Address, Command, GetPeers, InventoryAnnouncement, InventoryRequest, IpAddress, LastBeacon,
Message, Peers, Verack, Version,
Message, Peers, ProtocolVersion as ProtocolVersionType, Verack, Version,
},
};

Expand Down Expand Up @@ -59,6 +62,15 @@ impl Message {
beacon: LastBeacon,
) -> Message {
let addr = sender_addr.map(to_address);

let mut protocol_versions = vec![];
for protocol in ProtocolVersion::iter() {
protocol_versions.push(ProtocolVersionType {
version: protocol.into(),
activation_epoch: get_protocol_version_activation_epoch(protocol),
checkpoint_period: get_protocol_version_period(protocol),
});
}
Message::build_message(
magic,
Command::Version(Version {
Expand All @@ -70,6 +82,7 @@ impl Message {
user_agent: user_agent(),
nonce: random_nonce(),
beacon,
protocol_versions,
}),
)
}
Expand Down
12 changes: 11 additions & 1 deletion data_structures/src/proto/versioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
},
ProtobufConvert,
},
types::Message,
types::{Message, ProtocolVersionName},
};

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
Expand Down Expand Up @@ -167,6 +167,16 @@ impl PartialOrd for ProtocolVersion {
}
}

impl From<ProtocolVersionName> for ProtocolVersion {
fn from(version: ProtocolVersionName) -> Self {
match version {
ProtocolVersionName::V1_7(_) => ProtocolVersion::V1_7,
ProtocolVersionName::V1_8(_) => ProtocolVersion::V1_8,
ProtocolVersionName::V2_0(_) => ProtocolVersion::V2_0,
}
}
}

pub trait Versioned: ProtobufConvert {
type LegacyType: protobuf::Message;

Expand Down
52 changes: 50 additions & 2 deletions data_structures/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use serde::{Deserialize, Serialize};

use crate::{
chain::{Block, CheckpointBeacon, Hashable, InventoryEntry, SuperBlock, SuperBlockVote},
proto::{schema::witnet, ProtobufConvert},
proto::{
schema::witnet, versioning::ProtocolVersion as VersioningProtocolVersion, ProtobufConvert,
},
transaction::Transaction,
};

Expand Down Expand Up @@ -50,7 +52,26 @@ impl fmt::Display for Command {
Command::GetPeers(_) => f.write_str("GET_PEERS"),
Command::Peers(_) => f.write_str("PEERS"),
Command::Verack(_) => f.write_str("VERACK"),
Command::Version(_) => f.write_str("VERSION"),
Command::Version(Version {
version: v,
sender_address: sa,
protocol_versions: pv,
..
}) => {
let mut protocol_versions_str = String::from("(");
for protocol in pv {
protocol_versions_str.push_str(&format!(
"(version: {:?}, activation_epoch: {}, period: {}),",
protocol.version, protocol.activation_epoch, protocol.checkpoint_period
));
}
protocol_versions_str.push_str(")");
write!(
f,
"VERSION MESSAGE: version = {}, sender_address = {:?}, protocol_versions = {}",
v, sa, protocol_versions_str,
)
}
Command::Block(block) => write!(
f,
"BLOCK #{}: {}",
Expand Down Expand Up @@ -112,6 +133,32 @@ pub struct Peers {
#[protobuf_convert(pb = "witnet::Verack")]
pub struct Verack;

#[derive(Clone, Copy, Debug, Eq, PartialEq, ProtobufConvert)]
#[protobuf_convert(pb = "witnet::ProtocolVersionName")]
pub enum ProtocolVersionName {
V1_7(bool),
V1_8(bool),
V2_0(bool),
}

impl From<VersioningProtocolVersion> for ProtocolVersionName {
fn from(version: VersioningProtocolVersion) -> Self {
match version {
VersioningProtocolVersion::V1_7 => ProtocolVersionName::V1_7(true),
VersioningProtocolVersion::V1_8 => ProtocolVersionName::V1_8(true),
VersioningProtocolVersion::V2_0 => ProtocolVersionName::V2_0(true),
}
}
}

#[derive(Debug, Eq, PartialEq, Clone, ProtobufConvert)]
#[protobuf_convert(pb = "witnet::ProtocolVersion")]
pub struct ProtocolVersion {
pub version: ProtocolVersionName,
pub activation_epoch: u32,
pub checkpoint_period: u16,
}

#[derive(Debug, Eq, PartialEq, Clone, ProtobufConvert)]
#[protobuf_convert(pb = "witnet::Version")]
pub struct Version {
Expand All @@ -123,6 +170,7 @@ pub struct Version {
pub user_agent: String,
pub nonce: u64,
pub beacon: LastBeacon,
pub protocol_versions: Vec<ProtocolVersion>,
}

///////////////////////////////////////////////////////////
Expand Down
14 changes: 12 additions & 2 deletions node/src/actors/chain_manager/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use crate::{
GetMempoolResult, GetNodeStats, GetProtocolInfo, GetReputation, GetReputationResult,
GetSignalingInfo, GetState, GetSuperBlockVotes, GetSupplyInfo, GetUtxoInfo,
IsConfirmedBlock, PeersBeacons, QueryStake, ReputationStats, Rewind, SendLastBeacon,
SessionUnitResult, SetLastBeacon, SetPeersLimits, SignalingInfo, SnapshotExport,
SnapshotImport, TryMineBlock,
SessionUnitResult, SetEpochConstants, SetLastBeacon, SetPeersLimits, SignalingInfo,
SnapshotExport, SnapshotImport, TryMineBlock,
},
sessions_manager::SessionsManager,
},
Expand Down Expand Up @@ -2132,6 +2132,16 @@ impl Handler<SnapshotImport> for ChainManager {
Box::pin(fut)
}
}

impl Handler<SetEpochConstants> for ChainManager {
type Result = ();

fn handle(&mut self, msg: SetEpochConstants, _ctx: &mut Context<Self>) -> Self::Result {
log::debug!("Received new epoch constants: {:?}", msg.epoch_constants);
self.epoch_constants = Some(msg.epoch_constants);
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum BlockBatches<T> {
TargetNotReached(Vec<T>),
Expand Down
2 changes: 2 additions & 0 deletions node/src/actors/epoch_manager/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Handler<SetEpochConstants> for EpochManager {
type Result = ();

fn handle(&mut self, msg: SetEpochConstants, _ctx: &mut Context<Self>) -> Self::Result {
log::debug!("Received new epoch constants: {:?}", msg.epoch_constants);

// Check if the epoch calculated with the current version of the epoch constants
// and the last_checked_epoch are different and if they are, subtract that difference
// from the new last_checked_epoch.
Expand Down
15 changes: 14 additions & 1 deletion node/src/actors/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use witnet_data_structures::{
UnstakeTransaction, VTTransaction,
},
transaction_factory::NodeBalance,
types::LastBeacon,
types::{LastBeacon, ProtocolVersion},
utxo_pool::{UtxoInfo, UtxoSelectionStrategy},
wit::Wit,
};
Expand Down Expand Up @@ -1472,6 +1472,19 @@ impl Message for crate::actors::messages::GetProtocolInfo {
type Result = Result<Option<ProtocolInfo>, failure::Error>;
}

/// Message indicating the last beacon received from a peer
#[derive(Clone, Debug)]
pub struct SendProtocolVersions {
/// Socket address which identifies the peer
pub address: SocketAddr,
/// Protocol versions received from peer
pub protocol_versions: Vec<ProtocolVersion>,
}

impl Message for SendProtocolVersions {
type Result = ();
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
/// A value that can either be L, R, where an R can always be obtained through the `do_magic` method.
Expand Down
Loading

0 comments on commit 8d6d619

Please sign in to comment.