get-stacks-block-info?
Fetching information about Stacks blocks in Clarity smart contracts.
In Clarity 3, this function replaces the deprecated get-block-info?
function.
Function Signature
- Input:
prop-name
: A StacksBlockInfoPropertyNameblock-height
: A uint representing the Stacks block height
- Output:
(optional buff) | (optional uint)
depending on the property
Why it matters
The get-stacks-block-info?
function is crucial for:
- Accessing historical Stacks block data within smart contracts
- Retrieving globally unique block identifiers
- Implementing time-based logic using block information
- Verifying block-related properties for security or validation purposes
When to use it
Use get-stacks-block-info?
when you need to:
- Retrieve unique identifiers for Stacks blocks
- Access block timestamps for time-based logic
- Verify block hashes for validation purposes
- Implement logic that depends on block information
Best Practices
- Always use
id-header-hash
when global uniqueness is required - Handle the
none
case for non-existent or future blocks - Be aware of the different timestamp sources before and after epoch 3.0
- Consider caching frequently accessed block information
Practical Example: Block Hash Verification
This example demonstrates:
- Retrieving a block's unique identifier
- Handling the optional return value
- Comparing block hashes for verification
Available Properties
id-header-hash
: Returns the globally unique index block hash (buff 32)header-hash
: Returns the Stacks block's header hash (buff 32)time
: Returns the block time as Unix epoch timestamp (uint)
Common Pitfalls
- Using
header-hash
when global uniqueness is required (useid-header-hash
instead) - Not handling the
none
case for invalid or future block heights - Assuming block times are exact (accuracy varies by epoch):
- Pre-epoch 3.0: Accurate within two hours
- Post-epoch 3.0: Must be greater than previous block and at most 15 seconds in the future
- Not considering the different timestamp sources across epochs
Related Functions
get-tenure-info?
: Used to get information about block tenuresblock-height
: Returns the current block heightat-block
: Used withid-header-hash
for historical state access
Conclusion
The get-stacks-block-info?
function, introduced in Clarity 3, provides essential access to Stacks block data in smart contracts. It offers reliable ways to access block identifiers and timestamps, with important considerations for global uniqueness and time accuracy across different epochs. Understanding its properties and limitations is crucial for building robust smart contracts that interact with historical blockchain state.