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

feat(node): migrate vote tally #1139

Open
wants to merge 24 commits into
base: feat/topdown-enchancement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ libp2p-mplex = { version = "0.41" }
# libp2p-bitswap = "0.25.1"
libp2p-bitswap = { git = "https://github.com/consensus-shipyard/libp2p-bitswap.git", branch = "chore-upgrade-libipld" } # Updated to libipld 0.16
libsecp256k1 = "0.7"

literally = "0.1.3"
log = "0.4"
lru_time_cache = "0.11"
Expand Down Expand Up @@ -174,6 +175,7 @@ tracing-subscriber = { version = "0.3", features = [
tracing-appender = "0.2.3"
url = { version = "2.4.1", features = ["serde"] }
zeroize = "1.6"
digest = "0.10.7"

# Workspace deps
ipc-api = { path = "ipc/api" }
Expand Down
9 changes: 9 additions & 0 deletions fendermint/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ anyhow = { workspace = true }
base64 = { workspace = true }
rand = { workspace = true }
zeroize = { workspace = true }
multihash = { workspace = true, features = ["multihash-impl", "blake2b"] }
serde = { workspace = true, optional = true }

[dev-dependencies]
fvm_ipld_encoding = { workspace = true }

[features]
default = ["with_serde"]
with_serde = ["serde"]
2 changes: 2 additions & 0 deletions fendermint/crypto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

pub mod secp;

use base64::engine::GeneralPurpose;
use base64::engine::{DecodePaddingMode, GeneralPurposeConfig};
use base64::{alphabet, Engine};
Expand Down
100 changes: 100 additions & 0 deletions fendermint/crypto/src/secp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::SecretKey;
use anyhow::Context;
use libsecp256k1::{recover, sign, PublicKey, RecoveryId};
use multihash::{Code, MultihashDigest};

#[cfg(feature = "with_serde")]
use serde::de::Error;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecoverableECDSASignature((u8, [u8; 64]));
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved

impl RecoverableECDSASignature {
pub fn sign(sk: &SecretKey, payload: &[u8]) -> anyhow::Result<Self> {
let v = Code::Blake2b256.digest(payload);

let (sig, rec_id) = sign(&libsecp256k1::Message::parse_slice(v.digest())?, &sk.0);
Ok(Self((rec_id.serialize(), sig.serialize())))
}

pub fn recover(&self, raw_message: &[u8]) -> anyhow::Result<(PublicKey, &[u8; 64])> {
let v = Code::Blake2b256.digest(raw_message);

let message = libsecp256k1::Message::parse_slice(v.digest())?;

let signature = libsecp256k1::Signature::parse_standard(&self.0 .1)
.context("invalid secp signature")?;
let rec_id = RecoveryId::parse(self.0 .0)?;

let pk = recover(&message, &signature, &rec_id)?;
Ok((pk, &self.0 .1))
}

pub fn verify(&self, raw_message: &[u8], pk: &PublicKey) -> anyhow::Result<bool> {
let (recovered_pk, _) = self.recover(raw_message)?;
Ok(recovered_pk == *pk)
}
}

#[cfg(feature = "with_serde")]
impl serde::Serialize for RecoverableECDSASignature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let t = (self.0 .0, self.0 .1.to_vec());
t.serialize(serializer)
}
}

#[cfg(feature = "with_serde")]
impl<'de> serde::Deserialize<'de> for RecoverableECDSASignature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let (rec_id, sig) = <(u8, Vec<u8>)>::deserialize(deserializer)?;
if sig.len() != libsecp256k1::util::SIGNATURE_SIZE {
return Err(D::Error::custom("invalid secp sig length"));
}

let mut v = [0; libsecp256k1::util::SIGNATURE_SIZE];
v.copy_from_slice(&sig);

Ok(Self((rec_id, v)))
}
}

#[cfg(test)]
mod tests {
use crate::secp::RecoverableECDSASignature;
use crate::SecretKey;
use rand::{thread_rng, RngCore};

#[test]
fn test_sign_verify() {
let mut rng = thread_rng();
let sk = SecretKey::random(&mut rng);

let mut payload = [0u8; 128];
rng.fill_bytes(&mut payload);

let sig = RecoverableECDSASignature::sign(&sk, &payload).unwrap();
assert!(
sig.verify(&payload, &sk.public_key()).unwrap(),
"verify failed"
);

let mut payload2 = [0u8; 128];
rng.fill_bytes(&mut payload2);

let sig = RecoverableECDSASignature::sign(&sk, &payload).unwrap();
assert!(
!sig.verify(&payload2, &sk.public_key()).unwrap(),
"should verify fail"
);
}
}
2 changes: 1 addition & 1 deletion fendermint/vm/genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ multihash = { workspace = true, optional = true }
fvm_shared = { workspace = true }
ipc-api = { workspace = true }
fendermint_actor_eam = { workspace = true }
hex = { workspace = true }

