-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'create-sns-by-proposal-daniel-wong' into 'master'
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
Showing
30 changed files
with
2,191 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/...", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
22 changes: 22 additions & 0 deletions
22
rs/nervous_system/proto/proto/ic_nervous_system/pb/v1/nervous_system.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
rs/nervous_system/proto/src/gen/ic_nervous_system.pb.v1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod pb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.