Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Problem: (fix #1658) no api for minimum unbonded time in client-rpc
Browse files Browse the repository at this point in the history
Solution: add genesis, status api for client-rpc
change label


remove redundant code
  • Loading branch information
leejw51crypto committed May 26, 2020
1 parent 0c1afdb commit 633c655
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
8 changes: 7 additions & 1 deletion client-network/src/network_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod default_network_ops_client;

pub use self::default_network_ops_client::DefaultNetworkOpsClient;

use chain_core::init::coin::Coin;
use chain_core::state::account::{
CouncilNode, StakedState, StakedStateAddress, StakedStateOpAttributes,
Expand All @@ -12,6 +11,7 @@ use chain_core::tx::data::attribute::TxAttributes;
use chain_core::tx::data::input::TxoPointer;
use chain_core::tx::data::output::TxOut;
use chain_core::tx::TxAux;
use client_common::tendermint::types::{Genesis, StatusResponse};
use client_common::{ErrorKind, Result, ResultExt, SecKey};
use client_core::types::TransactionPending;

Expand Down Expand Up @@ -103,4 +103,10 @@ pub trait NetworkOpsClient: Send + Sync {
address: &StakedStateAddress,
verify: bool,
) -> Result<Option<StakedState>>;

/// Return genesis of tendermint
fn get_genesis(&self) -> Result<Genesis>;

/// Return status response
fn get_status(&self) -> Result<StatusResponse>;
}
19 changes: 16 additions & 3 deletions client-network/src/network_ops/default_network_ops_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use chain_core::tx::fee::FeeAlgorithm;
use chain_core::tx::{TxAux, TxPublicAux};
use chain_storage::jellyfish::SparseMerkleProof;
use chain_tx_validation::{check_inputs_basic, check_outputs_basic, verify_unjailed};
use client_common::tendermint::types::AbciQueryExt;
use client_common::tendermint::types::{AbciQueryExt, Genesis, StatusResponse};
use client_common::tendermint::Client;
use client_common::{
Error, ErrorKind, Result, ResultExt, SecKey, SignedTransaction, Storage, Transaction,
Expand All @@ -28,6 +28,7 @@ use client_core::{TransactionObfuscation, UnspentTransactions, WalletClient};
use tendermint::{block::Height, Time};

/// Default implementation of `NetworkOpsClient`
#[derive(Clone)]
pub struct DefaultNetworkOpsClient<W, S, C, F, E>
where
W: WalletClient,
Expand Down Expand Up @@ -254,11 +255,15 @@ where
) -> Result<(TxAux, TransactionPending)> {
let last_block_time = self.get_last_block_time()?;
let staked_state = self.get_staked_state(name, from_address, verify_staking)?;

if staked_state.unbonded_from > last_block_time {
let seconds = staked_state.unbonded_from - last_block_time;
let duration = std::time::Duration::from_secs(seconds);
return Err(Error::new(
ErrorKind::ValidationError,
"Staking state is not yet unbonded",
format!(
"Staking state is not yet unbonded, time left: {:?}",
duration
),
));
}

Expand Down Expand Up @@ -514,6 +519,14 @@ where
};
Ok(mstaking)
}

fn get_genesis(&self) -> Result<Genesis> {
self.client.genesis()
}

fn get_status(&self) -> Result<StatusResponse> {
self.client.status()
}
}

fn to_timespec(time: Time) -> Timespec {
Expand Down
6 changes: 5 additions & 1 deletion client-rpc/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use client_core::wallet::DefaultWalletClient;
use client_network::network_ops::DefaultNetworkOpsClient;

use crate::rpc::{
info_rpc::{InfoRpc, InfoRpcImpl},
multisig_rpc::{MultiSigRpc, MultiSigRpcImpl},
staking_rpc::{StakingRpc, StakingRpcImpl},
sync_rpc::{CBindingCore, SyncRpc, SyncRpcImpl},
Expand Down Expand Up @@ -75,7 +76,9 @@ impl RpcHandler {

let multisig_rpc = MultiSigRpcImpl::new(wallet_client.clone());
let transaction_rpc = TransactionRpcImpl::new(network_id);
let staking_rpc = StakingRpcImpl::new(wallet_client.clone(), ops_client, network_id);
let staking_rpc =
StakingRpcImpl::new(wallet_client.clone(), ops_client.clone(), network_id);
let info_rpc = InfoRpcImpl::new(ops_client);

let sync_wallet_client =
make_wallet_client(storage, tendermint_client, fee_policy, obfuscator)?;
Expand All @@ -88,6 +91,7 @@ impl RpcHandler {
io.extend_with(staking_rpc.to_delegate());
io.extend_with(sync_rpc.to_delegate());
io.extend_with(wallet_rpc.to_delegate());
io.extend_with(info_rpc.to_delegate());

Ok(RpcHandler { io })
}
Expand Down
1 change: 1 addition & 0 deletions client-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod info_rpc;
pub mod multisig_rpc;
pub mod staking_rpc;
pub mod sync_rpc;
Expand Down
42 changes: 42 additions & 0 deletions client-rpc/src/rpc/info_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

use crate::to_rpc_error;
use client_common::tendermint::types::{Genesis, StatusResponse};
use client_network::NetworkOpsClient;

#[rpc(server)]
pub trait InfoRpc: Send + Sync {
#[rpc(name = "genesis")]
fn genesis(&self) -> Result<Genesis>;
#[rpc(name = "status")]
fn status(&self) -> Result<StatusResponse>;
}

pub struct InfoRpcImpl<N>
where
N: NetworkOpsClient,
{
ops_client: N,
}

impl<N> InfoRpcImpl<N>
where
N: NetworkOpsClient,
{
pub fn new(ops_client: N) -> Self {
InfoRpcImpl { ops_client }
}
}

impl<N> InfoRpc for InfoRpcImpl<N>
where
N: NetworkOpsClient + 'static,
{
fn genesis(&self) -> Result<Genesis> {
self.ops_client.get_genesis().map_err(to_rpc_error)
}
fn status(&self) -> Result<StatusResponse> {
self.ops_client.get_status().map_err(to_rpc_error)
}
}

0 comments on commit 633c655

Please sign in to comment.