fendermint_crypto = { path = "../../crypto" }
fendermint_testing = { path = "../../testing", optional = true }
Expand All @@ -32,7 +33,6 @@ fendermint_vm_encoding = { path = "../encoding" }
[dev-dependencies]
quickcheck = { workspace = true }
quickcheck_macros = { workspace = true }
hex = { workspace = true }
serde_json = { workspace = true }
ipc-types = {workspace = true}

Expand Down
19 changes: 19 additions & 0 deletions fendermint/vm/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use anyhow::anyhow;
use fvm_shared::bigint::{BigInt, Integer};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};

use fendermint_actor_eam::PermissionModeParams;
use fvm_shared::chainid::ChainID;
Expand Down Expand Up @@ -162,6 +164,10 @@ impl ValidatorKey {
Self(normalize_public_key(key))
}

pub fn from_compressed_pubkey(compress: &[u8; 33]) -> anyhow::Result<Self> {
Ok(Self(PublicKey::parse_compressed(compress)?))
}

pub fn public_key(&self) -> &PublicKey {
&self.0
}
Expand Down Expand Up @@ -279,6 +285,19 @@ pub mod ipc {
}
}

impl Display for ValidatorKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Validator({})", hex::encode(self.0.serialize()))
}
}

impl Hash for ValidatorKey {
fn hash<H: Hasher>(&self, h: &mut H) {
let bytes = self.0.serialize();
Hash::hash(&bytes, h);
}
}

#[cfg(test)]
mod tests {
use fvm_shared::{bigint::BigInt, econ::TokenAmount};
Expand Down
2 changes: 1 addition & 1 deletion fendermint/vm/message/golden/chain/ipc_top_down.cbor
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a163497063a16b546f70446f776e45786563a2666865696768741ac0c004dd6a626c6f636b5f6861736889189600186418d418d10118b418a50c
a163497063a16b546f70446f776e45786563a2666865696768741a3a88b4f26a626c6f636b5f6861736882189c1873
2 changes: 1 addition & 1 deletion fendermint/vm/message/golden/chain/ipc_top_down.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ipc(TopDownExec(ParentFinality { height: 3233809629, block_hash: [150, 0, 100, 212, 209, 1, 180, 165, 12] }))
Ipc(TopDownExec(ParentFinality { height: 982037746, block_hash: [156, 115] }))
2 changes: 1 addition & 1 deletion fendermint/vm/message/golden/chain/signed.cbor
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a1665369676e6564836346766d8a1b4b8532f818ddaaf5550204d85d9156850dc6ce4b8a6b0101375f700a8278583904a4acb98481c882f28301ff0ae7e7c01e572b5282675f00ef067ce0a48c36fc3268a4abb000520c72ec076b4dc560014a2469bd27da777aa81b56c5d56baf7b2a1e510094ef316ac3b680473f7c04ed868770da1b6ea904e6e307bff05100347196edaa73fe5af0a455fcbbb0ed2d510024eaf6f65e84e2391e12a07a6f6253421ba8f37909642d99b746a923e1b3dc1a4402d83601
a1665369676e6564836346766d8a01550201c9fc09009c42509814adc6789300085dc64c0258310300800effaec8eedf3aa344f489e380f79d759d6e6bbd78352324645e20e92e45012583884fad46fefc5e21d3a4336f721b83092203d4d44aa05100ff01728a4f7fca0422b32e84bad2237e1b4e71b9a22c072e9c405100bd4bee58082d6517c9813b522d9741b41b77eb29502247176f44cb3fd01f4402295b21
2 changes: 1 addition & 1 deletion fendermint/vm/message/golden/chain/signed.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Signed(SignedMessage { origin_kind: Fvm, message: Message { version: 5441811765897571061, from: Address("f49503732383930537508f74fopz6adzlswuucm5pqb3ygptqkjdbw7qzgrjflwaafedds5qdwwtofmaauujdjxut5u532vdejzs63"), to: Address("f2atmf3ekwqug4ntslrjvqcajxl5yavatyuuaj5ba"), sequence: 6252638316156103198, value: TokenAmount(197967704622183385628.38811607395306313), method_num: 12174207298954959287, params: RawBytes { a923e1b3dc1a }, gas_limit: 7973910004934098928, gas_fee_cap: TokenAmount(69709646517097803841.751553548689141037), gas_premium: TokenAmount(49072214305296835349.416608417645220674) }, signature: Signature { sig_type: BLS, bytes: [216, 54, 1] } })
Signed(SignedMessage { origin_kind: Fvm, message: Message { version: 1, from: Address("f3acaa575ozdxn6ovdit2ity4a66oxlhlono6xqnjdersf4ihjfzcqcjmdrbh22rx67rpcdu5egnxxei5dafpa"), to: Address("f2ahe7yciatrbfbgauvxdhreyabbo4mtacy6s5dei"), sequence: 9442115493609884320, value: TokenAmount(338960654374797129026.300476426871251838), method_num: 8641045734189635439, params: RawBytes { cb3fd01f }, gas_limit: 5652503113501191836, gas_fee_cap: TokenAmount(0.0), gas_premium: TokenAmount(251618347655833941542.045183585254064564) }, signature: Signature { sig_type: BLS, bytes: [41, 91, 33] } })
8 changes: 6 additions & 2 deletions fendermint/vm/topdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
prometheus = { workspace = true }
arbitrary = { workspace = true }


multihash = { workspace = true }

fendermint_vm_genesis = { path = "../genesis" }
fendermint_vm_event = { path = "../event" }
fendermint_tracing = { path = "../../tracing" }
fendermint_crypto = { path = "../../crypto" }

ipc-observability = { workspace = true }

[dev-dependencies]
arbitrary = { workspace = true }

clap = { workspace = true }
rand = { workspace = true }
tracing-subscriber = { workspace = true }

fendermint_crypto = { path = "../../crypto" }
fendermint_testing = { path = "../../testing", features = ["smt"] }
1 change: 1 addition & 0 deletions fendermint/vm/topdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod proxy;
mod toggle;
pub mod voting;

pub mod observation;
pub mod observe;
pub mod vote;

Expand Down
123 changes: 123 additions & 0 deletions fendermint/vm/topdown/src/observation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::{BlockHeight, Bytes};
use anyhow::anyhow;
use arbitrary::Arbitrary;
use fendermint_crypto::secp::RecoverableECDSASignature;
use fendermint_crypto::SecretKey;
use fendermint_vm_genesis::ValidatorKey;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

/// The content that validators gossip among each other.
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Serialize, Deserialize, Hash, Debug, Clone, Eq, PartialEq, Arbitrary)]
pub struct Observation {
pub(crate) parent_height: u64,
/// The hash of the chain unit at that height. Usually a block hash, but could
/// be another entity (e.g. tipset CID), depending on the parent chain
/// and our interface to it. For example, if the parent is a Filecoin network,
/// this would be a tipset CID coerced into a block hash if queried through
/// the Eth API, or the tipset CID as-is if accessed through the Filecoin API.
pub(crate) parent_hash: Bytes,
/// A rolling/cumulative commitment to topdown effects since the beginning of
/// time, including the ones in this block.
pub(crate) cumulative_effects_comm: Bytes,
}

