Skip to content

Commit

Permalink
remove add inputs
Browse files Browse the repository at this point in the history
add further checks
redo audit
  • Loading branch information
SWvheerden committed Apr 28, 2021
1 parent f1fe004 commit 7ee4edc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {

let mut prev_mmr = 0;
let mut prev_kernel_mmr = 0;
let prev_chain_meta = self.shared.db.get_chain_metadata().await?;
for h in 0..=header.height() {
let curr_header = self.db().fetch_chain_header(h).await?;

Expand Down Expand Up @@ -594,6 +595,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
header.height(),
header.hash().clone(),
header.accumulated_data.total_accumulated_difficulty,
prev_chain_meta.best_block().clone(),
)
.set_pruned_height(header.height(), pruned_kernel_sum, pruned_utxo_sum)
.commit()
Expand Down
12 changes: 2 additions & 10 deletions base_layer/core/src/base_node/sync/block_sync/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
.ok_or_else(|| {
BlockSyncError::ReceivedInvalidBlockBody("Peer sent hash for block header we do not have".into())
})?;
let prev_chain_meta = self.db.get_chain_metadata().await?;

let header_hash = header.hash().clone();

Expand Down Expand Up @@ -219,6 +220,7 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
block.height(),
header_hash,
block.accumulated_data.total_accumulated_difficulty,
prev_chain_meta.best_block().clone(),
)
.commit()
.await?;
Expand All @@ -242,16 +244,6 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
}

if let Some(block) = current_block {
// Update metadata to last tip header
let header = &block.block.header;
let height = header.height;
let best_block = header.hash();
let accumulated_difficulty = block.accumulated_data.total_accumulated_difficulty;
self.db
.write_transaction()
.set_best_block(height, best_block.to_vec(), accumulated_difficulty)
.commit()
.await?;

self.hooks.call_on_complete_hooks(block);
}
Expand Down
11 changes: 9 additions & 2 deletions base_layer/core/src/chain_storage/async_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,15 @@ impl<'a, B: BlockchainBackend + 'static> AsyncDbTransaction<'a, B> {
}
}

pub fn set_best_block(&mut self, height: u64, hash: HashOutput, accumulated_data: u128) -> &mut Self {
self.transaction.set_best_block(height, hash, accumulated_data);
pub fn set_best_block(
&mut self,
height: u64,
hash: HashOutput,
accumulated_data: u128,
from: HashOutput,
) -> &mut Self
{
self.transaction.set_best_block(height, hash, accumulated_data, from);
self
}

Expand Down
4 changes: 3 additions & 1 deletion base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,9 +1183,10 @@ fn insert_block(txn: &mut DbTransaction, block: Arc<ChainBlock>) -> Result<(), C

let height = block.height();
let accumulated_difficulty = block.accumulated_data.total_accumulated_difficulty;
let prev_hash = block.block.header.prev_hash.clone();
txn.insert_header(block.block.header.clone(), block.accumulated_data.clone())
.insert_block_body(block)
.set_best_block(height, block_hash, accumulated_difficulty);
.set_best_block(height, block_hash, accumulated_difficulty, prev_hash);

Ok(())
}
Expand Down Expand Up @@ -1540,6 +1541,7 @@ fn rewind_to_height<T: BlockchainBackend>(
last_header.height,
header_accumulated_data.hash.clone(),
header_accumulated_data.total_accumulated_difficulty,
metadata.best_block().clone(),
);
db.write(txn)?;

Expand Down
21 changes: 11 additions & 10 deletions base_layer/core/src/chain_storage/db_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,6 @@ impl DbTransaction {
self
}

pub fn insert_input(&mut self, input: TransactionInput, header_hash: HashOutput, mmr_leaf_index: u32) -> &mut Self {
self.operations.push(WriteOperation::InsertInput {
header_hash,
input: Box::new(input),
mmr_position: mmr_leaf_index,
});
self
}

