From 081b251f2fa9dac5f1aa6cf98eac9c22bb284ef5 Mon Sep 17 00:00:00 2001 From: welbon Date: Thu, 17 Oct 2024 22:36:22 +0800 Subject: [PATCH] [compiler-v2 framework] add block prologue --- .../starcoin-framework/doc/stc_block.md | 83 ++++++++++++++++++- .../doc/stc_transaction_fee.md | 18 ++-- .../starcoin-framework/doc/timestamp.md | 44 +++++----- .../starcoin-framework/sources/account.move | 1 + .../starcoin-framework/sources/chain_id.move | 1 + .../sources/configs/version.move | 1 + .../sources/stc/consensus_config.move | 2 + .../sources/stc/stc_block.move | 52 +++++++++++- .../sources/stc/stc_transaction_fee.move | 6 +- .../starcoin-framework/sources/timestamp.move | 1 + 10 files changed, 171 insertions(+), 38 deletions(-) diff --git a/vm/framework/starcoin-framework/doc/stc_block.md b/vm/framework/starcoin-framework/doc/stc_block.md index 813f4131ea..6ce7448d22 100644 --- a/vm/framework/starcoin-framework/doc/stc_block.md +++ b/vm/framework/starcoin-framework/doc/stc_block.md @@ -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) @@ -44,12 +45,20 @@ Block module provide metadata for generated blocks.
use 0x1::account;
 use 0x1::bcs_util;
+use 0x1::block_reward;
+use 0x1::chain_id;
+use 0x1::coin;
+use 0x1::epoch;
 use 0x1::error;
 use 0x1::event;
 use 0x1::hash;
 use 0x1::option;
 use 0x1::ring;
+use 0x1::signer;
+use 0x1::starcoin_coin;
+use 0x1::stc_transaction_fee;
 use 0x1::system_addresses;
+use 0x1::timestamp;
 use 0x1::vector;
 
@@ -272,6 +281,15 @@ Events emitted when new block generated. + + + + +
const EPROLOGUE_BAD_CHAIN_ID: u64 = 6;
+
+ + + @@ -408,6 +426,65 @@ Gets the address of the author of the current block + + + + +## 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. + + +
public fun block_prologue(account: signer, parent_hash: vector<u8>, timestamp: u64, author: address, auth_key_vec: vector<u8>, uncles: u64, number: u64, chain_id: u8, parent_gas_used: u64)
+
+ + + +
+Implementation + + +
public fun block_prologue(
+    account: signer,
+    parent_hash: vector<u8>,
+    timestamp: u64,
+    author: address,
+    auth_key_vec: vector<u8>,
+    uncles: u64,
+    number: u64,
+    chain_id: u8,
+    parent_gas_used: u64,
+) acquires BlockMetadata {
+    // Can only be invoked by genesis account
+    system_addresses::assert_starcoin_framework(&account);
+
+    // Check that the chain ID stored on-chain matches the chain ID
+    // specified by the transaction
+    assert!(chain_id::get() == chain_id, error::invalid_argument(EPROLOGUE_BAD_CHAIN_ID));
+
+    // deal with previous block first.
+    let txn_fee = stc_transaction_fee::distribute_transaction_fees<STC>(&account);
+
+    // then deal with current block.
+    timestamp::update_global_time(&account, signer::address_of(&account), timestamp);
+    process_block_metadata(
+        &account,
+        parent_hash,
+        author,
+        timestamp,
+        uncles,
+        number,
+    );
+
+    let reward = epoch::adjust_epoch(&account, number, timestamp, uncles, parent_gas_used);
+    // pass in previous block gas fees.
+    block_reward::process_block_reward(&account, number, reward, author, auth_key_vec, txn_fee);
+}
+
+ + +
@@ -417,7 +494,7 @@ Gets the address of the author of the current block Call at block prologue -
public fun process_block_metadata(account: &signer, parent_hash: vector<u8>, author: address, timestamp: u64, uncles: u64, number: u64)
+
fun process_block_metadata(account: &signer, parent_hash: vector<u8>, author: address, timestamp: u64, uncles: u64, number: u64)
 
