bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 72쪽
nction"},{"inputs":[{"name":"arg0","type":"uint256"},{"name":"arg1","type":"bytes32"}],"name":"commitment_count","outputs":[{"name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"arg0","type":"address"},{"name":"arg1","type":"uint256"}],"name":"committer_votes","outputs":[{"name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"threshold","outputs":[{"name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"outputs":[],"stateMutability":"nonpayable","type":"constructor"}]
```
* * *
<a id="committers"></a>
## Committers
Committers are tracked on-chain and are required for consensus operations. Only the contract owner can modify the committer set; all users can query committer status.
<a id="managing-committers"></a>
## Managing Committers
Owner-only functions for adding or removing committers, as well as retrieving the current committer list. These operations directly affect consensus security, as only committers can submit or update block hash commitments. Exceeding the maximum committer count (32) is prevented at the contract level.
<a id="add_committer"></a>
### `add_committer`
`BlockOracle.add_committer(_committer: address, _bump_threshold: bool = False)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to add a committer.
| Input | Type | Description |
| --- | --- | --- |
| `_committer` | `address` | Address of the committer to add |
| `_bump_threshold` | `bool` | If true, automatically increase threshold by 1 |
Emits: `AddCommitter` event.
**<>Source code▼**
```vyper
event AddCommitter:
committer: indexed(address)
MAX_COMMITTERS: constant(uint256) = 32
committers: public(DynArray[address, MAX_COMMITTERS]) # List of all committers
is_committer: public(HashMap[address, bool])
threshold: public(uint256)
@external
def add_committer(_committer: address, _bump_threshold: bool = False):
"""
@notice Set trusted address that can commit block data
@param _committer Address of trusted committer
@param _bump_threshold If True, bump threshold to 1 more (useful for initial setup)
"""
ownable._check_owner()
if not self.is_committer[_committer]:
assert len(self.committers) < MAX_COMMITTERS, "Max committers reached"
self.is_committer[_committer] = True
self.committers.append(_committer)
log AddCommitter(committer=_committer)
if _bump_threshold:
self.threshold += 1
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> BlockOracle.add_committer('0x1234567890123456789012345678901234567890', False)
```
<a id="remove_committer"></a>
### `remove_committer`
`BlockOracle.remove_committer(_committer: address)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to remove a committer.
| Input | Type | Description |
| --- | --- | --- |
| `_committer` | `address` | Address of the committer to remove |
Emits: `RemoveCommitter` event.
**<>Source code▼**
```vyper
event RemoveCommitter:
committer: indexed(address)
MAX_COMMITTERS: constant(uint256) = 32
committers: public(DynArray[address, MAX_COMMITTERS]) # List of all committers
is_committer: public(HashMap[address, bool])
@external
def remove_committer(_committer: address):
"""
@notice Remove trusted address that can commit block data
@param _committer Address of trusted committer
"""
ownable._check_owner()
if self.is_committer[_committer]:
self.is_committer[_committer] = False
# Rebuild committers array excluding the removed committer
new_committers: DynArray[address, MAX_COMMITTERS] = []
for committer: address in self.committers:
if committer != _committer:
new_committers.append(committer)
self.committers = new_committers
log RemoveCommitter(committer=_committer)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> BlockOracle.remove_committer('0x1234567890123456789012345678901234567890')
```
<a id="get_all_committers"></a>
### `get_all_committers`
`BlockOracle.get_all_committers() -> DynArray[address, MAX_COMMITTERS]: view`
Getter which returns all registered committers.
Returns: array of all committers (`DynArray[address, MAX_COMMITTERS]`).
**<>Source code▼**
```vyper
MAX_COMMITTERS: constant(uint256) = 32
committers: public(DynArray[address, MAX_COMMITTERS]) # List of all committers
@view
@external
def get_all_committers() -> DynArray[address, MAX_COMMITTERS]:
"""
@notice Utility viewer that returns list of all committers
@return Array of all registered committer addresses
"""
return self.committers
```
**▶Example▼**
```shell
>>> BlockOracle.get_all_committers()
['0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2']
```
<a id="committers-1"></a>
### `committers`
`BlockOracle.committers(arg0: uint256) -> address: view`
Getter for the committers at a specific index.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `uint256` | Index of the committer |
Returns: address of the committer at the specified index (`address`).
**<>Source code▼**
```vyper
MAX_COMMITTERS: constant(uint256) = 32
committers: public(DynArray[address, MAX_COMMITTERS]) # List of all committers
```
**▶Example▼**
This example returns the committer at index 0.
```shell
>>> BlockOracle.committers(0)
'0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2'
```
<a id="is_committer"></a>
### `is_committer`
`BlockOracle.is_committer(arg0: address) -> bool: view`
Getter to check if a certain address is a committer.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `address` | Address to check |
Returns: true if the address is a committer, false otherwise (`bool`).
**<>Source code▼**
```vyper
is_committer: public(HashMap[address, bool])
```
**▶Example▼**
```shell
>>> BlockOracle.is_committer('0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2')
True
```
<a id="threshold-management"></a>
## Threshold Management
Owner-only functions for setting and querying the threshold parameter, which defines the minimum number of matching committer votes required to confirm a block hash. The threshold cannot exceed the number of registered committers.
<a id="set_threshold"></a>
### `set_threshold`
`BlockOracle.set_threshold(_new_threshold: uint256)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to update the threshold for block applications.
| Input | Type | Description |
| --- | --- | --- |
| `_new_threshold` | `uint256` | New `threshold` value |
Emits: `SetThreshold` event.
**<>Source code▼**
```vyper
event SetThreshold:
new_threshold: indexed(uint256)
threshold: public(uint256)
@external
def set_threshold(_new_threshold: uint256):
"""
@notice Update threshold for block application
@param _new_threshold New threshold value
"""
ownable._check_owner()
assert _new_threshold <= len(
self.committers
), "Threshold cannot be greater than number of committers"
self.threshold = _new_threshold
log SetThreshold(new_threshold=_new_threshold)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> BlockOracle.set_threshold(2)
```
<a id="threshold"></a>
### `threshold`
`BlockOracle.threshold() -> uint256: view`
Getter for the threshold of how many matching commitments required to confirm a block.
Returns: number of commitments required (`uint256`).
**<>Source code▼**
```vyper
threshold: public(uint256)
```
**▶Example▼**
```shell
>>> BlockOracle.threshold()
1
```
* * *
<a id="committing-block-hashes"></a>
## Committing Block Hashes
Only registered committers may call commit functions. The contract maintains a mapping of committer votes and a count of votes per block hash. Commitments are mutable until a block is confirmed; after confirmation, the block hash is immutable.
<a id="commit_block"></a>
### `commit_block`
`BlockOracle.commit_block(_block_number: uint256, _block_hash: bytes32, _apply: bool = True) -> bool`
Function to commit a block hash and optionally attempt to apply it.
| Input | Type | Description |
| --- | --- | --- |
| `_block_number` | `uint256` | Block number to commit |
| `_block_hash` | `bytes32` | The hash to commit |
| `_apply` | `bool` | `true` -> check if threshold is met and applies the block. |
Returns: true if the block was applied (`bool`).
Emits: `CommitBlock` event.
**<>Source code▼**
```vyper
@external
def commit_block(_block_number: uint256, _block_hash: bytes32, _apply: bool = True) -> bool:
"""
@notice Commit a block hash and optionally attempt to apply it
@param _block_number The block number to commit
@param _block_hash The hash to commit
@param _apply If True, checks if threshold is met and applies block
@return True if block was applied
"""
assert self.is_committer[msg.sender], "Not authorized"
assert self.block_hash[_block_number] == empty(bytes32), "Already applied"
assert _block_hash != empty(bytes32), "Invalid block hash"
previous_commitment: bytes32 = self.committer_votes[msg.sender][_block_number]
# Remove previous vote if exists, to avoid duplicate commitments
if previous_commitment != empty(bytes32):
self.commitment_count[_block_number][previous_commitment] -= 1
self.committer_votes[msg.sender][_block_number] = _block_hash
self.commitment_count[_block_number][_block_hash] += 1
log CommitBlock(committer=msg.sender, block_number=_block_number, block_hash=_block_hash)
# Optional attempt to apply block
if _apply and self.commitment_count[_block_number][_block_hash] >= self.threshold:
self._apply_block(_block_number, _block_hash)
return True
return False
```
**▶Example▼**
```shell
>>> BlockOracle.commit_block(22788903, 0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d, True)
True
```
<a id="committer_votes"></a>
### `committer_votes`
`BlockOracle.committer_votes(arg0: address, arg1: uint256) -> bytes32: view`
Getter for the committed hash by a specific committer for a specific block number.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `address` | Address of the committer |
| `arg1` | `uint256` | Block number |
Returns: committed hash for the committer and block number, or empty bytes32 if no commitment (`bytes32`).
**<>Source code▼**
```vyper
committer_votes: public(
HashMap[address, HashMap[uint256, bytes32]]
) # committer => block_number => committed_hash
```
**▶Example▼**
```shell
>>> BlockOracle.committer_votes('0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2', 22788903)
'0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d'
```
<a id="commitment_count"></a>
### `commitment_count`
`BlockOracle.commitment_count(arg0: uint256, arg1: bytes32) -> uint256: view`
Getter for the number of commitments for a specific hash at a specific block number.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `uint256` | Block number |
| `arg1` | `bytes32` | Hash to get count for |
Returns: number of commitments for the hash at the block number (`uint256`).
**<>Source code▼**
```vyper
commitment_count: public(
HashMap[uint256, HashMap[bytes32, uint256]]
) # block_number => hash => count
```
**▶Example▼**
```shell
>>> BlockOracle.commitment_count(22788903, 0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d)
1
```
* * *
<a id="block-application"></a>
## Block Application
Block application includes both permissionless (anyone can call) and owner-only (admin) application. Also provides views for querying confirmed block hashes and the most recent confirmed block number. Once applied, block hashes are immutable and serve as the canonical record for the oracle.
<a id="apply_block"></a>
### `apply_block`
`BlockOracle.apply_block(_block_number: uint256, _block_hash: bytes32)`
Function to apply a block hash if it has sufficient commitments.
| Input | Type | Description |
| --- | --- | --- |
| `_block_number` | `uint256` | Block number to apply |
| `_block_hash` | `bytes32` | The block hash to apply |
Emits: `ApplyBlock` event.
**<>Source code▼**
```vyper
event ApplyBlock:
block_number: indexed(uint256)
block_hash: bytes32
block_hash: HashMap[uint256, bytes32] # block_number => hash
last_confirmed_block_number: public(uint256) # number of the last confirmed block hash
@external
def apply_block(_block_number: uint256, _block_hash: bytes32):
"""
@notice Apply a block hash if it has sufficient commitments
@param _block_number The block number to apply
@param _block_hash The block hash to apply
"""
assert self.block_hash[_block_number] == empty(bytes32), "Already applied"
assert (
self.commitment_count[_block_number][_block_hash] >= self.threshold
), "Insufficient commitments"
self._apply_block(_block_number, _block_hash)
@internal
def _apply_block(_block_number: uint256, _block_hash: bytes32):
"""
@notice Confirm a block hash and apply it
@dev All security checks must be performed outside!
@param _block_number The block number to confirm
@param _block_hash The hash to confirm
"""
self.block_hash[_block_number] = _block_hash
if self.last_confirmed_block_number < _block_number:
self.last_confirmed_block_number = _block_number
log ApplyBlock(block_number=_block_number, block_hash=_block_hash)
```
**▶Example▼**
```shell
>>> BlockOracle.apply_block(22788903, 0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d)
```
<a id="admin_apply_block"></a>
### `admin_apply_block`
`BlockOracle.admin_apply_block(_block_number: uint256, _block_hash: bytes32)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to apply a block hash with admin rights.
| Input | Type | Description |
| --- | --- | --- |
| `_block_number` | `uint256` | Block number to apply |
| `_block_hash` | `bytes32` | Hash to apply |
Emits: `ApplyBlock` event.
**<>Source code▼**
```vyper
@external
def admin_apply_block(_block_number: uint256, _block_hash: bytes32):
"""
@notice Apply a block hash with admin rights
@param _block_number The block number to apply
@param _block_hash The hash to apply
@dev Only callable by owner
"""
ownable._check_owner()
self._apply_block(_block_number, _block_hash)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> BlockOracle.admin_apply_block(22788903, 0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d)
```
<a id="get_block_hash"></a>
### `get_block_hash`
`BlockOracle.get_block_hash(_block_number: uint256) -> bytes32: view`
Getter for the confirmed block hash for a given block number.
| Input | Type | Description |
| --- | --- | --- |
| `_block_number` | `uint256` | Block number |
Returns: confirmed block hash or empty `bytes32` if not confirmed (`bytes32`).
**<>Source code▼**
```vyper
block_hash: HashMap[uint256, bytes32] # block_number => hash
@view
@external
def get_block_hash(_block_number: uint256) -> bytes32:
"""
@notice Get the confirmed block hash for a given block number
@param _block_number The block number to query
@return The confirmed block hash, or empty bytes32 if not confirmed
"""
return self.block_hash[_block_number]
```
**▶Example▼**
```shell
>>> BlockOracle.get_block_hash(22788903)
'0xc215221221dd6673ae7ed2e50f47f6d020034657bb4a08010b5677a1f9d06d6d'
```
<a id="last_confirmed_block_number"></a>
### `last_confirmed_block_number`
`BlockOracle.last_confirmed_block_number() -> uint256: view`
Getter for the last confirmed block number.
Returns: most recently confirmed block number (`uint256`).
**<>Source code▼**
```vyper
last_confirmed_block_number: public(uint256) # number of the last confirmed block hash
```
**▶Example▼**
```shell
>>> BlockOracle.last_confirmed_block_number()
22788903
```
* * *
<a id="block-headers"></a>
## Block Headers
Functions for submitting and retrieving full block headers. Only the designated verifier contract may submit headers, which must match a previously confirmed block hash. Includes views for retrieving the state root, full header data, and the most recently confirmed header. These features enable advanced use cases such as state proofs and cross-chain verification.
<a id="submit_block_header"></a>
### `submit_block_header`
`BlockOracle.submit_block_header(_header_data: bh_rlp.BlockHeader)`
Guarded Method
This function can only be called by the designated `header_verifier` contract.
Function to submit a block header.
| Input | Type | Description |
| --- | --- | --- |
| `_header_data` | `bh_rlp.BlockHeader` | Block header to submit |
Emits: `SubmitBlockHeader` event.
**<>Source code▼**
```vyper
# Import RLP Block Header Decoder
from modules import BlockHeaderRLPDecoder as bh_rlp
block_hash: HashMap[uint256, bytes32] # block_number => hash
last_confirmed_block_number: public(uint256) # number of the last confirmed block hash
header_verifier: public(address) # address of the header verifier
block_header: public(HashMap[uint256, bh_rlp.BlockHeader]) # block_number => header
last_confirmed_header: public(bh_rlp.BlockHeader) # last confirmed header
@external
def submit_block_header(_header_data: bh_rlp.BlockHeader):
"""
@notice Submit block header. Available only to whitelisted verifier contract.
@param _header_data The block header to submit
"""
assert msg.sender == self.header_verifier, "Not authorized"
# Safety checks
assert _header_data.block_hash != empty(bytes32), "Invalid block hash"
assert self.block_hash[_header_data.block_number] != empty(bytes32), "Blockhash not applied"
assert _header_data.block_hash == self.block_hash[_header_data.block_number], "Blockhash does not match"
assert self.block_header[_header_data.block_number].block_hash == empty(bytes32), "Header already submitted"
# Store decoded header
self.block_header[_header_data.block_number] = _header_data
# Update last confirmed header if new
if _header_data.block_number > self.last_confirmed_header.block_number:
self.last_confirmed_header = _header_data
log SubmitBlockHeader(
block_number=_header_data.block_number,
block_hash=_header_data.block_hash,
)
```
**▶Example▼**
```shell
>>> BlockOracle.submit_block_header(header_data)
```
<a id="get_state_root"></a>
### `get_state_root`
`BlockOracle.get_state_root(_block_number: uint256) -> bytes32: view`
Getter for the state root for a given block number.
| Input | Type | Description |
| --- | --- | --- |
| `_block_number` | `uint256` | Block number |
Returns: state root from the block header, or empty `bytes32` if the header is not submitted (`bytes32`).
**<>Source code▼**
```vyper
@view
@external
def get_state_root(_block_number: uint256) -> bytes32:
"""
@notice Get the state root for a given block number
@param _block_number The block number to query
@return The state root from the block header, or empty bytes32 if header not submitted
"""
return self.block_header[_block_number].state_root
```
**▶Example▼**
```shell
>>> BlockOracle.get_state_roo프로젝트로 돌아가기