12

The field extraData is 97 bytes, but should be 32【Web3.py错误】

 3 years ago
source link: http://blog.hubwiz.com/2021/06/18/web3py-poa-issue/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The field extraData is 97 bytes, but should be 32【Web3.py错误】

发表于 2021-06-18

| 分类于 以太坊

当使用web3.py接入采用POA共识的以太坊节点时,可能会 出现错误The field extraData is 97 bytes, but should be 32...,本文将 介绍如何解决这一问题。

用自己熟悉的语言学习 以太坊开发Java | Php | Python | .Net / C# | Golang | Node.JS | Flutter / Dart

1、Web3.py ExtraData故障现象

当使用Web3.py接入Rrinkeby测试链或接入使用geth --dev命令 启动的开发私链时,代码会提示如下错误信息:

web3.exceptions.ExtraDataLengthError: The field extraData is 97 bytes, 
but should be 32. It is quite likely that you are connected to a POA chain.
Refer to http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority
for more details.

The full extraData is: HexBytes('0xd88301010084676...21465292f')

这是因为POA共识算法在extraData字段添加了一些额外的数据从而导致该字段 超过以太坊的黄皮书约定的32字节长度,而Web3.py默认情况下是按照以太坊 黄皮书的约定来检查extraData字段的长度,这造成了不一致并抛出异常。

2、Web3.py ExtraData故障的解决方法

Web3.py提供了中间件geth_poa_middleware来解决这一兼容性问题,只要 引入该中间件并将其注入web3对象即可,例如:

from web3 import Web3
from web3.middleware import geth_poa_middleware

w3 = Web3('http://localhost:8545')
w3.middleware_onion.inject(geth_poa_middleware, layer=0) # 注入poa中间件

汇智网原创,转载请标明出处


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK