besu-docs icon indicating copy to clipboard operation
besu-docs copied to clipboard

Can't deploy smart contract via web3.py

Open CaixiangFan opened this issue 3 years ago • 0 comments

Description

I want to deploy a smart contract Simple Storage to a local Besu through the web3.py. I followed the tutorial by using the send_raw_transaction function (similar to sendSignedTransaction in web3.js). I can get a transaction receipt but the status is 0, which indicates the deployment is not successful.

Acceptance Criteria

  • receipt's status should be 1

Steps to Reproduce (Bug)

  1. Run a local Besu network (IBFT2.0) with 4 nodes, exposing httpRPC port 8545.
  2. Connect to a RPC node; prepare deployer address and private key.
  3. Compile the Simple Storage smart contract.
  4. Compose all raw transaction options to a dictionary.
  5. Sign the composed transaction using the private key.
  6. Send the signed transaction using send_raw_transaction function.

Expected behavior: [What you expect to happen] Smart contract deployed successfully.

Actual behavior: [What actually happens] Smart contract is not deployed successfully.

Frequency: [What percentage of the time does it occur?] Always

Versions (Add all that apply)

  • Software version: [besu --version] 21.10
  • Java version: [java -version] JDK 11
  • OS Name & Version: [cat /etc/*release] Ubuntu 20.04.3 LTS
  • Kernel Version: [uname -a] 5.4.0-88-generic
  • Docker Version: [docker version] 20.10.8

Any suggestions would be much appreciated! BTW, if anyone knows how to pair Besu node with Ethsigner and can share it with me, that would be great. Now, Besu is not friendly to web3.py given that it doesn't support sendTransaction.

Thank you.

Python Code

import json
from web3 import Web3
from web3.middleware import geth_poa_middleware
w3 = Web3(Web3.HTTPProvider(httpUrls[nodes[0]]))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
genesisFile = 'genesis.json'
with open(genesisFile, 'r') as f:
    data = json.load(f)['alloc']
addrKeys = [addr for addr in data.keys()]
addrs = [Web3.toChecksumAddress(addr) for addr in addrKeys]
print('Connected to Besu: ', w3.isConnected(), 'Node: ', httpUrls[nodes[0]], w3.clientVersion)

with open('../build/contracts/SimpleStorage.json') as file:
    compiled = json.load(file)
contractAbi = compiled['abi']
contractBin = compiled['bytecode']
contractInit = "000000000000000000000000000000000000000000000000000000000000002F"
fromAddress = addrs[0]
rawtxOptions = {
    'nonce': w3.eth.get_transaction_count(fromAddress),
    'from': fromAddress,
    'to': None,
    'value': '0x00',
    'data': contractBin+contractInit,
    'gas': 90000,
    'gasPrice': 18000000000,
    'chainId': 1337
}
priv_key = data[addrKeys[0]]['privateKey']
signed_txn = w3.eth.account.sign_transaction(rawtxOptions, priv_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
receipt

Output

Connected to Besu:  True Node:  [http://127.0.0.1:8545] besu/besu-1/v21.10.0-RC1/linux-x86_64/adoptopenjdk-java-11

AttributeDict({'blockHash': HexBytes('0xa2e864afc225828d3b24e86f80cc4d0b46aa460cfd0c7589205686e087f48ad6'),
 'blockNumber': 342957,
 'contractAddress': '0x84a00f057F17Dcd0D4f5cF933c8403a6080b622f',
 'cumulativeGasUsed': 90000,
 'from': '0x0BCf26e7fbf1061403ab4B4308DBb9299A1da707',
 'gasUsed': 90000,
 'effectiveGasPrice': 18000000000,
 'logs': [],
 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'status': 0,
 'to': None,
 'transactionHash': HexBytes('0x8c9d357a53e208aea7bb834ec51232407e3be0d1e50ae44486f77187a197c42e'),
 'transactionIndex': 0})

CaixiangFan avatar Mar 31 '22 18:03 CaixiangFan