@@ -426,7 +503,7 @@ Call at block prologue Implementation -
public fun process_block_metadata(
+
fun process_block_metadata(
     account: &signer,
     parent_hash: vector<u8>,
     author: address,
@@ -859,7 +936,7 @@ Call at block prologue
 ### Function `process_block_metadata`
 
 
-
public fun process_block_metadata(account: &signer, parent_hash: vector<u8>, author: address, timestamp: u64, uncles: u64, number: u64)
+
fun process_block_metadata(account: &signer, parent_hash: vector<u8>, author: address, timestamp: u64, uncles: u64, number: u64)
 
diff --git a/vm/framework/starcoin-framework/doc/stc_transaction_fee.md b/vm/framework/starcoin-framework/doc/stc_transaction_fee.md index 96211ab2b2..49cf6c06d2 100644 --- a/vm/framework/starcoin-framework/doc/stc_transaction_fee.md +++ b/vm/framework/starcoin-framework/doc/stc_transaction_fee.md @@ -92,7 +92,7 @@ Called in genesis. Sets up the needed resources to collect transaction fees from publishing a wrapper of the Preburn<TokenType> resource under fee_account -
fun add_txn_fee_token<TokenType: store>(account: &signer)
+
fun add_txn_fee_token<TokenType>(account: &signer)
 
@@ -101,7 +101,7 @@ publishing a wrapper of the Preburn<TokenType> resource under Implementation -
fun add_txn_fee_token<TokenType: store>(account: &signer) {
+
fun add_txn_fee_token<TokenType>(account: &signer) {
     move_to(
         account,
         TransactionFee<TokenType> {
@@ -122,7 +122,7 @@ publishing a wrapper of the Preburn<TokenType> resource under
 Deposit token into the transaction fees bucket
 
 
-
public fun pay_fee<TokenType: store>(token: coin::Coin<TokenType>)
+
public fun pay_fee<TokenType>(token: coin::Coin<TokenType>)
 
@@ -131,7 +131,7 @@ Deposit token into the transaction fees bucket Implementation -
public fun pay_fee<TokenType: store>(token: coin::Coin<TokenType>) acquires TransactionFee {
+
public fun pay_fee<TokenType>(token: coin::Coin<TokenType>) acquires TransactionFee {
     let txn_fees = borrow_global_mut<TransactionFee<TokenType>>(
         system_addresses::get_starcoin_framework()
     );
@@ -152,7 +152,7 @@ If the TokenType is STC, it unpacks the token and preburns the
 underlying fiat.
 
 
-
public fun distribute_transaction_fees<TokenType: store>(account: &signer): coin::Coin<TokenType>
+
public fun distribute_transaction_fees<TokenType>(account: &signer): coin::Coin<TokenType>
 
@@ -161,7 +161,7 @@ underlying fiat. Implementation -
public fun distribute_transaction_fees<TokenType: store>(
+
public fun distribute_transaction_fees<TokenType>(
     account: &signer,
 ): coin::Coin<TokenType> acquires TransactionFee {
     let fee_address = system_addresses::get_starcoin_framework();
@@ -216,7 +216,7 @@ underlying fiat.
 ### Function `add_txn_fee_token`
 
 
-
fun add_txn_fee_token<TokenType: store>(account: &signer)
+
fun add_txn_fee_token<TokenType>(account: &signer)
 
@@ -232,7 +232,7 @@ underlying fiat. ### Function `pay_fee` -
public fun pay_fee<TokenType: store>(token: coin::Coin<TokenType>)
+
public fun pay_fee<TokenType>(token: coin::Coin<TokenType>)
 
@@ -251,7 +251,7 @@ underlying fiat. ### Function `distribute_transaction_fees` -
public fun distribute_transaction_fees<TokenType: store>(account: &signer): coin::Coin<TokenType>
+
public fun distribute_transaction_fees<TokenType>(account: &signer): coin::Coin<TokenType>
 
diff --git a/vm/framework/starcoin-framework/doc/timestamp.md b/vm/framework/starcoin-framework/doc/timestamp.md index cf34011e16..bd15ca533a 100644 --- a/vm/framework/starcoin-framework/doc/timestamp.md +++ b/vm/framework/starcoin-framework/doc/timestamp.md @@ -274,28 +274,6 @@ Gets the current time in seconds. - - - - -
fun spec_now_microseconds(): u64 {
-   global<CurrentTimeMicroseconds>(@starcoin_framework).microseconds
-}
-
- - - - - - - -
fun spec_now_seconds(): u64 {
-   spec_now_microseconds() / MICRO_CONVERSION_FACTOR
-}
-
- - - ### Function `update_global_time` @@ -331,4 +309,26 @@ Gets the current time in seconds.
+ + + + + +
fun spec_now_microseconds(): u64 {
+   global<CurrentTimeMicroseconds>(@starcoin_framework).microseconds
+}
+
+ + + + + + + +
fun spec_now_seconds(): u64 {
+   spec_now_microseconds() / MICRO_CONVERSION_FACTOR
+}
+
+ + [move-book]: https://starcoin.dev/move/book/SUMMARY diff --git a/vm/framework/starcoin-framework/sources/account.move b/vm/framework/starcoin-framework/sources/account.move index 70b3b739ec..50902967af 100644 --- a/vm/framework/starcoin-framework/sources/account.move +++ b/vm/framework/starcoin-framework/sources/account.move @@ -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; diff --git a/vm/framework/starcoin-framework/sources/chain_id.move b/vm/framework/starcoin-framework/sources/chain_id.move index 0d23258a9d..321072edc5 100644 --- a/vm/framework/starcoin-framework/sources/chain_id.move +++ b/vm/framework/starcoin-framework/sources/chain_id.move @@ -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 diff --git a/vm/framework/starcoin-framework/sources/configs/version.move b/vm/framework/starcoin-framework/sources/configs/version.move index 17de372622..6680ecf14e 100644 --- a/vm/framework/starcoin-framework/sources/configs/version.move +++ b/vm/framework/starcoin-framework/sources/configs/version.move @@ -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, diff --git a/vm/framework/starcoin-framework/sources/stc/consensus_config.move b/vm/framework/starcoin-framework/sources/stc/consensus_config.move index 090053f500..81ec98cdea 100644 --- a/vm/framework/starcoin-framework/sources/stc/consensus_config.move +++ b/vm/framework/starcoin-framework/sources/stc/consensus_config.move @@ -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 diff --git a/vm/framework/starcoin-framework/sources/stc/stc_block.move b/vm/framework/starcoin-framework/sources/stc/stc_block.move index 5f0dfee5bd..5ab5ec03d1 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_block.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_block.move @@ -3,13 +3,24 @@ module starcoin_framework::stc_block { use std::error; use std::hash; use std::option; + use std::signer; use std::vector; + + use starcoin_framework::epoch; + use starcoin_framework::block_reward; + use starcoin_framework::timestamp; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::stc_transaction_fee; + + use starcoin_framework::chain_id; use starcoin_framework::bcs_util; use starcoin_framework::account; use starcoin_framework::system_addresses; use starcoin_framework::ring; use starcoin_framework::event; + const EPROLOGUE_BAD_CHAIN_ID: u64 = 6; + /// Block metadata struct. struct BlockMetadata has key { /// number of the current block @@ -102,8 +113,47 @@ module starcoin_framework::stc_block { borrow_global(system_addresses::get_starcoin_framework()).author } + /// 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. + public fun block_prologue( + account: signer, + parent_hash: vector, + timestamp: u64, + author: address, + auth_key_vec: vector, + uncles: u64, + number: u64, + chain_id: u8, + parent_gas_used: u64, + ) acquires BlockMetadata { + // Can only be invoked by genesis account + system_addresses::assert_starcoin_framework(&account); + + // Check that the chain ID stored on-chain matches the chain ID + // specified by the transaction + assert!(chain_id::get() == chain_id, error::invalid_argument(EPROLOGUE_BAD_CHAIN_ID)); + + // deal with previous block first. + let txn_fee = stc_transaction_fee::distribute_transaction_fees(&account); + + // then deal with current block. + timestamp::update_global_time(&account, signer::address_of(&account), timestamp); + process_block_metadata( + &account, + parent_hash, + author, + timestamp, + uncles, + number, + ); + + let reward = epoch::adjust_epoch(&account, number, timestamp, uncles, parent_gas_used); + // pass in previous block gas fees. + block_reward::process_block_reward(&account, number, reward, author, auth_key_vec, txn_fee); + } + /// Call at block prologue - public fun process_block_metadata( + fun process_block_metadata( account: &signer, parent_hash: vector, author: address, diff --git a/vm/framework/starcoin-framework/sources/stc/stc_transaction_fee.move b/vm/framework/starcoin-framework/sources/stc/stc_transaction_fee.move index fb73481f65..0f0b575dcb 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_transaction_fee.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_transaction_fee.move @@ -35,7 +35,7 @@ module starcoin_framework::stc_transaction_fee { } /// publishing a wrapper of the `Preburn` resource under `fee_account` - fun add_txn_fee_token(account: &signer) { + fun add_txn_fee_token(account: &signer) { move_to( account, TransactionFee { @@ -50,7 +50,7 @@ module starcoin_framework::stc_transaction_fee { } /// Deposit `token` into the transaction fees bucket - public fun pay_fee(token: coin::Coin) acquires TransactionFee { + public fun pay_fee(token: coin::Coin) acquires TransactionFee { let txn_fees = borrow_global_mut>( system_addresses::get_starcoin_framework() ); @@ -69,7 +69,7 @@ module starcoin_framework::stc_transaction_fee { /// Distribute the transaction fees collected in the `TokenType` token. /// If the `TokenType` is STC, it unpacks the token and preburns the /// underlying fiat. - public fun distribute_transaction_fees( + public fun distribute_transaction_fees( account: &signer, ): coin::Coin acquires TransactionFee { let fee_address = system_addresses::get_starcoin_framework(); diff --git a/vm/framework/starcoin-framework/sources/timestamp.move b/vm/framework/starcoin-framework/sources/timestamp.move index 3d64ba5c05..563d6f754c 100644 --- a/vm/framework/starcoin-framework/sources/timestamp.move +++ b/vm/framework/starcoin-framework/sources/timestamp.move @@ -7,6 +7,7 @@ module starcoin_framework::timestamp { use std::error; friend starcoin_framework::genesis; + // friend starcoin_framework::stc_genesis; /// A singleton resource holding the current Unix time in microseconds struct CurrentTimeMicroseconds has key {