From 9e00e99c2143f66a5dd61d80cb5c292a77eefbc6 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 19 Nov 2019 16:57:05 +0800 Subject: [PATCH] add mock testing for lite client --- tendermint/src/lite/types.rs | 1 + tendermint/tests/lite.rs | 28 +++++++++ tendermint/tests/support/lite/basic.json | 78 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 tendermint/tests/lite.rs create mode 100644 tendermint/tests/support/lite/basic.json diff --git a/tendermint/src/lite/types.rs b/tendermint/src/lite/types.rs index ddf5534d3..a8f95bc5f 100644 --- a/tendermint/src/lite/types.rs +++ b/tendermint/src/lite/types.rs @@ -102,6 +102,7 @@ pub trait Vote { fn signature(&self) -> &[u8]; } +#[derive(Debug)] pub enum Error { Expired, NonSequentialHeight, diff --git a/tendermint/tests/lite.rs b/tendermint/tests/lite.rs new file mode 100644 index 000000000..251a8f916 --- /dev/null +++ b/tendermint/tests/lite.rs @@ -0,0 +1,28 @@ +use serde::{Deserialize, Serialize}; +use serde_json; +use std::{fs, path::PathBuf}; +use tendermint::{lite, rpc::endpoint::commit::SignedHeader, validator}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +struct TestSuite { + signed_header: SignedHeader, + last_validators: Vec, + validators: Vec, +} + +fn read_json_fixture(name: &str) -> String { + fs::read_to_string(PathBuf::from("./tests/support/lite/").join(name.to_owned() + ".json")) + .unwrap() +} + +#[test] +fn check_verifier_with_mock_data() { + let suite: TestSuite = serde_json::from_str(&read_json_fixture("basic")).unwrap(); + lite::verify_trusting( + suite.signed_header.header.clone(), + suite.signed_header, + validator::Set::new(suite.last_validators), + validator::Set::new(suite.validators), + ) + .expect("verify_trusting failed"); +} diff --git a/tendermint/tests/support/lite/basic.json b/tendermint/tests/support/lite/basic.json new file mode 100644 index 000000000..eb7c013ab --- /dev/null +++ b/tendermint/tests/support/lite/basic.json @@ -0,0 +1,78 @@ +{ + "signed_header": { + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "test-chain-y3m1e6-AB", + "height": "1", + "time": "2019-11-19T07:56:45.46738Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "", + "parts": { + "total": "0", + "hash": "" + } + }, + "last_commit_hash": "", + "data_hash": "", + "validators_hash": "1F0F3E080DCE3253C88394F2CE479C074C5A25FE94A6C8560C76B6F2281BD685", + "next_validators_hash": "1F0F3E080DCE3253C88394F2CE479C074C5A25FE94A6C8560C76B6F2281BD685", + "consensus_hash": "048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F", + "app_hash": "2E419E5A3CDD1F718EA956E9AF91E57FD4FD5D8C8DDEA43FB3D8728E8E41852D", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "30B3C78C25BB72796960B63AA06CA7ED5F648951" + }, + "commit": { + "block_id": { + "hash": "0D06FAEB490248DC504CDD2B4318BEFCB2FD06F866BACAE9864CC5B7373D2A82", + "parts": { + "total": "1", + "hash": "C74C19586564ECC8A348BAE68D505230618B9FABB291549FFD59A6322E961865" + } + }, + "precommits": [ + { + "type": 2, + "height": "1", + "round": "0", + "block_id": { + "hash": "0D06FAEB490248DC504CDD2B4318BEFCB2FD06F866BACAE9864CC5B7373D2A82", + "parts": { + "total": "1", + "hash": "C74C19586564ECC8A348BAE68D505230618B9FABB291549FFD59A6322E961865" + } + }, + "timestamp": "2019-11-19T07:57:02.408782Z", + "validator_address": "30B3C78C25BB72796960B63AA06CA7ED5F648951", + "validator_index": "0", + "signature": "C/AVaOIcMyXozkyMiWFAB2/hHZeBKCZ97W+ivbJtBlgwYxyVRZeQQhBF+8uxr0vvpbKDEKUIxaq8Zng1nN5LAA==" + } + ] + } + }, + "last_validators": [ + { + "address": "30B3C78C25BB72796960B63AA06CA7ED5F648951", + "pub_key": { + "type": "tendermint/PubKeyEd25519", + "value": "ZncnqXOpTbsTYZqkbdzHne9y1h8Z1gp3Xlayfi4QzW0=" + }, + "voting_power": "12500000000" + } + ], + "validators": [ + { + "address": "30B3C78C25BB72796960B63AA06CA7ED5F648951", + "pub_key": { + "type": "tendermint/PubKeyEd25519", + "value": "ZncnqXOpTbsTYZqkbdzHne9y1h8Z1gp3Xlayfi4QzW0=" + }, + "voting_power": "12500000000" + } + ] +}