Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI

on:
pull_request:
branches:
- main
push:
branches:
- main

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
checks:
name: Rust checks
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- name: Cache cargo registry and build artifacts
uses: Swatinem/rust-cache@v2

- name: cargo fmt
run: cargo fmt --all -- --check

- name: cargo check
run: cargo check --all-targets

- name: cargo clippy
run: cargo clippy --all-targets -- -D warnings

- name: cargo build
run: cargo build --all-targets

- name: cargo test
run: cargo test --all-targets
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ path = "src/lib.rs"
[[bin]]
name = "cube"
path = "src/operative/main.rs"

[lints.clippy]
module_inception = "allow"
result_large_err = "allow"
too_many_arguments = "allow"
wrong_self_convention = "allow"
large_enum_variant = "allow"
manual_div_ceil = "allow"
assign_op_pattern = "allow"
manual_async_fn = "allow"
8 changes: 3 additions & 5 deletions src/communicative/nns/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ impl NNSClient {
.send_event_builder(note_publish_event)
.await
{
Ok(ok) => {
return Some(ok.as_bytes().to_owned());
}
Err(_) => return None,
};
Ok(ok) => Some(ok.as_bytes().to_owned()),
Err(_) => None,
}
}
}
6 changes: 2 additions & 4 deletions src/communicative/nns/relay.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::future::Future;

