Skip to content

Commit

Permalink
Merge pull request #132 from openrankprotocol/use-getset-lib
Browse files Browse the repository at this point in the history
feat: use `getset` lib for deriving getters and setters
  • Loading branch information
lazovicff authored Nov 7, 2024
2 parents 396c63b + a112857 commit e264ca7
Show file tree
Hide file tree
Showing 34 changed files with 463 additions and 334 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ k256 = "0.13.3"
directories = "5.0.1"
thiserror = "1.0.63"
clap = "4.5.9"
getset = "0.1.3"
1 change: 1 addition & 0 deletions block-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ toml = { workspace = true }
dotenv = { workspace = true }
k256 = { workspace = true }
clap = { workspace = true, features = ["derive"] }
getset = { workspace = true }
7 changes: 5 additions & 2 deletions block-builder/src/coordinator.rs
Original file line number Diff line number Diff line change
@@ -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<u64, compute::Result>,
Expand All @@ -20,11 +23,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;
Expand Down
57 changes: 31 additions & 26 deletions block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -52,29 +53,33 @@ 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<Address>,
computer: Vec<Address>,
/// The list of addresses that are allowed to be verifiers.
pub verifier: Vec<Address>,
verifier: Vec<Address>,
/// The list of addresses that are allowed to broadcast transactions.
pub users: Vec<Address>,
users: Vec<Address>,
}

#[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<Domain>,
domains: Vec<Domain>,
/// 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.
#[derive(Getters)]
#[getset(get = "pub")]
pub struct Node {
swarm: Swarm<MyBehaviour>,
config: Config,
Expand Down Expand Up @@ -105,7 +110,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();
Expand Down Expand Up @@ -172,7 +177,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];
Expand Down Expand Up @@ -207,7 +212,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)?;
Expand All @@ -217,21 +222,21 @@ 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::<compute::ResultReference>(
assignment_body.request_tx_hash.0.to_vec(),
assignment_body.request_tx_hash().to_bytes(),
)
{
let mut result =
compute::Result::new(tx.hash(), Vec::new(), request.hash());
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,
result.seq_number.unwrap(),
assignment_body.request_tx_hash().clone(),
result.seq_number().unwrap(),
);
self.db.put(reference).map_err(Error::Db)?;
}
Expand Down Expand Up @@ -274,7 +279,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)?;
Expand All @@ -284,13 +289,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().to_bytes())
.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!(
Expand Down Expand Up @@ -325,7 +330,7 @@ impl Node {
/// - Handles gossipsub events.
/// - Handles mDNS events.
pub async fn run(&mut self) -> Result<(), Box<dyn StdError>> {
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<Topic> = self
.config
Expand Down Expand Up @@ -385,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
Expand All @@ -395,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)) => {
Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 }
Expand Down
3 changes: 3 additions & 0 deletions common/src/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions common/src/db/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl DbItem for TxEvent {

impl DbItem for Result {
fn get_key(&self) -> Vec<u8> {
self.compute_request_tx_hash.0.to_vec()
self.get_seq_number().to_be_bytes().to_vec()
}

fn get_cf() -> String {
Expand All @@ -44,7 +44,7 @@ impl DbItem for Result {

impl DbItem for ResultReference {
fn get_key(&self) -> Vec<u8> {
self.compute_request_tx_hash.0.to_vec()
self.compute_request_tx_hash().to_bytes()
}

fn get_prefix(&self) -> String {
Expand All @@ -58,7 +58,7 @@ impl DbItem for ResultReference {

impl DbItem for Tx {
fn get_key(&self) -> Vec<u8> {
self.hash().0.to_vec()
self.hash().to_bytes()
}

fn get_cf() -> String {
Expand Down
10 changes: 7 additions & 3 deletions common/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use getset::Getters;
use rocksdb::{self, Options, DB};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{self, to_vec};
Expand Down Expand Up @@ -46,10 +47,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<String>,
directory: String,
secondary: Option<String>,
}

impl Config {
Expand All @@ -58,6 +60,8 @@ impl Config {
}
}

#[derive(Getters)]
#[getset(get = "pub")]
/// Wrapper for database connection.
pub struct Db {
connection: DB,
Expand Down
Loading

0 comments on commit e264ca7

Please sign in to comment.