Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[compiler-v2]fix crate contrib contracts test(ignore test_merkle_distributor) #4341

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions cmd/merkle-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ The generated json file should be same as `examples/merkle-exmaple.json`.
2. Then create a distribution onchain.

``` move
public(script) fun create<T: store>(signer: signer, merkle_root: vector<u8>, token_amounts: u128, leafs: u64);
public entry fun create<T: store>(signer: signer, merkle_root: vector<u8>, token_amounts: u128, leafs: u64);
```

3. User claim

``` move
// claim from myslef.
public(script) fun claim<T: store>(signer: signer, distribution_address: address, index: u64, amount: u128, merkle_proof: vector<vector<u8>>);
public entry fun claim<T: store>(signer: signer, distribution_address: address, index: u64, amount: u128, merkle_proof: vector<vector<u8>>);

// claim for someone else.
public(script) fun claim_for_address<T: store>(distribution_address: address, index: u64, account: address, amount: u128, merkle_proof: vector<vector<u8>>);
public entry fun claim_for_address<T: store>(distribution_address: address, index: u64, account: address, amount: u128, merkle_proof: vector<vector<u8>>);
```
1 change: 1 addition & 0 deletions contrib-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
stdlib = { workspace = true }
stest = { workspace = true }
starcoin-move-stdlib = { workspace = true }
tempfile = { workspace = true }

