Skip to content

Commit

Permalink
[move-cli] better explanations when script args don't match parameter…
Browse files Browse the repository at this point in the history
… types

Previously, users saw only TYPE_MISMATCH, NUMBER_OF_TYPE_ARGUMENTS_MISMATCH, or LINKER_ERROR, which are not very descriptive

Closes: #6118
  • Loading branch information
sblackshear committed Sep 23, 2020
1 parent e7b6906 commit 8877d8b
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 173 deletions.
34 changes: 17 additions & 17 deletions json-rpc/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,35 @@ fn create_test_cases() -> Vec<Test> {
resp.result.unwrap(),
json!([
{
"burn_events_key": "05000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "07000000000000000000000000000000000000000a550c18",
"burn_events_key": "06000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "08000000000000000000000000000000000000000a550c18",
"code": "Coin1",
"exchange_rate_update_events_key": "08000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "09000000000000000000000000000000000000000a550c18",
"fractional_part": 100,
"mint_events_key": "04000000000000000000000000000000000000000a550c18",
"preburn_events_key": "06000000000000000000000000000000000000000a550c18",
"mint_events_key": "05000000000000000000000000000000000000000a550c18",
"preburn_events_key": "07000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 0.5
},
{
"burn_events_key": "0a000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "0c000000000000000000000000000000000000000a550c18",
"burn_events_key": "0b000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "0d000000000000000000000000000000000000000a550c18",
"code": "Coin2",
"exchange_rate_update_events_key": "0d000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "0e000000000000000000000000000000000000000a550c18",
"fractional_part": 100,
"mint_events_key": "09000000000000000000000000000000000000000a550c18",
"preburn_events_key": "0b000000000000000000000000000000000000000a550c18",
"mint_events_key": "0a000000000000000000000000000000000000000a550c18",
"preburn_events_key": "0c000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 0.5
},
{
"burn_events_key": "0f000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "11000000000000000000000000000000000000000a550c18",
"burn_events_key": "10000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "12000000000000000000000000000000000000000a550c18",
"code": "LBR",
"exchange_rate_update_events_key": "12000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "13000000000000000000000000000000000000000a550c18",
"fractional_part": 1000,
"mint_events_key": "0e000000000000000000000000000000000000000a550c18",
"preburn_events_key": "10000000000000000000000000000000000000000a550c18",
"mint_events_key": "0f000000000000000000000000000000000000000a550c18",
"preburn_events_key": "11000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 1.0
}
Expand Down Expand Up @@ -106,9 +106,9 @@ fn create_test_cases() -> Vec<Test> {
"delegated_key_rotation_capability": false,
"delegated_withdrawal_capability": false,
"is_frozen": false,
"received_events_key": "00000000000000000000000000000000000000000a550c18",
"received_events_key": "02000000000000000000000000000000000000000a550c18",
"role": { "type": "unknown" },
"sent_events_key": "01000000000000000000000000000000000000000a550c18",
"sent_events_key": "03000000000000000000000000000000000000000a550c18",
"sequence_number": 1
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! proposer: vivian
//! block-time: 1000000

// check: EventKey([21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: EventKey([22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: NewBlockEvent
// check: 1000000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! proposer: vivian
//! block-time: 1000000

// check: EventKey([21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: EventKey([22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: NewBlockEvent
// check: 1000000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fun main(account: &signer) {
//! args: 0, {{bob}}, {{bob::auth_key}}, x"", false

stdlib_script::create_designated_dealer
// check: CreateAccountEvent
// check: "Keep(EXECUTED)"

//! new-transaction
Expand Down
13 changes: 13 additions & 0 deletions language/move-lang/functional-tests/tests/libra/roles/basic.move
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ fun main(account: &signer) {
//! sender: libraroot
script {
use 0x1::Roles;
use 0x1::Signer;
fun main(account: &signer) {
assert(!Roles::has_validator_role(account), 1);
assert(!Roles::has_validator_operator_role(account), 1);
assert(Roles::get_role_id(Signer::address_of(account)) == 0, 1);
}
}
// check: "Keep(EXECUTED)"
Expand All @@ -194,8 +196,19 @@ fun main(account: &signer) {
//! sender: blessed
script {
use 0x1::Roles;
use 0x1::Signer;
fun main(account: &signer) {
assert(Roles::has_treasury_compliance_role(account), 0);
assert(Roles::get_role_id(Signer::address_of(account)) == 1, 1);
}
}
// check: "Keep(EXECUTED)"

//! new-transaction
script {
use 0x1::Roles;
fun main(_account: &signer) {
let _ = Roles::get_role_id(0x7); // does not exist, should abort
}
}
// check: "Keep(ABORTED"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ script {
//! sender: libraroot
//! args: 0, {{alice}}, {{alice::auth_key}}, b"alice"
stdlib_script::create_validator_operator_account
// check: CreateAccountEvent
// check: "Keep(EXECUTED)"

//! new-transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ script{

// register Alice as a validator candidate
fun main(creator: &signer) {
// LibraAccount::create_validator_account(
// creator, &r, 0xAA, x"00000000000000000000000000000000"
LibraAccount::create_validator_account(
creator, 0xAA, x"00000000000000000000000000000000", b"owner_name"
);
}
}

// check: CreateAccountEvent
// check: "Keep(EXECUTED)"

// TODO(valerini): enable the following test once the sender format is supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fun main(lr_account: &signer) {

}
}
// check: CreateAccountEvent
// check: "Keep(EXECUTED)"

// create some child VASP accounts
Expand All @@ -61,6 +62,7 @@ fun main(parent_vasp: &signer) {
assert(VASP::parent_address(0xBB) == {{parent}}, 2014);
}
}
// check: CreateAccountEvent
// check: "Keep(EXECUTED)"

//! new-transaction
Expand Down
Binary file modified language/stdlib/compiled/stdlib/005_Roles.mv
Binary file not shown.
Binary file modified language/stdlib/compiled/stdlib/036_LibraAccount.mv
Binary file not shown.
91 changes: 53 additions & 38 deletions language/stdlib/modules/LibraAccount.move
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module LibraAccount {
/// and to record freeze/unfreeze events.
resource struct AccountOperationsCapability {
limits_cap: AccountLimitMutationCapability,
creation_events: Event::EventHandle<CreateAccountEvent>,
}

/// A resource that holds the event handle for all the past WriteSet transactions that have been committed on chain.
Expand Down Expand Up @@ -123,6 +124,14 @@ module LibraAccount {
writeset_payload: vector<u8>,
}

/// Message for creation of a new account
struct CreateAccountEvent {
/// Address of the created account
created: address,
/// Role of the created account
role_id: u64
}

const MAX_U64: u128 = 18446744073709551615;

/// The `LibraAccount` resource is not in the required state
Expand Down Expand Up @@ -186,40 +195,18 @@ module LibraAccount {
public fun initialize(
lr_account: &signer,
dummy_auth_key_prefix: vector<u8>,
) {
) acquires AccountOperationsCapability {
LibraTimestamp::assert_genesis();
// Operational constraint, not a privilege constraint.
CoreAddresses::assert_libra_root(lr_account);

create_libra_root_account(
copy dummy_auth_key_prefix,
);
// Create the treasury compliance account
create_treasury_compliance_account(
lr_account,
copy dummy_auth_key_prefix,
);

assert(
!exists<AccountOperationsCapability>(CoreAddresses::LIBRA_ROOT_ADDRESS()),
Errors::already_published(EACCOUNT_OPERATIONS_CAPABILITY)
);
move_to(
lr_account,
AccountOperationsCapability {
limits_cap: AccountLimits::grant_mutation_capability(lr_account),
}
);

assert(
!exists<LibraWriteSetManager>(CoreAddresses::LIBRA_ROOT_ADDRESS()),
Errors::already_published(EWRITESET_MANAGER)
);
move_to(
lr_account,
LibraWriteSetManager {
upgrade_events: Event::new_event_handle<Self::UpgradeEvent>(lr_account),
}
);
}

/// Return `true` if `addr` has already published account limits for `Token`
Expand Down Expand Up @@ -895,7 +882,7 @@ module LibraAccount {
fun make_account(
new_account: signer,
auth_key_prefix: vector<u8>,
) {
) acquires AccountOperationsCapability {
let new_account_addr = Signer::address_of(&new_account);
// cannot create an account at the reserved address 0x0
assert(
Expand Down Expand Up @@ -931,6 +918,11 @@ module LibraAccount {
}
);
AccountFreezing::create(&new_account);

Event::emit_event(
&mut borrow_global_mut<AccountOperationsCapability>(CoreAddresses::LIBRA_ROOT_ADDRESS()).creation_events,
CreateAccountEvent { created: new_account_addr, role_id: Roles::get_role_id(new_account_addr) },
);
destroy_signer(new_account);
}

Expand All @@ -943,22 +935,45 @@ module LibraAccount {
/// Creates the libra root account in genesis.
fun create_libra_root_account(
auth_key_prefix: vector<u8>,
) {
) acquires AccountOperationsCapability {
LibraTimestamp::assert_genesis();
let new_account = create_signer(CoreAddresses::LIBRA_ROOT_ADDRESS());
CoreAddresses::assert_libra_root(&new_account);
Roles::grant_libra_root_role(&new_account);
SlidingNonce::publish_nonce_resource(&new_account, &new_account);
Event::publish_generator(&new_account);
make_account(new_account, auth_key_prefix)
let lr_account = create_signer(CoreAddresses::LIBRA_ROOT_ADDRESS());
CoreAddresses::assert_libra_root(&lr_account);
Roles::grant_libra_root_role(&lr_account);
SlidingNonce::publish_nonce_resource(&lr_account, &lr_account);
Event::publish_generator(&lr_account);

assert(
!exists<AccountOperationsCapability>(CoreAddresses::LIBRA_ROOT_ADDRESS()),
Errors::already_published(EACCOUNT_OPERATIONS_CAPABILITY)
);
move_to(
&lr_account,
AccountOperationsCapability {
limits_cap: AccountLimits::grant_mutation_capability(&lr_account),
creation_events: Event::new_event_handle<CreateAccountEvent>(&lr_account),
}
);
assert(
!exists<LibraWriteSetManager>(CoreAddresses::LIBRA_ROOT_ADDRESS()),
Errors::already_published(EWRITESET_MANAGER)
);
move_to(
&lr_account,
LibraWriteSetManager {
upgrade_events: Event::new_event_handle<Self::UpgradeEvent>(&lr_account),
}
);

make_account(lr_account, auth_key_prefix)
}

/// Create a treasury/compliance account at `new_account_address` with authentication key
/// `auth_key_prefix` | `new_account_address`
fun create_treasury_compliance_account(
lr_account: &signer,
auth_key_prefix: vector<u8>,
) {
) acquires AccountOperationsCapability {
LibraTimestamp::assert_genesis();
Roles::assert_libra_root(lr_account);
let new_account_address = CoreAddresses::TREASURY_COMPLIANCE_ADDRESS();
Expand All @@ -982,7 +997,7 @@ module LibraAccount {
auth_key_prefix: vector<u8>,
human_name: vector<u8>,
add_all_currencies: bool,
) {
) acquires AccountOperationsCapability {
let new_dd_account = create_signer(new_account_address);
Event::publish_generator(&new_dd_account);
Roles::new_designated_dealer_role(creator_account, &new_dd_account);
Expand All @@ -1005,7 +1020,7 @@ module LibraAccount {
auth_key_prefix: vector<u8>,
human_name: vector<u8>,
add_all_currencies: bool
) {
) acquires AccountOperationsCapability {
let new_account = create_signer(new_account_address);
Roles::new_parent_vasp_role(creator_account, &new_account);
VASP::publish_parent_vasp_credential(&new_account, creator_account);
Expand All @@ -1024,7 +1039,7 @@ module LibraAccount {
new_account_address: address,
auth_key_prefix: vector<u8>,
add_all_currencies: bool,
) {
) acquires AccountOperationsCapability {
let new_account = create_signer(new_account_address);
Roles::new_child_vasp_role(parent, &new_account);
VASP::publish_child_vasp_credential(
Expand Down Expand Up @@ -1516,7 +1531,7 @@ module LibraAccount {
new_account_address: address,
auth_key_prefix: vector<u8>,
human_name: vector<u8>,
) {
) acquires AccountOperationsCapability {
let new_account = create_signer(new_account_address);
// The lr_account account is verified to have the libra root role in `Roles::new_validator_role`
Roles::new_validator_role(lr_account, &new_account);
Expand All @@ -1530,7 +1545,7 @@ module LibraAccount {
new_account_address: address,
auth_key_prefix: vector<u8>,
human_name: vector<u8>,
) {
) acquires AccountOperationsCapability {
let new_account = create_signer(new_account_address);
// The lr_account is verified to have the libra root role in `Roles::new_validator_operator_role`
Roles::new_validator_operator_role(lr_account, &new_account);
Expand Down
5 changes: 5 additions & 0 deletions language/stdlib/modules/Roles.move
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ module Roles {
has_role(account, CHILD_VASP_ROLE_ID)
}

public fun get_role_id(addr: address): u64 acquires RoleId {
assert(exists<RoleId>(addr), Errors::not_published(EROLE_ID));
borrow_global<RoleId>(addr).role_id
}

/// Return true if `addr` is allowed to receive and send `Libra<T>` for any T
public fun can_hold_balance(account: &signer): bool acquires RoleId {
// VASP accounts and designated_dealers can hold balances.
Expand Down
Loading

0 comments on commit 8877d8b

Please sign in to comment.