cargo fmt

This commit is contained in:
Jude Nelson
2021-03-08 15:37:13 -05:00
parent 982da78959
commit a84c7ac02b
5 changed files with 128 additions and 70 deletions

View File

@@ -82,8 +82,8 @@ use vm::contracts::Contract;
use vm::costs::LimitedCostTracker;
use rand::thread_rng;
use rand::RngCore;
use rand::Rng;
use rand::RngCore;
use rusqlite::{Error as sqlite_error, OptionalExtension};
@@ -747,7 +747,7 @@ impl StacksChainState {
let block_path =
StacksChainState::make_block_dir(blocks_path, consensus_hash, &block_header_hash)
.expect("FATAL: failed to create block directory");
// already freed?
let sz = StacksChainState::get_file_size(&block_path)
.expect(&format!("FATAL: failed to stat {}", &block_path));
@@ -759,10 +759,10 @@ impl StacksChainState {
let random_bytes_str = to_hex(&random_bytes);
let invalid_path = format!("{}.invalid-{}", &block_path, &random_bytes_str);
fs::copy(&block_path, &invalid_path)
.expect(&format!(
"FATAL: failed to copy '{}' to '{}'", &block_path, &invalid_path,
));
fs::copy(&block_path, &invalid_path).expect(&format!(
"FATAL: failed to copy '{}' to '{}'",
&block_path, &invalid_path,
));
// truncate the original
fs::OpenOptions::new()
@@ -2052,6 +2052,9 @@ impl StacksChainState {
let update_children_args: &[&dyn ToSql] = &[consensus_hash, anchored_block_hash];
// find all descendant orphaned microblocks, and delete the block data
/*
// NOTE: temporarily disabled so we can keep invalid microblocks around for further
// analysis
let find_orphaned_microblocks_sql = "SELECT microblock_hash FROM staging_microblocks WHERE consensus_hash = ?1 AND anchored_block_hash = ?2".to_string();
let find_orphaned_microblocks_args: &[&dyn ToSql] = &[consensus_hash, anchored_block_hash];
let orphaned_microblock_hashes = query_row_columns::<BlockHeaderHash, _>(
@@ -2061,6 +2064,7 @@ impl StacksChainState {
"microblock_hash",
)
.map_err(Error::DBError)?;
*/
// drop microblocks (this processes them)
let update_microblock_children_sql = "UPDATE staging_microblocks SET orphaned = 1, processed = 1 WHERE consensus_hash = ?1 AND anchored_block_hash = ?2".to_string();
@@ -2284,7 +2288,6 @@ impl StacksChainState {
/// Also, orphan any anchored children blocks that build off of the now-orphaned microblocks.
fn drop_staging_microblocks<'a>(
tx: &mut DBTx<'a>,
blocks_path: &String,
consensus_hash: &ConsensusHash,
anchored_block_hash: &BlockHeaderHash,
invalid_block_hash: &BlockHeaderHash,
@@ -3224,15 +3227,10 @@ impl StacksChainState {
);
let sort_handle = SortitionHandleConn::open_reader_consensus(sort_ic, consensus_hash)?;
let mainnet = self.mainnet;
let chain_id = self.chain_id;
let blocks_path = self.blocks_path.clone();
let mut block_tx = self.db_tx_begin()?;
let mainnet = self.mainnet;
let chain_id = self.chain_id;
let blocks_path = self.blocks_path.clone();
let mut block_tx = self.db_tx_begin()?;
// already in queue or already processed?
@@ -3725,12 +3723,13 @@ impl StacksChainState {
let mut candidate = StagingBlock::from_row(&row).map_err(Error::DBError)?;
// block must correspond to a valid PoX snapshot
let sn_opt = SortitionDB::get_block_snapshot_consensus(
sort_tx,
&candidate.consensus_hash,
)?;
let sn_opt =
SortitionDB::get_block_snapshot_consensus(sort_tx, &candidate.consensus_hash)?;
if sn_opt.is_none() {
info!("Block {}/{} does not correspond to a sortition", &candidate.consensus_hash, &candidate.anchored_block_hash);
info!(
"Block {}/{} does not correspond to a sortition",
&candidate.consensus_hash, &candidate.anchored_block_hash
);
to_delete.push((
candidate.consensus_hash.clone(),
candidate.anchored_block_hash.clone(),
@@ -3738,7 +3737,10 @@ impl StacksChainState {
continue;
} else if let Some(sn) = sn_opt {
if !sn.pox_valid {
info!("Block {}/{} corresponds to an invalid PoX sortition", &candidate.consensus_hash, &candidate.anchored_block_hash);
info!(
"Block {}/{} corresponds to an invalid PoX sortition",
&candidate.consensus_hash, &candidate.anchored_block_hash
);
to_delete.push((
candidate.consensus_hash.clone(),
candidate.anchored_block_hash.clone(),
@@ -4966,7 +4968,6 @@ impl StacksChainState {
);
StacksChainState::drop_staging_microblocks(
chainstate_tx.deref_mut(),
&blocks_path,
&parent_header_info.consensus_hash,
&parent_header_info.anchored_header.block_hash(),
header_hash,
@@ -6128,11 +6129,9 @@ pub mod test {
anchored_block_hash: &BlockHeaderHash,
invalid_microblock: &BlockHeaderHash,
) {
let blocks_path = chainstate.blocks_path.clone();
let mut tx = chainstate.db_tx_begin().unwrap();
StacksChainState::drop_staging_microblocks(
&mut tx,
&blocks_path,
consensus_hash,
anchored_block_hash,
invalid_microblock,

View File

@@ -744,7 +744,7 @@ impl StacksChainState {
// instantiate!
StacksChainState::instantiate_db(mainnet, chain_id, index_path)
} else {
let mut marf = StacksChainState::open_index(index_path)?;
let marf = StacksChainState::open_index(index_path)?;
// sanity check
let db_config = query_row::<DBConfig, _>(
marf.sqlite_conn(),

View File

@@ -84,7 +84,7 @@ impl From<&UnconfirmedState> for MicroblockMinerRuntime {
prev_microblock_header: unconfirmed.last_mblock.clone(),
considered: Some(considered),
num_mined: 0,
tip: unconfirmed.confirmed_chain_tip.clone()
tip: unconfirmed.confirmed_chain_tip.clone(),
}
}
}
@@ -141,9 +141,11 @@ impl<'a> StacksMicroblockBuilder<'a> {
chainstate.set_unconfirmed_dirty(true);
// find parent block's execution cost
let parent_index_hash = StacksBlockHeader::make_index_block_hash(&anchor_block_consensus_hash, &anchor_block);
let cost_so_far = StacksChainState::get_stacks_block_anchored_cost(chainstate.db(), &parent_index_hash)?
.ok_or(Error::NoSuchBlockError)?;
let parent_index_hash =
StacksBlockHeader::make_index_block_hash(&anchor_block_consensus_hash, &anchor_block);
let cost_so_far =
StacksChainState::get_stacks_block_anchored_cost(chainstate.db(), &parent_index_hash)?
.ok_or(Error::NoSuchBlockError)?;
// We need to open the chainstate _after_ any possible errors could occur, otherwise, we'd have opened
// the chainstate, but will lose the reference to the clarity_tx before the Drop handler for StacksMicroblockBuilder
@@ -156,7 +158,11 @@ impl<'a> StacksMicroblockBuilder<'a> {
&MINER_BLOCK_HEADER_HASH,
);
debug!("Begin microblock mining from {} from unconfirmed state with cost {:?}", &StacksBlockHeader::make_index_block_hash(&anchor_block_consensus_hash, &anchor_block), &cost_so_far);
debug!(
"Begin microblock mining from {} from unconfirmed state with cost {:?}",
&StacksBlockHeader::make_index_block_hash(&anchor_block_consensus_hash, &anchor_block),
&cost_so_far
);
clarity_tx.reset_cost(cost_so_far);
Ok(StacksMicroblockBuilder {
@@ -218,7 +224,14 @@ impl<'a> StacksMicroblockBuilder<'a> {
Error::NoSuchBlockError
})?;
debug!("Resume microblock mining from {} from unconfirmed state with cost {:?}", &StacksBlockHeader::make_index_block_hash(&anchored_consensus_hash, &anchored_block_hash), cost_so_far);
debug!(
"Resume microblock mining from {} from unconfirmed state with cost {:?}",
&StacksBlockHeader::make_index_block_hash(
&anchored_consensus_hash,
&anchored_block_hash
),
cost_so_far
);
clarity_tx.reset_cost(cost_so_far.clone());
Ok(StacksMicroblockBuilder {
@@ -297,7 +310,10 @@ impl<'a> StacksMicroblockBuilder<'a> {
considered.insert(tx.txid());
}
if bytes_so_far + tx_len >= MAX_EPOCH_SIZE.into() {
warn!("Adding microblock tx {} would exceed epoch data size", &tx.txid());
warn!(
"Adding microblock tx {} would exceed epoch data size",
&tx.txid()
);
return Err(Error::BlockTooBigError);
}
let quiet = !cfg!(test);
@@ -477,7 +493,17 @@ impl<'a> StacksMicroblockBuilder<'a> {
impl<'a> Drop for StacksMicroblockBuilder<'a> {
fn drop(&mut self) {
debug!("Drop StacksMicroblockBuilder on {}; {} txs considered; {} mined; cost so far: {:?}", &self.runtime.tip, self.runtime.considered.as_ref().map(|x| x.len()).unwrap_or(0), self.runtime.num_mined, &self.get_cost_so_far());
debug!(
"Drop StacksMicroblockBuilder on {}; {} txs considered; {} mined; cost so far: {:?}",
&self.runtime.tip,
self.runtime
.considered
.as_ref()
.map(|x| x.len())
.unwrap_or(0),
self.runtime.num_mined,
&self.get_cost_so_far()
);
self.clarity_tx
.take()
.expect("Attempted to reclose closed microblock builder")
@@ -907,10 +933,12 @@ impl StacksBlockBuilder {
);
info!(
"Miner: mined anchored block {} with {} txs, parent block {}, state root = {}",
"Miner: mined anchored block {} with {} txs, parent block {}, parent microblock {} ({}), state root = {}",
block.block_hash(),
block.txs.len(),
&self.header.parent_block,
&self.header.parent_microblock,
self.header.parent_microblock_sequence,
state_root_hash
);

View File

@@ -314,20 +314,23 @@ fn mine_one_microblock(
let mint_result = {
let ic = sortdb.index_conn();
let mut microblock_miner =
match StacksMicroblockBuilder::resume_unconfirmed(chainstate, &ic, &microblock_state.cost_so_far) {
Ok(x) => x,
Err(e) => {
let msg = format!(
"Failed to create a microblock miner at chaintip {}/{}: {:?}",
&microblock_state.parent_consensus_hash,
&microblock_state.parent_block_hash,
&e
);
error!("{}", msg);
return Err(e);
}
};
let mut microblock_miner = match StacksMicroblockBuilder::resume_unconfirmed(
chainstate,
&ic,
&microblock_state.cost_so_far,
) {
Ok(x) => x,
Err(e) => {
let msg = format!(
"Failed to create a microblock miner at chaintip {}/{}: {:?}",
&microblock_state.parent_consensus_hash,
&microblock_state.parent_block_hash,
&e
);
error!("{}", msg);
return Err(e);
}
};
let mblock = microblock_miner.mine_next_microblock(mempool, &microblock_state.miner_key)?;
let new_cost_so_far = microblock_miner.get_cost_so_far().expect("BUG: cannot read cost so far from miner -- indicates that the underlying Clarity Tx is somehow in use still.");
@@ -378,8 +381,11 @@ fn try_mine_microblock(
match StacksChainState::get_anchored_block_header_info(chainstate.db(), ch, bhh) {
Ok(Some(_)) => {
let parent_index_hash = StacksBlockHeader::make_index_block_hash(&ch, &bhh);
let cost_so_far = StacksChainState::get_stacks_block_anchored_cost(chainstate.db(), &parent_index_hash)?
.ok_or(NetError::NotFoundError)?;
let cost_so_far = StacksChainState::get_stacks_block_anchored_cost(
chainstate.db(),
&parent_index_hash,
)?
.ok_or(NetError::NotFoundError)?;
microblock_miner_state.replace(MicroblockMinerState {
parent_consensus_hash: ch.clone(),
parent_block_hash: bhh.clone(),
@@ -387,7 +393,7 @@ fn try_mine_microblock(
frequency: config.node.microblock_frequency,
last_mined: 0,
quantity: 0,
cost_so_far: cost_so_far
cost_so_far: cost_so_far,
});
}
Ok(None) => {

View File

@@ -1,6 +1,7 @@
use super::{
make_contract_call, make_contract_publish, make_contract_publish_microblock_only,
make_microblock, make_stacks_transfer, make_stacks_transfer_mblock_only, to_addr, ADDR_4, SK_1, SK_2,
make_microblock, make_stacks_transfer, make_stacks_transfer_mblock_only, to_addr, ADDR_4, SK_1,
SK_2,
};
use stacks::chainstate::stacks::{
db::StacksChainState, StacksAddress, StacksBlock, StacksBlockHeader, StacksPrivateKey,
@@ -38,6 +39,7 @@ use stacks::net::{
use stacks::util::hash::Hash160;
use stacks::util::hash::{bytes_to_hex, hex_bytes};
use stacks::util::{get_epoch_time_secs, sleep_ms};
use std::cmp;
use std::sync::mpsc;
use std::sync::Arc;
use std::time::{Duration, Instant};
@@ -46,7 +48,6 @@ use std::{
sync::atomic::{AtomicU64, Ordering},
};
use std::{env, thread};
use std::cmp;
use stacks::burnchains::bitcoin::address::{BitcoinAddress, BitcoinAddressType};
use stacks::burnchains::bitcoin::BitcoinNetworkType;
@@ -1584,10 +1585,10 @@ fn size_overflow_unconfirmed_microblocks_integration_test() {
for _i in 0..(1024 * 1024 + 500) {
giant_contract.push_str(" ");
}
// small-sized contracts for microblocks
let mut small_contract = "(define-public (f) (ok 1))".to_string();
for _i in 0..((1024 * 1024 + 500) / 5) {
for _i in 0..((1024 * 1024 + 500) / 5) {
small_contract.push_str(" ");
}
@@ -1596,18 +1597,30 @@ fn size_overflow_unconfirmed_microblocks_integration_test() {
.map(|_| StacksPrivateKey::new())
.collect();
let spender_addrs: Vec<PrincipalData> = spender_sks.iter().map(|x| to_addr(x).into()).collect();
let txs: Vec<Vec<_>> = spender_sks
.iter()
.enumerate()
.map(|(ix, spender_sk)| {
if ix % 2 == 0 {
// almost fills a whole block
vec![make_contract_publish(spender_sk, 0, 1049230, "large-0", &giant_contract)]
vec![make_contract_publish(
spender_sk,
0,
1049230,
"large-0",
&giant_contract,
)]
} else {
let mut ret = vec![];
for i in 0..25 {
let tx = make_contract_publish_microblock_only(spender_sk, i as u64, 210000, &format!("small-{}", i), &small_contract);
let tx = make_contract_publish_microblock_only(
spender_sk,
i as u64,
210000,
&format!("small-{}", i),
&small_contract,
);
ret.push(tx);
}
ret
@@ -1711,7 +1724,7 @@ fn size_overflow_unconfirmed_microblocks_integration_test() {
mblock_nonces += res.nonce;
max_mblock_nonce = cmp::max(res.nonce, max_mblock_nonce);
}
}
}
debug!("Spender {},{}: {:?}", ix, &spender_addr, &res);
}
@@ -1722,7 +1735,7 @@ fn size_overflow_unconfirmed_microblocks_integration_test() {
);
assert_eq!(anchor_block_txs, 2);
// accept only 9 "small" txs
assert_eq!(mblock_nonces, 9);
@@ -1807,7 +1820,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
(begin
(crash-me))
";
let small_contract = "
(define-map data-map { input: uint } { output: (buff 1024) })
(define-private (folder (idx uint) (data (buff 1024)))
@@ -1847,24 +1860,36 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
(begin
(crash-me))
";
let spender_sks: Vec<_> = (0..10)
.into_iter()
.map(|_| StacksPrivateKey::new())
.collect();
let spender_addrs: Vec<PrincipalData> = spender_sks.iter().map(|x| to_addr(x).into()).collect();
let txs: Vec<Vec<_>> = spender_sks
.iter()
.enumerate()
.map(|(ix, spender_sk)| {
if ix % 2 == 0 {
// almost fills a whole block
vec![make_contract_publish(spender_sk, 0, 1049230, "large-0", &giant_contract)]
vec![make_contract_publish(
spender_sk,
0,
1049230,
"large-0",
&giant_contract,
)]
} else {
let mut ret = vec![];
for i in 0..25 {
let tx = make_contract_publish_microblock_only(spender_sk, i as u64, 210000, &format!("small-{}", i), &small_contract);
let tx = make_contract_publish_microblock_only(
spender_sk,
i as u64,
210000,
&format!("small-{}", i),
&small_contract,
);
ret.push(tx);
}
ret
@@ -1882,7 +1907,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
}
conf.node.mine_microblocks = true;
conf.node.wait_time_for_microblocks = 5000;
conf.node.wait_time_for_microblocks = 0;
conf.node.microblock_frequency = 15000;
let mut btcd_controller = BitcoinCoreController::new(conf.clone());
@@ -1936,7 +1961,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
}
}
sleep_ms(75_000);
sleep_ms(2_000_000);
// now let's mine a couple blocks, and then check the sender's nonce.
// at the end of mining three blocks, there should be _two_ transactions from the microblock
@@ -1948,7 +1973,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
// this one will contain the sortition from above anchor block,
// which *should* have also confirmed the microblock.
sleep_ms(75_000);
sleep_ms(2_000_000);
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
@@ -1968,7 +1993,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
mblock_nonces += res.nonce;
max_mblock_nonce = cmp::max(res.nonce, max_mblock_nonce);
}
}
}
debug!("Spender {},{}: {:?}", ix, &spender_addr, &res);
}
@@ -1979,7 +2004,7 @@ fn cost_overflow_unconfirmed_microblocks_integration_test() {
);
assert_eq!(anchor_block_txs, 2);
// accept only 9 "small" txs
assert_eq!(mblock_nonces, 9);
@@ -2065,9 +2090,9 @@ fn block_replay_integration_test() {
submit_tx(&http_origin, &tx);
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
// try and push the mined block back at the node lots of times
@@ -2085,7 +2110,7 @@ fn block_replay_integration_test() {
.unwrap()
.text()
.unwrap();
eprintln!("{}: text of {}\n{}", i, &path, &res_text);
}