- Mastering Ethereum
- Merunas Grincalaitis
- 584字
- 2021-06-24 15:01:03
Ethereum transactions
To understand how smart contracts are deployed on the network, it's important to understand first how transactions work, since when you deploy a smart contract, what you're actually doing is generating a transaction with the bytecode of the application that you just built.
The message that makes up a transaction on Ethereum is made up of the following encoded components:
- Recipient: This is the receiver Ethereum address that will get the transaction.
- Value: This represents the amount of ether to transfer to the recipient address. This value can be zero, and you can access it in Solidity with the global msg.value variable. The value is always in wei, the smallest unit in Ethereum.
- Data: This is a hexadecimal bytecode string that is mostly used to call a specific function with the required parameters. This is specific information that you need your smart contract to execute. When smart contracts communicate with each other, they need a way to tell when to execute a specific function with a given set of variables. Thanks to this data parameter, we can encode the functions that we want to call in the contract when the transaction is processed. On the other hand, when the smart contract is deployed to the blockchain for the first time, the data parameter contains the smart contract converted to bytecode so that machines can understand it. In general, it contains the smart contract functions to be executed in the next block by the miners.
- Gas limit: This represents the gas limit, which is how much gas you're willing to spend to process your function transactions. The gas limit is represented in wei, and it's mandatory to give miners as much gas as possible to process your code.
- Gas price: The gas price determines how much each gas you provide will cost. If your gas cost is one, you'll pay one wei per gas. If it's 20, you'll pay 20 wei per 1 gas. It's used to help miners process the transactions, since they will be rewarded partially with the transaction fees.
- Nonce: The nonce is a unique counter number that is used to identify transactions. This unique counter is used to identify each block, and it helps miners to identify invalid blocks, since the nonce must always be one number bigger than the previous block.
- Signature: This is a parameter made of three independent variables known as v, r, and s. These variables are used to sign transactions with your unique Ethereum address data so that people can confirm that you're the one that created it.
When a user makes a transaction to a smart contract, it's called a message instead of a transaction. This difference between transaction and message exists, because messages don't have signature data since they don't have to be signed by the other party. The nonce is required to prevent replay attacks where an external user could take the same transaction data and execute it again for his own benefit.
When you deploy a smart contract, you're actually sending a transaction to the address 0x0 with a special bytecode identifier, so that miners understand that you're creating a new smart contract. The data parameter in this case contains all the smart contract logic, including function names and parameters.
In summary, creating and working with smart contracts is a transparent process, where you tell miners to process your data. They will then understand the bytecode behind it and make the required changes to the blockchain with the needed parameters.