1.连接到私有链的RPC节点

投稿 2026-03-07 16:21 点击数: 1

Python与以太坊私有链交互:实战指南**


以太坊作为领先的区块链平台,不仅支持公有链,也允许用户搭建私有链进行测试、开发和研究,Python凭借其简洁的语法和强大的库支持,成为与以太坊(包括私有链)交互的热门选择,本文将详细介绍如何使用Python与以太坊私有链进行交互,涵盖环境搭建、连接节点、账户管理、智能合约部署与调用等核心环节。

准备工作:搭建以太坊私有链与Python环境

在开始Python交互之前,我们需要一个运行中的以太坊私有链节点,以及必要的Python库。

搭建以太坊私有链

搭建私有链有多种方式,这里介绍一种简单常用的方法,使用geth(Go-Ethereum)客户端:

  • 安装Geth:根据你的操作系统,从geth官网下载并安装对应版本的geth。
  • 初始化创世块:创建一个创世块配置文件,例如genesis.json,这是一个示例配置:
    {
      "config": {
        "chainId": 15, // 私有链唯一标识,避免与公有链冲突
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0
      },
      "alloc": {}, // 预先分配账户,可选
      "coinbase": "0x0000000000000000000000000000000000000000",
      "difficulty": "0x4000", // 初始难度,较低值便于挖矿
      "extraData": "",
      "gasLimit": "0xffffffff",
      "nonce": "0x0000000000000042",
      "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "timestamp": "0x00"
    }
  • 初始化节点:在命令行中执行以下命令(假设genesis.json在当前目录):
    geth --datadir "./data" init genesis.json
  • 启动私有链节点
    geth --datadir "./data" --networkid 15 --nodiscover --rpc --rpcaddr "0.0.0.0" --rpcport "8545" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"
    • --datadir: 指定数据存储目录。
    • --networkid: 设置网络ID,与genesis.json中的chainId保持一致。
    • --nodiscover: 不自动发现其他节点,适合私有链。
    • --rpc: 启动JSON-RPC服务。
    • --rpcaddr: RPC服务监听地址,0.0.0表示允许任何IP连接。
    • --rpcport: RPC服务端口,默认为8545。
    • --rpccorsdomain: CORS跨域设置,允许所有域名,开发时方便,生产环境需谨慎。
    • --rpcapi: 暴露的RPC API接口。

你的以太坊私有链节点已经在后台运行,并监听8545端口的RPC请求。

安装Python以太坊交互库

我们使用最流行的web3.py库,它提供了与以太坊节点进行交互的完整API。

pip install web3

Python连接以太坊私有链节点

使用web3.py连接到我们刚刚启动的私有链节点非常简单。

from web3 import Web3
# 使用HTTPProvider
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
# 或者使用IPCProvider(更高效,如果geth在同一台机器上运行)
# w3 = Web3(Web3.IPCProvider('./data/geth.ipc'))
# 2. 检查连接是否成功
if w3.is_connected():
    print(f"已成功连接到以太坊节点!Chain ID: {w3.eth.chain_id}")
    print(f"当前最新区块号: {w3.eth.block_number}")
else:
    print("连接失败!请检查节点是否运行及RPC配置。")

账户管理(创建、解锁、转账)

私有链中的账户需要以太币(ETH)才能进行交易,我们可以通过挖矿或使用personal模块来管理账户。

创建账户

# 创建新账户
# 注意:这会在geth节点上创建,需要节点支持personal API
# 如果geth启动时没有开启personal API,需要重新启动并添加personal
try:
    new_account = w3.geth.personal.new_account('your_password_here')
    print(f"新账户创建成功: {new_account}")
except Exception as e:
    print(f"创建账户失败: {e}")

解锁账户

发送交易前需要解锁账户。

account_address = '0xYourAccountAddressHere' # 替换为你的账户地址
password = 'your_password_here'
try:
    w3.geth.personal.unlock_account(account_address, password)
    print(f"账户 {account_address} 已解锁")
except Exception as e:
    print(f"解锁账户失败: {e}")

获取账户余额

balance = w3.eth.get_balance(account_address)
print(f"账户 {account_address} 的余额: {w3.from_wei(balance, 'ether')} ETH")

发送交易(转账)

假设我们已经有一个有余额的账户(通常是创世账户或挖矿账户),向另一个账户转账。

from eth_account import Account
# 发送方账户(需要解锁)
sender_address = '0xSenderAccountAddressHere' # 替换为发送方地址
sender_private_key = '0xSenderPrivateKeyHere' # 替换为发送方私钥(仅用于本地测试,生产环境勿暴露!)
receiver_address = '0xReceiverAccountAddressHere' # 替换为接收方地址
# 解锁发送方账户(如果前面没解锁)
# w3.geth.personal.unlock_account(sender_address, password)
# 构建交易
nonce = w3.eth.get_transaction_count(sender_address) # 获取nonce值
value = w3.to_wei(1, 'ether') # 转账1 ETH
gas_price = w3.eth.gas_price # 获取当前gas价格
gas_limit = 21000 # 转账通常的gas limit
# 创建交易字典
transaction = {
    'nonce': nonce,
    'to': receiver_address,
    'value': value,
    'gas': gas_limit,
    'gasPrice': gas_price,
}
# 签名交易(使用Account.sign_transaction更安全)
# signed_txn = Account.sign_transaction(transaction, sender_private_key)
# tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# 或者使用personal_sendTransaction(节点会帮忙签名,但需要解锁账户)
try:
    tx_hash = w3.geth.personal.send_transaction(transaction, sender_private_key)
    print(f"交易发送成功,交易哈希: {tx_hash.hex()}")
    # 等待交易被打包
    receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
    print(f"交易回执: {receipt}")
except Exception as e:
    print(f"发送交易失败: {e}")

智能合约的部署与交互

智能合约是以太坊的核心功能之一。web3.py也提供了与智能合约交互的能力。

编译智能合约

你需要先有一个Solidity编写的智能合约文件(例如SimpleStorage.sol),并使用编译器(如solc)编译它,获取ABI(Application Binary Interface)和字节码(Bytecode)。

// SimpleStorage.sol
pragma solidity ^0.8.0;
contract SimpleStorage {
    uint256 private storedData;
    function set(uint256 x) public {
        storedData = x;
    }
    function get() public view returns (uint256) {
        return storedData;
    }
}

你可以使用solc命令行工具或py-solc-x Python库来编译:

pip install py-solc-x

然后在Python中编译:

from solcx import compile_standard
with open('SimpleStorage.sol', 'r') as file:
    sol_source = file.read()
compiled_sol = compile_standard({
    "language": "Solidity",
    "sources": {