Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Sep 26, 2024
1 parent da38996 commit 33b405a
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 144 deletions.
17 changes: 9 additions & 8 deletions applications/minotari_app_grpc/proto/base_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ message TipInfoResponse {
MetaData metadata = 1;
bool initial_sync_achieved = 2;
BaseNodeState base_node_state = 3;
bytes parent_hash = 4;
}

enum BaseNodeState{
Expand Down Expand Up @@ -191,13 +192,13 @@ message GetNewBlockTemplateWithCoinbasesRequest{
PowAlgo algo = 1;
//This field should be moved to optional once optional keyword is standard
uint64 max_weight = 2;
repeated NewBlockCoinbase coinbases = 3;
repeated NewBlockCoinbase coinbases = 3;
}

/// request type of GetNewBlockWithCoinbasesRequest
/// request type of GetNewBlockWithCoinbasesRequest
message GetNewBlockWithCoinbasesRequest{
NewBlockTemplate new_template = 1;
repeated NewBlockCoinbase coinbases = 2;
repeated NewBlockCoinbase coinbases = 2;
}

message NewBlockCoinbase{
Expand All @@ -218,7 +219,7 @@ message NetworkDifficultyResponse {
uint64 sha3x_estimated_hash_rate = 6;
uint64 randomx_estimated_hash_rate = 7;
uint64 num_coinbases = 8;
repeated bytes coinbase_extras = 9;
repeated bytes coinbase_extras = 9;
}

// A generic single value response for a specific height
Expand Down Expand Up @@ -252,7 +253,7 @@ message BlockGroupRequest {
CalcType calc_type = 4;
}

/// GetBlockSize / GetBlockFees Response
/// GetBlockSize / GetBlockFees Response
message BlockGroupResponse {
repeated double value = 1;
CalcType calc_type = 2;
Expand Down Expand Up @@ -436,7 +437,7 @@ message GetPeersResponse{
message GetPeersRequest{}

message SubmitTransactionRequest {
Transaction transaction = 1;
Transaction transaction = 1;
}

message SubmitTransactionResponse {
Expand All @@ -447,7 +448,7 @@ enum SubmitTransactionResult {
NONE = 0;
ACCEPTED = 1;
NOT_PROCESSABLE_AT_THIS_TIME = 2;
ALREADY_MINED = 3;
ALREADY_MINED = 3;
REJECTED = 4;

}
Expand All @@ -461,7 +462,7 @@ message GetMempoolTransactionsResponse {
}

message TransactionStateRequest {
Signature excess_sig = 1;
Signature excess_sig = 1;
}

message TransactionStateResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl BlockTemplateProtocol<'_> {
) -> Result<FinalBlockTemplateData, MmProxyError> {
let best_block_hash = self.get_current_best_block_hash().await?;
let existing_block_template = block_templates.blocks_contains(best_block_hash).await;

let mut final_block_template = existing_block_template;

let mut loop_count = 0;
loop {
if loop_count >= 10 {
Expand All @@ -101,21 +101,29 @@ impl BlockTemplateProtocol<'_> {
loop_count
)));
}
// Invalidate the cached template as the tip is not the same anymore; force creating a new template
if loop_count == 1 && final_block_template.is_some() {
final_block_template = None;
}
if loop_count > 0 {
tokio::time::sleep(std::time::Duration::from_millis(loop_count * 250)).await;
}
loop_count += 1;
let (final_template_data, block_height) = if let Some(data) = final_block_template.clone() {
let (final_template_data, block_height, parent_hash) = if let Some(data) = final_block_template.clone() {
let height = data
.template
.tari_block
.header
.as_ref()
.map(|h| h.height)
.unwrap_or_default();
let prev_hash = data
.template
.tari_block
.header
.as_ref()
.map(|h| h.prev_hash.clone())
.unwrap_or_default();
debug!(
target: LOG_TARGET,
"Used existing block template and block for height: #{} (try {}), block hash: `{}`",
Expand All @@ -126,7 +134,7 @@ impl BlockTemplateProtocol<'_> {
None => "None".to_string(),
}
);
(data, height)
(data, height, prev_hash)
} else {
let block = match self.p2pool_client.as_mut() {
Some(client) => {
Expand Down Expand Up @@ -188,14 +196,23 @@ impl BlockTemplateProtocol<'_> {
.as_ref()
.map(|b| b.header.as_ref().map(|h| h.height).unwrap_or_default())
.unwrap_or_default();
let prev_hash = block
.block
.as_ref()
.map(|b| b.header.as_ref().map(|h| h.prev_hash.clone()).unwrap_or_default())
.unwrap_or_default();

let miner_data = block
.miner_data
.as_ref()
.cloned()
.ok_or_else(|| MmProxyError::GrpcResponseMissingField("miner_data"))?;

(add_monero_data(block, monero_mining_data.clone(), miner_data)?, height)
(
add_monero_data(block, monero_mining_data.clone(), miner_data)?,
height,
prev_hash,
)
};

block_templates
Expand All @@ -210,10 +227,12 @@ impl BlockTemplateProtocol<'_> {
.remove_new_block_template(best_block_hash.to_vec())
.await;

if !self.check_expected_tip(block_height).await? {
if !self.check_expected_tip_and_parent(block_height, &parent_hash).await? {
debug!(
target: LOG_TARGET,
"Chain tip has progressed past template height {}. Fetching a new block template (try {}).", block_height, loop_count
"Template (height {}, parent {}) not based on current chain tip anymore, fetching a new block \
template (try {}).",
block_height, parent_hash.to_hex(), loop_count
);
continue;
}
Expand All @@ -224,7 +243,8 @@ impl BlockTemplateProtocol<'_> {
.header
.as_ref()
.map(|h| h.height)
.unwrap_or_default(), loop_count,
.unwrap_or_default(),
loop_count,
match final_template_data.template.tari_block.header.as_ref() {
Some(h) => h.hash.to_hex(),
None => "None".to_string(),
Expand Down Expand Up @@ -337,16 +357,27 @@ impl BlockTemplateProtocol<'_> {
Ok(NewBlockTemplateData { template, miner_data })
}

/// Check if the height is more than the actual tip. So if still makes sense to compute block for that height.
async fn check_expected_tip(&mut self, height: u64) -> Result<bool, MmProxyError> {
/// Check if the height and parent hash is still as expected, so that it still makes sense to compute the block for
/// that height.
async fn check_expected_tip_and_parent(&mut self, height: u64, parent_hash: &[u8]) -> Result<bool, MmProxyError> {
let tip = self
.base_node_client
.clone()
.get_tip_info(grpc::Empty {})
.await?
.into_inner();
let tip_height = tip.metadata.as_ref().map(|m| m.best_block_height).unwrap_or(0);
let tip_parent_hash = tip.parent_hash;

if tip_parent_hash != parent_hash {
warn!(
target: LOG_TARGET,
"Base node received next block (hash={}) that has invalidated the block template (hash={})",
tip_parent_hash.to_hex(),
parent_hash.to_vec().to_hex()
);
return Ok(false);
}
if height <= tip_height {
warn!(
target: LOG_TARGET,
Expand Down Expand Up @@ -427,7 +458,7 @@ fn add_monero_data(

let aux_chain_hashes = AuxChainHashes::try_from(vec![monero::Hash::from_slice(merge_mining_hash.as_slice())])?;
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 p2pool_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 @@ -438,7 +469,7 @@ fn add_monero_data(
.monero_seed(monero_mining_data.seed_hash)
.monero_difficulty(monero_mining_data.difficulty)
.tari_target_difficulty(tari_target_difficulty)
.p2pool_target_difficulty(pool_target_difficulty)
.p2pool_target_difficulty(p2pool_target_difficulty)
.tari_merge_mining_hash(merge_mining_hash)
.aux_hashes(aux_chain_hashes.clone())
.build()?;
Expand All @@ -458,16 +489,17 @@ fn add_monero_data(

let mining_difficulty = cmp::min(
monero_mining_data.difficulty,
if let Some(val) = pool_target_difficulty {
if let Some(val) = p2pool_target_difficulty {
cmp::min(val, tari_target_difficulty)
} else {
tari_target_difficulty
},
);
info!(
target: LOG_TARGET,
"Difficulties: Minotari ({}), Monero({}), Selected({})",
"Difficulties: Minotari ({}), P2Pool ({:?}), Monero ({}), Selected ({})",
tari_target_difficulty,
p2pool_target_difficulty,
monero_mining_data.difficulty,
mining_difficulty
);
Expand Down
Loading

0 comments on commit 33b405a

Please sign in to comment.