diff --git a/Cargo.lock b/Cargo.lock index ea7c446d..6bf112fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,7 +138,7 @@ dependencies = [ [[package]] name = "starcoin-framework" -version = "11.0.0" +version = "13.0.0" dependencies = [ "anyhow", "include_dir", diff --git a/Cargo.toml b/Cargo.toml index a3bc053b..e6811b27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "starcoin-framework" -version = "11.0.0" +version = "13.0.0" authors = ["Starcoin Core Dev "] license = "Apache-2.0" publish = false @@ -14,4 +14,4 @@ once_cell = "1.8.0" tempfile = "3.2.0" log = "0.4.14" -[dev-dependencies] \ No newline at end of file +[dev-dependencies] diff --git a/integration-tests/block/block_metadata.exp b/integration-tests/block/block_metadata.exp index 1643c9e6..b44edfa9 100644 --- a/integration-tests/block/block_metadata.exp +++ b/integration-tests/block/block_metadata.exp @@ -1,7 +1,13 @@ -processed 3 tasks +processed 4 tasks task 2 'run'. lines 5-15: { - "gas_used": 16339, + "gas_used": 23606, + "status": "Executed" +} + +task 3 'run'. lines 17-27: +{ + "gas_used": 20390, "status": "Executed" } diff --git a/integration-tests/block/block_metadata.move b/integration-tests/block/block_metadata.move index d759ca9e..a8ab3968 100644 --- a/integration-tests/block/block_metadata.move +++ b/integration-tests/block/block_metadata.move @@ -13,3 +13,15 @@ script { } } // check: EXECUTED + +//# run --signers alice +script { + use StarcoinFramework::Block; + use StarcoinFramework::Debug; + + fun get_parents_hash(_account: signer) { + let hash = Block::get_parents_hash(); + Debug::print>(&hash); + } +} +// check: EXECUTED diff --git a/integration-tests/epoch/adjust_epoch_block_time_max.exp b/integration-tests/epoch/adjust_epoch_block_time_max.exp index 28237215..4a378569 100644 --- a/integration-tests/epoch/adjust_epoch_block_time_max.exp +++ b/integration-tests/epoch/adjust_epoch_block_time_max.exp @@ -2,6 +2,6 @@ processed 3 tasks task 2 'run'. lines 5-40: { - "gas_used": 26693545, + "gas_used": 32458705, "status": "Executed" } diff --git a/integration-tests/epoch/adjust_epoch_block_time_min.exp b/integration-tests/epoch/adjust_epoch_block_time_min.exp index 48ce10b5..da240864 100644 --- a/integration-tests/epoch/adjust_epoch_block_time_min.exp +++ b/integration-tests/epoch/adjust_epoch_block_time_min.exp @@ -2,6 +2,6 @@ processed 3 tasks task 2 'run'. lines 6-37: { - "gas_used": 26475471, + "gas_used": 32240631, "status": "Executed" } diff --git a/integration-tests/epoch/adjust_epoch_failed.exp b/integration-tests/epoch/adjust_epoch_failed.exp index 101e63fe..08d78895 100644 --- a/integration-tests/epoch/adjust_epoch_failed.exp +++ b/integration-tests/epoch/adjust_epoch_failed.exp @@ -18,13 +18,13 @@ task 3 'run'. lines 7-19: task 4 'run'. lines 22-34: { - "gas_used": 42342, + "gas_used": 54350, "status": "Executed" } task 5 'run'. lines 37-51: { - "gas_used": 41667, + "gas_used": 53675, "status": { "MoveAbort": { "location": { @@ -40,7 +40,7 @@ task 5 'run'. lines 37-51: task 6 'run'. lines 54-67: { - "gas_used": 38000, + "gas_used": 50008, "status": { "MoveAbort": { "location": { @@ -56,7 +56,7 @@ task 6 'run'. lines 54-67: task 7 'run'. lines 70-83: { - "gas_used": 45146, + "gas_used": 57286, "status": { "MoveAbort": { "location": { @@ -72,6 +72,6 @@ task 7 'run'. lines 70-83: task 8 'run'. lines 86-99: { - "gas_used": 168736, + "gas_used": 180876, "status": "Executed" } diff --git a/integration-tests/incubator/SortedLinkedList.exp b/integration-tests/incubator/SortedLinkedList.exp index 4f0b294d..8e4a83ab 100644 --- a/integration-tests/incubator/SortedLinkedList.exp +++ b/integration-tests/incubator/SortedLinkedList.exp @@ -72,19 +72,19 @@ task 19 'run'. lines 504-512: task 20 'run'. lines 514-523: { - "gas_used": 167705, + "gas_used": 172067, "status": "Executed" } task 21 'run'. lines 525-534: { - "gas_used": 196043, + "gas_used": 200405, "status": "Executed" } task 22 'run'. lines 536-545: { - "gas_used": 232559, + "gas_used": 236921, "status": "Executed" } @@ -102,7 +102,7 @@ task 24 'run'. lines 559-568: task 25 'run'. lines 570-580: { - "gas_used": 28739, + "gas_used": 33101, "status": { "MoveAbort": { "location": "Script", @@ -113,6 +113,6 @@ task 25 'run'. lines 570-580: task 32 'run'. lines 595-605: { - "gas_used": 113223, + "gas_used": 117585, "status": "Executed" } diff --git a/release/v12/sources/Block.move b/release/v12/sources/Block.move index ac4328a8..8f6a5cab 100644 --- a/release/v12/sources/Block.move +++ b/release/v12/sources/Block.move @@ -1,6 +1,7 @@ address StarcoinFramework { /// Block module provide metadata for generated blocks. module Block { + use StarcoinFramework::Vector; use StarcoinFramework::Event; use StarcoinFramework::Timestamp; use StarcoinFramework::Signer; @@ -34,7 +35,40 @@ module Block { uncles: u64, } + /// Block metadata struct. + // parents_hash is for FLexiDag block + struct BlockMetadataV2 has key { + /// number of the current block + number: u64, + /// Hash of the parent block. + parent_hash: vector, + /// Author of the current block. + author: address, + /// number of uncles. + uncles: u64, + /// An Array of the parents hash for a Dag block. + parents_hash: vector, + /// Handle of events when new blocks are emitted + new_block_events: Event::EventHandle, + } + + /// Events emitted when new block generated. + // parents_hash is for FLexiDag block + struct NewBlockEventV2 has drop, store { + number: u64, + author: address, + timestamp: u64, + uncles: u64, + parents_hash: vector, + } + const EBLOCK_NUMBER_MISMATCH: u64 = 17; + const ERROR_NOT_BLOCK_HEADER: u64 = 19; + const ERROR_INTERVAL_TOO_LITTLE: u64 = 20; + + const CHECKPOINT_LENGTH: u64 = 60; + const BLOCK_HEADER_LENGTH: u64 = 247; + const BLOCK_INTERVAL_NUMBER: u64 = 5; /// This can only be invoked by the GENESIS_ACCOUNT at genesis public fun initialize(account: &signer, parent_hash: vector) { @@ -45,7 +79,7 @@ module Block { account, BlockMetadata { number: 0, - parent_hash: parent_hash, + parent_hash, author: CoreAddresses::GENESIS_ADDRESS(), uncles: 0, new_block_events: Event::new_event_handle(account), @@ -58,63 +92,122 @@ module Block { aborts_if exists(Signer::address_of(account)); } + public fun initialize_blockmetadata_v2(account: &signer) acquires BlockMetadata { + CoreAddresses::assert_genesis_address(account); + + let block_meta_ref = borrow_global(CoreAddresses::GENESIS_ADDRESS()); + + // create new resource base on current block metadata + if (!exists(CoreAddresses::GENESIS_ADDRESS())) { + move_to( + account, + BlockMetadataV2 { + number: block_meta_ref.number, + parent_hash: block_meta_ref.parent_hash, + author: block_meta_ref.author, + uncles: block_meta_ref.uncles, + parents_hash: Vector::empty(), + new_block_events: Event::new_event_handle(account), + }); + } + } + + spec initialize_blockmetadata_v2 { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if exists(Signer::address_of(account)); + + ensures exists(Signer::address_of(account)); + } + /// Get the current block number - public fun get_current_block_number(): u64 acquires BlockMetadata { - borrow_global(CoreAddresses::GENESIS_ADDRESS()).number + public fun get_current_block_number(): u64 acquires BlockMetadataV2 { + borrow_global(CoreAddresses::GENESIS_ADDRESS()).number } spec get_current_block_number { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Get the hash of the parent block. - public fun get_parent_hash(): vector acquires BlockMetadata { - *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parent_hash + public fun get_parent_hash(): vector acquires BlockMetadataV2 { + *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parent_hash } spec get_parent_hash { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Gets the address of the author of the current block - public fun get_current_author(): address acquires BlockMetadata { - borrow_global(CoreAddresses::GENESIS_ADDRESS()).author + public fun get_current_author(): address acquires BlockMetadataV2 { + borrow_global(CoreAddresses::GENESIS_ADDRESS()).author } spec get_current_author { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + } + + public fun get_parents_hash(): vector acquires BlockMetadataV2 { + *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parents_hash + } + + spec get_parents_hash { + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Call at block prologue - public fun process_block_metadata(account: &signer, parent_hash: vector,author: address, timestamp: u64, uncles:u64, number:u64) acquires BlockMetadata{ + public fun process_block_metadata(account: &signer, + parent_hash: vector, + author: address, + timestamp: u64, + uncles: u64, + number: u64) acquires BlockMetadataV2 { + Self::process_block_metadata_v2(account, parent_hash, author, timestamp, uncles, number, Vector::empty()) + } + + spec process_block_metadata { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if number != global(CoreAddresses::GENESIS_ADDRESS()).number + 1; + } + + /// Call at block prologue for flexidag + public fun process_block_metadata_v2(account: &signer, + parent_hash: vector, + author: address, + timestamp: u64, + uncles: u64, + number: u64, + parents_hash: vector) acquires BlockMetadataV2 { CoreAddresses::assert_genesis_address(account); - let block_metadata_ref = borrow_global_mut(CoreAddresses::GENESIS_ADDRESS()); + let block_metadata_ref = borrow_global_mut(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.author = author; block_metadata_ref.parent_hash = parent_hash; block_metadata_ref.uncles = uncles; - - Event::emit_event( - &mut block_metadata_ref.new_block_events, - NewBlockEvent { - number: number, - author: author, - timestamp: timestamp, - uncles: uncles, - } + block_metadata_ref.parents_hash = parents_hash; + + Event::emit_event( + &mut block_metadata_ref.new_block_events, + NewBlockEventV2 { + number, + author, + timestamp, + uncles, + parents_hash, + } ); } - spec process_block_metadata { - aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS(); - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); - aborts_if number != global(CoreAddresses::SPEC_GENESIS_ADDRESS()).number + 1; + spec process_block_metadata_v2 { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if number != global(CoreAddresses::GENESIS_ADDRESS()).number + 1; } spec schema AbortsIfBlockMetadataNotExist { - aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } } -} \ No newline at end of file +} diff --git a/release/v12/sources/TransactionManager.move b/release/v12/sources/TransactionManager.move index cd9f44e1..0d572f3c 100644 --- a/release/v12/sources/TransactionManager.move +++ b/release/v12/sources/TransactionManager.move @@ -229,7 +229,7 @@ module TransactionManager { // then deal with current block. Timestamp::update_global_time(&account, timestamp); - Block::process_block_metadata( + Block::process_block_metadata_v2( &account, parent_hash, author, diff --git a/sources/Block.move b/sources/Block.move index ac4328a8..3b54221a 100644 --- a/sources/Block.move +++ b/sources/Block.move @@ -1,6 +1,7 @@ address StarcoinFramework { /// Block module provide metadata for generated blocks. module Block { + use StarcoinFramework::Vector; use StarcoinFramework::Event; use StarcoinFramework::Timestamp; use StarcoinFramework::Signer; @@ -34,7 +35,40 @@ module Block { uncles: u64, } + /// Block metadata struct. + // parents_hash is for FLexiDag block + struct BlockMetadataV2 has key { + /// number of the current block + number: u64, + /// Hash of the parent block. + parent_hash: vector, + /// Author of the current block. + author: address, + /// number of uncles. + uncles: u64, + /// An Array of the parents hash for a Dag block. + parents_hash: vector, + /// Handle of events when new blocks are emitted + new_block_events: Event::EventHandle, + } + + /// Events emitted when new block generated. + // parents_hash is for FLexiDag block + struct NewBlockEventV2 has drop, store { + number: u64, + author: address, + timestamp: u64, + uncles: u64, + parents_hash: vector, + } + const EBLOCK_NUMBER_MISMATCH: u64 = 17; + const ERROR_NOT_BLOCK_HEADER: u64 = 19; + const ERROR_INTERVAL_TOO_LITTLE: u64 = 20; + + const CHECKPOINT_LENGTH: u64 = 60; + const BLOCK_HEADER_LENGTH: u64 = 247; + const BLOCK_INTERVAL_NUMBER: u64 = 5; /// This can only be invoked by the GENESIS_ACCOUNT at genesis public fun initialize(account: &signer, parent_hash: vector) { @@ -45,7 +79,7 @@ module Block { account, BlockMetadata { number: 0, - parent_hash: parent_hash, + parent_hash, author: CoreAddresses::GENESIS_ADDRESS(), uncles: 0, new_block_events: Event::new_event_handle(account), @@ -58,35 +92,95 @@ module Block { aborts_if exists(Signer::address_of(account)); } + public fun initialize_blockmetadata_v2(account: &signer) acquires BlockMetadata { + CoreAddresses::assert_genesis_address(account); + + let block_meta_ref = borrow_global(CoreAddresses::GENESIS_ADDRESS()); + + // create new resource base on current block metadata + move_to( + account, + BlockMetadataV2 { + number: block_meta_ref.number, + parent_hash: block_meta_ref.parent_hash, + author: block_meta_ref.author, + uncles: block_meta_ref.uncles, + parents_hash: Vector::empty(), + new_block_events: Event::new_event_handle(account), + }); + } + + spec initialize_blockmetadata_v2 { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if exists(Signer::address_of(account)); + + ensures exists(Signer::address_of(account)); + } + /// Get the current block number - public fun get_current_block_number(): u64 acquires BlockMetadata { - borrow_global(CoreAddresses::GENESIS_ADDRESS()).number + public fun get_current_block_number(): u64 acquires BlockMetadataV2, BlockMetadata { + let addr = CoreAddresses::GENESIS_ADDRESS(); + if (exists(addr)) { + borrow_global(addr).number + } else { + borrow_global(addr).number + } } spec get_current_block_number { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Get the hash of the parent block. - public fun get_parent_hash(): vector acquires BlockMetadata { - *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parent_hash + public fun get_parent_hash(): vector acquires BlockMetadataV2, BlockMetadata { + let addr = CoreAddresses::GENESIS_ADDRESS(); + if (exists(addr)) { + *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parent_hash + } else { + *&borrow_global(CoreAddresses::GENESIS_ADDRESS()).parent_hash + } } spec get_parent_hash { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Gets the address of the author of the current block - public fun get_current_author(): address acquires BlockMetadata { - borrow_global(CoreAddresses::GENESIS_ADDRESS()).author + public fun get_current_author(): address acquires BlockMetadataV2, BlockMetadata { + let addr = CoreAddresses::GENESIS_ADDRESS(); + if (exists(addr)) { + *&borrow_global(addr).author + } else { + *&borrow_global(addr).author + } } spec get_current_author { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + } + + public fun get_parents_hash(): vector acquires BlockMetadataV2 { + let addr = CoreAddresses::GENESIS_ADDRESS(); + if (exists(addr)) { + *&borrow_global(addr).parents_hash + } else { + Vector::empty() + } + } + + spec get_parents_hash { + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } /// Call at block prologue - public fun process_block_metadata(account: &signer, parent_hash: vector,author: address, timestamp: u64, uncles:u64, number:u64) acquires BlockMetadata{ + public fun process_block_metadata( + account: &signer, + parent_hash: vector, + author: address, + timestamp: u64, + uncles: u64, + number: u64, + ) acquires BlockMetadata { CoreAddresses::assert_genesis_address(account); let block_metadata_ref = borrow_global_mut(CoreAddresses::GENESIS_ADDRESS()); @@ -97,24 +191,75 @@ module Block { block_metadata_ref.uncles = uncles; Event::emit_event( - &mut block_metadata_ref.new_block_events, - NewBlockEvent { - number: number, - author: author, - timestamp: timestamp, - uncles: uncles, - } + &mut block_metadata_ref.new_block_events, + NewBlockEvent { + number: number, + author: author, + timestamp: timestamp, + uncles: uncles, + } ); } spec process_block_metadata { - aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS(); - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); - aborts_if number != global(CoreAddresses::SPEC_GENESIS_ADDRESS()).number + 1; + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if number != global(CoreAddresses::GENESIS_ADDRESS()).number + 1; + } + + /// Call at block prologue for flexidag + public fun process_block_metadata_v2( + account: &signer, + parent_hash: vector, + author: address, + timestamp: u64, + uncles: u64, + number: u64, + parents_hash: vector + ) acquires BlockMetadataV2, BlockMetadata { + CoreAddresses::assert_genesis_address(account); + + let account_addr = Signer::address_of(account); + if (exists(account_addr)) { + let block_metadata_ref = borrow_global_mut(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; + block_metadata_ref.parents_hash = parents_hash; + + Event::emit_event( + &mut block_metadata_ref.new_block_events, + NewBlockEventV2 { + number, + author, + timestamp, + uncles, + parents_hash, + } + ); + } else { + Self::process_block_metadata( + account, + parent_hash, + author, + timestamp, + uncles, + number + ) + } + + } + + spec process_block_metadata_v2 { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if number != global(CoreAddresses::GENESIS_ADDRESS()).number + 1; } spec schema AbortsIfBlockMetadataNotExist { - aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); } } -} \ No newline at end of file +} diff --git a/sources/Epoch.move b/sources/Epoch.move index 81e1d751..9afa3f47 100644 --- a/sources/Epoch.move +++ b/sources/Epoch.move @@ -1,12 +1,16 @@ address StarcoinFramework { /// The module provide epoch functionality for starcoin. module Epoch { + use StarcoinFramework::FlexiDagConfig::FlexiDagConfig; + use StarcoinFramework::Math::u64_max; + use StarcoinFramework::Config::config_exist_by_address; + use StarcoinFramework::Errors; + use StarcoinFramework::FlexiDagConfig; use StarcoinFramework::Config; use StarcoinFramework::Signer; use StarcoinFramework::CoreAddresses; use StarcoinFramework::Event; - use StarcoinFramework::Errors; use StarcoinFramework::Timestamp; use StarcoinFramework::Math; use StarcoinFramework::Option; @@ -153,14 +157,25 @@ module Epoch { CoreAddresses::assert_genesis_address(account); let epoch_ref = borrow_global_mut(CoreAddresses::GENESIS_ADDRESS()); - assert!(epoch_ref.max_uncles_per_block >= uncles, Errors::invalid_argument(EINVALID_UNCLES_COUNT)); + let flexidag_fork_height = if (config_exist_by_address(CoreAddresses::GENESIS_ADDRESS())) { + FlexiDagConfig::effective_height(CoreAddresses::GENESIS_ADDRESS()) + } else { + u64_max() + }; + assert!( + block_number > flexidag_fork_height || epoch_ref.max_uncles_per_block >= uncles, + Errors::invalid_argument(EINVALID_UNCLES_COUNT) + ); let epoch_data = borrow_global_mut(CoreAddresses::GENESIS_ADDRESS()); let (new_epoch, reward_per_block) = if (block_number < epoch_ref.end_block_number) { (false, epoch_ref.reward_per_block) } else if (block_number == epoch_ref.end_block_number) { //start a new epoch - assert!(uncles == 0, Errors::invalid_argument(EINVALID_UNCLES_COUNT)); + assert!( + block_number > flexidag_fork_height || uncles == 0, + Errors::invalid_argument(EINVALID_UNCLES_COUNT) + ); // block time target unit is milli_seconds. let now_milli_seconds = timestamp; @@ -377,4 +392,4 @@ module Epoch { } } -} \ No newline at end of file +} diff --git a/sources/FlexiDagConfig.move b/sources/FlexiDagConfig.move new file mode 100644 index 00000000..4ac2c486 --- /dev/null +++ b/sources/FlexiDagConfig.move @@ -0,0 +1,52 @@ +address StarcoinFramework { + module FlexiDagConfig { + + use StarcoinFramework::Config; + use StarcoinFramework::CoreAddresses; + use StarcoinFramework::Signer; + + spec module { + pragma verify = false; + pragma aborts_if_is_strict; + } + + /// The struct to hold all config data needed for Flexidag. + struct FlexiDagConfig has copy, drop, store { + // todo: double check, epoch might be better? + // the height of dag genesis block + effective_height: u64, + } + + /// Create a new configuration for flexidag, mainly used in DAO. + public fun new_flexidag_config(effective_height: u64): FlexiDagConfig { + FlexiDagConfig { + effective_height, + } + } + + public fun initialize(account: &signer, effective_height: u64) { + CoreAddresses::assert_genesis_address(account); + Config::publish_new_config(account, new_flexidag_config(effective_height)); + } + + spec initialize { + aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS(); + aborts_if exists>(Signer::address_of(account)); + aborts_if exists>(Signer::address_of(account)); + ensures exists>(Signer::address_of(account)); + ensures + exists>( + Signer::address_of(account), + ); + } + + public fun effective_height(account: address): u64 { + let flexi_dag_config = Config::get_by_address(account); + flexi_dag_config.effective_height + } + + spec effective_height { + include Config::AbortsIfConfigNotExist { addr: account }; + } + } +} diff --git a/sources/Genesis.move b/sources/Genesis.move index 93328f81..92d0b9ca 100644 --- a/sources/Genesis.move +++ b/sources/Genesis.move @@ -104,6 +104,7 @@ module Genesis { ChainId::initialize(&genesis_account, chain_id); ConsensusStrategy::initialize(&genesis_account, strategy); Block::initialize(&genesis_account, parent_hash); + TransactionPublishOption::initialize( &genesis_account, script_allowed, @@ -447,7 +448,12 @@ module Genesis { GenesisNFT::initialize(&genesis_account, merkle_root, 1639u64, image); }; StdlibUpgradeScripts::do_upgrade_from_v6_to_v7_with_language_version(&genesis_account, 6); - StdlibUpgradeScripts::do_upgrade_from_v11_to_v12(&genesis_account); + + // stdlib_version == 0 is StdlibVersion::Latest + if (stdlib_version >= 12 || stdlib_version == 0) { + StdlibUpgradeScripts::do_upgrade_from_v11_to_v12(&genesis_account); + }; + // Initialize Frozen strategy FrozenConfigStrategy::do_initialize(&association); //Start time, Timestamp::is_genesis() will return false. this call should at the end of genesis init. @@ -559,4 +565,4 @@ module Genesis { ); } } -} \ No newline at end of file +} diff --git a/sources/OnChainConfigScripts.move b/sources/OnChainConfigScripts.move index 4ce105e9..7bc0bc29 100644 --- a/sources/OnChainConfigScripts.move +++ b/sources/OnChainConfigScripts.move @@ -1,5 +1,6 @@ address StarcoinFramework { module OnChainConfigScripts { + use StarcoinFramework::FlexiDagConfig; use StarcoinFramework::ConsensusConfig; use StarcoinFramework::OnChainConfigDao; use StarcoinFramework::STC; @@ -119,6 +120,15 @@ module OnChainConfigScripts { pragma verify = false; } + public entry fun propose_update_flexi_dag_effective_height(account: signer, new_height: u64, exec_delay: u64) { + let config = FlexiDagConfig::new_flexidag_config(new_height); + OnChainConfigDao::propose_update(&account, config, exec_delay); + } + + spec propose_update_flexi_dag_effective_height { + pragma verify = false; + } + public entry fun execute_on_chain_config_proposal(account: signer, proposal_id: u64) { OnChainConfigDao::execute(Signer::address_of(&account), proposal_id); } diff --git a/sources/StdlibUpgradeScripts.move b/sources/StdlibUpgradeScripts.move index 34f5260c..3e34cbb2 100644 --- a/sources/StdlibUpgradeScripts.move +++ b/sources/StdlibUpgradeScripts.move @@ -2,12 +2,15 @@ address StarcoinFramework { /// The module for StdlibUpgrade init scripts module StdlibUpgradeScripts { + use StarcoinFramework::Block; use StarcoinFramework::Errors; use StarcoinFramework::Signer; use StarcoinFramework::ChainId; use StarcoinFramework::Vector; use StarcoinFramework::ACL; use StarcoinFramework::FrozenConfigStrategy; + use StarcoinFramework::Math::u64_max; + use StarcoinFramework::FlexiDagConfig; use StarcoinFramework::CoreAddresses; use StarcoinFramework::STC::{Self, STC}; use StarcoinFramework::Token::{Self, LinearTimeMintKey}; @@ -120,7 +123,11 @@ module StdlibUpgradeScripts { while (i < Vector::length(&acl_vec)) { STC::burn(Account::withdraw_illegal_token(sender, *Vector::borrow(&acl_vec, i), 0)); i = i + 1; - } + }; + + FlexiDagConfig::initialize(sender, u64_max()); + Block::initialize_blockmetadata_v2(sender); + OnChainConfigDao::plugin(sender); } /// Burned by user account @@ -136,4 +143,4 @@ module StdlibUpgradeScripts { STC::burn(token); } } -} \ No newline at end of file +} diff --git a/sources/TransactionManager.move b/sources/TransactionManager.move index bc9c726d..84517fb3 100644 --- a/sources/TransactionManager.move +++ b/sources/TransactionManager.move @@ -220,6 +220,28 @@ module TransactionManager { number: u64, chain_id: u8, parent_gas_used: u64, + ) { + Self::block_prologue_v2(account, parent_hash, timestamp, author, auth_key_vec, uncles, number, chain_id, parent_gas_used, Vector::empty()) + } + + spec block_prologue { + pragma verify = false;//fixme : timeout + } + + /// 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. + /// For Flexidag block + public fun block_prologue_v2( + account: signer, + parent_hash: vector, + timestamp: u64, + author: address, + auth_key_vec: vector, + uncles: u64, + number: u64, + chain_id: u8, + parent_gas_used: u64, + parents_hash: vector, ) { // Can only be invoked by genesis account CoreAddresses::assert_genesis_address(&account); @@ -232,20 +254,21 @@ module TransactionManager { // then deal with current block. Timestamp::update_global_time(&account, timestamp); - Block::process_block_metadata( + Block::process_block_metadata_v2( &account, parent_hash, author, timestamp, uncles, number, + parents_hash, ); let reward = Epoch::adjust_epoch(&account, number, timestamp, uncles, parent_gas_used); // pass in previous block gas fees. BlockReward::process_block_reward(&account, number, reward, author, auth_key_vec, txn_fee); } - spec block_prologue { + spec block_prologue_v2 { pragma verify = false;//fixme : timeout } } diff --git a/sources/TransactionTimeout.move b/sources/TransactionTimeout.move index a4ff2b63..09c21da6 100644 --- a/sources/TransactionTimeout.move +++ b/sources/TransactionTimeout.move @@ -35,16 +35,16 @@ module TransactionTimeout { current_block_time < txn_timestamp && txn_timestamp < max_txn_time } spec is_valid_transaction_timestamp { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); include Timestamp::AbortsIfTimestampNotExists; aborts_if Block::get_current_block_number() != 0 && Timestamp::now_seconds() + TransactionTimeoutConfig::duration_seconds() > max_u64(); aborts_if Block::get_current_block_number() != 0 && !exists>(CoreAddresses::SPEC_GENESIS_ADDRESS()); } spec schema AbortsIfTimestampNotValid { - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); - aborts_if !exists(CoreAddresses::SPEC_GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); + aborts_if !exists(CoreAddresses::GENESIS_ADDRESS()); include Timestamp::AbortsIfTimestampNotExists; aborts_if Block::get_current_block_number() != 0 && Timestamp::now_seconds() + TransactionTimeoutConfig::duration_seconds() > max_u64(); aborts_if Block::get_current_block_number() != 0 && !exists>(CoreAddresses::SPEC_GENESIS_ADDRESS());