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

Replace amino with protobuf types #527

Merged
merged 11 commits into from
Sep 10, 2020
Prev Previous commit
Next Next commit
As close as test_validator_set gets without issue #506
  • Loading branch information
greg-szabo committed Sep 3, 2020
commit ffd3ed8db577b91c24195b8e8ac5948bed1b8580
62 changes: 50 additions & 12 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,43 +242,81 @@ where

#[cfg(test)]
mod tests {
use subtle_encoding::hex;

use super::*;

// make a validator from a hex ed25519 pubkey and a voting power
fn make_validator(pk_string: &str, vp: u64) -> Info {
let bytes = hex::decode_upper(pk_string).unwrap();
let pk = PublicKey::from_raw_ed25519(&bytes).unwrap();
// make a validator
fn make_validator(pk: Vec<u8>, vp: u64) -> Info {
let pk = PublicKey::from_raw_ed25519(&pk).unwrap();
Info::new(pk, vote::Power::new(vp))
}

#[test]
fn test_validator_set() {
// test vector generated by Go code
/*
import (
"fmt"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/types"
"strings"
)
func testValSet() {
pk1 := ed25519.GenPrivKeyFromSecret([]byte{4, 211, 14, 157, 10, 0, 205, 9, 10, 116, 207, 161, 4, 211, 190, 37, 108, 88, 202, 168, 63, 135, 0, 141, 53, 55, 254, 57, 40, 184, 20, 242})
pk2 := ed25519.GenPrivKeyFromSecret([]byte{99, 231, 126, 151, 159, 236, 2, 229, 33, 44, 200, 248, 147, 176, 13, 127, 105, 76, 49, 83, 25, 101, 44, 57, 20, 215, 166, 188, 134, 94, 56, 165})
pk3 := ed25519.GenPrivKeyFromSecret([]byte{54, 253, 151, 16, 182, 114, 125, 12, 74, 101, 54, 253, 174, 153, 121, 74, 145, 180, 111, 16, 214, 48, 193, 109, 104, 134, 55, 162, 151, 16, 182, 114})
not_in_set := ed25519.GenPrivKeyFromSecret([]byte{121, 74, 145, 180, 111, 16, 214, 48, 193, 109, 35, 68, 19, 27, 173, 69, 92, 204, 127, 218, 234, 81, 232, 75, 204, 199, 48, 163, 55, 132, 231, 147})
fmt.Println("pk1: ", strings.Join(strings.Split(fmt.Sprintf("%v", pk1.PubKey().Bytes()), " "), ", "))
fmt.Println("pk2:", strings.Join(strings.Split(fmt.Sprintf("%v", pk2.PubKey().Bytes()), " "), ", "))
fmt.Println("pk3: ", strings.Join(strings.Split(fmt.Sprintf("%v", pk3.PubKey().Bytes()), " "), ", "))
fmt.Println("not_in_set: ", strings.Join(strings.Split(fmt.Sprintf("%v", not_in_set.PubKey().Bytes()), " "), ", "))
v1 := types.NewValidator(pk1.PubKey(), 148151478422287875)
v2 := types.NewValidator(pk2.PubKey(), 158095448483785107)
v3 := types.NewValidator(pk3.PubKey(), 770561664770006272)
set := types.NewValidatorSet([]*types.Validator{v1, v2, v3})
fmt.Println("Hash:", strings.Join(strings.Split(fmt.Sprintf("%v", set.Hash()), " "), ", "))
}
*/
let v1 = make_validator(
"F349539C7E5EF7C49549B09C4BFC2335318AB0FE51FBFAA2433B4F13E816F4A7",
vec![
48, 163, 55, 132, 231, 147, 230, 163, 56, 158, 127, 218, 179, 139, 212, 103, 218,
89, 122, 126, 229, 88, 84, 48, 32, 0, 185, 174, 63, 72, 203, 52,
],
148_151_478_422_287_875,
);
let v2 = make_validator(
"5646AA4C706B7AF73768903E77D117487D2584B76D83EB8FF287934EE7758AFC",
vec![
54, 253, 174, 153, 121, 74, 145, 180, 111, 16, 214, 48, 193, 109, 104, 134, 55,
162, 151, 16, 182, 114, 125, 135, 32, 195, 236, 248, 64, 112, 74, 101,
],
158_095_448_483_785_107,
);
let v3 = make_validator(
"76A2B3F5CBB567F0D689D9DF7155FC89A4C878F040D7A5BB85FF68B74D253FC7",
vec![
182, 205, 13, 86, 147, 27, 65, 49, 160, 118, 11, 180, 117, 35, 206, 35, 68, 19, 27,
173, 69, 92, 204, 224, 200, 51, 249, 81, 105, 128, 112, 244,
],
770_561_664_770_006_272,
);
let hash_string = "7B7B00C03EBA5ED17923243D5F7CF974BE2499522EF5B92A3EC60E868A0CCA19";
let hash_expect = hex::decode_upper(hash_string).unwrap();
let hash_expect = vec![
11, 64, 107, 4, 234, 81, 232, 75, 204, 199, 160, 114, 229, 97, 243, 95, 118, 213, 17,
22, 57, 84, 71, 122, 200, 169, 192, 252, 41, 148, 223, 180,
];

let val_set = Set::new(vec![v1, v2, v3]);
let hash = val_set.hash();
// Todo: Issue #506. The expected hash is correct, but Rust doesn't implement yet the correct
// ordering of the validators. Hence this assertion fails.
assert_eq!(hash_expect, hash.as_bytes().to_vec());

let not_in_set = make_validator(
"76A2B3F5CBB567F0D689D9DF7155FC89A4C878F040D7A5BB85FF68B74D253FC9",
1,
vec![
110, 147, 87, 120, 27, 218, 66, 209, 81, 4, 169, 153, 64, 163, 137, 89, 168, 97,
219, 233, 42, 119, 24, 61, 47, 59, 76, 31, 182, 60, 13, 4,
],
10_000_000_000_000_000,
);

assert_eq!(val_set.validator(v1.address).unwrap(), v1);
assert_eq!(val_set.validator(v2.address).unwrap(), v2);
assert_eq!(val_set.validator(v3.address).unwrap(), v3);
Expand Down