當(dāng)前位置:首頁 > 物聯(lián)網(wǎng) > 區(qū)塊鏈
[導(dǎo)讀] 以太坊區(qū)塊鏈?zhǔn)褂眯薷暮蟮腗erkle Patricia樹進(jìn)行狀態(tài)認(rèn)證。這使區(qū)塊鏈節(jié)點(diǎn)在每個區(qū)塊的整個區(qū)塊鏈狀態(tài)上達(dá)成共識,并使輕客戶端可以為任何狀態(tài)信息創(chuàng)建Merkle證明。 但是自以太

以太坊區(qū)塊鏈?zhǔn)褂眯薷暮蟮腗erkle Patricia樹進(jìn)行狀態(tài)認(rèn)證。這使區(qū)塊鏈節(jié)點(diǎn)在每個區(qū)塊的整個區(qū)塊鏈狀態(tài)上達(dá)成共識,并使輕客戶端可以為任何狀態(tài)信息創(chuàng)建Merkle證明。

但是自以太坊初期以來就可以進(jìn)行狀態(tài)Merkle證明驗(yàn)證,但直到最近才將其添加到JSON RPC API中,因此很高興看到更多應(yīng)用程序利用此功能。

存儲Merkle證明僅對特定的trie root(即特定狀態(tài))有效。因此用戶或輕客戶端應(yīng)用程序應(yīng)該通過運(yùn)行輕客戶端或信任由多個證明方進(jìn)行多重簽名的狀態(tài)根來信任該狀態(tài)根:更安全。

賬戶和合約變量查詢

在本例中,我們將使用web3.py構(gòu)建一個merkle證明,證明指定的狀態(tài)根中包含鍵的某些值。

Web3.py尚不支持eth_getProof,因?yàn)樵谧珜懕疚臅r并未合并PR。因此可以改用web3.py的這個fork:https://github.com/paouvrard/web3.py/tree/EIP-1186-eth_getProof

編輯(2019年10月):eth_getProof現(xiàn)在在web3.py和web3.js中可用,但在Metamask提供程序中不可用。

Web3.py連接到以太坊節(jié)點(diǎn),并通過JSON RPC或IPC API eth_getProof進(jìn)行狀態(tài)查詢并提供證明。

合約狀態(tài)變量查詢

簡單的Solidity合約,我們要在其中證明映射鍵的價值:

contract Proof {

string public greeting;

mapping (address =》 uint) public my_map;

constructor() public {

greeting = ‘Hello’;

my_map[msg.sender] = 33333;

}

}

驗(yàn)證“ greeting”和“ my_map”的存儲證明:

from web3 import Web3, IPCProvider

from web3.middleware import geth_poa_middleware

from web3._utils.proof import verify_eth_getProof, storage_position

w3 = Web3(IPCProvider(。..))

w3.middleware_stack.inject(geth_poa_middleware, layer=0)

w3.eth.defaultAccount = Web3.toChecksumAddress(‘0x.。.’)

block = w3.eth.getBlock(‘latest’)

greeting = “0x0”

my_map_sender = storage_position(w3.eth.defaultAccount, “0x1”)

proof = w3.eth.getProof(contract_addr, [greeting, my_map_sender], block.number)

is_valid_proof = verify_eth_getProof(proof, block.stateRoot)

通過上面的腳本,我們現(xiàn)在可以構(gòu)建和驗(yàn)證帳戶和合約變量的狀態(tài)Merkle證明。 請注意,verify_eth_getProof(…)僅驗(yàn)證包含證明,并且如果包含排除證明,則將返回False。 可以通過eth.getProof(。..)返回的“proof”對象來驗(yàn)證排除情況。

合約變量如何存儲在Patricia trie中?

為了存儲變量,EVM根據(jù)合約中定義變量的位置使用key:keccack(LeftPad32(key,0),LeftPad32(map position,0))。 此處有更多詳細(xì)信息:https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getstorageat。

