Skip to content

Commit

Permalink
Merge pull request #3551 from stacks-network/feat/stackerdb-messages
Browse files Browse the repository at this point in the history
Feat/stackerdb messages
  • Loading branch information
jcnelson committed Jul 29, 2023
2 parents 1d2c57b + 5c84ee1 commit 10dc24d
Show file tree
Hide file tree
Showing 14 changed files with 3,572 additions and 886 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
will be useful for tools that use the Clarity library to analyze and
manipulate Clarity source code, e.g. a formatter.
- New RPC endpoint at /v2/constant_val to fetch a constant from a contract.
- Message definitions and codecs for Stacker DB, a replicated off-chain DB
hosted by subscribed Stacks nodes and controlled by smart contracts

### Fixed

Expand Down
17 changes: 12 additions & 5 deletions src/burnchains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,18 @@ impl PoxConstants {
/// Structure for encoding our view of the network
#[derive(Debug, PartialEq, Clone)]
pub struct BurnchainView {
pub burn_block_height: u64, // last-seen block height (at chain tip)
pub burn_block_hash: BurnchainHeaderHash, // last-seen burn block hash
pub burn_stable_block_height: u64, // latest stable block height (e.g. chain tip minus 7)
pub burn_stable_block_hash: BurnchainHeaderHash, // latest stable burn block hash
pub last_burn_block_hashes: HashMap<u64, BurnchainHeaderHash>, // map all block heights from burn_block_height back to the oldest one we'll take for considering the peer a neighbor
/// last-seen block height (at chain tip)
pub burn_block_height: u64,
/// last-seen burn block hash
pub burn_block_hash: BurnchainHeaderHash,
/// latest stable block height (e.g. chain tip minus 7)
pub burn_stable_block_height: u64,
/// latest stable burn block hash
pub burn_stable_block_hash: BurnchainHeaderHash,
/// map all block heights from burn_block_height back to the oldest one we'll take for considering the peer a neighbor
pub last_burn_block_hashes: HashMap<u64, BurnchainHeaderHash>,
/// consensus hash of the current reward cycle's start block
pub rc_consensus_hash: ConsensusHash,
}

/// The burnchain block's encoded state transition:
Expand Down
21 changes: 18 additions & 3 deletions src/chainstate/burn/db/sortdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3823,7 +3823,7 @@ impl SortitionDB {
/// Get a burn blockchain snapshot, given a burnchain configuration struct.
/// Used mainly by the network code to determine what the chain tip currently looks like.
pub fn get_burnchain_view(
conn: &DBConn,
conn: &SortitionDBConn,
burnchain: &Burnchain,
chain_tip: &BlockSnapshot,
) -> Result<BurnchainView, db_error> {
Expand Down Expand Up @@ -3895,19 +3895,34 @@ impl SortitionDB {
.unwrap_or(&burnchain.first_block_hash)
.clone();

let rc = burnchain
.block_height_to_reward_cycle(chain_tip.block_height)
.expect("FATAL: block height does not have a reward cycle");

let rc_height = burnchain.reward_cycle_to_block_height(rc);
let rc_consensus_hash = SortitionDB::get_ancestor_snapshot(
conn,
cmp::min(chain_tip.block_height, rc_height),
&chain_tip.sortition_id,
)?
.map(|sn| sn.consensus_hash)
.ok_or(db_error::NotFoundError)?;

test_debug!(
"Chain view: {},{}-{},{}",
"Chain view: {},{}-{},{},{}",
chain_tip.block_height,
chain_tip.burn_header_hash,
stable_block_height,
&burn_stable_block_hash
&burn_stable_block_hash,
&rc_consensus_hash,
);
Ok(BurnchainView {
burn_block_height: chain_tip.block_height,
burn_block_hash: chain_tip.burn_header_hash,
burn_stable_block_height: stable_block_height,
burn_stable_block_hash: burn_stable_block_hash,
last_burn_block_hashes: last_burn_block_hashes,
rc_consensus_hash,
})
}
}
Expand Down
Loading

0 comments on commit 10dc24d

Please sign in to comment.