mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-30 19:23:50 +00:00
Update to Spec v0.10 (#817)
* Start updating types * WIP * Signature hacking * Existing EF tests passing with fake_crypto * Updates * Delete outdated API spec * The refactor continues * It compiles * WIP test fixes * All release tests passing bar genesis state parsing * Update and test YamlConfig * Update to spec v0.10 compatible BLS * Updates to BLS EF tests * Add EF test for AggregateVerify And delete unused hash2curve tests for uncompressed points * Update EF tests to v0.10.1 * Use optional block root correctly in block proc * Use genesis fork in deposit domain. All tests pass * Cargo fmt * Fast aggregate verify test * Update REST API docs * Cargo fmt * Fix unused import * Bump spec tags to v0.10.1 * Add `seconds_per_eth1_block` to chainspec * Update to timestamp based eth1 voting scheme * Return None from `get_votes_to_consider` if block cache is empty * Handle overflows in `is_candidate_block` * Revert to failing tests * Fix eth1 data sets test * Choose default vote according to spec * Fix collect_valid_votes tests * Fix `get_votes_to_consider` to choose all eligible blocks * Uncomment winning_vote tests * Add comments; remove unused code * Reduce seconds_per_eth1_block for simulation * Addressed review comments * Add test for default vote case * Fix logs * Remove unused functions * Meter default eth1 votes * Fix comments * Address review comments; remove unused dependency * Disable/delete two outdated tests * Bump eth1 default vote warn to error * Delete outdated eth1 test Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>
This commit is contained in:
@@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
milagro_bls = { git = "https://github.com/sigp/milagro_bls", tag = "v0.11.2" }
|
||||
milagro_bls = { git = "https://github.com/sigp/milagro_bls", branch = "eth2.0-v0.10" }
|
||||
eth2_hashing = "0.1.0"
|
||||
hex = "0.3"
|
||||
rand = "0.7.2"
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
use super::PublicKey;
|
||||
use super::{PublicKey, BLS_PUBLIC_KEY_BYTE_SIZE};
|
||||
use milagro_bls::{AggregatePublicKey as RawAggregatePublicKey, G1Point};
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use serde_hex::{encode as hex_encode, PrefixedHexVisitor};
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
|
||||
/// A BLS aggregate public key.
|
||||
///
|
||||
@@ -13,6 +17,16 @@ impl AggregatePublicKey {
|
||||
AggregatePublicKey(RawAggregatePublicKey::new())
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let pubkey = RawAggregatePublicKey::from_bytes(&bytes).map_err(|_| {
|
||||
DecodeError::BytesInvalid(
|
||||
format!("Invalid AggregatePublicKey bytes: {:?}", bytes).to_string(),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(AggregatePublicKey(pubkey))
|
||||
}
|
||||
|
||||
pub fn add_without_affine(&mut self, public_key: &PublicKey) {
|
||||
self.0.point.add(&public_key.as_raw().point)
|
||||
}
|
||||
@@ -34,6 +48,11 @@ impl AggregatePublicKey {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Returns the underlying point as compressed bytes.
|
||||
pub fn as_bytes(&self) -> Vec<u8> {
|
||||
self.as_raw().as_bytes()
|
||||
}
|
||||
|
||||
pub fn into_raw(self) -> RawAggregatePublicKey {
|
||||
self.0
|
||||
}
|
||||
@@ -41,6 +60,37 @@ impl AggregatePublicKey {
|
||||
/// Return a hex string representation of this key's bytes.
|
||||
#[cfg(test)]
|
||||
pub fn as_hex_string(&self) -> String {
|
||||
serde_hex::encode(self.as_raw().as_bytes())
|
||||
serde_hex::encode(self.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl_ssz!(
|
||||
AggregatePublicKey,
|
||||
BLS_PUBLIC_KEY_BYTE_SIZE,
|
||||
"AggregatePublicKey"
|
||||
);
|
||||
impl_tree_hash!(AggregatePublicKey, BLS_PUBLIC_KEY_BYTE_SIZE);
|
||||
|
||||
impl Serialize for AggregatePublicKey {
|
||||
/// Serde serialization is compliant the Ethereum YAML test format.
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&hex_encode(self.as_bytes()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for AggregatePublicKey {
|
||||
/// Serde serialization is compliant the Ethereum YAML test format.
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
|
||||
let agg_sig = AggregatePublicKey::from_ssz_bytes(&bytes)
|
||||
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
|
||||
|
||||
Ok(agg_sig)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,17 +47,12 @@ impl AggregateSignature {
|
||||
///
|
||||
/// Only returns `true` if the set of keys in the `AggregatePublicKey` match the set of keys
|
||||
/// that signed the `AggregateSignature`.
|
||||
pub fn verify(
|
||||
&self,
|
||||
msg: &[u8],
|
||||
domain: u64,
|
||||
aggregate_public_key: &AggregatePublicKey,
|
||||
) -> bool {
|
||||
pub fn verify(&self, msg: &[u8], aggregate_public_key: &AggregatePublicKey) -> bool {
|
||||
if self.is_empty {
|
||||
return false;
|
||||
}
|
||||
self.aggregate_signature
|
||||
.verify(msg, domain, aggregate_public_key.as_raw())
|
||||
.verify(msg, aggregate_public_key.as_raw())
|
||||
}
|
||||
|
||||
/// Verify this AggregateSignature against multiple AggregatePublickeys with multiple Messages.
|
||||
@@ -67,7 +62,6 @@ impl AggregateSignature {
|
||||
pub fn verify_multiple(
|
||||
&self,
|
||||
messages: &[&[u8]],
|
||||
domain: u64,
|
||||
aggregate_public_keys: &[&AggregatePublicKey],
|
||||
) -> bool {
|
||||
if self.is_empty {
|
||||
@@ -83,7 +77,7 @@ impl AggregateSignature {
|
||||
}
|
||||
|
||||
self.aggregate_signature
|
||||
.verify_multiple(&msgs, domain, &aggregate_public_keys[..])
|
||||
.verify_multiple(&msgs, &aggregate_public_keys[..])
|
||||
}
|
||||
|
||||
/// Return AggregateSignature as bytes
|
||||
@@ -193,7 +187,7 @@ mod tests {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let mut original = AggregateSignature::new();
|
||||
original.add(&Signature::new(&[42, 42], 0, &keypair.sk));
|
||||
original.add(&Signature::new(&[42, 42], &keypair.sk));
|
||||
|
||||
let bytes = original.as_ssz_bytes();
|
||||
let decoded = AggregateSignature::from_ssz_bytes(&bytes).unwrap();
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
use super::{PublicKey, BLS_PUBLIC_KEY_BYTE_SIZE};
|
||||
use hex::encode as hex_encode;
|
||||
use milagro_bls::G1Point;
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use serde_hex::PrefixedHexVisitor;
|
||||
use ssz::{ssz_encode, Decode, DecodeError, Encode};
|
||||
|
||||
/// A BLS aggregate public key.
|
||||
///
|
||||
@@ -17,6 +22,20 @@ impl FakeAggregatePublicKey {
|
||||
Self::zero()
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
if bytes.len() != BLS_PUBLIC_KEY_BYTE_SIZE {
|
||||
Err(DecodeError::InvalidByteLength {
|
||||
len: bytes.len(),
|
||||
expected: BLS_PUBLIC_KEY_BYTE_SIZE,
|
||||
})
|
||||
} else {
|
||||
Ok(Self {
|
||||
bytes: bytes.to_vec(),
|
||||
point: G1Point::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_without_affine(&mut self, _public_key: &PublicKey) {
|
||||
// No nothing.
|
||||
}
|
||||
@@ -53,3 +72,32 @@ impl FakeAggregatePublicKey {
|
||||
self.bytes.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl_ssz!(
|
||||
FakeAggregatePublicKey,
|
||||
BLS_PUBLIC_KEY_BYTE_SIZE,
|
||||
"FakeAggregatePublicKey"
|
||||
);
|
||||
|
||||
impl_tree_hash!(FakeAggregatePublicKey, BLS_PUBLIC_KEY_BYTE_SIZE);
|
||||
|
||||
impl Serialize for FakeAggregatePublicKey {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&hex_encode(ssz_encode(self)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for FakeAggregatePublicKey {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
|
||||
let pubkey = <_>::from_ssz_bytes(&bytes[..])
|
||||
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
|
||||
Ok(pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +48,7 @@ impl FakeAggregateSignature {
|
||||
}
|
||||
|
||||
/// _Always_ returns `true`.
|
||||
pub fn verify(
|
||||
&self,
|
||||
_msg: &[u8],
|
||||
_domain: u64,
|
||||
_aggregate_public_key: &FakeAggregatePublicKey,
|
||||
) -> bool {
|
||||
pub fn verify(&self, _msg: &[u8], _aggregate_public_key: &FakeAggregatePublicKey) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
@@ -61,7 +56,6 @@ impl FakeAggregateSignature {
|
||||
pub fn verify_multiple(
|
||||
&self,
|
||||
_messages: &[&[u8]],
|
||||
_domain: u64,
|
||||
_aggregate_public_keys: &[&FakeAggregatePublicKey],
|
||||
) -> bool {
|
||||
true
|
||||
@@ -127,7 +121,7 @@ mod tests {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let mut original = FakeAggregateSignature::new();
|
||||
original.add(&Signature::new(&[42, 42], 0, &keypair.sk));
|
||||
original.add(&Signature::new(&[42, 42], &keypair.sk));
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let decoded = FakeAggregateSignature::from_ssz_bytes(&bytes).unwrap();
|
||||
|
||||
@@ -20,7 +20,7 @@ pub struct FakeSignature {
|
||||
|
||||
impl FakeSignature {
|
||||
/// Creates a new all-zero's signature
|
||||
pub fn new(_msg: &[u8], _domain: u64, _sk: &SecretKey) -> Self {
|
||||
pub fn new(_msg: &[u8], _sk: &SecretKey) -> Self {
|
||||
FakeSignature::zero()
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl FakeSignature {
|
||||
}
|
||||
|
||||
/// _Always_ returns `true`.
|
||||
pub fn verify(&self, _msg: &[u8], _domain: u64, _pk: &PublicKey) -> bool {
|
||||
pub fn verify(&self, _msg: &[u8], _pk: &PublicKey) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ mod tests {
|
||||
pub fn test_ssz_round_trip() {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let original = FakeSignature::new(&[42, 42], 0, &keypair.sk);
|
||||
let original = FakeSignature::new(&[42, 42], &keypair.sk);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let decoded = FakeSignature::from_ssz_bytes(&bytes).unwrap();
|
||||
|
||||
@@ -75,7 +75,6 @@ pub fn bls_verify_aggregate(
|
||||
pubkey: &AggregatePublicKey,
|
||||
message: &[u8],
|
||||
signature: &AggregateSignature,
|
||||
domain: u64,
|
||||
) -> bool {
|
||||
signature.verify(message, domain, pubkey)
|
||||
signature.verify(message, pubkey)
|
||||
}
|
||||
|
||||
@@ -17,39 +17,19 @@ pub struct Signature {
|
||||
|
||||
impl Signature {
|
||||
/// Instantiate a new Signature from a message and a SecretKey.
|
||||
pub fn new(msg: &[u8], domain: u64, sk: &SecretKey) -> Self {
|
||||
pub fn new(msg: &[u8], sk: &SecretKey) -> Self {
|
||||
Signature {
|
||||
signature: RawSignature::new(msg, domain, sk.as_raw()),
|
||||
is_empty: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantiate a new Signature from a message and a SecretKey, where the message has already
|
||||
/// been hashed.
|
||||
pub fn new_hashed(x_real_hashed: &[u8], x_imaginary_hashed: &[u8], sk: &SecretKey) -> Self {
|
||||
Signature {
|
||||
signature: RawSignature::new_hashed(x_real_hashed, x_imaginary_hashed, sk.as_raw()),
|
||||
signature: RawSignature::new(msg, sk.as_raw()),
|
||||
is_empty: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify the Signature against a PublicKey.
|
||||
pub fn verify(&self, msg: &[u8], domain: u64, pk: &PublicKey) -> bool {
|
||||
pub fn verify(&self, msg: &[u8], pk: &PublicKey) -> bool {
|
||||
if self.is_empty {
|
||||
return false;
|
||||
}
|
||||
self.signature.verify(msg, domain, pk.as_raw())
|
||||
}
|
||||
|
||||
/// Verify the Signature against a PublicKey, where the message has already been hashed.
|
||||
pub fn verify_hashed(
|
||||
&self,
|
||||
x_real_hashed: &[u8],
|
||||
x_imaginary_hashed: &[u8],
|
||||
pk: &PublicKey,
|
||||
) -> bool {
|
||||
self.signature
|
||||
.verify_hashed(x_real_hashed, x_imaginary_hashed, pk.as_raw())
|
||||
self.signature.verify(msg, pk.as_raw())
|
||||
}
|
||||
|
||||
/// Returns the underlying signature.
|
||||
@@ -141,7 +121,7 @@ mod tests {
|
||||
pub fn test_ssz_round_trip() {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let original = Signature::new(&[42, 42], 0, &keypair.sk);
|
||||
let original = Signature::new(&[42, 42], &keypair.sk);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let decoded = Signature::from_ssz_bytes(&bytes).unwrap();
|
||||
@@ -153,7 +133,7 @@ mod tests {
|
||||
pub fn test_byte_size() {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let signature = Signature::new(&[42, 42], 0, &keypair.sk);
|
||||
let signature = Signature::new(&[42, 42], &keypair.sk);
|
||||
let bytes = ssz_encode(&signature);
|
||||
assert_eq!(bytes.len(), BLS_SIG_BYTE_SIZE);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_valid_signature() {
|
||||
let keypair = Keypair::random();
|
||||
let original = Signature::new(&[42, 42], 0, &keypair.sk);
|
||||
let original = Signature::new(&[42, 42], &keypair.sk);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let signature_bytes = SignatureBytes::from_bytes(&bytes).unwrap();
|
||||
|
||||
@@ -6,7 +6,6 @@ use std::borrow::Cow;
|
||||
use milagro_bls::AggregateSignature as RawAggregateSignature;
|
||||
|
||||
type Message = Vec<u8>;
|
||||
type Domain = u64;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SignedMessage<'a> {
|
||||
@@ -27,23 +26,16 @@ impl<'a> SignedMessage<'a> {
|
||||
pub struct SignatureSet<'a> {
|
||||
pub signature: &'a G2Point,
|
||||
signed_messages: Vec<SignedMessage<'a>>,
|
||||
domain: Domain,
|
||||
}
|
||||
|
||||
impl<'a> SignatureSet<'a> {
|
||||
pub fn single<S>(
|
||||
signature: &'a S,
|
||||
signing_key: Cow<'a, G1Point>,
|
||||
message: Message,
|
||||
domain: Domain,
|
||||
) -> Self
|
||||
pub fn single<S>(signature: &'a S, signing_key: Cow<'a, G1Point>, message: Message) -> Self
|
||||
where
|
||||
S: G2Ref,
|
||||
{
|
||||
Self {
|
||||
signature: signature.g2_ref(),
|
||||
signed_messages: vec![SignedMessage::new(vec![signing_key], message)],
|
||||
domain,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +45,6 @@ impl<'a> SignatureSet<'a> {
|
||||
message_0_signing_keys: Vec<Cow<'a, G1Point>>,
|
||||
message_1: Message,
|
||||
message_1_signing_keys: Vec<Cow<'a, G1Point>>,
|
||||
domain: Domain,
|
||||
) -> Self
|
||||
where
|
||||
T: G1Ref + Clone,
|
||||
@@ -65,18 +56,16 @@ impl<'a> SignatureSet<'a> {
|
||||
SignedMessage::new(message_0_signing_keys, message_0),
|
||||
SignedMessage::new(message_1_signing_keys, message_1),
|
||||
],
|
||||
domain,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new<S>(signature: &'a S, signed_messages: Vec<SignedMessage<'a>>, domain: Domain) -> Self
|
||||
pub fn new<S>(signature: &'a S, signed_messages: Vec<SignedMessage<'a>>) -> Self
|
||||
where
|
||||
S: G2Ref,
|
||||
{
|
||||
Self {
|
||||
signature: signature.g2_ref(),
|
||||
signed_messages,
|
||||
domain,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +92,7 @@ impl<'a> SignatureSet<'a> {
|
||||
let pubkey_refs: Vec<&milagro_bls::AggregatePublicKey> =
|
||||
pubkeys.iter().map(std::borrow::Borrow::borrow).collect();
|
||||
|
||||
sig.verify_multiple(&messages, self.domain, &pubkey_refs)
|
||||
sig.verify_multiple(&messages, &pubkey_refs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +107,7 @@ pub fn verify_signature_sets<'a>(_iter: impl Iterator<Item = SignatureSet<'a>>)
|
||||
true
|
||||
}
|
||||
|
||||
type VerifySet<'a> = (G2Point, Vec<G1Point>, Vec<Vec<u8>>, u64);
|
||||
type VerifySet<'a> = (G2Point, Vec<G1Point>, Vec<Vec<u8>>);
|
||||
|
||||
impl<'a> Into<VerifySet<'a>> for SignatureSet<'a> {
|
||||
fn into(self) -> VerifySet<'a> {
|
||||
@@ -138,7 +127,7 @@ impl<'a> Into<VerifySet<'a>> for SignatureSet<'a> {
|
||||
})
|
||||
.unzip();
|
||||
|
||||
(signature, pubkeys, messages, self.domain)
|
||||
(signature, pubkeys, messages)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
const TAG: &str = "v0.9.2";
|
||||
const TAG: &str = "v0.10.1";
|
||||
// NOTE: the version of the unsafe contract lags the main tag, but the v0.9.2.1 code is compatible
|
||||
// with the unmodified v0.10.1 contract
|
||||
const UNSAFE_TAG: &str = "v0.9.2.1";
|
||||
|
||||
fn spec_url() -> String {
|
||||
|
||||
@@ -7,14 +7,14 @@ pub use ethabi::Error;
|
||||
|
||||
pub const CONTRACT_DEPLOY_GAS: usize = 4_000_000;
|
||||
pub const DEPOSIT_GAS: usize = 4_000_000;
|
||||
pub const ABI: &[u8] = include_bytes!("../contracts/v0.9.2_validator_registration.json");
|
||||
pub const BYTECODE: &[u8] = include_bytes!("../contracts/v0.9.2_validator_registration.bytecode");
|
||||
pub const ABI: &[u8] = include_bytes!("../contracts/v0.10.1_validator_registration.json");
|
||||
pub const BYTECODE: &[u8] = include_bytes!("../contracts/v0.10.1_validator_registration.bytecode");
|
||||
|
||||
pub mod testnet {
|
||||
pub const ABI: &[u8] =
|
||||
include_bytes!("../contracts/v0.9.2_testnet_validator_registration.json");
|
||||
include_bytes!("../contracts/v0.10.1_testnet_validator_registration.json");
|
||||
pub const BYTECODE: &[u8] =
|
||||
include_bytes!("../contracts/v0.9.2_testnet_validator_registration.bytecode");
|
||||
include_bytes!("../contracts/v0.10.1_testnet_validator_registration.bytecode");
|
||||
}
|
||||
|
||||
pub fn eth1_tx_data(deposit_data: &DepositData) -> Result<Vec<u8>, Error> {
|
||||
|
||||
@@ -11,7 +11,7 @@ lazy_static = "1.4.0"
|
||||
num-bigint = "0.2.3"
|
||||
eth2_hashing = "0.1.0"
|
||||
hex = "0.3"
|
||||
milagro_bls = { git = "https://github.com/sigp/milagro_bls", tag = "v0.11.2" }
|
||||
milagro_bls = { git = "https://github.com/sigp/milagro_bls", branch = "eth2.0-v0.10" }
|
||||
serde_yaml = "0.8.11"
|
||||
serde = "1.0.102"
|
||||
serde_derive = "1.0.102"
|
||||
|
||||
@@ -202,6 +202,7 @@ mod tests {
|
||||
|
||||
type E = MainnetEthSpec;
|
||||
|
||||
/* FIXME: add new testnet config and re-enable this test
|
||||
#[test]
|
||||
fn hard_coded_works() {
|
||||
let dir: Eth2TestnetConfig<E> =
|
||||
@@ -211,6 +212,7 @@ mod tests {
|
||||
assert!(dir.genesis_state.is_some());
|
||||
assert!(dir.yaml_config.is_some());
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn round_trip() {
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
use types::{
|
||||
Attestation, BeaconBlock, BeaconState, CommitteeIndex, Epoch, EthSpec, Fork, Hash256,
|
||||
PublicKey, Signature, Slot,
|
||||
PublicKey, Signature, SignedBeaconBlock, Slot,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
@@ -258,7 +258,7 @@ impl<E: EthSpec> Validator<E> {
|
||||
/// Posts a block to the beacon node, expecting it to verify it and publish it to the network.
|
||||
pub fn publish_block(
|
||||
&self,
|
||||
block: BeaconBlock<E>,
|
||||
block: SignedBeaconBlock<E>,
|
||||
) -> impl Future<Item = PublishStatus, Error = Error> {
|
||||
let client = self.0.clone();
|
||||
self.url("block")
|
||||
@@ -347,7 +347,7 @@ impl<E: EthSpec> Beacon<E> {
|
||||
pub fn get_block_by_slot(
|
||||
&self,
|
||||
slot: Slot,
|
||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||
) -> impl Future<Item = (SignedBeaconBlock<E>, Hash256), Error = Error> {
|
||||
self.get_block("slot".to_string(), format!("{}", slot.as_u64()))
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ impl<E: EthSpec> Beacon<E> {
|
||||
pub fn get_block_by_root(
|
||||
&self,
|
||||
root: Hash256,
|
||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||
) -> impl Future<Item = (SignedBeaconBlock<E>, Hash256), Error = Error> {
|
||||
self.get_block("root".to_string(), root_as_string(root))
|
||||
}
|
||||
|
||||
@@ -364,7 +364,7 @@ impl<E: EthSpec> Beacon<E> {
|
||||
&self,
|
||||
query_key: String,
|
||||
query_param: String,
|
||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||
) -> impl Future<Item = (SignedBeaconBlock<E>, Hash256), Error = Error> {
|
||||
let client = self.0.clone();
|
||||
self.url("block")
|
||||
.into_future()
|
||||
@@ -576,7 +576,7 @@ impl<E: EthSpec> Advanced<E> {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "T: EthSpec")]
|
||||
pub struct BlockResponse<T: EthSpec> {
|
||||
pub beacon_block: BeaconBlock<T>,
|
||||
pub beacon_block: SignedBeaconBlock<T>,
|
||||
pub root: Hash256,
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
//! format designed for use in Ethereum 2.0.
|
||||
//!
|
||||
//! Adheres to the Ethereum 2.0 [SSZ
|
||||
//! specification](https://github.com/ethereum/eth2.0-specs/blob/v0.8.1/specs/simple-serialize.md)
|
||||
//! at v0.8.1 .
|
||||
//! specification](https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/ssz/simple-serialize.md)
|
||||
//! at v0.10.1.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
//! for padding and verification.
|
||||
//!
|
||||
//! Adheres to the Ethereum 2.0 [SSZ
|
||||
//! specification](https://github.com/ethereum/eth2.0-specs/blob/v0.8.1/specs/simple-serialize.md)
|
||||
//! at v0.8.1 .
|
||||
//! specification](https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/ssz/simple-serialize.md)
|
||||
//! at v0.10.1.
|
||||
//!
|
||||
//! ## Example
|
||||
//! ```
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Provides list-shuffling functions matching the Ethereum 2.0 specification.
|
||||
//!
|
||||
//! See
|
||||
//! [compute_shuffled_index](https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#compute_shuffled_index)
|
||||
//! [compute_shuffled_index](https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/specs/phase0/beacon-chain.md#compute_shuffled_index)
|
||||
//! for specifications.
|
||||
//!
|
||||
//! There are two functions exported by this crate:
|
||||
|
||||
@@ -45,10 +45,6 @@ pub trait TreeHash {
|
||||
fn tree_hash_root(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
pub trait SignedRoot: TreeHash {
|
||||
fn signed_root(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tree_hash_ssz_encoding_as_vector {
|
||||
($type: ident) => {
|
||||
|
||||
@@ -121,58 +121,3 @@ pub fn tree_hash_derive(input: TokenStream) -> TokenStream {
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(SignedRoot, attributes(signed_root))]
|
||||
pub fn tree_hash_signed_root_derive(input: TokenStream) -> TokenStream {
|
||||
let item = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
let name = &item.ident;
|
||||
let (impl_generics, ty_generics, where_clause) = &item.generics.split_for_impl();
|
||||
|
||||
let struct_data = match &item.data {
|
||||
syn::Data::Struct(s) => s,
|
||||
_ => panic!("tree_hash_derive only supports structs."),
|
||||
};
|
||||
|
||||
let idents = get_signed_root_named_field_idents(&struct_data);
|
||||
let num_elems = idents.len();
|
||||
|
||||
let output = quote! {
|
||||
impl #impl_generics tree_hash::SignedRoot for #name #ty_generics #where_clause {
|
||||
fn signed_root(&self) -> Vec<u8> {
|
||||
let mut leaves = Vec::with_capacity(#num_elems * tree_hash::HASHSIZE);
|
||||
|
||||
#(
|
||||
leaves.append(&mut self.#idents.tree_hash_root());
|
||||
)*
|
||||
|
||||
tree_hash::merkle_root(&leaves, 0)
|
||||
}
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
|
||||
fn get_signed_root_named_field_idents(struct_data: &syn::DataStruct) -> Vec<&syn::Ident> {
|
||||
struct_data
|
||||
.fields
|
||||
.iter()
|
||||
.filter_map(|f| {
|
||||
if should_skip_signed_root(&f) {
|
||||
None
|
||||
} else {
|
||||
Some(match &f.ident {
|
||||
Some(ref ident) => ident,
|
||||
_ => panic!("tree_hash_derive only supports named struct fields"),
|
||||
})
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn should_skip_signed_root(field: &syn::Field) -> bool {
|
||||
field.attrs.iter().any(|attr| {
|
||||
attr.path.is_ident("signed_root")
|
||||
&& attr.tts.to_string().replace(" ", "") == "(skip_hashing)"
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user