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

Commit

Permalink
basic mocking data for tendermint data types
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Nov 12, 2019
1 parent 379fe8a commit dc36832
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 42 deletions.
31 changes: 3 additions & 28 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion client-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jsonrpc = { version = "0.11", optional = true }
serde_json = { version = "1.0", optional = true }
parity-scale-codec = { features = ["derive"], version = "1.0" }
websocket = { version = "0.24", default-features = false, features = ["sync"], optional = true }
tendermint = { git = "https://github.com/crypto-com/tendermint-rs.git", rev="95823039071a44dabc24825b777d3c5b95a6665b" }
tendermint = { git = "https://github.com/crypto-com/tendermint-rs.git", rev = "bb27fdc5dd0ce59767badab246d1d4c203cf3d23" }

[features]
default = ["sled", "websocket-rpc"]
Expand Down
6 changes: 3 additions & 3 deletions client-common/src/tendermint/types.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//! Structures used in Tendermint RPC (auto-generated)
//! Structures used in Tendermint RPC
mod block_results;
pub mod mock;

use base64::decode;
use parity_scale_codec::Decode;
use serde::{Deserialize, Serialize};

pub use self::block_results::*;
use crate::{ErrorKind, Result, ResultExt, Transaction};

use chain_core::init::config::InitConfig;
use chain_core::tx::fee::LinearFee;
use chain_core::tx::{TxAux, TxEnclaveAux};

pub use self::block_results::*;
pub use tendermint::rpc::endpoint::{
abci_query::AbciQuery, abci_query::Response as AbciQueryResponse,
block::Response as BlockResponse, broadcast::tx_sync::Response as BroadcastTxResponse,
Expand Down
121 changes: 121 additions & 0 deletions client-common/src/tendermint/types/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#![allow(missing_docs)]
use super::*;
use serde_json;
use std::str::FromStr;
use tendermint::{
account, block, chain, channel, net, node, validator, vote, Moniker, PrivateKey, PublicKey,
Version,
};

static DEFAULT_VALIDATOR_KEY: &str = "{
\"type\": \"tendermint/PrivKeyEd25519\",
\"value\": \"gJWgIetdLxRc/C2t/XjV65NCqZLTqHS9pU69kBzRmyOKCHDqT/6bKPmKajdBp+KCbYtu9ttTX7+MXrEQOw8Kqg==\"
}";
static BLOCK_VERSION: u64 = 10;
static APP_VERSION: u64 = 0;
static APP_HASH: &str = "93E6C15E5A52CAAB971A810E5F6F9C4965AA102C81120FCEDCB7F8A112270380";

pub fn validator_priv_key() -> PrivateKey {
serde_json::from_str(DEFAULT_VALIDATOR_KEY).unwrap()
}

pub fn validator_pub_key() -> PublicKey {
validator_priv_key().public_key()
}

pub fn validator_account_id() -> account::Id {
validator_pub_key().into()
}

pub fn default_chain_id() -> chain::Id {
chain::Id::from_str("test-chain-ktDVXo-AB").unwrap()
}

pub fn validator_info() -> validator::Info {
validator::Info {
address: validator_account_id(),
pub_key: validator_pub_key(),
voting_power: vote::Power::new(12500000000),
proposer_priority: None,
}
}

pub fn node_info() -> node::Info {
node::Info {
protocol_version: node::info::ProtocolVersionInfo {
p2p: 7,
block: BLOCK_VERSION,
app: APP_VERSION,
},
id: node::Id::from_str("7edc638f79308dfdfcd77b743e1375b8e1cea6f2").unwrap(),
listen_addr: node::info::ListenAddress::new("tcp://0.0.0.0:26656".to_owned()),
network: default_chain_id(),
version: Version::default(),
channels: channel::Channels::default(),
moniker: Moniker::from_str("test").unwrap(),
other: node::info::OtherInfo {
tx_index: node::info::TxIndexStatus::On,
rpc_address: net::Address::from_str("tcp://127.0.0.1:26657").unwrap(),
},
}
}

pub fn default_header() -> Header {
Header {
version: block::header::Version {
block: BLOCK_VERSION,
app: APP_VERSION,
},
chain_id: default_chain_id(),
height: Height::default(),
time: Time::default(),
num_txs: 0,
total_txs: 0,
last_block_id: block::Id::default(),
last_commit_hash: Hash::default(),
data_hash: Hash::default(),
validators_hash: Hash::default(),
next_validators_hash: Hash::default(),
consensus_hash: Hash::default(),
app_hash: Hash::from_str(APP_HASH).unwrap(),
last_results_hash: Hash::default(),
evidence_hash: Hash::default(),
proposer_address: validator_account_id(),
}
}

pub fn default_block() -> Block {
Block {
header: default_header(),
data: Default::default(),
evidence: Default::default(),
last_commit: block::Commit {
block_id: Default::default(),
precommits: Default::default(),
},
}
}

pub fn status_response() -> Status {
Status {
node_info: node_info(),
sync_info: status::SyncInfo::default(),
validator_info: validator_info(),
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;

#[test]
fn check_status_response() {
println!("{}", serde_json::to_string(&status_response()).unwrap());
}

#[test]
fn check_default_header() {
println!("{}", serde_json::to_string(&default_header()).unwrap());
}
}
11 changes: 6 additions & 5 deletions client-core/src/synchronizer/manual_synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ mod tests {
};
use chain_core::tx::TxAux;
use client_common::storage::MemoryStorage;
use client_common::tendermint::types::mock::*;
use client_common::tendermint::types::*;
use client_common::ErrorKind;

Expand Down Expand Up @@ -361,7 +362,7 @@ mod tests {
.unwrap(),
..Default::default()
},
..Default::default()
..status_response()
})
}

Expand All @@ -375,9 +376,9 @@ mod tests {
.unwrap(),
height: height.into(),
time: Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
..Default::default()
..default_header()
},
..Default::default()
..default_block()
})
} else if height == 2 {
Ok(Block {
Expand All @@ -388,10 +389,10 @@ mod tests {
.unwrap(),
height: height.into(),
time: Time::from_str("2019-04-10T09:38:41.735577Z").unwrap(),
..Default::default()
..default_header()
},
data: Data::new(vec![abci::Transaction::new(unbond_transaction().encode())]),
..Default::default()
..default_block()
})
} else {
Err(ErrorKind::InvalidInput.into())
Expand Down
11 changes: 6 additions & 5 deletions client-rpc/src/rpc/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ pub mod tests {
use chain_core::tx::fee::{Fee, FeeAlgorithm};
use chain_core::tx::{PlainTxAux, TransactionId, TxAux, TxObfuscated};
use client_common::storage::MemoryStorage;
use client_common::tendermint::types::mock::*;
use client_common::tendermint::types::*;
use client_common::tendermint::Client;
use client_common::{
Expand Down Expand Up @@ -384,7 +385,7 @@ pub mod tests {
.unwrap(),
..Default::default()
},
..Default::default()
..status_response()
})
}

Expand All @@ -396,9 +397,9 @@ pub mod tests {
)
.unwrap(),
time: Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
..Default::default()
..default_header()
},
..Default::default()
..default_block()
})
}

Expand All @@ -413,9 +414,9 @@ pub mod tests {
)
.unwrap(),
time: Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
..Default::default()
..default_header()
},
..Default::default()
..default_block()
}])
}

Expand Down

0 comments on commit dc36832

Please sign in to comment.