[package]
Expand Down
12 changes: 6 additions & 6 deletions contrib-contracts/modules/EthStateVerifier.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
address StarcoinAssociation {
module Bytes {
use StarcoinFramework::Vector;
use std::vector;

public fun slice(data: &vector<u8>, start: u64, end: u64): vector<u8> {
let i = start;
Expand All @@ -19,7 +19,7 @@ module Bytes {
}
}
module RLP {
use StarcoinFramework::Vector;
use std::vector;
use StarcoinAssociation::Bytes;
const INVALID_RLP_DATA: u64 = 100;
const DATA_TOO_SHORT: u64 = 101;
Expand Down Expand Up @@ -91,8 +91,8 @@ module RLP {
}
module EthStateVerifier {
use StarcoinAssociation::RLP;
use StarcoinFramework::Vector;
use StarcoinFramework::Hash;
use std::vector;
use starcoin_std::starcoin_hash;
use StarcoinAssociation::Bytes;

const INVALID_PROOF: u64 = 400;
Expand Down Expand Up @@ -132,7 +132,7 @@ module EthStateVerifier {
let dec = RLP::decode_list(node);
// trie root is always a hash
if (key_index == 0 || vector::length(node) >= 32u64) {
if (Hash::keccak_256(*node) != expected_root) {
if (starcoin_hash::keccak256(*node) != expected_root) {
return false
}
} else {
Expand Down Expand Up @@ -210,7 +210,7 @@ module EthStateVerifier {
proof: vector<vector<u8>>,
expected_value: vector<u8>,
): bool {
let hashed_key = Hash::keccak_256(key);
let hashed_key = starcoin_hash::keccak256(key);
let key = to_nibbles(&hashed_key);
return verify_inner(expected_root, key, proof, expected_value, 0, 0)
}
Expand Down
22 changes: 11 additions & 11 deletions contrib-contracts/modules/StarcoinVerifier.move
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
address StarcoinAssociation {
module StarcoinVerifierScripts {
use StarcoinAssociation::StarcoinVerifier;
public(script) fun create(signer: signer, merkle_root: vector<u8>) {
public entry fun create(signer: signer, merkle_root: vector<u8>) {
StarcoinVerifier::create(&signer, merkle_root);
}
}
module StarcoinVerifier {
use StarcoinFramework::Vector;
use std::vector;
use StarcoinAssociation::Bit;
use StarcoinAssociation::StructuredHash;
use StarcoinFramework::Hash;
use std::hash;

struct StarcoinMerkle has key {
merkle_root: vector<u8>,
Expand Down Expand Up @@ -38,7 +38,7 @@ address StarcoinAssociation {
}

public fun verify(expected_root: vector<u8>, account_address: vector<u8>, account_state_root_hash: vector<u8>, proofs: vector<vector<u8>>): bool {
let address_hash = Hash::sha3_256(account_address);
let address_hash = hash::sha3_256(account_address);
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;
Expand All @@ -59,14 +59,14 @@ address StarcoinAssociation {
}

module StructuredHash {
use StarcoinFramework::Hash;
use StarcoinFramework::Vector;
use StarcoinFramework::BCS;
use std::hash;
use std::vector;
use std::bcs;
const STARCOIN_HASH_PREFIX: vector<u8> = b"STARCOIN::";
public fun hash<MoveValue: store>(structure: vector<u8>, data: &MoveValue): vector<u8> {
let prefix_hash = Hash::sha3_256(concat(&STARCOIN_HASH_PREFIX, structure));
let bcs_bytes = BCS::to_bytes(data);
Hash::sha3_256(concat(&prefix_hash, bcs_bytes))
let prefix_hash = hash::sha3_256(concat(&STARCOIN_HASH_PREFIX, structure));
let bcs_bytes = bcs::to_bytes(data);
hash::sha3_256(concat(&prefix_hash, bcs_bytes))
}

fun concat(v1: &vector<u8>, v2: vector<u8>): vector<u8> {
Expand All @@ -77,7 +77,7 @@ address StarcoinAssociation {

}
module Bit {
use StarcoinFramework::Vector;
use std::vector;
public fun get_bit(data: &vector<u8>, index: u64): bool {
let pos = index / 8;
let bit = (7 - index % 8);
Expand Down
7 changes: 5 additions & 2 deletions contrib-contracts/src/eth_state_verifier_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starcoin_types::transaction::TransactionPayload;
use starcoin_vm_types::transaction::Package;
use starcoin_vm_types::value::MoveValue;
use test_helper::executor::{
association_execute_should_success, compile_modules_with_address, prepare_genesis,
association_execute_should_success, compile_modules_with_address_ext, prepare_genesis,
};

/// Basic account type.
Expand Down Expand Up @@ -66,7 +66,10 @@ fn test_eth_state_proof_verify() -> Result<()> {
// deploy the module
{
let source = include_str!("../../modules/EthStateVerifier.move");
let modules = compile_modules_with_address(association_address(), source);
let mut dep_libs = starcoin_move_stdlib::move_stdlib_files();
let starcoin_stdlib_files = starcoin_move_stdlib::starcoin_stdlib_files();
dep_libs.extend(starcoin_stdlib_files);
let modules = compile_modules_with_address_ext(association_address(), source, &dep_libs);

let package = Package::new(modules, None)?;
association_execute_should_success(
Expand Down
2 changes: 2 additions & 0 deletions contrib-contracts/src/genesis_nft_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct DataProof {
proof: Vec<String>,
}

// XXX FIXME BOB wait nft
#[ignore]
#[stest::test]
fn test_genesis_nft_verify() -> Result<()> {
assert!(verify_genesis_nft_address(genesis_address())?);
Expand Down
9 changes: 7 additions & 2 deletions contrib-contracts/src/merkle_distributor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starcoin_vm_types::token::stc::stc_type_tag;
use starcoin_vm_types::transaction::{EntryFunction, Package, TransactionPayload};
use starcoin_vm_types::value::MoveValue;
use test_helper::executor::{
association_execute, association_execute_should_success, compile_modules_with_address,
association_execute, association_execute_should_success, compile_modules_with_address_ext,
move_abort_code, prepare_genesis,
};

Expand All @@ -23,6 +23,8 @@ struct DataProof {
proof: Vec<String>,
}

// XXX FIXME YSG next pr
#[ignore]
#[stest::test]
fn test_merkle_distributor() -> Result<()> {
let association = Account::new_association();
Expand All @@ -37,7 +39,10 @@ fn test_merkle_distributor() -> Result<()> {
// deploy the module
{
let source = include_str!("../modules/MerkleDistributor.move");
let modules = compile_modules_with_address(association_address(), source);
let mut dep_libs = starcoin_move_stdlib::move_stdlib_files();
let starcoin_stdlib_files = starcoin_move_stdlib::starcoin_stdlib_files();
dep_libs.extend(starcoin_stdlib_files);
let modules = compile_modules_with_address_ext(association_address(), source, &dep_libs);

let package = Package::new(modules, None)?;
association_execute_should_success(
Expand Down
8 changes: 6 additions & 2 deletions contrib-contracts/src/starcoin_merkle_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starcoin_vm_types::state_store::state_key::StateKey;
use starcoin_vm_types::transaction::{EntryFunction, Package, TransactionPayload};
use starcoin_vm_types::value::MoveValue;
use test_helper::executor::{
association_execute_should_success, compile_modules_with_address, prepare_genesis,
association_execute_should_success, compile_modules_with_address_ext, prepare_genesis,
};
#[stest::test]
fn test_starcoin_merkle() -> Result<()> {
Expand All @@ -27,7 +27,11 @@ fn test_starcoin_merkle() -> Result<()> {

{
let source = include_str!("../modules/StarcoinVerifier.move");
let modules = compile_modules_with_address(association_address(), source);
let modules = compile_modules_with_address_ext(
association_address(),
source,
&starcoin_move_stdlib::move_stdlib_files(),
);

let package = Package::new(modules, None)?;
association_execute_should_success(
Expand Down
1 change: 0 additions & 1 deletion scripts/nextest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ cargo nextest -V >/dev/null 2>&1 || cargo install cargo-nextest --version "0.9.5
cargo nextest run --workspace \
--exclude starcoin-transactional-test-harness \
--exclude starcoin-framework \
--exclude contrib-contracts \
--exclude starcoin-consensus \
--retries 2 --build-jobs 8 --test-threads 12 --no-fail-fast --failure-output immediate-final

Expand Down
11 changes: 9 additions & 2 deletions test-helper/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,16 @@ pub fn get_balance<S: ChainStateReader>(address: AccountAddress, chain_state: &S
}

pub fn compile_modules_with_address(address: AccountAddress, code: &str) -> Vec<Module> {
compile_modules_with_address_ext(address, code, &stdlib_files())
}

pub fn compile_modules_with_address_ext(
address: AccountAddress,
code: &str,
libs: &[String],
) -> Vec<Module> {
let (_, compiled_result) =
starcoin_move_compiler::compile_source_string(code, &stdlib_files(), address)
.expect("compile fail");
starcoin_move_compiler::compile_source_string(code, libs, address).expect("compile fail");

compiled_result
.into_iter()
Expand Down
1 change: 1 addition & 0 deletions vm/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ starcoin-logger = { workspace = true }
starcoin-vm-types = { workspace = true }
tempfile = { workspace = true }
walkdir = { workspace = true }
starcoin-framework = { workspace = true }

[dev-dependencies]
stest = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions vm/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use anyhow::{bail, ensure, Result};
use move_binary_format::errors::PartialVMResult;
use move_compiler::compiled_unit::AnnotatedCompiledUnit;
use move_compiler::diagnostics::{unwrap_or_report_diagnostics, Diagnostics, FilesSourceText};
use move_compiler::shared::known_attributes::KnownAttribute;
use move_compiler::shared::{Flags, NumericalAddress};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
Expand Down Expand Up @@ -55,6 +54,8 @@ pub fn starcoin_framework_named_addresses() -> BTreeMap<String, NumericalAddress
("Genesis", "0x1"),
("StarcoinFramework", "0x1"),
("StarcoinAssociation", "0xA550C18"),
("std", "0x1"),
("starcoin_std", "0x1"),
];
mapping
.iter()
Expand Down Expand Up @@ -189,13 +190,12 @@ pub fn compile_source_string_no_report(
for dep in deps {
windows_line_ending_to_unix_in_file(dep)?;
}
let flags = Flags::empty().set_sources_shadow_deps(true);
let compiler = move_compiler::Compiler::from_files(
targets,
deps.to_vec(),
starcoin_framework_named_addresses(),
flags,
KnownAttribute::get_all_attribute_names(),
Flags::empty().set_sources_shadow_deps(false),
starcoin_framework::extended_checks::get_all_attribute_names(),
);
compiler.build()
}
Expand Down
15 changes: 10 additions & 5 deletions vm/framework/move-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
starcoin-gas-schedule = { workspace = true }
starcoin-native-interface = { workspace = true }
anyhow = { workspace = true }
include_dir = { workspace = true }
move-core-types = { workspace = true }
move-vm-runtime = { workspace = true }
move-vm-types = { workspace = true }
sha2 = "0.9.3"
sha3 = "0.9.1"
smallvec = "1.6.1"
once_cell = { workspace = true }
sha2 = { workspace = true }
sha3 = { workspace = true }
smallvec = { workspace = true }
starcoin-gas-schedule = { workspace = true }
starcoin-logger = { workspace = true }
starcoin-native-interface = { workspace = true }
tempfile = { workspace = true }

[dev-dependencies]
dir-diff = "0.3.2"
Expand Down
Loading
Loading