In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to achieve Solidity Event", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Solidity Event is realized.
A Solidity Event is defined as follows:
Event Deposit (address indexed _ from, bytes32 indexed _ id, uint _ value)
Up to 3 indexed parameters.
If the type of an indexed parameter is greater than 32 bytes (such as string and bytes), the actual data is not stored, but the KECCAK256 Digest of the data is stored.
EVM Log Primitives
Let's first take a look at the log0, log1,..., log4 EVM instruction.
The EVM logging feature uses different terms:
"topics": up to 4 topics. 32 bytes per topic.
"data": the data is event's Payload. Can be any length of bytes.
How does a Solidity event map to a log primitive?
All non-indexed parameters are saved as data.
Each "indexed parameter" is stored as a 32 bytes topic.
The log0 Primitive
Log0 generates a log project with only data and no topic. Data can be bytes of any length.
Let's look at an example
Pragma solidity ^ 0.4.18 X contract Logger {function Logger () public {log0 (0xc0fefe);}}
After compilation
0x40 pointers are free pointers to memory. The first part imports the data into memory, and the second part prepares the size of the data on the stack
Memory: {0x40 = > 0x60} tag_1: / / copy data into memory 0xc0fefe [0xc0fefe] mload (0x40) [0x60 0xc0fefe] swap1 [0xc0fefe 0x60] dup2 [0x60 0xc0fefe 0x60] mstore [0x60] memory: {0x40 = > 0x60 0x60 = > 0xc0fefe} / / calculate data start position and size 0x20 [0x20 0x60] add [0x80] mload (0x40) [0x60 0x80] dup1 [0x60 0x60 0x80] swap2 [0x60 0x80 0x60] sub [0x20 0x60] swap1 [0x60 0x20] log0
Before executing log0, there are two parameters on the stack: [0x60 0x20].
Start: 0x60 is a memory pointer used to store data.
Size: 0x20 (or 32) specifies the size of the loaded data.
Logging With Topics
The following example uses the log2 primitive. The first parameter is data (which can be arbitrarily long bytes), followed by 2 topics (32 bytes each):
/ / log-2.solpragma solidity ^ 0.4.18 th contract Logger {function Logger () public {log2 (0xc0fefe, 0xaaaa1111, 0xbbbb2222);}}
The assembly code is very similar. The only difference is that two topics (0xbbbb2222, 0xaaaa1111) are pushed down the stack:
Tag_1: / / push topics 0xbbbb2222 0xaaaa1111// copy data into memory 0xc0fefe mload (0x40) swap1 dup2 mstore 0x20 add mload (0x40) dup1 swap2 sub swap1// create log log2
The data is still 0xc0fefe, copied to memory. Before executing log2, the status is as follows:
Stack: [0x60 0x20 0xaaaa1111 0xbbbb2222] memory: {0x60: 0xc0fefe} log2
The first two parameters specify the memory area of the log data, and the two new stack elements are two 32 bytes topics.
All EVM Logging Primitives
EVM supports 5 log primitives:
0xa0 LOG00xa1 LOG10xa2 LOG20xa3 LOG30xa4 LOG4Logging Testnet Demopragma solidity ^ 0.4.18 X contract Logger {function Logger () public {log0 (0x0); log1 (0x1, 0xa); log2 (0x2, 0xa, 0xb); log3 (0x3, 0xa, 0xb, 0xc); log4 (0x4, 0xa, 0xb, 0xc, 0xd);}}
The contract is deployed to the Rinkeby test network. At: https://rinkeby.etherscan.io/tx/0x0e88c5281bb38290ae2e9cd8588cd979bc92755605021e78550fbc4d130053d1
Solidity Events
Here is a Log event with three uint256 parameters (non-indexed):
Pragma solidity ^ 0.4.18 X contract Logger {event Log (uint256 a, uint256 b, uint256 c); function log (uint256 a, uint256 b, uint256 c) public {Log (a, b, c);}}
The original log generated is as follows:
Https://rinkeby.etherscan.io/tx/0x9d3d394867330ae75d7153def724d062b474b0feb1f824fe1ff79e772393d395
The data is the event parameter and is encoded by ABI:
000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003
There is one topic and one 32 bytes hash:
0x00032a912636b05d31af43f00b91359ddcfddebcffa7c15470a13ba1992e10f0
This is the signed SHA3 hash of the event type:
# Install pyethereum # https://github.com/ethereum/pyethereum/#installation> from ethereum.utils import sha3 > sha3 ("Log (uint256,uint256,uint256)"). Hex () '00032a912636b05d31af43f00b91359ddcfddebcffa7c15470a13ba1992e10f0'
Because the Solidity event uses a topic for the event signature, there are only three topic. Exe left for the indexed parameter.
Solidity Event With Indexed Arguments
Here is an event with an indexed uint256 parameter:
Pragma solidity ^ 0.4.18 X contract Logger {event Log (uint256 a, uint256 indexed b, uint256 c); function log (uint256 a, uint256 b, uint256 c) public {Log (a, b, c);}}
There are 2 topic
0x00032a912636b05d31af43f00b91359ddcfddebcffa7c15470a13ba1992e10f00x0000000000000000000000000000000000000000000000000000000000000002
The first topic is the signature of the method.
The second topic is the value of the indexed parameter.
Except for the indexed parameter, the data is ABI encoded:
00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003String/Bytes Event Parameter
Set the parameter of the event to a string:
Pragma solidity ^ 0.4.18 X contract Logger {event Log (string a, string indexed b, string c); function log (string a, string b, string c) public {Log (a, b, c);}}
The transaction is on: https://rinkeby.etherscan.io/tx/0x21221c2924bbf1860db9e098ab98b3fd7a5de24dd68bab1ea9ce19ae9c303b56
There are 2 topics
0xb857d3ea78d03217f929ae616bf22aea6a354b78e5027773679b7b4a6f66e86b0xb5553de315e0edf504d9150af82dafa5c4667fa618ed0a6f19c69b41166c5510
The first topic is the signature of the method.
The second topic is the SHA256 summary of the string parameter.
Verify that the hash value of "b" is the same as the second topic:
> > sha3 ("b"). Hex () 'b5553de315e0edf504d9150af82dafa5c4667fa618ed0a6f19c69b41166c5510'
The log data is two non-indexed strings "a" and "c", ABI encoded:
000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016300000000000000000000000000000000000000000000000000000000000000
The indexed string parameter is not stored, so the DApp client cannot restore it.
If you do need the original string, record it twice, indexed and non-indexed:
Event Log (string a, string indexed indexedB, string b); Log ("a", "b", "b"). Now that you have a better understanding of "how Solidity Event is implemented", you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.