Events

事件結果文件

  • August 23, 2016

目前文件沒有詳細介紹事件返回的結果的結構。在這個執行緒和這個問題中,提到了事件監聽器返回的結果欄位,但是文件在哪裡?

來自JavaScript-API - web3.eth.filter

  • Object- 使用手動過濾選項時,它返回一個日誌對象,如下所示:

    • logIndex: Number- 塊中日誌索引位置的整數。待處理日誌時為空。
    • transactionIndex: Number- 創建事務索引位置日誌的整數。待處理日誌時為空。
    • transactionHash: String, 32 Bytes - 創建此日誌的事務的雜湊值。待處理日誌時為空。
    • blockHash: String, 32 Bytes - 此日誌所在塊的雜湊值。當它掛起時為空。待處理日誌時為空。
    • blockNumber: Number - 此日誌所在的塊號。當它掛起時為空。待處理日誌時為空。
    • address: String, 32 Bytes - 此日誌的來源地址。
    • data: String - 包含一個或多個 32 字節的非索引日誌參數。
    • topics: 字元串數組 - 索引日誌參數的 0 到 4 個 32 字節數據的數組。(在solidity:第一個主題是事件簽名的雜湊(例如存款(地址,字節32,uint256)),除非您使用匿名說明符聲明事件。)

一個TheDAO.CreatedToken事件的例子

使用以下geth命令提取一些CreatedToken事件:

var theDAOABIFragment = [{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"}, {"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"CreatedToken","type":"event"}, {"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_amount","type":"uint256"}],"name":"Transfer","type":"event"}];
var theDAOAddress = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413";
var theDAO = web3.eth.contract(theDAOABIFragment).at(theDAOAddress);
var theDAOCreatedTokenEvent = theDAO.CreatedToken({}, {fromBlock: 1428757, toBlock: 1429757});
theDAOCreatedTokenEvent.watch(function(error, result) {
 console.log(JSON.stringify(result));
});

這是前兩個事件

