Skip to content

Commit

Permalink
[proto] update protocol version 28 (#23)
Browse files Browse the repository at this point in the history
* proto: update to protocol version 28

Signed-off-by: Sam Batschelet <[email protected]>

* proto: regen

Signed-off-by: Sam Batschelet <[email protected]>

* bump protoc-gen-prost

Signed-off-by: Sam Batschelet <[email protected]>

* *: react to proto regen

Signed-off-by: Sam Batschelet <[email protected]>

* Move protoc plugins to community versions where possible

Signed-off-by: Sam Batschelet <[email protected]>

* Remove unneeded deps

Signed-off-by: Sam Batschelet <[email protected]>

* remove community plugins as deps from codegen

Signed-off-by: Sam Batschelet <[email protected]>

* regen

Signed-off-by: Sam Batschelet <[email protected]>

* Add note on community mods

Signed-off-by: Sam Batschelet <[email protected]>

* Add readme

Signed-off-by: Sam Batschelet <[email protected]>

---------

Signed-off-by: Sam Batschelet <[email protected]>
Co-authored-by: Ron Kuris <[email protected]>
  • Loading branch information
hexfusion and rkuris authored Sep 5, 2023
1 parent eb812da commit 6dd4f8e
Show file tree
Hide file tree
Showing 34 changed files with 9,465 additions and 6,469 deletions.
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"

[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,
))),
};

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

0 comments on commit 6dd4f8e

Please sign in to comment.