web3.py分叉提供了方便的storage_position(),它返回所請求的映射密鑰的Patricia樹存儲密鑰。

作為比較,Aergo Lua VM在trie中使用key存儲變量狀態(tài)信息:hash(bytes(“ __ sv __” + variable_name + [“-”,var_index],‘utf-8’)),其中var_u index是可選的,用于映射的鍵或數(shù)組的索引。

證明驗(yàn)證碼

這是一個很好的圖表,解釋了patricia樹中不同類型的節(jié)點(diǎn):

來源:https://ethereum.stackexchange.com/questions/6415/eli5-how-does-a-merkle-patricia-trie-tree-work

![](http://bitoken.world/wp-content/uploads/2019/10/11.png)

python

下面的代碼通過迭代驗(yàn)證節(jié)點(diǎn)(上圖中的extension、branch、leaf…)來驗(yàn)證key值和expected_value。 如果Expected_value等于證明中包含的值,則返回true。

def _verify(expected_root, key, proof, key_index, proof_index, expected_value):

‘’‘ Iterate the proof following the key.

Return True if the value at the leaf is equal to the expected value.

@param expected_root is the expected root of the current proof node.

@param key is the key for which we are proving the value.

@param proof is the proof the key nibbles as path.

@param key_index keeps track of the index while stepping through

the key nibbles.

@param proof_index keeps track of the index while stepping through

the proof nodes.

@param expected_value is the key’s value expected to be stored in

the last node (leaf node) of the proof.

‘’‘

node = proof[proof_index]

dec = rlp.decode(node)

if key_index == 0:

# trie root is always a hash

assert keccak(node) == expected_root

elif len(node) 《 32:

# if rlp 《 32 bytes, then it is not hashed

assert dec == expected_root

else:

assert keccak(node) == expected_root

if len(dec) == 17:

# branch node

if key_index 》= len(key):

if dec[-1] == expected_value:

# value stored in the branch

return True

else:

new_expected_root = dec[nibble_to_number[key[key_index]]]

if new_expected_root != b’‘:

return _verify(new_expected_root, key, proof, key_index + 1, proof_index + 1,

expected_value)

elif len(dec) == 2:

# leaf or extension node

# get prefix and optional nibble from the first byte

(prefix, nibble) = dec[0][:1].hex()

if prefix == ’2‘:

# even leaf node

key_end = dec[0][1:].hex()

if key_end == key[key_index:] and expected_value == dec[1]:

return True

elif prefix == ’3‘:

# odd leaf node

key_end = nibble + dec[0][1:].hex()

if key_end == key[key_index:] and expected_value == dec[1]:

return True

elif prefix == ’0‘:

# even extension node

shared_nibbles = dec[0][1:].hex()

extension_length = len(shared_nibbles)

if shared_nibbles == key[key_index:key_index + extension_length]:

new_expected_root = dec[1]

return _verify(new_expected_root, key, proof,

key_index + extension_length, proof_index + 1,

expected_value)

elif prefix == ’1‘:

# odd extension node

shared_nibbles = nibble + dec[0][1:].hex()

extension_length = len(shared_nibbles)

if shared_nibbles == key[key_index:key_index + extension_length]:

new_expected_root = dec[1]

return _verify(new_expected_root, key, proof,

key_index + extension_length, proof_index + 1,

expected_value)

else:

# This should not be reached if the proof has the correct format

assert False

return True if expected_value == b’‘ else False

Solidity:TODO

結(jié)論

這是一個關(guān)于如何使用包含/排除證明(inclusion/exclusion)來查詢Solidity合約變量的快速概述。 eth_getStorageAt和eth_getProof實(shí)際上消除了在合約代碼中定義getter的需要,因?yàn)間etProof API直接查詢了trie狀態(tài)數(shù)據(jù)庫(獲取另一個合約變量的合約仍然需要getter)。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