以太坊—JSON RPC API

Geth 使用JSON数据格式, JSON-RPC协议作为客户端和网络进行交互的方法。 Geth 1.4 的 pub/sub 是实验性质的, 同样, Partity 1.6 也是。

JavaScript API

使用web3.js和etherenum节点交互时, 遵循一致的RPC协议.

 

JSON-RPC Endpoint

默认的JSON-RPC终端:

Client URL

C++ http://localhost:8545

GO http://localhost:8545

Py http://localhost:4000

Parity http://localhost:8545

 

Go

可以通过 --rpc 标志开启HTTP JSON-RPC

geth --rpc

可以通过下面的方式修改默认的端口(8545)和地址(localhost):

geth --rpc --rpcaddr <ip>  --rpcport <portnumber>

如果通过浏览器访问RPC, 需要注意跨域问题, 可以通过一下方式:

geth --rpc --rpccorsdomain "http://localhost:3000"

JSON-RPC也可以通过 geth console 开启, 如下:

admin.startRPC(addr, port)

 

C++

首先运行aleth启动节点:

build/aleth/aleth

然后开启JSON-RPC代理(默认 ~/.ethereum/geth.ipc 和 http://127.0.0.1:8545

scripts/jsonrpcproxy.py

如果你使用非默认的IPC路径或者JSON-RPC, 可以指定:

scripts/jsonrpcproxy.py  <path to your node's geth.ipc>  <URL for this proxy server>

 

Python

在Python中, JSONRPC服务默认开启的端口和地址是127.0.0.1:4000

可以通过如下方式配置:

pyethapp -c jsonrpc.listen_port=4002 -c jsonrpc.listen_host=127.0.0.2 run

 

JSON-RPC 支持情况

cpp-ethereum :JSON-RPC 1.0 JSON-RPC 2.0 Batch requests HTTP IPC

go-ethereum :JSON-RPC 2.0 Batch requests HTTP IPC WS

py-ethereum :JSON-RPC 2.0 Batch requests HTTP

patity :JSON-RPC 2.0 Batch requests HTTP IPC WS

 

十六进制值编码 HEX value encoding

目前, 有2中主要的数据烈性用于JSON, unformatted byte arrays 和 quantities, 他们都是通过hex编码传递的。

 

默认block 参数

下面的方法有一个额外的默认的block参数:

  • eth_getBalance
  • eth_getCode
  • eth_getTransactionCount
  • eth_getStorageAt
  • eth_call

当请求访问erhereum的状态时, 最后默认的block参数决定了block的高度,该选项可以是一下的取值, defaultBlock:

  • HEX String 一个integer block 值
  • String "earliest" 用于earliest/genesis block
  • String "latest" 用于最近挖出的block
  • String "pending" 用于pending 的 state/transactions

 

 

Curl 例子解释

curl 命令参数中 --data 默认指定数据类型为 application/x-www-form-urlencoded, 如果是其他的格式, 需要手动指定, 例如: -H "application/json" , 最后面必须跟 URL/IP 和 端口, 例如 : curl e.x. 127.0.0.1:8545

 

JSON-RPC 方法

  • web3_clientVersion
  • web3_sha3

 

  • net_version
  • net_peeCount
  • net_listening

 

  • eth_protocolVersion
  • eth_syncing
  • eth_coinbase
  • eth_mining
  • eth_hashrate
  • eth_gasPrice
  • eth_accounts
  • eth_blockNumber
  • eth_getBalance
  • eth_getStorageAt
  • eth_getTransactionCount
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getUncleCountByBlockHash
  • eth_getUncleCountByBlockNumber
  • eth_getCode
  • eth_sign
  • eth_sendTransaction
  • eth_sendRawTransaction
  • eth_call
  • eth_estimateGas
  • eth_getBlockByHash
  • eth_getBlockByNumber
  • eth_getTransactionByHash
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionReceipt
  • eth_getUncleByBlockHashAndIndex
  • eth_getUnclueByBlockNumberAndIndex
  • eth_getCompilers
  • eth_compileSolidity
  • eth_compileSerpent
  • eth_newFilter
  • eth_newBlockFilter
  • eth_newPendingTransactionFilter
  • eth_uninstallFilter
  • eth_getFilterChanges
  • eth_getFilterLogs
  • eth_getLogs
  • eth_getWork
  • eth_submitWork
  • eth_submitHashrate

 

  • db_putString
  • db_getString
  • db_putHex
  • db_getHex

 

  • shh_post
  • shh_version
  • shh_newIdentity
  • shh_hasIdentity
  • shh_newGroup
  • shh_addToGroup
  • shh_newFilter
  • shh_uninstallFilter
  • shh_getFilterChanges
  • shh_getMessages

转载于:https://my.oschina.net/kingwjb/blog/1861490

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值