-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added config options & copied code into new app crate
- copied from tendermint-lite/src/main.rs to lite-node/src/command/start.rs
- Loading branch information
Showing
8 changed files
with
343 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use tendermint::block; | ||
use tendermint::lite; | ||
use tendermint::rpc; | ||
use tendermint::validator; | ||
|
||
use core::future::Future; | ||
use tokio::runtime::Builder; | ||
|
||
/// RPCRequester wraps the Tendermint rpc::Client. | ||
pub struct RPCRequester { | ||
client: rpc::Client, | ||
} | ||
|
||
impl RPCRequester { | ||
pub fn new(client: rpc::Client) -> Self { | ||
RPCRequester { client } | ||
} | ||
} | ||
|
||
impl lite::types::Requester for RPCRequester { | ||
type SignedHeader = block::signed_header::SignedHeader; | ||
type ValidatorSet = validator::Set; | ||
|
||
/// Request the signed header at height h. | ||
/// If h==0, request the latest signed header. | ||
/// TODO: use an enum instead of h==0. | ||
fn signed_header<H>(&self, h: H) -> Result<Self::SignedHeader, lite::Error> | ||
where | ||
H: Into<block::Height>, | ||
{ | ||
let height: block::Height = h.into(); | ||
let r = match height.value() { | ||
0 => block_on(self.client.latest_commit()), | ||
_ => block_on(self.client.commit(height)), | ||
}; | ||
match r { | ||
Ok(response) => Ok(response.signed_header), | ||
Err(_error) => Err(lite::Error::RequestFailed), | ||
} | ||
} | ||
|
||
/// Request the validator set at height h. | ||
fn validator_set<H>(&self, h: H) -> Result<Self::ValidatorSet, lite::Error> | ||
where | ||
H: Into<block::Height>, | ||
{ | ||
let r = block_on(self.client.validators(h)); | ||
match r { | ||
Ok(response) => Ok(validator::Set::new(response.validators)), | ||
Err(_error) => Err(lite::Error::RequestFailed), | ||
} | ||
} | ||
} | ||
|
||
pub fn block_on<F: Future>(future: F) -> F::Output { | ||
Builder::new() | ||
.basic_scheduler() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
.block_on(future) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tendermint::lite::types::Header as LiteHeader; | ||
use tendermint::lite::types::Requester as LiteRequester; | ||
use tendermint::lite::types::SignedHeader as LiteSignedHeader; | ||
use tendermint::lite::types::ValidatorSet as LiteValSet; | ||
use tendermint::rpc; | ||
|
||
// TODO: integration test | ||
#[test] | ||
#[ignore] | ||
fn test_val_set() { | ||
let client = block_on(rpc::Client::new(&"localhost:26657".parse().unwrap())).unwrap(); | ||
let req = RPCRequester::new(client); | ||
let r1 = req.validator_set(5).unwrap(); | ||
let r2 = req.signed_header(5).unwrap(); | ||
assert_eq!(r1.hash(), r2.header().validators_hash()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use tendermint::lite::TrustedState; | ||
use tendermint::{block::signed_header::SignedHeader, validator::Set}; | ||
|
||
#[derive(Clone)] | ||
pub struct State { | ||
last_header: SignedHeader, | ||
vals: Set, | ||
} | ||
|
||
impl TrustedState for State { | ||
type LastHeader = SignedHeader; | ||
type ValidatorSet = Set; | ||
|
||
fn new(last_header: &Self::LastHeader, vals: &Self::ValidatorSet) -> Self { | ||
State { | ||
last_header: last_header.clone(), | ||
vals: vals.clone(), | ||
} | ||
} | ||
|
||
// height H-1 | ||
fn last_header(&self) -> &Self::LastHeader { | ||
&self.last_header | ||
} | ||
|
||
// height H | ||
fn validators(&self) -> &Self::ValidatorSet { | ||
&self.vals | ||
} | ||
} |
Oops, something went wrong.