Skip to content

Commit

Permalink
Merge branch 'create-sns-by-proposal-daniel-wong' into 'master'
Browse files Browse the repository at this point in the history
feat: NNS1-1920: Added CreateServiceNervousSystem proposal to NNS. [override-didc-check]

Also, adds nervous_system.proto.

The new feature is currently only available when NNS governance is built with `cfg(feature = "test")` (feature is singular, not plural!).

IIUC, `didc check` is complaining, because this adds a value to a variant (for the new proposal type). IIUC, this is due to a bug in Candid that is being fixed. IIUC, we'll have to coordinate this release with clients. Also, once this merges into master, we won't be able to do an emergency upgrade, because of the "breaking" Candid interface changes (even though the feature will be disabled). 

See merge request dfinity-lab/public/ic!11755
  • Loading branch information
daniel-wong-dfinity-org committed Apr 18, 2023
2 parents e553baf + 4117f2a commit 878bc1d
Show file tree
Hide file tree
Showing 30 changed files with 2,191 additions and 59 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ members = [
"rs/nervous_system/common/test_canister",
"rs/nervous_system/common/test_keys",
"rs/nervous_system/common/test_utils",
"rs/nervous_system/proto",
"rs/nervous_system/proto/protobuf_generator",
"rs/nervous_system/root",
"rs/nns/constants",
"rs/nns/common",
Expand Down
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build:
- rs/bitcoin/service/proto
- rs/canister_http/service/proto
- rs/crypto/internal/crypto_service_provider/proto
- rs/nervous_system/proto/proto
- rs/nns/common/proto
- rs/nns/gtc/proto Temporarily removed because a PR was reverted and then un-reverted
- rs/nns/handlers/root/proto
Expand Down
1 change: 1 addition & 0 deletions publish/canisters/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ CANISTERS_MAX_SIZE_IN_BYTES = {
"cycles-minting-canister.wasm": "2500000",
"genesis-token-canister.wasm": "2500000",
"governance-canister.wasm": "2500000",
"governance-canister_test.wasm": "2500000",
"governance-mem-test-canister.wasm": "2500000",
"ic-ckbtc-minter.wasm": "1900000",
"ic-icrc1-ledger.wasm": "1900000",
Expand Down
8 changes: 8 additions & 0 deletions rs/nervous_system/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package_group(
name = "default_visibility",
packages = [
"//rs/nervous_system/...",
"//rs/nns/...",
"//rs/sns/...",
],
)
45 changes: 45 additions & 0 deletions rs/nervous_system/proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("@rules_rust//rust:defs.bzl", "rust_library")
load("//bazel:prost.bzl", "generated_files_check")

package(default_visibility = ["//rs/nervous_system:default_visibility"])

filegroup(
name = "protos",
srcs = glob(["proto/**"]),
)

DEPENDENCIES = [
"@crate_index//:candid",
"@crate_index//:comparable",
"@crate_index//:prost",
"@crate_index//:serde",
]

MACRO_DEPENDENCIES = [
]

ALIASES = {}

rust_library(
name = "proto",
srcs = glob(["src/**"]),
aliases = ALIASES,
crate_name = "ic_nervous_system_proto",
proc_macro_deps = MACRO_DEPENDENCIES,
version = "0.0.1",
deps = DEPENDENCIES,
)

generated_files_check(
name = "check_generated_files",
srcs = ["tests/check_generated_files.rs"],
data = glob(["src/gen/**/*.rs"]) + [
":protos",
],
manifest_dir = "rs/nervous_system/proto",
deps = [
"//rs/nervous_system/proto/protobuf_generator:lib",
"//rs/test_utilities/compare_dirs",
"@crate_index//:tempfile",
],
)
15 changes: 15 additions & 0 deletions rs/nervous_system/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "ic-nervous-system-proto"
version = "0.0.1"
edition = "2021"

[dependencies]
candid = "0.8.1"
comparable = { version = "0.5.1", features = ["derive"] }
prost = "0.11.0"
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
ic-nervous-system-proto-protobuf-generator = { path = "./protobuf_generator" }
ic-test-utilities-compare-dirs = { path = "../../test_utilities/compare_dirs" }
tempfile = "3.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

package ic_nervous_system.pb.v1;

message Duration {
optional uint64 seconds = 1;
}

message Tokens {
optional uint64 e8s = 1;
}

message Image {
// A data URI of a png. E.g.
// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0lEQVQIHQEEAPv/AAD/DwIRAQ8HgT3GAAAAAElFTkSuQmCC
// ^ 1 pixel containing the color #00FF0F.
optional string base64_encoding = 1;
}

message Percentage {
optional uint64 basis_points = 1;
}
22 changes: 22 additions & 0 deletions rs/nervous_system/proto/protobuf_generator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@rules_rust//rust:defs.bzl", "rust_library")
load("//bazel:prost.bzl", "protobuf_generator")

package(default_visibility = ["//rs/nervous_system:default_visibility"])

rust_library(
name = "lib",
srcs = ["src/lib.rs"],
crate_name = "ic_nervous_system_proto_protobuf_generator",
version = "0.8.0",
deps = [
"//rs/utils/rustfmt",
"@crate_index//:prost-build",
],
)

protobuf_generator(
name = "protobuf_generator",
srcs = ["src/main.rs"],
manifest_dir = "rs/nervous_system/proto/protobuf_generator",
deps = [":lib"],
)
8 changes: 8 additions & 0 deletions rs/nervous_system/proto/protobuf_generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ic-nervous-system-proto-protobuf-generator"
version = "0.0.1"
edition = "2021"

[dependencies]
ic-utils-rustfmt = { path = "../../../utils/rustfmt" }
prost-build = "0.11.0"
55 changes: 55 additions & 0 deletions rs/nervous_system/proto/protobuf_generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::path::Path;

/// Search paths used by Prost.
pub struct ProtoPaths<'a> {
pub nervous_system: &'a Path,
}

impl ProtoPaths<'_> {
fn to_vec(&self) -> Vec<&Path> {
vec![self.nervous_system]
}
}

