一、Ethereum事件日志查詢參數(shù)
詳見:https://www.quicknode.com/docs/ethereum/eth_getLogs
- address:合約地址
- fromBlock:開始區(qū)塊
- toBlock:結(jié)束區(qū)塊
- topics:主題數(shù)組
- blockHash:區(qū)塊哈希,優(yōu)先級(jí)高于fromBlock、toBlock
這里主要介紹topics參數(shù),其他參數(shù)都比較好理解,topics是長(zhǎng)度為4的數(shù)組集合,topic分為2種:一種事件簽名topic,另一種indexed索引參數(shù)值topic。
topics的的0號(hào)位子數(shù)組放事件簽名哈希,1/2/3號(hào)位子數(shù)組對(duì)應(yīng)放事件的indexed索引參數(shù)值對(duì)應(yīng)的哈希。
以demo合約舉例:
pragma solidity ^0.4.4;
contract Hello {
string name;
event LogSet(string s);
event LogSet1(string indexed s1);
event LogSet2(string indexed s1, string indexed s2);
event LogSet3(string indexed s1, string indexed s2, string indexed s3);
constructor() public {
name = "hello";
}
function get() public view returns (string) {
return name;
}
function set(string newName) public {
name = newName;
emit LogSet(newName);
emit LogSet1(newName);
emit LogSet2(newName, "name2");
emit LogSet3(newName, "name2", "name3");
}
}
如果合約調(diào)用set(“Tom”),事件LogSet3的1號(hào)位indexed索引參數(shù)值為"Tom",2號(hào)位為"name2",3號(hào)位為"name3"。
注意:
合約事件里最多只能有3個(gè)indexed索引參數(shù)。
如果事件定義改為:
event LogSet3(string indexed s1, string s2, string indexed s3);
事件LogSet3的1號(hào)位indexed索引參數(shù)值為"Tom",2號(hào)位為"name3",沒(méi)有3號(hào)位。
二、需求
部署一個(gè)新的Hello合約,并調(diào)用一次set函數(shù),以觸發(fā)生成4條不同的事件日志。要求查詢?cè)摵霞s的LogSet3事件日志。
三、實(shí)現(xiàn)
第一步,合約部署前,獲取到最新塊高,作為fromBlock,假設(shè)9684。
第二步,部署Hello合約,并調(diào)用set函數(shù),入?yún)ewName=“Tom”,假設(shè)獲取到新合約地址0x0dba67483eddb71a84ac0834cd4c8c89dc971d4b。
第三步,再次獲取最新塊高,作為toBlock,假設(shè)9686。
第四步,計(jì)算事件LogSet3(string,string,string)
的簽名,得到0x3e03ccf7099c79040ac78f368a6a038e5d7918b8504f8cc99fd4d1ae71181e7b。
第五步,計(jì)算第一個(gè)indexed索引參數(shù)值"Tom"的哈希0x6984758a5a2907300d836a0ed6101bb5426c0a4422c0d996e8bbf9e59bb8c7cc,計(jì)算第二個(gè)indexed索引參數(shù)值"name2"的哈希0x7d51639d4f8290223cffdcc7a75498fd9c00ab65e7daf27837046fec6a6d6504,計(jì)算第三個(gè)indexed索引參數(shù)值"name3"的哈希0x289ff8670e65b79f5a7c14daf83f381a29ae238fff49f37396cc9100fb243074。
第六步,組裝日志查詢請(qǐng)求參數(shù),如下:
{
"address": "0x0dba67483eddb71a84ac0834cd4c8c89dc971d4b",
"toBlock": "9686",
"topics": [
[
"0x3e03ccf7099c79040ac78f368a6a038e5d7918b8504f8cc99fd4d1ae71181e7b"
],
[
"0x6984758a5a2907300d836a0ed6101bb5426c0a4422c0d996e8bbf9e59bb8c7cc"
],
[
"0x7d51639d4f8290223cffdcc7a75498fd9c00ab65e7daf27837046fec6a6d6504"
],
[
"0x289ff8670e65b79f5a7c14daf83f381a29ae238fff49f37396cc9100fb243074"
]
],
"fromBlock": "9684"
}
實(shí)際上,如果指定了具體的Hello合約地址,請(qǐng)求參數(shù)里不需要第一個(gè)和第二個(gè)索引參數(shù)topic,也可以唯一區(qū)分開該合約的其他三個(gè)事件,如此,請(qǐng)求參數(shù)如下:
{
"address": "0x0dba67483eddb71a84ac0834cd4c8c89dc971d4b",
"toBlock": "9686",
"topics": [
[
"0x3e03ccf7099c79040ac78f368a6a038e5d7918b8504f8cc99fd4d1ae71181e7b"
],
null,
null,
[
"0x289ff8670e65b79f5a7c14daf83f381a29ae238fff49f37396cc9100fb243074"
]
],
"fromBlock": "9684"
}
四、其他
4.1、topics中4個(gè)子數(shù)組之間是OR的關(guān)系
以太坊官網(wǎng)說(shuō)明:https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_newfilter
- topics: Array of DATA, - (optional) Array of 32 Bytes DATA topics. Topics are order-dependent. Each topic can also be an array of DATA with “or” options.
舉例:
[[A, B], [A, B]] “(A OR B) in first position AND (A OR B) in second position (and anything after)”文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-465914.html
4.2、涉及枚舉入?yún)㈩愋陀?jì)算事件簽名
如果事件參數(shù)中包含枚舉類型,如何正確計(jì)算該事件簽名的topic。
在solidity中的enum
類型,實(shí)際上是無(wú)符號(hào)整數(shù),當(dāng)枚舉數(shù)量是小于等于256(2的8次方)個(gè),則enum是uint8類型的,如果大于256且小于等于65536(2的16次方),則enum是uint16類型的,以次類推。其實(shí)在remix中也可以看到,枚舉內(nèi)的數(shù)量小于256,枚舉類型自動(dòng)使用uint8,如下:
所以對(duì)上面的例子,事件簽名DataSaved(ProofType,bytes)
是錯(cuò)誤的,DataSaved(enum,bytes)
也是錯(cuò)誤的。正確應(yīng)該是DataSaved(uint8,bytes)
。
如果ProofType枚舉的類型從2種變?yōu)?57種,在remix里重新部署合約后,可以看到uint8自動(dòng)變?yōu)閡int16,如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-465914.html
到了這里,關(guān)于Ethereum以太坊事件日志查詢參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!