/// A self-certified observation made by a validator.
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct CertifiedObservation {
observation: Observation,
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
/// The signature for the observation only
observation_signature: RecoverableECDSASignature,
/// The hash of the subnet's last committed block when this observation was made.
/// Used to discard stale observations that are, e.g. replayed by an attacker
/// at a later time. Also used to detect nodes that might be wrongly gossiping
/// whilst being out of sync.
certified_at: BlockHeight,
/// A "recoverable" ECDSA signature with the validator's secp256k1 private key over the
/// CID of the DAG-CBOR encoded observation using a BLAKE2b-256 multihash.
signature: RecoverableECDSASignature,
}

impl TryFrom<&[u8]> for CertifiedObservation {
type Error = anyhow::Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Ok(fvm_ipld_encoding::from_slice(bytes)?)
}
}

impl CertifiedObservation {
pub fn observation(&self) -> &Observation {
&self.observation
}

pub fn ensure_valid(&self) -> anyhow::Result<ValidatorKey> {
let to_sign = fvm_ipld_encoding::to_vec(&self.observation)?;
let (pk1, _) = self.observation_signature.recover(&to_sign)?;

let p = Self::envelop_payload(&self.observation_signature, self.certified_at)?;
let (pk2, _) = self.signature.recover(p.as_slice())?;

if pk1 != pk2 {
return Err(anyhow!("public keys not aligned"));
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(ValidatorKey::new(pk1))
}

fn envelop_payload(
observation_sig: &RecoverableECDSASignature,
certified_at: BlockHeight,
) -> anyhow::Result<Bytes> {
Ok(fvm_ipld_encoding::to_vec(&(observation_sig, certified_at))?)
}

pub fn sign(
ob: Observation,
certified_at: BlockHeight,
sk: &SecretKey,
) -> anyhow::Result<Self> {
let obs_payload = fvm_ipld_encoding::to_vec(&ob)?;
let obs_sig = RecoverableECDSASignature::sign(sk, obs_payload.as_slice())?;

let p = Self::envelop_payload(&obs_sig, certified_at)?;
let sig = RecoverableECDSASignature::sign(sk, p.as_slice())?;
Ok(Self {
observation: ob,
observation_signature: obs_sig,
certified_at,
signature: sig,
})
}
}

impl Observation {
pub fn new(parent_height: BlockHeight, parent_hash: Bytes, commitment: Bytes) -> Self {
Self {
parent_height,
parent_hash,
cumulative_effects_comm: commitment,
}
}
}

impl Display for Observation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Observation(parent_height={}, parent_hash={}, commitment={})",
self.parent_height,
hex::encode(&self.parent_hash),
hex::encode(&self.cumulative_effects_comm),
)
}
}

impl Observation {
pub fn parent_height(&self) -> BlockHeight {
self.parent_height
}
}
Loading
Loading