pub const DEFAULT_RELAY_LIST: [&str; 5] = [
"wss://relay.damus.io",
"wss://relay.primal.net",
Expand All @@ -9,11 +7,11 @@ pub const DEFAULT_RELAY_LIST: [&str; 5] = [
];

pub trait Relay {
fn add_default_relay_list(&self) -> impl Future<Output = ()> + Send;
fn add_default_relay_list(&self) -> impl std::future::Future<Output = ()> + Send;
}

impl Relay for nostr_sdk::Client {
fn add_default_relay_list(&self) -> impl Future<Output = ()> + Send {
fn add_default_relay_list(&self) -> impl std::future::Future<Output = ()> + Send {
async move {
// Add the list of default relays.
for relay in DEFAULT_RELAY_LIST {
Expand Down
19 changes: 6 additions & 13 deletions src/communicative/peer/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl PeerManager {
chain: Chain,
nns_client: &NNSClient,
kind: PeerKind,
keys: &Vec<[u8; 32]>,
keys: &[[u8; 32]],
) -> Option<PEER_MANAGER> {
let manager_ = PeerManager {
chain,
Expand Down Expand Up @@ -81,10 +81,7 @@ impl PeerManager {
}

pub fn is_peer(&self, key: [u8; 32]) -> bool {
match self.retrieve_peer(key) {
Some(_) => return true,
None => return false,
}
self.retrieve_peer(key).is_some()
}

pub async fn peer_socket(&self, key: [u8; 32]) -> Option<SOCKET> {
Expand All @@ -104,22 +101,19 @@ impl PeerManager {
_peer.connection()
};

match conn {
Some(_) => return true,
None => return false,
}
conn.is_some()
}
}

#[async_trait]
pub trait PeerManagerExt {
async fn add_peers(&mut self, kind: PeerKind, keys: &Vec<[u8; 32]>) -> u64;
async fn add_peers(&mut self, kind: PeerKind, keys: &[[u8; 32]]) -> u64;
}

#[async_trait]
impl PeerManagerExt for PEER_MANAGER {
/// Tries to connect to a list of peers and returns the number of peers connected.
async fn add_peers(&mut self, kind: PeerKind, keys: &Vec<[u8; 32]>) -> u64 {
async fn add_peers(&mut self, kind: PeerKind, keys: &[[u8; 32]]) -> u64 {
let chain = {
let _self = self.lock().await;
_self.chain()
Expand All @@ -138,8 +132,7 @@ impl PeerManagerExt for PEER_MANAGER {
}

let peer_list_ = Arc::clone(&peer_list_);
let kind = kind.clone();
let key = key.clone();
let key = *key;
let nns_client = {
let _self = self.lock().await;
_self.nns_client.clone()
Expand Down
45 changes: 19 additions & 26 deletions src/communicative/peer/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Peer {
nns_client: &NNSClient,
) -> Result<PEER, TCPError> {
let (socket_, addr) = {
match connect_nns(key, &nns_client, chain).await {
match connect_nns(key, nns_client, chain).await {
Ok(socket) => {
let addr = match socket.peer_addr() {
Ok(addr) => addr,
Expand Down Expand Up @@ -106,10 +106,7 @@ impl Peer {
}

pub fn connected(&self) -> bool {
match self.connection() {
Some(_) => true,
None => false,
}
self.connection().is_some()
}

pub fn socket(&self) -> Option<SOCKET> {
Expand All @@ -123,12 +120,10 @@ impl Peer {
pub fn addr(&self) -> String {
match self.connection() {
Some(connection) => {
return format!("{}:{}", connection.1.ip(), connection.1.port());
}
None => {
return "Dead.".to_string();
format!("{}:{}", connection.1.ip(), connection.1.port())
}
};
None => "Dead.".to_string(),
}
}
}

Expand All @@ -155,22 +150,20 @@ impl PeerConnection for PEER {

async fn disconnection(&self) {
loop {
loop {
match self.ping().await {
Ok(_) => break,
Err(_) => {
let mut failure_iter: u8 = 0;
loop {
if failure_iter < 3 {
failure_iter += 1;
tokio::time::sleep(Duration::from_secs(5)).await;
continue;
} else {
let mut _peer = self.lock().await;
_peer.set_connection(None);

return ();
}
match self.ping().await {
Ok(_) => {}
Err(_) => {
let mut failure_iter: u8 = 0;
loop {
if failure_iter < 3 {
failure_iter += 1;
tokio::time::sleep(Duration::from_secs(5)).await;
continue;
} else {
let mut _peer = self.lock().await;
_peer.set_connection(None);

return;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/communicative/rpc/bitcoin_rpc/bitcoin_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::communicative::rpc::bitcoin_rpc::bitcoin_rpc_error::{
BitcoinRPCBroadcastRawTransactionError, BitcoinRPCGetChainTipError, BitcoinRPCRetrieveBlockError,
BitcoinRPCGetMempoolFeeRateError, BitcoinRPCValidateRPCError,
BitcoinRPCBroadcastRawTransactionError, BitcoinRPCGetChainTipError,
BitcoinRPCGetMempoolFeeRateError, BitcoinRPCRetrieveBlockError, BitcoinRPCValidateRPCError,
};
use crate::communicative::rpc::bitcoin_rpc::bitcoin_rpc_holder::BitcoinRPCHolder;
use crate::operative::run_args::chain::Chain;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn get_mempool_min_fee_rate(
// Bitcoin Core returns mempool minimum fee as BTC/kvB.
// Convert BTC/kvB -> sat/kvB -> sat/vbyte, rounded up to avoid underpaying.
let mempool_min_fee_sat_per_kvb = mempool_info.mempool_min_fee.to_sat();
let mempool_min_fee_sat_per_vbyte = ((mempool_min_fee_sat_per_kvb + 999) / 1000).max(1);
let mempool_min_fee_sat_per_vbyte = mempool_min_fee_sat_per_kvb.div_ceil(1000).max(1);

Ok(mempool_min_fee_sat_per_vbyte)
}
Expand Down
2 changes: 1 addition & 1 deletion src/communicative/rpc/bitcoin_rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod bitcoin_rpc_error;
pub mod bitcoin_rpc;
pub mod bitcoin_rpc_error;
pub mod bitcoin_rpc_holder;
23 changes: 12 additions & 11 deletions src/communicative/tcp/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
mod peer_tcp_client;
mod tcp_client;

pub use crate::communicative::tcp::protocol::batchrecord::{
BatchRecordRequestBody, BatchRecordResponseBody, BatchRecordResponseError,
BatchRecordSuccessBody,
};
pub use crate::communicative::tcp::protocol::batchcontainer::{
BatchContainerRequestBody, BatchContainerResponseBody, BatchContainerResponseError,
BatchContainerSuccessBody,
Expand All @@ -13,6 +9,18 @@ pub use crate::communicative::tcp::protocol::batchcontainer_by_prevoutpoint::{
BatchContainerByPrevOutpointRequestBody, BatchContainerByPrevOutpointResponseBody,
BatchContainerByPrevOutpointResponseError, BatchContainerByPrevOutpointSuccessBody,
};
pub use crate::communicative::tcp::protocol::batchrecord::{
BatchRecordRequestBody, BatchRecordResponseBody, BatchRecordResponseError,
BatchRecordSuccessBody,
};
pub use crate::communicative::tcp::protocol::config::{
ConfigRequestBody, ConfigResponseBody, ConfigResponseError, ConfigSuccessBody,
ExecConfigInPoolError,
};
pub use crate::communicative::tcp::protocol::deploy::{
DeployRequestBody, DeployResponseBody, DeployResponseError, DeploySuccessBody,
ExecDeployInPoolError,
};
pub use crate::communicative::tcp::protocol::in_flight_sync::{
InFlightSyncRequestBody, InFlightSyncResponseBody, InFlightSyncResponseError,
};
Expand All @@ -23,13 +31,6 @@ pub use crate::communicative::tcp::protocol::liftup_v1::{
pub use crate::communicative::tcp::protocol::r#move::{
ExecMoveInPoolError, MoveRequestBody, MoveResponseBody, MoveResponseError, MoveSuccessBody,
};
pub use crate::communicative::tcp::protocol::config::{
ExecConfigInPoolError, ConfigRequestBody, ConfigResponseBody, ConfigResponseError, ConfigSuccessBody,
};
pub use crate::communicative::tcp::protocol::deploy::{
DeployRequestBody, DeployResponseBody, DeployResponseError, DeploySuccessBody,
ExecDeployInPoolError,
};
pub use crate::communicative::tcp::protocol::swapout::{
ExecSwapoutInPoolError, SwapoutRequestBody, SwapoutResponseBody, SwapoutResponseError,
SwapoutSuccessBody,
Expand Down
4 changes: 2 additions & 2 deletions src/communicative/tcp/client/peer_tcp_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::communicative::peer::peer::PEER;
use crate::communicative::tcp::protocol::batchrecord::client::request_batchrecord;
use crate::communicative::tcp::protocol::batchrecord::BatchRecordResponseBody;
use crate::communicative::tcp::protocol::batchcontainer::client::request_batchcontainer;
use crate::communicative::tcp::protocol::batchcontainer::BatchContainerResponseBody;
use crate::communicative::tcp::protocol::batchcontainer_by_prevoutpoint::client::request_batchcontainer_by_prevoutpoint;
use crate::communicative::tcp::protocol::batchcontainer_by_prevoutpoint::BatchContainerByPrevOutpointResponseBody;
use crate::communicative::tcp::protocol::batchrecord::client::request_batchrecord;
use crate::communicative::tcp::protocol::batchrecord::BatchRecordResponseBody;
use crate::communicative::tcp::protocol::config::client::request_config;
use crate::communicative::tcp::protocol::config::ConfigResponseBody;
use crate::communicative::tcp::protocol::deploy::client::request_deploy;
Expand Down
2 changes: 1 addition & 1 deletion src/communicative/tcp/client/tcp_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::communicative::tcp::protocol::batchrecord::BatchRecordResponseBody;
use crate::communicative::tcp::protocol::batchcontainer::BatchContainerResponseBody;
use crate::communicative::tcp::protocol::batchcontainer_by_prevoutpoint::BatchContainerByPrevOutpointResponseBody;
use crate::communicative::tcp::protocol::batchrecord::BatchRecordResponseBody;
use crate::communicative::tcp::protocol::config::ConfigResponseBody;
use crate::communicative::tcp::protocol::deploy::DeployResponseBody;
use crate::communicative::tcp::protocol::in_flight_sync::InFlightSyncResponseBody;
Expand Down
16 changes: 8 additions & 8 deletions src/communicative/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ pub mod request_error;
pub mod server;
pub mod tcp;

pub use protocol::batchrecord::{
BatchRecordRequestBody, BatchRecordResponseBody, BatchRecordResponseError,
BatchRecordSuccessBody,
};
pub use protocol::batchcontainer::{
BatchContainerRequestBody, BatchContainerResponseBody, BatchContainerResponseError,
BatchContainerSuccessBody,
Expand All @@ -17,6 +13,14 @@ pub use protocol::batchcontainer_by_prevoutpoint::{
BatchContainerByPrevOutpointRequestBody, BatchContainerByPrevOutpointResponseBody,
BatchContainerByPrevOutpointResponseError, BatchContainerByPrevOutpointSuccessBody,
};
pub use protocol::batchrecord::{
BatchRecordRequestBody, BatchRecordResponseBody, BatchRecordResponseError,
BatchRecordSuccessBody,
};
pub use protocol::deploy::{
DeployRequestBody, DeployResponseBody, DeployResponseError, DeploySuccessBody,
ExecDeployInPoolError,
};
pub use protocol::in_flight_sync::{
InFlightSyncRequestBody, InFlightSyncResponseBody, InFlightSyncResponseError,
};
Expand All @@ -27,10 +31,6 @@ pub use protocol::liftup_v1::{
pub use protocol::r#move::{
ExecMoveInPoolError, MoveRequestBody, MoveResponseBody, MoveResponseError, MoveSuccessBody,
};
pub use protocol::deploy::{
DeployRequestBody, DeployResponseBody, DeployResponseError, DeploySuccessBody,
ExecDeployInPoolError,
};
pub use protocol::swapout::{
ExecSwapoutInPoolError, SwapoutRequestBody, SwapoutResponseBody, SwapoutResponseError,
SwapoutSuccessBody,
Expand Down
Loading
Loading