Blockchain Learning (3)

Ethereum account

  • Externally owned accounts, controlled by keys. The matemask wallet falls into this category
  • Contract accounts, controlled by the code of the smart contract
    insert image description here

transaction data structure

A transaction is a serialized binary message containing the following data:
• nonce: A serial number issued by the originator EOA to prevent transaction message replay.
• Gas price: The gas unit price (wei) that the transaction initiator is willing to pay.
• start gas: The maximum amount of gas that the transaction initiator is willing to pay.
• to: destination Ethereum address.
• value: The amount of ether to send to the destination.
• data: variable length binary data payload (payload).
• v, r, s: The three components of the ECDSA signature of the originator's EOA.
• The structure of transaction messages is serialized using the Recursive Length Prefix (RLP) encoding scheme, which
was created for accurate and byte-perfect data serialization in Ethereum

nonce in transactions

• Yellow Paper Definition: A scalar value equal to the number of transactions sent from this address, or for
accounts associated with code, the number of contracts created by this account.
• The nonce is not explicitly stored as part of the state of the account in the blockchain. Instead, it is
calculated dynamically by counting the number of confirmed transactions for the sending address.
• The nonce value is also used to prevent miscalculation of account balances. The nonce enforces that transactions from any address are
processed sequentially, without gaps, regardless of the order in which nodes receive them.
• Using a nonce to ensure that all nodes calculate the same balance and sequence transactions correctly, is equivalent to the
mechanism used to prevent Bitcoin "double spending" ("replay attack"). However, since Ethereum tracks account balances and does not track UTXOs individually, "double spending"
can only occur when account balances are miscalculated .
The nonce mechanism prevents this from happening.

gas in transaction

• When the EVM is triggered by a transaction or message, each instruction is executed on each node of the network
. This has a cost: for every operation performed, there is a fixed cost, which we
denote by a certain amount of gas.
• gas is the name of the cost that the transaction initiator needs to pay for each operation on the EVM. When initiating a transaction,
we need to buy gas with Ether from the miner executing the code.
• gas corresponds to system resources consumed, which has a natural cost. Therefore, gas and ether are intentionally decoupled in design
, the amount of gas consumed represents the occupation of resources, and the corresponding transaction fee is
also related to the unit price of gas to ether. The two are regulated by the free market: the price of gas
is actually determined by the miners, who can refuse to process transactions for which the gas price is lower than the minimum
. We don't need to buy gas specifically, just add ether to the account, and the client
will automatically use ether to buy gas when sending a transaction. While the price of ether itself often
fluctuates due to market forces

Calculation of gas

• The gas limit when initiating a transaction is not the amount of gas to be paid, but only an
upper limit of gas consumption, which is equivalent to a "deposit"
• The actual amount of gas paid is the gas consumed during execution (gasUsed), gas
limit The remaining part will be returned to the sender
• The final gas fee paid is the Ethereum fee corresponding to gasUsed, and the unit price is determined by the set gasPrice • The
final
payment fee totalCost = gasPrice * gasUsed
• totalCost will be used as a transaction fee (Tx fee ) paid to miners

eth.estimateGasYou can estimate the value of gas by

The value and data of the transaction

• The main "payload" of a transaction is contained in two fields: value and data. A transaction can
have both value and data, only value, only data, or neither value nor
data. All four combinations work.
• A transaction with only value is an ether payment
• A transaction with only data is generally a contract call
• While making a contract call, we can also send ether in addition to transmitting data, so that the transaction
contains both data and value
• None value also has no data transaction, just wasting gas, but it is effective

Special transaction: create (deploy) contract

• There is a special kind of transaction, with a data payload and no value, that is a
transaction that creates a new contract.
• Contract creation transactions are sent to a special destination address, zero address 0x0. This address neither
represents the EOA nor the contract. It never spends ether or initiates transactions, it is only used
as a destination and has a special meaning of "creating a contract".
• Although the zero address is only used for contract registration, it sometimes receives payments from various addresses.
This situation is either accidental misuse, resulting in the loss of ether, or the intentional destruction of ether. • Contract registration transactions should not contain ether values, only the data payload
of the contract's compiled bytecode .
The only effect of this transaction is to register the contract

Guess you like

Origin blog.csdn.net/weixin_44831720/article/details/122752326