chain_id - [mainnet]
The chain id distinguishes between different chains (e.g., testnet and the main network). One important role is to prevent transactions intended for one chain from being executed on another. This code provides a container for storing a chain id and functions to initialize and get it.
use 0x1::system_addresses;
Resources
ChainId
struct ChainId has key
Fields
-
id: u8
Functions
initialize
Only called during genesis.
Publish the chain ID id
of this instance under the SystemAddresses address
public(friend) fun initialize(aptos_framework: &signer, id: u8)
Implementation
public(friend) fun initialize(aptos_framework: &signer, id: u8) { system_addresses::assert_aptos_framework(aptos_framework); move_to(aptos_framework, ChainId { id })}
get
Return the chain ID of this instance.
#[view]public fun get(): u8
Implementation
public fun get(): u8 acquires ChainId { borrow_global<ChainId>(@aptos_framework).id}
Specification
High-level Requirements
No. | Requirement | Criticality | Implementation | Enforcement |
---|---|---|---|---|
1 | During genesis, the ChainId resource should be created and moved under the Aptos framework account with the specified chain id. | Medium | The chain_id::initialize function is responsible for generating the ChainId resource and then storing it under the aptos_framework account. | Formally verified via initialize. |
2 | The chain id can only be fetched if the chain id resource exists under the Aptos framework account. | Low | The chain_id::get function fetches the chain id by borrowing the ChainId resource from the aptos_framework account. | Formally verified via get. |
Module-level Specification
pragma verify = true;pragma aborts_if_is_strict;
initialize
public(friend) fun initialize(aptos_framework: &signer, id: u8)
let addr = signer::address_of(aptos_framework);aborts_if addr != @aptos_framework;aborts_if exists<ChainId>(@aptos_framework);// This enforces high-level requirement 1:ensures exists<ChainId>(addr);ensures global<ChainId>(addr).id == id;
get
#[view]public fun get(): u8
// This enforces high-level requirement 2:aborts_if !exists<ChainId>(@aptos_framework);