Skip to content

Commit

Permalink
Merge branch 'develop' into checkpoint-index-progression
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum authored Feb 12, 2024
2 parents a64eddb + 483fd66 commit 9e68002
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 82 deletions.
122 changes: 49 additions & 73 deletions rest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,12 +963,7 @@ async fn slashing_params() -> Value {
})
}

#[get("/cosmos/base/tendermint/v1beta1/blocks/latest")]
async fn latest_block() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let res = client.latest_block().await.unwrap();

fn parse_block(res: tendermint_rpc::endpoint::block::Response) -> Value {
let last_commit = res.block.last_commit.unwrap();
let signatures: Vec<_> = last_commit
.signatures
Expand All @@ -988,7 +983,25 @@ async fn latest_block() -> Value {
json!({
"block_id": res.block_id,
"block": {
"header": res.block.header,
"header": {
"version": {
"block": res.block.header.version.block,
"app": res.block.header.version.block,
},
"chain_id": res.block.header.chain_id,
"height": res.block.header.height,
"time": res.block.header.time,
"last_block_id": res.block.header.last_block_id,
"last_commit_hash": res.block.header.last_commit_hash.map(|hash| base64::encode(hash.as_bytes())),
"data_hash": res.block.header.data_hash.map(|hash| base64::encode(hash.as_bytes())),
"validators_hash": base64::encode(res.block.header.validators_hash.as_bytes()),
"next_validators_hash": base64::encode(res.block.header.next_validators_hash.as_bytes()),
"consensus_hash": base64::encode(res.block.header.consensus_hash.as_bytes()),
"app_hash": base64::encode(res.block.header.app_hash.value()),
"last_results_hash": res.block.header.last_results_hash.map(|hash| base64::encode(hash.as_bytes())),
"evidence_hash": res.block.header.evidence_hash.map(|hash| base64::encode(hash.as_bytes())),
"proposer_address": base64::encode(res.block.header.proposer_address),
},
"data": res.block.data,
"evidence": res.block.evidence,
"last_commit": {
Expand All @@ -999,6 +1012,14 @@ async fn latest_block() -> Value {
})
}

#[get("/cosmos/base/tendermint/v1beta1/blocks/latest")]
async fn latest_block() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let res = client.latest_block().await.unwrap();
parse_block(res)
}

#[get("/cosmos/base/tendermint/v1beta1/blocks/<height>")]
async fn block(height: u32) -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();
Expand All @@ -1008,50 +1029,11 @@ async fn block(height: u32) -> Value {
.await
.unwrap();

let last_commit = res.block.last_commit.unwrap();
let signatures: Vec<_> = last_commit
.signatures
.iter()
.map(|signature| -> Value {
let signature_raw = RawCommitSig::from(signature.clone());

json!({
"validator_address": base64::encode(signature_raw.validator_address),
"block_id_flag": signature_raw.block_id_flag,
"timestamp": signature_raw.timestamp,
"signature": base64::encode(signature_raw.signature),
})
})
.collect();

json!({
"block_id": res.block_id,
"block": {
"header": res.block.header,
"data": res.block.data,
"evidence": res.block.evidence,
"last_commit": {
"block_id": last_commit.block_id,
"signatures": signatures
}
}
})
parse_block(res)
}

#[get("/cosmos/base/tendermint/v1beta1/validatorsets/latest")]
async fn latest_validator_set() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let block = client.latest_block().await.unwrap();

let res = client
.validators(block.block.header.height, tendermint_rpc::Paging::All)
.await
.unwrap();

let validators: Vec<_> = res
.validators
.iter()
fn parse_validator_set(res: tendermint_rpc::endpoint::validators::Response) -> Value {
let validators: Vec<_> = res.validators.iter()
.map(|validator| -> Value {
json!({
"address": validator.address,
Expand All @@ -1075,6 +1057,23 @@ async fn latest_validator_set() -> Value {
})
}

#[get("/cosmos/base/tendermint/v1beta1/validatorsets/latest")]
async fn latest_validator_set() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let block = client
.latest_block()
.await
.unwrap();

let res = client
.validators(block.block.header.height, tendermint_rpc::Paging::All)
.await
.unwrap();

parse_validator_set(res)
}

#[get("/cosmos/base/tendermint/v1beta1/validatorsets/<height>")]
async fn validator_set(height: u32) -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();
Expand All @@ -1084,30 +1083,7 @@ async fn validator_set(height: u32) -> Value {
.await
.unwrap();

let validators: Vec<_> = res
.validators
.iter()
.map(|validator| -> Value {
json!({
"address": validator.address,
"voting_power": i64::from(validator.power).to_string(),
"proposer_priority": i64::from(validator.proposer_priority).to_string(),
"pub_key": {
"@type": "/cosmos.crypto.ed25519.PubKey",
"key": base64::encode(validator.pub_key.ed25519().unwrap().to_bytes()),
}
})
})
.collect();

json!({
"block_height": res.block_height,
"validators": validators,
"pagination": {
"next_key": null,
"total": res.validators.len(),
}
})
parse_validator_set(res)
}

#[get("/cosmos/distribution/v1beta1/community_pool")]
Expand Down
11 changes: 8 additions & 3 deletions src/bitcoin/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,7 @@ impl CheckpointQueue {
&mut self,
first_index: u32,
redeem_scripts: impl Iterator<Item = Script>,
threshold_ratio: (u64, u64),
) -> Result<()> {
let mut index = first_index + 1;

Expand All @@ -2384,7 +2385,7 @@ impl CheckpointQueue {
continue;
}

let (mut sigset, _) = SignatorySet::from_script(&script)?;
let (mut sigset, _) = SignatorySet::from_script(&script, threshold_ratio)?;
sigset.index = index;
sigset.create_time = create_time;
let mut cp = Checkpoint::new(sigset)?;
Expand Down Expand Up @@ -3035,7 +3036,9 @@ mod test {
sigset(4).redeem_script(&[0], (2, 3)).unwrap(),
sigset(3).redeem_script(&[0], (2, 3)).unwrap(),
];
queue.backfill(8, backfill_data.into_iter()).unwrap();
queue
.backfill(8, backfill_data.into_iter(), (2, 3))
.unwrap();

assert_eq!(queue.len().unwrap(), 8);
assert_eq!(queue.index, 10);
Expand Down Expand Up @@ -3069,7 +3072,9 @@ mod test {
.unwrap();

let backfill_data = vec![sigset(0).redeem_script(&[0], (2, 3)).unwrap()];
queue.backfill(0, backfill_data.into_iter()).unwrap();
queue
.backfill(0, backfill_data.into_iter(), (2, 3))
.unwrap();

assert_eq!(queue.len().unwrap(), 2);
assert_eq!(queue.index, 1);
Expand Down
38 changes: 32 additions & 6 deletions src/bitcoin/signatory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::redundant_closure_call)] // TODO: fix bitcoin-script then remove this
#![allow(unused_imports)] // TODO

use std::cmp::Ordering;

use crate::bitcoin::threshold_sig::Pubkey;
use crate::error::Error;
use crate::error::Result;
Expand Down Expand Up @@ -158,7 +160,10 @@ impl SignatorySet {
Ok(sigset)
}

pub fn from_script(script: &bitcoin::Script) -> Result<(Self, Vec<u8>)> {
pub fn from_script(
script: &bitcoin::Script,
threshold_ratio: (u64, u64),
) -> Result<(Self, Vec<u8>)> {
trait Iter<'a> = Iterator<
Item = std::result::Result<Instruction<'a>, bitcoin::blockdata::script::Error>,
>;
Expand Down Expand Up @@ -281,20 +286,41 @@ impl SignatorySet {
}
}

take_threshold(&mut ins)?;
let expected_threshold = take_threshold(&mut ins)?;
let commitment = take_commitment(&mut ins)?;

assert!(ins.next().is_none());

let total_vp: u64 = sigs.iter().map(|s| s.voting_power).sum();
let sigset = Self {
let mut sigset = Self {
signatories: sigs,
present_vp: total_vp,
possible_vp: total_vp,
create_time: 0,
index: 0,
};

for _ in 0..100 {
let actual_threshold = sigset.signature_threshold(threshold_ratio);
match actual_threshold.cmp(&expected_threshold) {
Ordering::Equal => break,
Ordering::Less => {
sigset.present_vp += 1;
sigset.possible_vp += 1;
}
Ordering::Greater => {
sigset.present_vp -= 1;
sigset.possible_vp -= 1;
}
}
}

assert_eq!(
sigset.signature_threshold(threshold_ratio),
expected_threshold,
);
assert_eq!(&sigset.redeem_script(commitment, threshold_ratio)?, script);

Ok((sigset, commitment.to_vec()))
}

Expand Down Expand Up @@ -595,15 +621,15 @@ mod tests {
fn from_script() {
let script = bitcoin::Script::from_hex("21028891f36b691a40036f2b3ecb17c13780a932503ef2c39f3faed9b95bf71ea27fac630339e0116700687c2102f6fee7ad7dc87d0a636ae1584273c849bf540f4c1780434a0430888b0c5b151cac63033c910e93687c2102d207371a1e9a588e447d91dc12a8f3479f1f9ff8da748aae04bb5d07f0737790ac630371730893687c2103713e9bb6025fa9dc3c26507762cffd2a9524ff48f1d84c6753caa581347e5e10ac63031def0793687c2103d8fc0412a866bfb14d3fbc9e1b714ca31141d0f7e211d0fa634d53dda9789ecaac6303d1f00693687c2102c7961e04206af92f4b4cf3f19b43722f301e4915a49f5ca2908d9af5ce343830ac6303496f0693687c2103205472bb87799cb9140b5d471cc045b65821a4e75591026a8411ee3ac3e27027ac6303fe500693687c2102c923df10e8141072504b1f9513ee6796dc4d748d774ce9396942b63d42d3d575ac6303ed1f0593687c21031e8124547a5f28e04652d61fab1053ba8af41b682ccecdf5fa58595add7c7d9eac6303d4a00493687c21038060738940b9b3513851aa45df9f8b9d8e3304ef5abc5f8c1928bf4f1c8601adac630347210493687c21022e1efe78c688bceb7a36bf8af0e905da65e1942b84afe31716a356a91c0d9c05ac6303c5620393687c21020598956ed409e190b763bed8ed1ec3a18138c582c761eb8a4cf60861bfb44f13ac6303b3550393687c2102c8b2e54cafced96b1438e9ee6ebddc27c4aca68f14b2199eb8b8da111b584c2cac63036c330393687c2102d8a4c0accefa93b6a8d390a81dbffa4d05cd0a844371b2bed0ba1b1b65e14300ac6303521d0393687c2102460ccc0db97b1027e4fe2ab178f015a786b6b8f016b580f495dde3230f34984cac630304060393687c2102def64dfc155e17988ea6dee5a5659e2ec0a19fce54af90ca84dcd4df53b1a222ac630341d20293687c21030c9057c92c19f749c891037379766c0642d03bd1c50e3b262fc7d954c232f4d8ac630356c30293687c21027e1ebe3dd4fbbf250a8161a8a7af19815d5c07363e220f28f81c535c3950c7cbac6303d3ab0293687c210235e1d72961cb475971e2bc437ac21f9be13c83f1aa039e64f406aae87e2b4816ac6303bdaa0293687c210295d565c8ae94d46d439b4591dcd146742f918893292c23c49d000c4023bad4ffac630308aa029368030fb34aa0010075").unwrap();

let (sigset, commitment) = SignatorySet::from_script(&script).unwrap();
let (sigset, commitment) = SignatorySet::from_script(&script, (2, 3)).unwrap();

let pk = |bytes| Pubkey::new(bytes).unwrap().into();
assert_eq!(
sigset,
SignatorySet {
create_time: 0,
present_vp: 7343244,
possible_vp: 7343244,
present_vp: 7343255,
possible_vp: 7343255,
index: 0,
signatories: vec![
Signatory {
Expand Down

0 comments on commit 9e68002

Please sign in to comment.