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 7, 2023
1 parent ad875bf commit 00f3b0c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 61 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 @@ -23,8 +23,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 @@ -44,8 +44,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: 11 additions & 6 deletions rust/chains/hyperlane-cosmos/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ use tracing::{instrument, warn};

/// A reference to a Mailbox contract on some Cosmos chain
pub struct CosmosMailbox {
config: 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 {
let provider = WasmGrpcProvider::new(conf, locator.clone(), signer.clone());
pub fn new(conf: ConnectionConf, locator: ContractLocator, signer: Option<Signer>) -> Self {
let provider = WasmGrpcProvider::new(conf.clone(), locator.clone(), signer.clone());

Self {
config: 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.config.get_prefix()
}
}

impl HyperlaneContract for CosmosMailbox {
Expand Down Expand Up @@ -136,7 +141,7 @@ impl Mailbox for CosmosMailbox {

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

let payload = mailbox::RecipientIsmRequest {
recipient_ism: mailbox::RecipientIsmRequestInner {
Expand Down Expand Up @@ -256,7 +261,7 @@ impl CosmosMailboxIndexer {
pub fn new(
conf: ConnectionConf,
locator: ContractLocator,
signer: Signer,
signer: Option<Signer>,
reorg_period: u32,
) -> ChainResult<Self> {
let mailbox = CosmosMailbox::new(conf.clone(), locator.clone(), signer.clone());
Expand Down
6 changes: 3 additions & 3 deletions rust/chains/hyperlane-cosmos/src/merkle_tree_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub struct CosmosMerkleTreeHook {

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 {
domain: locator.domain.clone(),
Expand Down Expand Up @@ -189,7 +189,7 @@ impl CosmosMerkleTreeHookIndexer {
pub fn new(
conf: ConnectionConf,
locator: ContractLocator,
signer: Signer,
signer: Option<Signer>,
reorg_period: u32,
) -> ChainResult<Self> {
let indexer = CosmosWasmIndexer::new(
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 @@ -27,8 +27,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
33 changes: 14 additions & 19 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().expect("Cosmos signer not configured"),
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,
self.reorg_period,
)?);
Ok(indexer as Box<dyn SequenceIndexer<HyperlaneMessage>>)
Expand Down Expand Up @@ -262,7 +259,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 @@ -306,7 +303,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 @@ -389,7 +386,7 @@ impl ChainConf {
conf.clone(),
locator,
// TODO: remove signer requirement entirely
signer.unwrap().clone(),
signer,
self.reorg_period,
)?);
Ok(indexer as Box<dyn SequenceIndexer<MerkleTreeInsertion>>)
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 00f3b0c

Please sign in to comment.