Skip to content

Perf: descendancy check #6038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
### Changed

- Reduce the default `block_rejection_timeout_steps` configuration so that miners will retry faster when blocks fail to reach 70% approved or 30% rejected.
- Added index for `next_ready_nakamoto_block()` which improves block processing performance.

## [3.1.0.0.8]

Expand Down
43 changes: 43 additions & 0 deletions stackslib/src/chainstate/burn/db/sortdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::io::{ErrorKind, Write};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::sync::{Arc, LazyLock, Mutex};
use std::{cmp, fmt, fs};

use clarity::util::lru_cache::LruCache;
use clarity::vm::ast::ASTRules;
use clarity::vm::costs::ExecutionCost;
use clarity::vm::representations::{ClarityName, ContractName};
Expand Down Expand Up @@ -95,6 +98,9 @@ pub const REWARD_WINDOW_END: u64 = 144 * 90 + REWARD_WINDOW_START;

pub type BlockHeaderCache = HashMap<ConsensusHash, (Option<BlockHeaderHash>, ConsensusHash)>;

static DESCENDANCY_CACHE: LazyLock<Arc<Mutex<LruCache<(SortitionId, BlockHeaderHash), bool>>>> =
LazyLock::new(|| Arc::new(Mutex::new(LruCache::new(2000))));

pub enum FindIter<R> {
Found(R),
Continue,
Expand Down Expand Up @@ -1112,12 +1118,46 @@ pub trait SortitionHandle {
test_debug!("No snapshot at height {}", block_at_burn_height);
db_error::NotFoundError
})?;
let top_sortition_id = sn.sortition_id;

let mut cache = DESCENDANCY_CACHE
.lock()
.expect("FATAL: lock poisoned in SortitionDB");

while sn.block_height >= earliest_block_height {
match cache.get(&(sn.sortition_id, potential_ancestor.clone())) {
Ok(Some(result)) => {
if sn.sortition_id != top_sortition_id {
if let Err(_) = cache
.insert_clean((top_sortition_id, potential_ancestor.clone()), result)
{
*cache = LruCache::new(2000);
}
}
return Ok(result);
}
// not cached, don't need to do anything.
Ok(None) => {}
// cache is broken, create a new one
Err(_) => {
*cache = LruCache::new(2000);
}
}

if !sn.sortition {
if let Err(_) =
cache.insert_clean((top_sortition_id, potential_ancestor.clone()), false)
{
*cache = LruCache::new(2000);
}
return Ok(false);
}
if &sn.winning_stacks_block_hash == potential_ancestor {
if let Err(_) =
cache.insert_clean((top_sortition_id, potential_ancestor.clone()), true)
{
*cache = LruCache::new(2000);
}
return Ok(true);
}

Expand Down Expand Up @@ -1153,6 +1193,9 @@ pub trait SortitionHandle {
}
}
}
if let Err(_) = cache.insert_clean((top_sortition_id, potential_ancestor.clone()), false) {
*cache = LruCache::new(2000);
}
return Ok(false);
}
}
Expand Down
16 changes: 15 additions & 1 deletion stackslib/src/chainstate/nakamoto/staging_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ pub const NAKAMOTO_STAGING_DB_SCHEMA_3: &[&str] = &[
r#"UPDATE db_version SET version = 3"#,
];

pub const NAKAMOTO_STAGING_DB_SCHEMA_LATEST: u32 = 3;
pub const NAKAMOTO_STAGING_DB_SCHEMA_4: &[&str] = &[
r#"CREATE INDEX nakamoto_staging_blocks_by_ready_and_height ON nakamoto_staging_blocks(burn_attachable, orphaned, processed, height);"#,
r#"UPDATE db_version SET version = 4"#,
];

pub const NAKAMOTO_STAGING_DB_SCHEMA_LATEST: u32 = 4;

pub struct NakamotoStagingBlocksConn(rusqlite::Connection);

Expand Down Expand Up @@ -796,6 +801,15 @@ impl StacksChainState {
assert_eq!(version, 3, "Nakamoto staging DB migration failure");
debug!("Migrated Nakamoto staging blocks DB to schema 3");
}
3 => {
debug!("Migrate Nakamoto staging blocks DB to schema 3");
for cmd in NAKAMOTO_STAGING_DB_SCHEMA_4.iter() {
conn.execute(cmd, NO_PARAMS)?;
}
let version = Self::get_nakamoto_staging_blocks_db_version(conn)?;
assert_eq!(version, 4, "Nakamoto staging DB migration failure");
debug!("Migrated Nakamoto staging blocks DB to schema 3");
}
NAKAMOTO_STAGING_DB_SCHEMA_LATEST => {
break;
}
Expand Down
Loading