From a7e52be935c9061946c711d883fc4fa9b90b4f5d Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:46:19 +0800 Subject: [PATCH 01/13] dep: add "getset" dep in "common" pkg --- Cargo.lock | 15 ++++++++++++++- common/Cargo.toml | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index e516b996..922b44f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2388,6 +2388,18 @@ dependencies = [ "wasi", ] +[[package]] +name = "getset" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f636605b743120a8d32ed92fc27b6cde1a769f8f936c065151eb66f88ded513c" +dependencies = [ + "proc-macro-error2", + "proc-macro2", + "quote", + "syn 2.0.75", +] + [[package]] name = "ghash" version = "0.5.1" @@ -3132,7 +3144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -3987,6 +3999,7 @@ dependencies = [ "alloy-rlp", "alloy-rlp-derive", "directories", + "getset", "hex", "k256", "libp2p", diff --git a/common/Cargo.toml b/common/Cargo.toml index 857a8f1b..9307b408 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -31,6 +31,7 @@ k256 = { workspace = true } directories = { workspace = true } thiserror = { workspace = true } toml = { workspace = true } +getset = "0.1.3" [dev-dependencies] rand = "0.8.5" From e2c1cc8fb0ca84598e60f0f6821f7697907d489c Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:47:00 +0800 Subject: [PATCH 02/13] feat: apply getset derive to structs in "common" --- block-builder/src/lib.rs | 12 +++--- common/src/tx_event.rs | 11 +++--- common/src/txs/mod.rs | 45 ++++++++-------------- common/src/txs/trust.rs | 64 ++++++++++---------------------- computer/src/lib.rs | 6 +-- sequencer/src/lib.rs | 6 +-- smart-contract-client/src/lib.rs | 4 +- verifier/src/lib.rs | 10 ++--- 8 files changed, 59 insertions(+), 99 deletions(-) diff --git a/block-builder/src/lib.rs b/block-builder/src/lib.rs index dc4b9a13..abb3659c 100644 --- a/block-builder/src/lib.rs +++ b/block-builder/src/lib.rs @@ -129,7 +129,7 @@ impl Node { .map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::TrustUpdate { + if *tx.kind() != Kind::TrustUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -149,7 +149,7 @@ impl Node { .map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::SeedUpdate { + if *tx.kind() != Kind::SeedUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -169,7 +169,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeRequest { + if *tx.kind() != Kind::ComputeRequest { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; @@ -209,7 +209,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeCommitment { + if *tx.kind() != Kind::ComputeCommitment { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; @@ -262,7 +262,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeScores { + if *tx.kind() != Kind::ComputeScores { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; @@ -284,7 +284,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeVerification { + if *tx.kind() != Kind::ComputeVerification { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; diff --git a/common/src/tx_event.rs b/common/src/tx_event.rs index 8816c520..344bb8a3 100644 --- a/common/src/tx_event.rs +++ b/common/src/tx_event.rs @@ -1,6 +1,7 @@ use crate::db::DbItem; use alloy_rlp::encode; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use serde::{Deserialize, Serialize}; use sha3::{Digest, Keccak256}; @@ -9,12 +10,15 @@ use sha3::{Digest, Keccak256}; pub struct InclusionProof([u8; 32]); /// Transaction event which includes proof of inclusion and custom data. -#[derive(Debug, Clone, RlpDecodable, RlpEncodable, Serialize, Deserialize)] +#[derive(Debug, Clone, RlpDecodable, RlpEncodable, Serialize, Deserialize, Getters)] pub struct TxEvent { + #[getset(skip)] /// Block height of the DA layer, where the tx was included. block_number: u64, + #[getset(skip)] /// Proof of inclusion in the DA block. proof: InclusionProof, + #[getset(get = "pub")] /// Data of the transaction. data: Vec, } @@ -28,11 +32,6 @@ impl TxEvent { pub fn default_with_data(data: Vec) -> Self { Self { block_number: 0, proof: InclusionProof::default(), data } } - - /// Returns the data of the tx event. - pub fn data(&self) -> Vec { - self.data.clone() - } } impl DbItem for TxEvent { diff --git a/common/src/txs/mod.rs b/common/src/txs/mod.rs index f96ae4d1..a2835bf6 100644 --- a/common/src/txs/mod.rs +++ b/common/src/txs/mod.rs @@ -2,6 +2,7 @@ use crate::db::DbItem; use crate::merkle::hash_leaf; use alloy_rlp::{encode, BufMut, Decodable, Encodable, Error as RlpError, Result as RlpResult}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use hex::FromHex; use k256::ecdsa::signature::hazmat::PrehashVerifier; use k256::ecdsa::{ @@ -81,16 +82,25 @@ impl From for String { } } -#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)] +#[derive( + Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters, +)] #[rlp(trailing)] pub struct Tx { + #[getset(get = "pub")] nonce: u64, + #[getset(get = "pub")] from: Address, + #[getset(get = "pub")] // Use 0x0 for transactions intended to be processed by the network to: Address, + #[getset(get = "pub")] kind: Kind, + #[getset(get = "pub")] body: Vec, + #[getset(get = "pub")] signature: Signature, + #[getset(skip)] sequence_number: Option, } @@ -107,30 +117,6 @@ impl Tx { } } - pub fn kind(&self) -> Kind { - self.kind - } - - pub fn body(&self) -> Vec { - self.body.clone() - } - - pub fn signature(&self) -> Signature { - self.signature.clone() - } - - pub fn nonce(&self) -> u64 { - self.nonce - } - - pub fn from(&self) -> Address { - self.from.clone() - } - - pub fn to(&self) -> Address { - self.to.clone() - } - pub fn hash(&self) -> TxHash { let mut hasher = Keccak256::new(); hasher.update(self.nonce.to_be_bytes()); @@ -289,11 +275,14 @@ impl TxHash { } #[derive( - Debug, Clone, PartialEq, Eq, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize, + Debug, Clone, PartialEq, Eq, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize, Getters, )] pub struct Signature { + #[getset(skip)] pub s: [u8; 32], + #[getset(skip)] pub r: [u8; 32], + #[getset(get = "pub")] r_id: u8, } @@ -301,10 +290,6 @@ impl Signature { pub fn new(s: [u8; 32], r: [u8; 32], r_id: u8) -> Self { Self { s, r, r_id } } - - pub fn r_id(&self) -> u8 { - self.r_id - } } #[cfg(test)] diff --git a/common/src/txs/trust.rs b/common/src/txs/trust.rs index 0d4a14bb..2a7bc589 100644 --- a/common/src/txs/trust.rs +++ b/common/src/txs/trust.rs @@ -2,6 +2,7 @@ use super::{Address, TxHash}; use crate::{merkle::Hash, topics::DomainHash}; use alloy_rlp::{BufMut, Decodable, Encodable, Error as RlpError, Result as RlpResult}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use core::result::Result as CoreResult; use hex::FromHex; use serde::{Deserialize, Serialize}; @@ -132,9 +133,13 @@ impl Decodable for TrustEntry { } } +#[derive(Getters)] pub struct AcceptedTrustUpdates { + #[get = "pub with_prefix"] sequence_number: u64, + #[getset(skip)] trust_update_tx_hashes: Vec, + #[getset(skip)] seed_update_tx_hashes: Vec, } @@ -146,10 +151,6 @@ impl AcceptedTrustUpdates { Self { sequence_number, trust_update_tx_hashes, seed_update_tx_hashes } } - pub fn get_sequence_number(&self) -> u64 { - self.sequence_number - } - pub fn get_trust_update_tx_hashes(&self) -> &Vec { &self.trust_update_tx_hashes } @@ -159,10 +160,15 @@ impl AcceptedTrustUpdates { } } +#[derive(Getters)] pub struct Assignment { + #[get = "pub with_prefix"] to_sequence: u64, + #[get = "pub with_prefix"] domain_id: DomainHash, + #[get = "pub with_prefix"] trust_builder: Address, + #[getset(skip)] trust_verifier: Vec
, } @@ -174,25 +180,16 @@ impl Assignment { Self { to_sequence, domain_id, trust_builder, trust_verifier } } - pub fn get_to_sequence(&self) -> u64 { - self.to_sequence - } - - pub fn get_domain_id(&self) -> DomainHash { - self.domain_id.clone() - } - - pub fn get_trust_builder(&self) -> Address { - self.trust_builder.clone() - } - pub fn get_trust_verifier(&self) -> &Vec
{ &self.trust_verifier } } +#[derive(Getters)] pub struct Commitment { + #[get = "pub with_prefix"] trust_assignment_tx_hash: TxHash, + #[get = "pub with_prefix"] root_hash: Hash, } @@ -200,18 +197,13 @@ impl Commitment { pub fn new(trust_assignment_tx_hash: TxHash, root_hash: Hash) -> Self { Self { trust_assignment_tx_hash, root_hash } } - - pub fn get_trust_assignment_tx_hash(&self) -> TxHash { - self.trust_assignment_tx_hash.clone() - } - - pub fn get_root_hash(&self) -> Hash { - self.root_hash.clone() - } } +#[derive(Getters)] pub struct Verification { + #[get = "pub with_prefix"] trust_commitment_tx_hash: TxHash, + #[get = "pub with_prefix"] verification_result: bool, } @@ -219,19 +211,15 @@ impl Verification { pub fn new(trust_commitment_tx_hash: TxHash, verification_result: bool) -> Self { Self { trust_commitment_tx_hash, verification_result } } - - pub fn get_trust_commitment_tx_hash(&self) -> TxHash { - self.trust_commitment_tx_hash.clone() - } - - pub fn get_verification_result(&self) -> bool { - self.verification_result - } } +#[derive(Getters)] pub struct Result { + #[get = "pub with_prefix"] trust_commitment_tx_hash: TxHash, + #[get = "pub with_prefix"] trust_verification_tx_hashes: Vec, + #[get = "pub with_prefix"] timestamp: u64, } @@ -241,16 +229,4 @@ impl Result { ) -> Self { Self { trust_commitment_tx_hash, trust_verification_tx_hashes, timestamp } } - - pub fn get_trust_commitment_tx_hash(&self) -> TxHash { - self.trust_commitment_tx_hash.clone() - } - - pub fn get_trust_verification_tx_hashes(&self) -> &Vec { - &self.trust_verification_tx_hashes - } - - pub fn get_timestamp(&self) -> u64 { - self.timestamp - } } diff --git a/computer/src/lib.rs b/computer/src/lib.rs index 457130a0..2fcca56a 100644 --- a/computer/src/lib.rs +++ b/computer/src/lib.rs @@ -105,7 +105,7 @@ impl Node { .map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::TrustUpdate { + if *tx.kind() != Kind::TrustUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -135,7 +135,7 @@ impl Node { .map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::SeedUpdate { + if *tx.kind() != Kind::SeedUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -165,7 +165,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeAssignment { + if *tx.kind() != Kind::ComputeAssignment { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; diff --git a/sequencer/src/lib.rs b/sequencer/src/lib.rs index 8b53bcda..a3ee6d49 100644 --- a/sequencer/src/lib.rs +++ b/sequencer/src/lib.rs @@ -74,7 +74,7 @@ impl Sequencer { let tx = Tx::decode(&mut tx_bytes.as_slice()) .map_err(|_| RPCError::ParseError("Failed to parse TX data".to_string()))?; - if tx.kind() != Kind::TrustUpdate { + if *tx.kind() != Kind::TrustUpdate { return Err(RPCError::InvalidRequest("Invalid tx kind")); } let address = tx @@ -112,7 +112,7 @@ impl Sequencer { let tx = Tx::decode(&mut tx_bytes.as_slice()) .map_err(|_| RPCError::ParseError("Failed to parse TX data".to_string()))?; - if tx.kind() != Kind::SeedUpdate { + if *tx.kind() != Kind::SeedUpdate { return Err(RPCError::InvalidRequest("Invalid tx kind")); } let address = tx @@ -152,7 +152,7 @@ impl Sequencer { error!("{}", e); RPCError::ParseError("Failed to parse TX data".to_string()) })?; - if tx.kind() != Kind::ComputeRequest { + if *tx.kind() != Kind::ComputeRequest { return Err(RPCError::InvalidRequest("Invalid tx kind")); } let address = tx diff --git a/smart-contract-client/src/lib.rs b/smart-contract-client/src/lib.rs index c7522b69..6a20c17e 100644 --- a/smart-contract-client/src/lib.rs +++ b/smart-contract-client/src/lib.rs @@ -94,7 +94,7 @@ impl ComputeManagerClient { let sig = Signature { s: tx.signature().s.into(), r: tx.signature().r.into(), - r_id: tx.signature().r_id(), + r_id: *tx.signature().r_id(), }; contract .submitComputeCommitment( @@ -114,7 +114,7 @@ impl ComputeManagerClient { let sig = Signature { s: tx.signature().s.into(), r: tx.signature().r.into(), - r_id: tx.signature().r_id(), + r_id: *tx.signature().r_id(), }; contract .submitComputeVerification( diff --git a/verifier/src/lib.rs b/verifier/src/lib.rs index bea7e272..1138a259 100644 --- a/verifier/src/lib.rs +++ b/verifier/src/lib.rs @@ -139,7 +139,7 @@ impl Node { .map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::TrustUpdate { + if *tx.kind() != Kind::TrustUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -169,7 +169,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::SeedUpdate { + if *tx.kind() != Kind::SeedUpdate { return Err(Error::InvalidTxKind); } tx.verify_against(namespace.owner()).map_err(Error::Signature)?; @@ -198,7 +198,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeAssignment { + if *tx.kind() != Kind::ComputeAssignment { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; @@ -256,7 +256,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeScores { + if *tx.kind() != Kind::ComputeScores { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; @@ -305,7 +305,7 @@ impl Node { .map_err(Error::Decode)?; let tx = Tx::decode(&mut tx_event.data().as_slice()) .map_err(Error::Decode)?; - if tx.kind() != Kind::ComputeCommitment { + if *tx.kind() != Kind::ComputeCommitment { return Err(Error::InvalidTxKind); } let address = tx.verify().map_err(Error::Signature)?; From 4871b11133b028704c850d4512510657fdc511d3 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:48:35 +0800 Subject: [PATCH 03/13] chore: fmt + clippy --- common/src/txs/mod.rs | 11 ++++++++++- common/src/txs/trust.rs | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/common/src/txs/mod.rs b/common/src/txs/mod.rs index a2835bf6..9f520f95 100644 --- a/common/src/txs/mod.rs +++ b/common/src/txs/mod.rs @@ -275,7 +275,16 @@ impl TxHash { } #[derive( - Debug, Clone, PartialEq, Eq, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize, Getters, + Debug, + Clone, + PartialEq, + Eq, + Default, + RlpDecodable, + RlpEncodable, + Serialize, + Deserialize, + Getters, )] pub struct Signature { #[getset(skip)] diff --git a/common/src/txs/trust.rs b/common/src/txs/trust.rs index 2a7bc589..bd9be2bd 100644 --- a/common/src/txs/trust.rs +++ b/common/src/txs/trust.rs @@ -2,8 +2,8 @@ use super::{Address, TxHash}; use crate::{merkle::Hash, topics::DomainHash}; use alloy_rlp::{BufMut, Decodable, Encodable, Error as RlpError, Result as RlpResult}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; -use getset::Getters; use core::result::Result as CoreResult; +use getset::Getters; use hex::FromHex; use serde::{Deserialize, Serialize}; use std::io::Read; From 6d8ec64c009fc97f8d6e7e33cabebe5385f288fc Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Thu, 31 Oct 2024 00:45:50 +0800 Subject: [PATCH 04/13] fix: update "sequencer/rpc.rs" --- sequencer/src/rpc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer/src/rpc.rs b/sequencer/src/rpc.rs index 62fcc7b3..b7c79679 100644 --- a/sequencer/src/rpc.rs +++ b/sequencer/src/rpc.rs @@ -63,7 +63,7 @@ impl SequencerServer { Some(e.to_string()), ) })?; - if tx.kind() != kind { + if *tx.kind() != kind { return Err(ErrorObjectOwned::owned( INVALID_REQUEST_CODE, "Invalid tx kind".to_string(), From 32b52a20da40161ce12d80995b13dd4f898df9ca Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:54:59 +0800 Subject: [PATCH 05/13] fix: correct some code related to tx body --- common/src/tx/mod.rs | 9 ++++++--- computer/src/lib.rs | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/common/src/tx/mod.rs b/common/src/tx/mod.rs index 75f96849..dc28b337 100644 --- a/common/src/tx/mod.rs +++ b/common/src/tx/mod.rs @@ -3,7 +3,6 @@ use crate::merkle::hash_leaf; use alloy_rlp::{encode, BufMut, Decodable, Encodable, Error as RlpError, Result as RlpResult}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; use getset::Getters; -use hex::FromHex; use block::{FinalisedBlock, ProposedBlock}; use k256::ecdsa::signature::hazmat::PrehashVerifier; use k256::ecdsa::{ @@ -104,7 +103,7 @@ impl Body { } #[derive( - Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters, + Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters, )] #[rlp(trailing)] pub struct Tx { @@ -115,7 +114,7 @@ pub struct Tx { #[getset(get = "pub")] // Use 0x0 for transactions intended to be processed by the network to: Address, - #[getset(get = "pub")] + #[getset(skip)] body: Body, #[getset(get = "pub")] signature: Signature, @@ -216,6 +215,10 @@ impl Tx { pub fn sequence_number(&self) -> u64 { self.sequence_number.unwrap_or_default() } + + pub fn body(&self) -> Body { + self.body.clone() + } } impl DbItem for Tx { diff --git a/computer/src/lib.rs b/computer/src/lib.rs index 5ab2403d..90d11a52 100644 --- a/computer/src/lib.rs +++ b/computer/src/lib.rs @@ -369,7 +369,7 @@ impl Node { for tx in txs { match tx.body() { Body::TrustUpdate(trust_update) => { - let namespace = trust_update.trust_id; + let namespace = trust_update.trust_id.clone(); let domain = self .config .domains @@ -381,7 +381,7 @@ impl Node { .map_err(Error::Runner)?; }, Body::SeedUpdate(seed_update) => { - let namespace = seed_update.seed_id; + let namespace = seed_update.seed_id.clone(); let domain = self .config .domains From b760368b435ac87c19645fa2b2bd18aba5dfdda9 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Fri, 1 Nov 2024 00:08:39 +0800 Subject: [PATCH 06/13] chore: fmt --- common/src/tx/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/common/src/tx/mod.rs b/common/src/tx/mod.rs index dc28b337..8e408767 100644 --- a/common/src/tx/mod.rs +++ b/common/src/tx/mod.rs @@ -2,8 +2,8 @@ use crate::db::DbItem; use crate::merkle::hash_leaf; use alloy_rlp::{encode, BufMut, Decodable, Encodable, Error as RlpError, Result as RlpResult}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; -use getset::Getters; use block::{FinalisedBlock, ProposedBlock}; +use getset::Getters; use k256::ecdsa::signature::hazmat::PrehashVerifier; use k256::ecdsa::{ Error as EcdsaError, RecoveryId, Signature as EcdsaSignature, SigningKey, VerifyingKey, @@ -102,9 +102,7 @@ impl Body { } } -#[derive( - Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters, -)] +#[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters)] #[rlp(trailing)] pub struct Tx { #[getset(get = "pub")] From 4ab82f71fcec1a01da3b54fdca268dc3d4c86d87 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:38:31 +0800 Subject: [PATCH 07/13] build: update dep version --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 83481b41..4852a853 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2369,7 +2369,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.85", ] [[package]] From 30dd1cb656b5fd6f249d57c57983474b74eb0e49 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Tue, 5 Nov 2024 03:26:25 +0800 Subject: [PATCH 08/13] build: update Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 61e2ed07..0fb9ae5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2369,7 +2369,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.85", + "syn 2.0.87", ] [[package]] From 1ccfa54e597d68f21e3f2fabd24cb45836d10831 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Tue, 5 Nov 2024 23:33:12 +0800 Subject: [PATCH 09/13] ref: improve the getters --- block-builder/src/lib.rs | 6 +++--- common/src/tx/mod.rs | 21 +++++---------------- common/src/tx/trust.rs | 31 +++++-------------------------- common/src/tx_event.rs | 4 +--- computer/src/lib.rs | 6 +++--- sequencer/src/rpc.rs | 8 ++++---- verifier/src/lib.rs | 8 ++++---- 7 files changed, 25 insertions(+), 59 deletions(-) diff --git a/block-builder/src/lib.rs b/block-builder/src/lib.rs index a36ba136..ea73b70b 100644 --- a/block-builder/src/lib.rs +++ b/block-builder/src/lib.rs @@ -207,7 +207,7 @@ impl Node { let assignment_tx_key = Tx::construct_full_key( consts::COMPUTE_ASSIGNMENT, - commitment.assignment_tx_hash, + commitment.assignment_tx_hash.clone(), ); let assignment_tx: Tx = self.db.get(assignment_tx_key).map_err(Error::Db)?; @@ -230,7 +230,7 @@ impl Node { self.coordinator.add_job_result(&mut result); self.db.put(result.clone()).map_err(Error::Db)?; let reference = compute::ResultReference::new( - assignment_body.request_tx_hash, + assignment_body.request_tx_hash.clone(), result.seq_number.unwrap(), ); self.db.put(reference).map_err(Error::Db)?; @@ -274,7 +274,7 @@ impl Node { let assignment_tx_key = Tx::construct_full_key( consts::COMPUTE_ASSIGNMENT, - compute_verification.assignment_tx_hash, + compute_verification.assignment_tx_hash.clone(), ); let assignment_tx: Tx = self.db.get(assignment_tx_key).map_err(Error::Db)?; diff --git a/common/src/tx/mod.rs b/common/src/tx/mod.rs index 8e408767..cea11a9f 100644 --- a/common/src/tx/mod.rs +++ b/common/src/tx/mod.rs @@ -104,19 +104,14 @@ impl Body { #[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters)] #[rlp(trailing)] +#[getset(get = "pub")] pub struct Tx { - #[getset(get = "pub")] nonce: u64, - #[getset(get = "pub")] from: Address, - #[getset(get = "pub")] // Use 0x0 for transactions intended to be processed by the network to: Address, - #[getset(skip)] body: Body, - #[getset(get = "pub")] signature: Signature, - #[getset(skip)] sequence_number: Option, } @@ -210,13 +205,9 @@ impl Tx { self.sequence_number = Some(sequence_number); } - pub fn sequence_number(&self) -> u64 { + pub fn get_sequence_number(&self) -> u64 { self.sequence_number.unwrap_or_default() } - - pub fn body(&self) -> Body { - self.body.clone() - } } impl DbItem for Tx { @@ -272,12 +263,10 @@ impl TxHash { Deserialize, Getters, )] +#[getset(get = "pub")] pub struct Signature { - #[getset(skip)] - pub s: [u8; 32], - #[getset(skip)] - pub r: [u8; 32], - #[getset(get = "pub")] + s: [u8; 32], + r: [u8; 32], r_id: u8, } diff --git a/common/src/tx/trust.rs b/common/src/tx/trust.rs index 336f99ad..698435fd 100644 --- a/common/src/tx/trust.rs +++ b/common/src/tx/trust.rs @@ -134,12 +134,10 @@ impl Decodable for TrustEntry { } #[derive(Getters)] +#[getset(get = "pub")] pub struct AcceptedTrustUpdates { - #[get = "pub with_prefix"] sequence_number: u64, - #[getset(skip)] trust_update_tx_hashes: Vec, - #[getset(skip)] seed_update_tx_hashes: Vec, } @@ -150,25 +148,14 @@ impl AcceptedTrustUpdates { ) -> Self { Self { sequence_number, trust_update_tx_hashes, seed_update_tx_hashes } } - - pub fn get_trust_update_tx_hashes(&self) -> &Vec { - &self.trust_update_tx_hashes - } - - pub fn get_seed_update_tx_hashes(&self) -> &Vec { - &self.seed_update_tx_hashes - } } #[derive(Getters)] +#[getset(get = "pub")] pub struct Assignment { - #[get = "pub with_prefix"] to_sequence: u64, - #[get = "pub with_prefix"] domain_id: DomainHash, - #[get = "pub with_prefix"] trust_builder: Address, - #[getset(skip)] trust_verifier: Vec
, } @@ -179,17 +166,12 @@ impl Assignment { ) -> Self { Self { to_sequence, domain_id, trust_builder, trust_verifier } } - - pub fn get_trust_verifier(&self) -> &Vec
{ - &self.trust_verifier - } } #[derive(Getters)] +#[getset(get = "pub")] pub struct Commitment { - #[get = "pub with_prefix"] trust_assignment_tx_hash: TxHash, - #[get = "pub with_prefix"] root_hash: Hash, } @@ -200,10 +182,9 @@ impl Commitment { } #[derive(Getters)] +#[getset(get = "pub")] pub struct Verification { - #[get = "pub with_prefix"] trust_commitment_tx_hash: TxHash, - #[get = "pub with_prefix"] verification_result: bool, } @@ -214,12 +195,10 @@ impl Verification { } #[derive(Getters)] +#[getset(get = "pub")] pub struct Result { - #[get = "pub with_prefix"] trust_commitment_tx_hash: TxHash, - #[get = "pub with_prefix"] trust_verification_tx_hashes: Vec, - #[get = "pub with_prefix"] timestamp: u64, } diff --git a/common/src/tx_event.rs b/common/src/tx_event.rs index e9e0d1f5..f2fec57a 100644 --- a/common/src/tx_event.rs +++ b/common/src/tx_event.rs @@ -11,14 +11,12 @@ pub struct InclusionProof([u8; 32]); /// Transaction event which includes proof of inclusion and custom data. #[derive(Debug, Clone, RlpDecodable, RlpEncodable, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct TxEvent { - #[getset(skip)] /// Block height of the DA layer, where the tx was included. block_number: u64, - #[getset(skip)] /// Proof of inclusion in the DA block. proof: InclusionProof, - #[getset(get = "pub")] /// Data of the transaction. data: Vec, } diff --git a/computer/src/lib.rs b/computer/src/lib.rs index 90d11a52..dd07f6c2 100644 --- a/computer/src/lib.rs +++ b/computer/src/lib.rs @@ -103,7 +103,7 @@ impl Node { TxEvent::decode(&mut message.data.as_slice()).map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()).map_err(Error::Decode)?; - if let Body::TrustUpdate(trust_update) = tx.body() { + if let Body::TrustUpdate(trust_update) = tx.body().clone() { tx.verify_against(namespace.owner()).map_err(Error::Signature)?; // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); @@ -129,7 +129,7 @@ impl Node { TxEvent::decode(&mut message.data.as_slice()).map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()).map_err(Error::Decode)?; - if let Body::SeedUpdate(seed_update) = tx.body() { + if let Body::SeedUpdate(seed_update) = tx.body().clone() { tx.verify_against(namespace.owner()).map_err(Error::Signature)?; // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); @@ -363,7 +363,7 @@ impl Node { drop(seed_update_txs); // sort txs by sequence_number - txs.sort_unstable_by_key(|tx| tx.sequence_number()); + txs.sort_unstable_by_key(|tx| tx.get_sequence_number()); // update compute runner for tx in txs { diff --git a/sequencer/src/rpc.rs b/sequencer/src/rpc.rs index 45a5bb8d..d2808645 100644 --- a/sequencer/src/rpc.rs +++ b/sequencer/src/rpc.rs @@ -93,7 +93,7 @@ impl SequencerServer { )); } - Ok((tx_bytes, tx.body())) + Ok((tx_bytes, tx.body().clone())) } } @@ -199,7 +199,7 @@ impl RpcServer for SequencerServer { error!("{}", e); ErrorObjectOwned::from(ErrorCode::InternalError) })?; - let commitment = match tx.body() { + let commitment = match tx.body().clone() { tx::Body::ComputeCommitment(commitment) => Ok(commitment), _ => Err(ErrorObjectOwned::from(ErrorCode::InternalError)), }?; @@ -218,7 +218,7 @@ impl RpcServer for SequencerServer { let create_scores: Vec = { let mut create_scores = Vec::new(); for tx in create_scores_tx.into_iter() { - let scores = match tx.body() { + let scores = match tx.body().clone() { tx::Body::ComputeScores(scores) => Ok(scores), _ => Err(ErrorObjectOwned::from(ErrorCode::InternalError)), }?; @@ -264,7 +264,7 @@ impl RpcServer for SequencerServer { let verification_results: Vec = { let mut verification_results = Vec::new(); for tx in verificarion_results_tx.into_iter() { - let result = match tx.body() { + let result = match tx.body().clone() { tx::Body::ComputeVerification(result) => Ok(result), _ => Err(ErrorObjectOwned::from(ErrorCode::InternalError)), }?; diff --git a/verifier/src/lib.rs b/verifier/src/lib.rs index 9980f16f..6e344706 100644 --- a/verifier/src/lib.rs +++ b/verifier/src/lib.rs @@ -137,7 +137,7 @@ impl Node { TxEvent::decode(&mut message.data.as_slice()).map_err(Error::Decode)?; let mut tx = Tx::decode(&mut tx_event.data().as_slice()).map_err(Error::Decode)?; - if let tx::Body::TrustUpdate(trust_update) = tx.body() { + if let tx::Body::TrustUpdate(trust_update) = tx.body().clone() { tx.verify_against(namespace.owner()).map_err(Error::Signature)?; // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); @@ -345,13 +345,13 @@ impl Node { drop(seed_update_txs); // sort txs by sequence_number - txs.sort_unstable_by_key(|tx| tx.sequence_number()); + txs.sort_unstable_by_key(|tx| tx.get_sequence_number()); // update verification runner for tx in txs { match tx.body() { tx::Body::TrustUpdate(trust_update) => { - let namespace = trust_update.trust_id; + let namespace = trust_update.trust_id.clone(); let domain = self .config .domains @@ -363,7 +363,7 @@ impl Node { .map_err(Error::Runner)?; }, tx::Body::SeedUpdate(seed_update) => { - let namespace = seed_update.seed_id; + let namespace = seed_update.seed_id.clone(); let domain = self .config .domains From 4f87c050f64ed71d7422eadfba2d51eb637acc23 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:39:25 +0800 Subject: [PATCH 10/13] ref: use getset for more structs --- Cargo.lock | 6 +++ Cargo.toml | 1 + block-builder/Cargo.toml | 1 + block-builder/src/coordinator.rs | 4 +- block-builder/src/lib.rs | 49 +++++++++++---------- common/Cargo.toml | 2 +- common/src/db.rs | 8 ++-- common/src/lib.rs | 1 - common/src/net.rs | 18 +++++--- common/src/result.rs | 10 +++-- common/src/tx/compute.rs | 73 ++++++++++++++++++++------------ common/src/tx/trust.rs | 34 +++++++++------ computer/Cargo.toml | 1 + computer/src/lib.rs | 46 +++++++++++--------- computer/src/runner.rs | 20 ++++----- openrank-sdk/Cargo.toml | 1 + openrank-sdk/src/main.rs | 25 ++++++----- relayer/Cargo.toml | 1 + relayer/src/lib.rs | 14 +++--- relayer/src/main.rs | 6 ++- sequencer/Cargo.toml | 1 + sequencer/src/lib.rs | 27 ++++++------ sequencer/src/rpc.rs | 32 +++++++------- verifier/Cargo.toml | 1 + verifier/src/lib.rs | 46 +++++++++++--------- verifier/src/runner.rs | 44 +++++++++---------- 26 files changed, 274 insertions(+), 198 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fb9ae5d..f333eaa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4205,6 +4205,7 @@ dependencies = [ "clap", "dotenv", "futures", + "getset", "hex", "k256", "libp2p", @@ -4270,6 +4271,7 @@ dependencies = [ "alloy-rlp", "dotenv", "futures", + "getset", "hex", "k256", "libp2p", @@ -4298,6 +4300,7 @@ dependencies = [ "dotenv", "env_logger", "futures", + "getset", "k256", "libp2p", "log", @@ -4325,6 +4328,7 @@ dependencies = [ "clap", "csv", "dotenv", + "getset", "hex", "jsonrpsee", "k256", @@ -4343,6 +4347,7 @@ version = "0.1.1" dependencies = [ "alloy-rlp", "futures", + "getset", "hex", "jsonrpsee", "libp2p", @@ -4380,6 +4385,7 @@ dependencies = [ "alloy-rlp", "dotenv", "futures", + "getset", "hex", "k256", "libp2p", diff --git a/Cargo.toml b/Cargo.toml index 00170236..38b84e2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,3 +45,4 @@ k256 = "0.13.3" directories = "5.0.1" thiserror = "1.0.63" clap = "4.5.9" +getset = "0.1.3" diff --git a/block-builder/Cargo.toml b/block-builder/Cargo.toml index 078acb48..ca688a5c 100644 --- a/block-builder/Cargo.toml +++ b/block-builder/Cargo.toml @@ -36,3 +36,4 @@ toml = { workspace = true } dotenv = { workspace = true } k256 = { workspace = true } clap = { workspace = true, features = ["derive"] } +getset = { workspace = true } diff --git a/block-builder/src/coordinator.rs b/block-builder/src/coordinator.rs index f58834a2..624d203a 100644 --- a/block-builder/src/coordinator.rs +++ b/block-builder/src/coordinator.rs @@ -20,11 +20,11 @@ impl JobCoordinator { /// Add a JobResult to memory and increase the counter in case /// it has not been seen before. pub fn add_job_result(&mut self, compute_result: &mut compute::Result) { - if compute_result.seq_number.is_none() { + if compute_result.seq_number().is_none() { compute_result.set_seq_number(self.count); self.count += 1; } - let seq_number = compute_result.seq_number.unwrap(); + let seq_number = compute_result.seq_number().unwrap(); self.job_results.insert(seq_number, compute_result.clone()); if seq_number > self.count { self.count = seq_number; diff --git a/block-builder/src/lib.rs b/block-builder/src/lib.rs index ea73b70b..eb5593db 100644 --- a/block-builder/src/lib.rs +++ b/block-builder/src/lib.rs @@ -2,6 +2,7 @@ use alloy_rlp::Decodable; use coordinator::JobCoordinator; use dotenv::dotenv; use futures::StreamExt; +use getset::Getters; use k256::ecdsa; use k256::ecdsa::SigningKey; use libp2p::{gossipsub, mdns, swarm::SwarmEvent, Swarm}; @@ -52,26 +53,28 @@ impl Display for Error { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The whitelist for the Block Builder. -pub struct Whitelist { +struct Whitelist { /// The list of addresses that are allowed to be computers. - pub computer: Vec
, + computer: Vec
, /// The list of addresses that are allowed to be verifiers. - pub verifier: Vec
, + verifier: Vec
, /// The list of addresses that are allowed to broadcast transactions. - pub users: Vec
, + users: Vec
, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the Block Builder. -pub struct Config { +struct Config { /// The list of domains to process ComputeRequest TXs for. - pub domains: Vec, + domains: Vec, /// The whitelist for the Block Builder. - pub whitelist: Whitelist, - pub database: db::Config, - pub p2p: net::Config, + whitelist: Whitelist, + database: db::Config, + p2p: net::Config, } /// The Block Builder node. It contains the Swarm, the Config, the DB, the SecretKey, and the ComputeRunner. @@ -105,7 +108,7 @@ impl Node { &[&Tx::get_cf(), &compute::Result::get_cf(), &compute::ResultReference::get_cf()], )?; - let swarm = build_node(net::load_keypair(&config.p2p.keypair, &config_loader)?).await?; + let swarm = build_node(net::load_keypair(config.p2p().keypair(), &config_loader)?).await?; info!("PEER_ID: {:?}", swarm.local_peer_id()); let coordinator = JobCoordinator::new(); @@ -172,7 +175,7 @@ impl Node { assert!(self.config.whitelist.users.contains(&address)); // Add Tx to db self.db.put(tx.clone()).map_err(Error::Db)?; - assert_eq!(&compute_request.domain_id, domain_id); + assert_eq!(compute_request.domain_id(), domain_id); let assignment_topic = Topic::DomainAssignent(*domain_id); let computer = self.config.whitelist.computer[0]; @@ -207,7 +210,7 @@ impl Node { let assignment_tx_key = Tx::construct_full_key( consts::COMPUTE_ASSIGNMENT, - commitment.assignment_tx_hash.clone(), + commitment.assignment_tx_hash().clone(), ); let assignment_tx: Tx = self.db.get(assignment_tx_key).map_err(Error::Db)?; @@ -217,12 +220,12 @@ impl Node { }; let request_tx_key = Tx::construct_full_key( consts::COMPUTE_REQUEST, - assignment_body.request_tx_hash.clone(), + assignment_body.request_tx_hash().clone(), ); let request: Tx = self.db.get(request_tx_key).map_err(Error::Db)?; if let Err(db::Error::NotFound) = self.db.get::( - assignment_body.request_tx_hash.0.to_vec(), + assignment_body.request_tx_hash().0.to_vec(), ) { let mut result = @@ -230,8 +233,8 @@ impl Node { self.coordinator.add_job_result(&mut result); self.db.put(result.clone()).map_err(Error::Db)?; let reference = compute::ResultReference::new( - assignment_body.request_tx_hash.clone(), - result.seq_number.unwrap(), + assignment_body.request_tx_hash().clone(), + result.seq_number().unwrap(), ); self.db.put(reference).map_err(Error::Db)?; } @@ -274,7 +277,7 @@ impl Node { let assignment_tx_key = Tx::construct_full_key( consts::COMPUTE_ASSIGNMENT, - compute_verification.assignment_tx_hash.clone(), + compute_verification.assignment_tx_hash().clone(), ); let assignment_tx: Tx = self.db.get(assignment_tx_key).map_err(Error::Db)?; @@ -284,13 +287,13 @@ impl Node { }; let result_reference: compute::ResultReference = self .db - .get(assignment_body.request_tx_hash.0.to_vec()) + .get(assignment_body.request_tx_hash().0.to_vec()) .map_err(Error::Db)?; let compute_result_key = - compute::Result::construct_full_key(result_reference.seq_number); + compute::Result::construct_full_key(*result_reference.seq_number()); let mut result: compute::Result = self.db.get(compute_result_key).map_err(Error::Db)?; - result.compute_verification_tx_hashes.push(tx.hash()); + result.append_verification_tx_hash(tx.hash()); self.coordinator.add_job_result(&mut result); self.db.put(result).map_err(Error::Db)?; info!( @@ -325,7 +328,7 @@ impl Node { /// - Handles gossipsub events. /// - Handles mDNS events. pub async fn run(&mut self) -> Result<(), Box> { - net::listen_on(&mut self.swarm, &self.config.p2p.listen_on)?; + net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; let topics_trust_updates: Vec = self .config diff --git a/common/Cargo.toml b/common/Cargo.toml index 15742041..8f5bcd04 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -21,6 +21,7 @@ libp2p = { workspace = true, features = [ ] } alloy-rlp = { workspace = true } alloy-rlp-derive = { workspace = true } +getset = { workspace = true } alloy-primitives = { workspace = true, features = ["serde", "rlp"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } @@ -32,7 +33,6 @@ k256 = { workspace = true } directories = { workspace = true } thiserror = { workspace = true } toml = { workspace = true } -getset = "0.1.3" [dev-dependencies] rand = "0.8.5" diff --git a/common/src/db.rs b/common/src/db.rs index fae580ee..338279b5 100644 --- a/common/src/db.rs +++ b/common/src/db.rs @@ -1,3 +1,4 @@ +use getset::Getters; use rocksdb::{self, Options, DB}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::{self, to_vec}; @@ -44,10 +45,11 @@ pub trait DbItem { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct Config { - pub directory: String, - pub secondary: Option, + directory: String, + secondary: Option, } impl Config { diff --git a/common/src/lib.rs b/common/src/lib.rs index 674341a7..11332879 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -29,7 +29,6 @@ use tx_event::TxEvent; pub struct MyBehaviour { pub gossipsub: gossipsub::Behaviour, pub mdns: mdns::tokio::Behaviour, - // pub identify: identify::Behaviour, } /// Builds a libp2p swarm with the custom behaviour. diff --git a/common/src/net.rs b/common/src/net.rs index 86fdede3..bbb5e8f8 100644 --- a/common/src/net.rs +++ b/common/src/net.rs @@ -1,3 +1,4 @@ +use getset::Getters; use libp2p::core::transport::ListenerId; use libp2p::identity::{DecodingError, Keypair}; use libp2p::{swarm, Swarm, TransportError}; @@ -23,24 +24,27 @@ pub enum Error { } /// Rpc configuration. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct RpcConfig { - pub address: SocketAddr, + address: SocketAddr, } /// Network configuration. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct Config { - pub listen_on: Vec, - pub keypair: Option, + listen_on: Vec, + keypair: Option, } /// P2P keypair configuration. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct KeypairConfig { /// Filename of the keypair. Either absolute or relative to the config directory. /// The file contains binary protobuf representation of the keypair. - pub file: Option, + file: Option, } /// Default P2P keypair filename. diff --git a/common/src/result.rs b/common/src/result.rs index 0cf89762..50d0f316 100644 --- a/common/src/result.rs +++ b/common/src/result.rs @@ -1,11 +1,13 @@ use crate::tx; +use getset::Getters; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct GetResultsQuery { - pub request_tx_hash: tx::TxHash, - pub start: u32, - pub size: u32, + request_tx_hash: tx::TxHash, + start: u32, + size: u32, } impl GetResultsQuery { diff --git a/common/src/tx/compute.rs b/common/src/tx/compute.rs index dcd30ee6..751209b1 100644 --- a/common/src/tx/compute.rs +++ b/common/src/tx/compute.rs @@ -1,14 +1,18 @@ use crate::tx::{trust::ScoreEntry, Address, TxHash}; use crate::{db::DbItem, merkle::Hash, topics::DomainHash}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct Commitment { - pub assignment_tx_hash: TxHash, - pub lt_root_hash: Hash, - pub compute_root_hash: Hash, - pub scores_tx_hashes: Vec, + assignment_tx_hash: TxHash, + lt_root_hash: Hash, + compute_root_hash: Hash, + scores_tx_hashes: Vec, } impl Commitment { @@ -20,9 +24,12 @@ impl Commitment { } } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct Scores { - pub entries: Vec, + entries: Vec, } impl Scores { @@ -31,11 +38,14 @@ impl Scores { } } -#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct Request { - pub domain_id: DomainHash, - pub block_height: u32, - pub compute_id: Hash, + domain_id: DomainHash, + block_height: u32, + compute_id: Hash, } impl Request { @@ -44,11 +54,14 @@ impl Request { } } -#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct Assignment { - pub request_tx_hash: TxHash, - pub assigned_compute_node: Address, - pub assigned_verifier_node: Address, + request_tx_hash: TxHash, + assigned_compute_node: Address, + assigned_verifier_node: Address, } impl Assignment { @@ -59,10 +72,11 @@ impl Assignment { } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters)] +#[getset(get = "pub")] pub struct Verification { - pub assignment_tx_hash: TxHash, - pub verification_result: bool, + assignment_tx_hash: TxHash, + verification_result: bool, } impl Verification { @@ -78,17 +92,18 @@ impl Default for Verification { } /// Combination of several tx hashes representing the result of a compute run by `Computer`. -#[derive(Debug, Clone, RlpEncodable, RlpDecodable, Serialize, Deserialize)] +#[derive(Debug, Clone, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters)] #[rlp(trailing)] +#[getset(get = "pub")] pub struct Result { /// Hash of the ComputeCommitment TX. - pub compute_commitment_tx_hash: TxHash, + compute_commitment_tx_hash: TxHash, /// Hashes of the ComputeVerification TXs. - pub compute_verification_tx_hashes: Vec, + compute_verification_tx_hashes: Vec, /// Hash of the original ComputeRequest TX. - pub compute_request_tx_hash: TxHash, + compute_request_tx_hash: TxHash, /// Sequence number assigned by the block builder. - pub seq_number: Option, + seq_number: Option, } impl Result { @@ -115,6 +130,11 @@ impl Result { pub fn set_seq_number(&mut self, seq_number: u64) { self.seq_number = Some(seq_number); } + + /// Append verification tx hash + pub fn append_verification_tx_hash(&mut self, tx_hash: TxHash) { + self.compute_verification_tx_hashes.push(tx_hash); + } } impl DbItem for Result { @@ -132,12 +152,13 @@ impl DbItem for Result { } /// Object connecting the sequence number with the original compute request -#[derive(Debug, Clone, RlpEncodable, RlpDecodable, Serialize, Deserialize)] +#[derive(Debug, Clone, RlpEncodable, RlpDecodable, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct ResultReference { /// Hash of the original job run request transaction. - pub compute_request_tx_hash: TxHash, + compute_request_tx_hash: TxHash, /// Sequence number assigned by the block builder. - pub seq_number: u64, + seq_number: u64, } impl ResultReference { diff --git a/common/src/tx/trust.rs b/common/src/tx/trust.rs index 698435fd..a2e8ef3d 100644 --- a/common/src/tx/trust.rs +++ b/common/src/tx/trust.rs @@ -40,10 +40,13 @@ impl FromHex for OwnedNamespace { } } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct TrustUpdate { - pub trust_id: OwnedNamespace, - pub entries: Vec, + trust_id: OwnedNamespace, + entries: Vec, } impl TrustUpdate { @@ -52,10 +55,13 @@ impl TrustUpdate { } } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct SeedUpdate { - pub seed_id: OwnedNamespace, - pub entries: Vec, + seed_id: OwnedNamespace, + entries: Vec, } impl SeedUpdate { @@ -64,10 +70,11 @@ impl SeedUpdate { } } -#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct ScoreEntry { - pub id: String, - pub value: f32, + id: String, + value: f32, } impl ScoreEntry { @@ -97,11 +104,12 @@ impl Decodable for ScoreEntry { } } -#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] pub struct TrustEntry { - pub from: String, - pub to: String, - pub value: f32, + from: String, + to: String, + value: f32, } impl TrustEntry { diff --git a/computer/Cargo.toml b/computer/Cargo.toml index 6aad701f..e927ee8b 100644 --- a/computer/Cargo.toml +++ b/computer/Cargo.toml @@ -33,3 +33,4 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } sha3 = { workspace = true } k256 = { workspace = true } +getset = { workspace = true } diff --git a/computer/src/lib.rs b/computer/src/lib.rs index dd07f6c2..832c29b2 100644 --- a/computer/src/lib.rs +++ b/computer/src/lib.rs @@ -1,6 +1,7 @@ use alloy_rlp::Decodable; use dotenv::dotenv; use futures::StreamExt; +use getset::Getters; use k256::ecdsa::{self, SigningKey}; use libp2p::{gossipsub, mdns, swarm::SwarmEvent, Swarm}; use openrank_common::{ @@ -62,18 +63,20 @@ impl From for Error { } } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Whitelist { +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] +struct Whitelist { block_builder: Vec
, verifier: Vec
, } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Config { - pub domains: Vec, - pub whitelist: Whitelist, - pub database: db::Config, - pub p2p: net::Config, +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] +struct Config { + domains: Vec, + whitelist: Whitelist, + database: db::Config, + p2p: net::Config, } pub struct Node { @@ -108,13 +111,13 @@ impl Node { // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); self.db.put(tx.clone()).map_err(Error::Db)?; - assert!(*namespace == trust_update.trust_id); + assert!(namespace == trust_update.trust_id()); let domain = domains .iter() .find(|x| &x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.compute_runner - .update_trust(domain.clone(), trust_update.entries.clone()) + .update_trust(domain.clone(), trust_update.entries().clone()) .map_err(Error::Runner)?; info!( "TOPIC: {}, ID: {message_id}, FROM: {propagation_source}", @@ -134,13 +137,13 @@ impl Node { // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); self.db.put(tx.clone()).map_err(Error::Db)?; - assert!(*namespace == seed_update.seed_id); + assert!(namespace == seed_update.seed_id()); let domain = domains .iter() .find(|x| &x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.compute_runner - .update_seed(domain.clone(), seed_update.entries.clone()) + .update_seed(domain.clone(), seed_update.entries().clone()) .map_err(Error::Runner)?; info!( "TOPIC: {}, ID: {message_id}, FROM: {propagation_source}", @@ -161,12 +164,15 @@ impl Node { // Add Tx to db self.db.put(tx.clone()).map_err(Error::Db)?; let computer_address = address_from_sk(&self.secret_key); - assert_eq!(computer_address, compute_assignment.assigned_compute_node); + assert_eq!( + &computer_address, + compute_assignment.assigned_compute_node() + ); assert!(self .config .whitelist .verifier - .contains(&compute_assignment.assigned_verifier_node)); + .contains(compute_assignment.assigned_verifier_node())); let domain = domains .iter() @@ -260,7 +266,7 @@ impl Node { let domain_hashes = config.domains.iter().map(|x| x.to_hash()).collect(); let compute_runner = ComputeRunner::new(domain_hashes); - let swarm = build_node(net::load_keypair(&config.p2p.keypair, &config_loader)?).await?; + let swarm = build_node(net::load_keypair(config.p2p().keypair(), &config_loader)?).await?; info!("PEER_ID: {:?}", swarm.local_peer_id()); Ok(Self { swarm, config, db, compute_runner, secret_key }) @@ -309,7 +315,7 @@ impl Node { self.swarm.behaviour_mut().gossipsub.subscribe(&topic)?; } - net::listen_on(&mut self.swarm, &self.config.p2p.listen_on)?; + net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; // Kick it off loop { @@ -369,7 +375,7 @@ impl Node { for tx in txs { match tx.body() { Body::TrustUpdate(trust_update) => { - let namespace = trust_update.trust_id.clone(); + let namespace = trust_update.trust_id().clone(); let domain = self .config .domains @@ -377,11 +383,11 @@ impl Node { .find(|x| x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.compute_runner - .update_trust(domain.clone(), trust_update.entries.clone()) + .update_trust(domain.clone(), trust_update.entries().clone()) .map_err(Error::Runner)?; }, Body::SeedUpdate(seed_update) => { - let namespace = seed_update.seed_id.clone(); + let namespace = seed_update.seed_id().clone(); let domain = self .config .domains @@ -389,7 +395,7 @@ impl Node { .find(|x| x.seed_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.compute_runner - .update_seed(domain.clone(), seed_update.entries.clone()) + .update_seed(domain.clone(), seed_update.entries().clone()) .map_err(Error::Runner)?; }, _ => (), diff --git a/computer/src/runner.rs b/computer/src/runner.rs index f74249d6..bd068c28 100644 --- a/computer/src/runner.rs +++ b/computer/src/runner.rs @@ -85,29 +85,29 @@ impl ComputeRunner { .ok_or(Error::SeedTrustNotFound(domain.to_hash()))?; let default_sub_tree = DenseIncrementalMerkleTree::::new(32); for entry in trust_entries { - let from_index = if let Some(i) = domain_indices.get(&entry.from) { + let from_index = if let Some(i) = domain_indices.get(entry.from()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.from.clone(), curr_count); + domain_indices.insert(entry.from().clone(), curr_count); *count += 1; curr_count }; - let to_index = if let Some(i) = domain_indices.get(&entry.to) { + let to_index = if let Some(i) = domain_indices.get(entry.to()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.to.clone(), *count); + domain_indices.insert(entry.to().clone(), *count); *count += 1; curr_count }; - lt.insert((from_index, to_index), entry.value); + lt.insert((from_index, to_index), *entry.value()); lt_sub_trees.entry(from_index).or_insert_with(|| default_sub_tree.clone()); let sub_tree = lt_sub_trees .get_mut(&from_index) .ok_or(Error::LocalTrustSubTreesNotFoundWithIndex(from_index))?; - let leaf = hash_leaf::(entry.value.to_be_bytes().to_vec()); + let leaf = hash_leaf::(entry.value().to_be_bytes().to_vec()); sub_tree.insert_leaf(to_index, leaf); let sub_tree_root = sub_tree.root().map_err(Error::Merkle)?; @@ -143,11 +143,11 @@ impl ComputeRunner { .ok_or(Error::SeedTrustNotFound(domain.to_hash()))?; let default_sub_tree = DenseIncrementalMerkleTree::::new(32); for entry in seed_entries { - let index = if let Some(i) = domain_indices.get(&entry.id) { + let index = if let Some(i) = domain_indices.get(entry.id()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.id.clone(), curr_count); + domain_indices.insert(entry.id().clone(), curr_count); *count += 1; curr_count }; @@ -157,11 +157,11 @@ impl ComputeRunner { .get_mut(&index) .ok_or(Error::LocalTrustSubTreesNotFoundWithIndex(index))?; let sub_tree_root = sub_tree.root().map_err(Error::Merkle)?; - let seed_hash = hash_leaf::(entry.value.to_be_bytes().to_vec()); + let seed_hash = hash_leaf::(entry.value().to_be_bytes().to_vec()); let leaf = hash_two::(sub_tree_root, seed_hash); lt_master_tree.insert_leaf(index, leaf); - seed.insert(index, entry.value); + seed.insert(index, *entry.value()); } Ok(()) diff --git a/openrank-sdk/Cargo.toml b/openrank-sdk/Cargo.toml index c86810d0..4095345f 100644 --- a/openrank-sdk/Cargo.toml +++ b/openrank-sdk/Cargo.toml @@ -21,3 +21,4 @@ dotenv = { workspace = true } rand = { workspace = true } csv = "1.3.0" clap = { version = "4.5.9", features = ["derive"] } +getset = { workspace = true } diff --git a/openrank-sdk/src/main.rs b/openrank-sdk/src/main.rs index 171ff856..bbfcd77d 100644 --- a/openrank-sdk/src/main.rs +++ b/openrank-sdk/src/main.rs @@ -2,6 +2,7 @@ use alloy_rlp::encode; use clap::{Parser, Subcommand}; use csv::StringRecord; use dotenv::dotenv; +use getset::Getters; use jsonrpsee::{core::client::ClientT, http_client::HttpClient}; use k256::{ecdsa::SigningKey, schnorr::CryptoRngCore}; use openrank_common::{ @@ -72,13 +73,14 @@ pub struct Sequencer { result_size: u32, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the SDK. pub struct Config { /// The domain to be updated. - pub domain: Domain, + domain: Domain, /// The Sequencer configuration. It contains the endpoint of the Sequencer. - pub sequencer: Sequencer, + sequencer: Sequencer, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -245,13 +247,16 @@ async fn get_compute_result_txs(arg: String, config_path: &str) -> Result Result<(), Box> { println!("votes: {:?}", votes); for res in &scores { - println!("{}: {}", res.id, res.value); + println!("{}: {}", res.id().clone(), *res.value()); } if let Some(output_path) = output_path { write_json_to_file(&output_path, scores)?; diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index 2fd9c3b9..05f8d354 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -43,3 +43,4 @@ async-graphql = "5.0" # or juniper for GraphQL? sqlx = { version = "0.6", features = ["postgres", "runtime-tokio-native-tls"] } async-graphql-warp = "5.0" base64 = "0.22.1" +getset = { workspace = true } diff --git a/relayer/src/lib.rs b/relayer/src/lib.rs index 7fc91361..c8133bbb 100644 --- a/relayer/src/lib.rs +++ b/relayer/src/lib.rs @@ -30,7 +30,7 @@ impl SQLRelayer { let mut last_processed_keys = HashMap::new(); - let path = db_config.clone().secondary.expect("No secondary path found"); + let path = db_config.clone().secondary().clone().expect("No secondary path found"); let last_processed_key = target_db .load_last_processed_key(&format!("relayer_last_key_{}_{}", path, "tx")) .await @@ -68,7 +68,7 @@ impl SQLRelayer { self.db.refresh().unwrap(); let results = self.db.read_from_end::("result", None).unwrap(); - let dir = self.db.get_config().secondary.expect("Secondary path missing"); + let dir = self.db.get_config().secondary().clone().expect("Secondary path missing"); let last_count = self.last_processed_keys[dir.as_str()].unwrap_or(0); let mut current_count = 0; @@ -78,8 +78,10 @@ impl SQLRelayer { // assert_eq!(last_count as u64, res.seq_number.unwrap()); // ComputeRequest - let (request_key, request_tx_with_hash) = - self.get_tx_with_hash(consts::COMPUTE_REQUEST, res.compute_request_tx_hash.clone()); + let (request_key, request_tx_with_hash) = self.get_tx_with_hash( + consts::COMPUTE_REQUEST, + res.compute_request_tx_hash().clone(), + ); current_count += 1; @@ -95,7 +97,7 @@ impl SQLRelayer { // ComputeCommitment let (commitment_key, commitment_tx_with_hash) = self.get_tx_with_hash( consts::COMPUTE_COMMITMENT, - res.compute_commitment_tx_hash.clone(), + res.compute_commitment_tx_hash().clone(), ); current_count += 1; @@ -110,7 +112,7 @@ impl SQLRelayer { } // ComputeVerification - for verification_tx_hash in res.compute_verification_tx_hashes.clone() { + for verification_tx_hash in res.compute_verification_tx_hashes().clone() { current_count += 1; let (verification_key, verification_tx_with_hash) = diff --git a/relayer/src/main.rs b/relayer/src/main.rs index 3edd6f32..43726a75 100644 --- a/relayer/src/main.rs +++ b/relayer/src/main.rs @@ -1,5 +1,6 @@ use api::server::serve; use dotenv::dotenv; +use getset::Getters; use openrank_common::{config, db}; use openrank_relayer::{self, SQLRelayer}; use serde::{Deserialize, Serialize}; @@ -8,10 +9,11 @@ use std::error::Error; pub mod api; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the Relayer. pub struct Config { - pub database: db::Config, + database: db::Config, } #[tokio::main] diff --git a/sequencer/Cargo.toml b/sequencer/Cargo.toml index 99950f67..825be803 100644 --- a/sequencer/Cargo.toml +++ b/sequencer/Cargo.toml @@ -32,3 +32,4 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } jsonrpsee = { workspace = true, features = ["server", "macros"] } toml = { workspace = true } +getset = { workspace = true } diff --git a/sequencer/src/lib.rs b/sequencer/src/lib.rs index 096b4e39..c421cc93 100644 --- a/sequencer/src/lib.rs +++ b/sequencer/src/lib.rs @@ -1,4 +1,5 @@ use futures::StreamExt; +use getset::Getters; use jsonrpsee::{server::Server, RpcModule}; use libp2p::{gossipsub, mdns, swarm::SwarmEvent, Swarm}; use openrank_common::{ @@ -20,21 +21,23 @@ use tracing::{error, info}; mod rpc; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The whitelist for the Sequencer. -pub struct Whitelist { +struct Whitelist { /// The list of addresses that are allowed to call the Sequencer. - pub users: Vec
, + users: Vec
, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the Sequencer. -pub struct Config { +struct Config { /// The whitelist for the Sequencer. - pub whitelist: Whitelist, - pub database: db::Config, - pub p2p: net::Config, - pub rpc: net::RpcConfig, + whitelist: Whitelist, + database: db::Config, + p2p: net::Config, + rpc: net::RpcConfig, } /// The Sequencer node. It contains the Swarm, the Server, and the Receiver. @@ -62,7 +65,7 @@ impl Node { let seq_server = SequencerServer::new(sender, config.whitelist.users.clone(), db); let rpc = seq_server.into_rpc(); - let swarm = build_node(net::load_keypair(&config.p2p.keypair, &config_loader)?).await?; + let swarm = build_node(net::load_keypair(config.p2p().keypair(), &config_loader)?).await?; info!("PEER_ID: {:?}", swarm.local_peer_id()); Ok(Self { swarm, config, rpc, receiver }) @@ -74,8 +77,8 @@ impl Node { /// - Handle gossipsub events /// - Handle mDNS events pub async fn run(&mut self) -> Result<(), Box> { - net::listen_on(&mut self.swarm, &self.config.p2p.listen_on)?; - let server = Server::builder().build(self.config.rpc.address).await?; + net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; + let server = Server::builder().build(self.config.rpc().address()).await?; let handle = server.start(self.rpc.clone()); tokio::spawn(handle.stopped()); diff --git a/sequencer/src/rpc.rs b/sequencer/src/rpc.rs index d2808645..8caf8182 100644 --- a/sequencer/src/rpc.rs +++ b/sequencer/src/rpc.rs @@ -113,7 +113,7 @@ impl RpcServer for SequencerServer { let tx_event = TxEvent::default_with_data(tx_bytes); let channel_message = ( encode(tx_event.clone()), - Topic::NamespaceTrustUpdate(trust_update.trust_id), + Topic::NamespaceTrustUpdate(trust_update.trust_id().clone()), ); self.sender.send(channel_message).await.map_err(|e| { error!("{}", e); @@ -136,7 +136,7 @@ impl RpcServer for SequencerServer { let tx_event = TxEvent::default_with_data(tx_bytes); let channel_message = ( encode(tx_event.clone()), - Topic::NamespaceSeedUpdate(seed_update.seed_id), + Topic::NamespaceSeedUpdate(seed_update.seed_id().clone()), ); self.sender.send(channel_message).await.map_err(|e| { error!("{}", e); @@ -159,7 +159,7 @@ impl RpcServer for SequencerServer { let tx_event = TxEvent::default_with_data(tx_bytes); let channel_message = ( encode(tx_event.clone()), - Topic::DomainRequest(compute_request.domain_id), + Topic::DomainRequest(*compute_request.domain_id()), ); self.sender.send(channel_message).await.map_err(|e| { error!("{}", e); @@ -180,20 +180,20 @@ impl RpcServer for SequencerServer { let result_reference = self .db - .get::(query.request_tx_hash.to_bytes()) + .get::(query.request_tx_hash().to_bytes()) .map_err(|e| { error!("{}", e); ErrorObjectOwned::from(ErrorCode::InternalError) })?; - let key = compute::Result::construct_full_key(result_reference.seq_number); + let key = compute::Result::construct_full_key(*result_reference.seq_number()); let result = self.db.get::(key).map_err(|e| { error!("{}", e); ErrorObjectOwned::from(ErrorCode::InternalError) })?; let key = Tx::construct_full_key( consts::COMPUTE_COMMITMENT, - result.compute_commitment_tx_hash, + result.compute_commitment_tx_hash().clone(), ); let tx = self.db.get::(key).map_err(|e| { error!("{}", e); @@ -205,8 +205,8 @@ impl RpcServer for SequencerServer { }?; let create_scores_tx: Vec = { let mut create_scores_tx = Vec::new(); - for tx_hash in commitment.scores_tx_hashes.into_iter() { - let key = Tx::construct_full_key(consts::COMPUTE_SCORES, tx_hash); + for tx_hash in commitment.scores_tx_hashes().iter() { + let key = Tx::construct_full_key(consts::COMPUTE_SCORES, tx_hash.clone()); let tx = self.db.get::(key).map_err(|e| { error!("{}", e); ErrorObjectOwned::from(ErrorCode::InternalError) @@ -227,13 +227,13 @@ impl RpcServer for SequencerServer { create_scores }; let mut score_entries: Vec = - create_scores.into_iter().flat_map(|x| x.entries).collect(); - score_entries.sort_by(|a, b| match a.value.partial_cmp(&b.value) { + create_scores.into_iter().flat_map(|x| x.entries().clone()).collect(); + score_entries.sort_by(|a, b| match a.value().partial_cmp(b.value()) { Some(ordering) => ordering, None => { - if a.value.is_nan() && b.value.is_nan() { + if a.value().is_nan() && b.value().is_nan() { Ordering::Equal - } else if a.value.is_nan() { + } else if a.value().is_nan() { Ordering::Greater } else { Ordering::Less @@ -242,16 +242,16 @@ impl RpcServer for SequencerServer { }); score_entries.reverse(); let score_entries: Vec = score_entries - .split_at(query.start as usize) + .split_at(*query.start() as usize) .1 .iter() - .take(query.size as usize) + .take(*query.size() as usize) .cloned() .collect(); let verificarion_results_tx: Vec = { let mut verification_resutls_tx = Vec::new(); - for tx_hash in result.compute_verification_tx_hashes.iter() { + for tx_hash in result.compute_verification_tx_hashes().iter() { let key = Tx::construct_full_key(consts::COMPUTE_VERIFICATION, tx_hash.clone()); let tx = self.db.get::(key).map_err(|e| { error!("{}", e); @@ -273,7 +273,7 @@ impl RpcServer for SequencerServer { verification_results }; let verification_results_bools: Vec = - verification_results.into_iter().map(|x| x.verification_result).collect(); + verification_results.into_iter().map(|x| *x.verification_result()).collect(); Ok((verification_results_bools, score_entries)) } diff --git a/verifier/Cargo.toml b/verifier/Cargo.toml index 2c56b190..ce7601e9 100644 --- a/verifier/Cargo.toml +++ b/verifier/Cargo.toml @@ -34,3 +34,4 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } sha3 = { workspace = true } k256 = { workspace = true } +getset = { workspace = true } diff --git a/verifier/src/lib.rs b/verifier/src/lib.rs index 6e344706..db4970ae 100644 --- a/verifier/src/lib.rs +++ b/verifier/src/lib.rs @@ -1,6 +1,7 @@ use alloy_rlp::Decodable; use dotenv::dotenv; use futures::StreamExt; +use getset::Getters; use k256::ecdsa; use k256::ecdsa::SigningKey; use libp2p::{gossipsub, mdns, swarm::SwarmEvent, Swarm}; @@ -64,24 +65,26 @@ impl From for Error { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The whitelist for the Verifier. -pub struct Whitelist { +struct Whitelist { /// The list of addresses that are allowed to be block builders. block_builder: Vec
, /// The list of addresses that are allowed to be computers. computer: Vec
, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the Verifier. -pub struct Config { +struct Config { /// The list of domains to perform verification for. - pub domains: Vec, + domains: Vec, /// The whitelist for the Verifier. - pub whitelist: Whitelist, - pub database: db::Config, - pub p2p: net::Config, + whitelist: Whitelist, + database: db::Config, + p2p: net::Config, } /// The Verifier node. It contains the Swarm, the Config, the DB, the VerificationRunner, and the SecretKey. @@ -113,7 +116,7 @@ impl Node { let domain_hashes = config.domains.iter().map(|x| x.to_hash()).collect(); let verification_runner = VerificationRunner::new(domain_hashes); - let swarm = build_node(net::load_keypair(&config.p2p.keypair, &config_loader)?).await?; + let swarm = build_node(net::load_keypair(config.p2p().keypair(), &config_loader)?).await?; info!("PEER_ID: {:?}", swarm.local_peer_id()); Ok(Self { swarm, config, db, verification_runner, secret_key }) @@ -142,13 +145,13 @@ impl Node { // Add Tx to db tx.set_sequence_number(message.sequence_number.unwrap_or_default()); self.db.put(tx.clone()).map_err(Error::Db)?; - assert!(*namespace == trust_update.trust_id); + assert!(namespace == trust_update.trust_id()); let domain = domains .iter() .find(|x| &x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.verification_runner - .update_trust(domain.clone(), trust_update.entries.clone()) + .update_trust(domain.clone(), trust_update.entries().clone()) .map_err(Error::Runner)?; info!( "TOPIC: {}, ID: {message_id}, FROM: {propagation_source}", @@ -167,13 +170,13 @@ impl Node { tx.verify_against(namespace.owner()).map_err(Error::Signature)?; // Add Tx to db self.db.put(tx.clone()).map_err(Error::Db)?; - assert!(*namespace == seed_update.seed_id); + assert!(namespace == seed_update.seed_id()); let domain = domains .iter() .find(|x| &x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.verification_runner - .update_seed(domain.clone(), seed_update.entries.clone()) + .update_seed(domain.clone(), seed_update.entries().clone()) .map_err(Error::Runner)?; info!( "TOPIC: {}, ID: {message_id}, FROM: {propagation_source}", @@ -194,12 +197,15 @@ impl Node { // Add Tx to db self.db.put(tx.clone()).map_err(Error::Db)?; let computer_address = address_from_sk(&self.secret_key); - assert_eq!(computer_address, compute_assignment.assigned_verifier_node); + assert_eq!( + computer_address, + *compute_assignment.assigned_verifier_node() + ); assert!(self .config .whitelist .computer - .contains(&compute_assignment.assigned_compute_node)); + .contains(compute_assignment.assigned_compute_node())); let domain = domains .iter() @@ -351,7 +357,7 @@ impl Node { for tx in txs { match tx.body() { tx::Body::TrustUpdate(trust_update) => { - let namespace = trust_update.trust_id.clone(); + let namespace = trust_update.trust_id().clone(); let domain = self .config .domains @@ -359,11 +365,11 @@ impl Node { .find(|x| x.trust_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.verification_runner - .update_trust(domain.clone(), trust_update.entries.clone()) + .update_trust(domain.clone(), trust_update.entries().clone()) .map_err(Error::Runner)?; }, tx::Body::SeedUpdate(seed_update) => { - let namespace = seed_update.seed_id.clone(); + let namespace = seed_update.seed_id().clone(); let domain = self .config .domains @@ -371,7 +377,7 @@ impl Node { .find(|x| x.seed_namespace() == namespace) .ok_or(Error::DomainNotFound(namespace.clone().to_hex()))?; self.verification_runner - .update_seed(domain.clone(), seed_update.entries.clone()) + .update_seed(domain.clone(), seed_update.entries().clone()) .map_err(Error::Runner)?; }, _ => (), @@ -442,7 +448,7 @@ impl Node { self.swarm.behaviour_mut().gossipsub.subscribe(&topic)?; } - net::listen_on(&mut self.swarm, &self.config.p2p.listen_on)?; + net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; // Kick it off loop { diff --git a/verifier/src/runner.rs b/verifier/src/runner.rs index d7a4e6c7..0e6b8581 100644 --- a/verifier/src/runner.rs +++ b/verifier/src/runner.rs @@ -96,29 +96,29 @@ impl VerificationRunner { .ok_or(Error::SeedTrustNotFound(domain.to_hash()))?; let default_sub_tree = DenseIncrementalMerkleTree::::new(32); for entry in trust_entries { - let from_index = if let Some(i) = domain_indices.get(&entry.from) { + let from_index = if let Some(i) = domain_indices.get(entry.from()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.from.clone(), curr_count); + domain_indices.insert(entry.from().clone(), curr_count); *count += 1; curr_count }; - let to_index = if let Some(i) = domain_indices.get(&entry.to) { + let to_index = if let Some(i) = domain_indices.get(entry.to()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.to.clone(), curr_count); + domain_indices.insert(entry.to().clone(), curr_count); *count += 1; curr_count }; - lt.insert((from_index, to_index), entry.value); + lt.insert((from_index, to_index), *entry.value()); lt_sub_trees.entry(from_index).or_insert_with(|| default_sub_tree.clone()); let sub_tree = lt_sub_trees .get_mut(&from_index) .ok_or(Error::LocalTrustSubTreesNotFoundWithIndex(from_index))?; - let leaf = hash_leaf::(entry.value.to_be_bytes().to_vec()); + let leaf = hash_leaf::(entry.value().to_be_bytes().to_vec()); sub_tree.insert_leaf(to_index, leaf); let sub_tree_root = sub_tree.root().map_err(Error::Merkle)?; @@ -154,11 +154,11 @@ impl VerificationRunner { .ok_or(Error::SeedTrustNotFound(domain.to_hash()))?; let default_sub_tree = DenseIncrementalMerkleTree::::new(32); for entry in seed_entries { - let index = if let Some(i) = domain_indices.get(&entry.id) { + let index = if let Some(i) = domain_indices.get(entry.id()) { *i } else { let curr_count = *count; - domain_indices.insert(entry.id.clone(), curr_count); + domain_indices.insert(entry.id().clone(), curr_count); *count += 1; curr_count }; @@ -168,11 +168,11 @@ impl VerificationRunner { .get_mut(&index) .ok_or(Error::LocalTrustSubTreesNotFoundWithIndex(index))?; let sub_tree_root = sub_tree.root().map_err(Error::Merkle)?; - let seed_hash = hash_leaf::(entry.value.to_be_bytes().to_vec()); + let seed_hash = hash_leaf::(entry.value().to_be_bytes().to_vec()); let leaf = hash_two::(sub_tree_root, seed_hash); lt_master_tree.insert_leaf(index, leaf); - seed.insert(index, entry.value); + seed.insert(index, *entry.value()); } Ok(()) @@ -186,8 +186,8 @@ impl VerificationRunner { .compute_scores .get(&domain.clone().to_hash()) .ok_or(Error::ComputeScoresNotFoundWithDomain(domain.to_hash()))?; - for score_tx in commitment.scores_tx_hashes { - let res = compute_scores_txs.contains_key(&score_tx); + for score_tx in commitment.scores_tx_hashes() { + let res = compute_scores_txs.contains_key(score_tx); if !res { return Ok(false); } @@ -211,8 +211,8 @@ impl VerificationRunner { self.check_scores_tx_hashes(domain.clone(), commitment.clone())?; if is_check_score_tx_hashes { let assgn_tx = assignment_id.clone(); - let lt_root = commitment.lt_root_hash.clone(); - let cp_root = commitment.compute_root_hash.clone(); + let lt_root = commitment.lt_root_hash().clone(); + let cp_root = commitment.compute_root_hash().clone(); self.create_compute_tree(domain.clone(), assignment_id.clone())?; let (res_lt_root, res_compute_root) = @@ -261,7 +261,7 @@ impl VerificationRunner { /// Add a new commitment of certain assignment pub fn update_commitment(&mut self, commitment: compute::Commitment) { - self.commitments.insert(commitment.assignment_tx_hash.clone(), commitment.clone()); + self.commitments.insert(commitment.assignment_tx_hash().clone(), commitment.clone()); } /// Build the compute tree of certain assignment, for certain domain. @@ -282,7 +282,7 @@ impl VerificationRunner { .ok_or(Error::ComputeScoresNotFoundWithDomain(domain.to_hash()))?; let scores: Vec<&compute::Scores> = { let mut scores = Vec::new(); - for tx_hash in commitment.scores_tx_hashes.iter() { + for tx_hash in commitment.scores_tx_hashes().iter() { scores.push( compute_scores .get(tx_hash) @@ -292,7 +292,7 @@ impl VerificationRunner { scores }; let score_entries: Vec = - scores.iter().flat_map(|cs| cs.entries.clone()).map(|x| x.value).collect(); + scores.iter().flat_map(|cs| cs.entries().clone()).map(|x| *x.value()).collect(); let score_hashes: Vec = score_entries .iter() .map(|&x| hash_leaf::(x.to_be_bytes().to_vec())) @@ -328,7 +328,7 @@ impl VerificationRunner { .ok_or(Error::SeedTrustNotFound(domain.to_hash()))?; let scores: Vec<&compute::Scores> = { let mut scores = Vec::new(); - for tx_hash in commitment.scores_tx_hashes.iter() { + for tx_hash in commitment.scores_tx_hashes().iter() { scores.push( compute_scores .get(tx_hash) @@ -339,14 +339,14 @@ impl VerificationRunner { }; let score_entries: HashMap = { let score_entries_vec: Vec = - scores.iter().flat_map(|cs| cs.entries.clone()).collect(); + scores.iter().flat_map(|cs| cs.entries().clone()).collect(); let mut score_entries_map: HashMap = HashMap::new(); for entry in score_entries_vec { let i = domain_indices - .get(&entry.id) - .ok_or(Error::DomainIndexNotFound(entry.id.clone()))?; - score_entries_map.insert(*i, entry.value); + .get(entry.id()) + .ok_or(Error::DomainIndexNotFound(entry.id().clone()))?; + score_entries_map.insert(*i, *entry.value()); } score_entries_map }; From 9eb8acf6fe1dc0b3974e945a0a4c13441602f404 Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:49:50 +0800 Subject: [PATCH 11/13] feat: apply getset to all of remaining structs --- block-builder/src/coordinator.rs | 3 +++ block-builder/src/lib.rs | 12 ++++++----- common/src/config.rs | 3 +++ common/src/db/items.rs | 6 +++--- common/src/db/mod.rs | 2 ++ common/src/lib.rs | 34 +++++++++++++++++++++++++++----- common/src/merkle/fixed.rs | 6 ++++-- common/src/merkle/incremental.rs | 6 ++++-- common/src/merkle/mod.rs | 6 +++++- common/src/topics.rs | 9 ++++++++- common/src/tx/block.rs | 21 ++++++++++++++++---- common/src/tx/mod.rs | 10 +++++++--- common/src/tx/trust.rs | 6 +++++- common/src/tx_event.rs | 10 ++++++++++ computer/src/lib.rs | 8 +++++--- computer/src/runner.rs | 3 +++ openrank-sdk/src/main.rs | 11 +++++++---- sequencer/src/lib.rs | 8 +++++--- sequencer/src/rpc.rs | 3 +++ verifier/src/lib.rs | 8 +++++--- verifier/src/runner.rs | 3 +++ 21 files changed, 138 insertions(+), 40 deletions(-) diff --git a/block-builder/src/coordinator.rs b/block-builder/src/coordinator.rs index 624d203a..1e97d98f 100644 --- a/block-builder/src/coordinator.rs +++ b/block-builder/src/coordinator.rs @@ -1,8 +1,11 @@ +use getset::Getters; use openrank_common::tx::compute; use std::collections::HashMap; /// Coordinator role for the OpenRank network. /// Responsible for sequencing job results. +#[derive(Default, Getters)] +#[getset(get = "pub")] pub struct JobCoordinator { /// A map of all job results. job_results: HashMap, diff --git a/block-builder/src/lib.rs b/block-builder/src/lib.rs index eb5593db..8240960a 100644 --- a/block-builder/src/lib.rs +++ b/block-builder/src/lib.rs @@ -78,6 +78,8 @@ struct Config { } /// The Block Builder node. It contains the Swarm, the Config, the DB, the SecretKey, and the ComputeRunner. +#[derive(Getters)] +#[getset(get = "pub")] pub struct Node { swarm: Swarm, config: Config, @@ -225,7 +227,7 @@ impl Node { let request: Tx = self.db.get(request_tx_key).map_err(Error::Db)?; if let Err(db::Error::NotFound) = self.db.get::( - assignment_body.request_tx_hash().0.to_vec(), + assignment_body.request_tx_hash().to_bytes(), ) { let mut result = @@ -287,7 +289,7 @@ impl Node { }; let result_reference: compute::ResultReference = self .db - .get(assignment_body.request_tx_hash().0.to_vec()) + .get(assignment_body.request_tx_hash().to_bytes()) .map_err(Error::Db)?; let compute_result_key = compute::Result::construct_full_key(*result_reference.seq_number()); @@ -388,7 +390,7 @@ impl Node { // Create a Gossipsub topic let topic = gossipsub::IdentTopic::new(topic.clone()); // subscribes to our topic - self.swarm.behaviour_mut().gossipsub.subscribe(&topic)?; + self.swarm.behaviour_mut().gossipsub_subscribe(&topic)?; } // Kick it off @@ -398,13 +400,13 @@ impl Node { SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discovered a new peer: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_add_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discover peer has expired: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_remove_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(event)) => { diff --git a/common/src/config.rs b/common/src/config.rs index 59e4d4cc..938e19d4 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -1,5 +1,6 @@ //! The config module provides configuration-loading mechanism for OpenRank programs. +use getset::Getters; use serde::de::DeserializeOwned; use std::io::ErrorKind; use std::path::{Path, PathBuf}; @@ -36,6 +37,8 @@ pub enum Error { /// // loads ~/.config/openrank-computer/x.toml /// let config: MyConfig = loader.load_named("x").unwrap(); /// ``` +#[derive(Getters)] +#[getset(get = "pub")] pub struct Loader { program_name: String, config_dir: PathBuf, diff --git a/common/src/db/items.rs b/common/src/db/items.rs index dd781214..a58c7918 100644 --- a/common/src/db/items.rs +++ b/common/src/db/items.rs @@ -30,7 +30,7 @@ impl DbItem for TxEvent { impl DbItem for Result { fn get_key(&self) -> Vec { - self.compute_request_tx_hash.0.to_vec() + self.compute_request_tx_hash().to_bytes() } fn get_cf() -> String { @@ -44,7 +44,7 @@ impl DbItem for Result { impl DbItem for ResultReference { fn get_key(&self) -> Vec { - self.compute_request_tx_hash.0.to_vec() + self.compute_request_tx_hash().to_bytes() } fn get_prefix(&self) -> String { @@ -58,7 +58,7 @@ impl DbItem for ResultReference { impl DbItem for Tx { fn get_key(&self) -> Vec { - self.hash().0.to_vec() + self.hash().to_bytes() } fn get_cf() -> String { diff --git a/common/src/db/mod.rs b/common/src/db/mod.rs index cc240aba..459fcb0d 100644 --- a/common/src/db/mod.rs +++ b/common/src/db/mod.rs @@ -60,6 +60,8 @@ impl Config { } } +#[derive(Getters)] +#[getset(get = "pub")] /// Wrapper for database connection. pub struct Db { connection: DB, diff --git a/common/src/lib.rs b/common/src/lib.rs index ce2a949b..f5f48ac1 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -11,9 +11,10 @@ pub mod tx_event; pub mod db; use alloy_rlp::encode; +use getset::Getters; use k256::ecdsa::SigningKey; use libp2p::{ - gossipsub::{self, MessageId, PublishError}, + gossipsub::{self, MessageId, PublishError, SubscriptionError}, identity, mdns, noise, swarm::NetworkBehaviour, tcp, yamux, Swarm, @@ -26,11 +27,34 @@ use tracing::info; use tx::{Address, Tx}; use tx_event::TxEvent; -#[derive(NetworkBehaviour)] +#[derive(NetworkBehaviour, Getters)] +#[getset(get = "pub")] /// A custom libp2p [network behavior](libp2p::swarm::NetworkBehaviour) used by OpenRank nodes. pub struct MyBehaviour { - pub gossipsub: gossipsub::Behaviour, - pub mdns: mdns::tokio::Behaviour, + gossipsub: gossipsub::Behaviour, + mdns: mdns::tokio::Behaviour, +} + +impl MyBehaviour { + pub fn gossipsub_subscribe( + &mut self, topic: &gossipsub::IdentTopic, + ) -> Result { + self.gossipsub.subscribe(topic) + } + + pub fn gossipsub_publish( + &mut self, topic: gossipsub::IdentTopic, data: Vec, + ) -> Result { + self.gossipsub.publish(topic, data) + } + + pub fn gossipsub_add_peer(&mut self, peer_id: &libp2p::PeerId) { + self.gossipsub.add_explicit_peer(peer_id); + } + + pub fn gossipsub_remove_peer(&mut self, peer_id: &libp2p::PeerId) { + self.gossipsub.remove_explicit_peer(peer_id); + } } /// Builds a libp2p swarm with the custom behaviour. @@ -108,7 +132,7 @@ pub fn address_from_sk(sk: &SigningKey) -> Address { let hash = hash_leaf::(vk_bytes[1..].to_vec()); let mut address_bytes = [0u8; 20]; - address_bytes.copy_from_slice(&hash.0[12..]); + address_bytes.copy_from_slice(&hash.inner()[12..]); Address::from_slice(&address_bytes) } diff --git a/common/src/merkle/fixed.rs b/common/src/merkle/fixed.rs index 0f1de748..1feb87e6 100644 --- a/common/src/merkle/fixed.rs +++ b/common/src/merkle/fixed.rs @@ -1,8 +1,10 @@ use crate::merkle::{self, hash_two, Hash}; +use getset::Getters; use sha3::Digest; use std::{collections::HashMap, marker::PhantomData}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Getters)] +#[getset(get = "pub")] /// Dense Merkle tree. /// The dense tree is a tree where leaf nodes are compressed to be next to each other /// which makes it more efficient to store and traverse. @@ -13,7 +15,7 @@ where H: Digest, { /// HashMap to keep the level and index of the nodes. - pub(crate) nodes: HashMap>, + nodes: HashMap>, // Number of levels num_levels: u8, /// PhantomData for the hasher diff --git a/common/src/merkle/incremental.rs b/common/src/merkle/incremental.rs index c9b20cf7..934b6f46 100644 --- a/common/src/merkle/incremental.rs +++ b/common/src/merkle/incremental.rs @@ -1,8 +1,10 @@ use crate::merkle::{self, hash_two, next_index, num_to_bits_vec, Hash}; +use getset::Getters; use sha3::Digest; use std::{collections::HashMap, marker::PhantomData}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Getters)] +#[getset(get = "pub")] /// Dense incremental Merkle tree. /// The dense tree is a tree where leaf nodes are compressed to be next to each other /// which makes it more efficient to store and traverse. @@ -12,7 +14,7 @@ where H: Digest, { /// HashMap to keep the level and index of the nodes. - pub(crate) nodes: HashMap<(u8, u64), Hash>, + nodes: HashMap<(u8, u64), Hash>, /// Default nodes. default: HashMap<(u8, u64), Hash>, /// Number of levels. diff --git a/common/src/merkle/mod.rs b/common/src/merkle/mod.rs index 14cd2ecd..213e58f8 100644 --- a/common/src/merkle/mod.rs +++ b/common/src/merkle/mod.rs @@ -14,13 +14,17 @@ pub mod incremental; Debug, Clone, Default, PartialEq, Eq, RlpDecodable, RlpEncodable, Serialize, Deserialize, )] /// Used to represent a hash of a node in the merkle tree. -pub struct Hash(#[serde(with = "hex")] pub [u8; 32]); +pub struct Hash(#[serde(with = "hex")] [u8; 32]); impl Hash { /// Converts the hash to a hex string. pub fn to_hex(self) -> String { hex::encode(self.0) } + + pub fn inner(&self) -> &[u8; 32] { + &self.0 + } } #[cfg(test)] diff --git a/common/src/topics.rs b/common/src/topics.rs index ed5af2ea..436664cc 100644 --- a/common/src/topics.rs +++ b/common/src/topics.rs @@ -1,5 +1,6 @@ use crate::tx::{consts, trust::OwnedNamespace, Address}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use hex::FromHex; use serde::{Deserialize, Serialize}; use std::{ @@ -28,6 +29,11 @@ impl DomainHash { pub fn to_hex(self) -> String { hex::encode(self.0.to_be_bytes()) } + + /// Get the inner value of the hash. + pub fn inner(self) -> u64 { + self.0 + } } impl FromHex for DomainHash { @@ -45,7 +51,8 @@ impl From for DomainHash { } } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// Domain of the openrank network. Consists of a trust namespace and a seed namespace + algorithm id. pub struct Domain { /// Address of the trust namespace owner. diff --git a/common/src/tx/block.rs b/common/src/tx/block.rs index d0e933ba..2d66bed8 100644 --- a/common/src/tx/block.rs +++ b/common/src/tx/block.rs @@ -1,22 +1,32 @@ use crate::tx::TxHash; use crate::{merkle::Hash, topics::DomainHash}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; +use getset::Getters; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] struct PendingDomainUpdate { domain_id: DomainHash, commitment_tx_hash: TxHash, } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] struct DomainUpdate { domain_id: DomainHash, commitment_tx_hash: TxHash, verification_results_tx_hashes: Vec, } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct ProposedBlock { previous_block_hash: TxHash, state_root: Hash, @@ -25,7 +35,10 @@ pub struct ProposedBlock { block_height: u64, } -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable)] +#[derive( + Debug, Clone, Default, PartialEq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Getters, +)] +#[getset(get = "pub")] pub struct FinalisedBlock { previous_block_hash: TxHash, state_root: Hash, diff --git a/common/src/tx/mod.rs b/common/src/tx/mod.rs index 575bca58..b932f388 100644 --- a/common/src/tx/mod.rs +++ b/common/src/tx/mod.rs @@ -169,7 +169,7 @@ impl Tx { let hash = hash_leaf::(vk_bytes[1..].to_vec()); let mut address_bytes = [0u8; 20]; - address_bytes.copy_from_slice(&hash.0[12..]); + address_bytes.copy_from_slice(&hash.inner()[12..]); if Address::from_slice(&address_bytes) != address { return Err(EcdsaError::new()); @@ -194,7 +194,7 @@ impl Tx { let hash = hash_leaf::(vk_bytes[1..].to_vec()); let mut address_bytes = [0u8; 20]; - address_bytes.copy_from_slice(&hash.0[12..]); + address_bytes.copy_from_slice(&hash.inner()[12..]); let address = Address::from_slice(&address_bytes); Ok(address) @@ -214,7 +214,7 @@ pub type Address = alloy_primitives::Address; #[derive( Debug, Clone, Hash, PartialEq, Eq, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize, )] -pub struct TxHash(#[serde(with = "hex")] pub [u8; 32]); +pub struct TxHash(#[serde(with = "hex")] [u8; 32]); impl TxHash { pub fn from_bytes(bytes: Vec) -> Self { @@ -234,6 +234,10 @@ impl TxHash { pub fn to_hex(self) -> String { hex::encode(self.0) } + + pub fn inner(&self) -> &[u8; 32] { + &self.0 + } } #[derive( diff --git a/common/src/tx/trust.rs b/common/src/tx/trust.rs index a2e8ef3d..71bb0ad3 100644 --- a/common/src/tx/trust.rs +++ b/common/src/tx/trust.rs @@ -11,7 +11,7 @@ use std::io::Read; #[derive( Debug, Clone, Hash, Default, PartialEq, Eq, RlpDecodable, RlpEncodable, Serialize, Deserialize, )] -pub struct OwnedNamespace(#[serde(with = "hex")] pub [u8; 24]); +pub struct OwnedNamespace(#[serde(with = "hex")] [u8; 24]); impl OwnedNamespace { pub fn new(owner: Address, id: u32) -> Self { @@ -30,6 +30,10 @@ impl OwnedNamespace { bytes.copy_from_slice(&self.0[..20]); Address::from_slice(&bytes) } + + pub fn inner(&self) -> &[u8; 24] { + &self.0 + } } impl FromHex for OwnedNamespace { diff --git a/common/src/tx_event.rs b/common/src/tx_event.rs index e6b0c45a..19cbe714 100644 --- a/common/src/tx_event.rs +++ b/common/src/tx_event.rs @@ -6,6 +6,16 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Default, RlpDecodable, RlpEncodable, Serialize, Deserialize)] pub struct InclusionProof([u8; 32]); +impl InclusionProof { + pub fn new(proof: [u8; 32]) -> Self { + Self(proof) + } + + pub fn inner(&self) -> &[u8; 32] { + &self.0 + } +} + /// Transaction event which includes proof of inclusion and custom data. #[derive(Debug, Clone, RlpDecodable, RlpEncodable, Serialize, Deserialize, Getters)] #[getset(get = "pub")] diff --git a/computer/src/lib.rs b/computer/src/lib.rs index 832c29b2..aeaea255 100644 --- a/computer/src/lib.rs +++ b/computer/src/lib.rs @@ -79,6 +79,8 @@ struct Config { p2p: net::Config, } +#[derive(Getters)] +#[getset(get = "pub")] pub struct Node { swarm: Swarm, config: Config, @@ -312,7 +314,7 @@ impl Node { // Create a Gossipsub topic let topic = gossipsub::IdentTopic::new(topic.clone()); // subscribes to our topic - self.swarm.behaviour_mut().gossipsub.subscribe(&topic)?; + self.swarm.behaviour_mut().gossipsub_subscribe(&topic)?; } net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; @@ -324,13 +326,13 @@ impl Node { SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discovered a new peer: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_add_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discover peer has expired: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_remove_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(event)) => { diff --git a/computer/src/runner.rs b/computer/src/runner.rs index bd068c28..84253ef9 100644 --- a/computer/src/runner.rs +++ b/computer/src/runner.rs @@ -1,3 +1,4 @@ +use getset::Getters; use openrank_common::{ algos::{self, et::positive_run}, merkle::{ @@ -16,6 +17,8 @@ use std::{ fmt::{Display, Formatter, Result as FmtResult}, }; +#[derive(Getters)] +#[getset(get = "pub")] /// Struct containing the state of the computer compute runner. pub struct ComputeRunner { count: HashMap, diff --git a/openrank-sdk/src/main.rs b/openrank-sdk/src/main.rs index bbfcd77d..5b1f4598 100644 --- a/openrank-sdk/src/main.rs +++ b/openrank-sdk/src/main.rs @@ -66,7 +66,8 @@ struct Args { method: Method, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] /// The configuration for the Sequencer. pub struct Sequencer { endpoint: String, @@ -83,13 +84,15 @@ pub struct Config { sequencer: Sequencer, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] struct ComputeRequestResult { compute_tx_hash: TxHash, tx_event: TxEvent, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Getters)] +#[getset(get = "pub")] struct ComputeResults { votes: Vec, scores: Vec, @@ -353,7 +356,7 @@ async fn main() -> Result<(), Box> { Method::ComputeRequest { path, output_path } => { let secret_key = get_secret_key()?; let res = compute_request(secret_key, path.as_str()).await?; - let hex_encoded_tx_hash = hex::encode(res.compute_tx_hash.0); + let hex_encoded_tx_hash = hex::encode(res.compute_tx_hash.inner()); println!("{}", hex_encoded_tx_hash); if let Some(output_path) = output_path { write_json_to_file(&output_path, res)?; diff --git a/sequencer/src/lib.rs b/sequencer/src/lib.rs index c421cc93..547c6a74 100644 --- a/sequencer/src/lib.rs +++ b/sequencer/src/lib.rs @@ -40,6 +40,8 @@ struct Config { rpc: net::RpcConfig, } +#[derive(Getters)] +#[getset(get = "pub")] /// The Sequencer node. It contains the Swarm, the Server, and the Receiver. pub struct Node { config: Config, @@ -90,7 +92,7 @@ impl Node { let topic_wrapper = gossipsub::IdentTopic::new(topic.clone()); info!("PUBLISH: {:?}", topic.clone()); if let Err(e) = - self.swarm.behaviour_mut().gossipsub.publish(topic_wrapper, data) + self.swarm.behaviour_mut().gossipsub_publish(topic_wrapper, data) { error!("Publish error: {e:?}"); } @@ -100,13 +102,13 @@ impl Node { SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discovered a new peer: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_add_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discover peer has expired: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_remove_peer(&peer_id); } }, SwarmEvent::NewListenAddr { address, .. } => { diff --git a/sequencer/src/rpc.rs b/sequencer/src/rpc.rs index 8caf8182..811cc6eb 100644 --- a/sequencer/src/rpc.rs +++ b/sequencer/src/rpc.rs @@ -1,4 +1,5 @@ use alloy_rlp::{encode, Decodable}; +use getset::Getters; use jsonrpsee::core::async_trait; use jsonrpsee::proc_macros::rpc; use jsonrpsee::types::error::{INVALID_REQUEST_CODE, PARSE_ERROR_CODE}; @@ -40,6 +41,8 @@ pub trait Rpc { async fn get_txs(&self, keys: Vec<(String, tx::TxHash)>) -> Result, ErrorObjectOwned>; } +#[derive(Getters)] +#[getset(get = "pub")] /// The Sequencer JsonRPC server. It contains the sender, the whitelisted users, and the database connection. pub struct SequencerServer { sender: Sender<(Vec, Topic)>, diff --git a/verifier/src/lib.rs b/verifier/src/lib.rs index db4970ae..822c6345 100644 --- a/verifier/src/lib.rs +++ b/verifier/src/lib.rs @@ -87,6 +87,8 @@ struct Config { p2p: net::Config, } +#[derive(Getters)] +#[getset(get = "pub")] /// The Verifier node. It contains the Swarm, the Config, the DB, the VerificationRunner, and the SecretKey. pub struct Node { swarm: Swarm, @@ -445,7 +447,7 @@ impl Node { // Create a Gossipsub topic let topic = gossipsub::IdentTopic::new(topic.clone()); // subscribes to our topic - self.swarm.behaviour_mut().gossipsub.subscribe(&topic)?; + self.swarm.behaviour_mut().gossipsub_subscribe(&topic)?; } net::listen_on(&mut self.swarm, self.config.p2p().listen_on())?; @@ -457,13 +459,13 @@ impl Node { SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discovered a new peer: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_add_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => { for (peer_id, _multiaddr) in list { info!("mDNS discover peer has expired: {peer_id}"); - self.swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id); + self.swarm.behaviour_mut().gossipsub_remove_peer(&peer_id); } }, SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(event)) => { diff --git a/verifier/src/runner.rs b/verifier/src/runner.rs index 0e6b8581..db1d84c6 100644 --- a/verifier/src/runner.rs +++ b/verifier/src/runner.rs @@ -1,3 +1,4 @@ +use getset::Getters; use openrank_common::{ algos::{self, et::convergence_check}, merkle::{ @@ -17,6 +18,8 @@ use std::{ fmt::{Display, Formatter, Result as FmtResult}, }; +#[derive(Getters)] +#[getset(get = "pub")] /// Struct containing the state of the verification runner pub struct VerificationRunner { count: HashMap, From 66b6b9d4249b03e2a1b6286f3513a69ac8dafd35 Mon Sep 17 00:00:00 2001 From: Filip Lazovic Date: Thu, 7 Nov 2024 15:27:57 +0100 Subject: [PATCH 12/13] Remove getset in the relayer --- Cargo.lock | 1 - relayer/Cargo.toml | 1 - relayer/src/main.rs | 4 +--- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f333eaa9..c1151975 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4300,7 +4300,6 @@ dependencies = [ "dotenv", "env_logger", "futures", - "getset", "k256", "libp2p", "log", diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index 05f8d354..2fd9c3b9 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -43,4 +43,3 @@ async-graphql = "5.0" # or juniper for GraphQL? sqlx = { version = "0.6", features = ["postgres", "runtime-tokio-native-tls"] } async-graphql-warp = "5.0" base64 = "0.22.1" -getset = { workspace = true } diff --git a/relayer/src/main.rs b/relayer/src/main.rs index 43726a75..8b04bf18 100644 --- a/relayer/src/main.rs +++ b/relayer/src/main.rs @@ -1,6 +1,5 @@ use api::server::serve; use dotenv::dotenv; -use getset::Getters; use openrank_common::{config, db}; use openrank_relayer::{self, SQLRelayer}; use serde::{Deserialize, Serialize}; @@ -9,8 +8,7 @@ use std::error::Error; pub mod api; -#[derive(Debug, Clone, Serialize, Deserialize, Getters)] -#[getset(get = "pub")] +#[derive(Debug, Clone, Serialize, Deserialize)] /// The configuration for the Relayer. pub struct Config { database: db::Config, From a112857575397a2882b06589d2c161bd428b9824 Mon Sep 17 00:00:00 2001 From: Filip Lazovic Date: Thu, 7 Nov 2024 16:02:19 +0100 Subject: [PATCH 13/13] Fixed key for compute::Result --- common/src/db/items.rs | 2 +- common/src/tx/compute.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/common/src/db/items.rs b/common/src/db/items.rs index a58c7918..fe5a7f20 100644 --- a/common/src/db/items.rs +++ b/common/src/db/items.rs @@ -30,7 +30,7 @@ impl DbItem for TxEvent { impl DbItem for Result { fn get_key(&self) -> Vec { - self.compute_request_tx_hash().to_bytes() + self.get_seq_number().to_be_bytes().to_vec() } fn get_cf() -> String { diff --git a/common/src/tx/compute.rs b/common/src/tx/compute.rs index b28a971b..4efe97d3 100644 --- a/common/src/tx/compute.rs +++ b/common/src/tx/compute.rs @@ -131,6 +131,11 @@ impl Result { self.seq_number = Some(seq_number); } + /// Get sequence number + pub fn get_seq_number(&self) -> u64 { + self.seq_number.unwrap() + } + /// Append verification tx hash pub fn append_verification_tx_hash(&mut self, tx_hash: TxHash) { self.compute_verification_tx_hashes.push(tx_hash);