如何在以太坊实施交易。在本文中,我们将讨论交易验证规则及其存在的原因;然后,我们将深入讨论如何实施交易,以及在验证交易时采取的每一步节点。
关注与讨论欢迎。
作者:@lunaray.cosupport。
介绍0x01。
简而言之,状态转换函数利用当前状态和交易作为输入来计算下一状态。
交易验证0x02。
在执行交易之前,节点会验证交易是否符合一些基本规则。如果这些基本规则没有通过,节点将不会执行交易。
符合RLP编码格式。
有合法签名。
具有合法的nonce(与交易者当前的nonce值相同)
在交易设定的gas限制下执行(内部成本)的交易。
交易所帐户余额大于或等于交易所要求的预付款。
还有一条规则不是交易的固有规则——如果一系列交易准备被打包成区块,再加上这笔交易,所有交易的总GasLimit都会超过区块的GasLimit,那么这笔交易就不能与这些交易打包成区块。
符合RLP编码格式。
RLP(RecursiveLengthPrefix)是一种以太坊序列化对象编码方法;与其他方法一样,如果对象没有按照RLP编码,则无法解码对象,并且无法通过数据编码信息获得原始对象。本规则的目的是确保以太坊客户端能够在收到交易后成功解码并执行。
必须有合法的交易签名。
假设你的以太坊账户中有很多以太坊货币,现在有人试图开始一笔交易,把你账户中的钱转移给他自己,你觉得怎么样?你绝对不想看到有人假装你,偷你的钱,这就是为什么我们需要交易签名。以太坊使用非对称加密货币,以确保只有实际控制人才能从账户中开始交易。在以太坊,帐户地址是由个人公钥生成的。在发送交易时,使用私钥签署交易,然后确定交易是否实际由相关帐户的私钥所有者签署。没有法律签名的交易没有实施意义,所以必须有法律签名是交易的内在规则之一。
必须匹配交易nonce和accountnonce。
在以太坊中,账户的nonce值表示账户发送的交易数量(在合同账户的情况下,nonce值是指账户创建的合同数量)。如果没有随机数,同一交易可能会被错误地执行多次(所谓的重播攻击)。以太坊强制交易nonce值与账户nonce值相匹配。这不仅可以防止重播攻击,还可以确保事务只执行和改变一次状态。
交易的内在成本必须低于gascap设定的交易。
每笔交易都有一个相关的标准交易——发送交易的成本由固有成本和执行成本两部分组成。执行成本取决于以太坊虚拟机(EVM)资源的运行量。执行交易所需的操作越多,执行成本就越高。内部成本由交易的有效载荷决定,分为以下三种有效载荷:
payload是创建智能合约的EVM代码,如果交易是创建智能合约。
payload是执行消息的输入数据,如果交易是调用智能合约的函数。
payload是空的,如果交易只是两个账户之间的转账。
=21,000Wei,Gtransaction。
32,000Wei,Gtxcreate。
Gtxdatazero=4Wei。
68Wei,Gtxdatanzero=68。
交易前
GasLimit*gasPrice+value(一笔交易的GasLimit)Pre-transaction
执行交易0x03。
在核实交易之后,是时候执行它了。在以太坊,一个交易的执行会改变状态——几个交易被打包成一个街区,每个街区相当于一个交易清单;当交易按顺序执行时,一个新的法律状态是一个输出。交易执行如下:
增加1发件人帐户的nonce值。
交易预付款(gasLimit*gasPrice)从发送方帐户中扣除
确定gas值(gasLimit-内在成本),该值可用于执行
操作(转帐、调用或创建智能合约)包含在执行交易中
发件人可以通过SELFDESTRUCT和SSTORE功能进行退款。
向交易发送者返还任何未使用的gas。
将矿业收入转移到受益人账户(通常是矿工挖掘包括交易区块)
参考:https://ethereum.github.io/yelowpaper.paper.pdf。
How to implement transactions on Ethereum. In this article, we will discuss transaction verification rules and why they exist; then, we will discuss in depth how transactions are implemented and each node takes when validating a transaction.
Attention and discussion are welcome.
Author: @lunaray.cosupport.
Introducing 0x01.
In short, the state transition function uses the current state and transactions as input to calculate the next state.
Transaction verification 0x02.
Before executing a transaction, the node verifies that the transaction complies with some basic rules. If these basic rules are not passed, the node will not execute the transaction.
Conforms to RLP encoding format.
Has a legal signature.
Has a legal nonce (same as the trader's current nonce value)
A transaction that is executed (internal cost) within the gas limit set by the transaction.
The exchange account balance is greater than or equal to the down payment required by the exchange.
There is another rule that is not an inherent rule of the transaction - if a series of transactions are to be packaged into a block, plus this transaction, the total GasLimit of all transactions will exceed the GasLimit of the block, then this transaction will These transactions cannot be packaged into blocks.
Conforms to RLP encoding format.
RLP (RecursiveLengthPrefix) is an Ethereum serialization object encoding method; like other methods, if the object is not encoded according to RLP, the object cannot be decoded and the original object cannot be obtained through the data encoding information. The purpose of this rule is to ensure that Ethereum clients can successfully decode and execute transactions after receiving them.
Must have a legal transaction signature.
Suppose you have a lot of Ethereum currency in your Ethereum account, and now someone tries to start a transaction to transfer the money in your account to himself, what do you think? You never want to see someone pretending to be you and stealing your money, which is why we need transaction signatures. Ethereum uses an asymmetric cryptocurrency to ensure that only the person with the actual control can initiate transactions from the account. In Ethereum, account addresses are generated from personal public keys. When sending a transaction, the private key is used to sign the transaction, and then it is determined whether the transaction was actually signed by the owner of the private key for the account in question. A transaction without a legal signature has no implementation meaning, so the necessity of a legal signature is one of the inherent rules of the transaction.
Must match transaction nonce and accountnonce.
In Ethereum, the nonce value of an account represents the number of transactions sent by the account (in the case of contract accounts, the nonce value refers to the number of contracts created by the account). Without nonces, the same transaction could be executed incorrectly multiple times (so-called replay attack). Ethereum is strongMake the transaction nonce value match the account nonce value. This not only prevents replay attacks, but also ensures that transactions are only executed and change state once.
The intrinsic cost of the transaction must be lower than the transaction set by the gascap.
Each transaction has an associated standard transaction - the cost of sending a transaction is made up of both the inherent cost and the execution cost. The execution cost depends on how much Ethereum Virtual Machine (EVM) resources are running. The more operations required to execute a trade, the higher the execution cost. The internal cost is determined by the payload of the transaction, which is divided into the following three payloads:
The payload is the EVM code that creates the smart contract, if the transaction is to create a smart contract.
Payload is the input data for executing the message, if the transaction is to call a function of the smart contract.
payload is empty if the transaction is just a transfer between two accounts.
=21,000Wei, Gtransaction.
32,000Wei, Gtxcreate.
Gtxdatazero=4Wei.
68Wei,Gtxdatanzero=68.
Pre-transaction
GasLimit*gasPrice+value (GasLimit of a transaction) Pre-transaction
Execute transaction 0x03.
After verifying the transaction, it’s time to execute it. In Ethereum, the execution of a transaction changes the state - several transactions are packaged into a block, and each block is equivalent to a list of transactions; when transactions are executed in sequence, a new legal state is an output. The transaction is executed as follows:
Increments the nonce value of the sender account by 1.
The transaction prepayment (gasLimit*gasPrice) is deducted from the sender's account
Determines the gas value (gasLimit-intrinsic cost), which can be used to perform
operations (transfer, call or create smart contract ) included in the executed transaction
The sender can refund via the SELFDESTRUCT and SSTORE functions.
Return any unused gas to the transaction sender.
Transfer mining revenue to beneficiary accounts (usually miners mining blocks including transactions)
Reference: https://ethereum.github.io/yelowpaper.paper.pdf.
- 上一篇: 扎克伯格的ins账号 扎克伯格 instagram
- 下一篇: 返回列表