博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过命令行在Python中测试以太坊RPC客户端
阅读量:6867 次
发布时间:2019-06-26

本文共 6816 字,大约阅读时间需要 22 分钟。

hot3.png

在这个笔记中,我将使用Python命令行测试以太坊的RPC客户端,准备好狂敲键盘吧。过程中有关JSON RPC的更多信息,可以参阅。

输入:

from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"
import syssys.version

输出:

'3.6.0 |Anaconda 4.3.0 (64-bit)| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'

准备环境

为以太坊安装,输入:

jitsejan@jjvps:~$ pip install ethereum-rpc-client

启动区块链以确保启用RPC。

输入:

jitsejan@jjvps:~$ geth --networkid 23 --nodiscover --maxpeers 0  --port 30333 --rpc

验证geth是否正在运行并且列出了该帐户。

输入:

!geth account list

输出:

Account #0: {8cf9deda0712f2291fb16739f8759e4a0a575854} keystore:///home/jitsejan/.ethereum/keystore/UTC--2017-05-01T14-58-43.532247863Z--8cf9deda0712f2291fb16739f8759e4a0a575854

链接到RPC客户端

输入:

from eth_rpc_client import Clientclient = Client(host="127.0.0.1", port="8545")

检查客户端

输入:

import pdirpdir(client)

输出:

abstract class:    __subclasshook__attribute access:    __delattr__, __dir__, __getattribute__, __setattr__class customization:    __init_subclass__object customization:    __format__, __hash__, __init__, __new__, __repr__, __sizeof__, __str__other:    _coinbase_cache, _coinbase_cache_til, _nonce, async_timeout, host, is_async, port, request_queue, request_thread, results, sessionpickle:    __reduce__, __reduce_ex__rich comparison:    __eq__, __ge__, __gt__, __le__, __lt__, __ne__special attribute:    __class__, __dict__, __doc__, __module__, __weakref__descriptor:    default_from_address: @property with getter, Cache the coinbase address so that we don't make two requests for everyfunction:    _make_request:     call: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call    construct_json_request:     get_accounts: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_accounts    get_balance: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getbalance    get_block_by_hash: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbyhash    get_block_by_number: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber    get_block_number: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_blocknumber
get_code: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getcode get_coinbase: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_coinbase get_filter_changes: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges get_filter_logs: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs get_gas_price: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gasprice get_logs: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs get_max_gas: get_nonce: get_transaction_by_hash: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionbyhash get_transaction_receipt: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionreceipt make_request: new_block_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newblockfilter new_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter new_pending_transaction_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newpendingtransactionfilter process_requests: Loop that runs in a thread to process requests synchronously. send_transaction: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction uninstall_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_uninstallfilter wait_for_block: wait_for_transaction:

获取区块链的coinbase

输入:

address = client.get_coinbase()address

输出:

0x8cf9deda0712f2291fb16739f8759e4a0a575854

查询主地址的余额

输入:

client.get_balance(address)

输出:

135000419895999999940

设置其他两台机器的地址

输入:

address_vps_one = "0xc257beaea430afb3a09640ce7f020c906331f805"address_vps_two = "0xe86ee31b7d32b743907fa7438c422a1803717deb"client.get_balance(address_vps_one)client.get_balance(address_vps_two)

输出:

699916006000000000083000420044000000060

交易

让我们从主机发送12个以太网到VPS,1 GWEI=0.000000001 Ether。

输入:

amount = 12 # Ethersending_address = addressreceiving_address = address_vps_one

获取密码以解锁发送帐户

输入:

from getpass import getpass  pw = getpass(prompt='Enter the password for the sender: ')

通过命令行解锁帐户

输入:

command = r'geth --exec "personal.unlockAccount(\"%s\", \"%s\");" attach ' % (sending_address, pw)result = !$commandif result[0] != 'true':    print('Fail: %s' % result[0])else:    print('Account is unlocked!')

输出:

Account is unlocked!

发送交易

tx_hash = client.send_transaction(to=receiving_address, _from=sending_address, value=amount * 10**9)

