赞
踩
基础实践教程与问题汇总
仅用于工业物联网的区块链安全解决方案研究!
这部分不再赘述。软件官网:https://geth.ethereum.org/downloads/
下载exe直接安装即可(允许改路径)
写在前头,虽然 geth -help 会出现一堆的参数,但是这个真的要耐心看看
geth --datadir node1 init genesis.json
geth --datadir node1 --networkid 1123 console 2>output.txt --port 30304 --http True --rpccorsdomain "*" --ws --allow-insecure-unlock --nodiscover
--http ,不知道是不是版本的问题,这里没有 --rpc 了!geth --datadir node2 --nodiscover --ipcdisable --networkid 1123 console 2>node2/output2.txt --port 30305 --authrpc.addr 127.0.1.1 --authrpc.port 8552
以太坊客户端Geth命令用法-参数详解:https://learnblockchain.cn/2017/11/29/geth_cmd_options/
这里基本和上述启动私链的命令一样,但是需要注意以下几个问题
这里只能做个说明。注意看报错信息。
尤其留意报错提示的 addr 或者port被占用问题。
这个时候就需要指定相应的类似:
--authrpc.addr 和 --authrpc.port 的参数了!
需要先运行geth 否则会连接失败
walet = Web3(HTTPProvider("http://localhost:8545")) # 有疑问请看web3.py官网
if walet.eth.getBlock(0) is None:
print("连接失败")
elif walet.isConnected():
print("连接成功")
print('账户数量:', walet.eth.accounts, 'block数量:', walet.eth.block_number)
account = walet.eth.accounts[0]
balance = walet.eth.getBalance(account)
token = walet.fromWei(balance, 'ether')
print('账户余额:', balance, 'ether=', token)
print('当前chain id:', walet.eth.chainId)
block = walet.eth.get_block('latest') ## 也可以获取指定序号的区块100
print('最新区块:', block)
signed_txn = walet.eth.account.signTransaction(dict(
nonce=walet.eth.getTransactionCount(walet.eth.coinbase),
gasPrice=walet.toWei(50, "gwei"), ##walet.eth.gasPrice, ##
gas=520000,
to=walet.eth.accounts[0],
value= walet.toWei(1, "ether"), # 23, ##
data=str(dat), ## 'biao9565', #
chainId=123
), accountA['private']) #
tx_hash = walet.eth.sendRawTransaction(signed_txn.rawTransaction)
print('发送成功:', tx_hash.hex())
注意这里的 accountA[‘private’],后面的私钥错误就是指这里。
注意翻译报错信息的英文, 以下的凭借调试过程的记忆记录
ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'})
miner.start()。人就在这第二条折腾了好久。一直在 1和3 之间尝试
如何获取私钥,请看本帖最后
"intrinsic gas too low"
error: authentication needed: password or unlock
sendRawTransaction 特有的。因为需要离线签名,walet.eth.account.signTransaction函数必须输入 私钥 。"already known"
replacement transaction underpriced
解释起来就是:
上一个交易发起时(nonce=a),还没有被打包,也就是没有挖矿成功。这个时候还是:nonce=a 。
此时,又发起了一个交易,根据打码规则,还是 nonce=a,因此就会出现上述错误。
0x7146e96a50984e3b69b10b979d9a7088c6a96c20 的公钥地址。UTC--2022-07-31T03……--7146e96a50……(文件名省略了部分,注意是UTC开头)这里可以参考这个网址:http://cw.hubwiz.com/card/c/web3.js-1.0/1/5/9/
具体代码如下:
with open('I:/Program Files/Geth/node1/keystore/UTC--2022-07-31T03-12-39.255879700Z--7146e96a50984e3b69b10b979d9a7088c6a96c20') as keyfile:
encrypted_key = keyfile.read()
private_key = w3.eth.account.decrypt(encrypted_key, 'biao')
print('private_key:', private_key.hex())
.hex() 获取类似 '0x8a42a667344ea4b56aa57……'的十六进制数据(字符串)walet.eth.account.signTransaction 的私钥输入。这里应该没有什么错误,主要会出现下面这个:
ValueError: MAC check failed
block = walet.eth.get_block('latest') ## 100
print('最新区块:', block)
tx = walet.eth.getTransaction(tx_hash) # 获取交易信息
print('获取交易信息:', tx)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。