From ba09bf3f19615dbeda7f12113e091765ec9ebc70 Mon Sep 17 00:00:00 2001 From: shaokun <605416017@qq.com> Date: Wed, 22 Nov 2023 15:34:11 +0800 Subject: [PATCH 1/3] Add AWM sign request --- crates/avalanche-types/src/lib.rs | 2 +- .../src/subnet/rpc/snow/engine/common/vm.rs | 5 +- .../src/subnet/rpc/vm/server.rs | 5 +- crates/avalanche-types/src/warp/client.rs | 50 +++++++++++++++++++ crates/avalanche-types/src/warp/mod.rs | 33 ++++++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 crates/avalanche-types/src/warp/client.rs create mode 100644 crates/avalanche-types/src/warp/mod.rs diff --git a/crates/avalanche-types/src/lib.rs b/crates/avalanche-types/src/lib.rs index 925fb89..407839a 100644 --- a/crates/avalanche-types/src/lib.rs +++ b/crates/avalanche-types/src/lib.rs @@ -29,7 +29,7 @@ pub mod txs; pub mod units; pub mod utils; pub mod verify; - +pub mod warp; #[cfg(feature = "avalanchego")] #[cfg_attr(docsrs, doc(cfg(feature = "avalanchego")))] pub mod avalanchego; diff --git a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs index cb061dd..ebefc09 100644 --- a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs +++ b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs @@ -18,7 +18,7 @@ use crate::{ }, }; use tokio::sync::mpsc::Sender; - +use crate::warp::WarpSignerClient_; /// Vm describes the trait that all consensus VMs must implement. /// /// ref. @@ -29,7 +29,7 @@ pub trait CommonVm: AppHandler + Connector + Checkable { type ChainHandler: Handle; type StaticHandler: Handle; type ValidatorState: validators::State; - + type WarpSigner: WarpSignerClient_; async fn initialize( &mut self, ctx: Option>, @@ -40,6 +40,7 @@ pub trait CommonVm: AppHandler + Connector + Checkable { to_engine: Sender, fxs: &[Fx], app_sender: Self::AppSender, + warp_signer: Self::WarpSigner, ) -> Result<()>; async fn set_state(&self, state: State) -> Result<()>; async fn shutdown(&self) -> Result<()>; diff --git a/crates/avalanche-types/src/subnet/rpc/vm/server.rs b/crates/avalanche-types/src/subnet/rpc/vm/server.rs index ec7e9ee..bd64d5d 100644 --- a/crates/avalanche-types/src/subnet/rpc/vm/server.rs +++ b/crates/avalanche-types/src/subnet/rpc/vm/server.rs @@ -44,6 +44,7 @@ use prost::bytes::Bytes; use semver::Version; use tokio::sync::{broadcast, mpsc, RwLock}; use tonic::{Request, Response}; +use crate::warp::client::WarpSignerClient; pub struct Server { /// Underlying Vm implementation. @@ -96,6 +97,7 @@ where DatabaseManager = DatabaseManager, AppSender = AppSenderClient, ValidatorState = ValidatorStateClient, + WarpSigner=WarpSignerClient, > + Send + Sync + 'static, @@ -182,7 +184,7 @@ where return tonic::Status::unknown("engine receiver closed unexpectedly"); } }); - + let warp_signer = WarpSignerClient::new(client_conn.clone()); let mut inner_vm = self.vm.write().await; inner_vm .initialize( @@ -194,6 +196,7 @@ where tx_engine, &[()], AppSenderClient::new(client_conn.clone()), + warp_signer ) .await .map_err(|e| tonic::Status::unknown(e.to_string()))?; diff --git a/crates/avalanche-types/src/warp/client.rs b/crates/avalanche-types/src/warp/client.rs new file mode 100644 index 0000000..3ec6bae --- /dev/null +++ b/crates/avalanche-types/src/warp/client.rs @@ -0,0 +1,50 @@ +use std::io::{Error, ErrorKind, Read, Result}; +use std::str::FromStr; + +use crate::proto::pb::warp::{ + signer_client, + SignRequest, + SignResponse, + }; +use prost::bytes::Bytes; +use tonic::transport::Channel; +use crate::ids::Id; + +#[derive(Clone)] +pub struct WarpSignerClient { + inner: signer_client::SignerClient, +} + +impl WarpSignerClient { + pub fn new(client_conn: Channel) -> Self { + Self { + inner: signer_client::SignerClient::new(client_conn) + .max_decoding_message_size(usize::MAX) + .max_encoding_message_size(usize::MAX), + } + } +} + +#[tonic::async_trait] +impl super::WarpSignerClient_ for WarpSignerClient { + async fn sign(&self, + network_id: u32, + source_chain_id: &str, + payload: &[u8]) -> Result { + let mut client = self.inner.clone(); + let res = client + .sign(SignRequest { + network_id, + source_chain_id: Bytes::from(Id::from_str(source_chain_id).unwrap().to_vec()), + payload: Bytes::from(payload.to_vec()), + }) + .await + .map_err(|e| { + Error::new( + ErrorKind::Other, + format!("sign failed: {:?}", e), + ) + })?; + Ok(res.into_inner()) + } +} diff --git a/crates/avalanche-types/src/warp/mod.rs b/crates/avalanche-types/src/warp/mod.rs new file mode 100644 index 0000000..82efa70 --- /dev/null +++ b/crates/avalanche-types/src/warp/mod.rs @@ -0,0 +1,33 @@ +pub mod client; + +use std::io::Result; +use crate::proto::warp::SignResponse; + +#[tonic::async_trait] +pub trait WarpSignerClient_: Send + Sync + CloneBox { + async fn sign( + &self, + network_id: u32, + source_chain_id: &str, + payload: &[u8], + ) -> Result; +} + +pub trait CloneBox { + fn clone_box(&self) -> Box; +} + +impl CloneBox for T + where + T: 'static + WarpSignerClient_ + Clone + Send + Sync, +{ + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Box { + self.clone_box() + } +} \ No newline at end of file From 6414ed3f248a28fa18919cb390e692b8de822f0a Mon Sep 17 00:00:00 2001 From: shaokun <605416017@qq.com> Date: Fri, 24 Nov 2023 00:25:08 +0800 Subject: [PATCH 2/3] format files --- crates/avalanche-types/src/lib.rs | 6 ++--- .../src/subnet/rpc/snow/engine/common/vm.rs | 2 +- .../src/subnet/rpc/vm/server.rs | 6 ++--- crates/avalanche-types/src/warp/client.rs | 25 +++++++------------ crates/avalanche-types/src/warp/mod.rs | 8 +++--- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/crates/avalanche-types/src/lib.rs b/crates/avalanche-types/src/lib.rs index 407839a..09122f6 100644 --- a/crates/avalanche-types/src/lib.rs +++ b/crates/avalanche-types/src/lib.rs @@ -12,6 +12,9 @@ //! ecosystem. //! #![cfg_attr(docsrs, feature(doc_cfg))] +#[cfg(feature = "avalanchego")] +#[cfg_attr(docsrs, doc(cfg(feature = "avalanchego")))] +pub mod avalanchego; pub mod avm; pub mod choices; pub mod codec; @@ -30,9 +33,6 @@ pub mod units; pub mod utils; pub mod verify; pub mod warp; -#[cfg(feature = "avalanchego")] -#[cfg_attr(docsrs, doc(cfg(feature = "avalanchego")))] -pub mod avalanchego; #[cfg(feature = "coreth")] #[cfg_attr(docsrs, doc(cfg(feature = "coreth")))] diff --git a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs index ebefc09..57ef902 100644 --- a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs +++ b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, io::Result}; +use crate::warp::WarpSignerClient_; use crate::{ ids, subnet::rpc::{ @@ -18,7 +19,6 @@ use crate::{ }, }; use tokio::sync::mpsc::Sender; -use crate::warp::WarpSignerClient_; /// Vm describes the trait that all consensus VMs must implement. /// /// ref. diff --git a/crates/avalanche-types/src/subnet/rpc/vm/server.rs b/crates/avalanche-types/src/subnet/rpc/vm/server.rs index bd64d5d..4df8080 100644 --- a/crates/avalanche-types/src/subnet/rpc/vm/server.rs +++ b/crates/avalanche-types/src/subnet/rpc/vm/server.rs @@ -4,6 +4,7 @@ use std::{ time::{Duration, Instant}, }; +use crate::warp::client::WarpSignerClient; use crate::{ ids, packer::U32_LEN, @@ -44,7 +45,6 @@ use prost::bytes::Bytes; use semver::Version; use tokio::sync::{broadcast, mpsc, RwLock}; use tonic::{Request, Response}; -use crate::warp::client::WarpSignerClient; pub struct Server { /// Underlying Vm implementation. @@ -97,7 +97,7 @@ where DatabaseManager = DatabaseManager, AppSender = AppSenderClient, ValidatorState = ValidatorStateClient, - WarpSigner=WarpSignerClient, + WarpSigner = WarpSignerClient, > + Send + Sync + 'static, @@ -196,7 +196,7 @@ where tx_engine, &[()], AppSenderClient::new(client_conn.clone()), - warp_signer + warp_signer, ) .await .map_err(|e| tonic::Status::unknown(e.to_string()))?; diff --git a/crates/avalanche-types/src/warp/client.rs b/crates/avalanche-types/src/warp/client.rs index 3ec6bae..c6bc7ed 100644 --- a/crates/avalanche-types/src/warp/client.rs +++ b/crates/avalanche-types/src/warp/client.rs @@ -1,14 +1,10 @@ use std::io::{Error, ErrorKind, Read, Result}; use std::str::FromStr; -use crate::proto::pb::warp::{ - signer_client, - SignRequest, - SignResponse, - }; +use crate::ids::Id; +use crate::proto::pb::warp::{signer_client, SignRequest, SignResponse}; use prost::bytes::Bytes; use tonic::transport::Channel; -use crate::ids::Id; #[derive(Clone)] pub struct WarpSignerClient { @@ -27,10 +23,12 @@ impl WarpSignerClient { #[tonic::async_trait] impl super::WarpSignerClient_ for WarpSignerClient { - async fn sign(&self, - network_id: u32, - source_chain_id: &str, - payload: &[u8]) -> Result { + async fn sign( + &self, + network_id: u32, + source_chain_id: &str, + payload: &[u8], + ) -> Result { let mut client = self.inner.clone(); let res = client .sign(SignRequest { @@ -39,12 +37,7 @@ impl super::WarpSignerClient_ for WarpSignerClient { payload: Bytes::from(payload.to_vec()), }) .await - .map_err(|e| { - Error::new( - ErrorKind::Other, - format!("sign failed: {:?}", e), - ) - })?; + .map_err(|e| Error::new(ErrorKind::Other, format!("sign failed: {:?}", e)))?; Ok(res.into_inner()) } } diff --git a/crates/avalanche-types/src/warp/mod.rs b/crates/avalanche-types/src/warp/mod.rs index 82efa70..f2c0770 100644 --- a/crates/avalanche-types/src/warp/mod.rs +++ b/crates/avalanche-types/src/warp/mod.rs @@ -1,7 +1,7 @@ pub mod client; -use std::io::Result; use crate::proto::warp::SignResponse; +use std::io::Result; #[tonic::async_trait] pub trait WarpSignerClient_: Send + Sync + CloneBox { @@ -18,8 +18,8 @@ pub trait CloneBox { } impl CloneBox for T - where - T: 'static + WarpSignerClient_ + Clone + Send + Sync, +where + T: 'static + WarpSignerClient_ + Clone + Send + Sync, { fn clone_box(&self) -> Box { Box::new(self.clone()) @@ -30,4 +30,4 @@ impl Clone for Box { fn clone(&self) -> Box { self.clone_box() } -} \ No newline at end of file +} From cfeff18649745af63279c4250bcf21c854e5e3e9 Mon Sep 17 00:00:00 2001 From: shaokun <605416017@qq.com> Date: Mon, 18 Dec 2023 11:12:16 +0800 Subject: [PATCH 3/3] Move signer_client from initialize method to context --- crates/avalanche-types/src/lib.rs | 8 ++-- .../avalanche-types/src/subnet/rpc/context.rs | 2 + .../src/subnet/rpc/snow/engine/common/vm.rs | 3 -- .../src/subnet/rpc/vm/server.rs | 10 ++--- crates/avalanche-types/src/warp/client.rs | 43 ------------------- crates/avalanche-types/src/warp/mod.rs | 33 -------------- 6 files changed, 9 insertions(+), 90 deletions(-) delete mode 100644 crates/avalanche-types/src/warp/client.rs delete mode 100644 crates/avalanche-types/src/warp/mod.rs diff --git a/crates/avalanche-types/src/lib.rs b/crates/avalanche-types/src/lib.rs index 09122f6..942a748 100644 --- a/crates/avalanche-types/src/lib.rs +++ b/crates/avalanche-types/src/lib.rs @@ -19,6 +19,9 @@ pub mod avm; pub mod choices; pub mod codec; pub mod constants; +#[cfg(feature = "coreth")] +#[cfg_attr(docsrs, doc(cfg(feature = "coreth")))] +pub mod coreth; pub mod errors; pub mod formatting; pub mod hash; @@ -32,11 +35,6 @@ pub mod txs; pub mod units; pub mod utils; pub mod verify; -pub mod warp; - -#[cfg(feature = "coreth")] -#[cfg_attr(docsrs, doc(cfg(feature = "coreth")))] -pub mod coreth; #[cfg(feature = "subnet_evm")] #[cfg_attr(docsrs, doc(cfg(feature = "subnet_evm")))] diff --git a/crates/avalanche-types/src/subnet/rpc/context.rs b/crates/avalanche-types/src/subnet/rpc/context.rs index 7a319fe..3d769c2 100644 --- a/crates/avalanche-types/src/subnet/rpc/context.rs +++ b/crates/avalanche-types/src/subnet/rpc/context.rs @@ -7,6 +7,7 @@ use crate::{ keystore::keystore_client::KeystoreClient, sharedmemory::shared_memory_client::SharedMemoryClient, }, + proto::warp::signer_client::SignerClient, }; use tonic::transport::Channel; @@ -25,6 +26,7 @@ pub struct Context { pub keystore: KeystoreClient, pub shared_memory: SharedMemoryClient, pub bc_lookup: AliasReaderClient, + pub singer_client: SignerClient, pub chain_data_dir: String, pub validator_state: S, // TODO metrics diff --git a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs index 57ef902..94feec8 100644 --- a/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs +++ b/crates/avalanche-types/src/subnet/rpc/snow/engine/common/vm.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, io::Result}; -use crate::warp::WarpSignerClient_; use crate::{ ids, subnet::rpc::{ @@ -29,7 +28,6 @@ pub trait CommonVm: AppHandler + Connector + Checkable { type ChainHandler: Handle; type StaticHandler: Handle; type ValidatorState: validators::State; - type WarpSigner: WarpSignerClient_; async fn initialize( &mut self, ctx: Option>, @@ -40,7 +38,6 @@ pub trait CommonVm: AppHandler + Connector + Checkable { to_engine: Sender, fxs: &[Fx], app_sender: Self::AppSender, - warp_signer: Self::WarpSigner, ) -> Result<()>; async fn set_state(&self, state: State) -> Result<()>; async fn shutdown(&self) -> Result<()>; diff --git a/crates/avalanche-types/src/subnet/rpc/vm/server.rs b/crates/avalanche-types/src/subnet/rpc/vm/server.rs index 4df8080..c3a81de 100644 --- a/crates/avalanche-types/src/subnet/rpc/vm/server.rs +++ b/crates/avalanche-types/src/subnet/rpc/vm/server.rs @@ -4,7 +4,6 @@ use std::{ time::{Duration, Instant}, }; -use crate::warp::client::WarpSignerClient; use crate::{ ids, packer::U32_LEN, @@ -17,6 +16,7 @@ use crate::{ sharedmemory::shared_memory_client::SharedMemoryClient, vm, }, + proto::warp::signer_client, subnet::rpc::{ consensus::snowman::{Block, Decidable}, context::Context, @@ -97,7 +97,6 @@ where DatabaseManager = DatabaseManager, AppSender = AppSenderClient, ValidatorState = ValidatorStateClient, - WarpSigner = WarpSignerClient, > + Send + Sync + 'static, @@ -125,7 +124,7 @@ where let keystore = KeystoreClient::new(client_conn.clone()); let shared_memory = SharedMemoryClient::new(client_conn.clone()); let bc_lookup = AliasReaderClient::new(client_conn.clone()); - + let singer_client = signer_client::SignerClient::new(client_conn.clone()); let ctx: Option> = Some(Context { network_id: req.network_id, subnet_id: ids::Id::from_slice(&req.subnet_id), @@ -137,6 +136,7 @@ where keystore, shared_memory, bc_lookup, + singer_client, chain_data_dir: req.chain_data_dir, validator_state: ValidatorStateClient::new(client_conn.clone()), }); @@ -184,7 +184,6 @@ where return tonic::Status::unknown("engine receiver closed unexpectedly"); } }); - let warp_signer = WarpSignerClient::new(client_conn.clone()); let mut inner_vm = self.vm.write().await; inner_vm .initialize( @@ -196,7 +195,6 @@ where tx_engine, &[()], AppSenderClient::new(client_conn.clone()), - warp_signer, ) .await .map_err(|e| tonic::Status::unknown(e.to_string()))?; @@ -1012,7 +1010,7 @@ where return Ok(Response::new(vm::GetBlockIdAtHeightResponse { blk_id: height.to_vec().into(), err: 0, - })) + })); } Err(e) => { if error_to_error_code(&e.to_string()) != 0 { diff --git a/crates/avalanche-types/src/warp/client.rs b/crates/avalanche-types/src/warp/client.rs deleted file mode 100644 index c6bc7ed..0000000 --- a/crates/avalanche-types/src/warp/client.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::io::{Error, ErrorKind, Read, Result}; -use std::str::FromStr; - -use crate::ids::Id; -use crate::proto::pb::warp::{signer_client, SignRequest, SignResponse}; -use prost::bytes::Bytes; -use tonic::transport::Channel; - -#[derive(Clone)] -pub struct WarpSignerClient { - inner: signer_client::SignerClient, -} - -impl WarpSignerClient { - pub fn new(client_conn: Channel) -> Self { - Self { - inner: signer_client::SignerClient::new(client_conn) - .max_decoding_message_size(usize::MAX) - .max_encoding_message_size(usize::MAX), - } - } -} - -#[tonic::async_trait] -impl super::WarpSignerClient_ for WarpSignerClient { - async fn sign( - &self, - network_id: u32, - source_chain_id: &str, - payload: &[u8], - ) -> Result { - let mut client = self.inner.clone(); - let res = client - .sign(SignRequest { - network_id, - source_chain_id: Bytes::from(Id::from_str(source_chain_id).unwrap().to_vec()), - payload: Bytes::from(payload.to_vec()), - }) - .await - .map_err(|e| Error::new(ErrorKind::Other, format!("sign failed: {:?}", e)))?; - Ok(res.into_inner()) - } -} diff --git a/crates/avalanche-types/src/warp/mod.rs b/crates/avalanche-types/src/warp/mod.rs deleted file mode 100644 index f2c0770..0000000 --- a/crates/avalanche-types/src/warp/mod.rs +++ /dev/null @@ -1,33 +0,0 @@ -pub mod client; - -use crate::proto::warp::SignResponse; -use std::io::Result; - -#[tonic::async_trait] -pub trait WarpSignerClient_: Send + Sync + CloneBox { - async fn sign( - &self, - network_id: u32, - source_chain_id: &str, - payload: &[u8], - ) -> Result; -} - -pub trait CloneBox { - fn clone_box(&self) -> Box; -} - -impl CloneBox for T -where - T: 'static + WarpSignerClient_ + Clone + Send + Sync, -{ - fn clone_box(&self) -> Box { - Box::new(self.clone()) - } -} - -impl Clone for Box { - fn clone(&self) -> Box { - self.clone_box() - } -}