0x1::Block
Block module provide metadata for generated blocks.
BlockMetadata
NewBlockEvent
initialize
get_current_block_number
get_parent_hash
get_current_author
process_block_metadata
use 0x1::CoreAddresses;
use 0x1::Errors;
use 0x1::Event;
use 0x1::Timestamp;
BlockMetadata
Block metadata struct.
struct BlockMetadata has key
number: u64
parent_hash: vector<u8>
author: address
uncles: u64
new_block_events: Event::EventHandle<Block::NewBlockEvent>
NewBlockEvent
Events emitted when new block generated.
struct NewBlockEvent has drop, store
number: u64
author: address
timestamp: u64
uncles: u64
const EBLOCK_NUMBER_MISMATCH: u64 = 17;
initialize
This can only be invoked by the GENESIS_ACCOUNT at genesis
public fun initialize(account: &signer, parent_hash: vector<u8>)
public fun initialize(account: &signer, parent_hash: vector<u8>) {
Timestamp::assert_genesis();
CoreAddresses::assert_genesis_address(account);
move_to<BlockMetadata>(
account,
BlockMetadata {
number: 0,
parent_hash: parent_hash,
author: CoreAddresses::GENESIS_ADDRESS(),
uncles: 0,
new_block_events: Event::new_event_handle<Self::NewBlockEvent>(account),
});
}
aborts_if !Timestamp::is_genesis();
aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS();
aborts_if exists<BlockMetadata>(Signer::address_of(account));
get_current_block_number
Get the current block number
public fun get_current_block_number(): u64
public fun get_current_block_number(): u64 acquires BlockMetadata {
borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).number
}
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
get_parent_hash
Get the hash of the parent block.
public fun get_parent_hash(): vector<u8>
public fun get_parent_hash(): vector<u8> acquires BlockMetadata {
*&borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).parent_hash
}
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
get_current_author
Gets the address of the author of the current block
public fun get_current_author(): address
public fun get_current_author(): address acquires BlockMetadata {
borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).author
}
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
process_block_metadata
Call at block prologue
public fun process_block_metadata(account: &signer, parent_hash: vector<u8>, author: address, timestamp: u64, uncles: u64, number: u64)
public fun process_block_metadata(account: &signer, parent_hash: vector<u8>,author: address, timestamp: u64, uncles:u64, number:u64) acquires BlockMetadata{
CoreAddresses::assert_genesis_address(account);
let block_metadata_ref = borrow_global_mut<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS());
assert!(number == (block_metadata_ref.number + 1), Errors::invalid_argument(EBLOCK_NUMBER_MISMATCH));
block_metadata_ref.number = number;
block_metadata_ref.author= author;
block_metadata_ref.parent_hash = parent_hash;
block_metadata_ref.uncles = uncles;
Event::emit_event<NewBlockEvent>(
&mut block_metadata_ref.new_block_events,
NewBlockEvent {
number: number,
author: author,
timestamp: timestamp,
uncles: uncles,
}
);
}
aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS();
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if number != global<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS()).number + 1;
schema AbortsIfBlockMetadataNotExist {
aborts_if !exists<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS());
}
pragma verify;
pragma aborts_if_is_strict = true;