Skip to content

Commit

Permalink
compiler v2 framework 08- Fixed part of integration testsuit (#4293)
Browse files Browse the repository at this point in the history
* [compiler-v2 framework] Fixed some DAO config integration test

* [compiler-v2 framework] Fixed some DAO config integration test

* [compiler-v2 framework] Add all integration tests that without process into repo

* [compiler-v2 framework] add some debug information

* [compiler-v2 framework] Add integration test for test block meta data deserialize code

* [compiler-v2 framework]  fixed the testcase: test_dao_propose.move

* [compiler-v2 framework]  fixed the testcase: test_dao_propose.move

* [compiler-v2 framework]  fixed the testcase: test_dao_failure.move after fixed open block

* [compiler-v2 framework] fixed the block-metadata

* [compiler-v2 framework] fixed on_chain_config

* [compiler-v2 framework] remove unsupport testcase

* [compiler-v2 framework] fixed on_chain_config

* [compiler-v2 framework] fixed fixedpoint32

* [compiler-v2 framework] framework testsuite: fixed debug module

* [compiler-v2 framework] framework testsuite: fixed epoch module

* [compiler-v2 framework] framework testsuite: removed the code implemented by stdlib

* [compiler-v2 framework] framework testsuite: removed the code implemented by stdlib

* [compiler-v2 framework] framework testsuite: fixed natives module

* [compiler-v2 framework] framework testsuite: fixed epilogue module

* [compiler-v2 framework] framework testsuite: fixed oracle module

* [compiler-v2 framework] framework testsuite: fixed oracle module

* [compiler-v2 framework] framework testsuite: removed the code implemented by stdlib

* [compiler-v2 framework] framework testsuite: removed the code implemented by stdlib

* [compiler-v2 framework] framework testsuite: fixed package_txn_manager module

* [compiler-v2 framework] framework testsuite: fixed resource module

* [compiler-v2 framework] framework testsuite: fixed transaction_fee module

* [compiler-v2 framework] fixed some integration-test for merge

* [compiler-v2 framework] test testcase for crytpo changed

* [compiler-v2 framework] framework testsuite: fixed token module test -- join_and_split.move

* [compiler-v2 framework] framework testsuite: fixed `token` module test -- mint_stc.move, my_token.move

* [compiler-v2 framework] framework testsuite: fixed `token` module test

* revert BlockMetadata parent_hash with HashValue

* [compiler-v2 framework] framework testsuite: fixed `treasury` module test

* [compiler-v2 framework] framework testsuite: fixed part of `transaction_scripts` module test

* [compiler-v2 framework] framework testsuite: fixed part of `transaction_scripts` module test

* [compiler-v2 framework] framework testsuite: fixed part of `stc_block.move` module test

---------

Co-authored-by: nk_ysg <[email protected]>
  • Loading branch information
welbon and nkysg authored Nov 25, 2024
1 parent b631b82 commit 7c2be75
Show file tree
Hide file tree
Showing 189 changed files with 11,333 additions and 311 deletions.
82 changes: 41 additions & 41 deletions contrib-contracts/modules/EthStateVerifier.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Bytes {

public fun slice(data: &vector<u8>, start: u64, end: u64): vector<u8> {
let i = start;
let result = Vector::empty<u8>();
let data_len = Vector::length(data);
let result = vector::empty<u8>();
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
Expand All @@ -28,14 +28,14 @@ module RLP {
/// Nested arrays are not supported.
public fun decode_list(data: &vector<u8>): vector<vector<u8>> {
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<u8>, offset: u64): (vector<vector<u8>>, 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);
Expand All @@ -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<u8>, offset: u64, child_offset: u64, length: u64): (vector<vector<u8>>, 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);
};
Expand All @@ -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
Expand All @@ -103,13 +103,13 @@ module EthStateVerifier {
(n1, n2)
}
public fun to_nibbles(bytes: &vector<u8>): vector<u8> {
let result = Vector::empty<u8>();
let result = vector::empty<u8>();
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;
};

Expand All @@ -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,
Expand All @@ -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(
Expand Down
22 changes: 11 additions & 11 deletions contrib-contracts/modules/MerkleDistributor.move
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ module MerkleProof {
public fun verify(proof: &vector<vector<u8>>, root: &vector<u8>, leaf: vector<u8>): 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);
Expand All @@ -45,7 +45,7 @@ module MerkleProof {


fun concat(v1: vector<u8>, v2: vector<u8>): vector<u8> {
Vector::append(&mut v1, v2);
vector::append(&mut v1, v2);
v1
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -129,25 +129,25 @@ module MerkleDistributor {
fun is_claimed_<T: store>(distribution: &MerkleDistribution<T>, 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
}

fun set_claimed_<T: store>(distribution: &mut MerkleDistribution<T>, 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<u8> {
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
}
}
Expand Down
8 changes: 4 additions & 4 deletions contrib-contracts/modules/StarcoinVerifier.move
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -71,7 +71,7 @@ address StarcoinAssociation {

fun concat(v1: &vector<u8>, v2: vector<u8>): vector<u8> {
let data = *v1;
Vector::append(&mut data, v2);
vector::append(&mut data, v2);
data
}

Expand All @@ -81,7 +81,7 @@ address StarcoinAssociation {
public fun get_bit(data: &vector<u8>, 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
}
}
}
55 changes: 55 additions & 0 deletions executor/tests/block_test.rs
Original file line number Diff line number Diff line change
@@ -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::<BlockMetadata>(genesis_address())?;
info!(
"onchain block_metadata: {:?}, data len: {}",
onchain_data.to_vec(),
onchain_data.len()
);
let on_chain_block_data =
bcs_ext::from_bytes::<BlockMetadata>(onchain_data.to_vec().as_slice())?;
assert_eq!(on_chain_block_data.number, 0);

assert_eq!(
chain_state
.get_resource_type::<BlockMetadata>(genesis_address())?
.number,
0
);

// let output = bcs_ext::from_bytes::<BlockMetadata>(bcs.as_slice())?;
// assert_eq!(output.number, block_metadata.number);
//
// let data = chain_state.get_resource_type::<BlockMetadata>(genesis_address())?;
// assert_ne!(data.number, 0);
// assert!(!block_metadata.number > 0);

Ok(())
}
Binary file added release/basic.v0.1.0.blob
Binary file not shown.
Loading

0 comments on commit 7c2be75

Please sign in to comment.