Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlexiDag] Release v12 with flexidag #257

Open
wants to merge 9 commits into
base: v11_branch
Choose a base branch
from
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starcoin-framework"
version = "11.0.0"
version = "13.0.0"
authors = ["Starcoin Core Dev <dev@starcoin.org>"]
license = "Apache-2.0"
publish = false
Expand All @@ -14,4 +14,4 @@ once_cell = "1.8.0"
tempfile = "3.2.0"
log = "0.4.14"

[dev-dependencies]
[dev-dependencies]
10 changes: 8 additions & 2 deletions integration-tests/block/block_metadata.exp
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions integration-tests/block/block_metadata.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<vector<u8>>(&hash);
}
}
// check: EXECUTED
2 changes: 1 addition & 1 deletion integration-tests/epoch/adjust_epoch_block_time_max.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ processed 3 tasks

task 2 'run'. lines 5-40:
{
"gas_used": 26693545,
"gas_used": 32458705,
"status": "Executed"
}
2 changes: 1 addition & 1 deletion integration-tests/epoch/adjust_epoch_block_time_min.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ processed 3 tasks

task 2 'run'. lines 6-37:
{
"gas_used": 26475471,
"gas_used": 32240631,
"status": "Executed"
}
10 changes: 5 additions & 5 deletions integration-tests/epoch/adjust_epoch_failed.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -72,6 +72,6 @@ task 7 'run'. lines 70-83:

task 8 'run'. lines 86-99:
{
"gas_used": 168736,
"gas_used": 180876,
"status": "Executed"
}
10 changes: 5 additions & 5 deletions integration-tests/incubator/SortedLinkedList.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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",
Expand All @@ -113,6 +113,6 @@ task 25 'run'. lines 570-580:

task 32 'run'. lines 595-605:
{
"gas_used": 113223,
"gas_used": 117585,
"status": "Executed"
}
149 changes: 121 additions & 28 deletions release/v12/sources/Block.move
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<u8>,
/// 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<u8>,
/// Handle of events when new blocks are emitted
new_block_events: Event::EventHandle<Self::NewBlockEventV2>,
}

/// 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<u8>,
}

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<u8>) {
Expand All @@ -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<Self::NewBlockEvent>(account),
Expand All @@ -58,63 +92,122 @@ module Block {
aborts_if exists<BlockMetadata>(Signer::address_of(account));
}

public fun initialize_blockmetadata_v2(account: &signer) acquires BlockMetadata {
CoreAddresses::assert_genesis_address(account);

let block_meta_ref = borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS());

// create new resource base on current block metadata
if (!exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS())) {
move_to<BlockMetadataV2>(
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<Self::NewBlockEventV2>(account),
});
}
}

spec initialize_blockmetadata_v2 {
aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS();
aborts_if exists<BlockMetadata>(Signer::address_of(account));

ensures exists<BlockMetadataV2>(Signer::address_of(account));
}

/// Get the current block number
public fun get_current_block_number(): u64 acquires BlockMetadata {
borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).number
public fun get_current_block_number(): u64 acquires BlockMetadataV2 {
borrow_global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).number
}

spec get_current_block_number {
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
}

/// Get the hash of the parent block.
public fun get_parent_hash(): vector<u8> acquires BlockMetadata {
*&borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).parent_hash
public fun get_parent_hash(): vector<u8> acquires BlockMetadataV2 {
*&borrow_global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).parent_hash
}

spec get_parent_hash {
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
}

/// Gets the address of the author of the current block
public fun get_current_author(): address acquires BlockMetadata {
borrow_global<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS()).author
public fun get_current_author(): address acquires BlockMetadataV2 {
borrow_global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).author
}

spec get_current_author {
aborts_if !exists<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
}

public fun get_parents_hash(): vector<u8> acquires BlockMetadataV2 {
*&borrow_global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).parents_hash
}

spec get_parents_hash {
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
}

/// Call at block prologue
public fun process_block_metadata(account: &signer, parent_hash: vector<u8>,author: address, timestamp: u64, uncles:u64, number:u64) acquires BlockMetadata{
public fun process_block_metadata(account: &signer,
parent_hash: vector<u8>,
author: address,
timestamp: u64,
uncles: u64,
number: u64) acquires BlockMetadataV2 {
Self::process_block_metadata_v2(account, parent_hash, author, timestamp, uncles, number, Vector::empty<u8>())
}

spec process_block_metadata {
aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS();
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
aborts_if number != global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).number + 1;
}

/// Call at block prologue for flexidag
public fun process_block_metadata_v2(account: &signer,
parent_hash: vector<u8>,
author: address,
timestamp: u64,
uncles: u64,
number: u64,
parents_hash: vector<u8>) acquires BlockMetadataV2 {
CoreAddresses::assert_genesis_address(account);

let block_metadata_ref = borrow_global_mut<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS());
let block_metadata_ref = borrow_global_mut<BlockMetadataV2>(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<NewBlockEvent>(
&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<NewBlockEventV2>(
&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<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if number != global<BlockMetadata>(CoreAddresses::SPEC_GENESIS_ADDRESS()).number + 1;
spec process_block_metadata_v2 {
aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS();
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
aborts_if number != global<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS()).number + 1;
}

spec schema AbortsIfBlockMetadataNotExist {
aborts_if !exists<BlockMetadata>(CoreAddresses::GENESIS_ADDRESS());
aborts_if !exists<BlockMetadataV2>(CoreAddresses::GENESIS_ADDRESS());
}
}
}
}
2 changes: 1 addition & 1 deletion release/v12/sources/TransactionManager.move
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading