From e434b70a5794bef79681ce520873662c78edca76 Mon Sep 17 00:00:00 2001 From: Noor Date: Tue, 18 Mar 2025 22:58:22 +0530 Subject: [PATCH] Refactor proto packages and add shared common.proto seperated non vm related services into general protofile shared common proto for types like Empty rename proto packages top level crate export for convenient imports --- build.rs | 6 +- proto/sgx/{brain.proto => app.proto} | 13 ++-- proto/sgx/dtpm.proto | 2 +- proto/shared/common.proto | 9 +++ proto/shared/general.proto | 91 ++++++++++++++++++++++++++++ proto/snp/vm.proto | 90 ++------------------------- src/lib.rs | 5 ++ src/sgx/mod.rs | 10 +-- src/sgx/types/brain.rs | 4 +- src/sgx/types/dtpm.rs | 60 +++++++++--------- src/shared/mod.rs | 12 ++++ src/snp/mod.rs | 7 ++- 12 files changed, 174 insertions(+), 135 deletions(-) rename proto/sgx/{brain.proto => app.proto} (91%) create mode 100644 proto/shared/common.proto create mode 100644 proto/shared/general.proto create mode 100644 src/shared/mod.rs diff --git a/build.rs b/build.rs index 1b7070d..092564d 100644 --- a/build.rs +++ b/build.rs @@ -20,11 +20,13 @@ fn main() -> Result<(), Box> { ) .compile_protos( &[ - "proto/sgx/brain.proto", + "proto/sgx/app.proto", "proto/sgx/dtpm.proto", "proto/snp/vm.proto", + "proto/shared/common.proto", + "proto/shared/general.proto", ], - &["proto/sgx", "proto/snp"], + &["proto"], )?; Ok(()) diff --git a/proto/sgx/brain.proto b/proto/sgx/app.proto similarity index 91% rename from proto/sgx/brain.proto rename to proto/sgx/app.proto index 3cdde95..8d80138 100644 --- a/proto/sgx/brain.proto +++ b/proto/sgx/app.proto @@ -1,9 +1,8 @@ syntax = "proto3"; -package brain; +package app_proto; -message Empty { -} +import "shared/common.proto"; message AppContract { string uuid = 1; @@ -92,13 +91,13 @@ message AppNodeListResp { service BrainAppCli { rpc DeployApp (NewAppReq) returns (NewAppRes); - rpc DeleteApp (DelAppReq) returns (Empty); + rpc DeleteApp (DelAppReq) returns (common_proto.Empty); rpc ListAppContracts (ListAppContractsReq) returns (stream AppContract); rpc ListAppNodes (AppNodeFilters) returns (stream AppNodeListResp); rpc GetOneAppNode (AppNodeFilters) returns (AppNodeListResp); - // super admin commands - rpc ListAllAppContracts (Empty) returns (stream AppContract); + // super admin commands + // rpc ListAllAppContracts (common_proto.Empty) returns (stream AppContract); } message RegisterAppNodeReq { @@ -145,5 +144,5 @@ message DaemonMessageApp { service BrainAppDaemon { rpc RegisterAppNode (RegisterAppNodeReq) returns (stream AppContract); rpc BrainMessages (DaemonAuth) returns (stream BrainMessageApp); - rpc DaemonMessages (stream DaemonMessageApp) returns (Empty); + rpc DaemonMessages (stream DaemonMessageApp) returns (common_proto.Empty); } \ No newline at end of file diff --git a/proto/sgx/dtpm.proto b/proto/sgx/dtpm.proto index 7299f29..edf83ab 100644 --- a/proto/sgx/dtpm.proto +++ b/proto/sgx/dtpm.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package dtpm; +package dtpm_proto; message Empty { } diff --git a/proto/shared/common.proto b/proto/shared/common.proto new file mode 100644 index 0000000..65d8b1e --- /dev/null +++ b/proto/shared/common.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package common_proto; + +message Empty { +} + +message Pubkey { + string pubkey = 1; +} diff --git a/proto/shared/general.proto b/proto/shared/general.proto new file mode 100644 index 0000000..632fedf --- /dev/null +++ b/proto/shared/general.proto @@ -0,0 +1,91 @@ +syntax = "proto3"; +package general_proto; + +import "sgx/app.proto"; +import "shared/common.proto"; +import "snp/vm.proto"; + +message AccountBalance { + uint64 balance = 1; + uint64 tmp_locked = 2; +} + +message ReportNodeReq { + string admin_pubkey = 1; + string node_pubkey = 2; + string contract = 3; + string reason = 4; +} + +message ListOperatorsResp { + string pubkey = 1; + uint64 escrow = 2; + string email = 3; + uint64 app_nodes = 4; + uint64 vm_nodes = 5; + uint64 reports = 6; +} + +message InspectOperatorResp { + ListOperatorsResp operator = 1; + repeated vm_proto.VmNodeListResp vm_nodes = 2; + repeated app_proto.AppNodeListResp app_nodes = 3; +} + +message RegOperatorReq { + string pubkey = 1; + uint64 escrow = 2; + string email = 3; +} + +message KickReq { + string operator_wallet = 1; + string contract_uuid = 2; + string reason = 3; +} + +message KickResp { + uint64 nano_lp = 1; +} + +message BanUserReq { + string operator_wallet = 1; + string user_wallet = 2; +} + +message AirdropReq { + string pubkey = 1; + uint64 tokens = 2; +} + +message SlashReq { + string pubkey = 1; + uint64 tokens = 2; +} + +message Account { + string pubkey = 1; + uint64 balance = 2; + uint64 tmp_locked = 3; +} + + +service BrainGeneralCli { + rpc GetBalance (common_proto.Pubkey) returns (AccountBalance); + + rpc ReportNode (ReportNodeReq) returns (common_proto.Empty); + rpc ListOperators (common_proto.Empty) returns (stream ListOperatorsResp); + rpc InspectOperator (common_proto.Pubkey) returns (InspectOperatorResp); + rpc RegisterOperator (RegOperatorReq) returns (common_proto.Empty); + rpc KickContract (KickReq) returns (KickResp); + rpc BanUser (BanUserReq) returns (common_proto.Empty); + + // admin commands + rpc Airdrop (AirdropReq) returns (common_proto.Empty); + rpc Slash (SlashReq) returns (common_proto.Empty); + rpc ListAccounts (common_proto.Empty) returns (stream Account); + rpc ListAllVmContracts (common_proto.Empty) + returns (stream vm_proto.VmContract); + rpc ListAllAppContracts (common_proto.Empty) + returns (stream app_proto.AppContract); +} \ No newline at end of file diff --git a/proto/snp/vm.proto b/proto/snp/vm.proto index f0f4d2e..abc840d 100644 --- a/proto/snp/vm.proto +++ b/proto/snp/vm.proto @@ -1,17 +1,7 @@ syntax = "proto3"; package vm_proto; -message Empty { -} - -message Pubkey { - string pubkey = 1; -} - -message AccountBalance { - uint64 balance = 1; - uint64 tmp_locked = 2; -} +import "shared/common.proto"; message VmContract { string uuid = 1; @@ -150,7 +140,7 @@ message VmDaemonMessage { service BrainVmDaemon { rpc RegisterVmNode (RegisterVmNodeReq) returns (stream VmContract); rpc BrainMessages (DaemonStreamAuth) returns (stream BrainVmMessage); - rpc DaemonMessages (stream VmDaemonMessage) returns (Empty); + rpc DaemonMessages (stream VmDaemonMessage) returns (common_proto.Empty); } message ListVmContractsReq { @@ -190,82 +180,12 @@ message ExtendVmReq { uint64 locked_nano = 3; } -message AirdropReq { - string pubkey = 1; - uint64 tokens = 2; -} - -message SlashReq { - string pubkey = 1; - uint64 tokens = 2; -} - -message Account { - string pubkey = 1; - uint64 balance = 2; - uint64 tmp_locked = 3; -} - -message RegOperatorReq { - string pubkey = 1; - uint64 escrow = 2; - string email = 3; -} - -message ListOperatorsResp { - string pubkey = 1; - uint64 escrow = 2; - string email = 3; - uint64 app_nodes = 4; - uint64 vm_nodes = 5; - uint64 reports = 6; -} - -message InspectOperatorResp { - ListOperatorsResp operator = 1; - repeated VmNodeListResp nodes = 2; -} - -message ReportNodeReq { - string admin_pubkey = 1; - string node_pubkey = 2; - string contract = 3; - string reason = 4; -} - -message KickReq { - string operator_wallet = 1; - string contract_uuid = 2; - string reason = 3; -} - -message BanUserReq { - string operator_wallet = 1; - string user_wallet = 2; -} - -message KickResp { - uint64 nano_lp = 1; -} - -service BrainCli { - rpc GetBalance (Pubkey) returns (AccountBalance); +service BrainVmCli { rpc NewVm (NewVmReq) returns (NewVmResp); rpc ListVmContracts (ListVmContractsReq) returns (stream VmContract); rpc ListVmNodes (VmNodeFilters) returns (stream VmNodeListResp); rpc GetOneVmNode (VmNodeFilters) returns (VmNodeListResp); - rpc DeleteVm (DeleteVmReq) returns (Empty); + rpc DeleteVm (DeleteVmReq) returns (common_proto.Empty); rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp); - rpc ExtendVm (ExtendVmReq) returns (Empty); - rpc ReportNode (ReportNodeReq) returns (Empty); - rpc ListOperators (Empty) returns (stream ListOperatorsResp); - rpc InspectOperator (Pubkey) returns (InspectOperatorResp); - rpc RegisterOperator (RegOperatorReq) returns (Empty); - rpc KickContract (KickReq) returns (KickResp); - rpc BanUser (BanUserReq) returns (Empty); - // admin commands - rpc Airdrop (AirdropReq) returns (Empty); - rpc Slash (SlashReq) returns (Empty); - rpc ListAllVmContracts (Empty) returns (stream VmContract); - rpc ListAccounts (Empty) returns (stream Account); + rpc ExtendVm (ExtendVmReq) returns (common_proto.Empty); } diff --git a/src/lib.rs b/src/lib.rs index b19cb29..6a7d986 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,7 @@ pub mod sgx; +pub mod shared; pub mod snp; + +pub use sgx::pb::app_proto; +pub use shared::pb::{common_proto, general_proto}; +pub use snp::pb::vm_proto; diff --git a/src/sgx/mod.rs b/src/sgx/mod.rs index 85ba45d..cad8bf7 100644 --- a/src/sgx/mod.rs +++ b/src/sgx/mod.rs @@ -1,12 +1,12 @@ pub mod types; pub mod pb { - - pub mod brain { - tonic::include_proto!("brain"); + pub mod app_proto { + tonic::include_proto!("app_proto"); } - pub mod dtpm { - tonic::include_proto!("dtpm"); + pub mod dtpm_proto { + tonic::include_proto!("dtpm_proto"); } + use crate::common_proto; } diff --git a/src/sgx/types/brain.rs b/src/sgx/types/brain.rs index ac29a49..904b452 100644 --- a/src/sgx/types/brain.rs +++ b/src/sgx/types/brain.rs @@ -1,5 +1,5 @@ -use crate::sgx::pb::brain::{daemon_message_app, AppNodeResources, DaemonMessageApp, NewAppRes}; -use crate::sgx::pb::brain::{AppResource, DaemonAuth, MappedPort, NewAppReq}; +use crate::app_proto::{daemon_message_app, AppNodeResources, DaemonMessageApp}; +use crate::app_proto::{AppResource, DaemonAuth, MappedPort, NewAppReq, NewAppRes}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)] diff --git a/src/sgx/types/dtpm.rs b/src/sgx/types/dtpm.rs index 66a442d..ebf36cf 100644 --- a/src/sgx/types/dtpm.rs +++ b/src/sgx/types/dtpm.rs @@ -1,4 +1,4 @@ -use crate::sgx::pb::dtpm as pb_dtpm; +use crate::sgx::pb::dtpm_proto; use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; use serde::{Deserialize, Serialize}; @@ -9,8 +9,8 @@ pub struct DtpmConfig { pub child_processes: Vec, } -impl From for DtpmConfig { - fn from(pb_val: pb_dtpm::DtpmConfigData) -> Self { +impl From for DtpmConfig { + fn from(pb_val: dtpm_proto::DtpmConfigData) -> Self { DtpmConfig { filesystems: pb_val .filesystems @@ -31,9 +31,9 @@ impl From for DtpmConfig { } } -impl From for pb_dtpm::DtpmConfigData { - fn from(val: DtpmConfig) -> pb_dtpm::DtpmConfigData { - pb_dtpm::DtpmConfigData { +impl From for dtpm_proto::DtpmConfigData { + fn from(val: DtpmConfig) -> dtpm_proto::DtpmConfigData { + dtpm_proto::DtpmConfigData { filesystems: val.filesystems.into_iter().map(Into::into).collect(), environments: val.environments.into_iter().map(Into::into).collect(), child_processes: val.child_processes.into_iter().map(Into::into).collect(), @@ -47,17 +47,17 @@ pub struct FileEntry { pub content: FileContent, } -impl From for FileEntry { - fn from(pb_val: pb_dtpm::FileEntry) -> Self { +impl From for FileEntry { + fn from(pb_val: dtpm_proto::FileEntry) -> Self { FileEntry { path: pb_val.path, content: FileContent::Data(pb_val.content), } } } -impl From for pb_dtpm::FileEntry { - fn from(val: FileEntry) -> pb_dtpm::FileEntry { - pb_dtpm::FileEntry { +impl From for dtpm_proto::FileEntry { + fn from(val: FileEntry) -> dtpm_proto::FileEntry { + dtpm_proto::FileEntry { path: val.path, content: match val.content { FileContent::Data(data) => data, @@ -81,8 +81,8 @@ pub struct EnvironmentEntry { pub value: String, } -impl From for EnvironmentEntry { - fn from(pb_val: pb_dtpm::EnvironmentEntry) -> Self { +impl From for EnvironmentEntry { + fn from(pb_val: dtpm_proto::EnvironmentEntry) -> Self { EnvironmentEntry { name: pb_val.name, value: pb_val.value, @@ -90,9 +90,9 @@ impl From for EnvironmentEntry { } } -impl From for pb_dtpm::EnvironmentEntry { - fn from(val: EnvironmentEntry) -> pb_dtpm::EnvironmentEntry { - pb_dtpm::EnvironmentEntry { +impl From for dtpm_proto::EnvironmentEntry { + fn from(val: EnvironmentEntry) -> dtpm_proto::EnvironmentEntry { + dtpm_proto::EnvironmentEntry { name: val.name, value: val.value, } @@ -106,8 +106,8 @@ pub struct ChildProcess { pub restart: Option, } -impl From for ChildProcess { - fn from(pb_val: pb_dtpm::ChildProcess) -> Self { +impl From for ChildProcess { + fn from(pb_val: dtpm_proto::ChildProcess) -> Self { ChildProcess { path: pb_val.path, arguments: pb_val.arguments, @@ -116,9 +116,9 @@ impl From for ChildProcess { } } -impl From for pb_dtpm::ChildProcess { - fn from(val: ChildProcess) -> pb_dtpm::ChildProcess { - pb_dtpm::ChildProcess { +impl From for dtpm_proto::ChildProcess { + fn from(val: ChildProcess) -> dtpm_proto::ChildProcess { + dtpm_proto::ChildProcess { path: val.path, arguments: val.arguments, restart: val.restart.map(Into::into), @@ -144,16 +144,16 @@ impl Default for RestartPolicyType { RestartPolicyType::Always(true) } } -impl From for RestartPolicy { - fn from(pb_val: pb_dtpm::RestartPolicy) -> Self { +impl From for RestartPolicy { + fn from(pb_val: dtpm_proto::RestartPolicy) -> Self { RestartPolicy { max_retries: pb_val.max_retries, delay_seconds: pb_val.delay_seconds, policy: match pb_val.policy_type { - Some(pb_dtpm::restart_policy::PolicyType::Always(_)) => { + Some(dtpm_proto::restart_policy::PolicyType::Always(_)) => { Some(RestartPolicyType::Always(true)) } - Some(pb_dtpm::restart_policy::PolicyType::OnNonZeroExit(_)) => { + Some(dtpm_proto::restart_policy::PolicyType::OnNonZeroExit(_)) => { Some(RestartPolicyType::OnNonZeroExit(true)) } None => None, @@ -162,17 +162,17 @@ impl From for RestartPolicy { } } -impl From for pb_dtpm::RestartPolicy { - fn from(val: RestartPolicy) -> pb_dtpm::RestartPolicy { - pb_dtpm::RestartPolicy { +impl From for dtpm_proto::RestartPolicy { + fn from(val: RestartPolicy) -> dtpm_proto::RestartPolicy { + dtpm_proto::RestartPolicy { max_retries: val.max_retries, delay_seconds: val.delay_seconds, policy_type: match val.policy { Some(RestartPolicyType::Always(_)) => { - Some(pb_dtpm::restart_policy::PolicyType::Always(true)) + Some(dtpm_proto::restart_policy::PolicyType::Always(true)) } Some(RestartPolicyType::OnNonZeroExit(_)) => { - Some(pb_dtpm::restart_policy::PolicyType::OnNonZeroExit(true)) + Some(dtpm_proto::restart_policy::PolicyType::OnNonZeroExit(true)) } None => None, }, diff --git a/src/shared/mod.rs b/src/shared/mod.rs new file mode 100644 index 0000000..a3f6c95 --- /dev/null +++ b/src/shared/mod.rs @@ -0,0 +1,12 @@ +pub mod pb { + pub mod general_proto { + tonic::include_proto!("general_proto"); + } + pub mod common_proto { + tonic::include_proto!("common_proto"); + } + + // For compiling proto + use crate::app_proto; + use crate::vm_proto; +} diff --git a/src/snp/mod.rs b/src/snp/mod.rs index d03398b..b7758f0 100644 --- a/src/snp/mod.rs +++ b/src/snp/mod.rs @@ -1,11 +1,12 @@ pub mod pb { - - pub mod vm { + pub mod vm_proto { tonic::include_proto!("vm_proto"); } + + use crate::common_proto; } -use pb::vm as snp_proto; +use pb::vm_proto as snp_proto; impl From for snp_proto::VmDaemonMessage { fn from(value: snp_proto::NewVmResp) -> Self {