From 7743d0ecb0b4b39d681f923880dd708e771321c1 Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Wed, 15 Jan 2025 09:36:32 +0800 Subject: [PATCH] [merkle-distributor] fixed the compiler error of test --- .../modules/MerkleDistributor.move | 83 +++++++++++++------ .../src/merkle_distributor_test.rs | 2 +- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/contrib-contracts/modules/MerkleDistributor.move b/contrib-contracts/modules/MerkleDistributor.move index f49ff44ccf..fa135a0404 100644 --- a/contrib-contracts/modules/MerkleDistributor.move +++ b/contrib-contracts/modules/MerkleDistributor.move @@ -1,24 +1,38 @@ -address StarcoinAssociation { -module MerkleDistributorScripts { +module StarcoinAssociation::MerkleDistributorScripts { use StarcoinAssociation::MerkleDistributor; use starcoin_framework::coin; use starcoin_std::signer; - public entry fun create(signer: signer, merkle_root: vector, coin_amounts: u64, leaves: u64) { + + public entry fun create(signer: signer, merkle_root: vector, coin_amounts: u64, leaves: u64) { let coins = coin::withdraw(&signer, coin_amounts); MerkleDistributor::create(&signer, merkle_root, coins, leaves); } - public entry fun claim_for_address(signer: signer, distribution_address: address, index: u64, account: address, amount: u64, merkle_proof: vector>) { + public entry fun claim_for_address( + signer: signer, + distribution_address: address, + index: u64, + account: address, + amount: u64, + merkle_proof: vector> + ) { MerkleDistributor::claim_for_address(&signer, distribution_address, index, account, amount, merkle_proof); } - public entry fun claim(signer: signer, distribution_address: address, index: u64, amount: u64, merkle_proof: vector>) { + + public entry fun claim( + signer: signer, + distribution_address: address, + index: u64, + amount: u64, + merkle_proof: vector> + ) { let coins = MerkleDistributor::claim(&signer, distribution_address, index, amount, merkle_proof); let account_addr = signer::address_of(&signer); coin::deposit(account_addr, coins); } } -module MerkleProof { +module StarcoinAssociation::MerkleProof { use std::hash; use std::vector; use starcoin_std::comparator; @@ -28,19 +42,18 @@ module MerkleProof { let computed_hash = leaf; let i = 0; let proof_length = vector::length(proof); - while(i < proof_length) { + while (i < proof_length) { let sibling = vector::borrow(proof, i); // computed_hash is left. - if (!comparator::is_greater_than(&comparator::compare_u8_vector(computed_hash,*sibling))) { + if (!comparator::is_greater_than(&comparator::compare_u8_vector(computed_hash, *sibling))) { let concated = concat(computed_hash, *sibling); computed_hash = hash::sha3_256(concated); } else { let concated = concat(*sibling, computed_hash); computed_hash = hash::sha3_256(concated); - }; - i = i+1; + i = i + 1; }; &computed_hash == root } @@ -53,27 +66,27 @@ module MerkleProof { } - -module MerkleDistributor { +module StarcoinAssociation::MerkleDistributor { use StarcoinAssociation::MerkleProof; - use std::bcs; use starcoin_framework::coin; + use std::bcs; use std::error; use std::hash; use std::vector; use starcoin_std::signer; - struct MerkleDistribution has key { + struct MerkleDistribution has key { merkle_root: vector, coins: coin::Coin, claimed_bitmap: vector, } + const INVALID_PROOF: u64 = 1; const ALREADY_CLAIMED: u64 = 2; /// Initialization. - public fun create(signer: &signer, merkle_root: vector, coins: coin::Coin, leaves: u64) { + public fun create(signer: &signer, merkle_root: vector, coins: coin::Coin, leaves: u64) { let bitmap_count = leaves / 64; if (bitmap_count * 64 < leaves) { bitmap_count = bitmap_count + 1; @@ -84,7 +97,7 @@ module MerkleDistributor { vector::push_back(&mut claimed_bitmap, 0u64); j = j + 1; }; - let distribution = MerkleDistribution{ + let distribution = MerkleDistribution { merkle_root, coins, claimed_bitmap, @@ -93,29 +106,48 @@ module MerkleDistributor { } /// claim for some address. - public fun claim_for_address(signer: &signer, distribution_address: address, index: u64, account: address, amount: u64, merkle_proof: vector>) - acquires MerkleDistribution { + public fun claim_for_address( + signer: &signer, + distribution_address: address, + index: u64, + account: address, + amount: u64, + merkle_proof: vector> + ) + acquires MerkleDistribution { let distribution = borrow_global_mut>(distribution_address); let claimed_coins = internal_claim(signer, distribution, index, account, amount, merkle_proof); coin::deposit(account, claimed_coins); } /// claim by myself. - public fun claim(signer: &signer, distribution_address: address, index: u64, amount: u64, merkle_proof: vector>): coin::Coin - acquires MerkleDistribution { + public fun claim( + signer: &signer, + distribution_address: address, + index: u64, + amount: u64, + merkle_proof: vector> + ): coin::Coin acquires MerkleDistribution { let distribution = borrow_global_mut>(distribution_address); internal_claim(signer, distribution, index, signer::address_of(signer), amount, merkle_proof) } /// Query whether `index` of `distribution_address` has already claimed. - public fun is_claimed(distribution_address: address, index: u64): bool + public fun is_claimed(distribution_address: address, index: u64): bool acquires MerkleDistribution { let distribution = borrow_global>(distribution_address); is_claimed_(distribution, index) } - fun internal_claim(signer: &signer, distribution: &mut MerkleDistribution, index: u64, account: address, amount: u64, merkle_proof: vector>): coin::Coin { - let claimed = is_claimed_(distribution, index); + fun internal_claim( + signer: &signer, + distribution: &mut MerkleDistribution, + index: u64, + account: address, + amount: u64, + merkle_proof: vector> + ): coin::Coin { + let claimed = is_claimed_(distribution, index); assert!(!claimed, error::invalid_argument(ALREADY_CLAIMED)); let leaf_data = encode_leaf(&index, &account, &amount); @@ -127,7 +159,7 @@ module MerkleDistributor { coin::withdraw(signer, amount) } - fun is_claimed_(distribution: &MerkleDistribution, index: u64): bool { + fun is_claimed_(distribution: &MerkleDistribution, index: u64): bool { let claimed_word_index = index / 64; let claimed_bit_index = ((index % 64) as u8); let word = vector::borrow(&distribution.claimed_bitmap, claimed_word_index); @@ -135,7 +167,7 @@ module MerkleDistributor { (*word & mask) == mask } - fun set_claimed_(distribution: &mut MerkleDistribution, index: u64) { + fun set_claimed_(distribution: &mut MerkleDistribution, index: u64) { let claimed_word_index = index / 64; let claimed_bit_index = ((index % 64) as u8); let word = vector::borrow_mut(&mut distribution.claimed_bitmap, claimed_word_index); @@ -151,5 +183,4 @@ module MerkleDistributor { vector::append(&mut leaf, bcs::to_bytes(amount)); leaf } -} } \ No newline at end of file diff --git a/contrib-contracts/src/merkle_distributor_test.rs b/contrib-contracts/src/merkle_distributor_test.rs index 4473a6c955..259e5ef10f 100644 --- a/contrib-contracts/src/merkle_distributor_test.rs +++ b/contrib-contracts/src/merkle_distributor_test.rs @@ -65,7 +65,7 @@ fn test_merkle_distributor() -> Result<()> { Identifier::new("MerkleDistributorScripts").unwrap(), ), Identifier::new("create").unwrap(), - vec![], + vec![stc_type_tag()], vec![ merkle_root.simple_serialize().unwrap(), rewards_total.simple_serialize().unwrap(),