Web3.exceptions.ExtraDataLengthError

Web3.exceptions.ExtraDataLengthError

错误:

​ 当我在使用geth搭建测试链尝试部署智能合约,用web3库运行如下命令时

tx_hash = contract.constructor().transact() 

​ 终端报出如下错误信息:

web3.exceptions.ExtraDataLengthError: The field extraData is 117 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.

分析

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

解决

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

from web3 import Web3
from web3.middleware import geth_poa_middleware

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

参考Web3.py错误 - The field extraData is 97 bytes, but should be 32_新缸中之脑的博客-CSDN博客

你可能感兴趣的:(以太坊,error,以太坊)