The field extraData is 97 bytes, but should be 32. It is quite likely that you are connected to a POA chain
Asked Answered
L

1

15

I deployed a private network via geth and posted a smart contract there. Accounts are set in the genesis block

enter image description here

I'm creating a python function in which I want to call a smart contract function

import web3
import json
class Estate():
    with open("abi.txt", 'r') as file:
        abi = json.load(file)
    contract_address = "0x4FD52ce1140d67b8c721AB7967eeca44FE56c883"
    contract_address = web3.Web3.toChecksumAddress(contract_address)
    w3 = web3.Web3(web3.HTTPProvider('http://127.0.0.1:8545'))
    con = w3.eth.contract(address=contract_address, abi=abi)

    def create_estate(self, address, info, square):
        address = web3.Web3.toChecksumAddress(address)
        print(self.con.functions.create_estate(address, info, square).transact())
        #self.w3.eth.waitForTransactionReceipt(tx)

c = Estate()
c.create_estate("0xE64021f5387Dc5FA859e0641437a45542D511e7E","street", 200)

I am getting the following error:

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('0xda83010a0d846765746888676f312e31372e328777696e646f7773000000000053ea481c4aca3824d51d3c8103abe60179d3f1c4ec68a0c87d5533e8e0580e14433b6cf8a285b519add550d59e70e60619721861b2d855cb7a175978610bb65500')

Logician answered 22/1, 2022 at 11:55 Comment(0)
R
30

The error comes from a discrepancy in the block format between your private geth network (or a public test network like Rinkeby) and the main network.

You can work around this by adding a middleware to your Web3 object:

from web3.middleware import geth_poa_middleware

Then inject the middleware:

w3 = web3.Web3(web3.HTTPProvider('http://127.0.0.1:8545'))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)

I had the same error as you and fixed it based on the solution here:

https://github.com/blockchain-etl/ethereum-etl/issues/178

The web3 documentation also mentions this:

https://web3py.readthedocs.io/en/stable/middleware.html#why-is-geth-poa-middleware-necessary

Randyranee answered 27/1, 2022 at 23:4 Comment(3)
What would you need to do if your using brownie?Regiment
@RobertJohnstone I'm not sure how that particular library would work. Maybe they have some docs around the topic of test networks? Otherwise you may have more success posting a new question.Randyranee
@RobertJohnstone In Brownie the only difference is, instead of w3 = web3.Web3(web3.HTTPProvider('http://127.0.0.1:8545')) do from brownie.network import web3 as w3.Titi

© 2022 - 2024 — McMap. All rights reserved.