Skip to content

Commit

Permalink
[Aptos Framework] Validate against empty execution hash in voting::re…
Browse files Browse the repository at this point in the history
…solve()
  • Loading branch information
movekevin committed Jul 23, 2022
1 parent 914654e commit d95adb6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion aptos-move/aptos-vm/src/move_vm_ext/transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@ fn test_native_get_script_hash(
_args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
let cost = native_gas(context.cost_table(), NativeCostIndex::SHA3_256, 0);
Ok(NativeResult::ok(cost, smallvec![Value::vector_u8(vec![])]))
// TODO: Allow tests to configure the script hash value.
Ok(NativeResult::ok(cost, smallvec![Value::vector_u8(vec![1])]))
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ module aptos_framework::aptos_governance {
create_proposal(
&proposer,
signer::address_of(&proposer),
b"",
b"123",
b"",
b"",
);
Expand Down
39 changes: 36 additions & 3 deletions aptos-move/framework/aptos-framework/sources/voting.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
*/
module aptos_framework::voting {
use std::error;
use aptos_std::event::{Self, EventHandle};
use std::option::{Self, Option};
use std::signer;
use std::vector;

use aptos_std::event::{Self, EventHandle};
use aptos_std::table::{Self, Table};
use aptos_std::type_info::{Self, TypeInfo};

Expand All @@ -36,6 +37,7 @@ module aptos_framework::voting {
const EPROPOSAL_EXECUTION_HASH_NOT_MATCHING: u64 = 1;
const EPROPOSAL_CANNOT_BE_RESOLVED: u64 = 2;
const EPROPOSAL_ALREADY_RESOLVED: u64 = 3;
const EPROPOSAL_EMPTY_EXECUTION_HASH: u64 = 4;

/// ProposalStateEnum representing proposal state.
const PROPOSAL_STATE_PENDING: u64 = 0;
Expand Down Expand Up @@ -161,6 +163,9 @@ module aptos_framework::voting {
expiration_secs: u64,
early_resolution_vote_threshold: Option<u128>,
): u64 acquires VotingForum {
// Make sure the execution script's hash is not empty.
assert!(vector::length(&execution_hash) > 0, error::invalid_argument(EPROPOSAL_EMPTY_EXECUTION_HASH));

let voting_forum = borrow_global_mut<VotingForum<ProposalType>>(voting_forum_address);
let proposal_id = voting_forum.next_proposal_id;
voting_forum.next_proposal_id = voting_forum.next_proposal_id + 1;
Expand Down Expand Up @@ -325,18 +330,25 @@ module aptos_framework::voting {
}

#[test_only]
public fun create_test_proposal(governance: &signer, early_resolution_threshold: Option<u128>): u64 acquires VotingForum {
public fun create_test_proposal(
governance: &signer,
early_resolution_threshold: Option<u128>,
): u64 acquires VotingForum {
// Register voting forum and create a proposal.
register<TestProposal>(governance);
let governance_address = signer::address_of(governance);
let proposal = TestProposal {
code_url: utf8(b"http://mycode.url"),
};

// This works because our Move unit test extensions mock out the execution hash to be [1].
let execution_hash = vector::empty<u8>();
vector::push_back(&mut execution_hash, 1);
let proposal_id = create_proposal<TestProposal>(
governance_address,
governance_address,
proposal,
b"",
execution_hash,
10,
100000,
early_resolution_threshold,
Expand All @@ -345,6 +357,27 @@ module aptos_framework::voting {
proposal_id
}

#[test(governance = @0x123)]
#[expected_failure(abort_code = 0x10004)]
public fun create_proposal_with_empty_execution_hash_should_fail(governance: &signer) acquires VotingForum {
register<TestProposal>(governance);
let governance_address = signer::address_of(governance);
let proposal = TestProposal {
code_url: utf8(b""),
};

// This should fail because execution hash is empty.
create_proposal<TestProposal>(
governance_address,
governance_address,
proposal,
b"",
10,
100000,
option::none<u128>(),
);
}

#[test(aptos_framework = @aptos_framework, governance = @0x123)]
public entry fun test_voting_passed(aptos_framework: signer, governance: signer) acquires VotingForum {
timestamp::set_time_has_started_for_testing(&aptos_framework);
Expand Down

0 comments on commit d95adb6

Please sign in to comment.