Skip to content

Commit

Permalink
fix: updated logic for share generation (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksrichard authored Sep 11, 2024
1 parent f6912a8 commit 14c65f2
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ jobs:
strategy:
matrix:
tari_target_network: [
{ target: "testnet", network: "esmeralda" },
{ network: "esmeralda" },
]
env:
TARI_TARGET_NETWORK: ${{ matrix.tari_target_network.target }}
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sha_p2pool"
version = "0.1.5-pre.4"
version = "0.1.5-pre.5"
edition = "2021"

[dependencies]
Expand Down
48 changes: 1 addition & 47 deletions src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use itertools::Itertools;
use log::{error, info, warn};
use minotari_app_grpc::tari_rpc::pow_algo::PowAlgos;
use minotari_app_grpc::tari_rpc::{
base_node_client::BaseNodeClient, sha_p2_pool_server::ShaP2Pool, GetNewBlockRequest, GetNewBlockResponse,
GetNewBlockTemplateWithCoinbasesRequest, NewBlockTemplateRequest, SubmitBlockRequest, SubmitBlockResponse,
};
use num::ToPrimitive;
use std::collections::HashMap;
use std::sync::Arc;
use tari_common::configuration::Network;
use tari_common_types::tari_address::TariAddress;
use tari_common_types::types::FixedHash;
use tari_core::consensus;
use tari_core::consensus::ConsensusManager;
Expand All @@ -28,7 +25,7 @@ use crate::server::http::stats::{
P2POOL_STAT_ACCEPTED_BLOCKS_COUNT, P2POOL_STAT_REJECTED_BLOCKS_COUNT,
};
use crate::server::stats_store::StatsStore;
use crate::sharechain::{BlockValidationParams, BLOCKS_WINDOW};
use crate::sharechain::BlockValidationParams;
use crate::{
server::{
grpc::{error::Error, util},
Expand All @@ -39,8 +36,6 @@ use crate::{

const LOG_TARGET: &str = "p2pool::server::grpc::p2pool";

const MAX_SUBMITTED_BLOCKS: u64 = BLOCKS_WINDOW as u64 / 2;

pub fn min_difficulty(pow: PowAlgorithm) -> Result<u64, Error> {
let network = Network::get_current_or_user_setting_or_default();
let consensus_constants = match network {
Expand Down Expand Up @@ -110,54 +105,13 @@ where
})
}

async fn submitted_shares_count(&self, pow: PowAlgorithm, miner_wallet_address: TariAddress) -> u64 {
let share_chain = match pow {
PowAlgorithm::RandomX => self.share_chain_random_x.clone(),
PowAlgorithm::Sha3x => self.share_chain_sha3x.clone(),
};
match share_chain.blocks(0).await {
Ok(blocks) => blocks
.iter()
.tail(BLOCKS_WINDOW)
.filter(|block| {
if let Some(addr) = block.miner_wallet_address() {
return miner_wallet_address == *addr;
}
false
})
.count()
.to_u64()
.unwrap(),
Err(error) => {
warn!(target: LOG_TARGET, "[{}] Failed to get share chain blocks: {error:?}", pow);
0
},
}
}

async fn can_submit_block(&self, pow: PowAlgorithm, block: &Block) -> bool {
match block.miner_wallet_address() {
Some(miner_wallet_address) => {
self.submitted_shares_count(pow, miner_wallet_address.clone()).await < MAX_SUBMITTED_BLOCKS
},
None => {
warn!(target: LOG_TARGET, "Missing miner wallet address!");
false
},
}
}

/// Submits a new block to share chain and broadcasts to the p2p network.
pub async fn submit_share_chain_block(&self, block: &Block) -> Result<(), Status> {
let pow_algo = block.original_block_header().pow.pow_algo;
let share_chain = match pow_algo {
PowAlgorithm::RandomX => self.share_chain_random_x.clone(),
PowAlgorithm::Sha3x => self.share_chain_sha3x.clone(),
};
if !self.can_submit_block(pow_algo, block).await {
warn!(target: LOG_TARGET, "Skipping block submission.. Too many blocks sent!");
return Ok(());
}
match share_chain.submit_block(block).await {
Ok(_) => {
self.stats_store
Expand Down
3 changes: 3 additions & 0 deletions src/server/http/stats/cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use crate::server::http::stats::models::Stats;
use std::sync::Arc;
use std::time::Duration;
Expand Down
2 changes: 2 additions & 0 deletions src/sharechain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum Error {
InvalidBlock(Block),
#[error("Number conversion error: {0}")]
FromIntConversion(#[from] TryFromIntError),
#[error("Number conversion error: {0} to u64")]
FromF64ToU64Conversion(f64),
#[error("Difficulty calculation error: {0}")]
Difficulty(#[from] DifficultyError),
#[error("RandomX difficulty calculation error: {0}")]
Expand Down
Loading

0 comments on commit 14c65f2

Please sign in to comment.