以太坊中的Events和Logs.pdf
谈谈区块链( 10)以太坊中的Events 和 Logs 以太坊中的事件( Events)和日志( Logs)是个特别让人困惑的概念,本文帮大家梳理。 首先,以太坊中的 Events 和 Logs 基本上算是同一个概念。 Solidity 和web3.js 中称为 Events,以太坊黄皮书中称为 Logs。你可以理解为以太坊通过 Logs 实现 Events(事件)功能。智能合约代码通过 LOG opcode 将日志写入区块链中。 日志内容位于区块链的什么地方 日志内容是交易收据( Transaction Receipts)的一部分,整个日志内容,包括 Receipts 的其它内容会生成一个 ReceiptsRoot 存储在区块的头部。而完整数据则是链下存储。 事件和日志的主要用途有三种 1. 帮助用户客户端( web3.js)读取智能合约的返回值; 2. 智能合约异步通知用户客户端( web3.js); 3. 用于智能合约的存储(比 Storage 便宜得多); 下面我们逐个解释 1.帮助用户客户端( web3.js)读取智能合约的返回值 假设下面的智能合约 contract ExampleContract { // some state variables . function fooint256 _value returns int256 { // manipulate state . return _value; } } 我们可以通过 web3.js 的 message call 功能模拟调用智能合约 var returnValue exampleContract.foo.call2; console.logreturnValue // 2 但是在真实的环境中我们需要发送交易( Transaction)来调用某个智能合约。这时我们将无法获得智能合约的返回值。因为该交易当前只是被发送,离被打包、执行还有一段时间。此时调用的返回值只是该交易的 txid 戒称为 tx hash 值。 var returnValue exampleContract.foo.sendTransaction2, {from web3.eth.coinbase}; console.logreturnValue // transaction hash 这时事件( Events)就登场了 //以下是 solidity智能合约代码 contract ExampleContract { event ReturnValueaddress indd _from, int256 _value; function fooint256 _value returns int256 { ReturnValuemsg.sender, _value; return _value; } } //以下是 web3.js用户客户端代码 var exampleEvent exampleContract.ReturnValue{_from web3.eth.coinbase}; exampleEvent.watchfunctionerr, result { if err { console.logerr return; } console.logresult.args._value // check that result.args._from is web3.eth.coinbase then // display result.args._value in the UI and call // exampleEvent.stopWatching } exampleContract.foo.sendTransaction2, {from web3.eth.coinbase} 当交易被打包时, web3.js中的 callback就会被调用,这时 web3.js就可以得到交易中的智能合约调用的返回值了。 至于为什么交易打包, web3中的 callback就被调用那是另一个 问题了,简单解释如下。 web3会和以太坊某个节点相连,当该节点得知某个交易进 block后,会通知相连的 web3相关信息。 2.智能合约异步通知用户客户端( web3.js) 上面例子是智能合约通知用户客户端的典型例子,但还有更多的异步调用可以通过类似的方式实现,从而实现了智能合约异步调用用户客户端的能力。 注智能合约通常用 solidity编写,运行在以太坊节点上( EVM)。 注用户客户端通常用 web3.js编写,运行在 web服务器上, web3.js跟某个以太坊节点相连。 3.用于智能合约的存储(比 Storage 便宜得多) 相比智能合约账户的 Storage,用日志的方式存储一些信息会便宜很多。Storage 中大致的价格是每 32 字节( 256 位)存储需要消耗 20,000气( Gas)。而日志大致是每字节 8 气( Gas)。 下面看一个例子 //solidity智能合约代码,模拟用户存款功能 contract CryptoExchange { event Deposituint256 indd _market, address indd _sender, uint256 _amount, uint256 _time; function deposituint256 _amount, uint256 _market returns int256 { // per deposit, update user’s balance, etc Deposit_market, msg.sender, _amount, now; } //当某户调用智能合约存了某笔款后, 智能合约需要主动通知用户客户端更新相应信息。 //以下是 web3.js代 码 var depositEvent cryptoExContract.Deposit{_sender userAddress}; depositEvent.watchfunctionerr, result { if err { console.logerr return; } // append details of result.args to UI } //通过增加 fromBlock参数指定关注的区块范围 var depositEventAll cryptoExContract.Deposit{_sender userAddress}, {fromBlock 0, toBlock latest}; depositEventAll.watchfunctionerr, result { if err { console.logerr return; } // append details of result.args to UI //如果不想再被通知可以调用 //depositEventAll.stopWatching; } //注 indd 关键字表示按该字段索引日志,以提高查询效率。 以上内容是作者根据网上资料和个人理解梳理而得。如有转载请注明出处谈谈区块连。