Skip to content

Commit

Permalink
Zcoin native mode done & multi lightwalletd servers WIP #927 (#1438)
Browse files Browse the repository at this point in the history
* WIP init_native_client,RpcClient, ZcoinNativeClientInitError added

* WIP match added in current_block check

* WIP trying trait Client

* WIP serialized_block added

* WIP remove BlockIDNative, BlockRangeNative

* WIP use if let

* WIP merge fix

* WIP if let added in check_watch_for_tx_existence

* WIP client.clone() added

* WIP match for get_raw_tx added

* WIP

* [r2r] trait impl comment removed

* WIP

* WIP

* WIP

* WIP

* WIP no method named `to_bytes`jubjub

* WIP group crate added

* WIP

* WIP merge fixes

* WIP log! added

* WIP log

* WIP verbose in getblockheader should be boolean

* WIP fee 0 for transparent inputs

* WIP Deafult added for CompactTxNative

* WIP custom structures were removed

* WIP z_coin_grpc compact structs

* WIP

* WIP fix fmt

* WIP

* WIP empty header

* r2r

* r2r

* r2r ZcoinClientInitError

* First review iteration

* WIP trait added

* WIP reference removed

* remove rpc_client, impl additional method

* fix errors

* WIP LightwalletdImpl and LightwalletdConnection added

* r2r native mode support for zcoin

* WIP multi lightwalletd servers support

* WIP use drop_mutability

* WIP added RpcClientInitErr, one cache paths, attempts fixed

* WIP propagate the error instead of panic, mutable watch_for_tx removed from check_watch_for_tx()

* WIP async_blocking added in create_wallet_db

* remove some unnecessary code for multi servers

* remove unused code

* type OnCompactBlockFn added

* rename to check_tx_existence, add return bool, databases init moved to ZCoinBuilder::build

* leave Vec of urls for init_light_client

* fix clippy
  • Loading branch information
