aptos-labs / aptos-core

Aptos is a layer 1 blockchain built to support the widespread use of blockchain through better technology and user experience.
https://aptosfoundation.org
Other
5.91k stars 3.58k forks source link

[Bug] High gas consumption observed when creating transactions with multisig_account V2 #13615

Open WGB5445 opened 1 month ago

WGB5445 commented 1 month ago

🐛 Bug > High gas consumption observed when creating transactions with Multi-signature Account V2.

When executing create_transaction_with_hash, I observed that the majority of gas consumption during this transaction process is concentrated in the table's add item operation.

After reviewing the source code, I believe there is significant room for improvement, such as utilizing smart_table or vector (with a limit on the number of pending transactions less than 20) to address this issue.

source code

inline fun add_transaction(
        creator: address,
        multisig_account: address,
        transaction: MultisigTransaction
    ) {
        ....
        simple_map::add(&mut transaction.votes, creator, true);

        let sequence_number = multisig_account_resource.next_sequence_number;
        multisig_account_resource.next_sequence_number = sequence_number + 1;
        table::add(&mut multisig_account_resource.transactions, sequence_number, transaction);
        ...
}

Therefore, I attempted to deploy similar code on the testnet and profiled different consumption patterns.

Data Structure Gas Consumption Transaction Link Profile Link
Table 467 Gas Units Link txn-02011f4d-0x4322-multisig_account_table-create_transaction_with_hash.zip
Smart Table 58 Gas Units Link txn-e7eb8f45-0x4322-multisig_account_smart_table-create_transaction_with_hash.zip
Vector 51 Gas Units Link txn-b1c93318-0x4322-multisig_account_vector-create_transaction_with_hash.zip
rei-alcove commented 1 month ago

Currently the multi-signature relies on the sequence number and can only have a maximum of 20 queued transactions. Using a vector is the most suitable option, but it requires more coding work. Therefore, we believe that directly replacing it with a smart table is the best choice.

movekevin commented 1 month ago

Multisig account was also created prior to SmartTable/SmartVector existing. It might not be easy to change to those data structures due to backward compat problem. We'd need to add a new struct for the SmartTable and then switch between the 2 for existing multisigs