Skip to content

Commit

Permalink
update vmconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne007 committed Nov 10, 2021
1 parent 73ba7bb commit 72ce2cb
Show file tree
Hide file tree
Showing 33 changed files with 304 additions and 185 deletions.
152 changes: 76 additions & 76 deletions executor/tests/on_chain_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,79 +188,79 @@ fn test_modify_on_chain_txn_publish_option() -> Result<()> {
Ok(())
}

// #[stest::test]
// fn test_modify_on_chain_vm_config_option() -> Result<()> {
// let alice = Account::new();
// let bob = Account::new();
// let (chain_state, net) = prepare_genesis();
// let pre_mint_amount = net.genesis_config().pre_mine_amount;
// let action_type_tag = vm_config_type_tag();
//
// let one_day: u64 = 60 * 60 * 24 * 1000;
//
// // blockmeta txn is needed to create reward info.
// // block 1
// {
// let block_number = current_block_number(&chain_state) + 1;
// let block_timestamp = net.time_service().now_millis() + one_day * block_number - 1;
// let miner = Account::new();
// blockmeta_execute(
// &chain_state,
// BlockMetadata::new(
// HashValue::zero(),
// block_timestamp,
// *miner.address(),
// Some(miner.auth_key()),
// 0,
// block_number,
// net.chain_id(),
// 0,
// ),
// )?;
// }
// //create user for txn verifier
// let script_function = encode_create_account_script_function(
// net.stdlib_version(),
// stc_type_tag(),
// bob.address(),
// bob.auth_key(),
// pre_mint_amount / 8,
// );
// association_execute_should_success(
// &net,
// &chain_state,
// TransactionPayload::ScriptFunction(script_function),
// )?;
//
// //get gas_used
// let output = account_execute_with_output(&bob, &chain_state, empty_txn_payload());
// let old_gas_used = output.gas_used();
// let account_state_reader = AccountStateReader::new(&chain_state);
// let mut vm_config = account_state_reader
// .get_on_chain_config::<VMConfig>()?
// .unwrap();
// //set vm config parameter
// vm_config
// .gas_schedule
// .gas_constants
// .global_memory_per_byte_cost = InternalGasUnits::new(8);
// vm_config
// .gas_schedule
// .gas_constants
// .global_memory_per_byte_write_cost = InternalGasUnits::new(12);
// let vote_script = vote_vm_config_script(&net, vm_config);
//
// dao_vote_test(
// &alice,
// &chain_state,
// &net,
// vote_script,
// on_chain_config_type_tag(action_type_tag.clone()),
// execute_script_on_chain_config(&net, action_type_tag, 0u64),
// 0,
// )?;
// // get gas used of modified gas schedule
// let output = account_execute_with_output(&bob, &chain_state, empty_txn_payload());
// assert!(output.gas_used() > old_gas_used);
// Ok(())
// }
#[stest::test]
fn test_modify_on_chain_vm_config_option() -> Result<()> {
let alice = Account::new();
let bob = Account::new();
let (chain_state, net) = prepare_genesis();
let pre_mint_amount = net.genesis_config().pre_mine_amount;
let action_type_tag = vm_config_type_tag();

let one_day: u64 = 60 * 60 * 24 * 1000;

// blockmeta txn is needed to create reward info.
// block 1
{
let block_number = current_block_number(&chain_state) + 1;
let block_timestamp = net.time_service().now_millis() + one_day * block_number - 1;
let miner = Account::new();
blockmeta_execute(
&chain_state,
BlockMetadata::new(
HashValue::zero(),
block_timestamp,
*miner.address(),
Some(miner.auth_key()),
0,
block_number,
net.chain_id(),
0,
),
)?;
}
//create user for txn verifier
let script_function = encode_create_account_script_function(
net.stdlib_version(),
stc_type_tag(),
bob.address(),
bob.auth_key(),
pre_mint_amount / 8,
);
association_execute_should_success(
&net,
&chain_state,
TransactionPayload::ScriptFunction(script_function),
)?;

//get gas_used
let output = account_execute_with_output(&bob, &chain_state, empty_txn_payload());
let old_gas_used = output.gas_used();
let account_state_reader = AccountStateReader::new(&chain_state);
let mut vm_config = account_state_reader
.get_on_chain_config::<VMConfig>()?
.unwrap();
//set vm config parameter
vm_config
.gas_schedule
.gas_constants
.global_memory_per_byte_cost = InternalGasUnits::new(8);
vm_config
.gas_schedule
.gas_constants
.global_memory_per_byte_write_cost = InternalGasUnits::new(12);
let vote_script = vote_vm_config_script(&net, vm_config);

dao_vote_test(
&alice,
&chain_state,
&net,
vote_script,
on_chain_config_type_tag(action_type_tag.clone()),
execute_script_on_chain_config(&net, action_type_tag, 0u64),
0,
)?;
// get gas used of modified gas schedule
let output = account_execute_with_output(&bob, &chain_state, empty_txn_payload());
assert!(output.gas_used() > old_gas_used);
Ok(())
}
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
2 changes: 1 addition & 1 deletion genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ mod tests {
use starcoin_types::account_config::{genesis_address, ModuleUpgradeStrategy};
use starcoin_vm_types::account_config::association_address;
use starcoin_vm_types::genesis_config::ChainId;
use starcoin_vm_types::on_chain_config::{ConsensusConfig, VMConfig, Version};
use starcoin_vm_types::on_chain_config::{ConsensusConfig, Version};
use starcoin_vm_types::on_chain_config::{DaoConfig, TransactionPublishOption};
use starcoin_vm_types::on_chain_resource::Epoch;

Expand Down
Binary file added vm/stdlib/compiled/latest/stdlib/18_ChainId.mv
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed vm/stdlib/compiled/latest/stdlib/39_ChainId.mv
Binary file not shown.
11 changes: 11 additions & 0 deletions vm/stdlib/modules/ChainId.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ module ChainId {
id: u8
}

const MAIN_CHAIN_ID: u8 = 1;
const BARNARD_CHAIN_ID: u8 = 251;
const PROXIMA_CHAIN_ID: u8 = 252;
const HALLEY_CHAIN_ID: u8 = 253;
const DEV_CHAIN_ID: u8 = 254;
const TEST_CHAIN_ID: u8 = 255;

/// Publish the chain ID under the genesis account
public fun initialize(account: &signer, id: u8) {
Timestamp::assert_genesis();
Expand All @@ -35,6 +42,10 @@ module ChainId {
borrow_global<ChainId>(CoreAddresses::GENESIS_ADDRESS()).id
}

public fun is_dev(): bool acquires ChainId {
get() == DEV_CHAIN_ID
}

spec get {
aborts_if !exists<ChainId>(CoreAddresses::SPEC_GENESIS_ADDRESS());
ensures exists<ChainId>(CoreAddresses::SPEC_GENESIS_ADDRESS());
Expand Down
4 changes: 3 additions & 1 deletion vm/stdlib/modules/VMConfig.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module VMConfig {
use 0x1::Signer;
use 0x1::CoreAddresses;
use 0x1::Vector;
use 0x1::ChainId;
spec module {
pragma verify = false;
pragma aborts_if_is_strict;
Expand Down Expand Up @@ -167,14 +168,15 @@ module VMConfig {
}

public fun gas_constants(): GasConstants {
let min_price_per_gas_unit: u64 = if (ChainId::is_dev()) { 0 } else { 1 };
GasConstants {
global_memory_per_byte_cost: 4,
global_memory_per_byte_write_cost: 9,
min_transaction_gas_units: 600,
large_transaction_cutoff: 600,
instrinsic_gas_per_byte: 8,
maximum_number_of_gas_units: 40000000, //must less than base_block_gas_limit
min_price_per_gas_unit: 1,
min_price_per_gas_unit,
max_price_per_gas_unit: 10000,
max_transaction_size_in_bytes: 1024 * 128,
gas_unit_scaling_factor: 1,
Expand Down
97 changes: 91 additions & 6 deletions vm/stdlib/modules/doc/ChainId.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ The module provides chain id information.


- [Resource `ChainId`](#0x1_ChainId_ChainId)
- [Constants](#@Constants_0)
- [Function `initialize`](#0x1_ChainId_initialize)
- [Function `get`](#0x1_ChainId_get)
- [Specification](#@Specification_0)
- [Function `initialize`](#@Specification_0_initialize)
- [Function `get`](#@Specification_0_get)
- [Function `is_dev`](#0x1_ChainId_is_dev)
- [Specification](#@Specification_1)
- [Function `initialize`](#@Specification_1_initialize)
- [Function `get`](#@Specification_1_get)


<pre><code><b>use</b> <a href="CoreAddresses.md#0x1_CoreAddresses">0x1::CoreAddresses</a>;
Expand Down Expand Up @@ -48,6 +50,65 @@ chain id data structure.

</details>

<a name="@Constants_0"></a>

## Constants


<a name="0x1_ChainId_BARNARD_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_BARNARD_CHAIN_ID">BARNARD_CHAIN_ID</a>: u8 = 251;
</code></pre>



<a name="0x1_ChainId_DEV_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_DEV_CHAIN_ID">DEV_CHAIN_ID</a>: u8 = 254;
</code></pre>



<a name="0x1_ChainId_HALLEY_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_HALLEY_CHAIN_ID">HALLEY_CHAIN_ID</a>: u8 = 253;
</code></pre>



<a name="0x1_ChainId_MAIN_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_MAIN_CHAIN_ID">MAIN_CHAIN_ID</a>: u8 = 1;
</code></pre>



<a name="0x1_ChainId_PROXIMA_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_PROXIMA_CHAIN_ID">PROXIMA_CHAIN_ID</a>: u8 = 252;
</code></pre>



<a name="0x1_ChainId_TEST_CHAIN_ID"></a>



<pre><code><b>const</b> <a href="ChainId.md#0x1_ChainId_TEST_CHAIN_ID">TEST_CHAIN_ID</a>: u8 = 255;
</code></pre>



<a name="0x1_ChainId_initialize"></a>

## Function `initialize`
Expand Down Expand Up @@ -100,7 +161,31 @@ Return the chain ID of this chain

</details>

<a name="@Specification_0"></a>
<a name="0x1_ChainId_is_dev"></a>

## Function `is_dev`



<pre><code><b>public</b> <b>fun</b> <a href="ChainId.md#0x1_ChainId_is_dev">is_dev</a>(): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ChainId.md#0x1_ChainId_is_dev">is_dev</a>(): bool <b>acquires</b> <a href="ChainId.md#0x1_ChainId">ChainId</a> {
<a href="ChainId.md#0x1_ChainId_get">get</a>() == <a href="ChainId.md#0x1_ChainId_DEV_CHAIN_ID">DEV_CHAIN_ID</a>
}
</code></pre>



</details>

<a name="@Specification_1"></a>

## Specification

Expand All @@ -112,7 +197,7 @@ Return the chain ID of this chain



<a name="@Specification_0_initialize"></a>
<a name="@Specification_1_initialize"></a>

### Function `initialize`

Expand All @@ -131,7 +216,7 @@ Return the chain ID of this chain



<a name="@Specification_0_get"></a>
<a name="@Specification_1_get"></a>

### Function `get`

Expand Down
6 changes: 4 additions & 2 deletions vm/stdlib/modules/doc/VMConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
- [Function `initialize`](#@Specification_0_initialize)


<pre><code><b>use</b> <a href="Config.md#0x1_Config">0x1::Config</a>;
<pre><code><b>use</b> <a href="ChainId.md#0x1_ChainId">0x1::ChainId</a>;
<b>use</b> <a href="Config.md#0x1_Config">0x1::Config</a>;
<b>use</b> <a href="CoreAddresses.md#0x1_CoreAddresses">0x1::CoreAddresses</a>;
<b>use</b> <a href="Vector.md#0x1_Vector">0x1::Vector</a>;
</code></pre>
Expand Down Expand Up @@ -385,14 +386,15 @@ The <code><a href="VMConfig.md#0x1_VMConfig_GasCost">GasCost</a></code> tracks:


<pre><code><b>public</b> <b>fun</b> <a href="VMConfig.md#0x1_VMConfig_gas_constants">gas_constants</a>(): <a href="VMConfig.md#0x1_VMConfig_GasConstants">GasConstants</a> {
<b>let</b> min_price_per_gas_unit: u64 = <b>if</b> (<a href="ChainId.md#0x1_ChainId_is_dev">ChainId::is_dev</a>()) { 0 } <b>else</b> { 1 };
<a href="VMConfig.md#0x1_VMConfig_GasConstants">GasConstants</a> {
global_memory_per_byte_cost: 4,
global_memory_per_byte_write_cost: 9,
min_transaction_gas_units: 600,
large_transaction_cutoff: 600,
instrinsic_gas_per_byte: 8,
maximum_number_of_gas_units: 40000000, //must less than base_block_gas_limit
min_price_per_gas_unit: 1,
min_price_per_gas_unit,
max_price_per_gas_unit: 10000,
max_transaction_size_in_bytes: 1024 * 128,
gas_unit_scaling_factor: 1,
Expand Down
5 changes: 1 addition & 4 deletions vm/types/src/on_chain_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ pub use self::{
},
move_lang_version::MoveLanguageVersion,
version::{version_config_type_tag, Version, VERSION_CONFIG_IDENTIFIER},
vm_config::{
vm_config_type_tag, TransactionPublishOption, VMConfig, SCRIPT_HASH_LENGTH,
VM_CONFIG_IDENTIFIER,
},
vm_config::*,
};
pub use crate::on_chain_resource::GlobalTimeOnChain;

Expand Down
Loading

0 comments on commit 72ce2cb

Please sign in to comment.