Skip to content

Commit

Permalink
refactor: make signer optional, remove more unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-savu committed Nov 3, 2023
1 parent 19d6ed3 commit d0036dc
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 66 deletions.
4 changes: 2 additions & 2 deletions rust/chains/hyperlane-cosmos/src/aggregation_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct CosmosAggregationIsm {

impl CosmosAggregationIsm {
/// create new Cosmos AggregationIsm agent
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
domain: locator.domain.clone(),
Expand Down
4 changes: 2 additions & 2 deletions rust/chains/hyperlane-cosmos/src/interchain_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl InterchainGasPaymaster for CosmosInterchainGasPaymaster {}

impl CosmosInterchainGasPaymaster {
/// create new Cosmos InterchainGasPaymaster agent
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
domain: locator.domain.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct CosmosInterchainSecurityModule {
/// The Cosmos Interchain Security Module Implementation.
impl CosmosInterchainSecurityModule {
/// Creates a new Cosmos Interchain Security Module.
pub fn new(conf: &ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
pub fn new(conf: &ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider: WasmGrpcProvider =
WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
domain: locator.domain.clone(),
Expand Down
17 changes: 10 additions & 7 deletions rust/chains/hyperlane-cosmos/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,30 @@ use tracing::{instrument, warn};

/// A reference to a Mailbox contract on some Cosmos chain
pub struct CosmosMailbox {
_conf: ConnectionConf,
conf: ConnectionConf,
domain: HyperlaneDomain,
address: H256,
signer: Signer,
provider: Box<WasmGrpcProvider>,
}

impl CosmosMailbox {
/// Create a reference to a mailbox at a specific Ethereum address on some
/// chain
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());

Self {
_conf: conf,
conf,
domain: locator.domain.clone(),
address: locator.address,
signer,
provider: Box::new(provider),
}
}

/// Prefix used in the bech32 address encoding
pub fn prefix(&self) -> String {
self.conf.get_prefix()
}
}

impl HyperlaneContract for CosmosMailbox {
Expand Down Expand Up @@ -134,7 +137,7 @@ impl Mailbox for CosmosMailbox {

#[instrument(err, ret, skip(self))]
async fn recipient_ism(&self, recipient: H256) -> ChainResult<H256> {
let address = CosmosAddress::from_h256(self.address, &self.signer.prefix)?.address();
let address = CosmosAddress::from_h256(self.address, &self.prefix())?.address();

let payload = mailbox::RecipientIsmRequest {
recipient_ism: mailbox::RecipientIsmRequestInner {
Expand Down Expand Up @@ -245,7 +248,7 @@ impl CosmosMailboxIndexer {
pub fn new(
conf: ConnectionConf,
locator: ContractLocator,
signer: Signer,
signer: Option<Signer>,
event_type: String,
reorg_period: u32,
) -> Self {
Expand Down
10 changes: 2 additions & 8 deletions rust/chains/hyperlane-cosmos/src/merkle_tree_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,22 @@ use crate::{
#[derive(Debug)]
/// A reference to a MerkleTreeHook contract on some Cosmos chain
pub struct CosmosMerkleTreeHook {
/// Connection configuration
_conf: ConnectionConf,
/// Domain
domain: HyperlaneDomain,
/// Contract address
address: H256,
/// Signer
_signer: Signer,
/// Provider
provider: Box<WasmGrpcProvider>,
}

impl CosmosMerkleTreeHook {
/// create new Cosmos MerkleTreeHook agent
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
_conf: conf,
domain: locator.domain.clone(),
address: locator.address,
_signer: signer,
provider: Box::new(provider),
}
}
Expand Down
8 changes: 2 additions & 6 deletions rust/chains/hyperlane-cosmos/src/multisig_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@ use crate::payloads::multisig_ism::{self, VerifyInfoRequest, VerifyInfoRequestIn
/// A reference to a MultisigIsm contract on some Cosmos chain
#[derive(Debug)]
pub struct CosmosMultisigIsm {
_conf: ConnectionConf,
domain: HyperlaneDomain,
address: H256,
_signer: Signer,
provider: Box<WasmGrpcProvider>,
}

impl CosmosMultisigIsm {
/// create a new instance of CosmosMultisigIsm
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
_conf: conf,
domain: locator.domain.clone(),
address: locator.address,
_signer: signer,
provider: Box::new(provider),
}
}
Expand Down
30 changes: 17 additions & 13 deletions rust/chains/hyperlane-cosmos/src/providers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use cosmrs::proto::traits::Message;

use cosmrs::tx::{self, Fee, MessageExt, SignDoc, SignerInfo};
use cosmrs::{Amount, Coin};
use hyperlane_core::{
ChainCommunicationError, ChainResult, ContractLocator, HyperlaneDomain, H256, U256,
};
use hyperlane_core::{ChainCommunicationError, ChainResult, ContractLocator, H256, U256};
use serde::Serialize;

use crate::address::CosmosAddress;
Expand Down Expand Up @@ -86,17 +84,15 @@ pub trait WasmProvider: Send + Sync {
/// Cosmwasm GRPC Provider
pub struct WasmGrpcProvider {
conf: ConnectionConf,
_domain: HyperlaneDomain,
address: H256,
signer: Signer,
signer: Option<Signer>,
}

impl WasmGrpcProvider {
/// create new Cosmwasm GRPC Provider
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
Self {
conf,
_domain: locator.domain.clone(),
address: locator.address,
signer,
}
Expand All @@ -110,6 +106,12 @@ impl WasmGrpcProvider {
let cosmos_address = CosmosAddress::from_h256(self.address, &self.conf.get_prefix())?;
Ok(cosmos_address.address())
}

fn get_signer(&self) -> ChainResult<Signer> {
self.signer
.clone()
.ok_or(ChainCommunicationError::SignerUnavailable)
}
}

#[async_trait]
Expand Down Expand Up @@ -219,14 +221,14 @@ impl WasmProvider for WasmGrpcProvider {
where
I: IntoIterator<Item = cosmrs::Any> + Send + Sync,
{
let account_info = self.account_query(self.signer.address.clone()).await?;
let signer = self.get_signer()?;
let account_info = self.account_query(signer.address.clone()).await?;

let private_key = self.signer.signing_key()?;
let private_key = signer.signing_key()?;
let public_key = private_key.public_key();

let tx_body = tx::Body::new(msgs, "", 9000000u32);
let signer_info =
SignerInfo::single_direct(Some(self.signer.public_key), account_info.sequence);
let signer_info = SignerInfo::single_direct(Some(signer.public_key), account_info.sequence);

let gas_limit: u64 = gas_limit.unwrap_or(U256::from(300000u64)).as_u64();

Expand Down Expand Up @@ -255,10 +257,11 @@ impl WasmProvider for WasmGrpcProvider {
where
T: Serialize + Send + Sync,
{
let signer = self.get_signer()?;
let mut client = TxServiceClient::connect(self.get_conn_url()?).await?;

let msgs = vec![MsgExecuteContract {
sender: self.signer.address.clone(),
sender: signer.address,
contract: self.get_contract_addr()?,
msg: serde_json::to_string(&payload)?.as_bytes().to_vec(),
funds: vec![],
Expand Down Expand Up @@ -289,8 +292,9 @@ impl WasmProvider for WasmGrpcProvider {
where
T: Serialize + Send + Sync,
{
let signer = self.get_signer()?;
let msg = MsgExecuteContract {
sender: self.signer.address.clone(),
sender: signer.address,
contract: self.get_contract_addr()?,
msg: serde_json::to_string(&payload)?.as_bytes().to_vec(),
funds: vec![],
Expand Down
4 changes: 2 additions & 2 deletions rust/chains/hyperlane-cosmos/src/routing_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct CosmosRoutingIsm {

impl CosmosRoutingIsm {
/// create a new instance of CosmosRoutingIsm
pub fn new(conf: &ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: &ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
domain: locator.domain.clone(),
Expand Down
8 changes: 2 additions & 6 deletions rust/chains/hyperlane-cosmos/src/validator_announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ use crate::{
/// A reference to a ValidatorAnnounce contract on some Cosmos chain
#[derive(Debug)]
pub struct CosmosValidatorAnnounce {
_conf: ConnectionConf,
domain: HyperlaneDomain,
address: H256,
_signer: Signer,
provider: Box<WasmGrpcProvider>,
}

impl CosmosValidatorAnnounce {
/// create a new instance of CosmosValidatorAnnounce
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Signer) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer);

Self {
_conf: conf,
domain: locator.domain.clone(),
address: locator.address,
_signer: signer,
provider: Box::new(provider),
}
}
Expand Down
31 changes: 13 additions & 18 deletions rust/hyperlane-base/src/settings/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl ChainConf {
Ok(Box::new(h_cosmos::CosmosMailbox::new(
conf.clone(),
locator.clone(),
signer.clone().unwrap(),
signer.clone(),
)) as Box<dyn Mailbox>)
}
}
Expand Down Expand Up @@ -184,12 +184,9 @@ impl ChainConf {
.map_err(Into::into)
}
ChainConnectionConf::Cosmos(conf) => {
let signer = self.cosmos_signer().await.context(ctx)?.unwrap();
let hook = h_cosmos::CosmosMerkleTreeHook::new(
conf.clone(),
locator.clone(),
signer.clone(),
);
let signer = self.cosmos_signer().await.context(ctx)?;
let hook =
h_cosmos::CosmosMerkleTreeHook::new(conf.clone(), locator.clone(), signer);

Ok(Box::new(hook) as Box<dyn MerkleTreeHook>)
}
Expand Down Expand Up @@ -223,11 +220,11 @@ impl ChainConf {
Ok(indexer as Box<dyn SequenceIndexer<HyperlaneMessage>>)
}
ChainConnectionConf::Cosmos(conf) => {
let signer = self.cosmos_signer().await.context(ctx)?.unwrap();
let signer = self.cosmos_signer().await.context(ctx)?;
let indexer = Box::new(h_cosmos::CosmosMailboxIndexer::new(
conf.clone(),
locator,
signer.clone(),
signer,
"mailbox_dispatch".to_string(),
self.reorg_period,
));
Expand Down Expand Up @@ -263,7 +260,7 @@ impl ChainConf {
Ok(indexer as Box<dyn SequenceIndexer<H256>>)
}
ChainConnectionConf::Cosmos(conf) => {
let signer = self.cosmos_signer().await.context(ctx)?.unwrap();
let signer = self.cosmos_signer().await.context(ctx)?;
let indexer = Box::new(h_cosmos::CosmosMailboxIndexer::new(
conf.clone(),
locator,
Expand Down Expand Up @@ -308,7 +305,7 @@ impl ChainConf {
let paymaster = Box::new(h_cosmos::CosmosInterchainGasPaymaster::new(
conf.clone(),
locator.clone(),
signer.unwrap().clone(),
signer,
));
Ok(paymaster as Box<dyn InterchainGasPaymaster>)
}
Expand Down Expand Up @@ -420,7 +417,7 @@ impl ChainConf {
let va = Box::new(h_cosmos::CosmosValidatorAnnounce::new(
conf.clone(),
locator.clone(),
signer.unwrap().clone(),
signer,
));

Ok(va as Box<dyn ValidatorAnnounce>)
Expand Down Expand Up @@ -460,9 +457,7 @@ impl ChainConf {
ChainConnectionConf::Cosmos(conf) => {
let signer = self.cosmos_signer().await.context(ctx)?;
let ism = Box::new(h_cosmos::CosmosInterchainSecurityModule::new(
conf,
locator,
signer.unwrap(),
conf, locator, signer,
));
Ok(ism as Box<dyn InterchainSecurityModule>)
}
Expand Down Expand Up @@ -496,7 +491,7 @@ impl ChainConf {
let ism = Box::new(h_cosmos::CosmosMultisigIsm::new(
conf.clone(),
locator.clone(),
signer.unwrap().clone(),
signer,
));
Ok(ism as Box<dyn MultisigIsm>)
}
Expand Down Expand Up @@ -530,7 +525,7 @@ impl ChainConf {
let ism = Box::new(h_cosmos::CosmosRoutingIsm::new(
&conf.clone(),
locator.clone(),
signer.unwrap().clone(),
signer,
));
Ok(ism as Box<dyn RoutingIsm>)
}
Expand Down Expand Up @@ -564,7 +559,7 @@ impl ChainConf {
let ism = Box::new(h_cosmos::CosmosAggregationIsm::new(
conf.clone(),
locator.clone(),
signer.unwrap().clone(),
signer,
));

Ok(ism as Box<dyn AggregationIsm>)
Expand Down

0 comments on commit d0036dc

Please sign in to comment.