Skip to content

Commit

Permalink
[compiler-v2 framework] add block prologue
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Oct 17, 2024
1 parent 4380a5a commit 081b251
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 38 deletions.
83 changes: 80 additions & 3 deletions vm/framework/starcoin-framework/doc/stc_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Block module provide metadata for generated blocks.
- [Function `get_current_block_number`](#0x1_stc_block_get_current_block_number)
- [Function `get_parent_hash`](#0x1_stc_block_get_parent_hash)
- [Function `get_current_author`](#0x1_stc_block_get_current_author)
- [Function `block_prologue`](#0x1_stc_block_block_prologue)
- [Function `process_block_metadata`](#0x1_stc_block_process_block_metadata)
- [Function `checkpoints_init`](#0x1_stc_block_checkpoints_init)
- [Function `checkpoint_entry`](#0x1_stc_block_checkpoint_entry)
Expand Down Expand Up @@ -44,12 +45,20 @@ Block module provide metadata for generated blocks.

<pre><code><b>use</b> <a href="account.md#0x1_account">0x1::account</a>;
<b>use</b> <a href="bcs_util.md#0x1_bcs_util">0x1::bcs_util</a>;
<b>use</b> <a href="block_reward.md#0x1_block_reward">0x1::block_reward</a>;
<b>use</b> <a href="chain_id.md#0x1_chain_id">0x1::chain_id</a>;
<b>use</b> <a href="coin.md#0x1_coin">0x1::coin</a>;
<b>use</b> <a href="epoch.md#0x1_epoch">0x1::epoch</a>;
<b>use</b> <a href="../../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
<b>use</b> <a href="event.md#0x1_event">0x1::event</a>;
<b>use</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="../../move-stdlib/doc/option.md#0x1_option">0x1::option</a>;
<b>use</b> <a href="ring.md#0x1_ring">0x1::ring</a>;
<b>use</b> <a href="../../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
<b>use</b> <a href="starcoin_coin.md#0x1_starcoin_coin">0x1::starcoin_coin</a>;
<b>use</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee">0x1::stc_transaction_fee</a>;
<b>use</b> <a href="system_addresses.md#0x1_system_addresses">0x1::system_addresses</a>;
<b>use</b> <a href="timestamp.md#0x1_timestamp">0x1::timestamp</a>;
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>

Expand Down Expand Up @@ -272,6 +281,15 @@ Events emitted when new block generated.



<a id="0x1_stc_block_EPROLOGUE_BAD_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="stc_block.md#0x1_stc_block_EPROLOGUE_BAD_CHAIN_ID">EPROLOGUE_BAD_CHAIN_ID</a>: u64 = 6;
</code></pre>



<a id="0x1_stc_block_ERROR_INTERVAL_TOO_LITTLE"></a>


Expand Down Expand Up @@ -408,6 +426,65 @@ Gets the address of the author of the current block



</details>

<a id="0x1_stc_block_block_prologue"></a>

## Function `block_prologue`

Set the metadata for the current block and distribute transaction fees and block rewards.
The runtime always runs this before executing the transactions in a block.


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_block_prologue">block_prologue</a>(<a href="account.md#0x1_account">account</a>: <a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, <a href="timestamp.md#0x1_timestamp">timestamp</a>: u64, author: <b>address</b>, auth_key_vec: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, uncles: u64, number: u64, <a href="chain_id.md#0x1_chain_id">chain_id</a>: u8, parent_gas_used: u64)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_block_prologue">block_prologue</a>(
<a href="account.md#0x1_account">account</a>: <a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
<a href="timestamp.md#0x1_timestamp">timestamp</a>: u64,
author: <b>address</b>,
auth_key_vec: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
uncles: u64,
number: u64,
<a href="chain_id.md#0x1_chain_id">chain_id</a>: u8,
parent_gas_used: u64,
) <b>acquires</b> <a href="stc_block.md#0x1_stc_block_BlockMetadata">BlockMetadata</a> {
// Can only be invoked by <a href="genesis.md#0x1_genesis">genesis</a> <a href="account.md#0x1_account">account</a>
<a href="system_addresses.md#0x1_system_addresses_assert_starcoin_framework">system_addresses::assert_starcoin_framework</a>(&<a href="account.md#0x1_account">account</a>);

// Check that the chain ID stored on-chain matches the chain ID
// specified by the transaction
<b>assert</b>!(<a href="chain_id.md#0x1_chain_id_get">chain_id::get</a>() == <a href="chain_id.md#0x1_chain_id">chain_id</a>, <a href="../../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="stc_block.md#0x1_stc_block_EPROLOGUE_BAD_CHAIN_ID">EPROLOGUE_BAD_CHAIN_ID</a>));

// deal <b>with</b> previous <a href="block.md#0x1_block">block</a> first.
<b>let</b> txn_fee = <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">stc_transaction_fee::distribute_transaction_fees</a>&lt;STC&gt;(&<a href="account.md#0x1_account">account</a>);

// then deal <b>with</b> current <a href="block.md#0x1_block">block</a>.
<a href="timestamp.md#0x1_timestamp_update_global_time">timestamp::update_global_time</a>(&<a href="account.md#0x1_account">account</a>, <a href="../../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(&<a href="account.md#0x1_account">account</a>), <a href="timestamp.md#0x1_timestamp">timestamp</a>);
<a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(
&<a href="account.md#0x1_account">account</a>,
parent_hash,
author,
<a href="timestamp.md#0x1_timestamp">timestamp</a>,
uncles,
number,
);

<b>let</b> reward = <a href="epoch.md#0x1_epoch_adjust_epoch">epoch::adjust_epoch</a>(&<a href="account.md#0x1_account">account</a>, number, <a href="timestamp.md#0x1_timestamp">timestamp</a>, uncles, parent_gas_used);
// pass in previous <a href="block.md#0x1_block">block</a> gas fees.
<a href="block_reward.md#0x1_block_reward_process_block_reward">block_reward::process_block_reward</a>(&<a href="account.md#0x1_account">account</a>, number, reward, author, auth_key_vec, txn_fee);
}
</code></pre>



</details>

<a id="0x1_stc_block_process_block_metadata"></a>
Expand All @@ -417,7 +494,7 @@ Gets the address of the author of the current block
Call at block prologue


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, author: <b>address</b>, <a href="timestamp.md#0x1_timestamp">timestamp</a>: u64, uncles: u64, number: u64)
<pre><code><b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, author: <b>address</b>, <a href="timestamp.md#0x1_timestamp">timestamp</a>: u64, uncles: u64, number: u64)
</code></pre>


Expand All @@ -426,7 +503,7 @@ Call at block prologue
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(
<pre><code><b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(
<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
author: <b>address</b>,
Expand Down Expand Up @@ -859,7 +936,7 @@ Call at block prologue
### Function `process_block_metadata`


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, author: <b>address</b>, <a href="timestamp.md#0x1_timestamp">timestamp</a>: u64, uncles: u64, number: u64)
<pre><code><b>fun</b> <a href="stc_block.md#0x1_stc_block_process_block_metadata">process_block_metadata</a>(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, author: <b>address</b>, <a href="timestamp.md#0x1_timestamp">timestamp</a>: u64, uncles: u64, number: u64)
</code></pre>


Expand Down
18 changes: 9 additions & 9 deletions vm/framework/starcoin-framework/doc/stc_transaction_fee.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Called in genesis. Sets up the needed resources to collect transaction fees from
publishing a wrapper of the <code>Preburn&lt;TokenType&gt;</code> resource under <code>fee_account</code>


<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType: store&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>)
<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>)
</code></pre>


Expand All @@ -101,7 +101,7 @@ publishing a wrapper of the <code>Preburn&lt;TokenType&gt;</code> resource under
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType: store&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>) {
<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>) {
<b>move_to</b>(
<a href="account.md#0x1_account">account</a>,
<a href="stc_transaction_fee.md#0x1_stc_transaction_fee_TransactionFee">TransactionFee</a>&lt;TokenType&gt; {
Expand All @@ -122,7 +122,7 @@ publishing a wrapper of the <code>Preburn&lt;TokenType&gt;</code> resource under
Deposit <code>token</code> into the transaction fees bucket


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType: store&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;)
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;)
</code></pre>


Expand All @@ -131,7 +131,7 @@ Deposit <code>token</code> into the transaction fees bucket
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType: store&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;) <b>acquires</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_TransactionFee">TransactionFee</a> {
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;) <b>acquires</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_TransactionFee">TransactionFee</a> {
<b>let</b> txn_fees = <b>borrow_global_mut</b>&lt;<a href="stc_transaction_fee.md#0x1_stc_transaction_fee_TransactionFee">TransactionFee</a>&lt;TokenType&gt;&gt;(
<a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>()
);
Expand All @@ -152,7 +152,7 @@ If the <code>TokenType</code> is STC, it unpacks the token and preburns the
underlying fiat.


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType: store&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;
</code></pre>


Expand All @@ -161,7 +161,7 @@ underlying fiat.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType: store&gt;(
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType&gt;(
<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
): <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt; <b>acquires</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_TransactionFee">TransactionFee</a> {
<b>let</b> fee_address = <a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>();
Expand Down Expand Up @@ -216,7 +216,7 @@ underlying fiat.
### Function `add_txn_fee_token`


<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType: store&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>)
<pre><code><b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_add_txn_fee_token">add_txn_fee_token</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>)
</code></pre>


Expand All @@ -232,7 +232,7 @@ underlying fiat.
### Function `pay_fee`


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType: store&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;)
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_pay_fee">pay_fee</a>&lt;TokenType&gt;(token: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;)
</code></pre>


Expand All @@ -251,7 +251,7 @@ underlying fiat.
### Function `distribute_transaction_fees`


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType: store&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee_distribute_transaction_fees">distribute_transaction_fees</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;TokenType&gt;
</code></pre>


Expand Down
44 changes: 22 additions & 22 deletions vm/framework/starcoin-framework/doc/timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,6 @@ Gets the current time in seconds.




<a id="0x1_timestamp_spec_now_microseconds"></a>


<pre><code><b>fun</b> <a href="timestamp.md#0x1_timestamp_spec_now_microseconds">spec_now_microseconds</a>(): u64 {
<b>global</b>&lt;<a href="timestamp.md#0x1_timestamp_CurrentTimeMicroseconds">CurrentTimeMicroseconds</a>&gt;(@starcoin_framework).microseconds
}
</code></pre>




<a id="0x1_timestamp_spec_now_seconds"></a>


<pre><code><b>fun</b> <a href="timestamp.md#0x1_timestamp_spec_now_seconds">spec_now_seconds</a>(): u64 {
<a href="timestamp.md#0x1_timestamp_spec_now_microseconds">spec_now_microseconds</a>() / <a href="timestamp.md#0x1_timestamp_MICRO_CONVERSION_FACTOR">MICRO_CONVERSION_FACTOR</a>
}
</code></pre>



<a id="@Specification_1_update_global_time"></a>

### Function `update_global_time`
Expand Down Expand Up @@ -331,4 +309,26 @@ Gets the current time in seconds.
</code></pre>




<a id="0x1_timestamp_spec_now_microseconds"></a>


<pre><code><b>fun</b> <a href="timestamp.md#0x1_timestamp_spec_now_microseconds">spec_now_microseconds</a>(): u64 {
<b>global</b>&lt;<a href="timestamp.md#0x1_timestamp_CurrentTimeMicroseconds">CurrentTimeMicroseconds</a>&gt;(@starcoin_framework).microseconds
}
</code></pre>




<a id="0x1_timestamp_spec_now_seconds"></a>


<pre><code><b>fun</b> <a href="timestamp.md#0x1_timestamp_spec_now_seconds">spec_now_seconds</a>(): u64 {
<a href="timestamp.md#0x1_timestamp_spec_now_microseconds">spec_now_microseconds</a>() / <a href="timestamp.md#0x1_timestamp_MICRO_CONVERSION_FACTOR">MICRO_CONVERSION_FACTOR</a>
}
</code></pre>


[move-book]: https://starcoin.dev/move/book/SUMMARY
1 change: 1 addition & 0 deletions vm/framework/starcoin-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module starcoin_framework::account {
friend starcoin_framework::starcoin_account;
friend starcoin_framework::coin;
friend starcoin_framework::genesis;
// friend starcoin_framework::stc_genesis;
friend starcoin_framework::multisig_account;
friend starcoin_framework::resource_account;
friend starcoin_framework::transaction_validation;
Expand Down
1 change: 1 addition & 0 deletions vm/framework/starcoin-framework/sources/chain_id.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module starcoin_framework::chain_id {
use starcoin_framework::system_addresses;

friend starcoin_framework::genesis;
// friend starcoin_framework::stc_genesis;

struct ChainId has key {
id: u8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module starcoin_framework::version {

friend starcoin_framework::genesis;
friend starcoin_framework::reconfiguration_with_dkg;
// friend starcoin_framework::stc_genesis;

struct Version has drop, key, store {
major: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module starcoin_framework::consensus_config {
use starcoin_framework::on_chain_config;
use starcoin_framework::system_addresses;

// friend starcoin_framework::stc_genesis;

/// consensus configurations.
struct ConsensusConfig has copy, drop, store {
/// Uncle block rate per epoch
Expand Down
Loading

0 comments on commit 081b251

Please sign in to comment.