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:
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:
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.
Example#
1rpc Vote (VoteInput) returns (google.protobuf.Empty) {2}
Example#
1rpc GetVotingResult (GetVotingResultInput) returns (VotingResult) {2option (aelf.is_view) = true;3}
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:
1option csharp_namespace = "AElf.Types";23message Transaction {4Address from = 1;5Address to = 2;6int64 ref_block_number = 3;7bytes ref_block_prefix = 4;8string method_name = 5;9bytes params = 6;10bytes signature = 10000;11}
Working with Transactions in JS SDK#
Use the getTransaction method to build a transaction object:
1import Aelf from 'aelf-sdk';23var rawTxn = proto.getTransaction(4'65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9',5'SomeMethod',6encodedParams7);
This will create a transaction to the contract at address 65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9 and call SomeMethod with encoded parameters.
Key Fields in Transactions#
Sign a transaction using the JS SDK:
1import Aelf from 'aelf-sdk';23var txn = Aelf.wallet.signTransaction(rawTxn, wallet.keyPair);
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:
Execution#
In aelf, transactions are executed via the .NET reflection mechanism.
Execution Plugins#
Transaction Result#
The data structure for TransactionResult:
1message TransactionResourceInfo {2repeated aelf.ScopedStatePath write_paths = 1;3repeated aelf.ScopedStatePath read_paths = 2;4ParallelType parallel_type = 3;5aelf.Hash transaction_id = 4;6aelf.Hash contract_hash = 5;7bool is_nonparallel_contract_code = 6;8}
Edited on: 14 July 2024 05:08:13 GMT+0