-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AWM sign request #99
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. <https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/common#Vm> | ||
|
@@ -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<Context<Self::ValidatorState>>, | ||
|
@@ -40,6 +40,7 @@ pub trait CommonVm: AppHandler + Connector + Checkable { | |
to_engine: Sender<Message>, | ||
fxs: &[Fx], | ||
app_sender: Self::AppSender, | ||
warp_signer: Self::WarpSigner, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just pass in the concrete type here. It's unclear if there would ever be any associated type other than |
||
) -> Result<()>; | ||
async fn set_state(&self, state: State) -> Result<()>; | ||
async fn shutdown(&self) -> Result<()>; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -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<Channel>, | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the benefit of the wrapper here? Why not just use the |
||||||||||
|
||||||||||
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), | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are already the defaults
Suggested change
|
||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
#[tonic::async_trait] | ||||||||||
impl super::WarpSignerClient_ for WarpSignerClient { | ||||||||||
async fn sign(&self, | ||||||||||
network_id: u32, | ||||||||||
source_chain_id: &str, | ||||||||||
payload: &[u8]) -> Result<SignResponse> { | ||||||||||
let mut client = self.inner.clone(); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you have to create a new signer client every time you need to sign? The clone here should be explicit by the user (or they can wrap their channel in some kind of lock). This masks the internal functionality and doesn't give users the option to do things more efficiently. |
||||||||||
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()) | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SignResponse>; | ||
} | ||
|
||
pub trait CloneBox { | ||
fn clone_box(&self) -> Box<dyn WarpSignerClient_ + Send + Sync>; | ||
} | ||
|
||
impl<T> CloneBox for T | ||
where | ||
T: 'static + WarpSignerClient_ + Clone + Send + Sync, | ||
{ | ||
fn clone_box(&self) -> Box<dyn WarpSignerClient_ + Send + Sync> { | ||
Box::new(self.clone()) | ||
} | ||
} | ||
|
||
impl Clone for Box<dyn WarpSignerClient_ + Send + Sync> { | ||
fn clone(&self) -> Box<dyn WarpSignerClient_ + Send + Sync> { | ||
self.clone_box() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this type alias?