Skip to content
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

Add tendermint-light-client-js crate #812

Merged
merged 17 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
command: build-all

light-client-wasm:
build-light-client-wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ jobs:
with:
command: test-all-features
args: -p tendermint-light-client
# From https://rustwasm.github.io/docs/wasm-bindgen/wasm-bindgen-test/continuous-integration.html#github-actions
tendermint-light-client-js:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack test --headless --chrome ./light-client-js/
- run: wasm-pack test --headless --firefox ./light-client-js/
tendermint-light-node:
runs-on: ubuntu-latest
steps:
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
## Unreleased
## Unreleased

### FEATURES

* `[tendermint-abci]` Release minimal framework for building ABCI applications
in Rust ([#794])
* `[tendermint-light-client-js]` First release of the
`tendermint-light-client-js` crate to provide access to Tendermint Light
Client functionality from WASM. This only provides access to the `verify`
method at present, exclusively provides access to block verification. This
does not include network access or the Light Client's bisection algorithm
([#812])

[#794]: https://github.com/informalsystems/tendermint-rs/pull/794
[#812]: https://github.com/informalsystems/tendermint-rs/pull/812

## v0.18.1

Expand Down
4 changes: 2 additions & 2 deletions light-client-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn deserialize_params(
}

/// Errors produced by this crate.
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Error {
/// A serialization/deserialization error occurred.
Expand All @@ -70,7 +70,7 @@ pub enum Error {

// Simplified options supplied from JavaScript.
#[derive(Debug, Serialize, Deserialize)]
struct JsOptions {
pub struct JsOptions {
pub trust_threshold: (u64, u64),
pub trusting_period: u64,
pub clock_drift: u64,
Expand Down
200 changes: 198 additions & 2 deletions light-client-js/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,207 @@
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use tendermint::Time;
use tendermint_light_client::components::verifier::Verdict;
use tendermint_light_client::types::LightBlock;
use tendermint_light_client_js::{verify, Error, JsOptions};
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

const UNTRUSTED_BLOCK: &str = r#"{
"signed_header": {
"header": {
"version": {
"block": "11",
"app": "0"
},
"chain_id": "test-chain",
"height": "4",
"time": "1970-01-01T00:00:04Z",
"last_block_id": null,
"last_commit_hash": null,
"data_hash": null,
"validators_hash": "75E6DD63C2DC2B58FE0ED82792EAB369C4308C7EC16B69446382CC4B41D46068",
"next_validators_hash": "C8CFFADA9808F685C4111693E1ADFDDBBEE9B9493493BEF805419F143C5B0D0A",
"consensus_hash": "75E6DD63C2DC2B58FE0ED82792EAB369C4308C7EC16B69446382CC4B41D46068",
"app_hash": "",
"last_results_hash": null,
"evidence_hash": null,
"proposer_address": "6AE5C701F508EB5B63343858E068C5843F28105F"
},
"commit": {
"height": "4",
"round": 1,
"block_id": {
"hash": "D0E7B0C678E290DA835BB26EE826472D66B6A306801E5FE0803C5320C554610A",
"part_set_header": {
"total": 1,
"hash": "D0E7B0C678E290DA835BB26EE826472D66B6A306801E5FE0803C5320C554610A"
}
},
"signatures": [
{
"block_id_flag": 2,
"validator_address": "6AE5C701F508EB5B63343858E068C5843F28105F",
"timestamp": "1970-01-01T00:00:04Z",
"signature": "lTGBsjVI6YwIRcxQ6Lct4Q+xrtJc9h3648c42uWe4MpSgy4rUI5g71AEpG90Tbn0PRizjKgCPhokPpQoQLiqAg=="
}
]
}
},
"validator_set": {
"total_voting_power": "0",
"validators": [
{
"address": "6AE5C701F508EB5B63343858E068C5843F28105F",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "GQEC/HB4sDBAVhHtUzyv4yct9ZGnudaP209QQBSTfSQ="
},
"voting_power": "50",
"proposer_priority": null
}
]
},
"next_validator_set": {
"total_voting_power": "0",
"validators": [
{
"address": "C479DB6F37AB9757035CFBE10B687E27668EE7DF",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "3wf60CidQcsIO7TksXzEZsJefMUFF73k6nP1YeEo9to="
},
"voting_power": "50",
"proposer_priority": null
}
]
},
"provider": "BADFADAD0BEFEEDC0C0ADEADBEEFC0FFEEFACADE"
}"#;

const TRUSTED_BLOCK: &str = r#"{
"signed_header": {
"header": {
"version": {
"block": "11",
"app": "0"
},
"chain_id": "test-chain",
"height": "3",
"time": "1970-01-01T00:00:03Z",
"last_block_id": null,
"last_commit_hash": null,
"data_hash": null,
"validators_hash": "75E6DD63C2DC2B58FE0ED82792EAB369C4308C7EC16B69446382CC4B41D46068",
"next_validators_hash": "75E6DD63C2DC2B58FE0ED82792EAB369C4308C7EC16B69446382CC4B41D46068",
"consensus_hash": "75E6DD63C2DC2B58FE0ED82792EAB369C4308C7EC16B69446382CC4B41D46068",
"app_hash": "",
"last_results_hash": null,
"evidence_hash": null,
"proposer_address": "6AE5C701F508EB5B63343858E068C5843F28105F"
},
"commit": {
"height": "3",
"round": 1,
"block_id": {
"hash": "AAB1B09D5FADAAE7CDF3451961A63F810DB73BF3214A7B74DBA36C52EDF1A793",
"part_set_header": {
"total": 1,
"hash": "AAB1B09D5FADAAE7CDF3451961A63F810DB73BF3214A7B74DBA36C52EDF1A793"
}
},
"signatures": [
{
"block_id_flag": 2,
"validator_address": "6AE5C701F508EB5B63343858E068C5843F28105F",
"timestamp": "1970-01-01T00:00:03Z",
"signature": "xn0eSsHYIsqUbmfAiJq1R0hqZbfuIjs5Na1c88EC1iPTuQAesKg9I7nXG4pk8d6U5fU4GysNLk5I4f7aoefOBA=="
}
]
}
},
"validator_set": {
"total_voting_power": "0",
"validators": [
{
"address": "6AE5C701F508EB5B63343858E068C5843F28105F",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "GQEC/HB4sDBAVhHtUzyv4yct9ZGnudaP209QQBSTfSQ="
},
"voting_power": "50",
"proposer_priority": null
}
]
},
"next_validator_set": {
"total_voting_power": "0",
"validators": [
{
"address": "6AE5C701F508EB5B63343858E068C5843F28105F",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "GQEC/HB4sDBAVhHtUzyv4yct9ZGnudaP209QQBSTfSQ="
},
"voting_power": "50",
"proposer_priority": null
}
]
},
"provider": "BADFADAD0BEFEEDC0C0ADEADBEEFC0FFEEFACADE"
}"#;

#[wasm_bindgen_test]
fn successful_verification() {
let (untrusted_block, trusted_block) = test_blocks();
let options = test_options();
// Choose a "now" value within the trusting period
let now =
JsValue::from_serde(&Time::parse_from_rfc3339("1970-01-07T00:00:00Z").unwrap()).unwrap();
let js_result = verify(&untrusted_block, &trusted_block, &options, &now);
console_log!("js_result = {:?}", js_result);
let verdict = JsValue::into_serde::<Result<Verdict, Error>>(&js_result)
.unwrap()
.unwrap();
assert_eq!(verdict, Verdict::Success);
}

#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
fn failed_verification_outside_trusting_period() {
let (untrusted_block, trusted_block) = test_blocks();
let options = test_options();
// Choose a "now" value outside the trusting period
let now =
JsValue::from_serde(&Time::parse_from_rfc3339("1970-01-16T00:00:00Z").unwrap()).unwrap();
let js_result = verify(&untrusted_block, &trusted_block, &options, &now);
console_log!("js_result = {:?}", js_result);
// The result is Ok because we successfully obtained a verdict, even if the
// verdict isn't Verdict::Success.
let verdict = JsValue::into_serde::<Result<Verdict, Error>>(&js_result)
.unwrap()
.unwrap();
match verdict {
Verdict::Success | Verdict::NotEnoughTrust(_) => panic!("unexpected verdict"),
_ => {}
}
}

fn test_blocks() -> (JsValue, JsValue) {
let untrusted_block =
JsValue::from_serde(&serde_json::from_str::<LightBlock>(UNTRUSTED_BLOCK).unwrap()).unwrap();
let trusted_block =
JsValue::from_serde(&serde_json::from_str::<LightBlock>(TRUSTED_BLOCK).unwrap()).unwrap();
(untrusted_block, trusted_block)
}

fn test_options() -> JsValue {
JsValue::from_serde(&JsOptions {
trust_threshold: (1, 3),
trusting_period: 1209600, // 2 weeks
clock_drift: 5, // 5 seconds
})
.unwrap()
}
2 changes: 1 addition & 1 deletion light-client/src/components/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};

/// Represents the result of the verification performed by the
/// verifier component.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
thanethomson marked this conversation as resolved.
Show resolved Hide resolved
pub enum Verdict {
/// Verification succeeded, the block is valid.
Success,
Expand Down