检查交易明细

client.get_transaction_by_hash(tx_hash)

输出:

{'blockHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 'blockNumber': None, 'from': '0x8cf9deda0712f2291fb16739f8759e4a0a575854', 'gas': '0x15f90', 'gasPrice': '0x4a817c800', 'hash': '0x3d1a193ccfccc4e9ab2a411044069deeec2feef31a594bbf73726b463e8e90b4', 'input': '0x', 'nonce': '0xb', 'r': '0xe8698846a461938e800698fcc34570e0c4e9a3425f0bc441bf3e0716dab7b3e0', 's': '0x4fcd9bda8a1e98a7b0e8d953dec0cc733238c383d97393aa15c43963551f8daf', 'to': '0xc257beaea430afb3a09640ce7f020c906331f805', 'transactionIndex': '0x0', 'v': '0x42', 'value': '0x2cb417800'}

执行一个挖掘步骤

执行矿工以验证交易。

输入:

prev_balance_sen = client.get_balance(sending_address)prev_balance_rec = client.get_balance(receiving_address)
result = !geth --exec "miner.start();admin.sleepBlocks(1);miner.stop();" attachif result[0] != 'true':    print('Fail: %s' % result[0])else:    print("Mining finished!")

输出挖矿完成:

Mining finished!

检查是否已收到以太网

输入:

print("Received %d"% (client.get_balance(receiving_address)-prev_balance_rec))

输出收到金额:

Received 12000000000

检查Ether是否已发送

首先检查余额的差异,输入:

print("Difference of the sender %d"% (client.get_balance(sending_address)-prev_balance_sen))

输出:

Difference of the sender 4999999988000000000

对于采矿来说,矿工将获得采矿奖金。

输入:

mining_bonus = 5000000000000000000

要获得以太网发送的数量,我们需要减去采矿奖金。

输入:

print("Amount difference: %d" % int(client.get_balance(sending_address)-prev_balance_sen - mining_bonus))

输出金额差异:

Amount difference: -12000000000

======================================================================

分享一些以太坊、EOS、比特币等区块链相关的交互式在线编程实战教程:

  • ,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。
  • ,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
  • ,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和交易等内容。
  • ,主要介绍智能合约与dapp应用开发,适合入门。
  • ,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • ,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和交易等。
  • ,本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。
  • ,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Java代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Java工程师不可多得的比特币开发学习课程。
  • ,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Php代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Php工程师不可多得的比特币开发学习课程。
  • ,本课程适合希望使用tendermint进行区块链开发的工程师,课程内容即包括tendermint应用开发模型中的核心概念,例如ABCI接口、默克尔树、多版本状态库等,也包括代币发行等丰富的实操代码,是go语言工程师快速入门区块链开发的最佳选择。

汇智网原创翻译,转载请标明出处。这里是原文

转载于:https://my.oschina.net/u/2472105/blog/2873926

你可能感兴趣的文章
3.25Day06元组、字典、集合常用及内置方法
查看>>
关于NB-IoT的十大问题和答案【转】
查看>>
Foundations of Qt Development 学习笔记 Part1 Tips1-50
查看>>
hive的实现机制
查看>>
PyQt5:布局
查看>>
shllter自动和手动实例
查看>>
团队冲刺第一天
查看>>
[转]数据结构:图的存储结构之邻接多重表
查看>>
Dreamweaver——如何使网页中的第一个DIV水平居中
查看>>
POJ_2001_Shortest Prefixes
查看>>
Webpack 的 HtmlWebpackPlugin 如何控制某个 chunks 的 inject 位置?
查看>>
Silverlight C# 游戏开发:未写代码先设计
查看>>
return false
查看>>
BZOJ3769:BST again(记忆化搜索DP)
查看>>
第二章:演化架构师
查看>>
20165315 第八周考试课下补做
查看>>
学习CAS实现SSO单点登录
查看>>
同步异步的知识补充
查看>>
[Leetcode]100. Same Tree -David_Lin
查看>>
阿里云推送服务
查看>>