Skip to content

Commit

Permalink
Merge branch 'development' of github.com:tari-project/sha-p2pool into…
Browse files Browse the repository at this point in the history
… development
  • Loading branch information
stringhandler committed Oct 26, 2024
2 parents a4e0af2 + c4ffb3b commit 8b6e590
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 227 deletions.
12 changes: 9 additions & 3 deletions src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,15 @@ where S: ShareChain
new_tip_block.fix_hash();

if let Some(miner_data) = response.miner_data.as_mut() {
let _ = self
.stats_broadcast
.send_network_difficulty(pow_algo, Difficulty::from_u64(miner_data.target_difficulty).unwrap());
match Difficulty::from_u64(miner_data.target_difficulty) {
Ok(diff) => {
let _ = self.stats_broadcast.send_network_difficulty(pow_algo, diff);
},
Err(e) => {
error!(target: LOG_TARGET, "Invalid target difficulty: {e:?}");
},
}

// what happens p2pool difficulty > base chain diff
if target_difficulty.as_u64() < miner_data.target_difficulty {
miner_data.target_difficulty = target_difficulty.as_u64();
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub mod grpc;
pub mod http;
pub mod p2p;

pub const PROTOCOL_VERSION: u64 = 9;
pub const PROTOCOL_VERSION: u64 = 10;
80 changes: 76 additions & 4 deletions src/server/p2p/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ impl PeerInfo {
}

pub fn public_addresses(&self) -> Vec<Multiaddr> {
self.public_addresses.iter().map(|addr| addr.parse().unwrap()).collect()
self.public_addresses
.iter()
.filter_map(|addr| addr.parse().ok())
.collect()
}
}

Expand All @@ -97,14 +100,83 @@ impl ShareChainSyncRequest {
}

pub fn algo(&self) -> PowAlgorithm {
PowAlgorithm::try_from(self.algo).unwrap()
PowAlgorithm::try_from(self.algo).unwrap_or(PowAlgorithm::RandomX)
}

pub fn missing_blocks(&self) -> &[(u64, FixedHash)] {
&self.missing_blocks
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CatchUpSyncRequest {
algo: u64,
i_have: Vec<(u64, FixedHash)>,
}

impl CatchUpSyncRequest {
pub fn new(algo: PowAlgorithm, i_have: Vec<(u64, FixedHash)>) -> Self {
Self {
algo: algo.as_u64(),
i_have,
}
}

pub fn algo(&self) -> PowAlgorithm {
PowAlgorithm::try_from(self.algo).unwrap_or(PowAlgorithm::RandomX)
}

pub fn i_have(&self) -> &[(u64, FixedHash)] {
&self.i_have
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CatchUpSyncResponse {
version: u64,
peer_id: PeerId,
algo: u64,
blocks: Vec<P2Block>,
// the tip is used to work out if we should continuing syncing from them
tip: (u64, FixedHash),
}

impl CatchUpSyncResponse {
pub fn new(algo: PowAlgorithm, peer_id: PeerId, blocks: &[Arc<P2Block>], tip: (u64, FixedHash)) -> Self {
Self {
version: PROTOCOL_VERSION,
algo: algo.as_u64(),
peer_id,
blocks: blocks.iter().map(|block| (**block).clone()).collect(),
tip,
}
}

pub fn peer_id(&self) -> &PeerId {
&self.peer_id
}

pub fn algo(&self) -> PowAlgorithm {
PowAlgorithm::try_from(self.algo).unwrap_or(PowAlgorithm::RandomX)
}

pub fn tip_hash(&self) -> &FixedHash {
&self.tip.1
}

pub fn tip_height(&self) -> u64 {
self.tip.0
}

pub fn into_blocks(self) -> Vec<P2Block> {
let mut blocks = self.blocks;
for block in &mut blocks {
block.verified = false;
}
blocks
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DirectPeerInfoRequest {
pub peer_id: String,
Expand Down Expand Up @@ -135,7 +207,7 @@ impl NotifyNewTipBlock {
}

pub fn algo(&self) -> PowAlgorithm {
PowAlgorithm::try_from(self.algo).unwrap()
PowAlgorithm::try_from(self.algo).unwrap_or(PowAlgorithm::RandomX)
}
}

Expand All @@ -162,7 +234,7 @@ impl ShareChainSyncResponse {
}

pub fn algo(&self) -> PowAlgorithm {
PowAlgorithm::try_from(self.algo).unwrap()
PowAlgorithm::try_from(self.algo).unwrap_or(PowAlgorithm::RandomX)
}

pub fn into_blocks(self) -> Vec<P2Block> {
Expand Down
Loading

0 comments on commit 8b6e590

Please sign in to comment.