80% of developers do not know the operation of the etheric square: "Events" and "log" can also play this way!

Source: Internet
Author: User
80% developers do not know the operation of the etheric square: "Events" and "log" can also play this way. May 02, 2018 00:00:00 Readings: 366


Author | Director of technology and technology Cai Yizhi top



April 6, Daniel Larimer released an article "Eosio Dawn 3.0 Now Available" (translated: Eosio Dawn), in the chapter 3.0来 "simplified development"(translation: Simpler contract development), an example of a"hello,world"intelligent contract is given to illustrate the simplicity of contract development.


https://gist.githubusercontent.com/bytemaster/

58d45d13dbf8732c8d467b6415a44df9/raw/ba460c2aff29c7f60ecdef7dc082a0190b5af418/

Hello.cpp


It is noteworthy, however, that the example contract executes a statement: print ("Hello,", user);

So the question is: where does the print content appear in the smart contract?


I didn't actually run this code, but I know that the contents of print will only appear on the log file or terminal (Console) of each block chain node. In fact, traditional print log printing has no meaning for the development and tracking of intelligent contracts.


So, how does the etheric square solve this problem? The etheric square introduces events and logs (Logs).


The terms "event" and "log" here may be confusing, and in essence, intelligent contracts generate "logs" through "events." This article will introduce the 3 scenarios commonly used by the "event" and "Log" systems of the Ethernet workshop for the reference of the Ethernet developers.



Purpose of events and logs


"Events" and "logs" are very important for the etheric square because they make communication between the smart contract and the user interface easier. In traditional Web development, a server response provides callbacks to the front end. In the etheric square, when a transaction is packaged, an intelligent contract can send events and write logs to the block chain for front-end processing. There are three situations in which you need to use events and logs.


1. Intelligent contract to the user interface return value

We still take the example of Eos, in view of the solidity concatenation string is troublesome, I return a string directly.

If you use Web3.js to invoke the function of HelloWorld this intelligent contract "hi (), you might think so:



So, the result of this call will be "hello, world". Not really, Web3.js returns a transaction hash (transaction hash) regardless of what value the function returns.


How do you do that? Now it's the turn of the game.


The revised smart contract code is as follows:

and web3.js needs to write this:


When the transaction is packaged, the callback function is triggered and the front end gets the return value of the smart contract function.


2. Asynchronous Data Notification

Using the return value to return to the user interface is actually rarely used. Most of the time, we use events to send data to the user interface. When an intelligent contract wants to notify the user of some data, the event can be sent, and the user community will be able to perform different operations when listening to the time.


3. Relatively inexpensive data storage

Another way to use this is to store logs as data. In the EVM specification and the Yellow Book, the event will produce a log, and the log will be stored in the block chain. The gas cost of journal storage is much cheaper than contract storage (log 8 gas per byte, and contract storage is 20,000 gas per 32 bytes). Therefore, when you have applications such as certificates of application, you can consider using the log to store, instead of using mapping in the contract to store.



The storage structure of the log


It says the three uses of time and log, and finally we look at the approximate structure of the log.


You can view the log interface through "remix". Topic point to a specific event, but the value is 16, what is this? This is actually the signature of the event. The 288d740d3b11a36c8526119855345e3ba2aee438370d264289ea6dfb76294fd0 in the figure is actually the result of Sha3 (Hi (string) execution. The following "event" and "args" are decoded by "remix" from the logs entry of the "receipts" (Receipt) data in the block chain.


The actual data is:


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.