Skip to content

Commit

Permalink
not store entire coinbase
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Nov 28, 2023
1 parent 5f93c53 commit d1bf0a4
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 62 deletions.
12 changes: 11 additions & 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 applications/minotari_merge_mining_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl InnerService {
let start = Instant::now();
let achieved_target = if self.config.check_tari_difficulty_before_submit {
trace!(target: LOG_TARGET, "Starting calculate achieved Tari difficultly");
let diff = randomx_difficulty(&tari_header, &self.randomx_factory, &gen_hash)?;
let diff = randomx_difficulty(&tari_header, &self.randomx_factory, &gen_hash, &self.consensus_manager)?;
trace!(
target: LOG_TARGET,
"Finished calculate achieved Tari difficultly - achieved {} vs. target {}",
Expand Down
1 change: 1 addition & 0 deletions base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ tokio = { version = "1.23", features = ["time", "sync", "macros"] }
tracing = "0.1.26"
zeroize = "1"
primitive-types = { version = "0.12", features = ["serde"] }
tiny-keccak = { git = "https://github.com/SWvheerden/tiny-keccak", rev = "7d8143eefdca14c8b30f805cbb0ca7aa764951af",features = ["keccak"] }

[dev-dependencies]
criterion = { version = "0.4.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,12 @@ where B: BlockchainBackend + 'static
.await?;
}
let achieved = match new_block.header.pow_algo() {
PowAlgorithm::RandomX => randomx_difficulty(&new_block.header, &self.randomx_factory, &gen_hash)?,
PowAlgorithm::RandomX => randomx_difficulty(
&new_block.header,
&self.randomx_factory,
&gen_hash,
&self.consensus_manager,
)?,
PowAlgorithm::Sha3x => sha3x_difficulty(&new_block.header)?,
};
if achieved < min_difficulty {
Expand Down
27 changes: 18 additions & 9 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,9 @@ where B: BlockchainBackend

fn insert_block(&self, block: Arc<ChainBlock>) -> Result<(), ChainStorageError> {
let mut db = self.db_write_access()?;

let mut txn = DbTransaction::new();
insert_best_block(&mut txn, block)?;
insert_best_block(&mut txn, block, &self.consensus_manager)?;
db.write(txn)
}

Expand Down Expand Up @@ -1182,6 +1183,7 @@ where B: BlockchainBackend
&self.config,
&*self.validators.block,
self.consensus_manager.chain_strength_comparer(),
&self.consensus_manager,
)?;
Ok(())
}
Expand Down Expand Up @@ -1467,7 +1469,11 @@ fn add_block<T: BlockchainBackend>(
}

/// Adds a new block onto the chain tip and sets it to the best block.
fn insert_best_block(txn: &mut DbTransaction, block: Arc<ChainBlock>) -> Result<(), ChainStorageError> {
fn insert_best_block(
txn: &mut DbTransaction,
block: Arc<ChainBlock>,
consensus: &ConsensusManager,
) -> Result<(), ChainStorageError> {
let block_hash = block.accumulated_data().hash;
debug!(
target: LOG_TARGET,
Expand All @@ -1477,7 +1483,7 @@ fn insert_best_block(txn: &mut DbTransaction, block: Arc<ChainBlock>) -> Result<
);
if block.header().pow_algo() == PowAlgorithm::RandomX {
let monero_header =
MoneroPowData::from_header(block.header()).map_err(|e| ChainStorageError::InvalidArguments {
MoneroPowData::from_header(block.header(), consensus).map_err(|e| ChainStorageError::InvalidArguments {
func: "insert_best_block",
arg: "block",
message: format!("block contained invalid or malformed monero PoW data: {}", e),
Expand Down Expand Up @@ -1822,7 +1828,7 @@ fn handle_possible_reorg<T: BlockchainBackend>(
let hash = candidate_block.header.hash();
insert_orphan_and_find_new_tips(db, candidate_block, header_validator, consensus_manager)?;
let after_orphans = timer.elapsed();
let res = swap_to_highest_pow_chain(db, config, block_validator, chain_strength_comparer);
let res = swap_to_highest_pow_chain(db, config, block_validator, chain_strength_comparer, consensus_manager);
trace!(
target: LOG_TARGET,
"[handle_possible_reorg] block #{}, insert_orphans in {:.2?}, swap_to_highest in {:.2?} '{}'",
Expand All @@ -1841,6 +1847,7 @@ fn reorganize_chain<T: BlockchainBackend>(
block_validator: &dyn CandidateBlockValidator<T>,
fork_hash: HashOutput,
chain: &VecDeque<Arc<ChainBlock>>,
consensus: &ConsensusManager,
) -> Result<Vec<Arc<ChainBlock>>, ChainStorageError> {
let removed_blocks = rewind_to_hash(backend, fork_hash)?;
debug!(
Expand Down Expand Up @@ -1872,11 +1879,11 @@ fn reorganize_chain<T: BlockchainBackend>(
remove_orphan(backend, block_hash)?;

info!(target: LOG_TARGET, "Restoring previous chain after failed reorg.");
restore_reorged_chain(backend, fork_hash, removed_blocks)?;
restore_reorged_chain(backend, fork_hash, removed_blocks, consensus)?;
return Err(e.into());
}

insert_best_block(&mut txn, block.clone())?;
insert_best_block(&mut txn, block.clone(), consensus)?;
// Failed to store the block - this should typically never happen unless there is a bug in the validator
// (e.g. does not catch a double spend). In any case, we still need to restore the chain to a
// good state before returning.
Expand All @@ -1886,7 +1893,7 @@ fn reorganize_chain<T: BlockchainBackend>(
"Failed to commit reorg chain: {:?}. Restoring last chain.", e
);

restore_reorged_chain(backend, fork_hash, removed_blocks)?;
restore_reorged_chain(backend, fork_hash, removed_blocks, consensus)?;
return Err(e);
}
}
Expand All @@ -1899,6 +1906,7 @@ fn swap_to_highest_pow_chain<T: BlockchainBackend>(
config: &BlockchainDatabaseConfig,
block_validator: &dyn CandidateBlockValidator<T>,
chain_strength_comparer: &dyn ChainStrengthComparer,
consensus: &ConsensusManager,
) -> Result<BlockAddResult, ChainStorageError> {
let metadata = db.fetch_chain_metadata()?;
// lets clear out all remaining headers that dont have a matching block
Expand Down Expand Up @@ -1955,7 +1963,7 @@ fn swap_to_highest_pow_chain<T: BlockchainBackend>(
.prev_hash;

let num_added_blocks = reorg_chain.len();
let removed_blocks = reorganize_chain(db, block_validator, fork_hash, &reorg_chain)?;
let removed_blocks = reorganize_chain(db, block_validator, fork_hash, &reorg_chain, consensus)?;
let num_removed_blocks = removed_blocks.len();

// reorg is required when any blocks are removed or more than one are added
Expand Down Expand Up @@ -2007,6 +2015,7 @@ fn restore_reorged_chain<T: BlockchainBackend>(
db: &mut T,
to_hash: HashOutput,
previous_chain: Vec<Arc<ChainBlock>>,
consensus: &ConsensusManager,
) -> Result<(), ChainStorageError> {
let invalid_chain = rewind_to_hash(db, to_hash)?;
debug!(
Expand All @@ -2022,7 +2031,7 @@ fn restore_reorged_chain<T: BlockchainBackend>(

for block in previous_chain.into_iter().rev() {
txn.delete_orphan(block.accumulated_data().hash);
insert_best_block(&mut txn, block)?;
insert_best_block(&mut txn, block, consensus)?;
}
db.write(txn)?;
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub struct ConsensusConstants {
/// This is the maximum age a Monero merge mined seed can be reused
/// Monero forces a change every height mod 2048 blocks
max_randomx_seed_height: u64,
/// Monero Coinbases are unlimited in size, but we limited the extra field to only a certain bytes.
max_extra_field_size: usize,
/// This keeps track of the block split targets and which algo is accepted
/// Ideally this should count up to 100. If this does not you will reduce your target time.
proof_of_work: HashMap<PowAlgorithm, PowAlgorithmConstants>,
Expand Down Expand Up @@ -200,6 +202,11 @@ impl ConsensusConstants {
Utc::now().add(Duration::seconds(self.future_time_limit as i64))
}

/// Monero Coinbases are unlimited in size, but we limited the extra field to only a certain bytes.
pub fn max_extra_field_size(&self) -> usize {
self.max_extra_field_size
}

/// When doing difficulty adjustments and FTL calculations this is the amount of blocks we look at.
pub fn difficulty_block_window(&self) -> u64 {
self.difficulty_block_window
Expand Down Expand Up @@ -371,6 +378,7 @@ impl ConsensusConstants {
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: u64::MAX,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: ESMERALDA_FAUCET_VALUE.into(), // The esmeralda genesis block is re-used for localnet
transaction_weight: TransactionWeight::latest(),
Expand Down Expand Up @@ -433,6 +441,7 @@ impl ConsensusConstants {
emission_decay: &EMISSION_DECAY,
emission_tail: 100.into(),
max_randomx_seed_height: u64::MAX,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: 1_195_651_566_094_148.into(),
transaction_weight: TransactionWeight::v1(),
Expand Down Expand Up @@ -494,6 +503,7 @@ impl ConsensusConstants {
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: ESMERALDA_FAUCET_VALUE.into(),
transaction_weight: TransactionWeight::v1(),
Expand Down Expand Up @@ -548,6 +558,7 @@ impl ConsensusConstants {
emission_decay: &EMISSION_DECAY,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: ESMERALDA_FAUCET_VALUE.into(), // The esmeralda genesis block is re-used for stagenet
transaction_weight: TransactionWeight::v1(),
Expand Down Expand Up @@ -596,6 +607,7 @@ impl ConsensusConstants {
emission_decay: &EMISSION_DECAY,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: ESMERALDA_FAUCET_VALUE.into(), // The esmeralda genesis block is re-used for stagenet
transaction_weight: TransactionWeight::v1(),
Expand Down Expand Up @@ -646,6 +658,7 @@ impl ConsensusConstants {
emission_decay: &EMISSION_DECAY,
emission_tail: 100.into(),
max_randomx_seed_height: u64::MAX,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: MicroMinotari::from(0),
transaction_weight: TransactionWeight::v1(),
Expand Down
Loading

0 comments on commit d1bf0a4

Please sign in to comment.