laruh committed Sep 12, 2022
1 parent 3a69055 commit ee81219
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 128 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ethkey = { git = "https://github.com/artemii235/parity-ethereum.git" }
futures01 = { version = "0.1", package = "futures" }
# using select macro requires the crate to be named futures, compilation failed with futures03 name
futures = { version = "0.3", package = "futures", features = ["compat", "async-await"] }
group = "0.8.0"
gstuff = { version = "0.7", features = ["nightly"] }
hex = "0.4.2"
http = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,14 @@ pub enum EstimateFeeMethod {
SmartFee,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum BlockNonce {
String(String),
U64(u64),
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct VerboseBlock {
/// Block hash
pub hash: H256Json,
Expand Down Expand Up @@ -1124,7 +1124,7 @@ impl NativeClientImpl {

/// https://developer.bitcoin.org/reference/rpc/getblockheader.html
pub fn get_block_header_bytes(&self, block_hash: H256Json) -> RpcRes<BytesJson> {
let verbose = 0;
let verbose = false;
rpc_func!(self, "getblockheader", block_hash, verbose)
}
}
Expand Down
44 changes: 27 additions & 17 deletions mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ use futures::compat::Future01CompatExt;
use futures::lock::Mutex as AsyncMutex;
use futures::{FutureExt, TryFutureExt};
use futures01::Future;
use http::Uri;
use keys::hash::H256;
use keys::{KeyPair, Message, Public};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use mm2_number::{BigDecimal, MmNumber};
#[cfg(test)] use mocktopus::macros::*;
use parking_lot::Mutex;
use primitives::bytes::Bytes;
use rpc::v1::types::{Bytes as BytesJson, Transaction as RpcTransaction, H256 as H256Json};
use script::{Builder as ScriptBuilder, Opcode, Script, TransactionInputSigner};
use serde_json::Value as Json;
use serialization::CoinVariant;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use zcash_client_backend::data_api::WalletRead;
use zcash_client_backend::encoding::{decode_payment_address, encode_extended_spending_key, encode_payment_address};
Expand All @@ -70,9 +69,10 @@ use z_htlc::{z_p2sh_spend, z_send_dex_fee, z_send_htlc};

mod z_rpc;
pub use z_rpc::SyncStatus;
use z_rpc::{init_light_client, SaplingSyncConnector, SaplingSyncGuard, WalletDbShared};
use z_rpc::{init_light_client, init_native_client, SaplingSyncConnector, SaplingSyncGuard, WalletDbShared};

mod z_coin_errors;
use crate::z_coin::z_rpc::{create_wallet_db, BlockDb};
pub use z_coin_errors::*;

#[cfg(all(test, feature = "zhtlc-native-tests"))]
Expand Down Expand Up @@ -766,29 +766,39 @@ impl<'a> UtxoCoinWithIguanaPrivKeyBuilder for ZCoinBuilder<'a> {
);

let evk = ExtendedFullViewingKey::from(&self.z_spending_key);
let cache_db_path = self.db_dir_path.join(format!("{}_cache.db", self.ticker));
let wallet_db_path = self.db_dir_path.join(format!("{}_wallet.db", self.ticker));
let blocks_db =
async_blocking(|| BlockDb::for_path(cache_db_path).map_to_mm(ZcoinClientInitError::BlocksDbInitFailure))
.await?;
let wallet_db = create_wallet_db(
wallet_db_path,
self.protocol_info.consensus_params.clone(),
self.protocol_info.check_point_block.clone(),
evk,
)
.await?;
let wallet_db = Arc::new(Mutex::new(wallet_db));
let (sync_state_connector, light_wallet_db) = match &self.z_coin_params.mode {
ZcoinRpcMode::Native => {
return MmError::err(ZCoinBuildError::NativeModeIsNotSupportedYet);
let native_client = self.native_client()?;
init_native_client(
native_client,
blocks_db,
wallet_db,
self.protocol_info.consensus_params.clone(),
)
.await?
},
ZcoinRpcMode::Light {
light_wallet_d_servers, ..
} => {
let cache_db_path = self.db_dir_path.join(format!("{}_light_cache.db", self.ticker));
let wallet_db_path = self.db_dir_path.join(format!("{}_light_wallet.db", self.ticker));
// TODO multi lightwalletd servers support will be added on the next iteration
let uri = Uri::from_str(
light_wallet_d_servers
.first()
.or_mm_err(|| ZCoinBuildError::EmptyLightwalletdUris)?,
)?;

init_light_client(
uri,
cache_db_path,
wallet_db_path,
light_wallet_d_servers.clone(),
blocks_db,
wallet_db,
self.protocol_info.consensus_params.clone(),
self.protocol_info.check_point_block,
evk,
)
.await?
},
Expand Down
37 changes: 24 additions & 13 deletions mm2src/coins/z_coin/z_coin_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::utxo::rpc_clients::UtxoRpcError;
use crate::utxo::utxo_builder::UtxoCoinBuildError;
use crate::WithdrawError;
use crate::{NumConversError, PrivKeyNotAllowed};
use common::jsonrpc_client::JsonRpcError;
use db_common::sqlite::rusqlite::Error as SqliteError;
use db_common::sqlite::rusqlite::Error as SqlError;
use derive_more::Display;
Expand All @@ -19,6 +20,9 @@ pub enum UpdateBlocksCacheErr {
GrpcError(tonic::Status),
BlocksDbError(SqliteError),
ZcashSqliteError(ZcashClientError),
UtxoRpcError(UtxoRpcError),
InternalError(String),
JsonRpcError(JsonRpcError),
}

impl From<tonic::Status> for UpdateBlocksCacheErr {
Expand All @@ -33,18 +37,32 @@ impl From<ZcashClientError> for UpdateBlocksCacheErr {
fn from(err: ZcashClientError) -> Self { UpdateBlocksCacheErr::ZcashSqliteError(err) }
}

impl From<UtxoRpcError> for UpdateBlocksCacheErr {
fn from(err: UtxoRpcError) -> Self { UpdateBlocksCacheErr::UtxoRpcError(err) }
}

impl From<JsonRpcError> for UpdateBlocksCacheErr {
fn from(err: JsonRpcError) -> Self { UpdateBlocksCacheErr::JsonRpcError(err) }
}

#[derive(Debug, Display)]
#[non_exhaustive]
pub enum ZcoinLightClientInitError {
pub enum ZcoinClientInitError {
TlsConfigFailure(tonic::transport::Error),
ConnectionFailure(tonic::transport::Error),
BlocksDbInitFailure(SqliteError),
WalletDbInitFailure(SqliteError),
ZcashSqliteError(ZcashClientError),
EmptyLightwalletdUris,
InvalidUri(InvalidUri),
}

impl From<ZcashClientError> for ZcoinLightClientInitError {
fn from(err: ZcashClientError) -> Self { ZcoinLightClientInitError::ZcashSqliteError(err) }
impl From<ZcashClientError> for ZcoinClientInitError {
fn from(err: ZcashClientError) -> Self { ZcoinClientInitError::ZcashSqliteError(err) }
}

impl From<InvalidUri> for ZcoinClientInitError {
fn from(err: InvalidUri) -> Self { ZcoinClientInitError::InvalidUri(err) }
}

#[derive(Debug, Display)]
Expand Down Expand Up @@ -182,10 +200,7 @@ pub enum ZCoinBuildError {
path: String,
},
Io(std::io::Error),
EmptyLightwalletdUris,
NativeModeIsNotSupportedYet,
InvalidLightwalletdUri(InvalidUri),
LightClientInitErr(ZcoinLightClientInitError),
RpcClientInitErr(ZcoinClientInitError),
ZCashParamsNotFound,
}

Expand All @@ -205,12 +220,8 @@ impl From<std::io::Error> for ZCoinBuildError {
fn from(err: std::io::Error) -> ZCoinBuildError { ZCoinBuildError::Io(err) }
}

impl From<InvalidUri> for ZCoinBuildError {
fn from(err: InvalidUri) -> Self { ZCoinBuildError::InvalidLightwalletdUri(err) }
}

impl From<ZcoinLightClientInitError> for ZCoinBuildError {
fn from(err: ZcoinLightClientInitError) -> Self { ZCoinBuildError::LightClientInitErr(err) }
impl From<ZcoinClientInitError> for ZCoinBuildError {
fn from(err: ZcoinClientInitError) -> Self { ZCoinBuildError::RpcClientInitErr(err) }
}

pub(super) enum SqlTxHistoryError {
Expand Down
Loading

0 comments on commit ee81219

Please sign in to comment.