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

[proto] update protocol version 28 #23

Merged
merged 11 commits into from
Sep 5, 2023
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
4 changes: 0 additions & 4 deletions crates/avalanche-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ prometheus = { version = "0.13.3", default-features = false, features = ["proces
base64 = { version = "0.21.2", optional = true } # https://github.com/marshallpierce/rust-base64
num-bigint = { version = "0.4.3", optional = true }

[build-dependencies]
protoc-gen-prost = "0.2.3"
protoc-gen-tonic = "0.3.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now using community plugins which are hosted by buf


[dev-dependencies]
env_logger = "0.10.0"
id-manager = "0.0.3"
Expand Down
31 changes: 9 additions & 22 deletions crates/avalanche-types/scripts/protobuf_codegen.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
#!/usr/bin/env bash

# This script is used to generate the protobuf stubs for the avalanche-types
# crate.

# protocol version is the version of the gRPC proto definitions
# as defined by the avalanchego rpcchainvm.
# ref. https://github.com/ava-labs/avalanchego/blob/v1.9.11/version/constants.go#L15-L17
PROTOCOL_VERSION='26'
PROTOCOL_VERSION='28'

if ! [[ "$0" =~ scripts/protobuf_codegen.sh ]]; then
echo "must be run from repository root"
exit 255
fi

# ref. https://docs.buf.build/installation
BUF_VERSION='1.19.0'
BUF_VERSION='1.26.1'
if [[ $(buf --version | cut -f2 -d' ') != "${BUF_VERSION}" ]]; then
echo "could not find buf ${BUF_VERSION}, is it installed + in PATH?"
exit 255
fi

# protoc plugin "protoc-gen-prost" is required
# protoc-gen-prost and protoc-gen-tonic are now community modules hosted by buf
# and not required by this script.
#
# e.g.,
# cargo install protoc-gen-prost --version 0.2.2
# ref. https://crates.io/crates/protoc-gen-prost
PROTOC_GEN_PROST_VERSION=0.2.2
if [[ $(protoc-gen-prost --version | cut -f2 -d' ') != "${PROTOC_GEN_PROST_VERSION}" ]]; then
echo "could not find protoc-gen-prost version ${PROTOC_GEN_PROST_VERSION} is it installed + in PATH?"
exit 255
fi

# protoc plugin "protoc-gen-tonic" is required
#
# e.g.,
# cargo install protoc-gen-tonic --version 0.2.2
# ref. https://crates.io/crates/protoc-gen-tonic
PROTOC_GEN_TONIC_VERSION=0.2.2
if [[ $(protoc-gen-tonic --version | cut -f2 -d' ') != "${PROTOC_GEN_TONIC_VERSION}" ]]; then
echo "could not find protoc-gen-tonic version ${PROTOC_GEN_TONIC_VERSION} is it installed + in PATH?"
exit 255
fi
# ref. https://buf.build/community/neoeinstein-tonic
# ref. https://buf.build/community/neoeinstein-prost

# protoc plugin "protoc-gen-prost-crate" is required
#
Expand Down
41 changes: 13 additions & 28 deletions crates/avalanche-types/src/message/accepted_frontier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{self, Error, ErrorKind};

use crate::{ids, message, proto::pb::p2p};
use prost::bytes::Bytes;
use prost::Message as ProstMessage;

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -19,17 +20,17 @@ impl Message {
pub fn default() -> Self {
Message {
msg: p2p::AcceptedFrontier {
chain_id: prost::bytes::Bytes::new(),
chain_id: Bytes::new(),
request_id: 0,
container_ids: Vec::new(),
container_id: Bytes::new(),
},
gzip_compress: false,
}
}

#[must_use]
pub fn chain_id(mut self, chain_id: ids::Id) -> Self {
self.msg.chain_id = prost::bytes::Bytes::from(chain_id.to_vec());
self.msg.chain_id = Bytes::from(chain_id.to_vec());
self
}

Expand All @@ -40,13 +41,8 @@ impl Message {
}

#[must_use]
pub fn container_ids(mut self, container_ids: Vec<ids::Id>) -> Self {
let mut container_ids_bytes: Vec<prost::bytes::Bytes> =
Vec::with_capacity(container_ids.len());
for id in container_ids.iter() {
container_ids_bytes.push(prost::bytes::Bytes::from(id.to_vec()));
}
self.msg.container_ids = container_ids_bytes;
pub fn container_id(mut self, id: ids::Id) -> Self {
self.msg.container_id = Bytes::from(id.to_vec());
self
}

Expand All @@ -68,9 +64,9 @@ impl Message {
let uncompressed_len = encoded.len();
let compressed = message::compress::pack_gzip(&encoded)?;
let msg = p2p::Message {
message: Some(p2p::message::Message::CompressedGzip(
prost::bytes::Bytes::from(compressed),
)),
message: Some(p2p::message::Message::CompressedGzip(Bytes::from(
compressed,
))),
};

let compressed_len = msg.encoded_len();
Expand Down Expand Up @@ -109,7 +105,7 @@ impl Message {
p2p::message::Message::CompressedGzip(msg) => {
let decompressed = message::compress::unpack_gzip(msg.as_ref())?;
let decompressed_msg: p2p::Message =
ProstMessage::decode(prost::bytes::Bytes::from(decompressed)).map_err(|e| {
ProstMessage::decode(Bytes::from(decompressed)).map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("failed prost::Message::decode '{}'", e),
Expand Down Expand Up @@ -146,20 +142,9 @@ fn test_message() {
&random_manager::secure_bytes(32).unwrap(),
))
.request_id(random_manager::u32())
.container_ids(vec![
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::from_slice(&random_manager::secure_bytes(32).unwrap()),
ids::Id::from_slice(&random_manager::secure_bytes(32).unwrap()),
]);
.container_id(ids::Id::from_slice(
&random_manager::secure_bytes(32).unwrap(),
));

let data1 = msg1_with_no_compression.serialize().unwrap();
let msg1_with_no_compression_deserialized = Message::deserialize(&data1).unwrap();
Expand Down
43 changes: 14 additions & 29 deletions crates/avalanche-types/src/message/chits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{self, Error, ErrorKind};

use crate::{ids, message, proto::pb::p2p};
use prost::bytes::Bytes;
use prost::Message as ProstMessage;

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -19,18 +20,18 @@ impl Message {
pub fn default() -> Self {
Message {
msg: p2p::Chits {
chain_id: prost::bytes::Bytes::new(),
chain_id: Bytes::new(),
request_id: 0,
preferred_container_ids: Vec::new(),
accepted_container_ids: Vec::new(),
preferred_id: Bytes::new(),
accepted_id: Bytes::new(),
},
gzip_compress: false,
}
}

#[must_use]
pub fn chain_id(mut self, chain_id: ids::Id) -> Self {
self.msg.chain_id = prost::bytes::Bytes::from(chain_id.to_vec());
self.msg.chain_id = Bytes::from(chain_id.to_vec());
self
}

Expand All @@ -41,13 +42,8 @@ impl Message {
}

#[must_use]
pub fn container_ids(mut self, container_ids: Vec<ids::Id>) -> Self {
let mut container_ids_bytes: Vec<prost::bytes::Bytes> =
Vec::with_capacity(container_ids.len());
for id in container_ids.iter() {
container_ids_bytes.push(prost::bytes::Bytes::from(id.to_vec()));
}
self.msg.preferred_container_ids = container_ids_bytes;
pub fn container_id(mut self, id: ids::Id) -> Self {
self.msg.preferred_id = Bytes::from(id.to_vec());
self
}

Expand All @@ -69,9 +65,9 @@ impl Message {
let uncompressed_len = encoded.len();
let compressed = message::compress::pack_gzip(&encoded)?;
let msg = p2p::Message {
message: Some(p2p::message::Message::CompressedGzip(
prost::bytes::Bytes::from(compressed),
)),
message: Some(p2p::message::Message::CompressedGzip(Bytes::from(
compressed,
))),
};

let compressed_len = msg.encoded_len();
Expand Down Expand Up @@ -110,7 +106,7 @@ impl Message {
p2p::message::Message::CompressedGzip(msg) => {
let decompressed = message::compress::unpack_gzip(msg.as_ref())?;
let decompressed_msg: p2p::Message =
ProstMessage::decode(prost::bytes::Bytes::from(decompressed)).map_err(|e| {
ProstMessage::decode(Bytes::from(decompressed)).map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("failed prost::Message::decode '{}'", e),
Expand Down Expand Up @@ -147,20 +143,9 @@ fn test_message() {
&random_manager::secure_bytes(32).unwrap(),
))
.request_id(random_manager::u32())
.container_ids(vec![
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::empty(),
ids::Id::from_slice(&random_manager::secure_bytes(32).unwrap()),
ids::Id::from_slice(&random_manager::secure_bytes(32).unwrap()),
]);
.container_id(ids::Id::from_slice(
&random_manager::secure_bytes(32).unwrap(),
));

let data1 = msg1_with_no_compression.serialize().unwrap();
let msg1_with_no_compression_deserialized = Message::deserialize(&data1).unwrap();
Expand Down
17 changes: 12 additions & 5 deletions crates/avalanche-types/src/message/ping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{self, Error, ErrorKind};

use crate::{message, proto::pb::p2p};
use prost::bytes::Bytes;
use prost::Message as ProstMessage;

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -18,7 +19,13 @@ impl Default for Message {
impl Message {
pub fn default() -> Self {
Message {
msg: p2p::Ping {},
msg: p2p::Ping {
uptime: 0,
subnet_uptimes: vec![p2p::SubnetUptime {
subnet_id: Bytes::new(),
uptime: 0,
}],
},
gzip_compress: false,
}
}
Expand All @@ -41,9 +48,9 @@ impl Message {
let uncompressed_len = encoded.len();
let compressed = message::compress::pack_gzip(&encoded)?;
let msg = p2p::Message {
message: Some(p2p::message::Message::CompressedGzip(
prost::bytes::Bytes::from(compressed),
)),
message: Some(p2p::message::Message::CompressedGzip(Bytes::from(
compressed,
richardpringle marked this conversation as resolved.
Show resolved Hide resolved
))),
};

let compressed_len = msg.encoded_len();
Expand Down Expand Up @@ -82,7 +89,7 @@ impl Message {
p2p::message::Message::CompressedGzip(msg) => {
let decompressed = message::compress::unpack_gzip(msg.as_ref())?;
let decompressed_msg: p2p::Message =
ProstMessage::decode(prost::bytes::Bytes::from(decompressed)).map_err(|e| {
ProstMessage::decode(Bytes::from(decompressed)).map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("failed prost::Message::decode '{}'", e),
Expand Down
21 changes: 21 additions & 0 deletions crates/avalanche-types/src/proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Avalanche-rs Proto

Now Serving: **Protocol Version 28**

Protobuf files are hosted at
[https://buf.build/ava-labs/avalanche](https://buf.build/ava-labs/avalanche) and
can be used as dependencies in other projects.

Protobuf linting and generation for this project is managed by
[buf](https://github.com/bufbuild/buf).

Please find installation instructions on
[https://docs.buf.build/installation/](https://docs.buf.build/installation/) or
use `Dockerfile.buf` provided in the `proto/` directory of AvalancheGo.

Introduction to `buf`
[https://docs.buf.build/tour/introduction](https://docs.buf.build/tour/introduction)

To update the protocol version update the `PROTOCOL_VERSION` environment variable
in `scripts/protobuf_codegen.sh` and `mod.rs` then run the script.

6 changes: 3 additions & 3 deletions crates/avalanche-types/src/proto/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: v1
plugins:
- name: prost
- plugin: buf.build/community/neoeinstein-prost:v0.2.3
out: pb
opt:
- bytes=.
Expand All @@ -9,13 +9,13 @@ plugins:
# https://github.com/neoeinstein/protoc-gen-prost/blob/main/protoc-gen-prost/README.md#protoc-gen-prost
- file_descriptor_set

- name: tonic
- plugin: buf.build/community/neoeinstein-tonic:v0.3.0
out: pb
opt:
# https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method.compile_well_known_types
- compile_well_known_types

- name: prost-crate
- name: prost-crate # remote plugin not supported yet
out: pb
strategy: all
opt:
Expand Down
2 changes: 1 addition & 1 deletion crates/avalanche-types/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pub mod pb;
pub use pb::*;

/// ref. <https://github.com/ava-labs/avalanchego/blob/v1.10.1/version/constants.go#L15-L17>
pub const PROTOCOL_VERSION: u32 = 26;
pub const PROTOCOL_VERSION: u32 = 28;
Loading
Loading