-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
first take at hooking up a node #117
Merged
ebuchman
merged 5 commits into
bucky/lite-reqester-store-impl
from
bucky/lite-node-impl
Dec 31, 2019
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod requester; | ||
pub mod state; | ||
pub mod store; | ||
pub mod threshold; |
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,134 @@ | ||
use tendermint::hash; | ||
use tendermint::lite; | ||
use tendermint::lite::Error; | ||
use tendermint::lite::{ | ||
Header as _, Requester as _, SignedHeader as _, Store as _, TrustedState as _, | ||
ValidatorSet as _, | ||
}; | ||
use tendermint::rpc; | ||
use tendermint::{block::Height, Hash}; | ||
|
||
use tendermint_lite::{ | ||
requester::Requester, state::State, store::MemStore, threshold::TrustThresholdOneThird, | ||
}; | ||
|
||
use core::future::Future; | ||
use std::time::{Duration, SystemTime}; | ||
use tokio::runtime::Builder; | ||
|
||
// TODO: these should be config/args | ||
static SUBJECTIVE_HEIGHT: u64 = 1; | ||
static SUBJECTIVE_VALS_HASH_HEX: &str = | ||
"A5A7DEA707ADE6156F8A981777CA093F178FC790475F6EC659B6617E704871DD"; | ||
static RPC_ADDR: &str = "localhost:26657"; | ||
|
||
// TODO: this should somehow be configurable ... | ||
static THRESHOLD: &TrustThresholdOneThird = &TrustThresholdOneThird {}; | ||
|
||
pub fn block_on<F: Future>(future: F) -> F::Output { | ||
Builder::new() | ||
.basic_scheduler() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
.block_on(future) | ||
} | ||
|
||
fn main() { | ||
// TODO: this should be config | ||
let trusting_period = Duration::new(600, 0); | ||
|
||
// setup requester for primary peer | ||
let client = block_on(rpc::Client::new(&RPC_ADDR.parse().unwrap())).unwrap(); | ||
let req = Requester::new(client); | ||
let mut store = MemStore::new(); | ||
|
||
let vals_hash = | ||
Hash::from_hex_upper(hash::Algorithm::Sha256, SUBJECTIVE_VALS_HASH_HEX).unwrap(); | ||
|
||
validate_and_init_subjective_state( | ||
Height::from(SUBJECTIVE_HEIGHT), | ||
vals_hash, | ||
&mut store, | ||
&req, | ||
) | ||
.unwrap(); | ||
|
||
loop { | ||
// NOTE: 0 is a bad idea. use an Enum{ Height, LatestHeight } or something | ||
// instead .. | ||
let latest = (&req).signed_header(0).unwrap(); | ||
let latest_height = latest.header().height(); | ||
|
||
println!( | ||
"attempting bisection from height {:?} to height {:?}", | ||
store | ||
.get(Height::from(0)) | ||
.unwrap() | ||
.last_header() | ||
.header() | ||
.height(), | ||
latest_height, | ||
); | ||
|
||
let now = &SystemTime::now(); | ||
lite::verify_and_update_bisection( | ||
latest_height, | ||
THRESHOLD, | ||
&trusting_period, | ||
now, | ||
&req, | ||
&mut store, | ||
) | ||
.unwrap(); | ||
|
||
println!("Succeeded bisecting!"); | ||
|
||
// notifications ? | ||
|
||
// sleep for a few secs ? | ||
} | ||
} | ||
|
||
/* | ||
* The following is initialization logic that should have a | ||
* function in the lite crate like: | ||
* `subjective_init(height, vals_hash, store, requester) -> Result<(), Error` | ||
* it would fetch the initial header/vals from the requester and populate a | ||
* trusted state and store it in the store ... | ||
* TODO: this should take traits ... | ||
* TODO: better name ? | ||
*/ | ||
fn validate_and_init_subjective_state( | ||
height: Height, | ||
vals_hash: Hash, | ||
store: &mut MemStore, | ||
req: &Requester, | ||
) -> Result<(), Error> { | ||
if store.get(height).is_ok() { | ||
// we already have this ! | ||
return Ok(()); | ||
} | ||
|
||
// check that the val hash matches | ||
let vals = req.validator_set(height)?; | ||
|
||
if vals.hash() != vals_hash { | ||
// TODO | ||
panic!("vals hash dont match") | ||
} | ||
|
||
let signed_header = req.signed_header(SUBJECTIVE_HEIGHT)?; | ||
|
||
// TODO: validate signed_header.commit() with the vals ... | ||
|
||
let next_vals = req.validator_set(height.increment())?; | ||
|
||
// TODO: check next_vals ... | ||
|
||
let trusted_state = &State::new(&signed_header, &next_vals); | ||
|
||
store.add(trusted_state)?; | ||
|
||
Ok(()) | ||
} |
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,11 @@ | ||
use tendermint::lite::TrustThreshold; | ||
|
||
pub struct TrustThresholdOneThird {} | ||
impl TrustThreshold for TrustThresholdOneThird {} | ||
|
||
pub struct TrustThresholdTwoThirds {} | ||
impl TrustThreshold for TrustThresholdTwoThirds { | ||
fn is_enough_power(&self, signed_voting_power: u64, total_voting_power: u64) -> bool { | ||
signed_voting_power * 3 > total_voting_power * 2 | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably write this like:
Alternatively perhaps define a method on
Height
for this? It seems useful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a shortcut to get the integration to work. There ought to be a more explicit way to request the latest height that depending on the implicit menaing of
0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 7477c9d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened an issue for this #118