const COPY_TYPE_NAMES: [&str; 3] = ["Duration", "Tokens", "Percentage"];

/// Build protos using prost_build.
pub fn generate_prost_files(proto_paths: ProtoPaths<'_>, out_dir: &Path) {
let mut config = prost_build::Config::new();
config.protoc_arg("--experimental_allow_proto3_optional");

// Candid-ify generated Rust types.
config.type_attribute(".", "#[derive(candid::CandidType, candid::Deserialize)]");
// Because users of the types we supply put these on their types, we must
// also add these derives.
config.type_attribute(".", "#[derive(comparable::Comparable, serde::Serialize)]");

let src_file = proto_paths
.nervous_system
.join("ic_nervous_system/pb/v1/nervous_system.proto");

// Make most types copy (currently, only Image is not Copy).
for type_name in COPY_TYPE_NAMES {
config.type_attribute(
format!(".ic_nervous_system_proto.pb.v1.{}", type_name),
"#[derive(Copy)]",
);
}

// Assert that all files and directories exist.
assert!(src_file.exists());
let search_paths = proto_paths.to_vec();
for p in search_paths {
assert!(p.exists());
}

config.out_dir(out_dir);
std::fs::create_dir_all(out_dir).expect("failed to create output directory");
config.out_dir(out_dir);

config
.compile_protos(&[src_file], &proto_paths.to_vec())
.unwrap();

ic_utils_rustfmt::rustfmt(out_dir).expect("failed to rustfmt protobufs");
}
29 changes: 29 additions & 0 deletions rs/nervous_system/proto/protobuf_generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ic_nervous_system_proto_protobuf_generator::{generate_prost_files, ProtoPaths};
use std::path::PathBuf;

fn main() {
let manifest_dir = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR env variable is not defined"),
);
let primary_search_path = manifest_dir.join("../proto");
let out_dir = manifest_dir.join("../src/gen");

// Delete contents of the output directory.
match std::fs::remove_dir_all(&out_dir) {
Ok(_) => (),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
Err(e) => panic!(
"failed to clean up output directory {}: {}",
out_dir.display(),
e
),
}

generate_prost_files(
ProtoPaths {
nervous_system: &primary_search_path,
},
out_dir.as_ref(),
);
}
55 changes: 55 additions & 0 deletions rs/nervous_system/proto/src/gen/ic_nervous_system.pb.v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[derive(
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
)]
pub struct Duration {
#[prost(uint64, optional, tag = "1")]
pub seconds: ::core::option::Option<u64>,
}
#[derive(
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
)]
pub struct Tokens {
#[prost(uint64, optional, tag = "1")]
pub e8s: ::core::option::Option<u64>,
}
#[derive(
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
)]
pub struct Image {
/// A data URI of a png. E.g.
/// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0lEQVQIHQEEAPv/AAD/DwIRAQ8HgT3GAAAAAElFTkSuQmCC
/// ^ 1 pixel containing the color #00FF0F.
#[prost(string, optional, tag = "1")]
pub base64_encoding: ::core::option::Option<::prost::alloc::string::String>,
}
#[derive(
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
)]
pub struct Percentage {
#[prost(uint64, optional, tag = "1")]
pub basis_points: ::core::option::Option<u64>,
}
1 change: 1 addition & 0 deletions rs/nervous_system/proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pb;
3 changes: 3 additions & 0 deletions rs/nervous_system/proto/src/pb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[allow(clippy::all)]
#[path = "../gen/ic_nervous_system.pb.v1.rs"]
pub mod v1;
46 changes: 46 additions & 0 deletions rs/nervous_system/proto/tests/check_generated_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use ic_nervous_system_proto_protobuf_generator::{generate_prost_files, ProtoPaths};
use ic_test_utilities_compare_dirs::{compare, CompareError};
use std::path::PathBuf;

#[test]
fn check_generated_files() {
let command_to_regenerate =
"bazel run //rs/nervous_system/proto/protobuf_generator:protobuf_generator";

let manifest_dir = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR env variable is not defined"),
);
let out_dir = tempfile::TempDir::new().expect("failed to create a temporary directory");
let nervous_system_proto = manifest_dir.join("proto");

generate_prost_files(
ProtoPaths {
nervous_system: &nervous_system_proto,
},
out_dir.path(),
);

let gen = manifest_dir.join("src/gen");

match compare(&gen, out_dir.path()) {
Ok(_) => (),
Err(CompareError::PathsDiffer { .. }) => {
panic!(
"Directory {} is outdated, run {}",
gen.display(),
command_to_regenerate
)
}
Err(CompareError::ContentDiffers { path }) => {
panic!(
"Source file {} is outdated, run {}",
path.display(),
command_to_regenerate
)
}
Err(CompareError::IoError { path, cause }) => {
panic!("I/O error on {}: {}", path.display(), cause)
}
}
}
Loading

0 comments on commit 878bc1d

Please sign in to comment.