diff --git a/contrib-contracts/modules/EthStateVerifier.move b/contrib-contracts/modules/EthStateVerifier.move index c60006353b..9c339c2833 100644 --- a/contrib-contracts/modules/EthStateVerifier.move +++ b/contrib-contracts/modules/EthStateVerifier.move @@ -4,15 +4,15 @@ module Bytes { public fun slice(data: &vector, start: u64, end: u64): vector { let i = start; - let result = Vector::empty(); - let data_len = Vector::length(data); + let result = vector::empty(); + let data_len = vector::length(data); let actual_end = if (end < data_len) { end } else { data_len }; while (i < actual_end){ - Vector::push_back(&mut result, *Vector::borrow(data, i)); + vector::push_back(&mut result, *vector::borrow(data, i)); i = i + 1; }; result @@ -28,14 +28,14 @@ module RLP { /// Nested arrays are not supported. public fun decode_list(data: &vector): vector> { let (decoded, consumed) = decode(data, 0); - assert!(consumed == Vector::length(data), INVALID_RLP_DATA); + assert!(consumed == vector::length(data), INVALID_RLP_DATA); decoded } fun decode(data: &vector, offset: u64): (vector>, u64) { - let data_len = Vector::length(data); + let data_len = vector::length(data); assert!(offset < data_len, DATA_TOO_SHORT); - let first_byte = *Vector::borrow(data, offset); + let first_byte = *vector::borrow(data, offset); if (first_byte >= 248u8) { // 0xf8 let length_of_length = ((first_byte - 247u8) as u64); assert!(offset + length_of_length < data_len, DATA_TOO_SHORT); @@ -53,24 +53,24 @@ module RLP { assert!(offset + length_of_length + length < data_len, DATA_TOO_SHORT); let bytes = Bytes::slice(data, offset + 1 + length_of_length, offset + 1 + length_of_length + length); - (Vector::singleton(bytes), 1+length_of_length+length) + (vector::singleton(bytes), 1+length_of_length+length) } else if (first_byte >= 128u8) { // 0x80 let length = ((first_byte - 128u8) as u64); assert!(offset + length < data_len, DATA_TOO_SHORT); let bytes = Bytes::slice(data, offset + 1, offset + 1 + length); - (Vector::singleton(bytes), 1+length) + (vector::singleton(bytes), 1+length) } else { let bytes = Bytes::slice(data, offset, offset + 1); - (Vector::singleton(bytes), 1) + (vector::singleton(bytes), 1) } } fun decode_children(data: &vector, offset: u64, child_offset: u64, length: u64): (vector>, u64) { - let result = Vector::empty(); + let result = vector::empty(); while (child_offset < offset + 1 + length) { let (decoded, consumed) = decode(data, child_offset); - Vector::append(&mut result, decoded); + vector::append(&mut result, decoded); child_offset = child_offset + consumed; assert!(child_offset <= offset + 1 + length, DATA_TOO_SHORT); }; @@ -82,7 +82,7 @@ module RLP { let result = 0; let i = 0u8; while(i < length) { - result = result * 256 + (*Vector::borrow(data, offset + (i as u64)) as u64); + result = result * 256 + (*vector::borrow(data, offset + (i as u64)) as u64); i = i + 1; }; result @@ -103,13 +103,13 @@ module EthStateVerifier { (n1, n2) } public fun to_nibbles(bytes: &vector): vector { - let result = Vector::empty(); + let result = vector::empty(); let i = 0; - let data_len = Vector::length(bytes); + let data_len = vector::length(bytes); while (i < data_len) { - let (a, b) = to_nibble(*Vector::borrow(bytes, i)); - Vector::push_back(&mut result, a); - Vector::push_back(&mut result, b); + let (a, b) = to_nibble(*vector::borrow(bytes, i)); + vector::push_back(&mut result, a); + vector::push_back(&mut result, b); i = i + 1; }; @@ -124,59 +124,59 @@ module EthStateVerifier { key_index: u64, proof_index: u64, ): bool { - if (proof_index >= Vector::length(&proof)) { + if (proof_index >= vector::length(&proof)) { return false }; - let node = Vector::borrow(&proof, proof_index); + let node = vector::borrow(&proof, proof_index); let dec = RLP::decode_list(node); // trie root is always a hash - if (key_index == 0 || Vector::length(node) >= 32u64) { + if (key_index == 0 || vector::length(node) >= 32u64) { if (Hash::keccak_256(*node) != expected_root) { return false } } else { // and if rlp < 32 bytes, then it is not hashed - let root = Vector::borrow(&dec, 0); + let root = vector::borrow(&dec, 0); if (root != &expected_root) { return false } }; - let rlp_len = Vector::length(&dec); + let rlp_len = vector::length(&dec); // branch node. if (rlp_len == 17) { - if (key_index >= Vector::length(&key)) { + if (key_index >= vector::length(&key)) { // value stored in the branch - let item = Vector::borrow(&dec, 16); + let item = vector::borrow(&dec, 16); if (item == &expected_value) { return true } } else { // down the rabbit hole. - let index = Vector::borrow(&key, key_index); - let new_expected_root = Vector::borrow(&dec, (*index as u64)); - if (Vector::length(new_expected_root) != 0) { + let index = vector::borrow(&key, key_index); + let new_expected_root = vector::borrow(&dec, (*index as u64)); + if (vector::length(new_expected_root) != 0) { return verify_inner(*new_expected_root, key, proof, expected_value, key_index + 1, proof_index + 1) } }; } else if (rlp_len == 2) { - let node_key = Vector::borrow(&dec, 0); - let node_value = Vector::borrow(&dec, 1); - let (prefix, nibble) = to_nibble(*Vector::borrow(node_key, 0)); + let node_key = vector::borrow(&dec, 0); + let node_value = vector::borrow(&dec, 1); + let (prefix, nibble) = to_nibble(*vector::borrow(node_key, 0)); if (prefix == 0) { // even extension node - let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, Vector::length(node_key))); - let extension_length = Vector::length(&shared_nibbles); + let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, vector::length(node_key))); + let extension_length = vector::length(&shared_nibbles); if (shared_nibbles == Bytes::slice(&key, key_index, key_index + extension_length)) { return verify_inner(*node_value, key, proof, expected_value, key_index + extension_length, proof_index + 1) } } else if (prefix == 1) { // odd extension node - let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, Vector::length(node_key))); - let extension_length = Vector::length(&shared_nibbles); - if (nibble == *Vector::borrow(&key, key_index) && + let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, vector::length(node_key))); + let extension_length = vector::length(&shared_nibbles); + if (nibble == *vector::borrow(&key, key_index) && shared_nibbles == Bytes::slice( &key, @@ -187,21 +187,21 @@ module EthStateVerifier { }; } else if (prefix == 2) { // even leaf node - let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, Vector::length(node_key))); - return shared_nibbles == Bytes::slice(&key, key_index, Vector::length(&key)) && &expected_value == node_value + let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, vector::length(node_key))); + return shared_nibbles == Bytes::slice(&key, key_index, vector::length(&key)) && &expected_value == node_value } else if (prefix == 3) { // odd leaf node - let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, Vector::length(node_key))); + let shared_nibbles = to_nibbles(&Bytes::slice(node_key, 1, vector::length(node_key))); return &expected_value == node_value && - nibble == *Vector::borrow(&key, key_index) && + nibble == *vector::borrow(&key, key_index) && shared_nibbles == - Bytes::slice(&key, key_index + 1, Vector::length(&key)) + Bytes::slice(&key, key_index + 1, vector::length(&key)) } else { // invalid proof abort INVALID_PROOF }; }; - return Vector::length(&expected_value) == 0 + return vector::length(&expected_value) == 0 } public fun verify( diff --git a/contrib-contracts/modules/MerkleDistributor.move b/contrib-contracts/modules/MerkleDistributor.move index 1c609b67f1..e7d61ad86d 100644 --- a/contrib-contracts/modules/MerkleDistributor.move +++ b/contrib-contracts/modules/MerkleDistributor.move @@ -25,9 +25,9 @@ module MerkleProof { public fun verify(proof: &vector>, root: &vector, leaf: vector): bool { let computed_hash = leaf; let i = 0; - let proof_length = Vector::length(proof); + let proof_length = vector::length(proof); while(i < proof_length) { - let sibling = Vector::borrow(proof, i); + let sibling = vector::borrow(proof, i); // computed_hash is left. if (Compare::cmp_bytes(&computed_hash,sibling) < 2) { let concated = concat(computed_hash, *sibling); @@ -45,7 +45,7 @@ module MerkleProof { fun concat(v1: vector, v2: vector): vector { - Vector::append(&mut v1, v2); + vector::append(&mut v1, v2); v1 } } @@ -77,10 +77,10 @@ module MerkleDistributor { if (bitmap_count * 128 < leaves) { bitmap_count = bitmap_count + 1; }; - let claimed_bitmap = Vector::empty(); + let claimed_bitmap = vector::empty(); let j = 0; while (j < bitmap_count) { - Vector::push_back(&mut claimed_bitmap, 0u128); + vector::push_back(&mut claimed_bitmap, 0u128); j = j + 1; }; let distribution = MerkleDistribution{ @@ -129,7 +129,7 @@ module MerkleDistributor { fun is_claimed_(distribution: &MerkleDistribution, index: u64): bool { let claimed_word_index = index / 128; let claimed_bit_index = ((index % 128) as u8); - let word = Vector::borrow(&distribution.claimed_bitmap, claimed_word_index); + let word = vector::borrow(&distribution.claimed_bitmap, claimed_word_index); let mask = 1u128 << claimed_bit_index; (*word & mask) == mask } @@ -137,17 +137,17 @@ module MerkleDistributor { fun set_claimed_(distribution: &mut MerkleDistribution, index: u64) { let claimed_word_index = index / 128; let claimed_bit_index = ((index % 128) as u8); - let word = Vector::borrow_mut(&mut distribution.claimed_bitmap, claimed_word_index); + let word = vector::borrow_mut(&mut distribution.claimed_bitmap, claimed_word_index); // word | (1 << bit_index) let mask = 1u128 << claimed_bit_index; *word = (*word | mask); } fun encode_leaf(index: &u64, account: &address, amount: &u128): vector { - let leaf = Vector::empty(); - Vector::append(&mut leaf, BCS::to_bytes(index)); - Vector::append(&mut leaf, BCS::to_bytes(account)); - Vector::append(&mut leaf, BCS::to_bytes(amount)); + let leaf = vector::empty(); + vector::append(&mut leaf, BCS::to_bytes(index)); + vector::append(&mut leaf, BCS::to_bytes(account)); + vector::append(&mut leaf, BCS::to_bytes(amount)); leaf } } diff --git a/contrib-contracts/modules/StarcoinVerifier.move b/contrib-contracts/modules/StarcoinVerifier.move index 57258bdfdf..9ce9a905e6 100644 --- a/contrib-contracts/modules/StarcoinVerifier.move +++ b/contrib-contracts/modules/StarcoinVerifier.move @@ -42,9 +42,9 @@ address StarcoinAssociation { let leaf_node = Node { hash1: copy address_hash, hash2: account_state_root_hash}; let current_hash = StructuredHash::hash(SPARSE_MERKLE_LEAF_NODE, &leaf_node); let i = 0; - let proof_length = Vector::length(&proofs); + let proof_length = vector::length(&proofs); while (i < proof_length) { - let sibling = *Vector::borrow(&proofs, i); + let sibling = *vector::borrow(&proofs, i); let bit = Bit::get_bit(&address_hash, proof_length - i - 1); let internal_node = if (bit) { Node {hash1: sibling, hash2: current_hash} @@ -71,7 +71,7 @@ address StarcoinAssociation { fun concat(v1: &vector, v2: vector): vector { let data = *v1; - Vector::append(&mut data, v2); + vector::append(&mut data, v2); data } @@ -81,7 +81,7 @@ address StarcoinAssociation { public fun get_bit(data: &vector, index: u64): bool { let pos = index / 8; let bit = (7 - index % 8); - (*Vector::borrow(data, pos) >> (bit as u8)) & 1u8 != 0 + (*vector::borrow(data, pos) >> (bit as u8)) & 1u8 != 0 } } } \ No newline at end of file diff --git a/executor/tests/block_test.rs b/executor/tests/block_test.rs new file mode 100644 index 0000000000..a5e86701fa --- /dev/null +++ b/executor/tests/block_test.rs @@ -0,0 +1,55 @@ +use log::info; +use starcoin_types::account_address::AccountAddress; +use starcoin_vm_types::account_config::genesis_address; +use starcoin_vm_types::{ + event::{EventHandle, EventKey}, + on_chain_resource::BlockMetadata, + state_view::StateReaderExt, +}; +use test_helper::executor::prepare_genesis; + +#[stest::test] +fn test_block_metadata_bcs_deserialize() -> anyhow::Result<()> { + let (chain_state, _net) = prepare_genesis(); + + let block_metadata = BlockMetadata { + number: 0, + parent_hash: Default::default(), + author: AccountAddress::ONE, + uncles: 0, + parents_hash: vec![], + new_block_events: EventHandle::new(EventKey::new(1, AccountAddress::ONE), 1), + }; + let bcs_block_metadata = bcs_ext::to_bytes(&block_metadata)?; + info!( + "block_metadata: {:?}, length: {}", + bcs_block_metadata, + bcs_block_metadata.len() + ); + + let onchain_data = chain_state.get_resource_type_bytes::(genesis_address())?; + info!( + "onchain block_metadata: {:?}, data len: {}", + onchain_data.to_vec(), + onchain_data.len() + ); + let on_chain_block_data = + bcs_ext::from_bytes::(onchain_data.to_vec().as_slice())?; + assert_eq!(on_chain_block_data.number, 0); + + assert_eq!( + chain_state + .get_resource_type::(genesis_address())? + .number, + 0 + ); + + // let output = bcs_ext::from_bytes::(bcs.as_slice())?; + // assert_eq!(output.number, block_metadata.number); + // + // let data = chain_state.get_resource_type::(genesis_address())?; + // assert_ne!(data.number, 0); + // assert!(!block_metadata.number > 0); + + Ok(()) +} diff --git a/release/basic.v0.1.0.blob b/release/basic.v0.1.0.blob new file mode 100644 index 0000000000..cb41639931 Binary files /dev/null and b/release/basic.v0.1.0.blob differ diff --git a/vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs b/vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs index e2d356bbc5..6ffc4e83d7 100644 --- a/vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs +++ b/vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs @@ -198,6 +198,64 @@ pub enum EntryFunctionCall { proposal_id: u64, }, + /// Once the proposal is agreed, anyone can call the method to make the proposal happen. + DaoModifyConfigProposalExecute { + token_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + }, + + /// Entrypoint for the proposal. + DaoModifyConfigProposalPropose { + token_t: TypeTag, + voting_delay: u64, + voting_period: u64, + voting_quorum_rate: u8, + min_action_delay: u64, + exec_delay: u64, + }, + + DaoVoteScriptsCastVote { + token: TypeTag, + action_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + agree: bool, + votes: u128, + }, + + /// Let user change their vote during the voting time. + DaoVoteScriptsFlipVote { + token_t: TypeTag, + action_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + }, + + /// revoke all votes on a proposal + DaoVoteScriptsRevokeVote { + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + }, + + /// revoke some votes on a proposal + DaoVoteScriptsRevokeVoteOfPower { + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + power: u128, + }, + + DaoVoteScriptsUnstakeVote { + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + }, + /// Add `amount` of coins to the delegation pool `pool_address`. DelegationPoolAddStake { pool_address: AccountAddress, @@ -603,21 +661,6 @@ pub enum EntryFunctionCall { value: u128, }, - OraclePriceScriptInitDataSource { - oracle_t: TypeTag, - init_value: u128, - }, - - OraclePriceScriptRegisterOracle { - oracle_t: TypeTag, - precision: u8, - }, - - OraclePriceScriptUpdate { - oracle_t: TypeTag, - value: u128, - }, - /// Creates a new resource account and rotates the authentication key to either /// the optional auth key if it is non-empty (though auth keys are 32-bytes) /// or the source accounts current auth key. @@ -1254,6 +1297,72 @@ impl EntryFunctionCall { proposer_address, proposal_id, } => dao_queue_proposal_action(token_t, action_t, proposer_address, proposal_id), + DaoModifyConfigProposalExecute { + token_t, + proposer_address, + proposal_id, + } => dao_modify_config_proposal_execute(token_t, proposer_address, proposal_id), + DaoModifyConfigProposalPropose { + token_t, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + exec_delay, + } => dao_modify_config_proposal_propose( + token_t, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + exec_delay, + ), + DaoVoteScriptsCastVote { + token, + action_t, + proposer_address, + proposal_id, + agree, + votes, + } => dao_vote_scripts_cast_vote( + token, + action_t, + proposer_address, + proposal_id, + agree, + votes, + ), + DaoVoteScriptsFlipVote { + token_t, + action_t, + proposer_address, + proposal_id, + } => dao_vote_scripts_flip_vote(token_t, action_t, proposer_address, proposal_id), + DaoVoteScriptsRevokeVote { + token, + action, + proposer_address, + proposal_id, + } => dao_vote_scripts_revoke_vote(token, action, proposer_address, proposal_id), + DaoVoteScriptsRevokeVoteOfPower { + token, + action, + proposer_address, + proposal_id, + power, + } => dao_vote_scripts_revoke_vote_of_power( + token, + action, + proposer_address, + proposal_id, + power, + ), + DaoVoteScriptsUnstakeVote { + token, + action, + proposer_address, + proposal_id, + } => dao_vote_scripts_unstake_vote(token, action, proposer_address, proposal_id), DelegationPoolAddStake { pool_address, amount, @@ -1525,17 +1634,6 @@ impl EntryFunctionCall { OraclePriceUpdateEntry { oracle_t, value } => { oracle_price_update_entry(oracle_t, value) } - OraclePriceScriptInitDataSource { - oracle_t, - init_value, - } => oracle_price_script_init_data_source(oracle_t, init_value), - OraclePriceScriptRegisterOracle { - oracle_t, - precision, - } => oracle_price_script_register_oracle(oracle_t, precision), - OraclePriceScriptUpdate { oracle_t, value } => { - oracle_price_script_update(oracle_t, value) - } ResourceAccountCreateResourceAccount { seed, optional_auth_key, @@ -2280,6 +2378,161 @@ pub fn dao_queue_proposal_action( )) } +/// Once the proposal is agreed, anyone can call the method to make the proposal happen. +pub fn dao_modify_config_proposal_execute( + token_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_modify_config_proposal").to_owned(), + ), + ident_str!("execute").to_owned(), + vec![token_t], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + ], + )) +} + +/// Entrypoint for the proposal. +pub fn dao_modify_config_proposal_propose( + token_t: TypeTag, + voting_delay: u64, + voting_period: u64, + voting_quorum_rate: u8, + min_action_delay: u64, + exec_delay: u64, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_modify_config_proposal").to_owned(), + ), + ident_str!("propose").to_owned(), + vec![token_t], + vec![ + bcs::to_bytes(&voting_delay).unwrap(), + bcs::to_bytes(&voting_period).unwrap(), + bcs::to_bytes(&voting_quorum_rate).unwrap(), + bcs::to_bytes(&min_action_delay).unwrap(), + bcs::to_bytes(&exec_delay).unwrap(), + ], + )) +} + +pub fn dao_vote_scripts_cast_vote( + token: TypeTag, + action_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + agree: bool, + votes: u128, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_vote_scripts").to_owned(), + ), + ident_str!("cast_vote").to_owned(), + vec![token, action_t], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + bcs::to_bytes(&agree).unwrap(), + bcs::to_bytes(&votes).unwrap(), + ], + )) +} + +/// Let user change their vote during the voting time. +pub fn dao_vote_scripts_flip_vote( + token_t: TypeTag, + action_t: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_vote_scripts").to_owned(), + ), + ident_str!("flip_vote").to_owned(), + vec![token_t, action_t], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + ], + )) +} + +/// revoke all votes on a proposal +pub fn dao_vote_scripts_revoke_vote( + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_vote_scripts").to_owned(), + ), + ident_str!("revoke_vote").to_owned(), + vec![token, action], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + ], + )) +} + +/// revoke some votes on a proposal +pub fn dao_vote_scripts_revoke_vote_of_power( + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, + power: u128, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_vote_scripts").to_owned(), + ), + ident_str!("revoke_vote_of_power").to_owned(), + vec![token, action], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + bcs::to_bytes(&power).unwrap(), + ], + )) +} + +pub fn dao_vote_scripts_unstake_vote( + token: TypeTag, + action: TypeTag, + proposer_address: AccountAddress, + proposal_id: u64, +) -> TransactionPayload { + TransactionPayload::EntryFunction(EntryFunction::new( + ModuleId::new( + AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + ident_str!("dao_vote_scripts").to_owned(), + ), + ident_str!("unstake_vote").to_owned(), + vec![token, action], + vec![ + bcs::to_bytes(&proposer_address).unwrap(), + bcs::to_bytes(&proposal_id).unwrap(), + ], + )) +} + /// Add `amount` of coins to the delegation pool `pool_address`. pub fn delegation_pool_add_stake(pool_address: AccountAddress, amount: u64) -> TransactionPayload { TransactionPayload::EntryFunction(EntryFunction::new( @@ -3318,45 +3571,6 @@ pub fn oracle_price_update_entry(oracle_t: TypeTag, value: u128) -> TransactionP )) } -pub fn oracle_price_script_init_data_source( - oracle_t: TypeTag, - init_value: u128, -) -> TransactionPayload { - TransactionPayload::EntryFunction(EntryFunction::new( - ModuleId::new( - AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), - ident_str!("oracle_price_script").to_owned(), - ), - ident_str!("init_data_source").to_owned(), - vec![oracle_t], - vec![bcs::to_bytes(&init_value).unwrap()], - )) -} - -pub fn oracle_price_script_register_oracle(oracle_t: TypeTag, precision: u8) -> TransactionPayload { - TransactionPayload::EntryFunction(EntryFunction::new( - ModuleId::new( - AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), - ident_str!("oracle_price_script").to_owned(), - ), - ident_str!("register_oracle").to_owned(), - vec![oracle_t], - vec![bcs::to_bytes(&precision).unwrap()], - )) -} - -pub fn oracle_price_script_update(oracle_t: TypeTag, value: u128) -> TransactionPayload { - TransactionPayload::EntryFunction(EntryFunction::new( - ModuleId::new( - AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), - ident_str!("oracle_price_script").to_owned(), - ), - ident_str!("update").to_owned(), - vec![oracle_t], - vec![bcs::to_bytes(&value).unwrap()], - )) -} - /// Creates a new resource account and rotates the authentication key to either /// the optional auth key if it is non-empty (though auth keys are 32-bytes) /// or the source accounts current auth key. @@ -5057,6 +5271,109 @@ mod decoder { } } + pub fn dao_modify_config_proposal_execute( + payload: &TransactionPayload, + ) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoModifyConfigProposalExecute { + token_t: script.ty_args().get(0)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_modify_config_proposal_propose( + payload: &TransactionPayload, + ) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoModifyConfigProposalPropose { + token_t: script.ty_args().get(0)?.clone(), + voting_delay: bcs::from_bytes(script.args().get(0)?).ok()?, + voting_period: bcs::from_bytes(script.args().get(1)?).ok()?, + voting_quorum_rate: bcs::from_bytes(script.args().get(2)?).ok()?, + min_action_delay: bcs::from_bytes(script.args().get(3)?).ok()?, + exec_delay: bcs::from_bytes(script.args().get(4)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_vote_scripts_cast_vote(payload: &TransactionPayload) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoVoteScriptsCastVote { + token: script.ty_args().get(0)?.clone(), + action_t: script.ty_args().get(1)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + agree: bcs::from_bytes(script.args().get(2)?).ok()?, + votes: bcs::from_bytes(script.args().get(3)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_vote_scripts_flip_vote(payload: &TransactionPayload) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoVoteScriptsFlipVote { + token_t: script.ty_args().get(0)?.clone(), + action_t: script.ty_args().get(1)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_vote_scripts_revoke_vote(payload: &TransactionPayload) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoVoteScriptsRevokeVote { + token: script.ty_args().get(0)?.clone(), + action: script.ty_args().get(1)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_vote_scripts_revoke_vote_of_power( + payload: &TransactionPayload, + ) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoVoteScriptsRevokeVoteOfPower { + token: script.ty_args().get(0)?.clone(), + action: script.ty_args().get(1)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + power: bcs::from_bytes(script.args().get(2)?).ok()?, + }) + } else { + None + } + } + + pub fn dao_vote_scripts_unstake_vote( + payload: &TransactionPayload, + ) -> Option { + if let TransactionPayload::EntryFunction(script) = payload { + Some(EntryFunctionCall::DaoVoteScriptsUnstakeVote { + token: script.ty_args().get(0)?.clone(), + action: script.ty_args().get(1)?.clone(), + proposer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(1)?).ok()?, + }) + } else { + None + } + } + pub fn delegation_pool_add_stake(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DelegationPoolAddStake { @@ -5782,43 +6099,6 @@ mod decoder { } } - pub fn oracle_price_script_init_data_source( - payload: &TransactionPayload, - ) -> Option { - if let TransactionPayload::EntryFunction(script) = payload { - Some(EntryFunctionCall::OraclePriceScriptInitDataSource { - oracle_t: script.ty_args().get(0)?.clone(), - init_value: bcs::from_bytes(script.args().get(0)?).ok()?, - }) - } else { - None - } - } - - pub fn oracle_price_script_register_oracle( - payload: &TransactionPayload, - ) -> Option { - if let TransactionPayload::EntryFunction(script) = payload { - Some(EntryFunctionCall::OraclePriceScriptRegisterOracle { - oracle_t: script.ty_args().get(0)?.clone(), - precision: bcs::from_bytes(script.args().get(0)?).ok()?, - }) - } else { - None - } - } - - pub fn oracle_price_script_update(payload: &TransactionPayload) -> Option { - if let TransactionPayload::EntryFunction(script) = payload { - Some(EntryFunctionCall::OraclePriceScriptUpdate { - oracle_t: script.ty_args().get(0)?.clone(), - value: bcs::from_bytes(script.args().get(0)?).ok()?, - }) - } else { - None - } - } - pub fn resource_account_create_resource_account( payload: &TransactionPayload, ) -> Option { @@ -6923,6 +7203,34 @@ static SCRIPT_FUNCTION_DECODER_MAP: once_cell::sync::Lazyuse 0x1::block_reward_config; use 0x1::coin; use 0x1::create_signer; +use 0x1::dao_treasury_withdraw_proposal; use 0x1::debug; use 0x1::error; use 0x1::event; @@ -28,7 +29,6 @@ The module provide block rewarding calculation logic. use 0x1::string; use 0x1::system_addresses; use 0x1::treasury; -use 0x1::treasury_withdraw_dao_proposal; use 0x1::vector; @@ -336,7 +336,7 @@ Process the given block rewards. debug::print(&std::string::utf8(b"block_reward::process_block_reward | treasury_balance: ")); debug::print(&treasury_balance); if (block_reward > 0) { - let reward = treasury_withdraw_dao_proposal::withdraw_for_block_reward<STC>(account, block_reward); + let reward = dao_treasury_withdraw_proposal::withdraw_for_block_reward<STC>(account, block_reward); coin::merge(&mut total_reward, reward); }; }; diff --git a/vm/framework/starcoin-framework/doc/coin.md b/vm/framework/starcoin-framework/doc/coin.md index c781a11e7d..67d788dfb0 100644 --- a/vm/framework/starcoin-framework/doc/coin.md +++ b/vm/framework/starcoin-framework/doc/coin.md @@ -1061,6 +1061,16 @@ The BurnRefReceipt does not match the BurnRef to be returned. + + +The coin decimal too long + + +
const ECOIN_COIN_DECIMAL_TOO_LARGE: u64 = 29;
+
+ + + The coin converison map is not created yet. @@ -1271,6 +1281,15 @@ The TransferRefReceipt does not match the TransferRef to be returned. + + + + +
const MAX_COIN_DECIMAL: u8 = 38;
+
+ + + @@ -2673,15 +2692,25 @@ Returns the amount of coin in existence.
public fun supply<CoinType>(): Option<u128> acquires CoinInfo, CoinConversionMap {
+    debug::print(&std::string::utf8(b"dao::supply | entered"));
+
     let coin_supply = coin_supply<CoinType>();
     let metadata = paired_metadata<CoinType>();
+
+    debug::print(&std::string::utf8(b"dao::supply | metadata"));
+    debug::print(&metadata);
+
     if (option::is_some(&metadata)) {
-        let fungible_asset_supply = fungible_asset::supply(option::extract(&mut metadata));
-        if (option::is_some(&coin_supply)) {
+        let fungible_asset_supply =
+            fungible_asset::supply(option::extract(&mut metadata));
+        if (option::is_some(&coin_supply) &&
+            option::is_some(&fungible_asset_supply)) {
             let supply = option::borrow_mut(&mut coin_supply);
             *supply = *supply + option::destroy_some(fungible_asset_supply);
         };
     };
+    debug::print(&std::string::utf8(b"dao::supply | exit, coin supply"));
+    debug::print(&coin_supply);
     coin_supply
 }
 
@@ -3251,6 +3280,7 @@ Same as initialize but supply can be initialized to parallelizable assert!(string::length(&name) <= MAX_COIN_NAME_LENGTH, error::invalid_argument(ECOIN_NAME_TOO_LONG)); assert!(string::length(&symbol) <= MAX_COIN_SYMBOL_LENGTH, error::invalid_argument(ECOIN_SYMBOL_TOO_LONG)); + assert!(decimals < MAX_COIN_DECIMAL, error::invalid_argument(ECOIN_COIN_DECIMAL_TOO_LARGE)); let coin_info = CoinInfo<CoinType> { name, @@ -4817,6 +4847,26 @@ Account is not frozen and sufficient balance. + + + + +
schema WithdrawAbortsIf<CoinType> {
+    account: &signer;
+    amount: u64;
+    let account_addr = signer::address_of(account);
+    let coin_store = global<CoinStore<CoinType>>(account_addr);
+    let balance = coin_store.coin.value;
+    // This enforces high-level requirement 6:
+    aborts_if !exists<CoinStore<CoinType>>(account_addr);
+    // This enforces high-level requirement 8:
+    aborts_if coin_store.frozen;
+    aborts_if balance < amount;
+}
+
+ + + ### Function `mint_internal` diff --git a/vm/framework/starcoin-framework/doc/dao.md b/vm/framework/starcoin-framework/doc/dao.md index b003ecbccd..6be881df98 100644 --- a/vm/framework/starcoin-framework/doc/dao.md +++ b/vm/framework/starcoin-framework/doc/dao.md @@ -78,12 +78,14 @@
use 0x1::account;
 use 0x1::coin;
+use 0x1::debug;
 use 0x1::error;
 use 0x1::event;
 use 0x1::on_chain_config;
 use 0x1::option;
 use 0x1::signer;
 use 0x1::stc_util;
+use 0x1::string;
 use 0x1::timestamp;
 use 0x1::treasury;
 
@@ -496,6 +498,15 @@ User vote info. + + + + +
const ERR_TOKEN_NOT_REGISTER: u64 = 1411;
+
+ + + @@ -659,6 +670,8 @@ propose a proposal. action: ActionT, action_delay: u64, ) acquires DaoGlobalInfo { + debug::print(&std::string::utf8(b"dao::proposal | Entered")); + if (action_delay == 0) { action_delay = min_action_delay<TokenT>(); } else { @@ -668,6 +681,9 @@ propose a proposal. let proposer = signer::address_of(signer); let start_time = timestamp::now_milliseconds() + voting_delay<TokenT>(); let quorum_votes = quorum_votes<TokenT>(); + + debug::print(&std::string::utf8(b"dao::proposal | Proposal {")); + let proposal = Proposal<TokenT, ActionT> { id: proposal_id, proposer, @@ -683,10 +699,15 @@ propose a proposal. move_to(signer, proposal); // emit event let gov_info = borrow_global_mut<DaoGlobalInfo<TokenT>>(stc_util::token_issuer<TokenT>()); + + debug::print(&std::string::utf8(b"dao::proposal | emit event")); + event::emit_event( &mut gov_info.proposal_create_event, ProposalCreatedEvent { proposal_id, proposer }, ); + + debug::print(&std::string::utf8(b"dao::proposal | Exited")); } @@ -1460,11 +1481,20 @@ Quorum votes to make proposal pass.
public fun quorum_votes<TokenT>(): u128 {
-    let market_cap = option::destroy_some(coin::supply<TokenT>());
+    debug::print(&std::string::utf8(b"dao::quorum_votes | entered "));
+
+    let supply = coin::supply<TokenT>();
+    debug::print(&std::string::utf8(b"dao::quorum_votes | supply "));
+    debug::print(&supply);
+
+    assert!(option::is_some(&supply), error::invalid_state(ERR_TOKEN_NOT_REGISTER));
+
+    let market_cap = option::destroy_some(supply);
     let balance_in_treasury = treasury::balance<TokenT>();
     let supply = market_cap - balance_in_treasury;
     let rate = voting_quorum_rate<TokenT>();
     let rate = (rate as u128);
+    debug::print(&std::string::utf8(b"dao::quorum_votes | exited "));
     supply * rate / 100
 }
 
diff --git a/vm/framework/starcoin-framework/doc/dao_modify_config_proposal.md b/vm/framework/starcoin-framework/doc/dao_modify_config_proposal.md new file mode 100644 index 0000000000..ed87e9f411 --- /dev/null +++ b/vm/framework/starcoin-framework/doc/dao_modify_config_proposal.md @@ -0,0 +1,334 @@ + + + +# Module `0x1::dao_modify_config_proposal` + +A proposal module which is used to modify Token's DAO configuration. + + +- [Resource `DaoConfigModifyCapability`](#0x1_dao_modify_config_proposal_DaoConfigModifyCapability) +- [Struct `DaoConfigUpdate`](#0x1_dao_modify_config_proposal_DaoConfigUpdate) +- [Constants](#@Constants_0) +- [Function `plugin`](#0x1_dao_modify_config_proposal_plugin) +- [Function `propose`](#0x1_dao_modify_config_proposal_propose) +- [Function `execute`](#0x1_dao_modify_config_proposal_execute) +- [Specification](#@Specification_1) + - [Function `plugin`](#@Specification_1_plugin) + - [Function `propose`](#@Specification_1_propose) + - [Function `execute`](#@Specification_1_execute) + + +
use 0x1::dao;
+use 0x1::debug;
+use 0x1::error;
+use 0x1::on_chain_config;
+use 0x1::signer;
+use 0x1::stc_util;
+use 0x1::string;
+
+ + + + + +## Resource `DaoConfigModifyCapability` + +A wrapper of on_chain_config::ModifyConfigCapability<dao::DaoConfig<TokenT>>. + + +
struct DaoConfigModifyCapability<TokenT> has key
+
+ + + +
+Fields + + +
+
+cap: on_chain_config::ModifyConfigCapability<dao::DaoConfig<TokenT>> +
+
+ +
+
+ + +
+ + + +## Struct `DaoConfigUpdate` + +a proposal action to update dao config. +if any field is 0, that means the proposal want to update. + + +
struct DaoConfigUpdate has copy, drop, store
+
+ + + +
+Fields + + +
+
+voting_delay: u64 +
+
+ new voting delay setting. +
+
+voting_period: u64 +
+
+ new voting period setting. +
+
+voting_quorum_rate: u8 +
+
+ new voting quorum rate setting. +
+
+min_action_delay: u64 +
+
+ new min action delay setting. +
+
+ + +
+ + + +## Constants + + + + + + +
const ERR_NOT_AUTHORIZED: u64 = 401;
+
+ + + + + + + +
const ERR_QUORUM_RATE_INVALID: u64 = 402;
+
+ + + + + +## Function `plugin` + +Plugin method of the module. +Should be called by token issuer. + + +
public fun plugin<TokenT>(signer: &signer)
+
+ + + +
+Implementation + + +
public fun plugin<TokenT>(signer: &signer) {
+    let token_issuer = stc_util::token_issuer<TokenT>();
+    assert!(signer::address_of(signer) == token_issuer, error::unauthenticated(ERR_NOT_AUTHORIZED));
+    let dao_config_modify_cap = on_chain_config::extract_modify_config_capability<
+        dao::DaoConfig<TokenT>,
+    >(signer);
+    assert!(
+        on_chain_config::account_address(&dao_config_modify_cap) == token_issuer,
+        error::unauthenticated(ERR_NOT_AUTHORIZED)
+    );
+    let cap = DaoConfigModifyCapability { cap: dao_config_modify_cap };
+    move_to(signer, cap);
+}
+
+ + + +
+ + + +## Function `propose` + +Entrypoint for the proposal. + + +
public entry fun propose<TokenT>(signer: signer, voting_delay: u64, voting_period: u64, voting_quorum_rate: u8, min_action_delay: u64, exec_delay: u64)
+
+ + + +
+Implementation + + +
public entry fun propose<TokenT>(
+    signer: signer,
+    voting_delay: u64,
+    voting_period: u64,
+    voting_quorum_rate: u8,
+    min_action_delay: u64,
+    exec_delay: u64,
+) {
+    assert!(voting_quorum_rate <= 100, error::invalid_argument(ERR_QUORUM_RATE_INVALID));
+    let action = DaoConfigUpdate {
+        voting_delay,
+        voting_period,
+        voting_quorum_rate,
+        min_action_delay,
+    };
+    dao::propose<TokenT, DaoConfigUpdate>(&signer, action, exec_delay);
+}
+
+ + + +
+ + + +## Function `execute` + +Once the proposal is agreed, anyone can call the method to make the proposal happen. + + +
public entry fun execute<TokenT>(proposer_address: address, proposal_id: u64)
+
+ + + +
+Implementation + + +
public entry fun execute<TokenT>(proposer_address: address, proposal_id: u64) acquires DaoConfigModifyCapability {
+    let DaoConfigUpdate {
+        voting_delay,
+        voting_period,
+        voting_quorum_rate,
+        min_action_delay,
+    } = dao::extract_proposal_action<TokenT, DaoConfigUpdate>(proposer_address, proposal_id);
+
+    debug::print(&std::string::utf8(b"dao_modify_config_proposal::execute | entered"));
+
+    let cap = borrow_global_mut<DaoConfigModifyCapability<TokenT>>(
+        stc_util::token_issuer<TokenT>(),
+    );
+    debug::print(
+        &std::string::utf8(
+            b"dao_modify_config_proposal::execute | borrow_global_mut<DaoConfigModifyCapability<TokenT>>"
+        )
+    );
+
+    dao::modify_dao_config(
+        &mut cap.cap,
+        voting_delay,
+        voting_period,
+        voting_quorum_rate,
+        min_action_delay,
+    );
+
+    debug::print(&std::string::utf8(b"dao_modify_config_proposal::execute | exited"));
+}
+
+ + + +
+ + + +## Specification + + + +
pragma verify = false;
+pragma aborts_if_is_strict;
+pragma aborts_if_is_partial;
+
+ + + + + +### Function `plugin` + + +
public fun plugin<TokenT>(signer: &signer)
+
+ + + + +
pragma aborts_if_is_partial = false;
+let sender = signer::address_of(signer);
+aborts_if sender != @0x2;
+include on_chain_config::AbortsIfCapNotExist<dao::DaoConfig<TokenT>> { address: sender };
+let config_cap =
+    on_chain_config::spec_cap<dao::DaoConfig<TokenT>>(sender);
+aborts_if option::is_none(config_cap);
+aborts_if option::borrow(config_cap).account_address != sender;
+aborts_if exists<DaoConfigModifyCapability<TokenT>>(sender);
+ensures exists<DaoConfigModifyCapability<TokenT>>(sender);
+
+ + + + + +### Function `propose` + + +
public entry fun propose<TokenT>(signer: signer, voting_delay: u64, voting_period: u64, voting_quorum_rate: u8, min_action_delay: u64, exec_delay: u64)
+
+ + + + +
pragma aborts_if_is_partial = false;
+aborts_if voting_quorum_rate > 100;
+include dao::AbortIfDaoConfigNotExist<TokenT>;
+include dao::AbortIfDaoInfoNotExist<TokenT>;
+aborts_if !exists<timestamp::CurrentTimeMicroseconds>(system_addresses::get_starcoin_framework());
+aborts_if exec_delay > 0 && exec_delay < dao::spec_dao_config<TokenT>().min_action_delay;
+include dao::CheckQuorumVotes<TokenT>;
+let sender = signer::address_of(signer);
+aborts_if exists<dao::Proposal<TokenT, DaoConfigUpdate>>(sender);
+
+ + + + + +### Function `execute` + + +
public entry fun execute<TokenT>(proposer_address: address, proposal_id: u64)
+
+ + + + +
pragma aborts_if_is_partial = true;
+aborts_if !exists<DaoConfigModifyCapability<TokenT>>(@0x2);
+
+ + +[move-book]: https://starcoin.dev/move/book/SUMMARY diff --git a/vm/framework/starcoin-framework/doc/dao_treasury_withdraw_proposal.md b/vm/framework/starcoin-framework/doc/dao_treasury_withdraw_proposal.md new file mode 100644 index 0000000000..c93ddd55e3 --- /dev/null +++ b/vm/framework/starcoin-framework/doc/dao_treasury_withdraw_proposal.md @@ -0,0 +1,350 @@ + + + +# Module `0x1::dao_treasury_withdraw_proposal` + +dao_treasury_withdraw_proposal is a dao proposal for withdraw Token from Treasury. + + +- [Resource `WrappedWithdrawCapability`](#0x1_dao_treasury_withdraw_proposal_WrappedWithdrawCapability) +- [Struct `WithdrawToken`](#0x1_dao_treasury_withdraw_proposal_WithdrawToken) +- [Constants](#@Constants_0) +- [Function `plugin`](#0x1_dao_treasury_withdraw_proposal_plugin) +- [Function `propose_withdraw`](#0x1_dao_treasury_withdraw_proposal_propose_withdraw) +- [Function `execute_withdraw_proposal`](#0x1_dao_treasury_withdraw_proposal_execute_withdraw_proposal) +- [Function `withdraw_for_block_reward`](#0x1_dao_treasury_withdraw_proposal_withdraw_for_block_reward) +- [Specification](#@Specification_1) + - [Function `plugin`](#@Specification_1_plugin) + - [Function `propose_withdraw`](#@Specification_1_propose_withdraw) + - [Function `execute_withdraw_proposal`](#@Specification_1_execute_withdraw_proposal) + + +
use 0x1::coin;
+use 0x1::dao;
+use 0x1::error;
+use 0x1::signer;
+use 0x1::stc_util;
+use 0x1::system_addresses;
+use 0x1::treasury;
+
+ + + + + +## Resource `WrappedWithdrawCapability` + +A wrapper of Token MintCapability. + + +
struct WrappedWithdrawCapability<TokenT> has key
+
+ + + +
+Fields + + +
+
+cap: treasury::WithdrawCapability<TokenT> +
+
+ +
+
+ + +
+ + + +## Struct `WithdrawToken` + +WithdrawToken request. + + +
struct WithdrawToken has copy, drop, store
+
+ + + +
+Fields + + +
+
+receiver: address +
+
+ the receiver of withdraw tokens. +
+
+amount: u128 +
+
+ how many tokens to mint. +
+
+period: u64 +
+
+ How long in milliseconds does it take for the token to be released +
+
+ + +
+ + + +## Constants + + + + + + +
const ERR_NOT_AUTHORIZED: u64 = 101;
+
+ + + + + +Only receiver can execute TreasuryWithdrawDaoProposal + + +
const ERR_NEED_RECEIVER_TO_EXECUTE: u64 = 102;
+
+ + + + + +The withdraw amount of propose is too many. + + +
const ERR_TOO_MANY_WITHDRAW_AMOUNT: u64 = 103;
+
+ + + + + +## Function `plugin` + +Plugin method of the module. +Should be called by token issuer. + + +
public fun plugin<TokenT>(signer: &signer, cap: treasury::WithdrawCapability<TokenT>)
+
+ + + +
+Implementation + + +
public fun plugin<TokenT>(signer: &signer, cap: treasury::WithdrawCapability<TokenT>) {
+    let token_issuer = stc_util::token_issuer<TokenT>();
+    assert!(signer::address_of(signer) == token_issuer, error::not_found(ERR_NOT_AUTHORIZED));
+    move_to(signer, WrappedWithdrawCapability<TokenT> { cap });
+}
+
+ + + +
+ + + +## Function `propose_withdraw` + +Entrypoint for the proposal. + + +
public fun propose_withdraw<TokenT>(signer: &signer, receiver: address, amount: u128, period: u64, exec_delay: u64)
+
+ + + +
+Implementation + + +
public fun propose_withdraw<TokenT>(
+    signer: &signer,
+    receiver: address,
+    amount: u128,
+    period: u64,
+    exec_delay: u64
+) {
+    let quorum_votes = dao::quorum_votes<TokenT>();
+    assert!(amount <= quorum_votes, error::invalid_argument(ERR_TOO_MANY_WITHDRAW_AMOUNT));
+    dao::propose<TokenT, WithdrawToken>(
+        signer,
+        WithdrawToken { receiver, amount, period },
+        exec_delay,
+    );
+}
+
+ + + +
+ + + +## Function `execute_withdraw_proposal` + +Once the proposal is agreed, anyone can call the method to make the proposal happen. + + +
public fun execute_withdraw_proposal<TokenT>(signer: &signer, proposer_address: address, proposal_id: u64)
+
+ + + +
+Implementation + + +
public fun execute_withdraw_proposal<TokenT>(
+    signer: &signer,
+    proposer_address: address,
+    proposal_id: u64,
+) acquires WrappedWithdrawCapability {
+    let WithdrawToken { receiver, amount, period } = dao::extract_proposal_action<TokenT, WithdrawToken>(
+        proposer_address,
+        proposal_id,
+    );
+    assert!(receiver == signer::address_of(signer), error::not_found(ERR_NEED_RECEIVER_TO_EXECUTE));
+    let cap =
+        borrow_global_mut<WrappedWithdrawCapability<TokenT>>(stc_util::token_issuer<TokenT>());
+    let linear_cap =
+        treasury::issue_linear_withdraw_capability<TokenT>(&mut cap.cap, amount, period);
+    treasury::add_linear_withdraw_capability(signer, linear_cap);
+}
+
+ + + +
+ + + +## Function `withdraw_for_block_reward` + +Provider a port for get block reward STC from Treasury, only genesis account can invoke this function. +The TreasuryWithdrawCapability is locked in TreasuryWithdrawDaoProposal, and only can withdraw by DAO proposal. +This approach is not graceful, but restricts the operation to genesis accounts only, so there are no security issues either. + + +
public fun withdraw_for_block_reward<TokenT>(signer: &signer, reward: u128): coin::Coin<TokenT>
+
+ + + +
+Implementation + + +
public fun withdraw_for_block_reward<TokenT>(
+    signer: &signer,
+    reward: u128
+): coin::Coin<TokenT> acquires WrappedWithdrawCapability {
+    system_addresses::assert_starcoin_framework(signer);
+    let cap = borrow_global_mut<WrappedWithdrawCapability<TokenT>>(signer::address_of(signer));
+    treasury::withdraw_with_capability(&mut cap.cap, reward)
+}
+
+ + + +
+ + + +## Specification + + + +
pragma verify = false;
+pragma aborts_if_is_strict;
+pragma aborts_if_is_partial;
+
+ + + + + +### Function `plugin` + + +
public fun plugin<TokenT>(signer: &signer, cap: treasury::WithdrawCapability<TokenT>)
+
+ + + + +
pragma aborts_if_is_partial = false;
+let sender = signer::address_of(signer);
+aborts_if sender != @0x2;
+aborts_if !exists<treasury::WithdrawCapability<TokenT>>(sender);
+aborts_if exists<WrappedWithdrawCapability<TokenT>>(sender);
+ensures !exists<treasury::WithdrawCapability<TokenT>>(sender);
+ensures exists<WrappedWithdrawCapability<TokenT>>(sender);
+
+ + + + + +### Function `propose_withdraw` + + +
public fun propose_withdraw<TokenT>(signer: &signer, receiver: address, amount: u128, period: u64, exec_delay: u64)
+
+ + + + +
pragma aborts_if_is_partial = false;
+let quorum_votes = dao::spec_quorum_votes<TokenT>();
+aborts_if amount > quorum_votes;
+include dao::AbortIfDaoConfigNotExist<TokenT>;
+include dao::AbortIfDaoInfoNotExist<TokenT>;
+aborts_if !exists<timestamp::CurrentTimeMicroseconds>(system_addresses::get_starcoin_framework());
+aborts_if exec_delay > 0 && exec_delay < dao::spec_dao_config<TokenT>().min_action_delay;
+include dao::CheckQuorumVotes<TokenT>;
+let sender = signer::address_of(signer);
+aborts_if exists<dao::Proposal<TokenT, WithdrawToken>>(sender);
+
+ + + + + +### Function `execute_withdraw_proposal` + + +
public fun execute_withdraw_proposal<TokenT>(signer: &signer, proposer_address: address, proposal_id: u64)
+
+ + + + +
pragma aborts_if_is_partial = true;
+let expected_states = vec<u8>(6);
+include dao::CheckProposalStates<TokenT, WithdrawToken> { expected_states };
+let proposal = global<dao::Proposal<TokenT, WithdrawToken>>(proposer_address);
+aborts_if option::is_none(proposal.action);
+aborts_if !exists<WrappedWithdrawCapability<TokenT>>(@0x2);
+
+ + +[move-book]: https://starcoin.dev/move/book/SUMMARY diff --git a/vm/framework/starcoin-framework/doc/dao_vote_scripts.md b/vm/framework/starcoin-framework/doc/dao_vote_scripts.md new file mode 100644 index 0000000000..4d1cf6a848 --- /dev/null +++ b/vm/framework/starcoin-framework/doc/dao_vote_scripts.md @@ -0,0 +1,199 @@ + + + +# Module `0x1::dao_vote_scripts` + + + +- [Function `cast_vote`](#0x1_dao_vote_scripts_cast_vote) +- [Function `revoke_vote`](#0x1_dao_vote_scripts_revoke_vote) +- [Function `flip_vote`](#0x1_dao_vote_scripts_flip_vote) +- [Function `revoke_vote_of_power`](#0x1_dao_vote_scripts_revoke_vote_of_power) +- [Function `unstake_vote`](#0x1_dao_vote_scripts_unstake_vote) +- [Specification](#@Specification_0) + + +
use 0x1::coin;
+use 0x1::dao;
+use 0x1::signer;
+
+ + + + + +## Function `cast_vote` + + + +
public entry fun cast_vote<Token, ActionT: copy, drop, store>(signer: signer, proposer_address: address, proposal_id: u64, agree: bool, votes: u128)
+
+ + + +
+Implementation + + +
public entry fun cast_vote<Token, ActionT: copy + drop + store>(
+    signer: signer,
+    proposer_address: address,
+    proposal_id: u64,
+    agree: bool,
+    votes: u128,
+) {
+    let sender = signer::address_of(&signer);
+    if (dao::has_vote<Token>(sender, proposer_address, proposal_id)) {
+        // if already voted, and vote is not same as the current cast, change the existing vote.
+        // resolve https://github.com/starcoinorg/starcoin/issues/2925.
+        let (agree_voted, _) = dao::vote_of<Token>(sender, proposer_address, proposal_id);
+        if (agree_voted != agree) {
+            dao::change_vote<Token, ActionT>(&signer, proposer_address, proposal_id, agree);
+        }
+    };
+
+    let votes = coin::withdraw<Token>(&signer, (votes as u64));
+    dao::cast_vote<Token, ActionT>(&signer, proposer_address, proposal_id, votes, agree);
+}
+
+ + + +
+ + + +## Function `revoke_vote` + +revoke all votes on a proposal + + +
public entry fun revoke_vote<Token, Action: copy, drop, store>(signer: signer, proposer_address: address, proposal_id: u64)
+
+ + + +
+Implementation + + +
public entry fun revoke_vote<Token, Action: copy + drop + store>(
+    signer: signer,
+    proposer_address: address,
+    proposal_id: u64,
+) {
+    let sender = signer::address_of(&signer);
+    let (_, power) = dao::vote_of<Token>(sender, proposer_address, proposal_id);
+    let my_token = dao::revoke_vote<Token, Action>(&signer, proposer_address, proposal_id, power);
+    coin::deposit(sender, my_token);
+}
+
+ + + +
+ + + +## Function `flip_vote` + +Let user change their vote during the voting time. + + +
public entry fun flip_vote<TokenT, ActionT: copy, drop, store>(signer: signer, proposer_address: address, proposal_id: u64)
+
+ + + +
+Implementation + + +
public entry fun flip_vote<TokenT, ActionT: copy + drop + store>(
+    signer: signer,
+    proposer_address: address,
+    proposal_id: u64,
+) {
+    let (agree, _) = dao::vote_of<TokenT>(signer::address_of(&signer), proposer_address, proposal_id);
+    dao::change_vote<TokenT, ActionT>(&signer, proposer_address, proposal_id, !agree);
+}
+
+ + + +
+ + + +## Function `revoke_vote_of_power` + +revoke some votes on a proposal + + +
public entry fun revoke_vote_of_power<Token, Action: copy, drop, store>(signer: signer, proposer_address: address, proposal_id: u64, power: u128)
+
+ + + +
+Implementation + + +
public entry fun revoke_vote_of_power<Token, Action: copy + drop + store>(
+    signer: signer,
+    proposer_address: address,
+    proposal_id: u64,
+    power: u128,
+) {
+    let sender = signer::address_of(&signer);
+    let my_token = dao::revoke_vote<Token, Action>(&signer, proposer_address, proposal_id, power);
+    coin::deposit(sender, my_token);
+}
+
+ + + +
+ + + +## Function `unstake_vote` + + + +
public entry fun unstake_vote<Token, Action: copy, drop, store>(signer: signer, proposer_address: address, proposal_id: u64)
+
+ + + +
+Implementation + + +
public entry fun unstake_vote<Token, Action: copy + drop + store>(
+    signer: signer,
+    proposer_address: address,
+    proposal_id: u64,
+) {
+    let my_token = dao::unstake_votes<Token, Action>(&signer, proposer_address, proposal_id);
+    coin::deposit(signer::address_of(&signer), my_token);
+}
+
+ + + +
+ + + +## Specification + + + +
pragma verify = false;
+pragma aborts_if_is_partial = false;
+pragma aborts_if_is_strict = true;
+
+ + +[move-book]: https://starcoin.dev/move/book/SUMMARY diff --git a/vm/framework/starcoin-framework/doc/epoch.md b/vm/framework/starcoin-framework/doc/epoch.md index be2660862a..f640ec587d 100644 --- a/vm/framework/starcoin-framework/doc/epoch.md +++ b/vm/framework/starcoin-framework/doc/epoch.md @@ -322,9 +322,7 @@ Initialization of the module. Implementation -
public fun initialize(
-    account: &signer,
-) {
+
public fun initialize(account: &signer) {
     // Timestamp::assert_genesis();
     system_addresses::assert_starcoin_framework(account);
 
diff --git a/vm/framework/starcoin-framework/doc/oracle.md b/vm/framework/starcoin-framework/doc/oracle.md
index f1f6bec8a8..65792cbcd6 100644
--- a/vm/framework/starcoin-framework/doc/oracle.md
+++ b/vm/framework/starcoin-framework/doc/oracle.md
@@ -33,7 +33,6 @@
 
use 0x1::account;
 use 0x1::error;
 use 0x1::event;
-use 0x1::reserved_accounts_signer;
 use 0x1::signer;
 use 0x1::system_addresses;
 use 0x1::timestamp;
@@ -378,9 +377,9 @@ Register OracleT as an oracle type.
 
 
 
public fun register_oracle<OracleT: copy+store+drop, Info: copy+store+drop>(sender: &signer, info: Info) {
-    let genesis_account =
-        reserved_accounts_signer::get_stored_signer(signer::address_of(sender));
-    move_to(&genesis_account, OracleInfo<OracleT, Info> {
+    // let genesis_account =
+    //     reserved_accounts_signer::get_stored_signer(signer::address_of(sender));
+    move_to(sender, OracleInfo<OracleT, Info> {
         counter: 0,
         info,
     });
diff --git a/vm/framework/starcoin-framework/doc/overview.md b/vm/framework/starcoin-framework/doc/overview.md
index 37dcda748b..405c4921ea 100644
--- a/vm/framework/starcoin-framework/doc/overview.md
+++ b/vm/framework/starcoin-framework/doc/overview.md
@@ -29,6 +29,9 @@ This is the reference documentation of the Starcoin framework.
 -  [`0x1::consensus_strategy`](consensus_strategy.md#0x1_consensus_strategy)
 -  [`0x1::create_signer`](create_signer.md#0x1_create_signer)
 -  [`0x1::dao`](dao.md#0x1_dao)
+-  [`0x1::dao_modify_config_proposal`](dao_modify_config_proposal.md#0x1_dao_modify_config_proposal)
+-  [`0x1::dao_treasury_withdraw_proposal`](dao_treasury_withdraw_proposal.md#0x1_dao_treasury_withdraw_proposal)
+-  [`0x1::dao_vote_scripts`](dao_vote_scripts.md#0x1_dao_vote_scripts)
 -  [`0x1::delegation_pool`](delegation_pool.md#0x1_delegation_pool)
 -  [`0x1::dispatchable_fungible_asset`](dispatchable_fungible_asset.md#0x1_dispatchable_fungible_asset)
 -  [`0x1::dkg`](dkg.md#0x1_dkg)
@@ -56,7 +59,6 @@ This is the reference documentation of the Starcoin framework.
 -  [`0x1::oracle`](oracle.md#0x1_oracle)
 -  [`0x1::oracle_aggregator`](oracle_aggregator.md#0x1_oracle_aggregator)
 -  [`0x1::oracle_price`](oracle_price.md#0x1_oracle_price)
--  [`0x1::oracle_price_script`](oracle_price_script.md#0x1_oracle_price_script)
 -  [`0x1::oracle_stc_usd`](oracle_stc_usd.md#0x1_oracle_stc_usd)
 -  [`0x1::primary_fungible_store`](primary_fungible_store.md#0x1_primary_fungible_store)
 -  [`0x1::randomness`](randomness.md#0x1_randomness)
@@ -80,6 +82,7 @@ This is the reference documentation of the Starcoin framework.
 -  [`0x1::stc_block`](stc_block.md#0x1_stc_block)
 -  [`0x1::stc_genesis`](stc_genesis.md#0x1_stc_genesis)
 -  [`0x1::stc_language_version`](stc_language_version.md#0x1_stc_language_version)
+-  [`0x1::stc_offer`](stc_offer.md#0x1_stc_offer)
 -  [`0x1::stc_transaction_fee`](stc_transaction_fee.md#0x1_stc_transaction_fee)
 -  [`0x1::stc_transaction_package_validation`](stc_transaction_package_validation.md#0x1_stc_transaction_package_validation)
 -  [`0x1::stc_transaction_timeout`](stc_transaction_timeout.md#0x1_stc_transaction_timeout)
@@ -96,7 +99,6 @@ This is the reference documentation of the Starcoin framework.
 -  [`0x1::transaction_validation`](transaction_validation.md#0x1_transaction_validation)
 -  [`0x1::transfer_scripts`](transfer_scripts.md#0x1_transfer_scripts)
 -  [`0x1::treasury`](treasury.md#0x1_treasury)
--  [`0x1::treasury_withdraw_dao_proposal`](treasury_withdraw_dao_proposal.md#0x1_treasury_withdraw_dao_proposal)
 -  [`0x1::util`](util.md#0x1_util)
 -  [`0x1::validator_consensus_info`](validator_consensus_info.md#0x1_validator_consensus_info)
 -  [`0x1::version`](version.md#0x1_version)
diff --git a/vm/framework/starcoin-framework/doc/stc_block.md b/vm/framework/starcoin-framework/doc/stc_block.md
index c25378df14..81648ac825 100644
--- a/vm/framework/starcoin-framework/doc/stc_block.md
+++ b/vm/framework/starcoin-framework/doc/stc_block.md
@@ -12,6 +12,7 @@ Block module provide metadata for generated blocks.
 -  [Function `initialize`](#0x1_stc_block_initialize)
 -  [Function `get_current_block_number`](#0x1_stc_block_get_current_block_number)
 -  [Function `get_parent_hash`](#0x1_stc_block_get_parent_hash)
+-  [Function `get_parents_hash`](#0x1_stc_block_get_parents_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)
@@ -83,16 +84,16 @@ Block metadata struct.
  number of uncles.
 
 
-new_block_events: event::EventHandle<stc_block::NewBlockEvent> +parents_hash: vector<u8>
- Handle of events when new blocks are emitted + An Array of the parents hash for a Dag block.
-parents_hash: vector<u8> +new_block_events: event::EventHandle<stc_block::NewBlockEvent>
- An Array of the parents hash for a Dag block. +
@@ -245,19 +246,18 @@ This can only be invoked by the GENESIS_ACCOUNT at genesis
public fun initialize(account: &signer, parent_hash: vector<u8>) {
-    // Timestamp::assert_genesis();
     system_addresses::assert_starcoin_framework(account);
 
-    move_to<BlockMetadata>(
-        account,
-        BlockMetadata {
-            number: 0,
-            parent_hash,
-            author: system_addresses::get_starcoin_framework(),
-            uncles: 0,
-            parents_hash: vector::empty<u8>(),
-            new_block_events: account::new_event_handle<Self::NewBlockEvent>(account),
-        });
+    let block_metadata = BlockMetadata {
+        number: 0,
+        parent_hash,
+        author: system_addresses::get_starcoin_framework(),
+        uncles: 0,
+        parents_hash: vector::empty<u8>(),
+        new_block_events: account::new_event_handle<NewBlockEvent>(account),
+    };
+
+    move_to<BlockMetadata>(account, block_metadata);
 }
 
@@ -313,6 +313,32 @@ Get the hash of the parent block. + + + + +## Function `get_parents_hash` + +Get the hash of the parents block, used for DAG + + +
public fun get_parents_hash(): vector<u8>
+
+ + + +
+Implementation + + +
public fun get_parents_hash(): vector<u8> {
+    // *&borrow_global<BlockMetadata>(system_addresses::get_starcoin_framework()).parent_hash
+    vector::empty()
+}
+
+ + +
@@ -331,8 +357,9 @@ Gets the address of the author of the current block Implementation -
public fun get_current_author(): address acquires BlockMetadata {
-    borrow_global<BlockMetadata>(system_addresses::get_starcoin_framework()).author
+
public fun get_current_author(): address {
+    // borrow_global<BlockMetadata>(system_addresses::get_starcoin_framework()).author
+    @0x1
 }
 
@@ -385,6 +412,8 @@ The runtime always runs this before executing the transactions in a block. debug::print(&coin::value(&txn_fee)); // then deal with current block. + debug::print(&std::string::utf8(b"stc_block::block_prologue | timestamp::update_global_time")); + debug::print(&timestamp); timestamp::update_global_time(&account, signer::address_of(&account), timestamp * 1000); process_block_metadata( @@ -399,9 +428,6 @@ The runtime always runs this before executing the transactions in a block. let reward = epoch::adjust_epoch(&account, number, timestamp, uncles, parent_gas_used); - debug::print(&std::string::utf8(b"stc_block::block_prologue | timestamp::update_global_time")); - debug::print(&timestamp); - // pass in previous block gas fees. block_reward::process_block_reward(&account, number, reward, author, auth_key_vec, txn_fee); diff --git a/vm/framework/starcoin-framework/doc/stc_genesis.md b/vm/framework/starcoin-framework/doc/stc_genesis.md index 8b0fea215a..61edb62f23 100644 --- a/vm/framework/starcoin-framework/doc/stc_genesis.md +++ b/vm/framework/starcoin-framework/doc/stc_genesis.md @@ -23,11 +23,14 @@ The module for init Genesis use 0x1::consensus_config; use 0x1::consensus_strategy; use 0x1::dao; +use 0x1::dao_modify_config_proposal; +use 0x1::dao_treasury_withdraw_proposal; use 0x1::debug; use 0x1::epoch; use 0x1::on_chain_config; use 0x1::on_chain_config_dao; use 0x1::option; +use 0x1::oracle_stc_usd; use 0x1::starcoin_coin; use 0x1::stc_block; use 0x1::stc_language_version; @@ -41,7 +44,6 @@ The module for init Genesis use 0x1::timestamp; use 0x1::transaction_publish_option; use 0x1::treasury; -use 0x1::treasury_withdraw_dao_proposal; use 0x1::vector; use 0x1::vm_config;
@@ -160,9 +162,9 @@ The module for init Genesis block_reward::initialize(&starcoin_framework_account, reward_delay); - // TODO(BobOng): [framework compatible] treasury_withdraw_dao_proposal not implemented. + // TODO(BobOng): [framework compatible] treasury_withdraw_dao_proposal not implemented. // Lock the TreasuryWithdrawCapability to Dao - // treasury_withdraw_dao_proposal::plugin(&genesis_account, withdraw_cap); + // treasury_withdraw_dao_proposal::plugin(&genesis_account, withdraw_cap); // Initliaze STC let total_supply_coin = Self::initialize_stc( @@ -216,6 +218,9 @@ The module for init Genesis // account::release_genesis_signer(genesis_account); // account::release_genesis_signer(association); + // Register oracle + oracle_stc_usd::register(&starcoin_framework_account); + debug::print(&std::string::utf8(b"stc_genesis::initialize | Exited")); }
@@ -375,7 +380,8 @@ Overall governance allocation strategy: ); treasury::add_linear_withdraw_capability(core_resource_account, liner_withdraw_cap); }; - treasury_withdraw_dao_proposal::plugin<STC>(starcoin_framework, treasury_withdraw_cap); + dao_treasury_withdraw_proposal::plugin<STC>(starcoin_framework, treasury_withdraw_cap); + dao_modify_config_proposal::plugin<STC>(starcoin_framework); }
diff --git a/vm/framework/starcoin-framework/doc/stc_offer.md b/vm/framework/starcoin-framework/doc/stc_offer.md new file mode 100644 index 0000000000..f963daa353 --- /dev/null +++ b/vm/framework/starcoin-framework/doc/stc_offer.md @@ -0,0 +1,289 @@ + + + +# Module `0x1::stc_offer` + + + +- [Resource `Offer`](#0x1_stc_offer_Offer) +- [Constants](#@Constants_0) +- [Function `create`](#0x1_stc_offer_create) +- [Function `redeem`](#0x1_stc_offer_redeem) +- [Function `exists_at`](#0x1_stc_offer_exists_at) +- [Function `address_of`](#0x1_stc_offer_address_of) +- [Specification](#@Specification_1) + - [Function `create`](#@Specification_1_create) + - [Function `redeem`](#@Specification_1_redeem) + - [Function `exists_at`](#@Specification_1_exists_at) + - [Function `address_of`](#@Specification_1_address_of) + + +
use 0x1::error;
+use 0x1::signer;
+use 0x1::timestamp;
+
+ + + + + +## Resource `Offer` + +A wrapper around value offered that can be claimed by the address stored in for when after lock time. + + +
struct Offer<Offered> has key
+
+ + + +
+Fields + + +
+
+offered: Offered +
+
+ +
+
+for_address: address +
+
+ +
+
+time_lock: u64 +
+
+ +
+
+ + +
+ + + +## Constants + + + + +An offer of the specified type for the account does not match + + +
const EOFFER_DNE_FOR_ACCOUNT: u64 = 101;
+
+ + + + + +Offer is not unlocked yet. + + +
const EOFFER_NOT_UNLOCKED: u64 = 102;
+
+ + + + + +## Function `create` + +Publish a value of type Offered under the sender's account. The value can be claimed by +either the for address or the transaction sender. + + +
public fun create<Offered: store>(account: &signer, offered: Offered, for_address: address, lock_period: u64)
+
+ + + +
+Implementation + + +
public fun create<Offered: store>(account: &signer, offered: Offered, for_address: address, lock_period: u64) {
+    let time_lock = timestamp::now_seconds() + lock_period;
+    //TODO should support multi Offer?
+    move_to(account, Offer<Offered> {
+        offered,
+        for_address,
+        time_lock
+    });
+}
+
+ + + +
+ + + +## Function `redeem` + +Claim the value of type Offered published at offer_address. +Only succeeds if the sender is the intended recipient stored in for or the original +publisher offer_address, and now >= time_lock +Also fails if no such value exists. + + +
public fun redeem<Offered: store>(account: &signer, offer_address: address): Offered
+
+ + + +
+Implementation + + +
public fun redeem<Offered: store>(account: &signer, offer_address: address): Offered acquires Offer {
+    let Offer<Offered> { offered, for_address, time_lock } = move_from<Offer<Offered>>(offer_address);
+    let sender = signer::address_of(account);
+    let now = timestamp::now_seconds();
+    assert!(sender == for_address || sender == offer_address, error::invalid_argument(EOFFER_DNE_FOR_ACCOUNT));
+    assert!(now >= time_lock, error::invalid_state(EOFFER_NOT_UNLOCKED));
+    offered
+}
+
+ + + +
+ + + +## Function `exists_at` + +Returns true if an offer of type Offered exists at offer_address. + + +
public fun exists_at<Offered: store>(offer_address: address): bool
+
+ + + +
+Implementation + + +
public fun exists_at<Offered: store>(offer_address: address): bool {
+    exists<Offer<Offered>>(offer_address)
+}
+
+ + + +
+ + + +## Function `address_of` + +Returns the address of the Offered type stored at offer_address. +Fails if no such Offer exists. + + +
public fun address_of<Offered: store>(offer_address: address): address
+
+ + + +
+Implementation + + +
public fun address_of<Offered: store>(offer_address: address): address acquires Offer {
+    borrow_global<Offer<Offered>>(offer_address).for_address
+}
+
+ + + +
+ + + +## Specification + + + +
pragma verify = true;
+pragma aborts_if_is_strict = true;
+
+ + + + + +### Function `create` + + +
public fun create<Offered: store>(account: &signer, offered: Offered, for_address: address, lock_period: u64)
+
+ + + + +
aborts_if timestamp::now_seconds() + lock_period > max_u64();
+aborts_if exists<Offer<Offered>>(signer::address_of(account));
+
+ + + + + +### Function `redeem` + + +
public fun redeem<Offered: store>(account: &signer, offer_address: address): Offered
+
+ + + + +
aborts_if !exists<Offer<Offered>>(offer_address);
+aborts_if
+    signer::address_of(account) != global<Offer<Offered>>(offer_address).for_address
+        && signer::address_of(account) != offer_address;
+aborts_if timestamp::now_seconds() < global<Offer<Offered>>(offer_address).time_lock;
+
+ + + + + +### Function `exists_at` + + +
public fun exists_at<Offered: store>(offer_address: address): bool
+
+ + + + +
aborts_if false;
+
+ + + + + +### Function `address_of` + + +
public fun address_of<Offered: store>(offer_address: address): address
+
+ + + + +
aborts_if !exists<Offer<Offered>>(offer_address);
+
+ + +[move-book]: https://starcoin.dev/move/book/SUMMARY diff --git a/vm/framework/starcoin-framework/doc/timestamp.md b/vm/framework/starcoin-framework/doc/timestamp.md index a8980cf8ad..75451986f0 100644 --- a/vm/framework/starcoin-framework/doc/timestamp.md +++ b/vm/framework/starcoin-framework/doc/timestamp.md @@ -137,7 +137,7 @@ Marks that time has started. This can only be called from genesis and with the s Updates the wall clock time by consensus. Requires VM privilege and will be invoked during block prologue. -
public fun update_global_time(account: &signer, proposer: address, timestamp: u64)
+
public fun update_global_time(account: &signer, _proposer: address, timestamp: u64)
 
@@ -148,10 +148,11 @@ Updates the wall clock time by consensus. Requires VM privilege and will be invo
public fun update_global_time(
     account: &signer,
-    proposer: address,
+    _proposer: address,
     timestamp: u64
 ) acquires CurrentTimeMicroseconds {
     debug::print(&std::string::utf8(b"timestamp::update_global_time | Entered"));
+    debug::print(&timestamp);
 
     // Can only be invoked by StarcoinVM signer.
     // system_addresses::assert_vm(account);
@@ -323,7 +324,7 @@ Gets the current time in seconds.
 ### Function `update_global_time`
 
 
-
public fun update_global_time(account: &signer, proposer: address, timestamp: u64)
+
public fun update_global_time(account: &signer, _proposer: address, timestamp: u64)
 
@@ -331,7 +332,7 @@ Gets the current time in seconds.
requires chain_status::is_operating();
 include UpdateGlobalTimeAbortsIf;
-ensures (proposer != @vm_reserved) ==> (spec_now_microseconds() == timestamp);
+ensures (_proposer != @vm_reserved) ==> (spec_now_microseconds() == timestamp);
 
@@ -342,13 +343,13 @@ Gets the current time in seconds.
schema UpdateGlobalTimeAbortsIf {
     account: signer;
-    proposer: address;
+    _proposer: address;
     timestamp: u64;
     // This enforces high-level requirement 3:
     aborts_if !system_addresses::is_vm(account);
     // This enforces high-level requirement 4:
-    aborts_if (proposer == @vm_reserved) && (spec_now_microseconds() != timestamp);
-    aborts_if (proposer != @vm_reserved) && (spec_now_microseconds() >= timestamp);
+    aborts_if (_proposer == @vm_reserved) && (spec_now_microseconds() != timestamp);
+    aborts_if (_proposer != @vm_reserved) && (spec_now_microseconds() >= timestamp);
 }
 
diff --git a/vm/framework/starcoin-framework/integration-tests/account/basic.exp b/vm/framework/starcoin-framework/integration-tests/account/basic.exp new file mode 100644 index 0000000000..a8b0987ed5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/basic.exp @@ -0,0 +1,14 @@ +processed 5 tasks + +task 4 'publish'. lines 9-167: +Error: error[E05001]: ability constraint not satisfied + ┌─ /var/folders/mc/m_3x4kk518jd_5x6plf003100000gn/T/.tmpU9Ns6F:16:9 + │ +16 │ move_to(account, Hold { x }) + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + │ │ │ │ + │ │ │ The type '(default=0x2747CABBB481A433679F6DC8AAE833DD)::Holder::Hold' can have the ability 'key' but the type argument 'T' does not have the required ability 'store' + │ │ The type '(default=0x2747CABBB481A433679F6DC8AAE833DD)::Holder::Hold' does not have the ability 'key' + │ Invalid call of 'move_to' + + diff --git a/vm/framework/starcoin-framework/integration-tests/account/basic.move b/vm/framework/starcoin-framework/integration-tests/account/basic.move new file mode 100644 index 0000000000..aa65018b26 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/basic.move @@ -0,0 +1,166 @@ +//# init -n dev + +//# faucet --addr bob + +//# faucet --addr alice --amount 0 + +//# faucet --addr default + +//# publish +module default::holder { + use starcoin_framework::signer; + + struct Hold has key { + x: T + } + + public fun hold(account: &signer, x: T) { + move_to(account, Hold { x }) + } + + public fun get(account: &signer): T + acquires Hold { + let Hold { x } = move_from>(signer::address_of(account)); + x + } +} + + +//# run --signers bob +// +// script { +// use starcoin_framework::starcoin_coin::STC; +// use starcoin_framework::account; +// +// fun main(account: signer) { +// let with_cap = account::offer_rotation_capability(&account); +// account::pay_from_capability(&with_cap, @bob, 10, x""); +// account::restore_withdraw_capability(with_cap); +// } +// } +// check: EXECUTED +// +// +// //# run --signers bob +// script { +// use starcoin_framework::account; +// +// fun main(account: signer) { +// let rot_cap = account::extract_key_rotation_capability(&account); +// account::rotate_authentication_key_with_capability(&rot_cap, x"123abc"); +// account::restore_key_rotation_capability(rot_cap); +// } +// } +// +// // check: ABORTED +// // check: 26119 +// + +//# run --signers default +// script { +// use starcoin_framework::account; +// use default::holder; +// +// fun main(account: signer) { +// Holder::hold( +// &account, +// account::extract_key_rotation_capability(&account) +// ); +// Holder::hold( +// &account, +// account::extract_key_rotation_capability(&account) +// ); +// } +// } +// // check: ABORTED +// // check: 26369 +// +// //# run --signers default +// script { +// use starcoin_framework::account; +// use starcoin_framework::signer; +// +// fun main(sender: signer) { +// let cap = account::extract_key_rotation_capability(&sender); +// assert!( +// *account::key_rotation_capability_address(&cap) == signer::address_of(&sender), 0 +// ); +// account::restore_key_rotation_capability(cap); +// let with_cap = account::extract_withdraw_capability(&sender); +// +// assert!( +// *account::withdraw_capability_address(&with_cap) == signer::address_of(&sender), +// 0 +// ); +// account::restore_withdraw_capability(with_cap); +// } +// } +// // check: EXECUTED +// +// +// //# run --signers bob +// +// script { +// use starcoin_framework::account; +// use starcoin_framework::starcoin_coin::STC; +// +// fun main(account: signer) { +// let with_cap = account::extract_withdraw_capability(&account); +// account::pay_from_capability(&with_cap, @alice, 10000, x""); +// account::restore_withdraw_capability(with_cap); +// assert!(coin::balance(@alice) == 10000, 60) +// } +// } +// // check: EXECUTED +// +// //# run --signers default +// // test core address +// +// script { +// use starcoin_framework::CoreAddresses; +// +// fun main() { +// assert!(CoreAddresses::VM_RESERVED_ADDRESS() == @0x0, 100); +// } +// } +// // check: EXECUTED +// +// //# run --signers default +// script { +// use starcoin_framework::account; +// use starcoin_framework::starcoin_coin::STC; +// use starcoin_framework::Authenticator; +// +// fun main() { +// let dummy_auth_key = x"91e941f5bc09a285705c092dd654b94a7a8e385f898968d4ecfba49609a13461"; +// let expected_address = Authenticator::derived_address(dummy_auth_key); +// account::create_account_with_address(expected_address); +// assert!(account::exists_at(expected_address), 1000); +// } +// } +// // check: EXECUTED +// +// +// //# run --signers bob +// script { +// use starcoin_framework::account; +// use starcoin_framework::signer; +// +// fun main(account: signer) { +// let seq = account::sequence_number(signer::address_of(&account)); +// assert!(seq == 3, seq); +// } +// } +// // check: EXECUTE +// + +//# run --signers bob +script { + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer) { + coin::transfer(&account, @alice, 0); + } +} +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.exp b/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.exp new file mode 100644 index 0000000000..7b334f805e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.exp @@ -0,0 +1,13 @@ +processed 4 tasks + +task 2 'run'. lines 6-16: +{ + "gas_used": 42583, + "status": "Executed" +} + +task 3 'run'. lines 19-30: +{ + "gas_used": 27910, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.move b/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.move new file mode 100644 index 0000000000..aa3bfc5c30 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/deposit_zero_token.move @@ -0,0 +1,32 @@ +//# init -n dev + +//# faucet --addr alice + + +//# run --signers alice +script { + use std::signer; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::coin; + + fun main(account: signer) { + let coin = coin::zero(); + coin::deposit(signer::address_of(&account), coin); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::starcoin_coin::{STC}; + use starcoin_framework::Token; + use starcoin_framework::account; + use starcoin_framework::signer; + + fun main(account: signer) { + let coin = Token::zero(); + account::deposit_with_metadata(signer::address_of(&account), coin, x""); + } +} +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.exp b/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.exp new file mode 100644 index 0000000000..3ff434b998 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.exp @@ -0,0 +1,125 @@ +processed 12 tasks + +task 3 'run'. lines 8-23: +{ + "gas_used": 318252, + "status": "Executed" +} + +task 4 'run'. lines 27-59: +{ + "gas_used": 91881, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "CoreAddresses" + } + }, + "abort_code": "2818" + } + } +} + +task 5 'run'. lines 63-90: +{ + "gas_used": 168011, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "1031" + } + } +} + +task 6 'run'. lines 94-122: +{ + "gas_used": 140032, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "263" + } + } +} + +task 7 'run'. lines 126-156: +{ + "gas_used": 174473, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "775" + } + } +} + +task 8 'run'. lines 160-205: +{ + "gas_used": 239024, + "status": "Executed" +} + +task 9 'run'. lines 209-239: +{ + "gas_used": 174341, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "519" + } + } +} + +task 10 'run'. lines 243-274: +{ + "gas_used": 95560, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "2568" + } + } +} + +task 11 'run'. lines 278-309: +{ + "gas_used": 89451, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "CoreAddresses" + } + }, + "abort_code": "2818" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.move b/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.move new file mode 100644 index 0000000000..721f9fda2c --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/txn_prologue_and_epilogue.move @@ -0,0 +1,309 @@ +//# init -n dev + +//# faucet --addr alice --amount 10000000 + +//# faucet --addr Genesis + + +//# run --signers alice +// create txn sender account +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + use starcoin_framework::Authenticator; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let address = Authenticator::derived_address(auth_key_vec); + account::create_account_with_address(address); + coin::transfer(&account, address, 5000); + } +} +// check: EXECUTED + + +//# run --signers alice +// prologue sender is not genesis +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 0, 1001); + let balance = coin::balance(txn_sender); + assert!(balance == 5000, 1001); + + let txn_sequence_number = 0; + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + txn_public_key, + txn_gas_price, + txn_max_gas_units + ); + } +} +// check: "Keep(ABORTED { code: 2818" + + +//# run --signers Genesis +// gas is not enough +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let txn_sequence_number = 0; + let txn_gas_price = 1; + let txn_max_gas_units = 10000; //EPROLOGUE_CANT_PAY_GAS_DEPOSIT + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + txn_public_key, + txn_gas_price, + txn_max_gas_units + ); + } +} +// check: "Keep(ABORTED { code: 1031" + + +//# run --signers Genesis +// invalid pub key +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let wrong_txn_public_key = x"c48b687a"; + let txn_sequence_number = 0; + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + wrong_txn_public_key, //EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY + txn_gas_price, + txn_max_gas_units + ); + } +} +// check: "Keep(ABORTED { code: 263" + + +//# run --signers Genesis +// sequence number too new +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 0, 1005); + + let txn_sequence_number = 1; //EPROLOGUE_SEQUENCE_NUMBER_TOO_NEW + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + txn_public_key, + txn_gas_price, + txn_max_gas_units + ); + } +} +// check: "Keep(ABORTED { code: 775" + + +//# run --signers Genesis +// successfully executed +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 0, 1005); + + let txn_sequence_number = 0; + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + txn_public_key, + txn_gas_price, + txn_max_gas_units + ); + + // execute the txn... + + let gas_units_remaining = 10; + + account::txn_epilogue( + &account, + txn_sender, + txn_sequence_number, + txn_gas_price, + txn_max_gas_units, + gas_units_remaining + ); + let seq = account::sequence_number(txn_sender); + assert!(seq == 1, 1006); + } +} +// check: EXECUTED + + +//# run --signers Genesis +// sequence number too old +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 1, 1005); + + let txn_sequence_number = 0; //EPROLOGUE_SEQUENCE_NUMBER_TOO_OLD + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + + account::txn_prologue( + &account, + txn_sender, + txn_sequence_number, + txn_public_key, + txn_gas_price, + txn_max_gas_units + ); + } +} +// check: "Keep(ABORTED { code: 519" + + +//# run --signers Genesis +// epilouge insufficient balance +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 1, 1007); + + let txn_sequence_number = 1; + let txn_gas_price = 1; + let txn_max_gas_units = 6000; //EINSUFFICIENT_BALANCE + let gas_units_remaining = 10; + + account::txn_epilogue( + &account, + txn_sender, + txn_sequence_number, + txn_gas_price, + txn_max_gas_units, + gas_units_remaining + ); + } +} +// check: "Keep(ABORTED { code: 2568" + + +//# run --signers alice +// epilogue sender is not genesis +script { + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Authenticator; + use std::vector; + + fun main(account: signer) { + let txn_public_key = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d"; + let auth_key_vec = Authenticator::ed25519_authentication_key(copy txn_public_key); + let txn_sender = Authenticator::derived_address(copy auth_key_vec); + vector::push_back(&mut txn_public_key, 0u8); //create preimage + + let seq = account::sequence_number(txn_sender); + assert!(seq == 1, 1007); + + let txn_sequence_number = 1; + let txn_gas_price = 1; + let txn_max_gas_units = 1000; + let gas_units_remaining = 10; + + account::txn_epilogue( + &account, + txn_sender, + txn_sequence_number, + txn_gas_price, + txn_max_gas_units, + gas_units_remaining + ); + } +} +// check: "Keep(ABORTED { code: 2818" \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.exp b/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.exp new file mode 100644 index 0000000000..40b6ab33c1 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.exp @@ -0,0 +1,63 @@ +processed 12 tasks + +task 5 'run'. lines 25-38: +{ + "gas_used": 38911, + "status": "Executed" +} + +task 6 'run'. lines 41-55: +{ + "gas_used": 23683, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "25857" + } + } +} + +task 7 'run'. lines 58-73: +{ + "gas_used": 23683, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "Account" + } + }, + "abort_code": "25857" + } + } +} + +task 8 'run'. lines 76-88: +{ + "gas_used": 109724, + "status": "Executed" +} + +task 9 'run'. lines 91-103: +{ + "gas_used": 128738, + "status": "Executed" +} + +task 10 'run'. lines 106-117: +{ + "gas_used": 90513, + "status": "Executed" +} + +task 11 'run'. lines 120-132: +{ + "gas_used": 116924, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.move b/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.move new file mode 100644 index 0000000000..f11ed18e4b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/withdraw_capability.move @@ -0,0 +1,132 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# faucet --addr carol + +//# publish + +module alice::SillyColdWallet { + use starcoin_framework::account; + + struct T has key, store { + cap: account::WithdrawCapability, + owner: address, + } + + public fun publish(account: &signer, cap: account::WithdrawCapability, owner: address) { + move_to(account, T { cap, owner }); + } +} + + +//# run --signers alice + + +script { + use alice::SillyColdWallet; + use starcoin_framework::account; + + // create a cold wallet for Bob that withdraws from Alice's account + fun main(sender: signer) { + let cap = account::extract_withdraw_capability(&sender); + SillyColdWallet::publish(&sender, cap, @bob); + } +} +// check: "Keep(EXECUTED)" + + +//# run --signers alice + +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + + // check that Alice can no longer withdraw from her account + fun main(account: signer) { + let with_cap = account::extract_withdraw_capability(&account); + // should fail with withdrawal capability already extracted + account::pay_from_capability(&with_cap, @alice, 1000, x""); + account::restore_withdraw_capability(with_cap); + } +} +// check: "Keep(ABORTED { code: 25857," + + +//# run --signers alice +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + use starcoin_framework::signer; + + // check that Alice can no longer withdraw from her account + fun main(account: signer) { + let with_cap = account::extract_withdraw_capability(&account); + // should fail with withdrawal capability already extracted + let coin = account::withdraw_with_metadata(&account, 1000, x""); + account::deposit_with_metadata(signer::address_of(&account), coin, x""); + account::restore_withdraw_capability(with_cap); + } +} +// check: "Keep(ABORTED { code: 25857," + + +//# run --signers bob + +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + + // check that Bob can still pay from his normal account + fun main(account: signer) { + let with_cap = account::extract_withdraw_capability(&account); + account::pay_from_capability(&with_cap, @bob, 1000, x""); + account::restore_withdraw_capability(with_cap); + } +} + + +//# run --signers bob +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + + // check that Bob can still withdraw from his normal account + fun main(account: signer) { + let with_cap = account::extract_withdraw_capability(&account); + let coin = account::withdraw_with_capability(&with_cap, 1000); + coin::deposit(&account, coin); + account::restore_withdraw_capability(with_cap); + } +} + + +//# run --signers bob +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + use starcoin_framework::signer; + + // check that Bob can still withdraw from his normal account + fun main(account: signer) { + let coin = account::withdraw_with_metadata(&account, 1000, x""); + account::deposit_with_metadata(signer::address_of(&account), coin, x""); + } +} + + +//# run --signers carol + +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::account; + + // check that other users can still pay into Alice's account in the normal way + fun main(account: signer) { + let with_cap = account::extract_withdraw_capability(&account); + account::pay_from_capability(&with_cap, @alice, 1000, x""); + account::restore_withdraw_capability(with_cap); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.exp b/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.exp new file mode 100644 index 0000000000..cf58db6309 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 6-16: +{ + "gas_used": 34047, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.move b/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.move new file mode 100644 index 0000000000..d6b36bdfb3 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/account/withdraw_zero_token.move @@ -0,0 +1,17 @@ +//# init -n dev + +//# faucet --addr alice + + +//# run --signers alice +script { + use starcoin_framework::starcoin_coin::{STC}; + use starcoin_framework::Token; + use starcoin_framework::account; + + fun main(account: signer) { + let coin = coin::withdraw(&account, 0); + Token::destroy_zero(coin); + } +} +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/block/block_metadata.exp b/vm/framework/starcoin-framework/integration-tests/block/block_metadata.exp new file mode 100644 index 0000000000..934c7e2d9f --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/block/block_metadata.exp @@ -0,0 +1,13 @@ +processed 4 tasks + +task 2 'run'. lines 5-15: +{ + "gas_used": 23647, + "status": "Executed" +} + +task 3 'run'. lines 17-27: +{ + "gas_used": 21132, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/block/block_metadata.move b/vm/framework/starcoin-framework/integration-tests/block/block_metadata.move new file mode 100644 index 0000000000..45e3f6179b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/block/block_metadata.move @@ -0,0 +1,27 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script { + use starcoin_framework::stc_block; + use starcoin_framework::debug; + + fun get_parent_hash(_account: signer) { + let hash = stc_block::get_parent_hash(); + debug::print>(&hash); + } +} +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::stc_block; + use starcoin_framework::debug; + + fun get_parents_hash(_account: signer) { + let hash = stc_block::get_parents_hash(); + debug::print>(&hash); + } +} +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/block_reward/basic.exp b/vm/framework/starcoin-framework/integration-tests/block_reward/basic.exp new file mode 100644 index 0000000000..ae220e67c6 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/block_reward/basic.exp @@ -0,0 +1,75 @@ +processed 12 tasks + +task 3 'run'. lines 7-28: +{ + "gas_used": 664427, + "status": "Executed" +} + +task 4 'run'. lines 31-54: +{ + "gas_used": 42139, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "system_addresses" + } + }, + "abort_code": "327683" + } + } +} + +task 5 'run'. lines 57-77: +{ + "gas_used": 62204, + "status": "Executed" +} + +task 6 'run'. lines 80-100: +{ + "gas_used": 72518, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "block_reward" + } + }, + "abort_code": "65638" + } + } +} + +task 7 'run'. lines 103-129: +{ + "gas_used": 671623, + "status": "Executed" +} + +task 8 'run'. lines 132-155: +{ + "gas_used": 2298030, + "status": "Executed" +} + +task 9 'run'. lines 158-178: +{ + "gas_used": 671627, + "status": "Executed" +} + +task 10 'run'. lines 181-202: +{ + "gas_used": 340983, + "status": "Executed" +} + +task 11 'run'. lines 205-226: +{ + "gas_used": 671627, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/block_reward/basic.move b/vm/framework/starcoin-framework/integration-tests/block_reward/basic.move new file mode 100644 index 0000000000..2fb6e93f01 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/block_reward/basic.move @@ -0,0 +1,226 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr Genesis + +//# run --signers Genesis + +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 2; + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers alice + + +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 1; + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + // failed with ENOT_GENESIS_ACCOUNT + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: "Keep(ABORTED { code: 2818" + + +//# run --signers Genesis +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 0; // if current_number == 0 then do_nothing + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers Genesis +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 1; //failed with ECURRENT_NUMBER_IS_WRONG + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: "Keep(ABORTED { code: 26119" + + +//# run --signers Genesis +// author account doesn't exist, process_block_reward() will create the account +script { + use std::vector; + use starcoin_framework::block_reward; + + fun process_block_reward(account: signer) { + let current_number = 3; + let current_reward = 1000; + + // let auth_key_vec = vector::empty(); + // let current_author = create_object_address( + // &signer::address_of(&account), + // auth_key_vec + // );// Authenticator::derived_address(copy auth_key_vec); + + block_reward::process_block_reward( + &account, + current_number, + current_reward, + @0x1, + vector::empty(), + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers Genesis +// author account doesn't exist, process_block_reward() will create the account +script { + use starcoin_framework::block_reward; + + fun process_block_reward(account: signer) { + let current_number = 4; + let current_reward = 1000; + + let current_author = @0x2; + // auth_key_vec argument is deprecated in StarcoinFrameworklib v5 + let auth_key_vec = x""; + + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers Genesis +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 5; + let current_reward = 0; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers Genesis + +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 6; + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED + + +//# run --signers Genesis + +script { + use starcoin_framework::block_reward; + use starcoin_framework::vector; + + fun process_block_reward(account: signer) { + let current_number = 7; + let current_reward = 1000; + let current_author = @alice; + let auth_key_vec = vector::empty(); + block_reward::process_block_reward( + &account, + current_number, + current_reward, + current_author, + auth_key_vec, + starcoin_framework::coin::zero() + ); + } +} +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.exp b/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.exp new file mode 100644 index 0000000000..296c5e8867 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-12: +{ + "gas_used": 14561, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.move b/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.move new file mode 100644 index 0000000000..e10fb3414d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/chain_id/chain_id.move @@ -0,0 +1,12 @@ +//# init -n test + +//# faucet --addr alice --amount 50000000 + +//# run --signers alice +script { + use starcoin_framework::chain_id; + + fun main() { + assert!(chain_id::get() == 255, 1000); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.exp b/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.exp new file mode 100644 index 0000000000..9d362f756e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.exp @@ -0,0 +1,13 @@ +processed 7 tasks + +task 5 'run'. lines 42-53: +{ + "gas_used": 15362, + "status": "Executed" +} + +task 6 'run'. lines 56-72: +{ + "gas_used": 14984, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.move b/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.move new file mode 100644 index 0000000000..7a9d0c24b5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/delta_size_check.move @@ -0,0 +1,72 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# publish +module alice::A { + struct Coin has key, store { + u: u64, + } + + public fun new(): Coin { + Coin { u: 1 } + } + + public fun value(this: &Coin): u64 { + //borrow of move + let f = (move this).u; + f + } + + public fun destroy(t: Coin): u64 { + let Coin { u } = t; + u + } +} + + +//# publish +module bob::Tester { + use alice::A; + + struct Pair has key, store { x: A::Coin, y: A::Coin } + + public fun test(account: &signer) { + move_to(account, Pair { x: A::new(), y: A::new() }); + } +} + + +//# run --signers bob + + +script { + use bob::Tester; + + fun main(account: signer) { + Tester::test(&account); + } +} + +// check: EXECUTED + + +//# run --signers alice + + +script { + use alice::A; + + fun main() { + let x = A::new(); + let x_ref = &x; + let y = A::value(x_ref); + assert!(y == 1, 42); + let z = A::destroy(x); + assert!(z == 1, 43); + } +} + +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/check/gas_check.exp b/vm/framework/starcoin-framework/integration-tests/check/gas_check.exp new file mode 100644 index 0000000000..8b4bbae000 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/gas_check.exp @@ -0,0 +1,29 @@ +processed 5 tasks + +task 2 'run'. lines 5-14: +{ + "gas_used": 1000, + "status": "OutOfGas" +} + +task 3 'run'. lines 17-26: +{ + "gas_used": 0, + "status": { + "Discard": { + "status_code": "14", + "status_code_name": "MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS" + } + } +} + +task 4 'run'. lines 29-38: +{ + "gas_used": 0, + "status": { + "Discard": { + "status_code": "13", + "status_code_name": "MAX_GAS_UNITS_EXCEEDS_MAX_GAS_UNITS_BOUND" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/check/gas_check.move b/vm/framework/starcoin-framework/integration-tests/check/gas_check.move new file mode 100644 index 0000000000..ee0ee2daec --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/gas_check.move @@ -0,0 +1,38 @@ +//# init -n dev + +//# faucet --addr alice --amount 50000000 + +//# run --signers alice --gas-budget 1000 +script { + fun main() { + while (true) {} + } +} + +// check: OUT_OF_GAS +// check: gas_used +// check: 1000 + + +//# run --signers alice --gas-budget 599 + +script { + fun main() { + while (true) {} + } +} + +// check: Discard +// check: MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS + + +//# run --signers alice --gas-budget 400000001 + +script { + fun main() { + while (true) {} + } +} + +// check: Discard +// check: MAX_GAS_UNITS_EXCEEDS_MAX_GAS_UNITS_BOUND \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.exp b/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.exp new file mode 100644 index 0000000000..61fe6f21e3 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.exp @@ -0,0 +1,13 @@ +processed 6 tasks + +task 4 'run'. lines 9-30: +{ + "gas_used": 700, + "status": "OutOfGas" +} + +task 5 'run'. lines 33-44: +{ + "gas_used": 24187, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.move b/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.move new file mode 100644 index 0000000000..32b5b6f96b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/gas_used_out.move @@ -0,0 +1,44 @@ +//# init -n dev + +//# faucet --addr alice --amount 1000 + +//# faucet --addr bob --amount 1000 + +//# faucet --addr default + +//# run --signers alice --gas-budget 700 + +// when gas used out, the txn is kept, the state is unchanged except balance is set to 0. + +script { + use starcoin_framework::coin; + + fun main(account: signer) { + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + coin::transfer(&account, @bob, 10); + // gas used out + } +} +// check: EXECUTION_FAILURE +// check: OUT_OF_GAS +// check: gas_used +// check: 700 + + +//# run --signers default +script { + use starcoin_framework::coin; + + fun main() { + // check the state is unchanged + assert!(coin::balance(@bob) == 1000, 42); + assert!(coin::balance(@alice) == 300, 43); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.exp b/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.exp new file mode 100644 index 0000000000..c5636c9ef4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.exp @@ -0,0 +1,25 @@ +processed 8 tasks + +task 4 'run'. lines 46-58: +{ + "gas_used": 13820, + "status": "Executed" +} + +task 5 'run'. lines 61-73: +{ + "gas_used": 23411, + "status": "Executed" +} + +task 6 'run'. lines 76-90: +{ + "gas_used": 29431, + "status": "Executed" +} + +task 7 'run'. lines 93-107: +{ + "gas_used": 23650, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.move b/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.move new file mode 100644 index 0000000000..5008df4848 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/check/resource_move_to_or_from.move @@ -0,0 +1,108 @@ +//# init -n dev + +//# faucet --addr alice --amount 900000 + +//# faucet --addr bob --amount 900000 + + + +//# publish + +module alice::M { + use starcoin_framework::signer; + + struct Cup has key, store { + a: u64, + } + + public fun new(): Cup { + Cup { a: 1 } + } + + public fun get_a(this: &Cup): u64 { + this.a + } + + public fun publish(cup: Cup, account: &signer) { + move_to(account, cup); + } + + public fun get_cup(account: &signer): Cup acquires Cup { + let cup = borrow_global(signer::address_of(account)); + Cup { a: cup.a } + } + + public fun destroy(sender: &signer): u64 acquires Cup { + let Cup { a } = move_from(signer::address_of(sender)); + a + } + + public fun destroy_x(x: Cup) { + Cup { a: _ } = x; + } +} + + +//# run --signers bob + +script { + use alice::M; + + fun main(account: signer) { + let cup = M::new(); + M::publish(cup, &account); + } +} + +// check: EXECUTED +//// check: delta_size 8 + + +//# run --signers bob + +script { + use alice::M; + + fun main(account: signer) { + let y = M::destroy(&account); + assert!(y == 1, 41) + } +} + +// check: EXECUTED +//// check: delta_size -8 + + +//# run --signers bob + +script { + use alice::M; + + fun main(account: signer) { + let cup = M::new(); + M::publish(cup, &account); + let y = M::destroy(&account); + assert!(y == 1, 41); + } +} + +// check: EXECUTED +//// check: delta_size 0 + + +//# run --signers bob + +script { + use alice::M; + + fun main(account: signer) { + let cup = M::new(); + M::publish(cup, &account); + let cup = M::get_cup(&account); + M::destroy_x(cup) + } +} + +// check: EXECUTED +//// check: delta_size 8 + diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.exp b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.exp new file mode 100644 index 0000000000..6cf22cdb7e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.exp @@ -0,0 +1,195 @@ +processed 18 tasks + +task 4 'run'. lines 52-65: +{ + "gas_used": 1340635, + "status": "Executed" +} + +task 5 'run'. lines 68-89: +{ + "gas_used": 348942, + "status": "Executed" +} + +task 6 'run'. lines 92-107: +{ + "gas_used": 31595, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 7 'run'. lines 110-126: +{ + "gas_used": 31595, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 8 'run'. lines 129-144: +{ + "gas_used": 31691, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66942" + } + } +} + +task 9 'run'. lines 147-163: +{ + "gas_used": 31595, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 10 'run'. lines 166-176: +{ + "gas_used": 21132, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao_modify_config_proposal" + } + }, + "abort_code": "262545" + } + } +} + +task 11 'run'. lines 181-221: +{ + "gas_used": 193974, + "status": "Executed" +} + +task 12 'run'. lines 224-250: +{ + "gas_used": 45696, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66942" + } + } +} + +task 13 'run'. lines 253-273: +{ + "gas_used": 9923, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 14 'run'. lines 276-296: +{ + "gas_used": 9955, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 15 'run'. lines 299-319: +{ + "gas_used": 10051, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 16 'run'. lines 322-342: +{ + "gas_used": 10083, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} + +task 17 'run'. lines 345-365: +{ + "gas_used": 10115, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66943" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.move b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.move new file mode 100644 index 0000000000..82b76285d1 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_config.move @@ -0,0 +1,365 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# publish +module alice::my_coin { + use std::string; + + use starcoin_framework::coin; + use starcoin_framework::coin::{BurnCapability, FreezeCapability, MintCapability}; + use starcoin_framework::dao; + + struct MyCoin has copy, drop, store { } + + struct CapabilityHolder has key { + burn_cap: BurnCapability, + freeze_cap: FreezeCapability, + mint_cap: MintCapability, + } + + public fun init(account: &signer) { + let ( + burn_cap, + freeze_cap, + mint_cap + ) = coin::initialize( + account, + string::utf8(b"MyCoin"), + string::utf8(b"MyCoin"), + 8, + true + ); + coin::register(account); + dao::plugin( + account, + 60 * 1000, + 60 * 60 * 1000, + 4, + 60 * 60 * 1000 + ); + move_to(account, CapabilityHolder { + burn_cap, + freeze_cap, + mint_cap, + }); + } +} + + +//# run --signers alice +script { + use alice::my_coin::{Self, MyCoin}; + use std::option; + use starcoin_framework::coin; + + fun main(account: signer) { + my_coin::init(&account); + let market_cap = coin::supply(); + assert!(option::destroy_some(market_cap) == 0, 8001); + assert!(coin::is_account_registered(@alice), 8002); + } +} +// check: EXECUTED + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + + fun set_dao_config(signer: signer) { + let cap = on_chain_config::extract_modify_config_capability>( + &signer + ); + + dao::set_voting_delay(&mut cap, 30 * 1000); + dao::set_voting_period(&mut cap, 30 * 30 * 1000); + dao::set_voting_quorum_rate(&mut cap, 50); + dao::set_min_action_delay(&mut cap, 30 * 30 * 1000); + + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun set_dao_config(signer: signer) { + let cap = + on_chain_config::extract_modify_config_capability>( + &signer + ); + dao::set_voting_delay(&mut cap, 0); + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: "Keep(ABORTED { code 66943" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun set_dao_config(signer: signer) { + let cap = + on_chain_config::extract_modify_config_capability>( + &signer + ); + dao::set_voting_period(&mut cap, 0); + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun set_dao_config(signer: signer) { + let cap = on_chain_config::extract_modify_config_capability>( + &signer + ); + dao::set_voting_quorum_rate(&mut cap, 0); + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: "Keep(ABORTED { code: 359943" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun set_dao_config(signer: signer) { + let cap = + on_chain_config::extract_modify_config_capability>( + &signer + ); + dao::set_min_action_delay(&mut cap, 0); + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers bob + +script { + use alice::my_coin::MyCoin; + use starcoin_framework::dao_modify_config_proposal; + + fun test_plugin(signer: signer) { + dao_modify_config_proposal::plugin(&signer); //ERR_NOT_AUTHORIZED + } +} +// check: "Keep(ABORTED { code: 102658" + + + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun modify_dao_config(signer: signer) { + let cap = on_chain_config::extract_modify_config_capability>( + &signer + ); + let voting_delay = 30 * 1000; + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 50; + let min_action_delay = 30 * 30 * 1000; + + dao::modify_dao_config( + &mut cap, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + + let voting_delay = 0; + let voting_period = 0; + let voting_quorum_rate = 0; + let min_action_delay = 0; + + dao::modify_dao_config( + &mut cap, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + use starcoin_framework::on_chain_config; + + fun modify_dao_config(signer: signer) { + let cap = on_chain_config::extract_modify_config_capability>( + &signer + ); + let voting_delay = 30 * 1000; + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 101; //ERR_QUORUM_RATE_INVALID + let min_action_delay = 30 * 30 * 1000; + + dao::modify_dao_config( + &mut cap, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + + on_chain_config::restore_modify_config_capability(cap); + } +} +// check: "Keep(ABORTED { code: 359943" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + + fun new_dao_config_failed(_signer: signer) { + let voting_delay = 0; //should > 0 + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 50; + let min_action_delay = 30 * 30 * 1000; + + dao::new_dao_config( + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + + fun new_dao_config_failed(_signer: signer) { + let voting_delay = 30 * 1000; + let voting_period = 0; //should > 0 + let voting_quorum_rate = 50; + let min_action_delay = 30 * 30 * 1000; + + dao::new_dao_config( + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + + fun new_dao_config_failed(_signer: signer) { + let voting_delay = 30 * 1000; + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 0; //should > 0 + let min_action_delay = 30 * 30 * 1000; + + dao::new_dao_config( + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + + fun new_dao_config_failed(_signer: signer) { + let voting_delay = 30 * 1000; + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 101; //should <= 100 + let min_action_delay = 30 * 30 * 1000; + + dao::new_dao_config( + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + } +} +// check: "Keep(ABORTED { code: 360199" + + +//# run --signers alice + +script { + use starcoin_framework::dao; + use alice::my_coin::MyCoin; + + fun new_dao_config_failed(_signer: signer) { + let voting_delay = 30 * 1000; + let voting_period = 30 * 30 * 1000; + let voting_quorum_rate = 50; + let min_action_delay = 0; //should > 0 + + dao::new_dao_config( + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay + ); + } +} +// check: "Keep(ABORTED { code: 360199" \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.exp b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.exp new file mode 100644 index 0000000000..8414097136 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.exp @@ -0,0 +1,384 @@ +processed 43 tasks + +task 5 'run'. lines 11-27: +{ + "gas_used": 2430743, + "status": "Executed" +} + +task 6 'run'. lines 29-45: +{ + "gas_used": 12429, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao_modify_config_proposal" + } + }, + "abort_code": "65938" + } + } +} + +task 7 'run'. lines 48-64: +{ + "gas_used": 1175537, + "status": "Executed" +} + +task 8 'run'. lines 67-83: +{ + "gas_used": 1175537, + "status": "Executed" +} + +task 9 'run'. lines 86-101: +{ + "gas_used": 1068873, + "status": { + "ExecutionFailure": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "function": 18, + "code_offset": 55 + } + } +} + +task 11 'run'. lines 106-132: +{ + "gas_used": 487869, + "status": "Executed" +} + +task 12 'run'. lines 135-162: +{ + "gas_used": 1198142, + "status": "Executed" +} + +task 13 'run'. lines 165-190: +{ + "gas_used": 1185321, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198016" + } + } +} + +task 14 'run'. lines 193-212: +{ + "gas_used": 1091179, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66946" + } + } +} + +task 15 'run'. lines 215-238: +{ + "gas_used": 1073817, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66940" + } + } +} + +task 16 'run'. lines 241-269: +{ + "gas_used": 59161, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66941" + } + } +} + +task 17 'run'. lines 272-296: +{ + "gas_used": 41415, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66940" + } + } +} + +task 18 'run'. lines 299-313: +{ + "gas_used": 32104, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66941" + } + } +} + +task 19 'run'. lines 316-330: +{ + "gas_used": 33802, + "status": "Executed" +} + +task 20 'run'. lines 333-347: +{ + "gas_used": 15120, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66940" + } + } +} + +task 21 'run'. lines 350-366: +{ + "gas_used": 29436, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 23 'run'. lines 371-393: +{ + "gas_used": 37453, + "status": "Executed" +} + +task 24 'run'. lines 396-412: +{ + "gas_used": 36677, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "394621" + } + } +} + +task 25 'run'. lines 415-437: +{ + "gas_used": 1099668, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 26 'run'. lines 440-455: +{ + "gas_used": 40977, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 27 'run'. lines 458-476: +{ + "gas_used": 53194, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 28 'run'. lines 479-497: +{ + "gas_used": 69488, + "status": "Executed" +} + +task 30 'run'. lines 502-524: +{ + "gas_used": 262315, + "status": "Executed" +} + +task 32 'run'. lines 528-542: +{ + "gas_used": 69700, + "status": "Executed" +} + +task 33 'run'. lines 545-560: +{ + "gas_used": 39425, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 34 'run'. lines 563-577: +{ + "gas_used": 67750, + "status": "Executed" +} + +task 35 'run'. lines 580-593: +{ + "gas_used": 1175537, + "status": "Executed" +} + +task 37 'run'. lines 598-620: +{ + "gas_used": 1105257, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66946" + } + } +} + +task 38 'run'. lines 623-642: +{ + "gas_used": 59297, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66946" + } + } +} + +task 39 'run'. lines 645-660: +{ + "gas_used": 32254, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66946" + } + } +} + +task 40 'run'. lines 663-685: +{ + "gas_used": 1198140, + "status": "Executed" +} + +task 42 'run'. lines 690-713: +{ + "gas_used": 51027, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "66946" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.move b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.move new file mode 100644 index 0000000000..d6a54d2cb5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_failure.move @@ -0,0 +1,717 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# faucet --addr carol + +//# block --author 0x1 --timestamp 86400000 + +//# run --signers StarcoinAssociation + + +script { + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + + fun transfer_some_token_to_alice_and_bob(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + coin::transfer(&signer, @alice, balance / 4); + coin::transfer(&signer, @bob, balance / 4); + coin::transfer(&signer, @carol, balance / 4); + } +} +// check: EXECUTED +// voting_quorum_rate should less or equal than 100 + +//# run --signers alice +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + dao_modify_config_proposal::propose( + signer, + 60 * 60 * 24 * 1000, + 0, + 101, + 0, + 0 + ); + } +} +// check: "Keep(ABORTED { code: 102919" + + +//# run --signers alice +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + dao_modify_config_proposal::propose( + signer, + 60 * 60 * 24 * 1000, + 0, + 50, + 0, + 0 + ); + } +} +// check: EXECUTED + + +//# run --signers bob +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + dao_modify_config_proposal::propose( + signer, + 60 * 60 * 24 * 1000, + 0, + 50, + 0, + 0 + ); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + dao_modify_config_proposal::propose(signer, + 60 * 60 * 24 * 1000, + 0, + 50, + 0, + 0 + ); + } +} +// check: RESOURCE_ALREADY_EXISTS + + +//# block --author 0x1 --timestamp 87000000 + +//# run --signers alice +// call cast_vote to stake some token +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let state = dao::proposal_state( + @bob, + 1 + ); + assert!(state == 2, (state as u64)); + { + let balance = coin::withdraw(&signer, 10); // less than quorum_votes + dao::cast_vote( + &signer, + @bob, + 1, + balance, + true + ); + } + } +} +// check: EXECUTED + + +//# run --signers bob +// call cast_vote to stake some token + + +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 2, (state as u64)); + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote( + &signer, + @alice, + 0, + balance, + true + ); + } + } +} +// check: EXECUTED + + +//# run --signers bob +// vote 'agree' votes on 'against' voting + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + use starcoin_framework::dao; + + fun vote(signer: signer) { + // flip + dao::change_vote(&signer, @alice, 0, false); + + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + // ERR_VOTE_STATE_MISMATCH + dao::cast_vote(&signer, @alice, 0, balance, true); + }; + // flip back + dao::change_vote(&signer, @alice, 0, true); + } +} +// check: "Keep(ABORTED { code: 360449" + + +//# run --signers bob +// cast a vote with wrong proposer, already vote others + + +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun cast(signer: signer) { + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote(&signer, @bob, 1, balance, true); + } + } +} +// check: "Keep(ABORTED { code: 360967" + + +//# run --signers bob +// cast a vote with wrong proposal id + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun cast(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote( + &signer, + @alice, + 1, + balance, + true + ); + } +} +// check: "Keep(ABORTED { code: 359431" + + +//# run --signers bob +// revoke a vote with wrong proposer + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 2, (state as u64)); + let (_, pow) = dao::vote_of( + signer::address_of(&signer), + @alice, + 0 + ); + let token = dao::revoke_vote( + &signer, + @bob, + 1, + pow / 2 + ); // proposer should be alice + coin::deposit(signer::address_of(&signer), token); + } +} +// check: "Keep(ABORTED { code: 359687" + + +//# run --signers bob +// revoke a vote with wrong proposal id + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 2, (state as u64)); + let (_, pow) = dao::vote_of(signer::address_of(&signer), @alice, 0); + let token = dao::revoke_vote( + &signer, + @alice, + 1, + pow / 2 + ); // proposal id should be 0 + coin::deposit(signer::address_of(&signer), token); + } +} +// check: "Keep(ABORTED { code: 359431" + + +//# run --signers bob +// flip_vote failed, wrong proposer + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun flip_vote(signer: signer) { + // flip + dao::change_vote(&signer, @bob, 1, false); + } +} +// check: "Keep(ABORTED { code: 359687" + + +//# run --signers bob +// flip_vote, flip 'agree' vote with 'agree', do nothing + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun flip_vote(signer: signer) { + // flip + dao::change_vote(&signer, @alice, 0, true); + } +} +// check: EXECUTED + + +//# run --signers bob +// flip_vote failed, wrong id + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun flip_vote(signer: signer) { + // flip + dao::change_vote( + &signer, @alice, 1, false); + } +} +// check: "Keep(ABORTED { code: 359431" + + +//# run --signers bob +// unstake_votes failed, wrong state, proposal is still active + + +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun unstake_votes(signer: signer) { + let coin = dao::unstake_votes(&signer, @bob, 1); + coin::deposit(signer::address_of(&signer), coin); + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# block --author 0x1 --timestamp 250000000 + +//# run --signers bob +// check state + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun check_state_and_revoke(_signer: signer) { + let state = dao::proposal_state( + @alice, + 0 + ); + assert!(state == 4, (state as u64)); + let state = dao::proposal_state( + @bob, + 1 + ); + assert!(state == 3, (state as u64)); + } +} +// check: EXECUTED + + +//# run --signers bob +// unstake_votes failed, wrong proposer + + +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun unstake_votes(signer: signer) { + let coin = dao::unstake_votes(&signer, @bob, 1); + coin::deposit(signer::address_of(&signer), coin); + } +} +// check: "Keep(ABORTED { code: 359682" + + +//# run --signers bob +// can't cast vote in the state other than ACTIVE + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 4, (state as u64)); + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance); + dao::cast_vote( + &signer, @alice, 0, balance, true); + } + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# run --signers bob +// can't change vote in the state other than ACTIVE + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 4, (state as u64)); + dao::change_vote(&signer, @alice, 0, false); + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# run --signers bob + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 4, (state as u64)); + let (_, pow) = dao::vote_of(signer::address_of(&signer), @alice, 0); + let token = dao::revoke_vote(&signer, @alice, 0, pow / 2); + coin::deposit(signer::address_of(&signer), token); + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# run --signers bob + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun queue_proposal(_signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 4, (state as u64)); + + dao::queue_proposal_action(@alice, 0); + // ModifyDaoConfigProposal::execute(@alice, 0); + let state = dao::proposal_state(@alice, 0); + assert!(state == 5, (state as u64)); + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 260000000 + +//# run --signers bob + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun execute_proposal_action(_signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 6, (state as u64)); + dao_modify_config_proposal::execute(@alice, 0); + assert!(dao::voting_delay() == 3600 * 24 * 1000, dao::voting_delay()); + assert!(dao::voting_quorum_rate() == 50, 1000); + } +} +// check: EXECUTED + + +//! block-prologue +//! author: genesis +//! block-number: 5 +//! block-time: 310000000 + +//# block --author 0x1 --timestamp 310000000 + +//# run --signers bob + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun cleanup_proposal(_signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 7, (state as u64)); + dao::destroy_terminated_proposal(@alice, 0); + } +} +// check: EXECUTED + + +//# run --signers alice + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun cleanup_proposal(_signer: signer) { + let state = dao::proposal_state(@bob, 1); + assert!(state == 3, (state as u64)); + //ERR_PROPOSAL_STATE_INVALID + dao::extract_proposal_action(@bob, 1); + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# run --signers alice + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun cleanup_proposal(_signer: signer) { + let state = dao::proposal_state(@bob, 1); + assert!(state == 3, (state as u64)); + dao::destroy_terminated_proposal(@bob, 1); + } +} +// check: EXECUTED + + +//# run --signers alice +// alice proposes a new proposal, the proposal_id is 2. + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + dao_modify_config_proposal::propose( + signer, 60 * 60 * 24 * 1000, 0, 50, 0, 0); + } +} +// check: EXECUTED + +//# block --author 0x1 --timestamp 400000000 + + +//# run --signers bob +// cast_vote will be failed, already vote others + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let state = dao::proposal_state(@alice, 2); + assert!(state == 2, (state as u64)); + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote( + &signer, @alice, 2, balance, true); + } + } +} +// check: "Keep(ABORTED { code: 360967" + + +//# run --signers bob +// revoke vote failed, alice has already proposed new proposal with id(2) + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(signer: signer) { + let state = dao::proposal_state(@alice, 2); + assert!(state == 2, (state as u64)); + let (_, pow) = dao::vote_of(signer::address_of(&signer), @alice, 0); + let token = dao::revoke_vote(&signer, @alice, 2, pow / 2); + coin::deposit(signer::address_of(&signer), token); + } +} +// check: "Keep(ABORTED { code: 360967" + + +//# run --signers bob +// flip_vote failed, alice has already proposed new proposal with id(2) + + +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun flip_vote(signer: signer) { + // flip + dao::change_vote( + &signer, @alice, 2, false); + } +} +// check: "Keep(ABORTED { code: 360967" + + +//# run --signers carol +// call cast_vote to stake some token + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let state = dao::proposal_state(@alice, 2); + assert!(state == 2, (state as u64)); + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote( + &signer, @alice, 2, balance, false); + } + } +} +// check: EXECUTED + +//# block --author 0x1 --timestamp 600000000 + + +//# run --signers bob +// unstake_votes failed, wrong proposal id + + +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun unstake_votes(signer: signer) { + let state = dao::proposal_state( + @alice, 2 + ); + assert!(state == 3, (state as u64)); + // bob should unstake proposal [@alice, 0] + let coin = dao::unstake_votes( + &signer, @alice, 2); + coin::deposit(signer::address_of(&signer), coin); + } +} + +// check: "Keep(ABORTED { code: 360967" + + + + diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.exp b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.exp new file mode 100644 index 0000000000..066a0583ca --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.exp @@ -0,0 +1,83 @@ +processed 21 tasks + +task 4 'run'. lines 9-21: +{ + "gas_used": 1763221, + "status": "Executed" +} + +task 5 'run'. lines 24-33: +{ + "gas_used": 1100997, + "status": "Executed" +} + +task 6 'run'. lines 36-59: +{ + "gas_used": 49947, + "status": "Executed" +} + +task 8 'run'. lines 64-82: +{ + "gas_used": 1143976, + "status": "Executed" +} + +task 9 'run'. lines 85-102: +{ + "gas_used": 1136908, + "status": "Executed" +} + +task 11 'run'. lines 107-126: +{ + "gas_used": 348880, + "status": "Executed" +} + +task 12 'run'. lines 129-149: +{ + "gas_used": 1148394, + "status": "Executed" +} + +task 13 'run'. lines 152-191: +{ + "gas_used": 2739806, + "status": "Executed" +} + +task 15 'run'. lines 197-219: +{ + "gas_used": 252843, + "status": "Executed" +} + +task 17 'run'. lines 225-240: +{ + "gas_used": 41583, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "dao" + } + }, + "abort_code": "198011" + } + } +} + +task 18 'run'. lines 243-267: +{ + "gas_used": 306887, + "status": "Executed" +} + +task 20 'run'. lines 273-285: +{ + "gas_used": 58675, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.move b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.move new file mode 100644 index 0000000000..2f3f47c281 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dao/test_dao_propose.move @@ -0,0 +1,286 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# block --author 0x1 --timestamp 86400000 + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + + fun transfer_some_token_to_alice_and_bob(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + coin::transfer(&signer, @alice, balance / 4); + coin::transfer(&signer, @bob, balance / 4); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun propose(account: signer) { + dao_modify_config_proposal::propose(account, 60 * 60 * 24 * 1000, 0, 50, 0, 0); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_std::debug; + use starcoin_framework::dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao_modify_config_proposal; + + fun proposal_info(_signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 1, (state as u64)); + + let (id, start_time, end_time, for_votes, against_votes) + = dao::proposal_info(@alice); + + assert!(id == 0, 101); + debug::print(&std::string::utf8(b"test_dao_proposal::proposal_info | Start time:")); + debug::print(&start_time); + assert!(start_time == 86460000, 102); // be consistent with genesis config + assert!(end_time == 90060000, 103); // be consistent with genesis config + assert!(for_votes == 0, 104); + assert!(against_votes == 0, 104); + } +} +// check: EXECUTED + +//# block --author 0x1 --timestamp 87000000 + + +//# run --signers bob +// call cast_vote to stake some token + + +script { + use std::signer; + use starcoin_framework::dao; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun vote(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote( + &signer, @alice, 0, balance, true); + } +} +// check: EXECUTED + + +//# run --signers bob +// call cast_vote again to stake more token + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote(&signer, @alice, 0, balance, true); + } +} +// check: EXECUTED + +//# block --author 0x1 --timestamp 88000000 + + +//# run --signers bob +// test revoke_vote + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun check_state_and_revoke(account: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 2, (state as u64)); + let (_, pow) = dao::vote_of(signer::address_of(&account), @alice, 0); + let token = dao::revoke_vote(&account, @alice, 0, pow / 2); + coin::deposit(signer::address_of(&account), token); + } +} +// check: EXECUTED + + +//# run --signers bob + + +script { + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + use starcoin_framework::signer; + + fun recast_vote(signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 2, (state as u64)); + { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote(&signer, @alice, 0, balance, true); + } + } +} +// check: EXECUTED + + +//# run --signers bob +// test flip_vote + + +script { + use std::signer; + use starcoin_framework::dao; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun flip_vote(account: signer) { + // flip + dao::change_vote(&account, @alice, 0, false); + { + let balance = coin::balance(signer::address_of(&account)); + let balance = coin::withdraw(&account, balance / 2); + dao::cast_vote(&account, @alice, 0, balance, false); + }; + // revoke while 'against' + { + let (_, pow) = dao::vote_of(signer::address_of(&account), @alice, 0); + let token = dao::revoke_vote( + &account, + @alice, + 0, + pow / 10 + ); + coin::deposit(signer::address_of(&account), token); + }; + // flip back + dao::change_vote(&account, @alice, 0, true); + { + let balance = coin::balance(signer::address_of(&account)); + let balance = coin::withdraw(&account, balance / 2); + dao::cast_vote(&account, @alice, 0, balance, true); + }; + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 180000000 + + +//# run --signers bob + +script { + use std::signer; + use starcoin_framework::dao; + use starcoin_framework::coin; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun queue_proposal(account: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 4, (state as u64)); + { + let token = dao::unstake_votes(&account, @alice, 0); + coin::deposit(signer::address_of(&account), token); + }; + dao::queue_proposal_action(@alice, 0); + // ModifyDaoConfigProposal::execute(@alice, 0); + let state = dao::proposal_state(@alice, 0); + assert!(state == 5, (state as u64)); + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 250000000 + + +//# run --signers bob +script { + use starcoin_std::debug; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun cleanup_proposal_should_fail(_signer: signer) { + debug::print(&std::string::utf8(b"cleanup_proposal_should_fail | entered")); + + dao::destroy_terminated_proposal(@alice, 0); + + debug::print(&std::string::utf8(b"cleanup_proposal_should_fail | exited")); + } +} +// check: "Keep(ABORTED { code: 359169" + + +//# run --signers bob +script { + use starcoin_std::debug; + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun execute_proposal_action(_signer: signer) { + debug::print(&std::string::utf8(b"execute_proposal_action | entered")); + let state = dao::proposal_state(@alice, 0); + + debug::print(&std::string::utf8(b"execute_proposal_action | state = ")); + debug::print(&state); + assert!(state == 6, (state as u64)); + dao_modify_config_proposal::execute(@alice, 0); + + debug::print(&std::string::utf8(b"execute_proposal_action | after dao_modify_config_proposal::execute ")); + + assert!(dao::voting_delay() == 3600 * 24 * 1000, dao::voting_delay()); + assert!(dao::voting_quorum_rate() == 50, 1000); + + debug::print(&std::string::utf8(b"execute_proposal_action | exited ")); + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 310000000 + + +//# run --signers bob +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun cleanup_proposal(_signer: signer) { + let state = dao::proposal_state(@alice, 0); + assert!(state == 7, (state as u64)); + dao::destroy_terminated_proposal(@alice, 0); + } +} +// check: EXECUTED + diff --git a/vm/framework/starcoin-framework/integration-tests/debug/debug.exp b/vm/framework/starcoin-framework/integration-tests/debug/debug.exp new file mode 100644 index 0000000000..8fe0d510b5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/debug/debug.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 42-51: +{ + "gas_used": 79241, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/debug/debug.move b/vm/framework/starcoin-framework/integration-tests/debug/debug.move new file mode 100644 index 0000000000..d55fd1a86e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/debug/debug.move @@ -0,0 +1,53 @@ +//# init -n dev + +//# faucet --addr default --amount 100000000000000000 + +//# publish +module default::M { + use starcoin_framework::debug; + use starcoin_framework::vector; + + struct Foo has copy, drop, store { x: bool } + + struct Bar has copy, drop, store { x: u128, y: Foo, z: bool } + + struct Collection has copy, drop, store { x: T } + + public fun test() { + let x: u64; + let v: vector; + let foo: Foo; + let bar: Bar; + let box: Collection; + + x = 42; + debug::print(&x); + + v = vector::empty(); + vector::push_back(&mut v, 100); + vector::push_back(&mut v, 200); + vector::push_back(&mut v, 300); + debug::print>(&v); + + foo = Foo { x: false }; + debug::print(&foo); + + bar = Bar { x: 404u128, y: Foo { x: false }, z: true }; + debug::print(&bar); + + box = Collection { x: Foo { x: false } }; + debug::print>(&box); + } +} +// check: EXECUTED + +//# run --signers default +script { + use default::M; + + fun main() { + M::test(); + } +} + +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/dummy.exp b/vm/framework/starcoin-framework/integration-tests/dummy.exp new file mode 100644 index 0000000000..06cb529239 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dummy.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-10: +{ + "gas_used": 8439, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/dummy.move b/vm/framework/starcoin-framework/integration-tests/dummy.move new file mode 100644 index 0000000000..2c4bbc82ef --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/dummy.move @@ -0,0 +1,8 @@ +//# init -n dev + +//# faucet --addr alice --amount 10000000000000 + +//# run --signers alice +script { + fun main() {} +} diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.exp b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.exp new file mode 100644 index 0000000000..6358a966c1 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 27-37: +{ + "gas_used": 620000, + "status": "OutOfGas" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.move b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.move new file mode 100644 index 0000000000..dd1281f9d8 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_module.move @@ -0,0 +1,37 @@ +//# init -n dev + +//# faucet --addr default --amount 100000000000000000 + +//# publish +module default::Swapper { + use std::vector; + + public fun swap_it_up(vec_len: u64) { + let v = vector::empty(); + + let i = 0; + while (i < vec_len) { + vector::push_back(&mut v, i); + i = i + 1; + }; + + i = 0; + + while (i < vec_len / 2) { + vector::swap(&mut v, i, vec_len - i - 1); + i = i + 1; + }; + } +} + +//# run --signers default --gas-budget 620000 +script { + use default::Swapper; + + fun main() { + Swapper::swap_it_up(10000) + } +} +// check: "EXECUTION_FAILURE { status_code: OUT_OF_GAS, location: 0xd98f86e3303c97b00313854b8314f51b::Swapper," +// check: "gas_used: 620000," +// check: "Keep(OUT_OF_GAS)" diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.exp b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.exp new file mode 100644 index 0000000000..d946daa536 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 30-39: +{ + "gas_used": 621000, + "status": "OutOfGas" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.move b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.move new file mode 100644 index 0000000000..f6006c0133 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_nested_module.move @@ -0,0 +1,39 @@ +//# init -n dev + +//# faucet --addr default --amount 100000000000000000 + +//# publish +module default::Swapper { + use std::vector; + + public fun call(x: u64) { + swap_it_up(x) + } + public fun swap_it_up(vec_len: u64) { + let v = vector::empty(); + + let i = 0; + while (i < vec_len) { + vector::push_back(&mut v, i); + i = i + 1; + }; + + i = 0; + + while (i < vec_len / 2) { + vector::swap(&mut v, i, vec_len - i - 1); + i = i + 1; + }; + } +} + +//# run --signers default --gas-budget 621000 +script { +use default::Swapper; +fun main() { + Swapper::call(10000) +} +} +// check: "EXECUTION_FAILURE { status_code: OUT_OF_GAS, location: 0xd98f86e3303c97b00313854b8314f51b::Swapper," +// check: "gas_used: 621000," +// check: "Keep(OUT_OF_GAS)" diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.exp b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.exp new file mode 100644 index 0000000000..c0ffe3ed03 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 10-36: +{ + "gas_used": 700, + "status": "OutOfGas" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.move b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.move new file mode 100644 index 0000000000..17ac31abe0 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_in_script_after_module_call.move @@ -0,0 +1,36 @@ +//# init -n dev + +//# faucet --addr default --amount 100000000000000000 + +//# publish +module default::Nop { + public fun nop() {} +} + +//# run --signers default --gas-budget 700 +script { + use std::vector; + use default::Nop; + + fun main() { + Nop::nop(); + let v = vector::empty(); + let vec_len = 1000; + + let i = 0; + while (i < vec_len) { + vector::push_back(&mut v, i); + i = i + 1; + }; + + i = 0; + + while (i < vec_len / 2) { + vector::swap(&mut v, i, vec_len - i - 1); + i = i + 1; + }; + } +} +// check: "EXECUTION_FAILURE { status_code: OUT_OF_GAS, location: Script," +// check: "gas_used: 700," +// check: "Keep(OUT_OF_GAS)" diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.exp b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.exp new file mode 100644 index 0000000000..bddd38e668 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 12-22: +{ + "gas_used": 600000, + "status": "OutOfGas" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.move b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.move new file mode 100644 index 0000000000..2c8022276b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epilogue/out_of_gas_recursive.move @@ -0,0 +1,22 @@ +//# init -n dev + +//# faucet --addr default --amount 100000000000000000 + +//# publish +module default::OddOrEven { + public fun is_odd(x: u64): bool { if (x == 0) false else is_even(x - 1) } + + public fun is_even(x: u64): bool { if (x == 0) true else is_odd(x - 1) } +} + +//# run --signers default --gas-budget 600000 +script { + use default::OddOrEven; + + fun main() { + OddOrEven::is_odd(1001); + } +} +// check: "EXECUTION_FAILURE { status_code: OUT_OF_GAS, location: 0xd98f86e3303c97b00313854b8314f51b::OddOrEven," +// check: "gas_used: 600000," +// check: "Keep(OUT_OF_GAS)" diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.exp b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.exp new file mode 100644 index 0000000000..a187548286 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-40: +{ + "gas_used": 43437669, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.move b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.move new file mode 100644 index 0000000000..ea2cbba494 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_max.move @@ -0,0 +1,40 @@ +//# init -n dev + +//# faucet --addr Genesis + +//# run --signers Genesis +script { + use starcoin_framework::consensus_config; + use starcoin_framework::epoch; + + fun adjust_epoch_block_time(genesis_account: signer) { + let block_number = 1; + let block_time_milliseonds = 1000; + let times = 0; + let config = consensus_config::get_config(); + let base_block_time_target = consensus_config::base_block_time_target(&config); + let max_block_time_target = consensus_config::max_block_time_target(&config); + let pre_block_time_target = epoch::block_time_target(); + + while (epoch::number() < 10) { + let uncles = 1; + if (block_number == epoch::end_block_number()) { + uncles = 0; + //Debug::print(&Epoch::block_time_target()); + }; + let _reward = epoch::adjust_epoch(&genesis_account, block_number, block_time_milliseonds, uncles, 0); + + let block_time_target = epoch::block_time_target(); + //Debug::print(&block_time_target); + assert!(pre_block_time_target <= block_time_target, 101); + assert!(block_time_target >= base_block_time_target, 102); + assert!(block_time_target <= max_block_time_target, 103); + times = times + 1; + block_number = block_number + 1; + block_time_milliseonds = block_time_milliseonds + block_time_target; + pre_block_time_target = block_time_target; + }; + } +} + +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.exp b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.exp new file mode 100644 index 0000000000..92d3958d2d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-36: +{ + "gas_used": 43276937, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.move b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.move new file mode 100644 index 0000000000..a5eedd9856 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_block_time_min.move @@ -0,0 +1,36 @@ +//# init -n dev + +//# faucet --addr Genesis + +//# run --signers Genesis +script { + use starcoin_framework::consensus_config; + use starcoin_framework::epoch; + + fun main(genesis_account: signer) { + let block_number = 1; + let block_time_milliseonds = 1000; + let times = 0; + let config = consensus_config::get_config(); + let base_block_time_target = consensus_config::base_block_time_target(&config); + let min_block_time_target = consensus_config::min_block_time_target(&config); + let pre_block_time_target = epoch::block_time_target(); + while (epoch::number() < 10) { + if (block_number == epoch::end_block_number()) { + //Debug::print(&Epoch::block_time_target()); + }; + let _reward = epoch::adjust_epoch(&genesis_account, block_number, block_time_milliseonds, 0, 0); + let block_time_target = epoch::block_time_target(); + assert!(pre_block_time_target >= block_time_target, 101); + assert!(block_time_target >= min_block_time_target, 102); + assert!(block_time_target <= base_block_time_target, 103); + times = times + 1; + block_number = block_number + 1; + block_time_milliseonds = block_time_milliseonds + block_time_target; + }; + } +} + + +// check: EXECUTED + diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.exp b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.exp new file mode 100644 index 0000000000..bbbbba8aa7 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.exp @@ -0,0 +1,77 @@ +processed 9 tasks + +task 3 'run'. lines 7-20: +{ + "gas_used": 38830, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "system_addresses" + } + }, + "abort_code": "327683" + } + } +} + +task 4 'run'. lines 23-36: +{ + "gas_used": 80346, + "status": "Executed" +} + +task 5 'run'. lines 39-54: +{ + "gas_used": 51293, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "epoch" + } + }, + "abort_code": "65637" + } + } +} + +task 6 'run'. lines 57-76: +{ + "gas_used": 48248, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "epoch" + } + }, + "abort_code": "19" + } + } +} + +task 7 'run'. lines 79-98: +{ + "gas_used": 54121, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "epoch" + } + }, + "abort_code": "65637" + } + } +} + +task 8 'run'. lines 101-121: +{ + "gas_used": 216363, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.move b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.move new file mode 100644 index 0000000000..422c8fddad --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_epoch_failed.move @@ -0,0 +1,121 @@ +//# init -n dev + +//# faucet --addr Genesis + +//# faucet --addr alice + +//# run --signers alice +script { + use starcoin_framework::epoch; + + // ENOT_GENESIS_ACCOUNT + fun adjust_epoch(genesis_account: signer) { + let block_number = 1; + let block_time_milliseonds = 1000; + let uncles = 1; + let _reward = epoch::adjust_epoch(&genesis_account, block_number, block_time_milliseonds, uncles, 0); + } +} + +// check: "Keep(ABORTED { code: 2818" + + +//# run --signers Genesis +script { + use starcoin_framework::epoch; + + //block_number < epoch_ref.end_block_number, do nothing + fun adjust_epoch(genesis_account: signer) { + let block_number = 1; + let block_time_milliseonds = 1000; + let uncles = 1; + let _reward = epoch::adjust_epoch(&genesis_account, block_number, block_time_milliseonds, uncles, 0); + } +} + +// check: EXECUTED + + +//# run --signers Genesis +script { + use starcoin_framework::consensus_config; + use starcoin_framework::epoch; + + // EINVALID_UNCLES_COUNT + fun adjust_epoch(genesis_account: signer) { + let block_number = 1; + let block_time_milliseonds = 1000; + let config = consensus_config::get_config(); + let max_uncles_per_block = consensus_config::base_max_uncles_per_block(&config); + let uncles = max_uncles_per_block + 1; + let _reward = epoch::adjust_epoch(&genesis_account, block_number, block_time_milliseonds, uncles, 0); + } +} +// check: "Keep(ABORTED { code: 25863" + + +//# run --signers Genesis +script { + use starcoin_framework::epoch; + use starcoin_framework::consensus_config; + + //EUNREACHABLE, block_number > epoch_ref.end_block_number + fun adjust_epoch(genesis_account: signer) { + let block_time_milliseonds = 1000; + let uncles = 1; + let config = consensus_config::get_config(); + let block_number = 1 + consensus_config::epoch_block_count(&config); + let _reward = epoch::adjust_epoch( + &genesis_account, block_number, + block_time_milliseonds, + uncles, + 0 + ); + } +} +// check: "Keep(ABORTED { code: 19" + + +//# run --signers Genesis +script { + use starcoin_framework::epoch; + use starcoin_framework::consensus_config; + + //EINVALID_UNCLES_COUNT. If block_number == epoch_ref.end_block_number, uncles should be 0 + fun adjust_epoch(genesis_account: signer) { + let block_time_milliseonds = 1000; + let uncles = 1; + let config = consensus_config::get_config(); + let block_number = consensus_config::epoch_block_count(&config); + let _reward = epoch::adjust_epoch( + &genesis_account, block_number, + block_time_milliseonds, + uncles, + 0 + ); + } +} +// check: "Keep(ABORTED { code: 25863" + + +//# run --signers Genesis +script { + use starcoin_framework::epoch; + use starcoin_framework::consensus_config; + + //block_number == epoch_ref.end_block_number + fun adjust_epoch(genesis_account: signer) { + let block_time_milliseonds = 1000; + let uncles = 0; + let config = consensus_config::get_config(); + let block_number = consensus_config::epoch_block_count(&config); + let _reward = epoch::adjust_epoch( + &genesis_account, + block_number, + block_time_milliseonds, + uncles, + 0 + ); + } +} +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.exp b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.exp new file mode 100644 index 0000000000..2fe980bcb5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-63: +{ + "gas_used": 159774, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.move b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.move new file mode 100644 index 0000000000..6898f9b028 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/adjust_gas_limit.move @@ -0,0 +1,64 @@ +//# init -n dev + +//# faucet --addr Genesis + +//# run --signers Genesis +// test adjust epoch zero uncle. +script { + use std::option; + use starcoin_framework::epoch; + use starcoin_std::debug; + use starcoin_framework::consensus_config; + + fun main() { + let config = consensus_config::get_config(); + let block_gas_limit: u64 = 2000000000; + let block_count = consensus_config::epoch_block_count(&config); + + // min + let min_time_target = consensus_config::min_block_time_target(&config); + let new_gas_limit_1 = epoch::compute_gas_limit( + &config, + min_time_target, + min_time_target, + block_gas_limit, + (block_gas_limit * block_count as u128) + ); + let base_gas_limit = consensus_config::base_block_gas_limit(&config); + assert!(option::is_some(&new_gas_limit_1), 100); + let new_gas_limit_1 = option::destroy_some(new_gas_limit_1); + debug::print(&base_gas_limit); + debug::print(&new_gas_limit_1); + assert!(new_gas_limit_1 > base_gas_limit, 101); + assert!(new_gas_limit_1 > block_gas_limit, 106); + + // max + let max_time_target = consensus_config::max_block_time_target(&config); + let new_gas_limit_2 = epoch::compute_gas_limit( + &config, + max_time_target, + max_time_target, + block_gas_limit, + (block_gas_limit * block_count / 2 as u128) + ); + assert!(option::is_some(&new_gas_limit_2), 102); + let new_gas_limit_2 = option::destroy_some(new_gas_limit_2); + debug::print(&new_gas_limit_2); + assert!(new_gas_limit_2 > base_gas_limit, 103); + assert!(new_gas_limit_2 < block_gas_limit, 104); + + // other + let new_gas_limit_3 = epoch::compute_gas_limit( + &config, + 40, + 40, + block_gas_limit, + (block_gas_limit * block_count / 2 as u128) + ); + assert!(option::is_none(&new_gas_limit_3), 105); + } +} + + +// check: EXECUTED + diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.exp b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.exp new file mode 100644 index 0000000000..13ad76c673 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-121: +{ + "gas_used": 130781, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.move b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.move new file mode 100644 index 0000000000..d84bcf6efa --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_compute.move @@ -0,0 +1,121 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use starcoin_framework::epoch; + use starcoin_framework::consensus_config; + + fun compute_next_block_time_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10000; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 2000; + let max_block_time_target = 60000; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + let config = consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + + let total_uncles = 0; + let now_milli_seconds = epoch_block_count * base_block_time_target; + let last_block_time_target = base_block_time_target; + assert!( + epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ) < base_block_time_target, + 101 + ); + + let total_uncles = epoch_block_count * uncle_rate_target / 1000; + let new_block_time_target = epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ); + assert!( + new_block_time_target >= base_block_time_target - 1 || new_block_time_target <= base_block_time_target + 1, + 102 + ); + + let total_uncles = epoch_block_count * uncle_rate_target / 1000; + let now_milli_seconds = epoch_block_count * base_block_time_target / 2; + let new_block_time_target = epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ); + assert!(new_block_time_target == last_block_time_target / 2, 103); + + let total_uncles = epoch_block_count * 2 - 1; + let now_milli_seconds = epoch_block_count * base_block_time_target; + let new_block_time_target = epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ); + assert!(new_block_time_target == last_block_time_target * 2, 104); + + let last_block_time_target = max_block_time_target - 1; + let total_uncles = epoch_block_count * 2 - 1; + let now_milli_seconds = epoch_block_count * last_block_time_target; + let new_block_time_target = epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ); + assert!(new_block_time_target == max_block_time_target, 105); + + let last_block_time_target = min_block_time_target; + let total_uncles = 0; + let now_milli_seconds = epoch_block_count * min_block_time_target; + let new_block_time_target = epoch::compute_next_block_time_target( + &config, + last_block_time_target, + 0, + now_milli_seconds, + 0, + epoch_block_count, + total_uncles + ); + assert!(new_block_time_target == min_block_time_target, 105); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.exp b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.exp new file mode 100644 index 0000000000..5c887ce87d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-38: +{ + "gas_used": 47000, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.move b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.move new file mode 100644 index 0000000000..ac3b57cf86 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/epoch/epoch_data.move @@ -0,0 +1,38 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use starcoin_framework::epoch; + + fun epoch_data() { + // default value should be consistent with genesis config + let default_block_gas_limit = 50000000 * 10; + let default_block_time_target = 10000; + let default_number = 0; + let default_start_block_number = 0; + let default_end_block_number = 24 * 2; + let default_start_time = 0; + let default_total_gas = 0; + let default_uncles = 0; + + let block_gas_limit = epoch::block_gas_limit(); + let block_time_target = epoch::block_time_target(); + let number = epoch::number(); + let start_block_number = epoch::start_block_number(); + let end_block_number = epoch::end_block_number(); + let start_time = epoch::start_time(); + let total_gas = epoch::total_gas(); + let uncles = epoch::uncles(); + + assert!(block_gas_limit == default_block_gas_limit, 101); + assert!(block_time_target == default_block_time_target, 102); + assert!(number == default_number, 103); + assert!(start_block_number == default_start_block_number, 104); + assert!(end_block_number == default_end_block_number, 105); + assert!(start_time == default_start_time, 106); + assert!(total_gas == default_total_gas, 107); + assert!(uncles == default_uncles, 108); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.exp new file mode 100644 index 0000000000..c16ab1ed53 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-17: +{ + "gas_used": 4239, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "65537" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.move new file mode 100644 index 0000000000..898070a05b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_div_zero.move @@ -0,0 +1,17 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + // A denominator of zero should cause an arithmetic error. + let f1 = fixed_point32::create_from_rational(2, 0); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(fixed_point32::get_raw_value(f1) == 999, 1); + } +} +// check: "Keep(ABORTED { code: 25863" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.exp new file mode 100644 index 0000000000..182f69a693 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 4559, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131077" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.move new file mode 100644 index 0000000000..d664b32219 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_overflow.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + // The maximum value is 2^32 - 1. Check that anything larger aborts + // with an overflow. + let f1 = fixed_point32::create_from_rational(4294967296, 1); // 2^32 + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(fixed_point32::get_raw_value(f1) == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26888" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.exp new file mode 100644 index 0000000000..793169b69e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 4591, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131077" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.move new file mode 100644 index 0000000000..7df0b0bb7d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_underflow.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + // The minimum non-zero value is 2^-32. Check that anything smaller + // aborts. + let f1 = fixed_point32::create_from_rational(1, 8589934592); // 2^-33 + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(fixed_point32::get_raw_value(f1) == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26887" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.exp new file mode 100644 index 0000000000..a44c9266a3 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-14: +{ + "gas_used": 14284, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.move new file mode 100644 index 0000000000..bfdb761078 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/create_zero.move @@ -0,0 +1,14 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let x = fixed_point32::create_from_rational(0, 1); + assert!(fixed_point32::get_raw_value(x) == 0, 0); + } +} +// check: "Keep(EXECUTED)" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.exp new file mode 100644 index 0000000000..87ed6f54d4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 6532, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "65540" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.move new file mode 100644 index 0000000000..3a52d28d0d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_by_zero.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + // Dividing by zero should cause an arithmetic error. + let f1 = fixed_point32::create_from_raw_value(0); + let fail = fixed_point32::divide_u64(1, copy f1); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(fail == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26631" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.exp new file mode 100644 index 0000000000..7f5da8cf5d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 6692, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131074" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.move new file mode 100644 index 0000000000..71fcb497ef --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow1.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let f1 = fixed_point32::create_from_raw_value(1); // 0x0.00000001 + // Divide 2^32 by the minimum fractional value. This should overflow. + let overflow = fixed_point32::divide_u64(4294967296, copy f1); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(overflow == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26120" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.exp new file mode 100644 index 0000000000..96a2f97250 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 8369, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131074" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.move new file mode 100644 index 0000000000..1a179dd9ad --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/divide_overflow2.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let f1 = fixed_point32::create_from_rational(1, 2); // 0.5 + // Divide the maximum u64 value by 0.5. This should overflow. + let overflow = fixed_point32::divide_u64(18446744073709551615, copy f1); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(overflow == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26120" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.exp new file mode 100644 index 0000000000..a853972aae --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 8273, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131075" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.move new file mode 100644 index 0000000000..a64a2518ac --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow1.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let f1 = fixed_point32::create_from_rational(3, 2); // 1.5 + // Multiply the maximum u64 value by 1.5. This should overflow. + let overflow = fixed_point32::multiply_u64(18446744073709551615, copy f1); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(overflow == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26376" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.exp new file mode 100644 index 0000000000..5ade5b8691 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.exp @@ -0,0 +1,17 @@ +processed 3 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 6596, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "fixed_point32" + } + }, + "abort_code": "131075" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.move new file mode 100644 index 0000000000..b57bf0a6f4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/multiply_overflow2.move @@ -0,0 +1,18 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let f1 = fixed_point32::create_from_raw_value(18446744073709551615); + // Multiply 2^33 by the maximum fixed-point value. This should overflow. + let overflow = fixed_point32::multiply_u64(8589934592, copy f1); + // The above should fail at runtime so that the following assertion + // is never even tested. + assert!(overflow == 999, 1); + } +} +// check: "Keep(ABORTED { code: 26376" diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.exp b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.exp new file mode 100644 index 0000000000..4ace8e4dfa --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-32: +{ + "gas_used": 42219, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.move b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.move new file mode 100644 index 0000000000..c2df9d951a --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/fixedpoint32/test.move @@ -0,0 +1,32 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# run --signers alice +script { + use std::fixed_point32; + + fun main() { + let f1 = fixed_point32::create_from_rational(3, 4); // 0.75 + let nine = fixed_point32::multiply_u64(12, copy f1); // 12 * 0.75 + assert!(nine == 9, nine); + let twelve = fixed_point32::divide_u64(9, copy f1); // 9 / 0.75 + assert!(twelve == 12, twelve); + + let f2 = fixed_point32::create_from_rational(1, 3); // 0.333... + let not_three = fixed_point32::multiply_u64(9, copy f2); // 9 * 0.333... + // multiply_u64 does NOT round -- it truncates -- so values that + // are not perfectly representable in binary may be off by one. + assert!(not_three == 2, not_three); + + // Try again with a fraction slightly larger than 1/3. + let f3 = fixed_point32::create_from_raw_value(fixed_point32::get_raw_value(copy f2) + 1); + let three = fixed_point32::multiply_u64(9, copy f3); + assert!(three == 3, three); + + // Test creating a 1.0 fraction from the maximum u64 value. + let f4 = fixed_point32::create_from_rational(18446744073709551615, 18446744073709551615); + let one = fixed_point32::get_raw_value(copy f4); + assert!(one == 4294967296, 4); // 0x1.00000000 + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.exp b/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.exp new file mode 100644 index 0000000000..9fed705b46 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.exp @@ -0,0 +1,4 @@ +processed 6 tasks + +task 5 'publish'. lines 22-30: +Publish failure: MiscellaneousError diff --git a/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.move b/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.move new file mode 100644 index 0000000000..b86d8929df --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/module_republish/cyclic_module_dependency.move @@ -0,0 +1,30 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//#publish +module alice::N { + public fun bar() {} +} + +//#publish +module bob::M { + use alice::N; + public fun foo() { + N::bar() + } +} + +// check: EXECUTED + +//# publish +module alice::N { + use bob::M; + public fun bar() { + M::foo() + } +} + +// check: "ERROR { status_code: \"CYCLIC_MODULE_DEPENDENCY\" }" \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/move/borrow_test.exp b/vm/framework/starcoin-framework/integration-tests/move/borrow_test.exp new file mode 100644 index 0000000000..076e5bd3fd --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/move/borrow_test.exp @@ -0,0 +1,19 @@ +processed 7 tasks + +task 4 'run'. lines 42-52: +{ + "gas_used": 13892, + "status": "Executed" +} + +task 5 'run'. lines 54-64: +{ + "gas_used": 15288, + "status": "Executed" +} + +task 6 'run'. lines 66-77: +{ + "gas_used": 25713, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/move/borrow_test.move b/vm/framework/starcoin-framework/integration-tests/move/borrow_test.move new file mode 100644 index 0000000000..0a8c2e1188 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/move/borrow_test.move @@ -0,0 +1,77 @@ +//# init -n dev + +//# faucet --addr alice --amount 90000 + +//# faucet --addr bob --amount 90000 + +//# publish +module alice::M { + use starcoin_framework::signer; + + struct M has key, store { + value: u64, + } + + public fun new(): M { + M { value: 1 } + } + + public fun value(this: &M): u64 { + this.value + } + + public fun save(account: &signer, m: M) { + move_to(account, m); + } + + public fun get(account: &signer): M acquires M { + move_from(signer::address_of(account)) + } + + public fun get_value(account: &signer): u64 acquires M { + let m = borrow_global(signer::address_of(account)); + m.value + } + + public fun destroy(m: M): u64 { + let M { value } = m; + value + } +} + +//# run --signers alice +script { + use alice::M; + + fun main(account: signer) { + let m = M::new(); + M::save(&account, m); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use alice::M; + + fun main(account: signer) { + let v = M::get_value(&account); + assert!(v == 1, 80001); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use alice::M; + + fun main(account: signer) { + let m = M::get(&account); + let v = M::destroy(m); + assert!(v == 1, 80001); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/move/option.exp b/vm/framework/starcoin-framework/integration-tests/move/option.exp new file mode 100644 index 0000000000..3ec3dd526b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/move/option.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-43: +{ + "gas_used": 118424, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/move/option.move b/vm/framework/starcoin-framework/integration-tests/move/option.move new file mode 100644 index 0000000000..16e62b6165 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/move/option.move @@ -0,0 +1,43 @@ +//# init -n dev + +//# faucet --addr creator + +//# run --signers creator +// Tests for the non-aborting behavior of option functions +script { + use std::option; + + fun main() { + let some = option::some(5); + let none = option::none(); + // borrow/get_with_default + assert!(*option::borrow_with_default(&some, &7) == 5, 8007); + assert!(*option::borrow_with_default(&none, &7) == 7, 8008); + assert!(option::get_with_default(&some, 7) == 5, 8009); + assert!(option::get_with_default(&none, 7) == 7, 8010); + + option::swap(&mut some, 1); + assert!(*option::borrow(&some) == 1, 8011); + + // contains/immutable borrowing + assert!(option::contains(&some, &1), 8012); + assert!(!option::contains(&none, &5), 8013); + + // destroy_with_default, destroy_some, destroy_none + assert!(option::destroy_with_default(option::none(), 4) == 4, 8014); + assert!(option::destroy_with_default(option::some(4), 5) == 4, 8015); + assert!(option::destroy_some(option::some(4)) == 4, 8016); + option::destroy_none(option::none()); + + // fill + option::fill(&mut none, 3); + let three = none; + assert!(option::is_some(&three), 8017); + assert!(*option::borrow(&three) == 3, 8018); + + // letting an option go out of scope is also ok + let _ = option::some(7); + } +} + +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/natives/bcs.exp b/vm/framework/starcoin-framework/integration-tests/natives/bcs.exp new file mode 100644 index 0000000000..a849b30010 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/bcs.exp @@ -0,0 +1,23 @@ +processed 4 tasks + +task 2 'run'. lines 5-14: +{ + "gas_used": 5763, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "from_bcs" + } + }, + "abort_code": "65537" + } + } +} + +task 3 'run'. lines 17-54: +{ + "gas_used": 26776, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/natives/bcs.move b/vm/framework/starcoin-framework/integration-tests/natives/bcs.move new file mode 100644 index 0000000000..5bb800aa1b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/bcs.move @@ -0,0 +1,54 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script { + use starcoin_std::from_bcs; + + fun main() { + let data = b"ff"; + let _addr = from_bcs::to_address(data); + } +} +// check: "Keep(ABORTED { code: 454" + + +//# run --signers alice +// Test for BCS serialization in Move + +script { + use std::bcs; + + fun main() { + // address + let addr = @0x89b9f9d1fadc027cf9532d6f99041522; + let expected_output = x"89b9f9d1fadc027cf9532d6f99041522"; + assert!(bcs::to_bytes(&addr) == expected_output, 8001); + + // bool + let b = true; + let expected_output = x"01"; + assert!(bcs::to_bytes(&b) == expected_output, 8002); + + // u8 + let i = 1u8; + let expected_output = x"01"; + assert!(bcs::to_bytes(&i) == expected_output, 8003); + + // u64 + let i = 1; + let expected_output = x"0100000000000000"; + assert!(bcs::to_bytes(&i) == expected_output, 8004); + + // u128 + let i = 1u128; + let expected_output = x"01000000000000000000000000000000"; + assert!(bcs::to_bytes(&i) == expected_output, 8005); + + // vector + let v = x"0f"; + let expected_output = x"010f"; + assert!(bcs::to_bytes(&v) == expected_output, 8006); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/natives/hash_model.exp b/vm/framework/starcoin-framework/integration-tests/natives/hash_model.exp new file mode 100644 index 0000000000..fc5a4436b2 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/hash_model.exp @@ -0,0 +1 @@ +processed 3 tasks diff --git a/vm/framework/starcoin-framework/integration-tests/natives/hash_model.move b/vm/framework/starcoin-framework/integration-tests/natives/hash_model.move new file mode 100644 index 0000000000..9e364ce3a2 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/hash_model.move @@ -0,0 +1,93 @@ +//# init -n dev + +//# faucet --addr alice + +//# publish +module alice::TestHash { + + use std::hash; + + spec module { + pragma verify = true; + } + + // ----------- + // SHA-2 Tests + // ----------- + + fun hash_test1(v1: vector, v2: vector): (vector, vector) { + let h1 = hash::sha2_256(v1); + let h2 = hash::sha2_256(v2); + (h1, h2) + } + spec hash_test1 { + aborts_if false; + ensures result_1 == result_2 ==> v1 == v2; + ensures v1 == v2 ==> result_1 == result_2; + ensures len(result_1) == 32; + // it knows result is vector + ensures len(result_1) > 0 ==> result_1[0] <= max_u8(); + } + + fun hash_test2(v1: vector, v2: vector): bool { + let h1 = hash::sha2_256(v1); + let h2 = hash::sha2_256(v2); + h1 == h2 + } + spec hash_test2 { + aborts_if false; + ensures result == (v1 == v2); + } + + fun hash_test1_incorrect(v1: vector, v2: vector): (vector, vector) { + let h1 = hash::sha2_256(v1); + let h2 = hash::sha2_256(v2); + (h1, h2) + } + spec hash_test1_incorrect { + aborts_if false; + ensures result_1 == result_2; + ensures len(result_1) > 0 ==> result_1[0] < max_u8(); // should be <= + } + + + // ----------- + // SHA-3 Tests + // ----------- + + fun hash_test3(v1: vector, v2: vector): (vector, vector) { + let h1 = hash::sha3_256(v1); + let h2 = hash::sha3_256(v2); + (h1, h2) + } + + spec hash_test3 { + aborts_if false; + ensures result_1 == result_2 ==> v1 == v2; + ensures v1 == v2 ==> result_1 == result_2; + ensures len(result_1) == 32; + // it knows result is vector + ensures len(result_1) > 0 ==> result_1[0] <= max_u8(); + } + + fun hash_test4(v1: vector, v2: vector): bool { + let h1 = hash::sha3_256(v1); + let h2 = hash::sha3_256(v2); + h1 == h2 + } + spec hash_test4 { + aborts_if false; + ensures result == (v1 == v2); + } + + fun hash_test2_incorrect(v1: vector, v2: vector): (vector, vector) { + let h1 = hash::sha2_256(v1); + let h2 = hash::sha2_256(v2); + (h1, h2) + } + spec hash_test2_incorrect { + aborts_if false; + ensures result_1 == result_2; + ensures len(result_1) > 0 ==> result_1[0] < max_u8(); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/natives/hash_test.exp b/vm/framework/starcoin-framework/integration-tests/natives/hash_test.exp new file mode 100644 index 0000000000..f393eb00b9 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/hash_test.exp @@ -0,0 +1,25 @@ +processed 6 tasks + +task 2 'run'. lines 5-18: +{ + "gas_used": 10334, + "status": "Executed" +} + +task 3 'run'. lines 21-32: +{ + "gas_used": 10463, + "status": "Executed" +} + +task 4 'run'. lines 34-45: +{ + "gas_used": 25467, + "status": "Executed" +} + +task 5 'run'. lines 47-58: +{ + "gas_used": 32891, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/natives/hash_test.move b/vm/framework/starcoin-framework/integration-tests/natives/hash_test.move new file mode 100644 index 0000000000..a0d9a80b5a --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/natives/hash_test.move @@ -0,0 +1,58 @@ +//# init -n dev + +//# faucet --addr creator + +//# run --signers creator +// Test for Hash function in Move +script { + use std::hash; + + fun test_sha2_256() { + let input = x"616263"; + let expected_output = x"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; + assert!(hash::sha2_256(input) == expected_output, 0); + } +} + +// check: gas_used +// check: 11039 + + +//# run --signers creator +script { + use starcoin_framework::hash; + + fun test_sha3_256() { + let input = x"616263"; + let expected_output = x"3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"; + assert!(hash::sha3_256(input) == expected_output, 0); + } +} +// check: gas_used +// check: 11168 + +//# run --signers creator +script { + use starcoin_std::starcoin_hash; + + fun test_keccak_256() { + let input = x"616263"; + let expected_output = x"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"; + assert!(starcoin_hash::keccak256(input) == expected_output, 0); + } +} +// check: gas_used +// check: 11168 + +//# run --signers creator +script { + use starcoin_std::starcoin_hash; + + fun test_ripemd160() { + let input = x"616263"; + let expected_output = x"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"; + assert!(starcoin_hash::ripemd160(input) == expected_output, 0); + } +} +// check: gas_used +// check: 11096 \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.exp b/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.exp new file mode 100644 index 0000000000..5724ae31a0 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.exp @@ -0,0 +1,59 @@ +processed 12 tasks + +task 4 'run'. lines 66-73: +{ + "gas_used": 49523, + "status": "Executed" +} + +task 5 'run'. lines 76-83: +{ + "gas_used": 69854, + "status": "Executed" +} + +task 6 'run'. lines 85-95: +{ + "gas_used": 85405, + "status": "Executed" +} + +task 7 'run'. lines 98-105: +{ + "gas_used": 29592, + "status": "Executed" +} + +task 8 'run'. lines 107-117: +{ + "gas_used": 20271, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "on_chain_config" + } + }, + "abort_code": "589925" + } + } +} + +task 9 'run'. lines 119-128: +{ + "gas_used": 73873, + "status": "Executed" +} + +task 10 'run'. lines 131-138: +{ + "gas_used": 61966, + "status": "Executed" +} + +task 11 'run'. lines 141-151: +{ + "gas_used": 85405, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.move b/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.move new file mode 100644 index 0000000000..3ca20dcbe3 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/config.move @@ -0,0 +1,151 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# publish +module alice::MyConfig { + use starcoin_framework::on_chain_config; + use starcoin_framework::signer; + + struct MyConfig has copy, drop, store { + version: u64, + } + + struct CapHolder has key, store { + cap: on_chain_config::ModifyConfigCapability, + } + + public fun new_config(version: u64): MyConfig { + MyConfig { + version: version, + } + } + + public fun init(account: &signer) { + assert!(signer::address_of(account) == @alice, 1000); + on_chain_config::publish_new_config(account, MyConfig { + version: 0, + }); + } + + public fun publish_new_config_with_capability(account: &signer, myconfig: MyConfig) { + assert!(signer::address_of(account) == @bob, 1000); + let cap = on_chain_config::publish_new_config_with_capability(account, myconfig); + move_to(account, CapHolder { cap: cap }); + } + + public fun extract_modify_config_capability(account: &signer) { + assert!(signer::address_of(account) == @alice, 1000); + let cap = on_chain_config::extract_modify_config_capability(account); + move_to(account, CapHolder { cap: cap }); + } + + public fun restore_modify_config_capability() acquires CapHolder { + let CapHolder { cap: cap } = move_from(@alice); + on_chain_config::restore_modify_config_capability(cap); + } + + public fun update_my_config(version: u64) acquires CapHolder { + let holder = borrow_global_mut(@alice); + on_chain_config::set_with_capability(&mut holder.cap, MyConfig { version: version }); + } + + public fun get_my_config(): u64 { + on_chain_config::get_by_address(@alice).version + } + + public fun get(): u64 { + on_chain_config::get_by_address(@alice).version + } +} + + +//# run --signers alice +script { + use alice::MyConfig; + + fun main(account: signer) { + MyConfig::init(&account); + } +} + + +//# run --signers bob +script { + use alice::MyConfig; + + fun main(account: signer) { + MyConfig::publish_new_config_with_capability(&account, MyConfig::new_config(10)); + } +} + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use alice::MyConfig; + + fun main(account: signer) { + on_chain_config::set(&account, MyConfig::new_config(2)); + assert!(MyConfig::get_my_config() == 2, 1001); + assert!(MyConfig::get() == 2, 1002); + } +} + + +//# run --signers alice +script { + use alice::MyConfig; + + fun main(account: signer) { + MyConfig::extract_modify_config_capability(&account); + } +} + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use alice::MyConfig; + + fun main(account: signer) { + on_chain_config::set(&account, MyConfig::new_config(3)); + assert!(MyConfig::get_my_config() == 3, 1002); + // assert!(MyConfig::get() == 3, 1003); + } +} + +//# run --signers bob +script { + use alice::MyConfig; + + fun main() { + MyConfig::update_my_config(4); + assert!(MyConfig::get_my_config() == 4, 1003); + assert!(MyConfig::get() == 4, 1004); + } +} + + +//# run --signers bob +script { + use alice::MyConfig; + + fun main() { + MyConfig::restore_modify_config_capability(); + } +} + + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use alice::MyConfig; + + fun main(account: signer) { + on_chain_config::set(&account, MyConfig::new_config(5)); + assert!(MyConfig::get_my_config() == 5, 1004); + assert!(MyConfig::get() == 5, 1005); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.exp b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.exp new file mode 100644 index 0000000000..5054ab18cd --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.exp @@ -0,0 +1,125 @@ +processed 11 tasks + +task 2 'run'. lines 5-65: +{ + "gas_used": 39589, + "status": "Executed" +} + +task 3 'run'. lines 67-78: +{ + "gas_used": 21474, + "status": "Executed" +} + +task 4 'run'. lines 80-110: +{ + "gas_used": 20021, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 5 'run'. lines 112-142: +{ + "gas_used": 20085, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 6 'run'. lines 144-174: +{ + "gas_used": 20053, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 7 'run'. lines 176-207: +{ + "gas_used": 20117, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 8 'run'. lines 209-240: +{ + "gas_used": 20149, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 9 'run'. lines 242-273: +{ + "gas_used": 20181, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} + +task 10 'run'. lines 275-306: +{ + "gas_used": 20245, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "consensus_config" + } + }, + "abort_code": "65554" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.move b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.move new file mode 100644 index 0000000000..3a4d296af3 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_config.move @@ -0,0 +1,306 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + // test do_compute_reward_per_block() + fun compute_reward_per_block() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + let config = consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + + assert!( + consensus_config::do_compute_reward_per_block( + &config, + base_block_time_target * 2 + ) == base_reward_per_block * 2, + 101 + ); + assert!( + consensus_config::do_compute_reward_per_block( + &config, + base_block_time_target / 2 + ) == base_reward_per_block / 2, + 102 + ); + assert!( + consensus_config::do_compute_reward_per_block( + &config, + base_block_time_target / 5 + ) == base_reward_per_block / 5, + 103 + ); + assert!( + consensus_config::do_compute_reward_per_block( + &config, + base_block_time_target / base_block_time_target + ) == base_reward_per_block / (base_block_time_target as u128), + 104 + ); + } +} + +//# run --signers alice +// test compute_reward_per_block +script { + use starcoin_framework::consensus_config; + + fun compute_reward_per_block() { + let block_time_target = 10000; // equal to default block_time_target + let default_reward_per_block = 10000000000; // should be consistent with genesis config + let reward_per_block = consensus_config::compute_reward_per_block(block_time_target); + assert!(reward_per_block == default_reward_per_block, 102); + } +} + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 0; // should large than 0 + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 0; // should large than 0 + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 0; // should large than 0 + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 0; // should large than 0 + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} +// check: "Keep(ABORTED { code: 4615" + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 0; // should large than 0 + let min_block_time_target = 5; + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} +// check: "Keep(ABORTED { code: 4615" + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 0; // should large than 0 + let max_block_time_target = 60; + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} +// check: "Keep(ABORTED { code: 4615" + +//# run --signers alice +script { + use starcoin_framework::consensus_config; + + fun incorrect_uncle_rate_target() { + let uncle_rate_target = 80; + let base_reward_per_block = 10000; + let base_block_time_target = 10; + let base_reward_per_uncle_percent = 0; + let epoch_block_count = 240; + let base_block_difficulty_window = 24; + let min_block_time_target = 5; + let max_block_time_target = 4; //max_block_time_target should large than min_block_time_target + let base_max_uncles_per_block = 2; + let base_block_gas_limit = 10000; + let strategy = 1; + + consensus_config::new_consensus_config( + uncle_rate_target, + base_block_time_target, + base_reward_per_block, + base_reward_per_uncle_percent, + epoch_block_count, + base_block_difficulty_window, + min_block_time_target, + max_block_time_target, + base_max_uncles_per_block, + base_block_gas_limit, + strategy); + } +} +// check: "Keep(ABORTED { code: 4615" diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.exp b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.exp new file mode 100644 index 0000000000..0812787fc9 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-12: +{ + "gas_used": 14753, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.move b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.move new file mode 100644 index 0000000000..fb051f750e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/consensus_strategy.move @@ -0,0 +1,12 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script { + use starcoin_framework::consensus_strategy; + + fun main() { + assert!(consensus_strategy::get() == 0, 1); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.exp b/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.exp new file mode 100644 index 0000000000..50f3245a27 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.exp @@ -0,0 +1,47 @@ +processed 14 tasks + +task 4 'run'. lines 10-21: +{ + "gas_used": 1763221, + "status": "Executed" +} + +task 5 'run'. lines 22-33: +{ + "gas_used": 21217, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "on_chain_config_dao" + } + }, + "abort_code": "393617" + } + } +} + +task 6 'run'. lines 34-49: +{ + "gas_used": 1100057, + "status": "Executed" +} + +task 8 'run'. lines 54-74: +{ + "gas_used": 1143976, + "status": "Executed" +} + +task 11 'run'. lines 81-116: +{ + "gas_used": 252843, + "status": "Executed" +} + +task 13 'run'. lines 121-139: +{ + "gas_used": 132513, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.move b/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.move new file mode 100644 index 0000000000..e6914d8aa8 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/on_chain_config/test_onchain_config_dao.move @@ -0,0 +1,140 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# block --author 0x1 --timestamp 86400000 + + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + + fun transfer_some_token_to_alice_and_bob(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + coin::transfer(&signer, @alice, balance / 4); + coin::transfer(&signer, @bob, balance / 4); + } +} +//# run --signers alice +script { + use starcoin_framework::transaction_publish_option; + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::starcoin_coin::STC; + + fun test_plugin_fail(account: signer) { + on_chain_config_dao::plugin>( + &account + ); //ERR_NOT_AUTHORIZED + } +} +//# run --signers alice +script { + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::transaction_publish_option; + use starcoin_framework::starcoin_coin::STC; + + fun propose(signer: signer) { + let new_config = transaction_publish_option::new_transaction_publish_option(true, false); + on_chain_config_dao::propose_update( + &signer, + new_config, + 0 + ); + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 87000000 + +//# run --signers bob +script { + use starcoin_framework::transaction_publish_option; + use starcoin_framework::coin; + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::signer; + use starcoin_framework::dao; + + fun vote(signer: signer) { + let balance = coin::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote>( + &signer, + @alice, + 0, + balance, + true + ); + } +} + + +//# block --author 0x1 --timestamp 110000000 + +//# block --author 0x1 --timestamp 120000000 + +//# run --signers bob +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::transaction_publish_option; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun queue_proposal(signer: signer) { + let state = dao::proposal_state>( + @alice, + 0 + ); + assert!(state == 4, (state as u64)); + { + let token = dao::unstake_votes>( + &signer, + @alice, + 0 + ); + coin::deposit(signer::address_of(&signer), token); + }; + dao::queue_proposal_action>( + @alice, + 0 + ); + // ModifyDaoConfigProposal::execute(@alice, 0); + let state = dao::proposal_state>( + @alice, + 0 + ); + assert!(state == 5, (state as u64)); + } +} +// check: EXECUTED + + +//# block --author 0x1 --timestamp 130000000 + +//# run --signers bob +script { + use starcoin_framework::transaction_publish_option; + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao; + + fun execute_proposal_action(_signer: signer) { + let state = dao::proposal_state>( + @alice, + 0 + ); + assert!(state == 6, (state as u64)); + on_chain_config_dao::execute(@alice, 0); + assert!(!transaction_publish_option::is_module_allowed(@starcoin_framework), 401); + assert!(transaction_publish_option::is_script_allowed(@starcoin_framework), 402); + } +} +// check: EXECUTED + diff --git a/vm/framework/starcoin-framework/integration-tests/oracle/oracle.exp b/vm/framework/starcoin-framework/integration-tests/oracle/oracle.exp new file mode 100644 index 0000000000..cab6b9e5fb --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/oracle/oracle.exp @@ -0,0 +1,103 @@ +processed 20 tasks + +task 5 'run'. lines 13-22: +{ + "gas_used": 56358, + "status": "Executed" +} + +task 6 'run'. lines 26-36: +{ + "gas_used": 56358, + "status": "Executed" +} + +task 7 'run'. lines 39-51: +{ + "gas_used": 24391, + "status": "Executed" +} + +task 8 'run'. lines 53-66: +{ + "gas_used": 30000, + "status": "Executed" +} + +task 10 'run'. lines 71-79: +{ + "gas_used": 74102, + "status": "Executed" +} + +task 11 'run'. lines 82-91: +{ + "gas_used": 16602, + "status": "Executed" +} + +task 13 'run'. lines 96-114: +{ + "gas_used": 41317, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "oracle_aggregator" + } + }, + "abort_code": "196709" + } + } +} + +task 14 'run'. lines 117-132: +{ + "gas_used": 41317, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "oracle_aggregator" + } + }, + "abort_code": "196709" + } + } +} + +task 16 'run'. lines 162-172: +{ + "gas_used": 17597, + "status": "Executed" +} + +task 17 'run'. lines 175-185: +{ + "gas_used": 20202, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "oracle" + } + }, + "abort_code": "589926" + } + } +} + +task 18 'run'. lines 187-197: +{ + "gas_used": 75457, + "status": "Executed" +} + +task 19 'run'. lines 199-209: +{ + "gas_used": 12201, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/oracle/oracle.move b/vm/framework/starcoin-framework/integration-tests/oracle/oracle.move new file mode 100644 index 0000000000..7d2e9efa20 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/oracle/oracle.move @@ -0,0 +1,209 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# faucet --addr ds1 + +//# faucet --addr ds2 + + + +//# run --signers ds1 +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun main(account: signer) { + oracle_price::init_data_source(&account, 100000); + } +} +// check: EXECUTED + + + +//# run --signers ds2 +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun main(account: signer) { + oracle_price::init_data_source(&account, 110000); + } +} + +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::{STCUSD}; + + fun test_is_data_source_initiailzed(_signer: signer) { + assert!(!oracle_price::is_data_source_initialized(@alice), 997); + assert!(oracle_price::is_data_source_initialized(@ds1), 998); + assert!(oracle_price::is_data_source_initialized(@ds2), 999); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::{Self, STCUSD}; + + fun test_read_price(_signer: signer) { + assert!(oracle_price::read(@ds1) == 100000, 1000); + assert!(oracle_stc_usd::read(@ds1) == 100000, 2000); + assert!(oracle_price::read(@ds2) == 110000, 1001); + assert!(oracle_stc_usd::read(@ds2) == 110000, 2001); + } +} + +// check: EXECUTED + + +//# block --author 0x1 + +//# run --signers ds1 +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun update_price(account: signer) { + oracle_price::update(&account, 200000); + } +} + + +//# run --signers alice +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun test_read_price(_signer: signer) { + assert!(oracle_price::read(@ds1) == 200000, 1002); + assert!(oracle_price::read(@ds2) == 110000, 1003); + } +} + + +//# block --author 0x1 + +//# run --signers alice +script { + use starcoin_framework::oracle_aggregator; + use starcoin_framework::oracle_stc_usd::STCUSD; + use std::vector; + + fun test_aggregator_price(_signer: signer) { + let ds = vector::empty(); + vector::push_back(&mut ds, @ds1); + vector::push_back(&mut ds, @ds2); + assert!( + oracle_aggregator::latest_price_average_aggregator(&ds, 2000) == ((200000 + 110000) / 2), + 1004 + ); + assert!(oracle_aggregator::latest_price_average_aggregator(&ds, 1000) == 200000, 1005); + } +} + +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::oracle_aggregator; + use starcoin_framework::oracle_stc_usd::STCUSD; + use std::vector; + + fun test_aggregator_price(_signer: signer) { + let ds = vector::empty(); + vector::push_back(&mut ds, @ds1); + vector::push_back(&mut ds, @ds2); + oracle_aggregator::latest_price_average_aggregator(&ds, 100); + } +} + +// abort OracleAggregator::ERR_NO_PRICE_DATA_AVIABLE +// check: "Keep(ABORTED { code: 25857" + +//# publish +module bob::DelegateOracleDS { + use starcoin_framework::oracle::{Self, UpdateCapability}; + use starcoin_framework::oracle_price; + + struct DelegateUpdateOracleCapability has key { + cap: UpdateCapability, + } + + public fun delegate(account: &signer) { + let cap = oracle::remove_update_capability(account); + move_to(account, DelegateUpdateOracleCapability { + cap, + }); + } + + //any one can update the ds in ds_addr + public fun update_price_any( + ds_addr: address, + price: u128 + ) acquires DelegateUpdateOracleCapability { + let cap = borrow_global_mut>(ds_addr); + oracle_price::update_with_cap(&mut cap.cap, price); + } +} + +// check: EXECUTED + +//# run --signers ds2 +script { + use bob::DelegateOracleDS; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun main(account: signer) { + DelegateOracleDS::delegate(&account); + } +} + +// check: EXECUTED + + +//# run --signers ds2 +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun update_price(account: signer) { + oracle_price::update(&account, 0); + } +} +// ds2 can not update ds2 datasource price +// check: ABORTED + +//# run --signers alice +script { + use starcoin_framework::oracle_stc_usd::STCUSD; + use bob::DelegateOracleDS; + + fun update_price(_signer: signer) { + DelegateOracleDS::update_price_any(@ds2, 0); + } +} +// alice can update ds2 datasource price by DelegateOracleDS +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::oracle_price; + use starcoin_framework::oracle_stc_usd::STCUSD; + + fun test_read_price(_signer: signer) { + assert!(oracle_price::read(@ds2) == 0, 1004); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.exp b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.exp new file mode 100644 index 0000000000..8cae80ddc5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.exp @@ -0,0 +1,41 @@ +processed 8 tasks + +task 3 'run'. lines 8-20: +{ + "gas_used": 22875, + "status": "Executed" +} + +task 4 'run'. lines 22-41: +{ + "gas_used": 111871, + "status": "Executed" +} + +task 5 'run'. lines 42-53: +{ + "gas_used": 24541, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "stc_transaction_package_validation" + } + }, + "abort_code": "65638" + } + } +} + +task 6 'run'. lines 55-65: +{ + "gas_used": 40248, + "status": "Executed" +} + +task 7 'run'. lines 67-76: +{ + "gas_used": 26374, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.move b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.move new file mode 100644 index 0000000000..3bd371b5cd --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/cancel_upgrade_plan.move @@ -0,0 +1,76 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# run --signers alice +// default upgrade strategy is arbitrary +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use std::option; + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::stc_version; + use starcoin_framework::on_chain_config; + + fun main(account: signer) { + on_chain_config::publish_new_config(&account, stc_version::new_version(1)); + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_two_phase(), + option::some(0) + ); + } +} + +// check: EXECUTED + +// two phase upgrade need to submit upgrade plan first. +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: ABORTED + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::submit_upgrade_plan_v2(&account, copy hash, 1, false); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + stc_transaction_package_validation::cancel_upgrade_plan(&account); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.exp b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.exp new file mode 100644 index 0000000000..e9ac499f8a --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.exp @@ -0,0 +1,37 @@ +processed 12 tasks + +task 3 'run'. lines 7-18: +{ + "gas_used": 22875, + "status": "Executed" +} + +task 4 'run'. lines 20-37: +{ + "gas_used": 111871, + "status": "Executed" +} + +task 5 'run'. lines 39-49: +{ + "gas_used": 40248, + "status": "Executed" +} + +task 7 'run'. lines 53-63: +{ + "gas_used": 40248, + "status": "Executed" +} + +task 9 'run'. lines 67-83: +{ + "gas_used": 38153, + "status": "Executed" +} + +task 11 'run'. lines 88-99: +{ + "gas_used": 38153, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.move b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.move new file mode 100644 index 0000000000..cbdd269079 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/override_upgrade_plan.move @@ -0,0 +1,99 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use starcoin_framework::stc_version; + use starcoin_framework::stc_transaction_package_validation; + use std::option; + + fun main(account: signer) { + on_chain_config::publish_new_config(&account, stc_version::new_version(1)); + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_two_phase(), + option::some(3) + ); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::submit_upgrade_plan_v2(&account, copy hash, 1, false); + } +} + +// check: EXECUTED + +//# block --author bob + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + let hash = x"2222222222222222"; + stc_transaction_package_validation::submit_upgrade_plan_v2(&account, copy hash, 2, false); + } +} + +// check: EXECUTED + +//# block --author bob + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"2222222222222222"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: ABORTED + +//! block-prologue +//! author: bob +//! block-time: 4 +//! block-number: 3 + +//# block --author bob + + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"2222222222222222"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.exp b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.exp new file mode 100644 index 0000000000..48c453a574 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.exp @@ -0,0 +1,107 @@ +processed 15 tasks + +task 3 'run'. lines 8-20: +{ + "gas_used": 22875, + "status": "Executed" +} + +task 4 'run'. lines 22-41: +{ + "gas_used": 111871, + "status": "Executed" +} + +task 5 'run'. lines 42-53: +{ + "gas_used": 24541, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "stc_transaction_package_validation" + } + }, + "abort_code": "65638" + } + } +} + +task 6 'run'. lines 55-67: +{ + "gas_used": 40248, + "status": "Executed" +} + +task 7 'run'. lines 68-79: +{ + "gas_used": 36746, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "stc_transaction_package_validation" + } + }, + "abort_code": "65640" + } + } +} + +task 9 'run'. lines 83-96: +{ + "gas_used": 38153, + "status": "Executed" +} + +task 10 'run'. lines 97-108: +{ + "gas_used": 26374, + "status": "Executed" +} + +task 11 'run'. lines 109-118: +{ + "gas_used": 23118, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "stc_transaction_package_validation" + } + }, + "abort_code": "196710" + } + } +} + +task 12 'run'. lines 120-134: +{ + "gas_used": 26105, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "stc_transaction_package_validation" + } + }, + "abort_code": "65642" + } + } +} + +task 13 'run'. lines 136-150: +{ + "gas_used": 116316, + "status": "Executed" +} + +task 14 'run'. lines 153-166: +{ + "gas_used": 28642, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.move b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.move new file mode 100644 index 0000000000..e4caa50669 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/package_txn_manager/package_txn_manager.move @@ -0,0 +1,167 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# faucet --addr bob + + +//# run --signers alice +// default upgrade strategy is arbitrary +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use std::option; + use starcoin_framework::stc_version; + use starcoin_framework::on_chain_config; + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + on_chain_config::publish_new_config(&account, stc_version::new_version(1)); + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_two_phase(), + option::some(2) + ); + } +} + +// check: EXECUTED + +// two phase upgrade need to submit upgrade plan first. +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: ABORTED + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::submit_upgrade_plan_v2(&account, copy hash, 1, false); + } +} + +// check: EXECUTED + +// package txn must wait after plan's active_after_number +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: ABORTED + +//# block --author bob --timestamp 100000000000 + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use starcoin_framework::signer; + + fun main(account: signer) { + let hash = x"1111111111111111"; + stc_transaction_package_validation::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED + +// cancel the upgrade plan +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + stc_transaction_package_validation::cancel_upgrade_plan(&account); + } +} + +// check: EXECUTED + +// cancel a none plan will report EUPGRADE_PLAN_IS_NONE +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + + fun main(account: signer) { + stc_transaction_package_validation::cancel_upgrade_plan(&account); + } +} + +// check: "Keep(ABORTED { code: 26113" + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use std::option; + + fun main(account: signer) { + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_arbitrary(), + option::some(0) + ); + } +} + +// check: "Keep(ABORTED { code: 27143" + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use std::option; + + fun main(account: signer) { + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_new_module(), + option::some(0) + ); + } +} + +// check: EXECUTED + + +//# run --signers alice +script { + use starcoin_framework::stc_transaction_package_validation; + use std::option; + + fun main(account: signer) { + stc_transaction_package_validation::update_module_upgrade_strategy( + &account, + stc_transaction_package_validation::get_strategy_freeze(), + option::some(0) + ); + } +} +// check: EXECUTED + diff --git a/vm/framework/starcoin-framework/integration-tests/resource/resource_example.exp b/vm/framework/starcoin-framework/integration-tests/resource/resource_example.exp new file mode 100644 index 0000000000..07a1542d61 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/resource/resource_example.exp @@ -0,0 +1,13 @@ +processed 6 tasks + +task 4 'run'. lines 34-42: +{ + "gas_used": 12472, + "status": "Executed" +} + +task 5 'run'. lines 44-53: +{ + "gas_used": 21748, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/resource/resource_example.move b/vm/framework/starcoin-framework/integration-tests/resource/resource_example.move new file mode 100644 index 0000000000..439c6d4735 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/resource/resource_example.move @@ -0,0 +1,53 @@ +//# init -n dev + +//# faucet --addr alice --amount 9000000 + +//# faucet --addr bob + + +//# publish +module alice::Example { + use starcoin_framework::signer; + + public fun new(): R { + R { x: true } + } + + public fun destroy(r: R) { + R { x: _ } = r; + } + + struct R has key, store { x: bool } + + public fun save(account: &signer, r: R) { + move_to(account, r); + } + + public fun get_x(account: &signer): bool acquires R { + let sender = signer::address_of(account); + assert!(exists(sender), 1); + let r = borrow_global(sender); + r.x + } +} + +//# run --signers alice +script { + use alice::Example; + + fun main() { + let r = Example::new(); + Example::destroy(r); + } +} + +//# run --signers bob +script { + use alice::Example; + + fun main(account: signer) { + let r = Example::new(); + Example::save(&account, r); + assert!(Example::get_x(&account), 1); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.exp b/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.exp new file mode 100644 index 0000000000..7196150c29 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.exp @@ -0,0 +1,51 @@ +processed 9 tasks + +task 4 'run'. lines 54-69: +{ + "gas_used": 1373436, + "status": "Executed" +} + +task 5 'run'. lines 72-97: +{ + "gas_used": 1443928, + "status": "Executed" +} + +task 6 'run'. lines 100-123: +{ + "gas_used": 1206586, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "coin" + } + }, + "abort_code": "65542" + } + } +} + +task 7 'run'. lines 126-140: +{ + "gas_used": 1317275, + "status": "Executed" +} + +task 8 'run'. lines 142-167: +{ + "gas_used": 516643, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "coin" + } + }, + "abort_code": "65543" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.move b/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.move new file mode 100644 index 0000000000..3fb4e4ff73 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/burn_and_destroy.move @@ -0,0 +1,167 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# publish +module alice::fake_money { + use std::signer; + use std::string; + + use starcoin_framework::coin; + + struct FakeMoney {} + + struct FakeMoneyCapabilities has key { + burn_cap: coin::BurnCapability, + freeze_cap: coin::FreezeCapability, + mint_cap: coin::MintCapability, + } + + public fun init(account: &signer, decimal: u8) { + let ( + burn_cap, + freeze_cap, + mint_cap + ) = coin::initialize( + account, + string::utf8(b"FakeMoney"), + string::utf8(b"FakeMoney"), + decimal, + true, + ); + coin::register(account); + move_to(account, FakeMoneyCapabilities { + burn_cap, + freeze_cap, + mint_cap, + }) + } + + public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { + let cap = borrow_global(signer::address_of(account)); + coin::mint(amount, &cap.mint_cap) + } + + public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { + let cap = borrow_global(@alice); + coin::burn(coin, &cap.burn_cap) + } +} + +//# run --signers alice +script { + use std::option; + use alice::fake_money::{Self, FakeMoney}; + use starcoin_framework::coin; + + fun init_and_register(account: signer) { + fake_money::init(&account, 9); + + let market_cap = coin::supply(); + assert!(option::destroy_some(market_cap) == 0, 8001); + assert!(coin::is_account_registered(@alice), 8002); + // Create 'Balance' resource under sender account, and init with zero + coin::register(&account); + } +} + + +//# run --signers alice +script { + use std::option; + use std::signer; + use starcoin_framework::coin; + use alice::fake_money::{Self, FakeMoney}; + + fun test_supply(account: signer) { + // mint 100 coins and check that the market cap increases appropriately + let old_market_supply = option::destroy_some(coin::supply()); + let coin = fake_money::mint(&account, 10000); + assert!(coin::value(&coin) == 10000, 8002); + assert!(option::destroy_some(coin::supply()) == old_market_supply + 10000, 8003); + coin::deposit(signer::address_of(&account), coin); + } +} + +// //# run --signers alice +// script { +// use starcoin_framework::coin; +// use alice::fake_money::{FakeMoney}; +// +// fun test_withdraw_and_burn(account: signer) { +// // Token::add_burn_capability(&account, cap); +// } +// } + + +//# run --signers alice +script { + use std::option; + use std::string; + use starcoin_framework::coin; + use alice::fake_money::{FakeMoney, Self}; + use starcoin_std::debug; + + fun test_withdraw_and_burn(account: signer) { + let supply = option::destroy_some(coin::supply()); + + assert!(supply == 10000, 8004); + + debug::print(&string::utf8(b"test_withdraw_and_burn | 1")); + let token = coin::withdraw(&account, 10000); + debug::print(&string::utf8(b"test_withdraw_and_burn | 2")); + let t1 = coin::extract(&mut token, 100); + debug::print(&string::utf8(b"test_withdraw_and_burn | 3")); + let t2 = coin::extract(&mut token, 10000); // amount is not enough + fake_money::burn(token); + fake_money::burn(t1); + fake_money::burn(t2); + } +} + + +//# run --signers alice +script { + use std::option; + use alice::fake_money::{FakeMoney, Self}; + use starcoin_framework::coin; + + fun test_mint_and_burn(account: signer) { + let old_market_cap = option::destroy_some(coin::supply()); + let amount = 100; + let coin = fake_money::mint(&account, amount); + assert!(coin::value(&coin) == amount, 8008); + assert!(option::destroy_some(coin::supply()) == (old_market_cap + (amount as u128)), 8009); + fake_money::burn(coin); + } +} + +//# run --signers alice +script { + use alice::fake_money::{FakeMoney}; + use starcoin_framework::coin; + + fun test_withdraw_and_burn(account: signer) { + let zero = coin::withdraw(&account, 0); + coin::destroy_zero(zero); + let token = coin::withdraw(&account, 10); //EDESTROY_TOKEN_NON_ZERO + coin::destroy_zero(token); + } +} + +// //# run --signers alice +// script { +// use starcoin_framework::Token; +// use alice::fake_money::{FakeMoney}; +// use starcoin_framework::coin; +// +// fun test_withdraw_and_burn(account: signer) { +// let burn_cap = coin::remove_burn_capability(&account); +// coin::destroy_burn_capability(burn_cap); +// let mint_cap = Token::remove_mint_capability(&account); +// Token::destroy_mint_capability(mint_cap); +// } +// } diff --git a/vm/framework/starcoin-framework/integration-tests/token/is_same_token.exp b/vm/framework/starcoin-framework/integration-tests/token/is_same_token.exp new file mode 100644 index 0000000000..6851073428 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/is_same_token.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 11-23: +{ + "gas_used": 28334, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/is_same_token.move b/vm/framework/starcoin-framework/integration-tests/token/is_same_token.move new file mode 100644 index 0000000000..b39c97d07b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/is_same_token.move @@ -0,0 +1,23 @@ +//# init -n dev + +//# faucet --addr alice + + +//# publish +module alice::fake_money { + struct FakeMoney {} +} + +//# run --signers alice +script { + use starcoin_framework::stc_util; + use starcoin_framework::starcoin_coin::{STC}; + use alice::fake_money; + + fun main() { + assert!(stc_util::is_stc(), 1); + assert!(!stc_util::is_stc(), 2); + } +} + +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/integration-tests/token/join_and_split.exp b/vm/framework/starcoin-framework/integration-tests/token/join_and_split.exp new file mode 100644 index 0000000000..8042ca1571 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/join_and_split.exp @@ -0,0 +1,13 @@ +processed 6 tasks + +task 4 'run'. lines 54-72: +{ + "gas_used": 1382608, + "status": "Executed" +} + +task 5 'run'. lines 73-92: +{ + "gas_used": 377361, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/join_and_split.move b/vm/framework/starcoin-framework/integration-tests/token/join_and_split.move new file mode 100644 index 0000000000..2d24b2fdf0 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/join_and_split.move @@ -0,0 +1,92 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# publish +module alice::fake_money { + use std::signer; + use std::string; + + use starcoin_framework::coin; + + struct FakeMoney {} + + struct FakeMoneyCapabilities has key { + burn_cap: coin::BurnCapability, + freeze_cap: coin::FreezeCapability, + mint_cap: coin::MintCapability, + } + + public fun init(account: &signer, decimal: u8) { + let ( + burn_cap, + freeze_cap, + mint_cap + ) = coin::initialize( + account, + string::utf8(b"FakeMoney"), + string::utf8(b"FakeMoney"), + decimal, + true, + ); + coin::register(account); + move_to(account, FakeMoneyCapabilities { + burn_cap, + freeze_cap, + mint_cap, + }) + } + + public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { + let cap = borrow_global(signer::address_of(account)); + coin::mint(amount, &cap.mint_cap) + } + + public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { + let cap = borrow_global(@alice); + coin::burn(coin, &cap.burn_cap) + } +} +// check: EXECUTED + +//# run --signers alice +script { + use std::option; + use alice::fake_money::{Self, FakeMoney}; + use starcoin_framework::coin; + + fun main(account: signer) { + fake_money::init(&account, 9); + + let supply = option::destroy_some(coin::supply()); + assert!(supply == 0, 8001); + assert!(coin::is_account_registered(@alice), 8002); + // Create 'Balance' resource under sender account, and init with zero + // account::do_accept_token(&account); + } +} +// check: EXECUTED + +// split and join +//# run --signers alice +script { + use std::signer; + use alice::fake_money::{Self, FakeMoney}; + use starcoin_framework::coin; + + fun main(account: signer) { + let coin = fake_money::mint(&account, 10000); + assert!(coin::value(&coin) == 10000, 8002); + let coin1 = coin::extract(&mut coin, 5000); + let coin2 = coin::extract(&mut coin, 5000); + assert!(coin::value(&coin1) == 5000, 8003); + assert!(coin::value(&coin2) == 5000, 8004); + coin::merge(&mut coin1, coin2); + coin::deposit(signer::address_of(&account), coin1); + fake_money::burn(coin); + } +} + +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/token/my_token.exp b/vm/framework/starcoin-framework/integration-tests/token/my_token.exp new file mode 100644 index 0000000000..d08a5a3ada --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/my_token.exp @@ -0,0 +1,31 @@ +processed 10 tasks + +task 5 'run'. lines 71-87: +{ + "gas_used": 1441522, + "status": "Executed" +} + +task 6 'run'. lines 89-105: +{ + "gas_used": 1523080, + "status": "Executed" +} + +task 7 'run'. lines 107-115: +{ + "gas_used": 807342, + "status": "Executed" +} + +task 8 'run'. lines 118-126: +{ + "gas_used": 584804, + "status": "Executed" +} + +task 9 'run'. lines 128-138: +{ + "gas_used": 363645, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/my_token.move b/vm/framework/starcoin-framework/integration-tests/token/my_token.move new file mode 100644 index 0000000000..be4c07689a --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/my_token.move @@ -0,0 +1,138 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + +//# publish +module alice::fake_money { + use std::signer; + use std::string; + + use starcoin_framework::coin; + + struct FakeMoney {} + + struct FakeMoneyCapabilities has key { + burn_cap: coin::BurnCapability, + freeze_cap: coin::FreezeCapability, + mint_cap: coin::MintCapability, + } + + public fun init(account: &signer, decimal: u8) { + let ( + burn_cap, + freeze_cap, + mint_cap + ) = coin::initialize( + account, + string::utf8(b"FakeMoney"), + string::utf8(b"FakeMoney"), + decimal, + true, + ); + coin::register(account); + move_to(account, FakeMoneyCapabilities { + burn_cap, + freeze_cap, + mint_cap, + }) + } + + public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { + let cap = borrow_global(signer::address_of(account)); + coin::mint(amount, &cap.mint_cap) + } + + public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { + let cap = borrow_global(@alice); + coin::burn(coin, &cap.burn_cap) + } +} +// check: EXECUTED + +//# publish +module bob::HideToken { + use alice::fake_money::FakeMoney; + + use starcoin_framework::coin; + + struct Collection has key, store { + t: coin::Coin, + } + + public fun hide(account: &signer, coin: coin::Coin) { + let b = Collection { t: coin }; + move_to(account, b); + } +} + + +//# run --signers alice +script { + use std::option; + use alice::fake_money::{Self, FakeMoney}; + use starcoin_framework::coin; + + fun main(account: signer) { + fake_money::init(&account, 9); + + let market_cap = option::destroy_some(coin::supply()); + assert!(market_cap == 0, 8001); + assert!(coin::is_account_registered(@alice), 8002); + // Create 'Balance' resource under sender account, and init with zero + coin::register(&account); + } +} +// check: EXECUTED + +//# run --signers alice +script { + use std::option; + use std::signer; + use starcoin_framework::coin; + use alice::fake_money::{FakeMoney, Self}; + + fun main(account: signer) { + // mint 100 coins and check that the market cap increases appropriately + let old_market_cap = option::destroy_some(coin::supply()); + let coin = fake_money::mint(&account, 10000); + assert!(coin::value(&coin) == 10000, 8002); + assert!(option::destroy_some(coin::supply()) == old_market_cap + 10000, 8003); + coin::deposit(signer::address_of(&account), coin); + } +} +// check: EXECUTED + +//# run --signers bob +script { + use alice::fake_money::FakeMoney; + use starcoin_framework::coin; + + fun main(account: signer) { + coin::register(&account); + } +} + + +//# run --signers alice +script { + use alice::fake_money::FakeMoney; + use starcoin_framework::coin; + + fun main(account: signer) { + coin::transfer(&account, @bob, 10); + } +} + +//# run --signers bob +script { + use alice::fake_money::FakeMoney; + use bob::HideToken; + use starcoin_framework::coin; + + fun main(account: signer) { + let token = coin::withdraw(&account, 10); + HideToken::hide(&account, token); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.exp b/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.exp new file mode 100644 index 0000000000..338501451f --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.exp @@ -0,0 +1,29 @@ +processed 7 tasks + +task 4 'run'. lines 55-63: +{ + "gas_used": 55290, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "coin" + } + }, + "abort_code": "65565" + } + } +} + +task 5 'run'. lines 65-73: +{ + "gas_used": 886368, + "status": "Executed" +} + +task 6 'run'. lines 75-88: +{ + "gas_used": 50734, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.move b/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.move new file mode 100644 index 0000000000..c78c22e2e2 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/scaling_factor.move @@ -0,0 +1,88 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# publish +module alice::fake_money { + use std::signer; + use std::string; + + use starcoin_framework::coin; + + struct FakeMoney {} + + struct FakeMoneyCapabilities has key { + burn_cap: coin::BurnCapability, + freeze_cap: coin::FreezeCapability, + mint_cap: coin::MintCapability, + } + + public fun init(account: &signer, decimal: u8) { + let ( + burn_cap, + freeze_cap, + mint_cap + ) = coin::initialize( + account, + string::utf8(b"FakeMoney"), + string::utf8(b"FakeMoney"), + decimal, + true, + ); + coin::register(account); + move_to(account, FakeMoneyCapabilities { + burn_cap, + freeze_cap, + mint_cap, + }) + } + + public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { + let cap = borrow_global(signer::address_of(account)); + coin::mint(amount, &cap.mint_cap) + } + + public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { + let cap = borrow_global(@alice); + coin::burn(coin, &cap.burn_cap) + } +} +// check: EXECUTED + +//# run --signers alice +script { + use alice::fake_money; + + fun main(account: signer) { + fake_money::init(&account, 39); // ECOIN_COIN_DECIMAL_TOO_LARGE + } +} +// check: "Keep(ABORTED { code: 65565" + +//# run --signers alice +script { + use alice::fake_money; + + fun main(account: signer) { + fake_money::init(&account, 3); + } +} +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::coin; + use alice::fake_money::{FakeMoney}; + use starcoin_std::debug; + + fun check_decimal(_account: signer) { + let decimal = coin::decimals(); + debug::print(&std::string::utf8(b"scaling_factor.move - check_decimal")); + debug::print(&decimal); + assert!(decimal == 3, 101); + } +} +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/token/simple_token.exp b/vm/framework/starcoin-framework/integration-tests/token/simple_token.exp new file mode 100644 index 0000000000..b134af1aea --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/simple_token.exp @@ -0,0 +1 @@ +processed 5 tasks diff --git a/vm/framework/starcoin-framework/integration-tests/token/simple_token.move b/vm/framework/starcoin-framework/integration-tests/token/simple_token.move new file mode 100644 index 0000000000..969b757efc --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/simple_token.move @@ -0,0 +1,79 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# publish +module alice::Token { + + struct Coin has key, store { + type: AssetType, + value: u64, + } + + // control the minting/creation in the defining module of `ATy` + public fun create(type: ATy, value: u64): Coin { + Coin { type, value } + } + + public fun value(coin: &Coin): u64 { + coin.value + } + + public fun split(coin: Coin, amount: u64): (Coin, Coin) { + let other = withdraw(&mut coin, amount); + (coin, other) + } + + public fun withdraw(coin: &mut Coin, amount: u64): Coin { + assert!(coin.value >= amount, 10); + coin.value = coin.value - amount; + Coin { type: *&coin.type, value: amount } + } + + public fun join(coin1: Coin, coin2: Coin): Coin { + deposit(&mut coin1, coin2); + coin1 + } + + public fun deposit(coin: &mut Coin, check: Coin) { + let Coin { value, type } = check; + assert!(&coin.type == &type, 42); + coin.value = coin.value + value; + } + + public fun destroy_zero(coin: Coin) { + let Coin { value, type: _ } = coin; + assert!(value == 0, 11) + } + +} + +//# publish +module bob::ToddNickles { + use alice::Token; + use starcoin_framework::signer; + + struct T has copy, drop, store {} + + struct Wallet has key, store { + nickles: Token::Coin, + } + + public fun init(account: signer) { + assert!(signer::address_of(&account) == @bob, 42); + move_to(&account, Wallet { nickles: Token::create(T{}, 0) }) + } + + public fun mint(account: signer): Token::Coin { + assert!(signer::address_of(&account) == @bob, 42); + Token::create(T{}, 5) + } + + public fun destroy(c: Token::Coin) acquires Wallet { + Token::deposit(&mut borrow_global_mut(@bob).nickles, c) + } + +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.exp b/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.exp new file mode 100644 index 0000000000..6e2b111a83 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.exp @@ -0,0 +1,18 @@ +processed 5 tasks + +task 3 'run'. lines 7-18: +{ + "gas_used": 564057, + "status": { + "MoveAbort": { + "location": "Script", + "abort_code": "41" + } + } +} + +task 4 'run'. lines 20-30: +{ + "gas_used": 713171, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.move b/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.move new file mode 100644 index 0000000000..bbd7a0e84c --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/token/transfer_then_abort.move @@ -0,0 +1,29 @@ +//# init -n dev + +//# faucet --addr alice --amount 1000000 + +//# faucet --addr bob --amount 1000000 + +//# run --signers alice +script { + use starcoin_framework::coin; + + fun main(account: signer) { + coin::transfer(&account, @bob, 10); + abort 41 + } +} +// txn is kept +// check: ABORTED +// check: 41 + +//# run --signers bob +script { + use starcoin_framework::coin; + + fun main() { + // check the state is unchanged + assert!(coin::balance(@bob) == 1000000, 42); + } +} +// check: EXECUTED \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.exp b/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.exp new file mode 100644 index 0000000000..29832333a4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.exp @@ -0,0 +1,29 @@ +processed 7 tasks + +task 4 'run'. lines 9-20: +{ + "gas_used": 340522, + "status": "Executed" +} + +task 5 'run'. lines 23-37: +{ + "gas_used": 283040, + "status": "Executed" +} + +task 6 'run'. lines 40-53: +{ + "gas_used": 36434, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "system_addresses" + } + }, + "abort_code": "327683" + } + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.move b/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.move new file mode 100644 index 0000000000..3b8d4246c2 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_fee/distribute_txn_fee.move @@ -0,0 +1,54 @@ +//# init -n dev + +//# faucet --addr Genesis + +//# faucet --addr alice + +//# faucet --addr bob + +//# run --signers bob +script { + use starcoin_framework::stc_transaction_fee; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::coin; + + fun pay_fees(account: signer) { + let coin = coin::withdraw(&account, 200); + assert!(coin::value(&coin) == 200, 8001); + stc_transaction_fee::pay_fee(coin); + } +} + + +//# run --signers Genesis +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::{STC}; + use starcoin_framework::stc_transaction_fee; + + fun distribute_fees(account: signer) { + let coin = stc_transaction_fee::distribute_transaction_fees(&account); + let value = coin::value(&coin); + assert!(value >= 200, 10000); + coin::deposit(signer::address_of(&account), coin); + } +} +// check: EXECUTED + + +//# run --signers alice +script { + use std::signer; + use starcoin_framework::stc_transaction_fee; + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::{STC}; + + fun main(account: signer) { + let coin = stc_transaction_fee::distribute_transaction_fees(&account); + coin::deposit(signer::address_of(&account), coin); + } +} + +// check: ABORTED + diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.exp new file mode 100644 index 0000000000..8dfc5b4f4b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 8-16: +{ + "gas_used": 66735, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.move new file mode 100644 index 0000000000..52db6ac6e7 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/accept_token.move @@ -0,0 +1,16 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# run --signers bob +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::coin; + + fun main(account: signer) { + coin::register(&account); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.exp new file mode 100644 index 0000000000..5069286b1c --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.exp @@ -0,0 +1,7 @@ +processed 4 tasks + +task 3 'run'. lines 8-19: +{ + "gas_used": 1692301, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.move new file mode 100644 index 0000000000..9e75071bfe --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/create_account.move @@ -0,0 +1,19 @@ +//# init -n dev + +//# faucet --addr alice + +//# faucet --addr bob + + +//# run --signers alice --args @0x75995fa86f8ebc0b0819ebf80abc0ee6 --args 100u128 +script { + use starcoin_framework::transfer_scripts::peer_to_peer_v2; + use starcoin_framework::account; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, fresh_address: address, initial_amount: u128) { + account::create_account_if_does_not_exist(fresh_address); + // coin::transfer(&account, fresh_address, (initial_amount as u64)); + peer_to_peer_v2(account, fresh_address, initial_amount); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.exp new file mode 100644 index 0000000000..62c41c6383 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.exp @@ -0,0 +1,7 @@ +processed 3 tasks + +task 2 'run'. lines 5-8: +{ + "gas_used": 8439, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.move new file mode 100644 index 0000000000..e83d45d718 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/empty_script.move @@ -0,0 +1,8 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script { + fun empty_script() {} +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.exp new file mode 100644 index 0000000000..64760372e4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.exp @@ -0,0 +1,31 @@ +processed 12 tasks + +task 4 'run'. lines 11-33: +{ + "gas_used": 1175687, + "status": "Executed" +} + +task 6 'run'. lines 37-60: +{ + "gas_used": 473883, + "status": "Executed" +} + +task 8 'run'. lines 64-89: +{ + "gas_used": 121924, + "status": "Executed" +} + +task 9 'run'. lines 92-112: +{ + "gas_used": 241000, + "status": "Executed" +} + +task 11 'run'. lines 116-127: +{ + "gas_used": 209173, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.move new file mode 100644 index 0000000000..1acc00f63a --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_modify_dao_config_proposal.move @@ -0,0 +1,127 @@ +//# init -n dev + + +//# faucet --addr alice --amount 159256800000 + +//# faucet --addr bob --amount 49814200010000000 + +//# block --author 0x1 --timestamp 86400000 + + +//# run --signers alice --args 86400000 --args 0 --args 50u8 --args 0 --args 0 +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun create_proposal_by_alice( + signer: signer, + voting_delay: u64, + voting_period: u64, + voting_quorum_rate: u8, + min_action_delay: u64, + exec_delay: u64 + ) { + dao_modify_config_proposal::propose( + signer, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + exec_delay + ); + } +} + +//# block --author 0x1 --timestamp 87000000 + +//# run --signers bob --args @alice --args 0 --args true --args 39814200010000000u128 +script { + use starcoin_framework::coin; + use starcoin_framework::dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao_modify_config_proposal::DaoConfigUpdate; + + fun do_cast_vote_by_bob( + account: signer, + proposer_address: address, + proposal_id: u64, + agree: bool, + votes: u128 + ) { + let coin = coin::withdraw(&account, (votes as u64)); + dao::cast_vote( + &account, + proposer_address, + proposal_id, + coin, + agree + ); + } +} + +//# block --author 0x1 --timestamp 110000000 + +//# run --signers bob --args @alice --args 0 +script { + use std::string; + use starcoin_framework::timestamp; + use starcoin_std::debug; + use starcoin_framework::dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao_modify_config_proposal::DaoConfigUpdate; + + fun do_queue_proposal_action_by_bob( + _account: signer, + proposer_address: address, + proposal_id: u64, + ) { + debug::print(&string::utf8(b"do_queue_proposal_action_by_bob | proposal state")); + debug::print(&dao::proposal_state(proposer_address, proposal_id)); + + debug::print(&string::utf8(b"do_queue_proposal_action_by_bob | current timestamp")); + debug::print(×tamp::now_milliseconds()); + + dao::queue_proposal_action( + proposer_address, + proposal_id + ); + } +} + + +//# run --signers bob --args @alice --args 0 +script { + use std::signer; + use starcoin_framework::starcoin_account::deposit_coins; + use starcoin_framework::dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::dao_modify_config_proposal::DaoConfigUpdate; + + fun main( + account: signer, + proposer_address: address, + proposal_id: u64, + ) { + let coin = dao::unstake_votes( + &account, + proposer_address, + proposal_id + ); + deposit_coins(signer::address_of(&account), coin); + } +} + +//# block --author 0x1 --timestamp 250000000 + +//# run --signers bob --args @alice --args 0 +script { + use starcoin_framework::dao_modify_config_proposal; + use starcoin_framework::starcoin_coin::STC; + + fun main(proposer_address: address, proposal_id: u64) { + dao_modify_config_proposal::execute( + proposer_address, + proposal_id + ); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.exp new file mode 100644 index 0000000000..4e87b6eae4 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.exp @@ -0,0 +1,31 @@ +processed 12 tasks + +task 4 'run'. lines 10-30: +{ + "gas_used": 1175343, + "status": "Executed" +} + +task 6 'run'. lines 33-53: +{ + "gas_used": 492147, + "status": "Executed" +} + +task 8 'run'. lines 58-75: +{ + "gas_used": 40171, + "status": "Executed" +} + +task 9 'run'. lines 77-94: +{ + "gas_used": 224733, + "status": "Executed" +} + +task 11 'run'. lines 98-111: +{ + "gas_used": 116553, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.move new file mode 100644 index 0000000000..b376fe144b --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/execute_on_chain_config_proposal.move @@ -0,0 +1,111 @@ +//# init -n dev + +//# faucet --addr alice --amount 159256800000 + +//# faucet --addr bob --amount 49814200010000000 + +//# block --author 0x1 --timestamp 86400000 + + +//# run --signers alice --args false --args false --args 0 +script { + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::transaction_publish_option; + + fun main(account: signer, + script_allowed: bool, + module_publishing_allowed: bool, + exec_delay: u64) { + let txn_publish_option = transaction_publish_option::new_transaction_publish_option( + script_allowed, + module_publishing_allowed + ); + on_chain_config_dao::propose_update( + &account, + txn_publish_option, + exec_delay + ); + } +} +//# block --author 0x1 --timestamp 87000000 + +//# run --signers bob --args @alice --args 0 --args true --args 39814200010000000u128 +script { + use starcoin_framework::dao_vote_scripts; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::on_chain_config_dao::OnChainConfigUpdate; + use starcoin_framework::transaction_publish_option::TransactionPublishOption; + + fun main(account: signer, + proposer_address: address, + proposal_id: u64, + agree: bool, + votes: u128 + ) { + dao_vote_scripts::cast_vote>( + account, + proposer_address, + proposal_id, + agree, + votes); + } +} + + +//# block --author 0x1 --timestamp 110000000 + +//# run --signers bob --args @alice --args 0 +script { + use starcoin_framework::dao; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::on_chain_config_dao::OnChainConfigUpdate; + use starcoin_framework::transaction_publish_option::TransactionPublishOption; + + fun main( + _account: signer, + proposer_address: address, + proposal_id: u64, + ) { + dao::queue_proposal_action>( + proposer_address, + proposal_id + ); + } +} + +//# run --signers bob --args @alice --args 0 +script { + use starcoin_framework::transaction_publish_option::TransactionPublishOption; + use starcoin_framework::on_chain_config_dao; + use starcoin_framework::dao_vote_scripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, + proposer_address: address, + proposal_id: u64, + ) { + dao_vote_scripts::unstake_vote>( + account, + proposer_address, + proposal_id + ); + } +} + +//# block --author 0x1 --timestamp 250000000 + +//# run --signers alice --args 0 +script { + use std::signer; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::transaction_publish_option::TransactionPublishOption; + use starcoin_framework::on_chain_config_dao; + + fun main(account: signer, proposal_id: u64) { + on_chain_config_dao::execute( + signer::address_of(&account), + proposal_id + ); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.exp new file mode 100644 index 0000000000..d5d4990982 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.exp @@ -0,0 +1,43 @@ +processed 15 tasks + +task 5 'run'. lines 27-39: +{ + "gas_used": 322755, + "status": "Executed" +} + +task 6 'run'. lines 41-52: +{ + "gas_used": 143983, + "status": "Executed" +} + +task 7 'run'. lines 54-64: +{ + "gas_used": 44543, + "status": "Executed" +} + +task 8 'run'. lines 67-86: +{ + "gas_used": 158774, + "status": "Executed" +} + +task 10 'run'. lines 92-109: +{ + "gas_used": 118835, + "status": "Executed" +} + +task 12 'run'. lines 113-126: +{ + "gas_used": 32929, + "status": "Executed" +} + +task 14 'run'. lines 131-140: +{ + "gas_used": 77775, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.move new file mode 100644 index 0000000000..deee4e0f26 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/module_upgrade.move @@ -0,0 +1,140 @@ +//# init -n dev + + +//# faucet --addr alice + +//# faucet --addr bob + + +//# publish +module alice::MyToken { + use starcoin_framework::Token; + use starcoin_framework::dao; + + struct MyToken has copy, drop, store { } + + public fun init(account: &signer) { + Token::register_token( + account, + 3, + ); + dao::plugin(account, 60 * 1000, 60 * 60 * 1000, 4, 60 * 60 * 1000); + } +} + +//# block --author 0x1 --timestamp 2601000 + +//# run --signers alice +script { + use alice::MyToken::{MyToken, Self}; + use starcoin_framework::account; + use starcoin_framework::Token; + + fun main(account: signer) { + MyToken::init(&account); + account::do_accept_token(&account); + let coin = Token::mint(&account, 10000); + coin::deposit(&account, coin) + } +} + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use starcoin_framework::stc_version::Version; + use starcoin_framework::PackageTxnManager; + use std::option; + + fun update_module_upgrade_strategy(account: signer) { + Config::publish_new_config(&account, Version::new_version(1)); + PackageTxnManager::update_module_upgrade_strategy(&account, PackageTxnManager::get_strategy_two_phase(), option::some(1)); + } +} + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::PackageTxnManager; + use alice::MyToken::MyToken; + + fun plugin(account: signer) { + let upgrade_plan_cap = PackageTxnManager::extract_submit_upgrade_plan_cap(&account); + UpgradeModuleDaoProposal::plugin(&account, upgrade_plan_cap); + } +} + + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use alice::MyToken::MyToken; + + fun propose_module_upgrade(account: signer) { + let module_address = @alice; + let package_hash = x"1111111111111111"; + let version = 1; + let exec_delay = 60 * 60 * 1000; + UpgradeModuleDaoProposal::propose_module_upgrade_v2( + &account, + module_address, + copy package_hash, + version, + exec_delay, + false, + ); + } +} + + + +//# block --author 0x1 --timestamp 3601000 + +//# run --signers alice --args @alice --args 0 --args true --args 500u128 +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::dao; + use starcoin_framework::account; + use alice::MyToken::MyToken; + + fun cast_vote( + signer: signer, + proposer_address: address, + proposal_id: u64, + agree: bool, + votes: u128, + ) { + let stake = coin::withdraw(&signer, votes); + dao::cast_vote(&signer, proposer_address, proposal_id, stake, agree); + } +} + +//# block --author 0x1 --timestamp 7662000 + +//# run --signers alice --args @alice --args 0 + +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::dao; + use alice::MyToken::MyToken; + + fun queue_proposal_action(_signer: signer, + proposer_address: address, + proposal_id: u64 + ) { + dao::queue_proposal_action(proposer_address, proposal_id); + } +} + +//# block --author 0x1 --timestamp 12262000 + + +//# run --signers alice --args @alice --args 0 + +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use alice::MyToken::MyToken; + + fun submit_module_upgrade_plan(_account: signer, proposer_address: address, proposal_id: u64) { + UpgradeModuleDaoProposal::submit_module_upgrade_plan(proposer_address, proposal_id); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.exp new file mode 100644 index 0000000000..433de19135 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.exp @@ -0,0 +1,13 @@ +processed 5 tasks + +task 3 'run'. lines 9-17: +{ + "gas_used": 111257, + "status": "Executed" +} + +task 4 'run'. lines 19-27: +{ + "gas_used": 107018, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.move new file mode 100644 index 0000000000..91f768012c --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer.move @@ -0,0 +1,27 @@ +//# init -n dev + + +//# faucet --addr alice + +//# faucet --addr bob + + +//# run --signers alice --args @bob --args x"" --args 100u128 +script { + use starcoin_framework::transfer_scripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, payee: address, payee_auth_key: vector, amount: u128) { + transfer_scripts::peer_to_peer(account, payee, payee_auth_key, amount); + } +} + +//# run --signers alice --args @bob --args 100u128 +script { + use starcoin_framework::transfer_scripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, payee: address, amount: u128) { + transfer_scripts::peer_to_peer_v2(account, payee, amount); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.exp new file mode 100644 index 0000000000..255be7188d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.exp @@ -0,0 +1,13 @@ +processed 5 tasks + +task 3 'run'. lines 8-16: +{ + "gas_used": 108793, + "status": "Executed" +} + +task 4 'run'. lines 18-26: +{ + "gas_used": 103969, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.move new file mode 100644 index 0000000000..c2e3d4da94 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/peer_to_peer_with_metadata.move @@ -0,0 +1,26 @@ +//# init -n dev + + +//# faucet --addr alice + +//# faucet --addr bob + +//# run --signers alice --args @bob --args x"" --args 100u128 --args x"" +script { + use starcoin_framework::TransferScripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, payee: address, payee_auth_key: vector, amount: u128, metadata: vector) { + TransferScripts::peer_to_peer_with_metadata(account, payee, payee_auth_key, amount, metadata); + } +} + +//# run --signers alice --args @bob --args 100u128 --args x"" +script { + use starcoin_framework::TransferScripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer, payee: address, amount: u128, metadata: vector) { + TransferScripts::peer_to_peer_with_metadata_v2(account, payee, amount, metadata); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.exp b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.exp new file mode 100644 index 0000000000..7e9be7750d --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.exp @@ -0,0 +1,25 @@ +processed 9 tasks + +task 2 'run'. lines 5-16: +{ + "gas_used": 25085, + "status": "Executed" +} + +task 4 'run'. lines 21-29: +{ + "gas_used": 171890, + "status": "Executed" +} + +task 6 'run'. lines 35-45: +{ + "gas_used": 40842, + "status": "Executed" +} + +task 8 'run'. lines 51-60: +{ + "gas_used": 140131, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.move b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.move new file mode 100644 index 0000000000..ca91ea600f --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/transaction_scripts/withdraw_token_from_treasury.move @@ -0,0 +1,60 @@ +//# init -n dev + +//# faucet --addr alice --amount 0 + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Treasury; + //use starcoin_framework::Debug; + + fun mint(account: signer) { + let cap = Treasury::remove_linear_withdraw_capability(&account); + assert!(Treasury::get_linear_withdraw_capability_withdraw(&cap) == 0, 1001); + Treasury::add_linear_withdraw_capability(&account, cap); + } +} + +//# block --author alice + + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::TreasuryScripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer) { + TreasuryScripts::withdraw_and_split_lt_withdraw_cap(account, @alice, 100000000000000, 0); + } +} + + +//# block --author alice + + +//# run --signers alice +script { + use starcoin_framework::Offer; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::Treasury; + + fun redeem_offer(account: signer) { + let cap = Offer::redeem>(&account, @StarcoinAssociation); + Treasury::add_linear_withdraw_capability(&account,cap); + } +} + + +//# block --author alice + + +//# run --signers alice + +script { + use starcoin_framework::TreasuryScripts; + use starcoin_framework::starcoin_coin::STC; + + fun main(account: signer) { + TreasuryScripts::withdraw_token_with_linear_withdraw_capability(account); + } +} diff --git a/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.exp b/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.exp new file mode 100644 index 0000000000..7e93a0dbb1 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.exp @@ -0,0 +1,25 @@ +processed 9 tasks + +task 2 'run'. lines 5-20: +{ + "gas_used": 48026, + "status": "Executed" +} + +task 4 'run'. lines 24-40: +{ + "gas_used": 351963, + "status": "Executed" +} + +task 6 'run'. lines 45-62: +{ + "gas_used": 351963, + "status": "Executed" +} + +task 8 'run'. lines 67-89: +{ + "gas_used": 354815, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.move b/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.move new file mode 100644 index 0000000000..6d4c226421 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/treasury/linear_withdraw_cap.move @@ -0,0 +1,89 @@ +//# init -n dev + +//# faucet --addr alice --amount 0 + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury; + //use starcoin_framework::Debug; + + fun mint(account: signer) { + let cap = treasury::remove_linear_withdraw_capability(&account); + assert!(treasury::get_linear_withdraw_capability_total(&cap) == 477770400000000000, 1000); + assert!(treasury::get_linear_withdraw_capability_withdraw(&cap) == 0, 1001); + assert!(treasury::get_linear_withdraw_capability_start_time(&cap) == 0, 1002); + starcoin_framework::debug::print(&treasury::get_linear_withdraw_capability_period(&cap)); + assert!(treasury::get_linear_withdraw_capability_period(&cap) == 86400, 1003); + treasury::add_linear_withdraw_capability(&account, cap); + } +} + +//# block --author alice --timestamp 3600000 + +//# run --signers StarcoinAssociation +script { + use std::signer; + use starcoin_std::debug; + use starcoin_framework::coin; + use starcoin_framework::treasury; + use starcoin_framework::starcoin_coin::STC; + + fun mint(account: signer) { + let linear_cap = treasury::remove_linear_withdraw_capability(&account); + let token = treasury::withdraw_with_linear_capability(&mut linear_cap); + debug::print(&coin::value(&token)); + assert!(coin::value(&token) == 19907100000000000, 1004); + treasury::add_linear_withdraw_capability(&account, linear_cap); + coin::deposit(signer::address_of(&account), token); + } +} + + +//# block --author alice --timestamp 7200000 + +//# run --signers StarcoinAssociation +script { + use std::signer; + use starcoin_std::debug; + + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury; + + fun mint(account: signer) { + let linear_cap = treasury::remove_linear_withdraw_capability(&account); + let token = treasury::withdraw_with_linear_capability(&mut linear_cap); + debug::print(&coin::value(&token)); + assert!(coin::value(&token) == 19907100000000000, 1005); + treasury::add_linear_withdraw_capability(&account, linear_cap); + coin::deposit(signer::address_of(&account), token); + } +} + + +//# block --author alice --timestamp 94608000000 + +//# run --signers StarcoinAssociation +script { + use std::signer; + use starcoin_framework::coin; + use starcoin_std::debug; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury; + + fun mint(account: signer) { + let cap = treasury::remove_linear_withdraw_capability(&account); + let token = treasury::withdraw_with_linear_capability(&mut cap); + debug::print(&coin::value(&token)); + assert!(coin::value(&token) == (477770400000000000 - 19907100000000000 * 2), 1006); + coin::deposit(signer::address_of(&account), token); + assert!( + treasury::get_linear_withdraw_capability_withdraw(&cap) == treasury::get_linear_withdraw_capability_total( + &cap + ), + 1007 + ); + treasury::destroy_linear_withdraw_capability(cap); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.exp b/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.exp new file mode 100644 index 0000000000..aa0e264c53 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.exp @@ -0,0 +1,19 @@ +processed 7 tasks + +task 2 'run'. lines 7-22: +{ + "gas_used": 25085, + "status": "Executed" +} + +task 4 'run'. lines 25-45: +{ + "gas_used": 358818, + "status": "Executed" +} + +task 6 'run'. lines 48-60: +{ + "gas_used": 40447, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.move b/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.move new file mode 100644 index 0000000000..b3f808450e --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/treasury/split_linear_cap.move @@ -0,0 +1,60 @@ +//# init -n dev + +//# faucet --addr alice --amount 0 + +// Test split linear mint key + +//# run --signers StarcoinAssociation +script { + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury; + + fun mint(account: signer) { + let cap = treasury::remove_linear_withdraw_capability(&account); + assert!(treasury::get_linear_withdraw_capability_withdraw(&cap) == 0, 1001); + treasury::add_linear_withdraw_capability(&account, cap); + } +} + +//! block-prologue +//! author: alice +//! block-time: 1000 +//! block-number: 1 +//# block --author alice + +//# run --signers StarcoinAssociation +script { + use std::signer; + use starcoin_framework::stc_offer; + use starcoin_framework::coin; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury; + + fun bob_take_linear_key_from_offer(account: signer) { + let cap = treasury::remove_linear_withdraw_capability(&account); + let (token, cap2) = treasury::split_linear_withdraw_cap(&mut cap, 47777040000000000 / 2); + stc_offer::create(&account, cap2, @alice, 0); + coin::deposit(signer::address_of(&account), token); + treasury::add_linear_withdraw_capability(&account, cap); + } +} + +//! block-prologue +//! author: alice +//! block-time: 2000 +//! block-number: 2 +//# block --author alice + +//# run --signers alice +script { + use starcoin_framework::stc_offer; + use starcoin_framework::starcoin_coin::STC; + use starcoin_framework::treasury::{Self, LinearWithdrawCapability}; + + fun alice_take_linear_key_from_offer(account: signer) { + let cap = + stc_offer::redeem>(&account, @StarcoinAssociation); + assert!(treasury::get_linear_withdraw_capability_total(&cap) == 47777040000000000 / 2, 1002); + treasury::add_linear_withdraw_capability(&account, cap); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.exp b/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.exp new file mode 100644 index 0000000000..18f38d02de --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.exp @@ -0,0 +1,87 @@ +processed 19 tasks + +task 5 'run'. lines 25-40: +{ + "gas_used": 249651, + "status": "Executed" +} + +task 6 'run'. lines 42-57: +{ + "gas_used": 113077, + "status": "Executed" +} + +task 7 'run'. lines 58-68: +{ + "gas_used": 25441, + "status": "Executed" +} + +task 8 'run'. lines 70-80: +{ + "gas_used": 143983, + "status": "Executed" +} + +task 9 'run'. lines 82-92: +{ + "gas_used": 42480, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "UpgradeModuleDaoProposal" + } + }, + "abort_code": "102658" + } + } +} + +task 10 'run'. lines 95-108: +{ + "gas_used": 44543, + "status": "Executed" +} + +task 11 'run'. lines 110-129: +{ + "gas_used": 21735, + "status": { + "MoveAbort": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "UpgradeModuleDaoProposal" + } + }, + "abort_code": "102916" + } + } +} + +task 12 'run'. lines 131-150: +{ + "gas_used": 158774, + "status": "Executed" +} + +task 14 'run'. lines 155-171: +{ + "gas_used": 146311, + "status": "Executed" +} + +task 16 'run'. lines 175-189: +{ + "gas_used": 62325, + "status": "Executed" +} + +task 18 'run'. lines 193-206: +{ + "gas_used": 97672, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.move b/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.move new file mode 100644 index 0000000000..1a6af5a820 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/upgrade_module_dao_proposal/basic.move @@ -0,0 +1,207 @@ +//# init -n dev + +//# faucet --addr alice --amount 100000000000000000 + +//# faucet --addr bob + +//# publish +module alice::MyToken { + use starcoin_framework::coin; + use starcoin_framework::dao; + + struct MyToken has copy, drop, store { } + + public fun init(account: &signer) { + Token::register_token( + account, + 3, + ); + dao::plugin(account, 60 * 1000, 60 * 60 * 1000, 4, 60 * 60 * 1000); + } +} + +//# block --author 0x1 + +//# run --signers alice +script { + use alice::MyToken::{MyToken, Self}; + use starcoin_framework::account; + use starcoin_framework::Token; + + fun main(account: signer) { + MyToken::init(&account); + + let market_cap = Token::market_cap(); + assert!(market_cap == 0, 8001); + assert!(Token::is_registered_in(@alice), 8002); + // Create 'Balance' resource under sender account, and init with zero + account::do_accept_token(&account); + } +} + +//# run --signers alice +script { + use starcoin_framework::account; + use starcoin_framework::Token; + use alice::MyToken::{MyToken}; + fun main(account: signer) { + // mint 100 coins and check that the market cap increases appropriately + let old_market_cap = Token::market_cap(); + let coin = Token::mint(&account, 10000); + assert!(Token::value(&coin) == 10000, 8002); + assert!(Token::market_cap() == old_market_cap + 10000, 8003); + coin::deposit(&account, coin) + } +} + +// default upgrade strategy is arbitrary +//# run --signers alice +script { + use starcoin_framework::PackageTxnManager; + use starcoin_framework::signer; + fun main(account: signer) { + let hash = x"1111111111111111"; + PackageTxnManager::check_package_txn(signer::address_of(&account), hash); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::on_chain_config; + use starcoin_framework::stc_version::Version; + use starcoin_framework::PackageTxnManager; + use std::option; + fun main(account: signer) { + Config::publish_new_config(&account, Version::new_version(1)); + PackageTxnManager::update_module_upgrade_strategy(&account, PackageTxnManager::get_strategy_two_phase(), option::some(0)); + } +} + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::PackageTxnManager; + use starcoin_framework::starcoin_coin::STC; + + fun test_plugin_fail(account: signer) { + let upgrade_plan_cap = PackageTxnManager::extract_submit_upgrade_plan_cap(&account); + UpgradeModuleDaoProposal::plugin(&account, upgrade_plan_cap); //ERR_NOT_AUTHORIZED + } +} + + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::PackageTxnManager; + use alice::MyToken::MyToken; + + +fun test_plugin(account: signer) { + let upgrade_plan_cap = PackageTxnManager::extract_submit_upgrade_plan_cap(&account); + UpgradeModuleDaoProposal::plugin(&account, upgrade_plan_cap); + } +} + +// check: EXECUTED + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::starcoin_coin::STC; + + fun test_propose_fail(account: signer) { + let module_address = @alice; + let package_hash = x"1111111111111111"; + let version = 1; + let exec_delay = 1; + UpgradeModuleDaoProposal::propose_module_upgrade_v2( + &account, + module_address, //ERR_ADDRESS_MISSMATCH + copy package_hash, + version, + exec_delay, + false, + ); + } +} + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use alice::MyToken::MyToken; + + fun test_propose(account: signer) { + let module_address = @alice; + let package_hash = x"1111111111111111"; + let version = 1; + let exec_delay = 60 * 60 * 1000; + UpgradeModuleDaoProposal::propose_module_upgrade_v2( + &account, + module_address, + copy package_hash, + version, + exec_delay, + false, + ); + } +} + + +//# block --author 0x1 --timestamp 3601000 + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::dao; + use alice::MyToken::MyToken; + use starcoin_framework::account; + use starcoin_framework::signer; + + fun vote_proposal(signer: signer) { + let proposal_id = 0; + let state = dao::proposal_state(@alice, proposal_id); + assert!(state == 2, (state as u64)); + let balance = account::balance(signer::address_of(&signer)); + let balance = coin::withdraw(&signer, balance / 2); + dao::cast_vote(&signer, @alice, proposal_id, balance, true); + } +} + +//# block --author 0x1 --timestamp 7262000 + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use starcoin_framework::dao; + use alice::MyToken::MyToken; + + fun queue_proposal(_signer: signer) { + let proposal_id = 0; + let state = dao::proposal_state(@alice, proposal_id); + assert!(state == 4, (state as u64)); + dao::queue_proposal_action(@alice, proposal_id); + let state = dao::proposal_state(@alice, proposal_id); + assert!(state == 5, (state as u64)); + } +} + +//# block --author 0x1 --timestamp 14262000 + +//# run --signers alice +script { + use starcoin_framework::UpgradeModuleDaoProposal; + use alice::MyToken::MyToken; + use starcoin_framework::dao; + + fun test_submit_plan(_account: signer) { + let proposal_id = 0; + let proposer_address = @alice; + let state = dao::proposal_state(proposer_address, proposal_id); + assert!(state == 6, (state as u64)); + UpgradeModuleDaoProposal::submit_module_upgrade_plan(proposer_address, proposal_id); + } +} + diff --git a/vm/framework/starcoin-framework/integration-tests/version/basic.exp b/vm/framework/starcoin-framework/integration-tests/version/basic.exp new file mode 100644 index 0000000000..8f676e56d5 --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/version/basic.exp @@ -0,0 +1,13 @@ +processed 4 tasks + +task 2 'run'. lines 5-12: +{ + "gas_used": 57582, + "status": "Executed" +} + +task 3 'run'. lines 14-23: +{ + "gas_used": 20093, + "status": "Executed" +} diff --git a/vm/framework/starcoin-framework/integration-tests/version/basic.move b/vm/framework/starcoin-framework/integration-tests/version/basic.move new file mode 100644 index 0000000000..fe90d6d87f --- /dev/null +++ b/vm/framework/starcoin-framework/integration-tests/version/basic.move @@ -0,0 +1,24 @@ +//# init -n dev + +//# faucet --addr alice + +//# run --signers alice +script{ +use starcoin_framework::stc_version::Version; +use starcoin_framework::on_chain_config; +fun main(account: signer) { + Config::publish_new_config(&account, Version::new_version(1)); +} +} + +//# run --signers alice +script{ +use starcoin_framework::stc_version::Version; +use starcoin_framework::signer; +fun main(account: signer) { + let version = Version::get(signer::address_of(&account)); + assert!(version == 1, 100); + let _ = version; +} +} + diff --git a/vm/framework/starcoin-framework/sources/coin.move b/vm/framework/starcoin-framework/sources/coin.move index b7d7aecea6..592997537b 100644 --- a/vm/framework/starcoin-framework/sources/coin.move +++ b/vm/framework/starcoin-framework/sources/coin.move @@ -108,12 +108,16 @@ module starcoin_framework::coin { /// APT pairing is not eanbled yet. const EAPT_PAIRING_IS_NOT_ENABLED: u64 = 28; + /// The coin decimal too long + const ECOIN_COIN_DECIMAL_TOO_LARGE: u64 = 29; + // // Constants // const MAX_COIN_NAME_LENGTH: u64 = 32; const MAX_COIN_SYMBOL_LENGTH: u64 = 10; + const MAX_COIN_DECIMAL: u8 = 38; /// Core data structures @@ -835,15 +839,25 @@ module starcoin_framework::coin { #[view] /// Returns the amount of coin in existence. public fun supply(): Option acquires CoinInfo, CoinConversionMap { + debug::print(&std::string::utf8(b"dao::supply | entered")); + let coin_supply = coin_supply(); let metadata = paired_metadata(); + + debug::print(&std::string::utf8(b"dao::supply | metadata")); + debug::print(&metadata); + if (option::is_some(&metadata)) { - let fungible_asset_supply = fungible_asset::supply(option::extract(&mut metadata)); - if (option::is_some(&coin_supply)) { + let fungible_asset_supply = + fungible_asset::supply(option::extract(&mut metadata)); + if (option::is_some(&coin_supply) && + option::is_some(&fungible_asset_supply)) { let supply = option::borrow_mut(&mut coin_supply); *supply = *supply + option::destroy_some(fungible_asset_supply); }; }; + debug::print(&std::string::utf8(b"dao::supply | exit, coin supply")); + debug::print(&coin_supply); coin_supply } @@ -1116,6 +1130,7 @@ module starcoin_framework::coin { assert!(string::length(&name) <= MAX_COIN_NAME_LENGTH, error::invalid_argument(ECOIN_NAME_TOO_LONG)); assert!(string::length(&symbol) <= MAX_COIN_SYMBOL_LENGTH, error::invalid_argument(ECOIN_SYMBOL_TOO_LONG)); + assert!(decimals < MAX_COIN_DECIMAL, error::invalid_argument(ECOIN_COIN_DECIMAL_TOO_LARGE)); let coin_info = CoinInfo { name, diff --git a/vm/framework/starcoin-framework/sources/stc/bcs_util.move b/vm/framework/starcoin-framework/sources/stc/bcs_util.move index c07085faac..0fc715aeb6 100644 --- a/vm/framework/starcoin-framework/sources/stc/bcs_util.move +++ b/vm/framework/starcoin-framework/sources/stc/bcs_util.move @@ -48,7 +48,7 @@ module starcoin_framework::bcs_util { pragma verify = false; } - #[test] use std::bcs; + #[test_only] use std::bcs; #[test] public fun test_deserialize_bytes_array() { @@ -398,7 +398,7 @@ module starcoin_framework::bcs_util { } #[test] - #[expected_failure(abort_code = 206, location= StarcoinFramework::BCS)] + #[expected_failure(abort_code = 206, location= starcoin_framework::bcs)] public fun test_deserialize_uleb128_as_u32_exceeded_max_int() { let max_int: u64 = 2147483647; let exceeded_max_int: u64 = max_int + 1; diff --git a/vm/framework/starcoin-framework/sources/stc/block_reward.move b/vm/framework/starcoin-framework/sources/stc/block_reward.move index a532010bfb..2079fd8889 100644 --- a/vm/framework/starcoin-framework/sources/stc/block_reward.move +++ b/vm/framework/starcoin-framework/sources/stc/block_reward.move @@ -12,7 +12,7 @@ module starcoin_framework::block_reward { use starcoin_framework::starcoin_coin::STC; use starcoin_framework::system_addresses; use starcoin_framework::treasury; - use starcoin_framework::treasury_withdraw_dao_proposal; + use starcoin_framework::dao_treasury_withdraw_proposal; use starcoin_std::debug; /// Queue of rewards distributed to miners. @@ -140,7 +140,7 @@ module starcoin_framework::block_reward { debug::print(&std::string::utf8(b"block_reward::process_block_reward | treasury_balance: ")); debug::print(&treasury_balance); if (block_reward > 0) { - let reward = treasury_withdraw_dao_proposal::withdraw_for_block_reward(account, block_reward); + let reward = dao_treasury_withdraw_proposal::withdraw_for_block_reward(account, block_reward); coin::merge(&mut total_reward, reward); }; }; diff --git a/vm/framework/starcoin-framework/sources/stc/dao.move b/vm/framework/starcoin-framework/sources/stc/dao.move index e8cb1b589f..4fd69c2583 100644 --- a/vm/framework/starcoin-framework/sources/stc/dao.move +++ b/vm/framework/starcoin-framework/sources/stc/dao.move @@ -3,6 +3,7 @@ module starcoin_framework::dao { use std::error; use std::option; use std::signer; + use starcoin_std::debug; use starcoin_framework::account; use starcoin_framework::coin; @@ -113,6 +114,7 @@ module starcoin_framework::dao { const ERR_VOTE_STATE_MISMATCH: u64 = 1408; const ERR_ACTION_MUST_EXIST: u64 = 1409; const ERR_VOTED_OTHERS_ALREADY: u64 = 1410; + const ERR_TOKEN_NOT_REGISTER: u64 = 1411; /// plugin function, can only be called by token issuer. /// Any token who wants to have gov functionality @@ -169,6 +171,8 @@ module starcoin_framework::dao { action: ActionT, action_delay: u64, ) acquires DaoGlobalInfo { + debug::print(&std::string::utf8(b"dao::proposal | Entered")); + if (action_delay == 0) { action_delay = min_action_delay(); } else { @@ -178,6 +182,9 @@ module starcoin_framework::dao { let proposer = signer::address_of(signer); let start_time = timestamp::now_milliseconds() + voting_delay(); let quorum_votes = quorum_votes(); + + debug::print(&std::string::utf8(b"dao::proposal | Proposal {")); + let proposal = Proposal { id: proposal_id, proposer, @@ -193,10 +200,15 @@ module starcoin_framework::dao { move_to(signer, proposal); // emit event let gov_info = borrow_global_mut>(stc_util::token_issuer()); + + debug::print(&std::string::utf8(b"dao::proposal | emit event")); + event::emit_event( &mut gov_info.proposal_create_event, ProposalCreatedEvent { proposal_id, proposer }, ); + + debug::print(&std::string::utf8(b"dao::proposal | Exited")); } @@ -587,11 +599,20 @@ module starcoin_framework::dao { /// Quorum votes to make proposal pass. public fun quorum_votes(): u128 { - let market_cap = option::destroy_some(coin::supply()); + debug::print(&std::string::utf8(b"dao::quorum_votes | entered ")); + + let supply = coin::supply(); + debug::print(&std::string::utf8(b"dao::quorum_votes | supply ")); + debug::print(&supply); + + assert!(option::is_some(&supply), error::invalid_state(ERR_TOKEN_NOT_REGISTER)); + + let market_cap = option::destroy_some(supply); let balance_in_treasury = treasury::balance(); let supply = market_cap - balance_in_treasury; let rate = voting_quorum_rate(); let rate = (rate as u128); + debug::print(&std::string::utf8(b"dao::quorum_votes | exited ")); supply * rate / 100 } diff --git a/vm/framework/starcoin-framework/sources/stc/dao_modify_config_proposal.move b/vm/framework/starcoin-framework/sources/stc/dao_modify_config_proposal.move new file mode 100644 index 0000000000..9a042e3e6d --- /dev/null +++ b/vm/framework/starcoin-framework/sources/stc/dao_modify_config_proposal.move @@ -0,0 +1,143 @@ +/// A proposal module which is used to modify Token's DAO configuration. +module starcoin_framework::dao_modify_config_proposal { + use starcoin_std::debug; + use starcoin_framework::stc_util; + use starcoin_framework::signer; + use starcoin_framework::on_chain_config; + use starcoin_framework::dao; + use starcoin_framework::error; + + spec module { + pragma verify = false; // break after enabling v2 compilation scheme + pragma aborts_if_is_strict; + pragma aborts_if_is_partial; + } + + /// A wrapper of `on_chain_config::ModifyConfigCapability>`. + struct DaoConfigModifyCapability has key { + cap: on_chain_config::ModifyConfigCapability>, + } + + const ERR_NOT_AUTHORIZED: u64 = 401; + const ERR_QUORUM_RATE_INVALID: u64 = 402; + + /// a proposal action to update dao config. + /// if any field is `0`, that means the proposal want to update. + struct DaoConfigUpdate has copy, drop, store { + /// new voting delay setting. + voting_delay: u64, + /// new voting period setting. + voting_period: u64, + /// new voting quorum rate setting. + voting_quorum_rate: u8, + /// new min action delay setting. + min_action_delay: u64, + } + + /// Plugin method of the module. + /// Should be called by token issuer. + public fun plugin(signer: &signer) { + let token_issuer = stc_util::token_issuer(); + assert!(signer::address_of(signer) == token_issuer, error::unauthenticated(ERR_NOT_AUTHORIZED)); + let dao_config_modify_cap = on_chain_config::extract_modify_config_capability< + dao::DaoConfig, + >(signer); + assert!( + on_chain_config::account_address(&dao_config_modify_cap) == token_issuer, + error::unauthenticated(ERR_NOT_AUTHORIZED) + ); + let cap = DaoConfigModifyCapability { cap: dao_config_modify_cap }; + move_to(signer, cap); + } + + spec plugin { + use starcoin_framework::signer; + use starcoin_framework::option; + + pragma aborts_if_is_partial = false; + let sender = signer::address_of(signer); + aborts_if sender != @0x2; + include on_chain_config::AbortsIfCapNotExist> { address: sender }; + + let config_cap = + on_chain_config::spec_cap>(sender); + aborts_if option::is_none(config_cap); + aborts_if option::borrow(config_cap).account_address != sender; + aborts_if exists>(sender); + ensures exists>(sender); + } + + /// Entrypoint for the proposal. + public entry fun propose( + signer: signer, + voting_delay: u64, + voting_period: u64, + voting_quorum_rate: u8, + min_action_delay: u64, + exec_delay: u64, + ) { + assert!(voting_quorum_rate <= 100, error::invalid_argument(ERR_QUORUM_RATE_INVALID)); + let action = DaoConfigUpdate { + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + }; + dao::propose(&signer, action, exec_delay); + } + spec propose { + use starcoin_framework::timestamp; + use starcoin_framework::system_addresses; + use starcoin_framework::signer; + use starcoin_framework::dao; + + pragma aborts_if_is_partial = false; + aborts_if voting_quorum_rate > 100; + + // copy from dao::propose spec. + include dao::AbortIfDaoConfigNotExist; + include dao::AbortIfDaoInfoNotExist; + aborts_if !exists(system_addresses::get_starcoin_framework()); + aborts_if exec_delay > 0 && exec_delay < dao::spec_dao_config().min_action_delay; + include dao::CheckQuorumVotes; + let sender = signer::address_of(signer); + aborts_if exists>(sender); + } + + /// Once the proposal is agreed, anyone can call the method to make the proposal happen. + public entry fun execute(proposer_address: address, proposal_id: u64) acquires DaoConfigModifyCapability { + let DaoConfigUpdate { + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + } = dao::extract_proposal_action(proposer_address, proposal_id); + + debug::print(&std::string::utf8(b"dao_modify_config_proposal::execute | entered")); + + let cap = borrow_global_mut>( + stc_util::token_issuer(), + ); + debug::print( + &std::string::utf8( + b"dao_modify_config_proposal::execute | borrow_global_mut>" + ) + ); + + dao::modify_dao_config( + &mut cap.cap, + voting_delay, + voting_period, + voting_quorum_rate, + min_action_delay, + ); + + debug::print(&std::string::utf8(b"dao_modify_config_proposal::execute | exited")); + } + spec execute { + pragma aborts_if_is_partial = true; + // let expected_states = vec(6); + // include dao::CheckProposalStates{expected_states}; + aborts_if !exists>(@0x2); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/sources/stc/treasury_withdraw_dao_proposal.move b/vm/framework/starcoin-framework/sources/stc/dao_treasury_withdraw_proposal.move similarity index 97% rename from vm/framework/starcoin-framework/sources/stc/treasury_withdraw_dao_proposal.move rename to vm/framework/starcoin-framework/sources/stc/dao_treasury_withdraw_proposal.move index 3177fa6c3b..f8f1c1198a 100644 --- a/vm/framework/starcoin-framework/sources/stc/treasury_withdraw_dao_proposal.move +++ b/vm/framework/starcoin-framework/sources/stc/dao_treasury_withdraw_proposal.move @@ -1,5 +1,5 @@ -/// TreasuryWithdrawDaoProposal is a dao proposal for withdraw Token from Treasury. -module starcoin_framework::treasury_withdraw_dao_proposal { +/// dao_treasury_withdraw_proposal is a dao proposal for withdraw Token from Treasury. +module starcoin_framework::dao_treasury_withdraw_proposal { use std::error; use std::signer; diff --git a/vm/framework/starcoin-framework/sources/stc/dao_vote_scripts.move b/vm/framework/starcoin-framework/sources/stc/dao_vote_scripts.move new file mode 100644 index 0000000000..e6912f11b6 --- /dev/null +++ b/vm/framework/starcoin-framework/sources/stc/dao_vote_scripts.move @@ -0,0 +1,75 @@ +module starcoin_framework::dao_vote_scripts { + use std::signer; + use starcoin_framework::coin; + use starcoin_framework::dao; + + spec module { + pragma verify = false; // break after enabling v2 compilation scheme + pragma aborts_if_is_partial = false; + pragma aborts_if_is_strict = true; + } + + public entry fun cast_vote( + signer: signer, + proposer_address: address, + proposal_id: u64, + agree: bool, + votes: u128, + ) { + let sender = signer::address_of(&signer); + if (dao::has_vote(sender, proposer_address, proposal_id)) { + // if already voted, and vote is not same as the current cast, change the existing vote. + // resolve https://github.com/starcoinorg/starcoin/issues/2925. + let (agree_voted, _) = dao::vote_of(sender, proposer_address, proposal_id); + if (agree_voted != agree) { + dao::change_vote(&signer, proposer_address, proposal_id, agree); + } + }; + + let votes = coin::withdraw(&signer, (votes as u64)); + dao::cast_vote(&signer, proposer_address, proposal_id, votes, agree); + } + + /// revoke all votes on a proposal + public entry fun revoke_vote( + signer: signer, + proposer_address: address, + proposal_id: u64, + ) { + let sender = signer::address_of(&signer); + let (_, power) = dao::vote_of(sender, proposer_address, proposal_id); + let my_token = dao::revoke_vote(&signer, proposer_address, proposal_id, power); + coin::deposit(sender, my_token); + } + + /// Let user change their vote during the voting time. + public entry fun flip_vote( + signer: signer, + proposer_address: address, + proposal_id: u64, + ) { + let (agree, _) = dao::vote_of(signer::address_of(&signer), proposer_address, proposal_id); + dao::change_vote(&signer, proposer_address, proposal_id, !agree); + } + + /// revoke some votes on a proposal + public entry fun revoke_vote_of_power( + signer: signer, + proposer_address: address, + proposal_id: u64, + power: u128, + ) { + let sender = signer::address_of(&signer); + let my_token = dao::revoke_vote(&signer, proposer_address, proposal_id, power); + coin::deposit(sender, my_token); + } + + public entry fun unstake_vote( + signer: signer, + proposer_address: address, + proposal_id: u64, + ) { + let my_token = dao::unstake_votes(&signer, proposer_address, proposal_id); + coin::deposit(signer::address_of(&signer), my_token); + } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/sources/stc/epoch.move b/vm/framework/starcoin-framework/sources/stc/epoch.move index c88aba25e6..002240ceb7 100644 --- a/vm/framework/starcoin-framework/sources/stc/epoch.move +++ b/vm/framework/starcoin-framework/sources/stc/epoch.move @@ -76,9 +76,7 @@ module starcoin_framework::epoch { const EINVALID_UNCLES_COUNT: u64 = 101; /// Initialization of the module. - public fun initialize( - account: &signer, - ) { + public fun initialize(account: &signer) { // Timestamp::assert_genesis(); system_addresses::assert_starcoin_framework(account); diff --git a/vm/framework/starcoin-framework/sources/stc/on_chain_config_dao.move b/vm/framework/starcoin-framework/sources/stc/on_chain_config_dao.move index 4f4eaeef88..43e9aca7e5 100644 --- a/vm/framework/starcoin-framework/sources/stc/on_chain_config_dao.move +++ b/vm/framework/starcoin-framework/sources/stc/on_chain_config_dao.move @@ -66,7 +66,7 @@ module starcoin_framework::on_chain_config_dao { pragma aborts_if_is_partial = false; - // copy from Dao::propose spec. + // copy from dao::propose spec. include dao::AbortIfDaoConfigNotExist; include dao::AbortIfDaoInfoNotExist; aborts_if !exists(system_addresses::get_starcoin_framework()); diff --git a/vm/framework/starcoin-framework/sources/stc/oracle.move b/vm/framework/starcoin-framework/sources/stc/oracle.move index 144be75c86..e024817ca0 100644 --- a/vm/framework/starcoin-framework/sources/stc/oracle.move +++ b/vm/framework/starcoin-framework/sources/stc/oracle.move @@ -3,11 +3,11 @@ module starcoin_framework::oracle { use std::error; use std::signer; use std::vector; - use starcoin_framework::timestamp; - use starcoin_framework::reserved_accounts_signer; - use starcoin_framework::system_addresses; + use starcoin_framework::account; use starcoin_framework::event; + use starcoin_framework::system_addresses; + use starcoin_framework::timestamp; struct OracleInfo has key { ///The datasource counter @@ -71,9 +71,9 @@ module starcoin_framework::oracle { /// Register `OracleT` as an oracle type. public fun register_oracle(sender: &signer, info: Info) { - let genesis_account = - reserved_accounts_signer::get_stored_signer(signer::address_of(sender)); - move_to(&genesis_account, OracleInfo { + // let genesis_account = + // reserved_accounts_signer::get_stored_signer(signer::address_of(sender)); + move_to(sender, OracleInfo { counter: 0, info, }); diff --git a/vm/framework/starcoin-framework/sources/stc/oracle_price_script.move b/vm/framework/starcoin-framework/sources/stc/oracle_price_script.move deleted file mode 100644 index b90b368234..0000000000 --- a/vm/framework/starcoin-framework/sources/stc/oracle_price_script.move +++ /dev/null @@ -1,15 +0,0 @@ -module starcoin_framework::oracle_price_script { - use starcoin_framework::oracle_price; - - public entry fun register_oracle(sender: signer, precision: u8) { - oracle_price::register_oracle_entry(sender, precision); - } - - public entry fun init_data_source(sender: signer, init_value: u128) { - oracle_price::init_data_source_entry(sender, init_value); - } - - public entry fun update(sender: signer, value: u128) { - oracle_price::update_entry(sender, value); - } -} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/sources/stc/stc_block.move b/vm/framework/starcoin-framework/sources/stc/stc_block.move index 06d9f2b963..e729fe4212 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_block.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_block.move @@ -4,16 +4,28 @@ module starcoin_framework::stc_block { use std::signer; use std::vector; - use starcoin_framework::epoch; + use starcoin_framework::account; use starcoin_framework::block_reward; - use starcoin_framework::timestamp; + use starcoin_framework::chain_id; + use starcoin_framework::coin; + use starcoin_framework::epoch; + use starcoin_framework::event; use starcoin_framework::starcoin_coin::STC; use starcoin_framework::stc_transaction_fee; - - use starcoin_framework::chain_id; - use starcoin_framework::account; use starcoin_framework::system_addresses; - use starcoin_framework::event; + use starcoin_framework::timestamp; + use starcoin_std::debug; + + #[test_only] + use std::bcs; + #[test_only] + use std::hash; + #[test_only] + use starcoin_framework::account::create_signer_for_test; + #[test_only] + use starcoin_framework::bcs_util; + #[test_only] + use starcoin_framework::create_signer; const EPROLOGUE_BAD_CHAIN_ID: u64 = 6; @@ -27,10 +39,10 @@ module starcoin_framework::stc_block { author: address, /// number of uncles. uncles: u64, - /// Handle of events when new blocks are emitted - new_block_events: event::EventHandle, /// 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. @@ -53,19 +65,18 @@ module starcoin_framework::stc_block { /// This can only be invoked by the GENESIS_ACCOUNT at genesis public fun initialize(account: &signer, parent_hash: vector) { - // Timestamp::assert_genesis(); system_addresses::assert_starcoin_framework(account); - move_to( - account, - BlockMetadata { - number: 0, - parent_hash, - author: system_addresses::get_starcoin_framework(), - uncles: 0, - parents_hash: vector::empty(), - new_block_events: account::new_event_handle(account), - }); + let block_metadata = BlockMetadata { + number: 0, + parent_hash, + author: system_addresses::get_starcoin_framework(), + uncles: 0, + parents_hash: vector::empty(), + new_block_events: account::new_event_handle(account), + }; + + move_to(account, block_metadata); } spec initialize { @@ -90,6 +101,11 @@ module starcoin_framework::stc_block { aborts_if !exists(system_addresses::get_starcoin_framework()); } + /// Get the hash of the parents block, used for DAG + public fun get_parents_hash(): vector acquires BlockMetadata { + *&borrow_global(system_addresses::get_starcoin_framework()).parents_hash + } + /// Gets the address of the author of the current block public fun get_current_author(): address acquires BlockMetadata { borrow_global(system_addresses::get_starcoin_framework()).author @@ -125,6 +141,8 @@ module starcoin_framework::stc_block { debug::print(&coin::value(&txn_fee)); // then deal with current block. + debug::print(&std::string::utf8(b"stc_block::block_prologue | timestamp::update_global_time")); + debug::print(×tamp); timestamp::update_global_time(&account, signer::address_of(&account), timestamp * 1000); process_block_metadata( @@ -139,9 +157,6 @@ module starcoin_framework::stc_block { let reward = epoch::adjust_epoch(&account, number, timestamp, uncles, parent_gas_used); - debug::print(&std::string::utf8(b"stc_block::block_prologue | timestamp::update_global_time")); - debug::print(×tamp); - // pass in previous block gas fees. block_reward::process_block_reward(&account, number, reward, author, auth_key_vec, txn_fee); @@ -189,15 +204,6 @@ module starcoin_framework::stc_block { debug::print(&std::string::utf8(b"stc_block::process_block_metadata | Exited")); } - #[test] - use starcoin_framework::bcs_util; - #[test] - use std::hash; - #[test] - use std::option; - use starcoin_framework::coin; - use starcoin_std::debug; - #[test] fun test_header() { // Block header Unit test @@ -286,4 +292,27 @@ module starcoin_framework::stc_block { assert!(number == 2, 1002); assert!(state_root == x"d2df4c8c579f9e05b0adf14b53785379fb245465d703834eb19fba74d9114a9a", 1003); } + + #[test] + fun test_block_metadata_bcs() { + let test_framework = create_signer_for_test(system_addresses::get_starcoin_framework()); + let block_metadata = BlockMetadata { + number: 0, + parent_hash: vector::empty(), + author: system_addresses::get_starcoin_framework(), + uncles: 0, + parents_hash: vector::empty(), + new_block_events: account::new_event_handle(&test_framework), + }; + let bcs = bcs::to_bytes(&block_metadata); + debug::print(&std::string::utf8(b"test_block_metadata_bcs | bcs")); + debug::print(&vector::length(&bcs)); + debug::print(&bcs); + + let BlockMetadata { + number: _, parent_hash: _, author: _, uncles: _, parents_hash: _, new_block_events: event + } = block_metadata; + event::destroy_handle(event); + } + } \ No newline at end of file diff --git a/vm/framework/starcoin-framework/sources/stc/stc_genesis.move b/vm/framework/starcoin-framework/sources/stc/stc_genesis.move index 8d3b2ad655..1e09a0c75c 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_genesis.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_genesis.move @@ -3,8 +3,8 @@ module starcoin_framework::stc_genesis { use std::option; use std::vector; - use starcoin_std::debug; - use starcoin_framework::stc_language_version; + use starcoin_framework::oracle_stc_usd; + use starcoin_framework::dao_modify_config_proposal; use starcoin_framework::account; use starcoin_framework::aggregator_factory; @@ -21,6 +21,7 @@ module starcoin_framework::stc_genesis { use starcoin_framework::starcoin_coin; use starcoin_framework::starcoin_coin::STC; use starcoin_framework::stc_block; + use starcoin_framework::stc_language_version; use starcoin_framework::stc_transaction_fee; use starcoin_framework::stc_transaction_package_validation; use starcoin_framework::stc_transaction_timeout_config; @@ -30,8 +31,9 @@ module starcoin_framework::stc_genesis { use starcoin_framework::timestamp; use starcoin_framework::transaction_publish_option; use starcoin_framework::treasury; - use starcoin_framework::treasury_withdraw_dao_proposal; + use starcoin_framework::dao_treasury_withdraw_proposal; use starcoin_framework::vm_config; + use starcoin_std::debug; spec module { pragma verify = false; // break after enabling v2 compilation scheme @@ -192,6 +194,9 @@ module starcoin_framework::stc_genesis { // account::release_genesis_signer(genesis_account); // account::release_genesis_signer(association); + // Register oracle + oracle_stc_usd::register(&starcoin_framework_account); + debug::print(&std::string::utf8(b"stc_genesis::initialize | Exited")); } @@ -291,7 +296,8 @@ module starcoin_framework::stc_genesis { ); treasury::add_linear_withdraw_capability(core_resource_account, liner_withdraw_cap); }; - treasury_withdraw_dao_proposal::plugin(starcoin_framework, treasury_withdraw_cap); + dao_treasury_withdraw_proposal::plugin(starcoin_framework, treasury_withdraw_cap); + dao_modify_config_proposal::plugin(starcoin_framework); } /// Init the genesis for unit tests diff --git a/vm/framework/starcoin-framework/sources/stc/stc_offer.move b/vm/framework/starcoin-framework/sources/stc/stc_offer.move new file mode 100644 index 0000000000..5165e4343a --- /dev/null +++ b/vm/framework/starcoin-framework/sources/stc/stc_offer.move @@ -0,0 +1,101 @@ +module starcoin_framework::stc_offer { + use std::error; + use std::signer; + use starcoin_framework::timestamp; + + spec module { + pragma verify = true; + pragma aborts_if_is_strict = true; + } + + /// A wrapper around value `offered` that can be claimed by the address stored in `for` when after lock time. + struct Offer has key { + offered: Offered, + for_address: address, + time_lock: u64 + } + + /// An offer of the specified type for the account does not match + const EOFFER_DNE_FOR_ACCOUNT: u64 = 101; + + /// Offer is not unlocked yet. + const EOFFER_NOT_UNLOCKED: u64 = 102; + + /// Publish a value of type `Offered` under the sender's account. The value can be claimed by + /// either the `for` address or the transaction sender. + public fun create(account: &signer, offered: Offered, for_address: address, lock_period: u64) { + let time_lock = timestamp::now_seconds() + lock_period; + //TODO should support multi Offer? + move_to(account, Offer { + offered, + for_address, + time_lock + }); + } + + spec create { + use starcoin_framework::timestamp; + use starcoin_framework::signer; + + // include timestamp::AbortsIfTimestampNotExists; + aborts_if timestamp::now_seconds() + lock_period > max_u64(); + aborts_if exists>(signer::address_of(account)); + } + + /// Claim the value of type `Offered` published at `offer_address`. + /// Only succeeds if the sender is the intended recipient stored in `for` or the original + /// publisher `offer_address`, and now >= time_lock + /// Also fails if no such value exists. + public fun redeem(account: &signer, offer_address: address): Offered acquires Offer { + let Offer { offered, for_address, time_lock } = move_from>(offer_address); + let sender = signer::address_of(account); + let now = timestamp::now_seconds(); + assert!(sender == for_address || sender == offer_address, error::invalid_argument(EOFFER_DNE_FOR_ACCOUNT)); + assert!(now >= time_lock, error::invalid_state(EOFFER_NOT_UNLOCKED)); + offered + } + + spec redeem { + use starcoin_framework::timestamp; + use starcoin_framework::signer; + + aborts_if !exists>(offer_address); + aborts_if + signer::address_of(account) != global>(offer_address).for_address + && signer::address_of(account) != offer_address; + aborts_if timestamp::now_seconds() < global>(offer_address).time_lock; + // include timestamp::AbortsIfTimestampNotExists; + } + + /// Returns true if an offer of type `Offered` exists at `offer_address`. + public fun exists_at(offer_address: address): bool { + exists>(offer_address) + } + + spec exists_at { + aborts_if false; + } + + /// Returns the address of the `Offered` type stored at `offer_address`. + /// Fails if no such `Offer` exists. + public fun address_of(offer_address: address): address acquires Offer { + borrow_global>(offer_address).for_address + } + + spec address_of { + aborts_if !exists>(offer_address); + } + + // /// Take Offer and put to signer's Collection. + // public entry fun take_offer( + // signer: signer, + // offer_address: address, + // ) acquires Offer { + // let offered = redeem(&signer, offer_address); + // Collection2::put(&signer, signer::address_of(&signer), offered); + // } + + // spec take_offer { + // pragma verify = false; + // } +} \ No newline at end of file diff --git a/vm/framework/starcoin-framework/sources/stc/treasury.move b/vm/framework/starcoin-framework/sources/stc/treasury.move index a963953a8d..7bbc5ed870 100644 --- a/vm/framework/starcoin-framework/sources/stc/treasury.move +++ b/vm/framework/starcoin-framework/sources/stc/treasury.move @@ -2,14 +2,15 @@ module starcoin_framework::treasury { use std::error; use std::signer; - use starcoin_std::math128; - use starcoin_std::type_info; - use starcoin_framework::timestamp; - use starcoin_framework::stc_util; + use starcoin_framework::account; - use starcoin_framework::event; use starcoin_framework::coin; + use starcoin_framework::event; + use starcoin_framework::stc_util; + use starcoin_framework::timestamp; + use starcoin_std::math128; + use starcoin_std::type_info; struct Treasury has store, key { balance: coin::Coin, diff --git a/vm/framework/starcoin-framework/sources/timestamp.move b/vm/framework/starcoin-framework/sources/timestamp.move index 57972601d4..46a99fb487 100644 --- a/vm/framework/starcoin-framework/sources/timestamp.move +++ b/vm/framework/starcoin-framework/sources/timestamp.move @@ -36,10 +36,11 @@ module starcoin_framework::timestamp { /// Updates the wall clock time by consensus. Requires VM privilege and will be invoked during block prologue. public fun update_global_time( account: &signer, - proposer: address, + _proposer: address, timestamp: u64 ) acquires CurrentTimeMicroseconds { debug::print(&std::string::utf8(b"timestamp::update_global_time | Entered")); + debug::print(×tamp); // Can only be invoked by StarcoinVM signer. // system_addresses::assert_vm(account); diff --git a/vm/framework/starcoin-framework/sources/timestamp.spec.move b/vm/framework/starcoin-framework/sources/timestamp.spec.move index 74bd5581e2..d2cb143ef7 100644 --- a/vm/framework/starcoin-framework/sources/timestamp.spec.move +++ b/vm/framework/starcoin-framework/sources/timestamp.spec.move @@ -40,18 +40,18 @@ spec starcoin_framework::timestamp { use starcoin_framework::chain_status; requires chain_status::is_operating(); include UpdateGlobalTimeAbortsIf; - ensures (proposer != @vm_reserved) ==> (spec_now_microseconds() == timestamp); + ensures (_proposer != @vm_reserved) ==> (spec_now_microseconds() == timestamp); } spec schema UpdateGlobalTimeAbortsIf { account: signer; - proposer: address; + _proposer: address; timestamp: u64; /// [high-level-req-3] aborts_if !system_addresses::is_vm(account); /// [high-level-req-4] - aborts_if (proposer == @vm_reserved) && (spec_now_microseconds() != timestamp); - aborts_if (proposer != @vm_reserved) && (spec_now_microseconds() >= timestamp); + aborts_if (_proposer == @vm_reserved) && (spec_now_microseconds() != timestamp); + aborts_if (_proposer != @vm_reserved) && (spec_now_microseconds() >= timestamp); } spec fun spec_now_microseconds(): u64 { diff --git a/vm/starcoin-transactional-test-harness/tests/cases/borrow_field_test.move b/vm/starcoin-transactional-test-harness/tests/cases/borrow_field_test.move index 0bd32d6586..2ee1f8d579 100644 --- a/vm/starcoin-transactional-test-harness/tests/cases/borrow_field_test.move +++ b/vm/starcoin-transactional-test-harness/tests/cases/borrow_field_test.move @@ -28,7 +28,7 @@ script { use creator::test; fun main(s: signer) { - let addr = Signer::address_of(&s); + let addr = signer::address_of(&s); test::test_borrow_field(addr); } } diff --git a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd.move b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd.move index a24d3a9d05..d7a7dc48c9 100644 --- a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd.move +++ b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd.move @@ -13,8 +13,8 @@ script{ use StarcoinFramework::Vector; fun main(_sender: signer, block_number: u64, block_hash: vector, state_proof: vector){ assert!(block_number == 1, 1000); - assert!(Vector::length(&block_hash) == 32, 1001); - assert!(Vector::length(&state_proof) > 32, 1002); + assert!(vector::length(&block_hash) == 32, 1001); + assert!(vector::length(&state_proof) > 32, 1002); } } diff --git a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_barnard.move b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_barnard.move index ebc586b0dc..cda21e6965 100644 --- a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_barnard.move +++ b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_barnard.move @@ -12,7 +12,7 @@ // use StarcoinFramework::Vector; // fun main(_sender: signer, block_number: u64, block_hash: vector){ // assert!(block_number == 6487000, 1000); -// assert!(Vector::length(&block_hash) == 32, 1001); +// assert!(vector::length(&block_hash) == 32, 1001); // assert!(x"58d3b6aa35ba1f52c809382b876950b6038c4ce9fa078358c0fcf0b072e5ae3d" == block_hash, 1002); // } // } \ No newline at end of file diff --git a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move index f705723351..8480b31170 100644 --- a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move +++ b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move @@ -9,6 +9,6 @@ script{ use StarcoinFramework::Vector; fun main(_sender: signer, block_number: u64, block_hash: vector){ assert!(block_number == 1, 1000); - assert!(Vector::length(&block_hash) == 32, 1001); + assert!(vector::length(&block_hash) == 32, 1001); } } \ No newline at end of file diff --git a/vm/types/src/on_chain_resource/block_metadata.rs b/vm/types/src/on_chain_resource/block_metadata.rs index 7e7e723b43..3eb3afaea7 100644 --- a/vm/types/src/on_chain_resource/block_metadata.rs +++ b/vm/types/src/on_chain_resource/block_metadata.rs @@ -2,15 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 use crate::event::EventHandle; -use move_core_types::account_address::AccountAddress; -use move_core_types::ident_str; -use move_core_types::identifier::IdentStr; -use move_core_types::move_resource::{MoveResource, MoveStructType}; +use move_core_types::{ + account_address::AccountAddress, + ident_str, + identifier::IdentStr, + move_resource::{MoveResource, MoveStructType}, +}; use serde::{Deserialize, Serialize}; use starcoin_crypto::HashValue; /// On chain resource BlockMetadata mapping for FlexiDag block -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct BlockMetadata { // number of the current block pub number: u64, @@ -18,11 +20,12 @@ pub struct BlockMetadata { pub parent_hash: HashValue, // Author of the current block. pub author: AccountAddress, + // Uncle blocks number pub uncles: u64, + // Parents hash for DAG + pub parents_hash: Vec, // Handle where events with the time of new blocks are emitted pub new_block_events: EventHandle, - // An Array of the parents hash for a Dag block. - pub parents_hash: Vec, } impl BlockMetadata { diff --git a/vm/types/src/on_chain_resource/global_time.rs b/vm/types/src/on_chain_resource/global_time.rs index 18329c8ed3..ac4840b1c8 100644 --- a/vm/types/src/on_chain_resource/global_time.rs +++ b/vm/types/src/on_chain_resource/global_time.rs @@ -1,13 +1,14 @@ // Copyright (c) The Starcoin Core Contributors // SPDX-License-Identifier: Apache-2.0 -use move_core_types::ident_str; -use move_core_types::identifier::IdentStr; -use move_core_types::move_resource::{MoveResource, MoveStructType}; +use move_core_types::{ + ident_str, + identifier::IdentStr, + move_resource::{MoveResource, MoveStructType}, +}; use serde::{Deserialize, Serialize}; use schemars::JsonSchema; -const TIMESTAMP_MODULE_NAME: &str = "timestamp"; /// The CurrentTimeMilliseconds on chain. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema)] @@ -25,10 +26,14 @@ impl GlobalTimeOnChain { pub fn seconds(&self) -> u64 { self.microseconds / 1000000 } + + pub fn milli_seconds(&self) -> u64 { + self.microseconds / 1000 + } } impl MoveStructType for GlobalTimeOnChain { - const MODULE_NAME: &'static IdentStr = ident_str!(TIMESTAMP_MODULE_NAME); + const MODULE_NAME: &'static IdentStr = ident_str!("timestamp"); const STRUCT_NAME: &'static IdentStr = ident_str!("CurrentTimeMicroseconds"); } diff --git a/vm/types/src/state_view.rs b/vm/types/src/state_view.rs index 56d14f91d7..1d14b035e8 100644 --- a/vm/types/src/state_view.rs +++ b/vm/types/src/state_view.rs @@ -53,6 +53,22 @@ pub trait StateReaderExt: StateView { Ok(rsrc_bytes) } + fn get_resource_type_bytes(&self, address: AccountAddress) -> Result + where + R: MoveResource, + { + Ok(self + .get_state_value_bytes(&StateKey::resource_typed::(&address)?)? + .ok_or_else(|| { + format_err!( + "Resource {:?} {:?} not exists at address:{}", + R::module_identifier(), + R::struct_identifier(), + address + ) + })?) + } + /// Get Resource by type R fn get_resource_type(&self, address: AccountAddress) -> Result where