Skip to content

Commit

Permalink
Version bump to 4, updates ping message format
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Nov 20, 2022
1 parent 268a803 commit b0efbd4
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 15 deletions.
12 changes: 7 additions & 5 deletions node/messages/src/block_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use super::*;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlockRequest {
pub start_block_height: u32,
pub end_block_height: u32,
/// The starting block height (inclusive).
pub start_height: u32,
/// The ending block height (exclusive).
pub end_height: u32,
}

impl MessageTrait for BlockRequest {
Expand All @@ -32,16 +34,16 @@ impl MessageTrait for BlockRequest {
/// Serializes the message into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
Ok(bincode::serialize_into(writer, &(self.start_block_height, self.end_block_height))?)
Ok(bincode::serialize_into(writer, &(self.start_height, self.end_height))?)
}

/// Deserializes the given buffer into a message.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();
Ok(Self {
start_block_height: bincode::deserialize_from(&mut reader)?,
end_block_height: bincode::deserialize_from(&mut reader)?,
start_height: bincode::deserialize_from(&mut reader)?,
end_height: bincode::deserialize_from(&mut reader)?,
})
}
}
7 changes: 3 additions & 4 deletions node/messages/src/helpers/noise_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,8 @@ mod tests {

#[test]
fn block_request_roundtrip() {
let block_request = MessageOrBytes::Message(Box::new(Message::BlockRequest(BlockRequest {
start_block_height: 0,
end_block_height: 100,
})));
let block_request =
MessageOrBytes::Message(Box::new(Message::BlockRequest(BlockRequest { start_height: 0, end_height: 100 })));

assert_roundtrip(block_request);
}
Expand Down Expand Up @@ -392,6 +390,7 @@ mod tests {
fork_depth: 0,
node_type: NodeType::Client,
status: Status::Ready,
block_height: Some(0),
})));

assert_roundtrip(ping)
Expand Down
2 changes: 1 addition & 1 deletion node/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub enum Message<N: Network> {

impl<N: Network> Message<N> {
/// The version of the network protocol; it can be incremented in order to force users to update.
pub const VERSION: u32 = 3;
pub const VERSION: u32 = 4;

/// Returns the message name.
#[inline]
Expand Down
10 changes: 7 additions & 3 deletions node/messages/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Ping {
pub fork_depth: u32,
pub node_type: NodeType,
pub status: Status,
pub block_height: Option<u32>,
}

impl MessageTrait for Ping {
Expand All @@ -34,14 +35,17 @@ impl MessageTrait for Ping {
/// Serializes the message into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
Ok(bincode::serialize_into(&mut *writer, &(self.version, self.fork_depth, self.node_type, self.status))?)
Ok(bincode::serialize_into(
&mut *writer,
&(self.version, self.fork_depth, self.node_type, self.status, self.block_height),
)?)
}

/// Deserializes the given buffer into a message.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();
let (version, fork_depth, node_type, status) = bincode::deserialize_from(&mut reader)?;
Ok(Self { version, fork_depth, node_type, status })
let (version, fork_depth, node_type, status, block_height) = bincode::deserialize_from(&mut reader)?;
Ok(Self { version, fork_depth, node_type, status, block_height })
}
}
1 change: 1 addition & 0 deletions node/router/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<N: Network> Router<N> {
fork_depth: ALEO_MAXIMUM_FORK_DEPTH,
node_type: self.node_type,
status: self.status.get(),
block_height: None,
});
trace!("Sending '{}' to '{peer_ip}'", message.name());
framed.send(message).await?;
Expand Down
11 changes: 9 additions & 2 deletions node/router/src/helpers/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use snarkos_node_tcp::ConnectionSide;
use parking_lot::RwLock;
use std::{
net::SocketAddr,
sync::{atomic::AtomicU32, Arc},
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
time::Instant,
};

Expand All @@ -41,7 +44,6 @@ pub struct Peer {
/// The node type of the peer.
status: RawStatus,
/// The block height of the peer.
#[allow(dead_code)]
block_height: Arc<AtomicU32>,
}

Expand Down Expand Up @@ -121,4 +123,9 @@ impl Peer {
pub fn set_status(&mut self, status: RawStatus) {
self.status = status
}

/// Updates the block height.
pub fn set_block_height(&self, block_height: u32) {
self.block_height.store(block_height, Ordering::SeqCst);
}
}
7 changes: 7 additions & 0 deletions node/router/src/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ use std::{

#[async_trait]
pub trait Inbound<N: Network>: Reading + Outbound<N> {
/// The maximum number of blocks that can be requested per `BlockRequest`.
const MAXIMUM_BLOCK_REQUEST: u32 = 10;
/// The maximum number of puzzle requests per interval.
const MAXIMUM_PUZZLE_REQUESTS_PER_INTERVAL: usize = 5;
/// The duration in seconds to sleep in between ping requests with a connected peer.
Expand Down Expand Up @@ -304,6 +306,10 @@ pub trait Inbound<N: Network>: Reading + Outbound<N> {
peer.set_node_type(message.node_type);
// Update the status of the peer.
peer.set_status(RawStatus::from_status(message.status));
// Update the block height of the peer.
if let Some(block_height) = message.block_height {
peer.set_block_height(block_height);
}
});

// // Determine if the peer is on a fork (or unknown).
Expand Down Expand Up @@ -344,6 +350,7 @@ pub trait Inbound<N: Network>: Reading + Outbound<N> {
fork_depth: ALEO_MAXIMUM_FORK_DEPTH,
node_type: self_clone.router().node_type(),
status: self_clone.router().status(),
block_height: None,
});

// Send a `Ping` message to the peer.
Expand Down
51 changes: 51 additions & 0 deletions run-validator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
# USAGE examples:
# CLI with env vars: VALIDATOR_PRIVATE_KEY=APrivateKey1... ./run-validator.sh
# CLI with prompts for vars: ./run-validator.sh

# If the env var VALIDATOR_PRIVATE_KEY is not set, prompt for it
if [ -z "${VALIDATOR_PRIVATE_KEY}" ]
then
read -r -p "Enter the Aleo Validator account private key: "
VALIDATOR_PRIVATE_KEY=$REPLY
fi

if [ "${VALIDATOR_PRIVATE_KEY}" == "" ]
then
VALIDATOR_PRIVATE_KEY="APrivateKey1zkp8cC4jgHEBnbtu3xxs1Ndja2EMizcvTRDq5Nikdkukg1p"
fi

COMMAND="cargo run --release -- start --nodisplay --validator ${VALIDATOR_PRIVATE_KEY}"

for word in $*;
do
COMMAND="${COMMAND} ${word}"
done

function exit_node()
{
echo "Exiting..."
kill $!
exit
}

trap exit_node SIGINT

echo "Running an Aleo Validator node..."
$COMMAND &

while :
do
echo "Checking for updates..."
git stash
rm Cargo.lock
STATUS=$(git pull)

if [ "$STATUS" != "Already up to date." ]; then
echo "Updated code found, rebuilding and relaunching validator"
cargo clean
kill -INT $!; sleep 2; $COMMAND &
fi

sleep 1800;
done

0 comments on commit b0efbd4

Please sign in to comment.