Ether Trading life cycle __ethereum

Source: Internet
Author: User

This article summarizes how the transactions on the etheric square are built and broadcast all over the web.

Trading is the core of the block chain. When you interact with an Ethernet trader, you are essentially executing a transaction and changing its state. Are you curious as to what has happened in the process of being executed on the etheric square? Let's use an example to illustrate the nature of the ether trade, in this article we will cover the following knowledge points: End-to-end profiling transactions from the user's browser or command line to launch the transaction to the whole network of block chain broadcast to return to the user terminal process understanding how transactions work when using wallet plug-ins instead of full data nodes, such as Metamask or myetherwallet"" When you are paranoid about not trusting other third-party wallet plug-ins, how do you perform the transaction "slightly"

The default reader has the basic knowledge of the ether, such as account system, gas and contract. If you are a developer, recommend reading articles ethereum for Web developers, simple voting Dapp development tutorials

While reading this article, you should understand that if you are doing a deal, you should be more transparent (capable programmers can build private chain nodes to transfer money), such as sending ETH to other ordinary accounts or contract accounts, voting Dapp interactions, etc. Trading Process

Explain the entire process of the transaction lifecycle by invoking the contract. The source code of the voting contract here, as a whole, this is an initialization of some candidates to participate in the election, anyone can vote for the candidate, the final vote will be permanently tamper record on the block chain.

Voting.deployed (). Then (function (instance) {
instance.voteforcandidate (' Nick ', {gas:140000, from: Web3.eth.accounts[0]}). Then (function (r) {
  Console.log ("Voted successfully!")})



Assume that the Ethernet square client (Geth or parity) has been deployed on your computer and connected to the Ethernet block chain network (testnet or mainnet) to invoke the function in the contract through the contract address and the contract ABI. Call the Voteforcandidate function after you get the contract object. building the original transaction object

The initial transaction is first generated after the Voteforcandidate function is invoked:

Txncount = Web3.eth.getTransactionCount (web3.eth.accounts[0])
var rawtxn = {
    Nonce:web3.toHex (txncount),
    GasPrice:web3.toHex (100000000000),
    GasLimit:web3.toHex (140000), to
    : ' 0x633296baebc20f33ac2e1c1b105d7cd1f6a0718b ',
    value:web3.toHex (0),
    data: ' 0xc7ed014952616d6100000000000000000000000000000000000000000000000000000000 '
};

Explain the meaning of each field in the original transaction nonce: There is a nonce field on each account in the ether to mark the number of transactions that occurred on the account. Each new transaction occurs in the account, the nonce is increased by 1, while the block chain network can also handle the order of execution of the transaction. Nonce is also used to replay protection.

What is a replay attack? Without a replay protection, when you are say send out 1 Bitcoin to the legacy chain, the transaction is also valid on the Forked chain with the same amount of new coins and same recipient. Someone else can make use the this and send out your new coins without your agreement. This is the same case for the opposite Direction:when your send out the new coins, your are potentially also sending out yo ur bitcoin! Gasprice: Pay the price of the gas for each unit of the deal. Gaslimit: Pay the maximum amount of gas for the deal. This field prevents special circumstances in the execution of the transaction, such as an unlimited contract cycle, resulting in the loss of the account balance and, once the transaction is completed, the remaining gas will be returned to your account. To: This field is the contract address when the contract is invoked, and the target user address is the normal transaction. Here is the voting contract address value: The number of transfers. Here our purpose is to call the voting contract, so the assignment is 0 data: The transaction carries the information, here has a tip, the ordinary transaction obtains the transaction result the input data is usually 0x, the contract transaction result this field protects the contract transaction information. This field enables the distinction between ordinary and contract transactions.

Further explains the generation rules for data field values.

First is the hash of the called contract function to get the front four bytes and get 0xcc9ab267

> Web3.sha3 (' voteforcandidate (bytes32 candidate) ')
' 0xc7ed014922ff9493a686391b70ca0e8bb7e80f91c98a5cd3d285778ab2e245b3 '

Then the parameter value of the called contract function is converted to 32 bytes, and the 52616d6100000000000000000000000000000000000000000000000000000000*

The above two-time combination is the value of the data field. Signature Transactions

WEB3.ETH.ACCOUNTS[0] Executing the transaction, the Ethernet network needs to verify that the transaction initiator is valid and that the signature of the private key will prove that you have the right to use the account balance.

Const PRIVATEKEY = buffer.from (' e331b6d69882b4ab4ea581s88e0b6s4039a3de5967d88dfdcffdd2270c0fd109 ', ' hex ')
Const TXN = new ETHEREUMTX (RAWTXN)
txn.sign (privatekey)
const SERIALIZEDTXN = txn.serialize ()
Local node verifies transaction legality

The signed transaction is submitted to the node you set up, and the node verifies that the signed transaction is actually signed by the corresponding private key. The deal is broadcast all over the net.

Once the constructed transaction is broadcast to the block chain network by the node you set up, the local node returns the transaction ID through which the transaction status can be traced.

TransactionID = SHA3 (SERIALIZEDTXN)



Mainnet transactions can be viewed on http://etherscan.io, and if your transaction is received by other nodes, you can see the transaction status as pending on the Block browser. The local broadcast of the transaction will not be received by all nodes, this situation occurs in the transaction of gas price below the node set the lowest gas price. ore-Digging node package transaction

The Miners ' node maintains a trading pool, arranging the collection of unpacked transactions from high to low according to gas price (the arrangement rules are configurable, of course), and then packaging to generate a block. Trading pools can hold a cap on transactions, and if congestion in the network blocks can lead to higher fees and less handling fees and delays in packaging and even being discarded by miners, we will be broadcasting the deal again. Another trick is to get the miners dumped from the trading pool to be repackaged: Keep the nonce unchanged while raising gas price and broadcasting again, and after the miners receive a deal to increase the fees, the new deal covers the deal that was removed from the trading pool, and the old deals will expire. out block and broadcast all over the net

The miners eventually packaged the deals we built and other deals to build blocks. The ether-square agreement by setting the number of transactions in the gas limit limit block of a block, the sum of all transaction gas limit in the block cannot be added beyond the gas limit set by the block. The current block gas limit can be viewed through ethstats.net.

Once the miners choose to package the transaction to generate blocks, which means that these transactions are successfully verified, the state of the block is pending blocks, and then the miners ' nodes begin to prove the workload. Finally, only one mining node obtains the block right and appends the pending block to the chain. Broadcast block just like our node broadcast trade, the miners in the block broadcast the block to the whole net. Local node receives/synchronizes latest blocks

The local node receives the latest block of the miner's broadcast and synchronizes, and when the new block is received, all transactions in the block are executed by the local node. If you use truffle to execute a transaction, the tool will constantly rotation the data on the chain to determine whether the transaction is confirmed and will be executed once the confirmation is received:

. then (function (r) {
Console.log ("voted successfully!")
})
Recommended ReadingsWhat is transaction replay and replay protection?

Life Cycle of a ethereum Transaction

https://zhuanlan.zhihu.com/p/35271647

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.