logo

Transactions

Overview#

Transactions play a critical role in modifying the state of the aelf blockchain through interactions with smart contracts. Here’s how transactions function:

  • Transaction Process:
  • Transactions are initiated by calling methods on smart contracts.
  • They are either sent to a node through RPC (Remote Procedure Call) or received from the network.
  • Transaction Execution:
  • Upon broadcasting a transaction, if it meets validation criteria, it becomes eligible for inclusion in a block.
  • Once included in a block and executed by the node, the transaction potentially alters the state of the involved contracts.
  • In essence, transactions in aelf blockchain are fundamental actions that drive changes to the blockchain’s state, ensuring secure and efficient operation across the network.

    Smart Contract#

    In the aelf blockchain, smart contracts consist of two main components:

  • State Definitions:
  • Smart contracts define a set of variables that represent their state.
  • These variables can store data such as account balances, token ownership, or any other relevant information.
  • Methods:
  • Smart contracts include a set of methods designed to interact with and modify these states.
  • These methods are executed when triggered by transactions or external calls.
  • They can update the state variables based on predefined rules and conditions.
  • Smart contracts in aelf blockchain serve as self-executing contracts with predefined terms and conditions agreed upon by participating parties. They enable automated and trustless execution of agreements and transactions within the blockchain ecosystem.

    Action & View#

    In the aelf blockchain, smart contract methods are categorized into two types based on their functionality: actions and views.

  • Action Methods:
  • Definition: Action methods are designed to modify the state of a contract.
  • Execution: When a transaction containing an action method is included in a block and successfully executed, it can change the internal state of the contract.
  • Example#

    1
    rpc Vote (VoteInput) returns (google.protobuf.Empty) {
    2
    }
  • Action methods are typically used for operations that alter data within the blockchain, such as updating balances, transferring tokens, or recording votes.
  • View Methods:
  • Definition: View methods retrieve data from the contract but do not modify its state under any circumstances.
  • Execution: They are purely read-only operations.
  • Example#
    1
    rpc GetVotingResult (GetVotingResultInput) returns (VotingResult) {
    2
    option (aelf.is_view) = true;
    3
    }
  • View methods are annotated with option (aelf.is_view) = true; in their protocol buffer (proto) definitions.
  • They are used for querying information stored in the contract, such as retrieving voting results, fetching account details, or checking contract status.
  • These distinctions ensure that smart contracts in aelf blockchain can effectively manage state changes while maintaining secure and efficient data access.

    Transaction Instance#

    In the aelf blockchain, a Transaction is structured as follows:

    1
    option csharp_namespace = "AElf.Types";
    2
    3
    message Transaction {
    4
    Address from = 1;
    5
    Address to = 2;
    6
    int64 ref_block_number = 3;
    7
    bytes ref_block_prefix = 4;
    8
    string method_name = 5;
    9
    bytes params = 6;
    10
    bytes signature = 10000;
    11
    }

    Working with Transactions in JS SDK#

    Use the getTransaction method to build a transaction object:

    1
    import Aelf from 'aelf-sdk';
    2
    3
    var rawTxn = proto.getTransaction(
    4
    '65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9',
    5
    'SomeMethod',
    6
    encodedParams
    7
    );

    This will create a transaction to the contract at address 65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9 and call SomeMethod with encoded parameters.

    Key Fields in Transactions#

  • From: The sender's address. Currently derived from the signature.
  • To: The contract address being called.
  • MethodName: The name of the method in the smart contract.
  • Params: The parameters to pass to the method.
  • Signature: The signature of the transaction, which includes fields like from, to, method, parameters, reference block number, and prefix.
  • Sign a transaction using the JS SDK:

    1
    import Aelf from 'aelf-sdk';
    2
    3
    var txn = Aelf.wallet.signTransaction(rawTxn, wallet.keyPair);
  • RefBlockNumber & RefBlockPrefix: Ensure the transaction is not expired. These two fields measure whether this transaction has expired. The transaction will be discarded if it is too old.
  • Transaction Id: A unique identifier for the transaction, consisting of a cryptographic hash of the essential fields, excluding the signature.
  • Note that the Transaction Id of transactions will be the same if the sender broadcasted several transactions with the same origin data, and then these transactions will be regarded as one transaction even though broadcasting several times.

    Verification#

    Before forwarding a transaction to other nodes, it is verified by the node. If the execution fails, the transaction is neither forwarded nor included in a new block.

    Validation providers include:

  • BasicTransactionValidationProvider: Verifies transaction signature and size.
  • TransactionExecutionValidationProvider: Pre-executes the transaction before forwarding or packaging it.
  • TransactionMethodValidationProvider: Prevents view-only method transactions from being packaged into new blocks.
  • Execution#

    In aelf, transactions are executed via the .NET reflection mechanism.

    Execution Plugins#

  • FeeChargePreExecutionPlugin: Charges method fees from the sender.
  • MethodCallingThresholdPreExecutionPlugin: Checks the calling threshold of specific contracts or methods.
  • ResourceConsumptionPostExecutionPlugin: Charges resource tokens based on the resources consumed during execution.
  • Transaction Result#

    The data structure for TransactionResult:

    1
    message TransactionResourceInfo {
    2
    repeated aelf.ScopedStatePath write_paths = 1;
    3
    repeated aelf.ScopedStatePath read_paths = 2;
    4
    ParallelType parallel_type = 3;
    5
    aelf.Hash transaction_id = 4;
    6
    aelf.Hash contract_hash = 5;
    7
    bool is_nonparallel_contract_code = 6;
    8
    }

    Edited on: 14 July 2024 05:08:13 GMT+0