Skip to content

Commit

Permalink
Add pool and tari difficulties
Browse files Browse the repository at this point in the history
Added pool and tari difficulties to the p2pool request
  • Loading branch information
hansieodendaal committed Sep 24, 2024
1 parent fd41ba4 commit da38996
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 82 deletions.
8 changes: 7 additions & 1 deletion applications/minotari_app_grpc/proto/base_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,16 @@ message GetNewBlockBlobResult{
// This is mining data for the miner asking for a new block
message MinerData{
PowAlgo algo = 1;
uint64 target_difficulty = 2;
uint64 tari_target_difficulty = 2;
uint64 reward = 3;
// bytes merge_mining_hash =4;
uint64 total_fees = 5;
PoolDifficulty p2pool_target_difficulty = 6;
}

// Pool difficulty
message PoolDifficulty {
uint64 difficulty = 1;
}

// This is the request type for the Search Kernels rpc
Expand Down
8 changes: 7 additions & 1 deletion applications/minotari_app_grpc/proto/p2pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ message GetNewBlockRequest {

message GetNewBlockResponse {
tari.rpc.GetNewBlockResult block = 1;
uint64 target_difficulty = 2;
uint64 tari_target_difficulty = 2;
uint64 p2pool_target_difficulty = 3;
}

message SubmitBlockRequest {
tari.rpc.Block block = 1;
string wallet_payment_address = 2;
Difficulty achieved_difficulty = 3;
}

message Difficulty {
uint64 difficulty = 1;
}
44 changes: 30 additions & 14 deletions applications/minotari_merge_mining_proxy/src/block_template_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ pub struct BlockTemplateData {
pub tari_block: grpc::Block,
pub tari_miner_data: grpc::MinerData,
pub monero_difficulty: u64,
pub tari_difficulty: u64,
pub tari_target_difficulty: u64,
pub p2pool_target_difficulty: Option<u64>,
pub tari_merge_mining_hash: FixedHash,
#[allow(dead_code)]
pub aux_chain_hashes: AuxChainHashes,
Expand All @@ -226,7 +227,8 @@ pub struct BlockTemplateDataBuilder {
tari_block: Option<grpc::Block>,
tari_miner_data: Option<grpc::MinerData>,
monero_difficulty: Option<u64>,
tari_difficulty: Option<u64>,
tari_target_difficulty: Option<u64>,
p2pool_target_difficulty: Option<Option<u64>>,
tari_merge_mining_hash: Option<FixedHash>,
aux_chain_hashes: AuxChainHashes,
}
Expand Down Expand Up @@ -256,8 +258,13 @@ impl BlockTemplateDataBuilder {
self
}

pub fn tari_difficulty(mut self, difficulty: u64) -> Self {
self.tari_difficulty = Some(difficulty);
pub fn tari_target_difficulty(mut self, difficulty: u64) -> Self {
self.tari_target_difficulty = Some(difficulty);
self
}

pub fn p2pool_target_difficulty(mut self, difficulty: Option<u64>) -> Self {
self.p2pool_target_difficulty = Some(difficulty);
self
}

Expand Down Expand Up @@ -289,9 +296,12 @@ impl BlockTemplateDataBuilder {
let monero_difficulty = self
.monero_difficulty
.ok_or_else(|| MmProxyError::MissingDataError("monero_difficulty not provided".to_string()))?;
let tari_difficulty = self
.tari_difficulty
.ok_or_else(|| MmProxyError::MissingDataError("tari_difficulty not provided".to_string()))?;
let tari_target_difficulty = self
.tari_target_difficulty
.ok_or_else(|| MmProxyError::MissingDataError("tari_target_difficulty not provided".to_string()))?;
let p2pool_target_difficulty = self
.p2pool_target_difficulty
.ok_or_else(|| MmProxyError::MissingDataError("p2pool_target_difficulty not provided".to_string()))?;
let tari_merge_mining_hash = self
.tari_merge_mining_hash
.ok_or_else(|| MmProxyError::MissingDataError("tari_hash not provided".to_string()))?;
Expand All @@ -304,7 +314,8 @@ impl BlockTemplateDataBuilder {
tari_block,
tari_miner_data,
monero_difficulty,
tari_difficulty,
tari_target_difficulty,
p2pool_target_difficulty,
tari_merge_mining_hash,
aux_chain_hashes: self.aux_chain_hashes,
})
Expand Down Expand Up @@ -332,7 +343,8 @@ pub mod test {
let hash = block.hash();
let miner_data = grpc::MinerData {
reward: 10000,
target_difficulty: 600000,
tari_target_difficulty: 600000,
p2pool_target_difficulty: None,
total_fees: 100,
algo: Some(grpc::PowAlgo { pow_algo: 0 }),
};
Expand All @@ -341,7 +353,8 @@ pub mod test {
.tari_block(block.try_into().unwrap())
.tari_miner_data(miner_data)
.monero_difficulty(123456)
.tari_difficulty(12345)
.tari_target_difficulty(12345)
.p2pool_target_difficulty(None)
.tari_merge_mining_hash(hash)
.aux_hashes(AuxChainHashes::try_from(vec![monero::Hash::from_slice(hash.as_slice())]).unwrap());
let block_template_data = btdb.build().unwrap();
Expand Down Expand Up @@ -398,7 +411,8 @@ pub mod test {
// With monero seed, block, miner data
let miner_data = grpc::MinerData {
reward: 10000,
target_difficulty: 600000,
tari_target_difficulty: 600000,
p2pool_target_difficulty: None,
total_fees: 100,
algo: Some(grpc::PowAlgo { pow_algo: 0 }),
};
Expand All @@ -416,7 +430,7 @@ pub mod test {
.tari_miner_data(miner_data)
.monero_difficulty(123456);
assert!(
matches!(btdb.build(), Err(MmProxyError::MissingDataError(err)) if err == *"tari_difficulty not provided")
matches!(btdb.build(), Err(MmProxyError::MissingDataError(err)) if err == *"tari_target_difficulty not provided")
);
}

Expand All @@ -425,9 +439,11 @@ pub mod test {
let build = create_block_template_data();
assert!(build.template.monero_seed.is_empty());
assert_eq!(build.template.tari_block.header.unwrap().version, 100);
assert_eq!(build.template.tari_miner_data.target_difficulty, 600000);
assert_eq!(build.template.tari_miner_data.tari_target_difficulty, 600000);
assert_eq!(build.template.tari_miner_data.p2pool_target_difficulty, None);
assert_eq!(build.template.monero_difficulty, 123456);
assert_eq!(build.template.tari_difficulty, 12345);
assert_eq!(build.template.tari_target_difficulty, 12345);
assert_eq!(build.template.p2pool_target_difficulty, None);
assert_eq!(build.blockhashing_blob, "no blockhashing_blob data".to_string());
assert_eq!(build.blocktemplate_blob, "no blocktemplate_blob data".to_string());
assert_eq!(build.target_difficulty, Difficulty::from_u64(12345).unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ fn add_monero_data(
.map_err(|e| MmProxyError::ConversionError(e.to_string()))?;

let aux_chain_hashes = AuxChainHashes::try_from(vec![monero::Hash::from_slice(merge_mining_hash.as_slice())])?;
let tari_difficulty = miner_data.target_difficulty;
let tari_target_difficulty = miner_data.tari_target_difficulty;
let pool_target_difficulty = miner_data.p2pool_target_difficulty.as_ref().map(|val| val.difficulty);
let block_template_data = BlockTemplateDataBuilder::new()
.tari_block(
tari_block_result
Expand All @@ -436,7 +437,8 @@ fn add_monero_data(
.tari_miner_data(miner_data)
.monero_seed(monero_mining_data.seed_hash)
.monero_difficulty(monero_mining_data.difficulty)
.tari_difficulty(tari_difficulty)
.tari_target_difficulty(tari_target_difficulty)
.p2pool_target_difficulty(pool_target_difficulty)
.tari_merge_mining_hash(merge_mining_hash)
.aux_hashes(aux_chain_hashes.clone())
.build()?;
Expand All @@ -454,12 +456,18 @@ fn add_monero_data(
let blockhashing_blob = monero_rx::create_blockhashing_blob_from_block(&monero_block)?;
let blocktemplate_blob = monero_rx::serialize_monero_block_to_hex(&monero_block)?;

let monero_difficulty = monero_mining_data.difficulty;
let mining_difficulty = cmp::min(monero_difficulty, tari_difficulty);
let mining_difficulty = cmp::min(
monero_mining_data.difficulty,
if let Some(val) = pool_target_difficulty {
cmp::min(val, tari_target_difficulty)
} else {
tari_target_difficulty
},
);
info!(
target: LOG_TARGET,
"Difficulties: Minotari ({}), Monero({}), Selected({})",
tari_difficulty,
tari_target_difficulty,
monero_mining_data.difficulty,
mining_difficulty
);
Expand Down
5 changes: 3 additions & 2 deletions applications/minotari_merge_mining_proxy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ pub struct MergeMiningProxyConfig {
/// Address of the minotari_merge_mining_proxy application
pub listener_address: Multiaddr,
/// In sole merged mining, the block solution is usually submitted to the Monero blockchain (monerod) as well as to
/// the Minotari blockchain, then this setting should be "true". With pool merged mining, there is no sense in
/// submitting the solution to the Monero blockchain as thepool does that, then this setting should be "false".
/// the Minotari blockchain, then this setting should be "true". With Monero pool merged mining, there is no sense
/// in submitting the solution to the Monero blockchain as the Monero pool does that, then this setting should be
/// "false".
pub submit_to_origin: bool,
/// The merge mining proxy can either wait for the base node to achieve initial sync at startup before it enables
/// mining, or not. If merge mining starts before the base node has achieved initial sync, those Minotari mined
Expand Down
2 changes: 2 additions & 0 deletions applications/minotari_merge_mining_proxy/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub enum MmProxyError {
MaxSizeBytesError(#[from] MaxSizeBytesError),
#[error("Max sized vector error: {0}")]
MaxSizeVecError(#[from] MaxSizeVecError),
#[error("Logical process error: {0}")]
LogicalError(String),
}

impl From<tonic::Status> for MmProxyError {
Expand Down
Loading

0 comments on commit da38996

Please sign in to comment.