Skip to content

Commit

Permalink
Merge branch 'development' into core-output-features-canonical-encoding
Browse files Browse the repository at this point in the history
* development:
  fix: add display_currency_decimal method (tari-project#3445)
  fix: add sanity checks to prepare_new_block (tari-project#3448)
  test: profile wallet sqlite database operations (tari-project#3451)
  test: create cucumber test directory if necessary (tari-project#3452)
  chore: improve cucumber tags and run time speed (tari-project#3439)
  • Loading branch information
sdbondi committed Oct 15, 2021
2 parents 95a7868 + 1f52ffc commit e7c35dd
Show file tree
Hide file tree
Showing 44 changed files with 2,245 additions and 2,506 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {

let new_block = match handler.get_new_block(block_template).await {
Ok(b) => b,
Err(CommsInterfaceError::ChainStorageError(ChainStorageError::InvalidArguments { message, .. })) => {
return Err(Status::invalid_argument(message));
},
Err(CommsInterfaceError::ChainStorageError(ChainStorageError::CannotCalculateNonTipMmr(msg))) => {
let status = Status::with_details(
tonic::Code::FailedPrecondition,
Expand Down
3 changes: 2 additions & 1 deletion applications/tari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use super::error::CommandError;
use log::*;
use std::{
convert::TryFrom,
fs::File,
io::{LineWriter, Write},
str::FromStr,
Expand Down Expand Up @@ -661,7 +662,7 @@ pub async fn command_runner(
}
if count > 0 {
let average = f64::from(sum) / count as f64;
let average = Tari::from(average / 1_000_000f64);
let average = Tari::try_from(average / 1_000_000f64)?;
println!("Average value UTXO : {}", average);
}
if let Some(max) = values.iter().max() {
Expand Down
7 changes: 6 additions & 1 deletion applications/tari_console_wallet/src/automation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use std::num::{ParseFloatError, ParseIntError};
use chrono_english::DateError;
use log::*;
use tari_app_utilities::utilities::ExitCodes;
use tari_core::transactions::{tari_amount::MicroTariError, transaction::TransactionError};
use tari_core::transactions::{
tari_amount::{MicroTariError, TariConversionError},
transaction::TransactionError,
};
use tari_wallet::{
error::{WalletError, WalletStorageError},
output_manager_service::error::OutputManagerError,
Expand All @@ -41,6 +44,8 @@ pub const LOG_TARGET: &str = "wallet::automation::error";
pub enum CommandError {
#[error("Argument error - were they in the right order?")]
Argument,
#[error("Tari value conversion error `{0}`")]
TariConversionError(#[from] TariConversionError),
#[error("Transaction service error `{0}`")]
TransactionError(#[from] TransactionError),
#[error("Transaction service error `{0}`")]
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_mining_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl MinerConfig {
Duration::from_secs(10)
}

pub fn validate_tip_timeout_sec(&self) -> Duration {
pub fn validate_tip_interval(&self) -> Duration {
Duration::from_secs(self.validate_tip_timeout_sec)
}
}
2 changes: 1 addition & 1 deletion applications/tari_mining_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async fn mining_cycle(
} else {
display_report(&report, config).await;
}
if config.mine_on_tip_only && reporting_timeout.elapsed() > config.validate_tip_timeout_sec() {
if config.mine_on_tip_only && reporting_timeout.elapsed() > config.validate_tip_interval() {
validate_tip(node_conn, report.height, bootstrap.mine_until_height).await?;
reporting_timeout = Instant::now();
}
Expand Down
15 changes: 8 additions & 7 deletions base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,37 @@ async-trait = "0.1.50"
bincode = "1.1.4"
bitflags = "1.0.4"
blake2 = "^0.9.0"
sha3 = "0.9"
bytes = "0.5"
chrono = { version = "0.4.6", features = ["serde"] }
croaring = { version = "=0.4.5", optional = true }
decimal-rs = "0.1.20"
digest = "0.9.0"
fs2 = "0.3.0"
futures = { version = "^0.3.16", features = ["async-await"] }
fs2 = "0.3.0"
hex = "0.4.2"
integer-encoding = "3.0.2"
lazy_static = "1.4.0"
lmdb-zero = "0.4.4"
log = "0.4"
monero = { version = "^0.13.0", features = ["serde_support"], optional = true }
newtype-ops = "0.1.4"
num = "0.3"
num-format = "0.4.0"
prost = "0.8.0"
prost-types = "0.8.0"
rand = "0.8"
randomx-rs = { version = "1.1.9", optional = true }
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0"
sha3 = "0.9"
strum_macros = "0.17.1"
thiserror = "1.0.26"
tokio = { version = "1.11", features = ["time", "sync", "macros"] }
tracing = "0.1.26"
tracing-attributes = "*"
tracing-futures = "*"
ttl_cache = "0.5.1"
uint = { version = "0.9", default-features = false }
num-format = "0.4.0"
tracing = "0.1.26"
tracing-futures = "*"
tracing-attributes = "*"
derive_more = "0.99.16"

[dev-dependencies]
tari_p2p = { version = "^0.11", path = "../../base_layer/p2p", features = ["test-mocks"] }
Expand Down
50 changes: 48 additions & 2 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,67 @@ where B: BlockchainBackend

pub fn prepare_new_block(&self, template: NewBlockTemplate) -> Result<Block, ChainStorageError> {
let NewBlockTemplate { header, mut body, .. } = template;
if header.height == 0 {
return Err(ChainStorageError::InvalidArguments {
func: "prepare_new_block",
arg: "template",
message: "Invalid height for NewBlockTemplate: must be greater than 0".to_string(),
});
}

body.sort(header.version);
let mut header = BlockHeader::from(header);
// If someone advanced the median timestamp such that the local time is less than the median timestamp, we need
// to increase the timestamp to be greater than the median timestamp
let height = header.height - 1;
let prev_block_height = header.height - 1;
let min_height = header.height.saturating_sub(
self.consensus_manager
.consensus_constants(header.height)
.get_median_timestamp_count() as u64,
);

let db = self.db_read_access()?;
let timestamps = fetch_headers(&*db, min_height, height)?
let tip_header = db.fetch_tip_header()?;
if header.height != tip_header.height() + 1 {
return Err(ChainStorageError::InvalidArguments {
func: "prepare_new_block",
arg: "template",
message: format!(
"Expected new block template height to be {} but was {}",
tip_header.height() + 1,
header.height
),
});
}
if header.prev_hash != *tip_header.hash() {
return Err(ChainStorageError::InvalidArguments {
func: "prepare_new_block",
arg: "template",
message: format!(
"Expected new block template previous hash to be set to the current tip hash ({}) but was {}",
tip_header.hash().to_hex(),
header.prev_hash.to_hex()
),
});
}

let timestamps = fetch_headers(&*db, min_height, prev_block_height)?
.iter()
.map(|h| h.timestamp)
.collect::<Vec<_>>();
if timestamps.is_empty() {
return Err(ChainStorageError::DataInconsistencyDetected {
function: "prepare_new_block",
details: format!(
"No timestamps were returned within heights {} - {} by the database despite the tip header height \
being {}",
min_height,
prev_block_height,
tip_header.height()
),
});
}

let median_timestamp = calc_median_timestamp(&timestamps);
if median_timestamp > header.timestamp {
header.timestamp = median_timestamp.increase(1);
Expand Down
54 changes: 51 additions & 3 deletions base_layer/core/src/chain_storage/tests/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{
blocks::Block,
chain_storage::BlockchainDatabase,
blocks::{Block, BlockHeader, NewBlockTemplate},
chain_storage::{BlockchainDatabase, ChainStorageError},
consensus::ConsensusManager,
proof_of_work::Difficulty,
tari_utilities::Hashable,
test_helpers::{
blockchain::{create_new_blockchain, TempDatabase},
create_block,
BlockSpec,
},
transactions::transaction::{Transaction, UnblindedOutput},
transactions::{
tari_amount::T,
transaction::{Transaction, UnblindedOutput},
},
};
use std::sync::Arc;
use tari_common::configuration::Network;
Expand Down Expand Up @@ -160,6 +164,12 @@ mod fetch_headers {
let db = setup();
let headers = db.fetch_headers(0..).unwrap();
assert_eq!(headers.len(), 1);
let headers = db.fetch_headers(0..0).unwrap();
assert_eq!(headers.len(), 1);
let headers = db.fetch_headers(0..=0).unwrap();
assert_eq!(headers.len(), 1);
let headers = db.fetch_headers(..).unwrap();
assert_eq!(headers.len(), 1);
}

#[test]
Expand Down Expand Up @@ -432,3 +442,41 @@ mod fetch_total_size_stats {
assert_eq!(stats.sizes().len(), 20);
}
}

mod prepare_new_block {
use super::*;

#[test]
fn it_errors_for_genesis_block() {
let db = setup();
let genesis = db.fetch_block(0).unwrap();
let template = NewBlockTemplate::from_block(genesis.block().clone(), Difficulty::min(), 5000 * T);
let err = db.prepare_new_block(template).unwrap_err();
assert!(matches!(err, ChainStorageError::InvalidArguments { .. }));
}

#[test]
fn it_errors_for_non_tip_template() {
let db = setup();
let genesis = db.fetch_block(0).unwrap();
let next_block = BlockHeader::from_previous(genesis.header());
let mut template = NewBlockTemplate::from_block(next_block.into_builder().build(), Difficulty::min(), 5000 * T);
// This would cause a panic if the sanity checks were not there
template.header.height = 100;
let err = db.prepare_new_block(template.clone()).unwrap_err();
assert!(matches!(err, ChainStorageError::InvalidArguments { .. }));
template.header.height = 1;
template.header.prev_hash[0] += 1;
let err = db.prepare_new_block(template).unwrap_err();
assert!(matches!(err, ChainStorageError::InvalidArguments { .. }));
}
#[test]
fn it_prepares_the_first_block() {
let db = setup();
let genesis = db.fetch_block(0).unwrap();
let next_block = BlockHeader::from_previous(genesis.header());
let template = NewBlockTemplate::from_block(next_block.into_builder().build(), Difficulty::min(), 5000 * T);
let block = db.prepare_new_block(template).unwrap();
assert_eq!(block.header.height, 1);
}
}
Loading

0 comments on commit e7c35dd

Please sign in to comment.