Skip to content
Open
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
75 changes: 75 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# CITA Cloud Common Rust (cloud-common-rs)

## Project Information
- **Name**: cloud-common-rs
- **Description**: Common Rust utilities and protocol definitions for CITA Cloud ecosystem.
- **Repository**: [cloud-common-rs](https://github.com/cita-cloud/cloud-common-rs)

## Core Components

### 1. cloud-proto
Contains gRPC protocol definitions and generated Rust code for CITA Cloud microservices.
- **Services**: Blockchain, Consensus, Controller, Crypto, Executor, Network, Storage.
- **Tools**: Includes `client` module for easy service connection.

### 2. cloud-util
Provides common utilities for building CITA Cloud components.
- **Tracing**: Distributed tracing with OpenTelemetry (v0.31) and Jaeger/OTLP support.
- **Metrics**: Prometheus metrics collection via `axum`.
- **Cryptography**: Common crypto traits and implementations.
- **Signal Handling**: Graceful shutdown support.
- **Storage**: Storage traits abstraction.

## Dependencies
- **Rust Toolchain**: 1.85+ (2024 edition).
- **Protobuf Compiler**: `protoc` (required for building `cloud-proto`).
- **OpenSSL**: `openssl-sys` dependency.

## Usage

### Updating Protocols
Initialize and update git submodules for proto files:
```bash
sh update_proto.sh
```

### Building
Build the project in release mode:
```bash
cargo build --release --all --all-features
```

### Using Tracing (Example)
```rust
use cloud_util::tracer::{init_tracer, LogConfig};

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let config = LogConfig {
service_name: "my-service".to_string(),
agent_endpoint: Some("grpc://localhost:4317".to_string()), // OTLP endpoint
..Default::default()
};

// Initialize tracer
init_tracer("my-domain".to_string(), &config)?;

// ... application logic ...

Ok(())
}
```

### Using gRPC Client (Example)
```rust
use cloud_proto::client::ClientOptions;

async fn connect() {
let opts = ClientOptions::new(
"client-name".to_string(),
"http://localhost:50004".to_string()
);

let controller = opts.connect_controller();
// ...
}
```
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
resolver = "2"
resolver = "3"
members = [
"cloud-proto",
"cloud-util"
Expand Down
22 changes: 13 additions & 9 deletions cloud-proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
[package]
name = "cita_cloud_proto"
version = "6.7.4"
version = "6.7.5"
authors = ["Rivtower Technologies <contact@rivtower.com>"]
edition = "2021"
edition = "2024"
license = "Apache-2.0"
description = "cita-cloud proto library"


[dependencies]
tonic = "0.12"
prost = "0.13"
tokio = { version = "1.41", default-features = false, features = ["macros"] }
tonic = "0.14"
prost = "0.14"
tonic-prost = "0.14"
tokio = { version = "1.49", default-features = false, features = ["macros"] }
thiserror = "2.0"
http = "1.1"
http = "1.4"
futures-retry = "0.6"
backoff = "0.4"
async-trait = "0.1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
tracing = "0.1"
tracing-opentelemetry = "0.27"
opentelemetry = "0.26"
tracing-opentelemetry = "0.32"
opentelemetry = "0.31"

[features]
tonic-build = ["dep:tonic-prost-build"]

[build-dependencies]
tonic-build = { version = "0.12", optional = true }
tonic-prost-build = { version = "0.14", optional = true }
8 changes: 4 additions & 4 deletions cloud-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.

fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=protos");
#[cfg(feature = "tonic-build")]
{
println!("cargo:rerun-if-changed=protos");
tonic_build::configure()
tonic_prost_build::configure()
.out_dir("src/proto")
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile_protos(
Expand All @@ -32,12 +32,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
],
&["protos/protos"],
)?;
tonic_build::configure()
tonic_prost_build::configure()
.out_dir("src/proto")
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.file_descriptor_set_path("src/reflect/controller.bin")
.compile_protos(&["controller.proto"], &["protos/protos"])?;
tonic_build::configure()
tonic_prost_build::configure()
.out_dir("src/proto")
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.file_descriptor_set_path("src/reflect/executor.bin")
Expand Down
14 changes: 7 additions & 7 deletions cloud-proto/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use http::uri::InvalidUri;
use opentelemetry::{global, propagation::Injector};
use std::time::{Duration, Instant};
use tonic::{
codegen::InterceptedService, metadata::MetadataValue, service::Interceptor, transport::Channel,
Request, Status,
Request, Status, codegen::InterceptedService, metadata::MetadataValue, service::Interceptor,
transport::Channel,
};
use tracing_opentelemetry::OpenTelemetrySpanExt;

Expand Down Expand Up @@ -414,7 +414,7 @@ pub trait ControllerClientTrait {
#[async_trait::async_trait]
pub trait NetworkClientTrait {
async fn send_msg(&self, msg: network::NetworkMsg)
-> Result<common::StatusCode, tonic::Status>;
-> Result<common::StatusCode, tonic::Status>;

async fn broadcast(
&self,
Expand Down Expand Up @@ -520,10 +520,10 @@ struct MutMetadataMap<'a>(&'a mut tonic::metadata::MetadataMap);
impl<'a> Injector for MutMetadataMap<'a> {
/// Set a key and value in the MetadataMap. Does nothing if the key or value are not valid inputs
fn set(&mut self, key: &str, value: String) {
if let Ok(key) = tonic::metadata::MetadataKey::from_bytes(key.as_bytes()) {
if let Ok(val) = std::str::FromStr::from_str(&value) {
self.0.insert(key, val);
}
if let Ok(key) = tonic::metadata::MetadataKey::from_bytes(key.as_bytes())
&& let Ok(val) = std::str::FromStr::from_str(&value)
{
self.0.insert(key, val);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions cloud-proto/src/proto/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file is @generated by prost-build.
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct BlockHeader {
/// hash of previous BlockHeader
#[prost(bytes = "vec", tag = "1")]
Expand All @@ -13,12 +13,12 @@ pub struct BlockHeader {
#[prost(bytes = "vec", tag = "5")]
pub proposer: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Transaction {
#[prost(uint32, tag = "1")]
pub version: u32,
/// 1. length is 20 bytes for evm.
/// 2. if executor is multi-vm, it will be a path.
/// 1. if executor is multi-vm, it will be a path.
#[prost(bytes = "vec", tag = "2")]
pub to: ::prost::alloc::vec::Vec<u8>,
/// length is less than 128
Expand All @@ -37,15 +37,15 @@ pub struct Transaction {
#[prost(bytes = "vec", tag = "8")]
pub chain_id: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Witness {
#[prost(bytes = "vec", tag = "1")]
pub signature: ::prost::alloc::vec::Vec<u8>,
/// add to support multi-address, or we don't know which address algorithm to use
#[prost(bytes = "vec", tag = "2")]
pub sender: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct UnverifiedTransaction {
#[prost(message, optional, tag = "1")]
pub transaction: ::core::option::Option<Transaction>,
Expand All @@ -55,7 +55,7 @@ pub struct UnverifiedTransaction {
#[prost(message, optional, tag = "3")]
pub witness: ::core::option::Option<Witness>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct UtxoTransaction {
#[prost(uint32, tag = "1")]
pub version: u32,
Expand Down Expand Up @@ -96,13 +96,13 @@ pub mod raw_transaction {
UtxoTx(super::UnverifiedUtxoTransaction),
}
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct CompactBlockBody {
/// transaction hash of UnverifiedTransaction or UnverifyedUtxoTransaction.
#[prost(bytes = "vec", repeated, tag = "1")]
pub tx_hashes: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec<u8>>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct CompactBlock {
#[prost(uint32, tag = "1")]
pub version: u32,
Expand Down
34 changes: 19 additions & 15 deletions cloud-proto/src/proto/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// This file is @generated by prost-build.
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, ::prost::Message)]
#[derive(
serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, Hash, ::prost::Message,
)]
pub struct Empty {}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Hash {
#[prost(bytes = "vec", tag = "1")]
pub hash: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct StateRoot {
#[prost(bytes = "vec", tag = "1")]
pub state_root: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Proof {
#[prost(bytes = "vec", tag = "1")]
pub proof: ::prost::alloc::vec::Vec<u8>,
Expand All @@ -21,33 +23,33 @@ pub struct Hashes {
#[prost(message, repeated, tag = "1")]
pub hashes: ::prost::alloc::vec::Vec<Hash>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Address {
#[prost(bytes = "vec", tag = "1")]
pub address: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Proposal {
#[prost(uint64, tag = "1")]
pub height: u64,
#[prost(bytes = "vec", tag = "2")]
pub data: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ProposalInner {
#[prost(bytes = "vec", tag = "1")]
pub pre_state_root: ::prost::alloc::vec::Vec<u8>,
#[prost(message, optional, tag = "2")]
pub proposal: ::core::option::Option<super::blockchain::CompactBlock>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ProposalWithProof {
#[prost(message, optional, tag = "1")]
pub proposal: ::core::option::Option<Proposal>,
#[prost(bytes = "vec", tag = "2")]
pub proof: ::prost::alloc::vec::Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ConsensusConfiguration {
#[prost(uint64, tag = "1")]
pub height: u64,
Expand All @@ -56,33 +58,35 @@ pub struct ConsensusConfiguration {
#[prost(bytes = "vec", repeated, tag = "3")]
pub validators: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec<u8>>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, ::prost::Message)]
#[derive(
serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, Hash, ::prost::Message,
)]
pub struct StatusCode {
#[prost(uint32, tag = "1")]
pub code: u32,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct HashResponse {
#[prost(message, optional, tag = "1")]
pub status: ::core::option::Option<StatusCode>,
#[prost(message, optional, tag = "2")]
pub hash: ::core::option::Option<Hash>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ProposalResponse {
#[prost(message, optional, tag = "1")]
pub status: ::core::option::Option<StatusCode>,
#[prost(message, optional, tag = "2")]
pub proposal: ::core::option::Option<Proposal>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ConsensusConfigurationResponse {
#[prost(message, optional, tag = "1")]
pub status: ::core::option::Option<StatusCode>,
#[prost(message, optional, tag = "2")]
pub config: ::core::option::Option<ConsensusConfiguration>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct NodeNetInfo {
#[prost(string, tag = "1")]
pub multi_address: ::prost::alloc::string::String,
Expand All @@ -94,7 +98,7 @@ pub struct TotalNodeNetInfo {
#[prost(message, repeated, tag = "1")]
pub nodes: ::prost::alloc::vec::Vec<NodeNetInfo>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct PeerStatus {
#[prost(uint64, tag = "1")]
pub height: u64,
Expand Down
Loading
Loading