pub fn update_pruned_hash_set(
&mut self,
mmr_tree: MmrTree,
Expand Down Expand Up @@ -211,11 +202,19 @@ impl DbTransaction {
self
}

pub fn set_best_block(&mut self, height: u64, hash: HashOutput, accumulated_difficulty: u128) -> &mut Self {
pub fn set_best_block(
&mut self,
height: u64,
hash: HashOutput,
accumulated_difficulty: u128,
from: HashOutput,
) -> &mut Self
{
self.operations.push(WriteOperation::SetBestBlock {
height,
hash,
accumulated_difficulty,
from,
});
self
}
Expand Down Expand Up @@ -310,6 +309,7 @@ pub enum WriteOperation {
height: u64,
hash: HashOutput,
accumulated_difficulty: u128,
from: HashOutput,
},
SetPruningHorizonConfig(u64),
SetPrunedHeight {
Expand Down Expand Up @@ -415,6 +415,7 @@ impl fmt::Display for WriteOperation {
height,
hash,
accumulated_difficulty,
from: _,
} => write!(
f,
"Update best block to height:{} ({}) with difficulty: {}",
Expand Down
30 changes: 29 additions & 1 deletion base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ impl LMDBDatabase {
lmdb_delete(&write_txn, &self.orphan_chain_tips_db, &hash)?;
},
InsertOrphanChainTip(hash) => {
lmdb_replace(&write_txn, &self.orphan_chain_tips_db, &hash, &hash)?;
lmdb_insert(
&write_txn,
&self.orphan_chain_tips_db,
&hash,
&hash,
"orphan_chain_tips_db",
)?;
},
DeleteBlock(hash) => {
self.delete_block_body(&write_txn, hash)?;
Expand Down Expand Up @@ -261,7 +267,29 @@ impl LMDBDatabase {
height,
hash,
accumulated_difficulty,
from,
} => {
// for security we check that the best block does exist, and we check the previous value
// we dont want to check this if the prev block has never been set, this means a empty hash of 32
// bytes.
if height > 0 {
let prev = fetch_best_block(&write_txn, &self.metadata_db)?;
if from != prev {
return Err(ChainStorageError::InvalidOperation(format!(
"There was a change in best_block, the best block is suppose to be: ({}), but it \
currently is: ({})",
from.to_hex(),
prev.to_hex(),
)));
};
}
if !lmdb_exists(&write_txn, &self.block_hashes_db, hash.as_slice())? {
// we dont care about the header or the height, we just want to know its there.
return Err(ChainStorageError::InvalidOperation(format!(
"There is no Blockheader hash ({}) in db",
from.to_hex(),
)));
};
self.set_metadata(&write_txn, MetadataKey::ChainHeight, MetadataValue::ChainHeight(height))?;
self.set_metadata(&write_txn, MetadataKey::BestBlock, MetadataValue::BestBlock(hash))?;
self.set_metadata(
Expand Down
5 changes: 0 additions & 5 deletions base_layer/core/tests/node_comms_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,12 @@ async fn inbound_fetch_txos() {
txn.insert_utxo(utxo.clone(), header_hash.clone(), 6000);
txn.insert_utxo(stxo.clone(), header_hash.clone(), 6001);
assert!(store.commit(txn).is_ok());
let mut txn = DbTransaction::new();
txn.insert_input(stxo.clone().into(), header_hash.clone(), 1);
assert!(store.commit(txn).is_ok());

if let Ok(NodeCommsResponse::TransactionOutputs(received_txos)) = inbound_nch
.handle_request(NodeCommsRequest::FetchMatchingTxos(vec![utxo_hash, stxo_hash]))
.await
{
assert_eq!(received_txos.len(), 2);
assert_eq!(received_txos[0], utxo);
assert_eq!(received_txos[1], stxo);
} else {
panic!();
}
Expand Down

0 comments on commit 7ee4edc

Please sign in to comment.