Skip to content

Commit

Permalink
feat: allow freezing blobstreamx (#113)
Browse files Browse the repository at this point in the history
* Added frozen functionality

---------

Co-authored-by: Brandon R <[email protected]>
  • Loading branch information
thomas192 and b-j-roberts committed Mar 12, 2024
1 parent 7d88b37 commit ae194ea
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 99 deletions.
18 changes: 17 additions & 1 deletion src/blobstreamx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod blobstreamx {
block_height_to_header_hash: LegacyMap::<u64, u256>,
header_range_function_id: u256,
next_header_function_id: u256,
frozen: bool,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
Expand Down Expand Up @@ -100,7 +101,8 @@ mod blobstreamx {
}

mod Errors {
const DataCommitmentNotFound: felt252 = 'bad data commitment for range';
const DataCommitmentNotFound: felt252 = 'Bad data commitment for range';
const ContractFrozen: felt252 = 'Contract is frozen';
}

#[constructor]
Expand All @@ -121,6 +123,7 @@ mod blobstreamx {
self.block_height_to_header_hash.write(height, header);
self.header_range_function_id.write(header_range_function_id);
self.next_header_function_id.write(next_header_function_id);
self.frozen.write(false);
}

#[abi(embed_v0)]
Expand All @@ -136,6 +139,8 @@ mod blobstreamx {
fn verify_attestation(
self: @ContractState, proof_nonce: u64, root: DataRoot, proof: BinaryMerkleProof
) -> bool {
assert(!self.frozen.read(), Errors::ContractFrozen);

if (proof_nonce >= self.state_proof_nonce.read()) {
return false;
}
Expand Down Expand Up @@ -198,6 +203,13 @@ mod blobstreamx {
self.ownable.assert_only_owner();
self.next_header_function_id.write(_function_id);
}
fn get_frozen(self: @ContractState) -> bool {
self.frozen.read()
}
fn set_frozen(ref self: ContractState, _frozen: bool) {
self.ownable.assert_only_owner();
self.frozen.write(_frozen);
}

/// Request a header_range proof for the next header hash and a data commitment for the block range [latest_block, _target_block).
/// Used to skip from the latest block to the target block.
Expand Down Expand Up @@ -251,6 +263,8 @@ mod blobstreamx {
///
/// * `_target_block` - The end block of the header range request
fn commit_header_range(ref self: ContractState, _target_block: u64) {
assert(!self.frozen.read(), Errors::ContractFrozen);

let latest_block = self.get_latest_block();
let trusted_header = self.block_height_to_header_hash.read(latest_block);
assert(trusted_header != 0, TendermintXErrors::TrustedHeaderNotFound);
Expand Down Expand Up @@ -330,6 +344,8 @@ mod blobstreamx {
///
/// * `_trusted_block` - The latest block when the request was made.
fn commit_next_header(ref self: ContractState, _trusted_block: u64) {
assert(!self.frozen.read(), Errors::ContractFrozen);

let trusted_header = self.block_height_to_header_hash.read(_trusted_block);
assert(trusted_header != 0, TendermintXErrors::TrustedHeaderNotFound);

Expand Down
9 changes: 6 additions & 3 deletions src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,21 @@ trait IBlobstreamX<TContractState> {
/// Max num of blocks that can be skipped in a single request
/// ref: https://github.com/celestiaorg/celestia-core/blob/main/pkg/consts/consts.go#L43-L44
fn data_commitment_max(self: @TContractState) -> u64;
// Address of the gateway contract
// Address of the gateway contract.
fn set_gateway(ref self: TContractState, new_gateway: ContractAddress);
fn get_gateway(self: @TContractState) -> ContractAddress;
// Nonce for proof events. Must be incremented sequentially
fn get_state_proof_nonce(self: @TContractState) -> u64;
// Header range function id
// Header range function id.
fn get_header_range_id(self: @TContractState) -> u256;
fn set_header_range_id(ref self: TContractState, _function_id: u256);
// Next header function id.
fn get_next_header_id(self: @TContractState) -> u256;
fn set_next_header_id(ref self: TContractState, _function_id: u256);
// Prove the validity of the header at the target block and a data commitment for the block range [latestBlock, _targetBlock)
// Contract freezing state.
fn get_frozen(self: @TContractState) -> bool;
fn set_frozen(ref self: TContractState, _frozen: bool);
// Prove the validity of the header at the target block and a data commitment for the block range [latestBlock, _targetBlock).
fn request_header_range(ref self: TContractState, _target_block: u64);
// Commits the new header at targetBlock and the data commitment for the block range [trustedBlock, targetBlock).
fn commit_header_range(ref self: TContractState, _target_block: u64);
Expand Down
Loading

0 comments on commit ae194ea

Please sign in to comment.