Skip to content

Commit

Permalink
feat: add target-chain-height to commit-block
Browse files Browse the repository at this point in the history
This allows us to check if the target block is anywhere in the past, not
just the last block. That means if the block commit misses its target
block, but lands in a different block that is still in the same fork as
the target block, then the block commit can be successful.
  • Loading branch information
obycode committed Jun 21, 2023
1 parent cd3fa9f commit b702561
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core-contracts/contracts/subnet.clar
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
(define-constant ERR_VALIDATION_FAILED 10)
;;; The value supplied for `target-chain-tip` does not match the current chain tip.
(define-constant ERR_INVALID_CHAIN_TIP 11)
;;; The contract was called before reaching this-chain height reaches 1.
(define-constant ERR_CALLED_TOO_EARLY 12)
;;; The target block height is beyond the current chain tip.
(define-constant ERR_INVALID_CHAIN_HEIGHT 12)
(define-constant ERR_MINT_FAILED 13)
(define-constant ERR_ATTEMPT_TO_TRANSFER_ZERO_AMOUNT 14)
(define-constant ERR_IN_COMPUTATION 15)
Expand Down Expand Up @@ -127,15 +127,19 @@
;; Helper function: determines whether the commit-block operation satisfies pre-conditions
;; listed in `commit-block`.
;; Returns response<bool, int>
(define-private (can-commit-block? (l1-block-height uint) (target-chain-tip (buff 32)))
(define-private (can-commit-block?
(l1-block-height uint)
(target-chain-tip (buff 32))
(target-chain-height uint)
)
(begin
;; check no block has been committed at this height
(asserts! (is-none (map-get? block-commits l1-block-height)) (err ERR_BLOCK_ALREADY_COMMITTED))

;; check that `target-chain-tip` matches the burn chain tip
;; check that `target-chain-tip` matches the block at the specified height
(asserts! (is-eq
target-chain-tip
(unwrap! (get-block-info? id-header-hash (- block-height u1)) (err ERR_CALLED_TOO_EARLY)) )
(unwrap! (get-block-info? id-header-hash target-chain-height) (err ERR_INVALID_CHAIN_HEIGHT)))
(err ERR_INVALID_CHAIN_TIP))

;; check that the tx sender is one of the miners
Expand Down Expand Up @@ -183,10 +187,11 @@
(block (buff 32))
(subnet-block-height uint)
(target-chain-tip (buff 32))
(target-chain-heght uint)
(withdrawal-root (buff 32))
)
(let ((l1-block-height block-height))
(try! (can-commit-block? l1-block-height target-chain-tip))
(try! (can-commit-block? l1-block-height target-chain-tip target-chain-heght))
(inner-commit-block block subnet-block-height l1-block-height withdrawal-root)
)
)
Expand Down
16 changes: 16 additions & 0 deletions testnet/stacks-node/src/burnchains/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub trait Layer1Committer {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
signatures: Vec<ClaritySignature>,
attempt: u64,
Expand Down Expand Up @@ -235,6 +236,7 @@ impl MultiPartyCommitter {
commit_to: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_root: Sha512Trunc256Sum,
signatures: Vec<ClaritySignature>,
) -> Result<StacksTransaction, Error> {
Expand All @@ -253,6 +255,7 @@ impl MultiPartyCommitter {
let height_val = ClarityValue::UInt(committed_block_height.into());
let target_tip_val = ClarityValue::buff_from(target_tip.as_bytes().to_vec())
.map_err(|_| Error::BadCommitment)?;
let target_height_val = ClarityValue::UInt(target_height.into());
let withdrawal_root_val = ClarityValue::buff_from(withdrawal_root.as_bytes().to_vec())
.map_err(|_| Error::BadCommitment)?;
let signatures_val = ClarityValue::cons_list(
Expand All @@ -272,6 +275,7 @@ impl MultiPartyCommitter {
("subnet-block-height".into(), height_val),
("withdrawal-root".into(), withdrawal_root_val),
("target-tip".into(), target_tip_val),
("target-height".into(), target_height_val),
])
.map_err(|_| Error::BadCommitment)?;

Expand Down Expand Up @@ -308,6 +312,7 @@ impl MultiPartyCommitter {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
signatures: Vec<ClaritySignature>,
attempt: u64,
Expand All @@ -334,6 +339,7 @@ impl MultiPartyCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
signatures.clone(),
)
Expand All @@ -357,6 +363,7 @@ impl MultiPartyCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
signatures,
)
Expand Down Expand Up @@ -438,6 +445,7 @@ impl Layer1Committer for MultiPartyCommitter {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
signatures: Vec<ClaritySignature>,
attempt: u64,
Expand All @@ -447,6 +455,7 @@ impl Layer1Committer for MultiPartyCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
signatures,
attempt,
Expand All @@ -465,6 +474,7 @@ impl Layer1Committer for DirectCommitter {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
_signatures: Vec<ClaritySignature>,
attempt: u64,
Expand All @@ -474,6 +484,7 @@ impl Layer1Committer for DirectCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
attempt,
op_signer,
Expand All @@ -498,6 +509,7 @@ impl DirectCommitter {
commit_to: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_root: Sha512Trunc256Sum,
) -> Result<StacksTransaction, Error> {
let QualifiedContractIdentifier {
Expand All @@ -520,6 +532,7 @@ impl DirectCommitter {
ClarityValue::buff_from(committed_block).map_err(|_| Error::BadCommitment)?,
ClarityValue::UInt(committed_block_height.into()),
ClarityValue::buff_from(target_tip_bytes).map_err(|_| Error::BadCommitment)?,
ClarityValue::UInt(target_height.into()),
ClarityValue::buff_from(withdrawal_root_bytes).map_err(|_| Error::BadCommitment)?,
],
};
Expand Down Expand Up @@ -550,6 +563,7 @@ impl DirectCommitter {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
attempt: u64,
op_signer: &mut BurnchainOpSigner,
Expand All @@ -575,6 +589,7 @@ impl DirectCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
)
.map_err(|e| {
Expand All @@ -597,6 +612,7 @@ impl DirectCommitter {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
)
.map_err(|e| {
Expand Down
2 changes: 2 additions & 0 deletions testnet/stacks-node/src/burnchains/l1_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ impl BurnchainController for L1Controller {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_tip: BurnchainHeaderHash,
target_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
signatures: Vec<super::ClaritySignature>,
op_signer: &mut BurnchainOpSigner,
Expand All @@ -467,6 +468,7 @@ impl BurnchainController for L1Controller {
committed_block_hash,
committed_block_height,
target_tip,
target_height,
withdrawal_merkle_root,
signatures,
attempt,
Expand Down
1 change: 1 addition & 0 deletions testnet/stacks-node/src/burnchains/mock_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ impl BurnchainController for MockController {
committed_block_hash: BlockHeaderHash,
_committed_block_height: u64,
_target_block: BurnchainHeaderHash,
_target_block_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
_signatures: Vec<ClaritySignature>,
_op_signer: &mut BurnchainOpSigner,
Expand Down
2 changes: 2 additions & 0 deletions testnet/stacks-node/src/burnchains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub trait BurnchainController {
committed_block_hash: BlockHeaderHash,
committed_block_height: u64,
target_burn_chain: BurnchainHeaderHash,
target_burn_block_height: u64,
withdrawal_merkle_root: Sha512Trunc256Sum,
signatures: Vec<ClaritySignature>,
op_signer: &mut BurnchainOpSigner,
Expand Down Expand Up @@ -224,6 +225,7 @@ impl BurnchainController for PanicController {
_committed_block_hash: BlockHeaderHash,
_committed_block_height: u64,
_target_block: BurnchainHeaderHash,
_target_block_height: u64,
_withdrawal_merkle_root: Sha512Trunc256Sum,
_signatures: Vec<ClaritySignature>,
_op_signer: &mut BurnchainOpSigner,
Expand Down
2 changes: 2 additions & 0 deletions testnet/stacks-node/src/neon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,7 @@ impl StacksNode {
// collect required contents for commit
let committed_block_hash = anchored_block.block_hash();
let target_burn_hash = burn_block.burn_header_hash.clone();
let target_burn_height = burn_block.block_height;
let withdrawal_merkle_root = anchored_block.header.withdrawal_merkle_root;

let mut op_signer = keychain.generate_op_signer();
Expand Down Expand Up @@ -2005,6 +2006,7 @@ impl StacksNode {
committed_block_hash,
block_height,
target_burn_hash,
target_burn_height,
withdrawal_merkle_root,
signatures,
&mut op_signer,
Expand Down

0 comments on commit b702561

Please sign in to comment.