{“地址”:“0xbb9bc244d798123fde783fcc1c72d3bb8c189413”,“ARGS”:{“量”:“300000000億”,“到”:“0xb504e60998c6f354a0794abd91d85e8bc8436211”},“blockHash”:“0x031d5bac6154ca7616ac62e966da2b50a0aaa1b3bc24958ed9cb52d8c8fc1e2f”,“blockNumber”:1429038,“事件”:“ CreatedToken“,”logIndex“:3”,transactionHash“:”0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1“,”transactionIndex“:9} {”地址“:”0xbb9bc244d798123fde783fcc1c72d3bb8c189413“,”ARGS“:{”量“:”500000000000000000“,”為“:” 0x53024f875bc85709af41d1c65c01fb4cc92d5c1c"},“blockHash”:“0x48cf967fc94c2f808d82906c1a56e3e09abc99bb8279266fbace13963dc30a1f“,”blockNumber“:1429053,”事件“:”CreatedToken“,”logIndex“:0”,transactionHash“:”0x1e9ec3974b89653961cbd996d4f6cfc2845db977a3385761b99ed459c2464740“,”transactionIndex“:1}

TheDAO 的原始碼,這裡是CreatedToken事件的定義:

event CreatedToken(address indexed to, uint amount);

因此,從我們得到的結果中查看第一條記錄:

  • 地址:“0xbb9bc244d798123fde783fcc1c72d3bb8c189413” - DAO 的地址

  • args -CreatedToken的參數

    • 金額:“30000000000000000”
    • :“0xb504e60998c6f354a0794abd91d85e8bc8436211”
  • blockHash : “0x031d5bac6154ca7616ac62e966da2b50a0aaa1b3bc24958ed9cb52d8c8fc1e2f” - 包含事件的塊的雜湊

  • blockNumber : 1429038 - 包含事件的塊的編號

  • 事件:CreatedToken - 事件名稱

  • 日誌索引:3

  • 交易雜湊:“ 0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1

  • 交易指數:9

獲取 transactions getTransactionReceipt(...),您可以在資料結構中看到相同的事件數據logs

> eth.getTransactionReceipt("0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1")
{
 blockHash: "0x031d5bac6154ca7616ac62e966da2b50a0aaa1b3bc24958ed9cb52d8c8fc1e2f",
 blockNumber: 1429038,
 contractAddress: null,
 cumulativeGasUsed: 284538,
 from: "0xfbb1b73c4f0bda4f67dca266ce6ef42f520fbb98",
 gasUsed: 91206,
 logs: [{
     address: "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
     blockHash: "0x031d5bac6154ca7616ac62e966da2b50a0aaa1b3bc24958ed9cb52d8c8fc1e2f",
     blockNumber: 1429038,
     data: "0x000000000000000000000000000000000000000000000000006a94d74f430000",
     logIndex: 3,
     topics: ["0xdbccb92686efceafb9bb7e0394df7f58f71b954061b81afb57109bf247d3d75a", "0x000000000000000000000000b504e60998c6f354a0794abd91d85e8bc8436211"],
     transactionHash: "0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1",
     transactionIndex: 9
 }],
 root: "e8ba75bf5d12f93c991721565270a135194887b9260b3f7d809d9a6270dcd6c5",
 to: "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
 transactionHash: "0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1",
 transactionIndex: 9
}

獲取區塊的數據,可以看到上面的blockHash和下面的****hash是一樣的:

> eth.getBlock(1429038);
{
 difficulty: 32654434227321,
 extraData: "0xd783010306844765746887676f312e352e31856c696e7578",
 gasLimit: 4712388,
 gasUsed: 368538,
 hash: "0x031d5bac6154ca7616ac62e966da2b50a0aaa1b3bc24958ed9cb52d8c8fc1e2f",
 logsBloom: "0x00400000000000020000000000020000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000001002000000000000000005000000020000000000000000000000000000000000000000000000000002000000000000000000000000000040000000000000000000008000000000000000000000000000000000000000000000000000000000000000000400000000000000000",
 miner: "0x2a65aca4d5fc5b5c859090a6c34d164135398226",
 nonce: "0x1ec6b2f3920a1b75",
 number: 1429038,
 parentHash: "0xb70be7a89e89a93248498c60e0254f7212bdec42ec5f3be3e9c4687c46a8dd21",
 receiptRoot: "0x0c80b60072f41ffe060af09913c09e8760839e084b1711663b6c05e5b5b4a698",
 sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
 size: 2144,
 stateRoot: "0x1b7a7df358c82b30a2e84e10bde606af1a1e868bde1abf499518f5282b4c2caa",
 timestamp: 1461984747,
 totalDifficulty: 16452703203387523239,
 transactions: ["0x07233561a800dcd2d0b7f5b913f689d15418264f3c025959a04c6a06fc83a715", "0x7d62d8988352db1bef3d2814c80bd78c0789e0345bb5a71bb4b61595e0f2c490", "0xebf630e4bdc04eeebfac6283ca208ed49d80c4ae3340cb6f15df050fc8c1e4ef", "0xa60119d8f8078b862e8df96877f9846d68ba89798be0510b7cf8593eb2f80bc4", "0xbc9850758015bf24e115a0d019001972259b58a562f3664e03df71f709e6cff5", "0xc271d871f5becd2e00b0bd6e1460e7f77812b985ae5de79a0e0f3bfc9ee4f82d", "0xada6df7c55113ddda794ba9924245e4c2b3ff9a7a61d00477976df28910961f6", "0x222b83cac7b441eea0b769268b9534167295065b11b62ad4c3b80c17384ae4da", "0xa3c3bcdb89da86c6b7d174ccb375702c32fb9bfc662d5281436d0da7bb66630f", "0xc96b0f95a1e7e8c07cd488a05f20f9e8d4003fe8eea0ec7f7f4bf199af3198e1", "0xa8ae7985e399753bcc8fd9b92a6a2af206c1b9e813412a337a0c073d3d25ef05", "0xea513060036eb3128e1b9a0d0d434a51b63c9fa45a8048961a766231f7e6ab03", "0x81fc3f258042fc69b11970e632e7fbbb4bbaae1fa52facde2cdd8d76d46fb8e4", "0x6e806f8ecd9b511d560819219e8fb11cf47cb7d9ad643574fb121687d89d175b"],
 transactionsRoot: "0x15fb5d46063445c898589e3894c1002eecac98fdecf6d4ae6c82c78a03ce9390",
 uncles: []
}

讓我們從getTransactionReceipt(...)結果中計算出數據欄位:

data: "0x000000000000000000000000000000000000000000000000006a94d74f430000"

這是CreatedToken事件的未索引參數:

> web3.toBigNumber("0x000000000000000000000000000000000000000000000000006a94d74f430000")
30000000000000000

讓我們從getTransactionReceipt(...)結果中找出主題:

topics: ["0xdbccb92686efceafb9bb7e0394df7f58f71b954061b81afb57109bf247d3d75a", "0x000000000000000000000000b504e60998c6f354a0794abd91d85e8bc8436211"],

第一個元素"0xdbccb92686efceafb9bb7e0394df7f58f71b954061b81afb57109bf247d3d75a"是事件的簽名:

> web3.sha3('CreatedToken(address,uint256)')
"0xdbccb92686efceafb9bb7e0394df7f58f71b954061b81afb57109bf247d3d75a"

第二個元素"0x000000000000000000000000b504e60998c6f354a0794abd91d85e8bc8436211"是索引地址參數。

來自github - go-ethereum/core/vm/log.go,第 67-80 行

func (r *Log) MarshalJSON() ([]byte, error) {
   fields := map[string]interface{}{
       "address":          r.Address,
       "data":             fmt.Sprintf("%#x", r.Data),
       "blockNumber":      fmt.Sprintf("%#x", r.BlockNumber),
       "logIndex":         fmt.Sprintf("%#x", r.Index),
       "blockHash":        r.BlockHash,
       "transactionHash":  r.TxHash,
       "transactionIndex": fmt.Sprintf("%#x", r.TxIndex),
       "topics":           r.Topics,
   }

   return json.Marshal(fields)
}

引用自:https://ethereum.stackexchange.com/questions/8241