web3.py - The field extraData is 97 byte...

2021-06-21 10:30:39 浏览数 (1)

当使用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命令启动的开发私链时,代码会提示如下错误信息:

代码语言:javascript复制
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对象即可,例如:

代码语言:javascript复制
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中间件

原文链接:The field extraData is 97 bytes, but should be 32 — 汇智网

0 人点赞