当前位置:首页 > 物联网 > 区块链
[导读] 以太坊区块链使用修改后的Merkle Patricia树进行状态认证。这使区块链节点在每个区块的整个区块链状态上达成共识,并使轻客户端可以为任何状态信息创建Merkle证明。 但是自以太

以太坊区块链使用修改后的Merkle Patricia树进行状态认证。这使区块链节点在每个区块的整个区块链状态上达成共识,并使轻客户端可以为任何状态信息创建Merkle证明。

但是自以太坊初期以来就可以进行状态Merkle证明验证,但直到最近才将其添加到JSON RPC API中,因此很高兴看到更多应用程序利用此功能。

存储Merkle证明仅对特定的trie root(即特定状态)有效。因此用户或轻客户端应用程序应该通过运行轻客户端或信任由多个证明方进行多重签名的状态根来信任该状态根:更安全。

账户和合约变量查询

在本例中,我们将使用web3.py构建一个merkle证明,证明指定的状态根中包含键的某些值。

Web3.py尚不支持eth_getProof,因为在撰写本文时并未合并PR。因此可以改用web3.py的这个fork:https://github.com/paouvrard/web3.py/tree/EIP-1186-eth_getProof

编辑(2019年10月):eth_getProof现在在web3.py和web3.js中可用,但在Metamask提供程序中不可用。

Web3.py连接到以太坊节点,并通过JSON RPC或IPC API eth_getProof进行状态查询并提供证明。

合约状态变量查询

简单的Solidity合约,我们要在其中证明映射键的价值:

contract Proof {

string public greeting;

mapping (address =》 uint) public my_map;

constructor() public {

greeting = ‘Hello’;

my_map[msg.sender] = 33333;

}

}

验证“ 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)

通过上面的脚本,我们现在可以构建和验证帐户和合约变量的状态Merkle证明。 请注意,verify_eth_getProof(…)仅验证包含证明,并且如果包含排除证明,则将返回False。 可以通过eth.getProof(。..)返回的“proof”对象来验证排除情况。

合约变量如何存储在Patricia trie中?

为了存储变量,EVM根据合约中定义变量的位置使用key:keccack(LeftPad32(key,0),LeftPad32(map position,0))。 此处有更多详细信息:https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getstorageat。

web3.py分叉提供了方便的storage_position(),它返回所请求的映射密钥的Patricia树存储密钥。

作为比较,Aergo Lua VM在trie中使用key存储变量状态信息:hash(bytes(“ __ sv __” + variable_name + [“-”,var_index],‘utf-8’)),其中var_u index是可选的,用于映射的键或数组的索引。

证明验证码

这是一个很好的图表,解释了patricia树中不同类型的节点:

来源: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

下面的代码通过迭代验证节点(上图中的extension、branch、leaf…)来验证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

结论

这是一个关于如何使用包含/排除证明(inclusion/exclusion)来查询Solidity合约变量的快速概述。 eth_getStorageAt和eth_getProof实际上消除了在合约代码中定义getter的需要,因为getProof API直接查询了trie状态数据库(获取另一个合约变量的合约仍然需要getter)。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

上海2023年9月21日 /美通社/ -- 由于在雇主品牌建设拥有优异表现,台达于9月15日在 “2023 HRoot 人力资本论坛·上海站”获颁“2023大中华区卓越雇...

关键字: ROOT MIDDOT EXPLORING

SanerNow Risk Prioritization依托CISA的SSVC框架,以SecPod著名的漏洞情报为基础而创建,可有效地对漏洞、错误配置和其他安全风险进行优先级排序,并改善网络安全态势。 加利福尼亚州雷德...

关键字: 安全漏洞 SE RIO BSP

(全球TMT2023年9月12日讯)Microland Limited与Serco AsPac宣布建立战略合作伙伴关系,以推动数字化转型,利用云提高业务敏捷性和韧性。Serco AsPac是全球最大的公共服务提供商之一...

关键字: LAN MICRO SE RC

此次合作将专注于提供卓越的数字公共服务、促进业务增长、打造增强的数字体验并加速云采用。 印度班加罗尔2023年9月12日 /美通社/ -- 今日,Microland Lim...

关键字: LAN MICRO 数字化 SE

(全球TMT2023年9月7日讯)思享无限控股有限公司宣布,将以300万美元投资DVCC TECHNOLOGY L.L.C(DVCC),以获取其30%股权。这一重要举措标志着思享无限从移动娱乐向元宇宙生活方式的转变,也...

关键字: DVCC SE RS AI

北京2023年9月6日 /美通社/ -- 思享无限控股有限公司(以下简称:思享无限,纳斯达克股票代码:SJ)对外宣布,将以300万美元投资DVCC TECHNOLOGY L.L.C(以下简称DVCC),以获取其30%股权...

关键字: DVCC TECHNOLOGY SE RS

(全球TMT2023年9月4日讯)当地时间9月1日,荣耀终端有限公司CEO赵明在2023德国柏林消费电子展(Internationale Funkausstellung Berlin,IFA)开幕日发表题为《展开未来(...

关键字: 荣耀 折叠屏手机 SE RS

柏林2023年9月4日 /美通社/ -- 当地时间9月1日,荣耀终端有限公司CEO赵明在2023德国柏林消费电子展(Internationale Funkausstellung Berlin,以下简称IFA)开幕日发表题...

关键字: 荣耀 折叠屏 SE RS

国际酒店运营商升级其在线支付功能 上海2023年8月28日 /美通社/ -- 加拿大金融科技公司Nuvei Corporation(以下简称“Nuvei”或“公司”)(纳斯达克代码:NVEI)(多伦多证券交易所代码:N...

关键字: 代码 IP SE 纳斯达克

韩国济州2023年8月25日 /美通社/ -- 2023年7月25日,TÜV南德意志集团(以下简称"TÜV南德")与HSEwind关于...

关键字: WIND 风力发电机组 SE 海上风电
关闭
关闭