From 6a8fc4d28e16c17174681c27a893b78beb86628d Mon Sep 17 00:00:00 2001 From: Ash Manning Date: Tue, 5 Nov 2024 23:43:27 +0800 Subject: [PATCH] update to latest proto --- app/app.rs | 1 + lib/types/proto.rs | 67 +++++++++++++++++++++++++++++----------------- proto | 2 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/app/app.rs b/app/app.rs index 8824e85..60a87a6 100644 --- a/app/app.rs +++ b/app/app.rs @@ -187,6 +187,7 @@ impl App { format!("https://{}", config.main_addr), ) .unwrap() + .concurrency_limit(256) .connect_lazy(); let (cusf_mainchain, cusf_mainchain_wallet) = if runtime.block_on( Self::check_proto_support(config.main_addr, transport.clone()), diff --git a/lib/types/proto.rs b/lib/types/proto.rs index 5d4ed3d..d27b6b7 100644 --- a/lib/types/proto.rs +++ b/lib/types/proto.rs @@ -104,10 +104,10 @@ pub mod common { tonic::include_proto!("cusf.common.v1"); } - pub use generated::{ConsensusHex, ReverseHex}; + pub use generated::{ConsensusHex, Hex, ReverseHex}; impl ConsensusHex { - pub fn consensus_decode( + pub fn decode( self, field_name: &str, ) -> Result @@ -124,7 +124,7 @@ pub mod common { } /// Variant of [`Self::decode`] that returns a `tonic::Status` error - pub fn consensus_decode_tonic( + pub fn decode_tonic( self, field_name: &str, ) -> Result @@ -132,18 +132,20 @@ pub mod common { Message: prost::Name, T: bitcoin::consensus::Decodable, { - self.consensus_decode::(field_name) + self.decode::(field_name) .map_err(|err| tonic::Status::from_error(Box::new(err))) } - pub fn consensus_encode(value: &T) -> Self + pub fn encode(value: &T) -> Self where T: bitcoin::consensus::Encodable, { let hex = bitcoin::consensus::encode::serialize_hex(value); Self { hex: Some(hex) } } + } + impl Hex { pub fn decode( self, field_name: &str, @@ -162,6 +164,14 @@ pub mod common { super::Error::invalid_field_value::(field_name, &hex) }) } + + pub fn encode(value: &T) -> Self + where + T: hex::ToHex, + { + let hex = value.encode_hex(); + Self { hex: Some(hex) } + } } impl ReverseHex { @@ -221,7 +231,7 @@ pub mod mainchain { use serde::{Deserialize, Serialize}; use thiserror::Error; - use super::common::{ConsensusHex, ReverseHex}; + use super::common::{ConsensusHex, Hex, ReverseHex}; use crate::types::{Output, OutputContent, THIS_SIDECHAIN}; pub mod generated { @@ -297,7 +307,7 @@ pub mod mainchain { return Ok(None); }; commitment.decode::("commitment") - .map(Some) + .map(|block_hash| Some(crate::types::BlockHash(block_hash))) } } @@ -338,19 +348,28 @@ pub mod mainchain { } } - impl TryFrom for Output { + impl TryFrom for Output { type Error = super::Error; - fn try_from(output: generated::Output) -> Result { - let generated::Output { + fn try_from( + output: generated::deposit::Output, + ) -> Result { + let generated::deposit::Output { address, value_sats, } = output; let address = address .ok_or_else(|| { - super::Error::missing_field::("address") + super::Error::missing_field::( + "address", + ) })? - .decode::("address")?; + .decode::("address")?; + let value_sats = value_sats.ok_or_else(|| { + super::Error::missing_field::( + "value_sats", + ) + })?; Ok(Self { address, content: OutputContent::Value(value_sats), @@ -384,10 +403,15 @@ pub mod mainchain { fn try_from(deposit: generated::Deposit) -> Result { let generated::Deposit { - sequence_number: tx_index, + sequence_number, outpoint, output, } = deposit; + let sequence_number = sequence_number.ok_or_else(|| { + super::Error::missing_field::( + "sequence_number", + ) + })?; let Some(outpoint) = outpoint else { return Err(super::Error::missing_field::( "outpoint", @@ -399,7 +423,7 @@ pub mod mainchain { )); }; Ok(Self { - tx_index, + tx_index: sequence_number, outpoint: outpoint.try_into()?, output: output.try_into()?, }) @@ -459,9 +483,7 @@ pub mod mainchain { generated::WithdrawalBundleEvent, >("m6id") })? - .consensus_decode::( - "m6id", - )?; + .decode::("m6id")?; let status = generated::WithdrawalBundleEventType::try_from( withdrawal_bundle_event_type, ) @@ -511,6 +533,7 @@ pub mod mainchain { .map(|bmm_commitment| { bmm_commitment .decode::("bmm_commitment") + .map(crate::types::BlockHash) }) .transpose()?; Ok(Self { @@ -625,7 +648,7 @@ pub mod mainchain { "work", ) })? - .consensus_decode::("work") + .decode::("work") .map(bitcoin::Work::from_le_bytes)?; Ok(BlockHeaderInfo { block_hash, @@ -903,9 +926,7 @@ pub mod mainchain { sidechain_id: Some(THIS_SIDECHAIN as u32), value_sats: Some(value_sats), height: Some(height), - critical_hash: Some(ConsensusHex::consensus_encode( - &critical_hash, - )), + critical_hash: Some(ConsensusHex::encode(&critical_hash)), prev_bytes: Some(ReverseHex::encode(&prev_bytes)), }; let generated::CreateBmmCriticalDataTransactionResponse { txid } = @@ -927,9 +948,7 @@ pub mod mainchain { ) -> Result { let request = generated::CreateDepositTransactionRequest { sidechain_id: Some(THIS_SIDECHAIN as u32), - address: Some(ConsensusHex::consensus_encode( - &address.0.to_vec(), - )), + address: Some(Hex::encode(&address.0)), value_sats: Some(value_sats), fee_sats: Some(fee_sats), }; diff --git a/proto b/proto index 1182ca0..bd6f850 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 1182ca0f6990ab9b309a0f0d2dc27dc6e81b6c76 +Subproject commit bd6f85098b4ecd614f54f36caa9d5cd544d615b5