专栏首页本体研究院本体技术视点 | 关于Merkle Proof问题的一点思考

本体技术视点 | 关于Merkle Proof问题的一点思考

这一期,我们来说说 Merkle Patricia Tree。

Merkle Patricia Tree(又称为 Merkle Patricia Trie)是一种经过改良、融合了 Merkle tree 和前缀树两种树结构优点的数据结构,是 Ethereum 中用来组织管理账户数据、生成交易集合 Hash 的重要数据结构。

MPT 有以下几个作用:

  • 存储任意长度的 key-value 键值对数据,符合 Ethereum 的 state 模型;
  • 提供了一种快速计算所维护数据集哈希标识的机制;
  • 提供了快速状态回滚的机制;
  • 提供了一种称为默克尔证明的证明方法,进行轻节点的扩展,实现简单支付验证。

(引用来源:《Merkle Patricia Tree 详解》 https://ethfans.org/toya/articles/588)

图片来源于网络

本期要与伙伴们分享的是本体技术人员在 Ethereum 平台上进行 Merkle Proof 问题分析的实践。

背 景

在 Ethereum Solidity 合约中进行 mapping 存储时,如:

mapping(uint256 => bytes32) public Transactions;

Ethereum 会将该 Storage 的 Keccak256(key)和 value 添加到其 MPT Tree 中,MPT 根 hash 在区块头的 Root 中。

如:

Transactions[000000000000000000000000000000000000000000000000000000000000023e] = c9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394

通过 Remote Procedure Call(RPC )来查询存储状态:

Request

{    "jsonrpc":"2.0",    "method":"eth_getStorageAt",    "params":[

合约如下:

"0x8D069f5a5B877D9ECc5EE28715982c12f3879414"  

Keccak256(Key)见下方:

 "0x1fcb2eebde5e81eef238fb4a50a1d5b718fbbb2174404a7f42c53a14e792f2b8",   // Keccak256(key)        "latest"    ],    "id":100}

Response

{    "jsonrpc": "2.0",    "id": 100,    "result": "0xc9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394"}

通过 RPC 来查询 proof:

Request:

{    "jsonrpc":"2.0",    "method":"eth_getProof",    "params":[        "0x8D069f5a5B877D9ECc5EE28715982c12f3879414",        [            "0x1fcb2eebde5e81eef238fb4a50a1d5b718fbbb2174404a7f42c53a14e792f2b8"        ],        "latest"    ],    "id":100}

Response

{    "jsonrpc": "2.0",    "id": 100,    "result": {        "accountProof": [            "0xf90211a037a47f689332918eb714ab1e66236242545ac695160344a3c696e6c994df2f9ba01be2b144394b850a2ea9d6aec2552c4175da0b82f4b74abe6f49c87ea70aed44a0062840046cde35009205683fd0ca59c5793135f14a3aebba616b9bf8ac2ad44da0f352f50900ab4fd7770d4cf36cb0fd2a84964632940710ef3adf83cd780b1b4fa02092fbe693d01c923015853c033b85c2a1bdb7b8b44d48133a272581b747b240a075487bb84198538367ef3f85534983d12af8b165a89a683cb138a15f5f556907a03aa3fea81951ee6036971559abd8330acc025b5317468fe2e92bc6ba47e1459ea028384272abf5b4ec060a9ea8034c8bbfd1679431b68c5157749309bb9ae4b287a03f9c719c31750107717d3006b2872b7f1fb9b6b296c3bf44f056ee08380d6a82a0954cddbf9ef981a04a78f6fc20bb0527398445664e6985c68e9b5941371bf94aa03fe5c75fb26d08b42acfeadc761a4dafb7fbcdd9ce3e75214f77686924fa0ef9a063a5a67eb140cc42101786342b6d7f750b89e5b20b205dd959ae57cb763d6b5fa082063bec809c435e52c2296ac0a16e59605d147145a1568289cac2a9c74d9883a030c26478c10d82813700bf1432dc61def2efb7c2ffd1b02e8b93d01ef874b4cda04dc2690d190da8cf0d204301366bb551d35b92112822c818989ad17ae06ba0baa097b8022a1d95227c998fc32ebaa4265378fb51a9a64ee0713c77c0c32829dd7580",            "0xf90211a01967d6ad86e0730a2080481c71731dca40762c72dfee1199a5c0cae0ad08c815a06be9d681f1bf54fa3e6687840a58fbd826e0aa79dd3da2d372a080d618572ceea0a15783c4a8c1462a8de73cbbc8c803bff3a0bf89c5a51ae7a4810a4c8176a0c0a01d241a4734f93a3843310175ff405c0ee8629df6a3641f66c241f8d4bb3c6262a00aaf8af1a6854efa2807467332272ea06ce7920e5bcd292e953b6f323d0a5b58a0499a9602d15083b25b17409abc4e818b974fc39cf8939e5199a94c44d0c35629a08de076c1851a3d9d564b0ff6e43d02f695eaa518b1865221fc4ca104a417e658a06fad23a4afae1434d7b4061fea88a3d87b1e3829d513131a71d707aca249a6e8a09695acb56f797494aaeba35fe13be286eb3d47d5f6a2ccd543cfad14cc7fa0fea0d701ca5cca54ff8c58cee9c52b55dcdbb3e2b7dd132aac41445f09c4d30b6325a07d0d3d643cbd4c8a46e495acb14acad598a2359e62bcd4abc7500b4f21f91158a0f0302cf6e2f3ea2ef585abc4dadf329d98c28effc2a7f7f3c04b8c878fd344e3a05ce175459974f2d3c7e0e4bd452db57b0b3d2a5e661cf3a14880852fa70bab06a06287f026c64125d7c21ecbc7458082c5d68e05f661df3d6f0d435ea8bf2ff6f0a09846bc626553738c12a80156bab4705ab11d0062ea376ca7810c4f86132f6085a06ffe94188ced1008695992a9abacfb46ae2f533e9384f9ed539aa5d7b1947f7e80",            "0xf90211a021e3e853b8dece06f9fc4a641babe042864b2f6dd9b12d30de731d8b17902716a0095a4ce915ce44a7ac44c3bfcf49b073939ee4fccaa51b7d14d62623e1a2bce4a00ddd94bef6d21bd286569321b05b320944e9a1cf9636e81b3852d22249a9e053a04ff1f0528d1696b89b29b1cfae5209533e7afe8a544225e35ad851e4fbeb98d8a0916b29035575970a5813a7990b124dd1af37dc7b9df30057551da0ff0a57f050a045179e11fa00f5e69a280a90c406692b0005f82c57cfffc596fad6ff59c02efda0e1375101aa6c6c1822162197bd8a1a8235a39734c2b7e555215ff18a1c0bedbba0808cd23a299ebfce440a79228fcad08d5ffaf790f4c687123464f3ddb1afeb47a0dd8552acd4732b195703df4b7df5bc06b7b870b95852f32202e9b85c5e2bd93fa0b5f4abb654575428517a44ee726eb22f980281c6bb3220dd9e9efc23234cf9aaa0b454b3abfa35296992b4e5991db6922393a407340e6bcfa6a68b20734ee09516a049043d5e13700512792f8fe9361712b45adc0607b0e723c6cec7f18383be8949a01c6b45f8d70a93d5571c15eae4c3131564664f0b20cd72bb93b16112cfd1b322a05152df97d6f63c484962c11decde8d6d18a0c6e12905a5b20851ee9174eb264da0b47c62694515803792dac01204abf573ea15b699620ffc30b7d6e7d9eb968467a0e77c699bd3f0b52433eb5ebad9542b6c30b2c495ad1785ba4643bc21a79fa74280",            "0xf90211a03c697c0aeceb3466b845dc80fb034747d69696813023c337453e305d2a7e9283a0e769a5551d80ecb3514d753c7ddb5d5301b7c3f8cf463267f39606e292b37ea3a047aca9091e3bcc3cd8038a1f20169cc5ddcc68f8b1dbd3ba2f4dc5d1f46afd34a0e32798bac3a869e0eaead3f7858a18fdf5b3d7c7ba224b47c0a2d35d38840e2ba0d19e831f941c5ba8d3d4111dbbb2bd63dcbd5ec64927400b6e0aa0fea47a0024a0d925643873f60b3b56f28bf8f45138874be6117cf730d3db0db5279a1c80c290a0787f885a477b35102416253d1a9211110f0353db7c50223729371afd20ea8174a02107d23b98ab8944fb6d07a64c14b4b7ae14bd3651ac07a9e548bd2df5a845b6a0a367dc4d90b37f043dfd453d9b051a62d0e1fa018c95a1487fce7dc102304121a051db6f80b22224a32c17e0645d7ae3d7d486cb20fdddb8e25a081379f25813bea0631d8043dae7633e9ab7b95fb4ec40f768ba59858d520e0ff3962c5dcd15300ea0f88cc4478e6ea0012866adf76b5507679c9cc598efea9a7c5d6805d3b8be2c54a0b3f68cb702a69f78a30073364be2962ee157213e65097bff98e22a9879fff6d3a0885d3993a43605ba6bd24c38e5eac3a7ffd47be6f674550dca2a45539fac64a3a0f2625009a9644d7c00375bcd8518259d887847719f798445f32de7f8706dea23a0f6919f32c1a0c7b38c79c6632309fe80f82c5f0c6a2b866387bde731a4342b6e80",            "0xf90211a0c423763462f31f12c0e32181601e76dfafadf44bab89ada67570d43dd7b88862a0f0598e24ce6df2c89661b8ed63424e1014ba21c664a8fb7d0ba83d8d99bd53f7a08da8c8d66fa171ce6fa4d3c701072ded631c0708580727f8ab676f8d3f82947fa01f45118988e3315c230ec4e23252b94050aa816238f3fa9f851feb596166c97fa0f1344000f147fd5049433321a125ee30070e74a304d1845ea23077afc2054241a02247602172797f0f106b4a15a91981cc747c108e9a479c893edb30b64232c3b8a0ff46da37205525038deccfef623d3fdd85ca73484c8e0832cfc89547c6962a52a0d372f69118c5939534627ea6d4f32fe080fd18dd1e8ddf3a2c27c515d51518ffa04c7a6b2e5fb7d545ea92fe128320e3c2545147a4cf64d16a1a88d4369cbba608a09b9de9fbf4595d4a5ecbfcb1384a51397c6f4d519befe13148d3f49aba8d1659a023542c1e1efaa76dde006b67542d77d596fddaf27feb6269dd651f5b61b58ae3a071b3caa47a51d29bc62a14bfec2ef82444fdcb38a304f3416e97f56b9acb1e83a0bdfb50a24c2fe4cfba92ac3bb176d764ecb306f1fa812c183a82eeea3fc39a18a0c53d014d235ebfbf41b3108bddca048dbcbae4fa1530b4c571370883c81e5af5a0e7c0f349d1346dfa3d8bf54cf86dac87c31829a3b7dce889fb74dced3041cfdba0bac0f43b139479c08a92203bae67d77cae368e1bf3857dd2d038eec0978f138280",            "0xf90211a0deff2c78facd67bdd752adfaed2d1bd785122e30fb9b831bd815417f0292baeaa066a94cc4537bbfc07d4c8df80bc1eab9c51c7c228a1aa84f383074f098dbcf9ba054dbc3bd1c18ea17e0175e16358ad7e30de3e3c587147a6ac35ded187b59fa53a0b9f37a7c692b069f786d4c251d6092918657a0ab80b7e342da77741f9169ab09a0bb3ae6c0bae28ed8c32b94712de09a5faa48aa52b6fb1972eb5ff6a20d05cfe7a0cf3eeaf40fb8bc2084c04e5db06a6ff5083967ec7c3b50f6b610c5fe7ef4b074a0c4a44c5a484ce35240af24aa25a6f6d730ee0326231a898248cbc63884604387a0d718c7d24ff060799e644104035c6959bf80e7040decabaaff05b0bd062a811fa0253837f8e0975cd69ed31c364cf19049e28e54d0a4333c0129bd2238414553cca0d0128a60d2b3433a1710c2530b09226c6635fa8469abe651bbfb2f69cbb31150a0c81ff500df29c6eb34e77d9249a68fafb11c1fab9a0c612c34599e4e0a99c783a018befafc88076ed909d12a2eb2b12a4be09829e663f57a0c6cc6707aa94c6e7da04d3f8f50c468843000ca435a47ceb4f926e94321f74cb4dce5f4b34efffe85dba0180349f996985ce0a820d35bff94185e2e5ca7e14ad6b5515da5e2443ec644faa07ba8664c88dbdf5ba34016550a23a1e40d9f88c03a1081c7296fe142006afa26a07750aad81ec12bb52b22af86642d6017175e2ba8f9cf9cb0b4e09535c072cb6580",            "0xf85180a0db9b674ce8e64dcc246c4708218bc773662ae6b2de2d6e6953cbcb240b3709988080808080808080808080a0f60803ca53282fb28312d69ae9bc3dd29f0e131a19da8591b2382b7fee200219808080",            "0xf8669d3492cbe0c4cc38b98898c3be4350da6c91ec0add9503273a55901cb4c8b846f8440180a05c485d8c06df97d5ae39ac0685a09af428162e12164f4eafef6085f62439ea69a0d5415eb1d2e74e08407476508707137c7e35dbf42995dd07e273c83b4c384c9d"        ],        "address": "0x8d069f5a5b877d9ecc5ee28715982c12f3879414",        "balance": "0x0",        "codeHash": "0xd5415eb1d2e74e08407476508707137c7e35dbf42995dd07e273c83b4c384c9d",        "nonce": "0x1",        "storageHash": "0x5c485d8c06df97d5ae39ac0685a09af428162e12164f4eafef6085f62439ea69",        "storageProof": [            {                "key": "0x1fcb2eebde5e81eef238fb4a50a1d5b718fbbb2174404a7f42c53a14e792f2b8",                "proof": [                    "0xf90211a00a10dd6e66aac750896197d78be889068faf3c4b76ab4176103e677fe89fab4ca08eeb2dbef2bade354801c64ed39f3be489e759161a217453b532738bf2069caca02546f78e90b7a6a6c1b0c642d577827a606865755661b52ef38b54488f9eb484a0cb3051b6cbaa65069811c98be990187a3fcc590e263bec91e5371f1a412fd878a08c881c284d6c99d1e3f99cc6e2d483974ba3f9d0d95a45de3368ff84293878c6a0529955848f095bdf6892846856e48d993cf158c6224146b8f309dbe025e8ec06a00bbb03e43b4081fd3fd05b815677f4f2a488507eca7b96b8955103949ac9ea5fa0bcc40aa91452398ee8a5d91a08f7d98c0dabc83b31fb6c5320b37fa96aa48518a03039a130a4454460f504ce48b9ca0caa73ab9550097d397144b4cea8d5882270a0d824dbe84c729c486461978fb3f9ea6a2c15b2c1c60c8c56ec5a4aea39d6e832a0ff9fc42e3d1341ef6fca3e82cfde7343bbee875b70b6fb4379166ac2f761e9f5a0868fda2f9997fec29ead6c1e680af995702684dc1f60dcae188a39b8d070ae83a00711c902ca62e75c33510f916cb335d3ee34a08c5caab7ef1149770a85ee4428a06737b42b5a7af1a9c37a1287df55c34f21e3a9bf757decdae78b665da5edf71fa0324005a2bcea95f567f02262af2771389dcc8f08b6a78f0d813c298eb1a61c5aa010c1074ce834658cb58e90807ebe14d1d4334a91fadc80bcc9129e18b52ae3b680",                    "0xf90211a009f17b55b0d98a43ba5b5d2ec88005c9710cfe32133b2a807c719ce4ed65edc4a0b1c0ce8590941d0752d7fdce66868fb013140e7dc6027b5e2c23a3c6a6ce53aaa04015f40fddb19e0bda07adac359492ce6f0079cb099c8914feafb994fca9e542a08610b133070a1137c94d91fdfd0f0a05502899b592f4f15f410758f78a77fdeca075f1b9beb323db9a46d48e9205e850f0f2533dd56862d360ba9b063b7c34f2cda058ae6d265bece840f88e830d747dd7ce3e1c5c5fd1aef7119a9c9fbf6aa8be0ca08a08e8713bb888a40c9ffdad08b4862599776872b842aaee7ab90f6e767f180fa023c65bba27d8028221315ff066e781d34768dfeeedd749396e0a198a32a4ce79a015b01441926647406dae4e42529b263eb5a01336bd7411ecf442f03366afefb1a03a03533855865496fa36eb3df8fe553a66c5aaa7cb151410fe15196352177df6a018ee20cb1608aac6fbf76fbfad78689984233bf743f36548c9ad4990f1397e8ea0aac7297e3f439fd5d865cfc40c4b5b906c077721f43f6dd95f933f0ab0d81ae0a08112446d47f4e4cd6a06bffd47ccbacda1aed9632d4a8cec014b6706ae225b2da0d07acb4d7e0a00e575275081e2fcd16f841d5317deb052f6be4948840dfed72ba0156d08f0bf1ae403dead0ff045d131d441c0435f33b8eb46e42c0f046e9c4330a0fcb49d8a583b46077537d6c4b8c41b43045870d790d7c1615f1e91ff41ea739580",                    "0xf90131a099108d0584b26f16f4d6ccb0fc755ca0817b9eb7296b314b67d69ea19b950a29808080a07a45f9339b67c4bf4f3d958982de54617bb964fb0c708119d20464a2fb6a572ba0706c33de1de9d7aa2c4e709d3863cf64f70dadb6e2297b52033b22a5c6ca4b9180a0534da7c159bdffff15ecca666fc14d074e933331e2325c9665effaa55bfeb444a0b972e411ee8dd486cdc6f9ccc57ef9d6edd08446443eecbdaa7cc77e539e95ef80a0665f8f2dd1234858a5011871e8601129e816daf91fd71e5032eda57c09682befa0e442c66db240893810a43bf177b4e67dfdab6dc5cabf8f7ec3865c7537d3aab1a05bcaa30f48bad7b070bfaf1ffbd8ed58b519d373169f6255f2aef7d4c728dc2180a02c3eabfebe89a209e06a3fa675fa0fab11bf07c33b830357988b75ec0165e6de8080",                    "0xf85180a0aff4c547385ac713d0f2b6dab38e28d444d3ece2d6de4914aeab4ee0b4e6c5e98080808080808080a0a1f1f7d3675acf565d621077d0619f0babe86b8631317c44a20927958e7074a6808080808080",                    "0xf8429f20020928fbe56afa5fcb40252eb39ba9b27bfe8961de523c8b3f73268d9af2a1a0c9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394"                ],                "value": "0xc9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394"            }        ]    }}

可以看到 RPC 查询 proof 返回的 value 和查询 storage 返回的 value一致。↓↓↓

"value": "0xc9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394"

在其给出的 Mekle Proof 证明中,叶子节点上的 value 值和 storage 一致 ↓↓↓

0c9f550d3ba5a3c584de7e13d7b4ead29cc8bf09d530c04c054320789384d3394

问题分析

如果 value 最前端为0时,查询数据的结果会是什么?

图片来源于网络

下面给出案例:

transactions[000000000000000000000000000000000000000000000000000000000000023d] = 00b93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460

注意 value 最左端是以0为开始(使用十六进制表示)

通过 RPC 查询存储状态

Request:

{    "jsonrpc":"2.0",    "method":"eth_getStorageAt",    "params":[        "0x8D069f5a5B877D9ECc5EE28715982c12f3879414",        "0x9a215a401a334e0d4544e852c160ee647d373a69d4f8652344f59d6ab6019d14",        "latest"    ],    "id":100}

Response

{    "jsonrpc": "2.0",    "id": 100,    "result": "0x00b93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460"}

查询存储返回的 value 正确,通过 RPC 来查询 proof

Request:

{    "jsonrpc":"2.0",    "method":"eth_getProof",    "params":[        "0x8D069f5a5B877D9ECc5EE28715982c12f3879414",        [            "0x9a215a401a334e0d4544e852c160ee647d373a69d4f8652344f59d6ab6019d14"        ],        "latest"    ],    "id":100}

Response:

{    "jsonrpc": "2.0",    "id": 100,    "result": {        "accountProof": [            "0xf90211a01ccf43be1b9ed4e3c0743265505a7d983a77dd44cea2c0289e4d7917bcc8a0daa0d769519ff26bfb05f7d49ee2600f5d1f7ded5a8135e3afd74cf79f9d8bd0bd2ea02f53857ee4959f84b0e3c9f7910cdc582c941109cafed09c42e4de381670dab5a0543c80e8e5fd350bb774015b438476106b359084649376a01389296d3a8154cfa0018d41a299307c56d49e28cec08d72f32ef17bdd70d358343cc43974ded8e9f4a00071b5f0e4e1287a9d39cb015c04438dd240c00665b4a42b257a9d60d178a875a08c90ccc16848066ea0c73dd211c9693aadefa36eae18b60ea39e089ad4a869a7a0f69bbca9804df5193b9d977f45056a8572da60eb3196c9087194ed98b76d3c3ca0c6bea867a1facd53ae928888261b18a20d905dd8d6a34a74020fd5486b90b9aba0cf8820953d589a8ae1d078680d8891f10602899107383a420058702d98e1d680a0d2b7a22692d7fcfed90eb87732aff194b16cab97017c9ff30a8201cb6d1b13f4a005be7f27341d531a8b3b92c33ada4c213c1404ef7a5a638847ce0ec2a16bcc43a0230045ce40d610fa82bb5ab721f42fb894a5041f1321dbd8339503ed9679ea2da0a3e4b15ea40e2dd7f1cfd1317f882d67e8d37eba8f662cf8f0eaca41c52c893ca0a0a542f05ff3bd2eed25ec32b1c6b33dd8888470ee2dc8671e62900ccd1bc5e8a0e8fddcd7af7c991d549144c4dac25b0b5b1e5b9e926a2b94c213af2099f0422780",            "0xf90211a04fa71603858ee421bcd6c843ce11e1efe7e51edc1fa051a832d37b8d892decc6a06be9d681f1bf54fa3e6687840a58fbd826e0aa79dd3da2d372a080d618572ceea0394e06f0339118de7730091f6b6ee80ce49bf6c7aa7e0da1dedbdfd240bfc245a0067cb28df6cbee1d6a07e5b4c58cb5cc85a78a10c99b4850fa420a1614b37ca5a00aaf8af1a6854efa2807467332272ea06ce7920e5bcd292e953b6f323d0a5b58a026004214b3168451d39e25063e99188b4823a44e6753fc6c12c3d5ef409a0215a071569a9ef2b28c7617d1c1c013b201f14480b72cf52bff1c734601d0e5804c05a0099c00309a6e1ea8ea4cd6bb5d44c6f1c827a5aab6fcbfa88448bb22218729bba002ab52a428317181820af76bb7bb58e6be7c0c4f01a44d7fa3512004e99d0b24a08f1c3a18f5820b8d5e9da277ee04da367ef27416e8ee97bad8f10a6be9a2e08ba0a7c76705412cac40175ea08bcdf86948262a49b368f41dc9534c605113c10a5da0ee3fc290b0319d88958ae593c0d1811fa66254044e25a5e49aed566e0242b15da045a4abeb8348d7cd2ab6147f968c0c8ccefe489405afeebe656b0445da6e9325a00cd3238722b4db11c374fdc0f4c3e728886d3fdd2ac58e9fca21decee12ba25ca0e7aa5f2092cda86cc43ceabc42a412ab93c61b1fca2df91ce51faee5d3faa32ea0e27ef5d3fa2dcaa1c6f76d94f3b0b3b42d5d79fec78e6f88d2fb45957413890f80",            "0xf90211a021e3e853b8dece06f9fc4a641babe042864b2f6dd9b12d30de731d8b17902716a0095a4ce915ce44a7ac44c3bfcf49b073939ee4fccaa51b7d14d62623e1a2bce4a00ddd94bef6d21bd286569321b05b320944e9a1cf9636e81b3852d22249a9e053a04ff1f0528d1696b89b29b1cfae5209533e7afe8a544225e35ad851e4fbeb98d8a0916b29035575970a5813a7990b124dd1af37dc7b9df30057551da0ff0a57f050a045179e11fa00f5e69a280a90c406692b0005f82c57cfffc596fad6ff59c02efda0e1375101aa6c6c1822162197bd8a1a8235a39734c2b7e555215ff18a1c0bedbba0808cd23a299ebfce440a79228fcad08d5ffaf790f4c687123464f3ddb1afeb47a0dd8552acd4732b195703df4b7df5bc06b7b870b95852f32202e9b85c5e2bd93fa0bac205c6ca9b3b95b2aba520002a75a63ab89ec6605740bbb7043326d33c12cca0b454b3abfa35296992b4e5991db6922393a407340e6bcfa6a68b20734ee09516a032b16162e91d2f592e495d2b83f90701a75b0fd69c22a6dd6483bb0a71c07d10a01c6b45f8d70a93d5571c15eae4c3131564664f0b20cd72bb93b16112cfd1b322a05152df97d6f63c484962c11decde8d6d18a0c6e12905a5b20851ee9174eb264da0b47c62694515803792dac01204abf573ea15b699620ffc30b7d6e7d9eb968467a0e77c699bd3f0b52433eb5ebad9542b6c30b2c495ad1785ba4643bc21a79fa74280",            "0xf90211a03c697c0aeceb3466b845dc80fb034747d69696813023c337453e305d2a7e9283a0e769a5551d80ecb3514d753c7ddb5d5301b7c3f8cf463267f39606e292b37ea3a047aca9091e3bcc3cd8038a1f20169cc5ddcc68f8b1dbd3ba2f4dc5d1f46afd34a0e32798bac3a869e0eaead3f7858a18fdf5b3d7c7ba224b47c0a2d35d38840e2ba0d19e831f941c5ba8d3d4111dbbb2bd63dcbd5ec64927400b6e0aa0fea47a0024a0d925643873f60b3b56f28bf8f45138874be6117cf730d3db0db5279a1c80c290a0787f885a477b35102416253d1a9211110f0353db7c50223729371afd20ea8174a02107d23b98ab8944fb6d07a64c14b4b7ae14bd3651ac07a9e548bd2df5a845b6a0a367dc4d90b37f043dfd453d9b051a62d0e1fa018c95a1487fce7dc102304121a07eea4e37db2b7c237fc39419f3dee0060631fb29a550aab1e9b7087d16c89aada0631d8043dae7633e9ab7b95fb4ec40f768ba59858d520e0ff3962c5dcd15300ea0f88cc4478e6ea0012866adf76b5507679c9cc598efea9a7c5d6805d3b8be2c54a0b3f68cb702a69f78a30073364be2962ee157213e65097bff98e22a9879fff6d3a0885d3993a43605ba6bd24c38e5eac3a7ffd47be6f674550dca2a45539fac64a3a0f2625009a9644d7c00375bcd8518259d887847719f798445f32de7f8706dea23a0f6919f32c1a0c7b38c79c6632309fe80f82c5f0c6a2b866387bde731a4342b6e80",            "0xf90211a0c423763462f31f12c0e32181601e76dfafadf44bab89ada67570d43dd7b88862a0f0598e24ce6df2c89661b8ed63424e1014ba21c664a8fb7d0ba83d8d99bd53f7a08da8c8d66fa171ce6fa4d3c701072ded631c0708580727f8ab676f8d3f82947fa01f45118988e3315c230ec4e23252b94050aa816238f3fa9f851feb596166c97fa0f1344000f147fd5049433321a125ee30070e74a304d1845ea23077afc2054241a02247602172797f0f106b4a15a91981cc747c108e9a479c893edb30b64232c3b8a0ff46da37205525038deccfef623d3fdd85ca73484c8e0832cfc89547c6962a52a0d372f69118c5939534627ea6d4f32fe080fd18dd1e8ddf3a2c27c515d51518ffa04c7a6b2e5fb7d545ea92fe128320e3c2545147a4cf64d16a1a88d4369cbba608a09b9de9fbf4595d4a5ecbfcb1384a51397c6f4d519befe13148d3f49aba8d1659a023542c1e1efaa76dde006b67542d77d596fddaf27feb6269dd651f5b61b58ae3a071b3caa47a51d29bc62a14bfec2ef82444fdcb38a304f3416e97f56b9acb1e83a0bdfb50a24c2fe4cfba92ac3bb176d764ecb306f1fa812c183a82eeea3fc39a18a0c53d014d235ebfbf41b3108bddca048dbcbae4fa1530b4c571370883c81e5af5a0e7c0f349d1346dfa3d8bf54cf86dac87c31829a3b7dce889fb74dced3041cfdba0bac0f43b139479c08a92203bae67d77cae368e1bf3857dd2d038eec0978f138280",            "0xf90211a0deff2c78facd67bdd752adfaed2d1bd785122e30fb9b831bd815417f0292baeaa066a94cc4537bbfc07d4c8df80bc1eab9c51c7c228a1aa84f383074f098dbcf9ba054dbc3bd1c18ea17e0175e16358ad7e30de3e3c587147a6ac35ded187b59fa53a0b9f37a7c692b069f786d4c251d6092918657a0ab80b7e342da77741f9169ab09a0bb3ae6c0bae28ed8c32b94712de09a5faa48aa52b6fb1972eb5ff6a20d05cfe7a0cf3eeaf40fb8bc2084c04e5db06a6ff5083967ec7c3b50f6b610c5fe7ef4b074a0c4a44c5a484ce35240af24aa25a6f6d730ee0326231a898248cbc63884604387a0d718c7d24ff060799e644104035c6959bf80e7040decabaaff05b0bd062a811fa0253837f8e0975cd69ed31c364cf19049e28e54d0a4333c0129bd2238414553cca0d0128a60d2b3433a1710c2530b09226c6635fa8469abe651bbfb2f69cbb31150a0c81ff500df29c6eb34e77d9249a68fafb11c1fab9a0c612c34599e4e0a99c783a018befafc88076ed909d12a2eb2b12a4be09829e663f57a0c6cc6707aa94c6e7da04d3f8f50c468843000ca435a47ceb4f926e94321f74cb4dce5f4b34efffe85dba0180349f996985ce0a820d35bff94185e2e5ca7e14ad6b5515da5e2443ec644faa07ba8664c88dbdf5ba34016550a23a1e40d9f88c03a1081c7296fe142006afa26a07750aad81ec12bb52b22af86642d6017175e2ba8f9cf9cb0b4e09535c072cb6580",            "0xf85180a0db9b674ce8e64dcc246c4708218bc773662ae6b2de2d6e6953cbcb240b3709988080808080808080808080a0f60803ca53282fb28312d69ae9bc3dd29f0e131a19da8591b2382b7fee200219808080",            "0xf8669d3492cbe0c4cc38b98898c3be4350da6c91ec0add9503273a55901cb4c8b846f8440180a05c485d8c06df97d5ae39ac0685a09af428162e12164f4eafef6085f62439ea69a0d5415eb1d2e74e08407476508707137c7e35dbf42995dd07e273c83b4c384c9d"        ],        "address": "0x8d069f5a5b877d9ecc5ee28715982c12f3879414",        "balance": "0x0",        "codeHash": "0xd5415eb1d2e74e08407476508707137c7e35dbf42995dd07e273c83b4c384c9d",        "nonce": "0x1",        "storageHash": "0x5c485d8c06df97d5ae39ac0685a09af428162e12164f4eafef6085f62439ea69",        "storageProof": [            {                "key": "0x9a215a401a334e0d4544e852c160ee647d373a69d4f8652344f59d6ab6019d14",                "proof": [                    "0xf90211a00a10dd6e66aac750896197d78be889068faf3c4b76ab4176103e677fe89fab4ca08eeb2dbef2bade354801c64ed39f3be489e759161a217453b532738bf2069caca02546f78e90b7a6a6c1b0c642d577827a606865755661b52ef38b54488f9eb484a0cb3051b6cbaa65069811c98be990187a3fcc590e263bec91e5371f1a412fd878a08c881c284d6c99d1e3f99cc6e2d483974ba3f9d0d95a45de3368ff84293878c6a0529955848f095bdf6892846856e48d993cf158c6224146b8f309dbe025e8ec06a00bbb03e43b4081fd3fd05b815677f4f2a488507eca7b96b8955103949ac9ea5fa0bcc40aa91452398ee8a5d91a08f7d98c0dabc83b31fb6c5320b37fa96aa48518a03039a130a4454460f504ce48b9ca0caa73ab9550097d397144b4cea8d5882270a0d824dbe84c729c486461978fb3f9ea6a2c15b2c1c60c8c56ec5a4aea39d6e832a0ff9fc42e3d1341ef6fca3e82cfde7343bbee875b70b6fb4379166ac2f761e9f5a0868fda2f9997fec29ead6c1e680af995702684dc1f60dcae188a39b8d070ae83a00711c902ca62e75c33510f916cb335d3ee34a08c5caab7ef1149770a85ee4428a06737b42b5a7af1a9c37a1287df55c34f21e3a9bf757decdae78b665da5edf71fa0324005a2bcea95f567f02262af2771389dcc8f08b6a78f0d813c298eb1a61c5aa010c1074ce834658cb58e90807ebe14d1d4334a91fadc80bcc9129e18b52ae3b680",                    "0xf90211a0a71d0bc11a51dbdc5ff4789472d9bcaf9b38d725f21de78d2cedc8a3768d732fa04858cc09849436ef700c4ab18ba8ac52e7fc86ade1bc44f5a40afd23eb991c66a097f1b3aacd56b899741914df77f63fc97bf20339043cdbbcbd7d9a766a8c07aca01871c14b702da06f74e7435d73d19e54ccca21c6e2416f9cd091a96b9c1bde06a0073f15a3dca2d18b0b91cf08cbd0bd4f6659d91a8e98f73401f5634e973b1dffa00ad6f48a8ae80436aeb614f71957c915237b28c0b71182fdf03a7f77307d6aaea01d243c53c260dbc6f4bd2ace94c1e569608909c83128d7a500f02028bf1d3135a0fd2a9db9b8da62844ddfc2417cae29172e08bc0f3615e0a0ad0c1415835baf3da06b6b40570ff49380438ea442acaa532eb08ccc09542c780fe3e971c09badbcf6a03c10a702d7a598ac568f6b04f37313d52a99008af4f8c58c3bf7f054e6baac91a0bc99014264980d68084615ba8b8ae5ce8fc6af8b96921cb2c9c63e8f47ba394ca0f1f21f3eb1975c4111d6ec35789005369dba1e22fbf25cc2d2ea751c2a06448ba0cb44e5bbcda580b362d9c5b9ba22b77913c5ca1c1b7023199a7d8ef99b2b4211a07d134950cca1021a4405c78a477628ea9dd93527b8697e35fcf04903fc54dca1a085bd16e0e6d45acdecac43f7694a21853a7d7b4a8847120464e7de02943b6fc3a0a6ab99f6df08c65a7b4b8b985133c152b01343bc056daa920db8d0c9bdc24d9c80",                    "0xf90111a00fc037475019567a9494d8c518c4e715fb94815b621c4985d0be2ba621079d5e80a03459d967524da36104c0b5c4f0377d9d34e77289a3885b051f4a826e9997eefda0c6dc63e07108ee9764dc00943d6f8bcafb090b7946e77666095a6639fa605564a09c3faad60b86ec6e1261834f868922df065d5530ec3de04f89ae867c030ed6a680a0b001877ef896a03a8ac57bc60f4d40649c106e90db992d608b6f73505a84800f8080a0cff6057866aa9cb5d33b008d2225be8d94badf6dce3f1a3a568f29031f687b8880808080a06a2958438119f0c236baade464df3cb4f39df778a95471cc50dd9ad920c8ba60a099f188088c9338fe70125c05417c6a6109234b4f75d89f9b652955d6cbd6f41780",                    "0xf851808080808080808080808080a0bf160cb5d412ac51aaab38102ed4a3e642e84bbb6ea7ef16a85fee6ca3ddaea780a0acbfbae5393615847e1b039a42d544adde87c52b9d98523640bacf4a0935571a8080",                    "0xf8419f2094275d2782da6748b94922bbcd1c251ada014f58c11c9e6fb131ceafa97ca09fb93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460"                ],                "value": "0xb93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460"            }        ]    }}

可以看到查询 proof 返回的 value 和查询存储返回的 value 有一定区别。

"value": "0xb93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460"

在其给出的 Merkle Proof 中,叶子节点上的数据如下:

09fb93cdc2c48e0574cd27a2fcb580843fc6c3441bcbe467532a9c52aa6055460

得出结论:Ethereum 生成 proof 时对 value 的处理机制并不简单。下面我们将对 proof 对 value 的处理机制进行详细描述。

原因解析

我们先查看通过 RPC 获取 proof 时,Ethereum 给出 proof 的方式。

在给出 proof 的 value 时,将存储数据转为 big.int,在后续 json 后,最左端0会被去除掉。

Merkle Tree 的叶子节点上的 value 为什么左端0也被去除掉?再看生成 block 的 root hash 时 Ethereum 的计算方式。

下面是计算 Ethereum Merkle Tree 的入口:

在最后处理 value 时:

在计算 Merkle Tree 时,对叶子节点上的 value,把最左端的0去掉。

结 论

在验证 value 的 Merkle Proof 时,需要先去掉 Value 最左端的0,或者将 Proof 中给出的 value 的最左端补齐0到 value 的长度。这样才能通过 Merkle Proof 的验证。

本文分享自微信公众号 - 本体研究院(ontologyresearch),作者:egaotan

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-03-13

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Ontology Layer 2链下扩容技术上线测试网,率先构建公链平台完整体系

    试想这样一个场景,一个区块链平台业务迅猛发展,用户数量呈现井喷式增长,很快从百万级达到了千万级,短时间内带来了相关成本的急剧上升。此时此刻,什么样的方式能够继续...

    本体Ontology
  • 本体技术视点 | 智能合约安全与漏洞分析(二)

    智能合约安全问题一直是区块链技术体系中探讨得比较多的话题之一。无论是以以太坊 EVM 虚拟机为代表的智能合约体系,还是以 EOS WASM 虚拟机为代表的智能合...

    本体Ontology
  • 本体技术视点 | 手把手教你Wasm合约开发

    Ontology Wasm 自从上线测试网以来,得到了社区开发人员的极大关注。因为这项技术使得业务逻辑复杂的 dApp 合约上链成本降低,极大丰富 dApp 生...

    本体Ontology
  • 排序算法-快速排序

    排序算法-快速排序 <?php /** * 快速排序. * * @param array $value 待排序数组 * @param array $...

    琯琯
  • 关于 python 中使用 lambda 表达式的问题

    --------------------------------  def doLambda(val):   print "value 2:", val ...

    田春峰-JCJC错别字检测
  • AppScan安全漏洞说明及解决方案

    解决方案:向所有会话cookie 添加HttpOnly属性 ,可以在过滤器中统一添加。

    java乐园
  • 排序算法-冒泡排序

    排序算法-冒泡排序 <?php /** * 冒泡排序 * * @param array $value 待排序数组 * * @return array...

    琯琯
  • 什么,你算出的P-value看上去像齐天大圣变的庙?

    前几天,Nature上一篇comment再度引发关于p-value如何使用和解释的文章:Scientists rise up against statistic...

    生信宝典
  • lodash 是如何做类型检测的

    JS 的基本数据类型有 Number,String,Boolean,Symbol,Null,Undefined,六种数据类型。一种引用类型 object。

    Javanx
  • 大众点评App的短视频耗电量优化实战

    前言 美团点评测试团队负责App的质量保证工作,日常除了App的功能测试以外,还会重点关注App的性能测试。现在大家对手机越来越依赖,而上面各App的耗电量,直...

    美团技术团队

扫码关注云+社区

领取腾讯云代金券