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

Enhance Transaction Simulation: Bypass Auth Key Check and Fee Payment, Improve Multisig Consistency #13714

Merged
merged 1 commit into from
Aug 21, 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
32 changes: 31 additions & 1 deletion api/doc/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -13968,6 +13968,9 @@
},
{
"$ref": "#/components/schemas/AccountSignature_MultiKeySignature"
},
{
"$ref": "#/components/schemas/AccountSignature_NoAccountSignature"
}
],
"discriminator": {
Expand All @@ -13976,7 +13979,8 @@
"ed25519_signature": "#/components/schemas/AccountSignature_Ed25519Signature",
"multi_ed25519_signature": "#/components/schemas/AccountSignature_MultiEd25519Signature",
"single_key_signature": "#/components/schemas/AccountSignature_SingleKeySignature",
"multi_key_signature": "#/components/schemas/AccountSignature_MultiKeySignature"
"multi_key_signature": "#/components/schemas/AccountSignature_MultiKeySignature",
"no_account_signature": "#/components/schemas/AccountSignature_NoAccountSignature"
}
}
},
Expand Down Expand Up @@ -14046,6 +14050,28 @@
}
]
},
"AccountSignature_NoAccountSignature": {
"allOf": [
{
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"enum": [
"no_account_signature"
],
"example": "no_account_signature"
}
}
},
{
"$ref": "#/components/schemas/NoAccountSignature"
}
]
},
"AccountSignature_SingleKeySignature": {
"allOf": [
{
Expand Down Expand Up @@ -15582,6 +15608,10 @@
}
]
},
"NoAccountSignature": {
"type": "object",
"description": "A placeholder to represent the absence of account signature"
},
"PendingTransaction": {
"type": "object",
"description": "A transaction waiting in mempool",
Expand Down
17 changes: 17 additions & 0 deletions api/doc/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10453,13 +10453,15 @@ components:
- $ref: '#/components/schemas/AccountSignature_MultiEd25519Signature'
- $ref: '#/components/schemas/AccountSignature_SingleKeySignature'
- $ref: '#/components/schemas/AccountSignature_MultiKeySignature'
- $ref: '#/components/schemas/AccountSignature_NoAccountSignature'
discriminator:
propertyName: type
mapping:
ed25519_signature: '#/components/schemas/AccountSignature_Ed25519Signature'
multi_ed25519_signature: '#/components/schemas/AccountSignature_MultiEd25519Signature'
single_key_signature: '#/components/schemas/AccountSignature_SingleKeySignature'
multi_key_signature: '#/components/schemas/AccountSignature_MultiKeySignature'
no_account_signature: '#/components/schemas/AccountSignature_NoAccountSignature'
AccountSignature_Ed25519Signature:
allOf:
- type: object
Expand Down Expand Up @@ -10496,6 +10498,18 @@ components:
- multi_key_signature
example: multi_key_signature
- $ref: '#/components/schemas/MultiKeySignature'
AccountSignature_NoAccountSignature:
allOf:
- type: object
required:
- type
properties:
type:
type: string
enum:
- no_account_signature
example: no_account_signature
- $ref: '#/components/schemas/NoAccountSignature'
AccountSignature_SingleKeySignature:
allOf:
- type: object
Expand Down Expand Up @@ -11702,6 +11716,9 @@ components:
- entry_function_payload
example: entry_function_payload
- $ref: '#/components/schemas/EntryFunctionPayload'
NoAccountSignature:
type: object
description: A placeholder to represent the absence of account signature
PendingTransaction:
type: object
description: A transaction waiting in mempool
Expand Down
135 changes: 133 additions & 2 deletions api/src/tests/multisig_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,54 @@ async fn test_multisig_transaction_with_mismatching_payload() {

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_multisig_transaction_simulation() {
let mut context = new_test_context(current_function_name!());
let owner_account_1 = &mut context.create_account().await;
let owner_account_2 = &mut context.create_account().await;
let owner_account_3 = &mut context.create_account().await;
let multisig_account = context
.create_multisig_account(
owner_account_1,
vec![owner_account_2.address(), owner_account_3.address()],
1, /* 1-of-3 */
1000, /* initial balance */
)
.await;

let multisig_payload = construct_multisig_txn_transfer_payload(owner_account_1.address(), 1000);
context
.create_multisig_transaction(owner_account_1, multisig_account, multisig_payload.clone())
.await;

// Simulate the multisig tx
let simulation_resp = context
.simulate_multisig_transaction(
owner_account_1,
multisig_account,
"0x1::aptos_account::transfer",
&[],
&[&owner_account_1.address().to_hex_literal(), "1000"],
200,
)
.await;
// Validate that the simulation did successfully execute a transfer of 1000 coins from the
// multisig account.
let simulation_resp = &simulation_resp.as_array().unwrap()[0];
assert!(simulation_resp["success"].as_bool().unwrap());
let withdraw_event = &simulation_resp["events"].as_array().unwrap()[0];
assert_eq!(
withdraw_event["type"].as_str().unwrap(),
"0x1::coin::CoinWithdraw"
);
let withdraw_from_account =
AccountAddress::from_hex_literal(withdraw_event["data"]["account"].as_str().unwrap())
.unwrap();
let withdrawn_amount = withdraw_event["data"]["amount"].as_str().unwrap();
assert_eq!(withdraw_from_account, multisig_account);
assert_eq!(withdrawn_amount, "1000");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_multisig_transaction_simulation_2_of_3() {
let mut context = new_test_context(current_function_name!());
let owner_account_1 = &mut context.create_account().await;
let owner_account_2 = &mut context.create_account().await;
Expand All @@ -416,8 +464,16 @@ async fn test_multisig_transaction_simulation() {
)
.await;

// Should be able to simulate the multisig tx without having enough approvals or the transaction
// created.
let multisig_payload = construct_multisig_txn_transfer_payload(owner_account_1.address(), 1000);
context
.create_multisig_transaction(owner_account_1, multisig_account, multisig_payload.clone())
.await;

context
.approve_multisig_transaction(owner_account_2, multisig_account, 1)
.await;

// Simulate the multisig transaction
let simulation_resp = context
.simulate_multisig_transaction(
owner_account_1,
Expand All @@ -443,6 +499,27 @@ async fn test_multisig_transaction_simulation() {
let withdrawn_amount = withdraw_event["data"]["amount"].as_str().unwrap();
assert_eq!(withdraw_from_account, multisig_account);
assert_eq!(withdrawn_amount, "1000");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_multisig_transaction_simulation_fail() {
let mut context = new_test_context(current_function_name!());
let owner_account_1 = &mut context.create_account().await;
let owner_account_2 = &mut context.create_account().await;
let owner_account_3 = &mut context.create_account().await;
let multisig_account = context
.create_multisig_account(
owner_account_1,
vec![owner_account_2.address(), owner_account_3.address()],
1, /* 1-of-3 */
1000, /* initial balance */
)
.await;

let multisig_payload = construct_multisig_txn_transfer_payload(owner_account_1.address(), 2000);
context
.create_multisig_transaction(owner_account_1, multisig_account, multisig_payload.clone())
.await;

// Simulating transferring more than what the multisig account has should fail.
let simulation_resp = context
Expand All @@ -456,7 +533,56 @@ async fn test_multisig_transaction_simulation() {
)
.await;
let simulation_resp = &simulation_resp.as_array().unwrap()[0];
let transaction_failed = &simulation_resp["events"]
.as_array()
.unwrap()
.iter()
.any(|event| {
event["type"]
.as_str()
.unwrap()
.contains("TransactionExecutionFailed")
});
assert!(transaction_failed);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_multisig_transaction_simulation_fail_2_of_3_insufficient_approvals() {
let mut context = new_test_context(current_function_name!());
let owner_account_1 = &mut context.create_account().await;
let owner_account_2 = &mut context.create_account().await;
let owner_account_3 = &mut context.create_account().await;
let multisig_account = context
.create_multisig_account(
owner_account_1,
vec![owner_account_2.address(), owner_account_3.address()],
2, /* 2-of-3 */
1000, /* initial balance */
)
.await;

let multisig_payload = construct_multisig_txn_transfer_payload(owner_account_1.address(), 2000);
context
.create_multisig_transaction(owner_account_1, multisig_account, multisig_payload.clone())
.await;

// Simulating without sufficient approvals has should fail.
let simulation_resp = context
.simulate_multisig_transaction(
owner_account_1,
multisig_account,
"0x1::aptos_account::transfer",
&[],
&[&owner_account_1.address().to_hex_literal(), "1000"],
200,
)
.await;
let simulation_resp = &simulation_resp.as_array().unwrap()[0];
assert!(!simulation_resp["success"].as_bool().unwrap());
assert!(simulation_resp["vm_status"]
.as_str()
.unwrap()
.contains("MULTISIG_TRANSACTION_INSUFFICIENT_APPROVALS"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
Expand All @@ -473,6 +599,11 @@ async fn test_simulate_multisig_transaction_should_charge_gas_against_sender() {
.await;
assert_eq!(10, context.get_apt_balance(multisig_account).await);

let multisig_payload = construct_multisig_txn_transfer_payload(owner_account.address(), 10);
context
.create_multisig_transaction(owner_account, multisig_account, multisig_payload.clone())
.await;

// This simulation should succeed because gas should be paid out of the sender account (owner),
// not the multisig account itself.
let simulation_resp = context
Expand Down
Loading
Loading