more rpc APIs. (#863)

* added readme

* more RPC APIs

* update for comments; fix travis issues

* update auto-generated host_mock.go
pull/868/head
Richard Liu 6 years ago committed by GitHub
parent d4bc279d88
commit fe217ad2b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      accounts/abi/bind/backends/simulated.go
  2. 5
      core/api_backend.go
  3. 100
      internal/hmyapi/README.md
  4. 11
      internal/hmyapi/backend.go
  5. 18
      internal/hmyapi/harmony.go
  6. 19
      internal/hmyapi/net.go
  7. 0
      internal/hmyapi/private_account.go
  8. 28
      internal/hmyapi/public_account.go
  9. 52
      internal/hmyapi/transactionpool.go
  10. 1
      p2p/host.go
  11. 5
      p2p/host/hostv2/hostv2.go
  12. 14
      p2p/host/mock/host_mock.go

@ -240,7 +240,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
// Determine the lowest and highest possible gas limits to binary search in between
var (
lo uint64 = params.TxGas - 1
lo = params.TxGas - 1
hi uint64
cap uint64
)

@ -105,3 +105,8 @@ func (b *HmyAPIBackend) CurrentBlock() *types.Block {
func (b *HmyAPIBackend) AccountManager() *accounts.Manager {
return b.accountManager
}
// GetReceipts ...
func (b *HmyAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
return b.blockchain.GetReceiptsByHash(hash), nil
}

@ -0,0 +1,100 @@
# JSON RPC
## JSSDK
[FireStack-Lab/Harmony-sdk-core](https://github.com/FireStack-Lab/Harmony-sdk-core)
## JSON-RPC methods
### Network info related
* [ ] net_listening - check if network is connected
* [x] hmy_protocolVersion - check protocol version
* [ ] net_version - get network id
* [ ] net_peerCount - peer count
### BlockChain info related
* [ ] hmy_gasPrice - return min-gas-price
* [ ] hmy_estimateGas - calculating estimate gas using signed bytes
* [ ] hmy_blockNumber - get latest block number
* [x] hmy_getBlockByHash - get block by block hash
* [x] hmy_getBlockByNumber
* [ ] hmy_getUncleByBlockHashAndIndex - get uncle by block hash and index number
* [ ] hmy_getUncleByBlockNumberAndIndex - get uncle by block number and index number
* [ ] hmy_getUncleCountByBlockHash - get uncle count by block hash
* [ ] hmy_getUncleCountByBlockNumber - get uncle count by block number
* [ ] hmy_syncing - Returns an object with data about the sync status
* [ ] hmy_coinbase - return coinbase address
* [ ] hmy_mining - return if mining client is mining
* [ ] hmy_hashrate - return current hash rate for blockchain
### Account related
* [x] hmy_getBalance - get balance for account address
* [x] hmy_getTransactionCount - get nonce for account address
* [ ] hmy_accounts - return accounts that lives in node
### Transactions related
* [ ] hmy_getTransactionReceipt - get transaction receipt by given transaction hash
* [ ] hmy_sendRawTransaction - send transaction bytes(signed) to blockchain
* [ ] hmy_sendTransaction - send transaction object(with signature) to blockchain
* [x] hmy_getBlockTransactionCountByHash - get transaction count of block by block hash
* [x] hmy_getBlockTransactionCountByNumber - get transaction count of block by block number
* [x] hmy_getTransactionByHash - get transaction object of block by block hash
* [x] hmy_getTransactionByBlockHashAndIndex - get transaction object of block by block hash and index number
* [x] hmy_getTransactionByBlockNumberAndIndex - get transaction object of block by block number and index number
* [ ] hmy_sign - sign message using node specific sign method.
### Contract related
* [ ] hmy_call - call contract method
* [x] hmy_getCode - get deployed contract's byte code
* [x] hmy_getStorageAt - get storage position at a given address
* ~~[ ] hmy_getCompilers~~ - DEPRECATED
* ~~[ ] hmy_compileLLL~~ - DEPRECATED
* ~~[ ] hmy_compileSolidity~~ - DEPRECATED
* ~~[ ] hmy_compileSerpent~~ - DEPRECATED
### Subscribes
* [ ] hmy_pendingTransactions - pending transaction subscriber
* [ ] hmy_getLogs - log subscriber
* [ ] hmy_newFilter - creates a filter object, based on filter options
* [ ] hmy_newBlockFilter - creates a filter in the node, to notify when a new block arrives
* [ ] hmy_newPendingTransactionFilter - creates a filter in the node, to notify when new pending transactions arrive
* [ ] hmy_getFilterChanges - polling method for a filter
* [ ] hmy_getFilterLogs - returns an array of all logs matching filter with given id.
* [ ] hmy_uninstallFilter - uninstalls a filter with given id
### Others, not very important for current stage of work
* [ ] web3_clientVersion
* [ ] web3_sha3
* [ ] hmy_getWork
* [ ] hmy_submitWork
* [ ] hmy_submitHashrate
* [ ] hmy_getProof
* [ ] 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

@ -9,6 +9,12 @@ import (
func GetAPIs(b *core.HmyAPIBackend) []rpc.API {
nonceLock := new(AddrLocker)
return []rpc.API{
{
Namespace: "hmy",
Version: "1.0",
Service: NewPublicHarmonyAPI(b),
Public: true,
},
{
Namespace: "hmy",
Version: "1.0",
@ -19,6 +25,11 @@ func GetAPIs(b *core.HmyAPIBackend) []rpc.API {
Version: "1.0",
Service: NewPublicTransactionPoolAPI(b, nonceLock),
Public: true,
}, {
Namespace: "hmy",
Version: "1.0",
Service: NewPublicAccountAPI(b.AccountManager()),
Public: true,
},
}
}

@ -1,6 +1,9 @@
package hmyapi
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/harmony-one/harmony/api/proto"
"github.com/harmony-one/harmony/core"
@ -9,7 +12,12 @@ import (
// PublicHarmonyAPI provides an API to access Harmony related information.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicHarmonyAPI struct {
b *core.BlockChain
b *core.HmyAPIBackend
}
// NewPublicHarmonyAPI ...
func NewPublicHarmonyAPI(b *core.HmyAPIBackend) *PublicHarmonyAPI {
return &PublicHarmonyAPI{b}
}
// ProtocolVersion returns the current Harmony protocol version this node supports
@ -25,6 +33,12 @@ func (s *PublicHarmonyAPI) ProtocolVersion() hexutil.Uint {
// - pulledStates: number of state entries processed until now
// - knownStates: number of known state entries that still need to be pulled
func (s *PublicHarmonyAPI) Syncing() (interface{}, error) {
// TODO(ricl): port
// TODO(ricl): find our Downloader module for syncing blocks
return false, nil
}
// GasPrice returns a suggestion for a gas price.
func (s *PublicHarmonyAPI) GasPrice(ctx context.Context) (*hexutil.Big, error) {
// TODO(ricl): add SuggestPrice API
return (*hexutil.Big)(big.NewInt(1)), nil
}

@ -1,22 +1,29 @@
package hmyapi
import (
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p"
"github.com/harmony-one/harmony/p2p"
)
// PublicNetAPI offers network related RPC methods
type PublicNetAPI struct {
net *p2p.Server
networkVersion uint64
net p2p.Host
networkID uint64
}
// NewPublicNetAPI creates a new net API instance.
func NewPublicNetAPI(net *p2p.Server, networkVersion uint64) *PublicNetAPI {
return &PublicNetAPI{net, networkVersion}
func NewPublicNetAPI(net p2p.Host, networkID uint64) *PublicNetAPI {
return &PublicNetAPI{net, networkID}
}
// PeerCount returns the number of connected peers
func (s *PublicNetAPI) PeerCount() hexutil.Uint {
return hexutil.Uint(s.net.PeerCount())
return hexutil.Uint(s.net.GetPeerCount())
}
// NetworkID ...
func (s *PublicNetAPI) NetworkID() string {
return fmt.Sprintf("%d", 1) // TODO(ricl): we should add support for network id (https://github.com/ethereum/wiki/wiki/JSON-RPC#net_version)
}

@ -0,0 +1,28 @@
package hmyapi
import (
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/accounts"
)
// PublicAccountAPI provides an API to access accounts managed by this node.
// It offers only methods that can retrieve accounts.
type PublicAccountAPI struct {
am *accounts.Manager
}
// NewPublicAccountAPI creates a new PublicAccountAPI.
func NewPublicAccountAPI(am *accounts.Manager) *PublicAccountAPI {
return &PublicAccountAPI{am: am}
}
// Accounts returns the collection of accounts this node manages
func (s *PublicAccountAPI) Accounts() []common.Address {
addresses := make([]common.Address, 0) // return [] instead of nil if empty
for _, wallet := range s.am.Wallets() {
for _, account := range wallet.Accounts() {
addresses = append(addresses, account.Address)
}
}
return addresses
}

@ -132,3 +132,55 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod
}
return SubmitTransaction(ctx, s.b, tx)
}
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
if tx == nil {
return nil, nil
}
receipts, err := s.b.GetReceipts(ctx, blockHash)
if err != nil {
return nil, err
}
if len(receipts) <= int(index) {
return nil, nil
}
receipt := receipts[index]
var signer types.Signer = types.FrontierSigner{}
if tx.Protected() {
signer = types.NewEIP155Signer(tx.ChainID())
}
from, _ := types.Sender(signer, tx)
fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockNumber),
"transactionHash": hash,
"transactionIndex": hexutil.Uint64(index),
"from": from,
"to": tx.To(),
"shardID": tx.ShardID(),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
}
// Assign receipt status or post state.
if len(receipt.PostState) > 0 {
fields["root"] = hexutil.Bytes(receipt.PostState)
} else {
fields["status"] = hexutil.Uint(receipt.Status)
}
if receipt.Logs == nil {
fields["logs"] = [][]*types.Log{}
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}
return fields, nil
}

@ -14,6 +14,7 @@ type Host interface {
AddPeer(*Peer) error
GetID() libp2p_peer.ID
GetP2PHost() libp2p_host.Host
GetPeerCount() int
//AddIncomingPeer(Peer)
//AddOutgoingPeer(Peer)

@ -209,6 +209,11 @@ func (host *HostV2) GetP2PHost() libp2p_host.Host {
return host.h
}
// GetPeerCount ...
func (host *HostV2) GetPeerCount() int {
return host.h.Peerstore().Peers().Len()
}
// ConnectHostPeer connects to peer host
func (host *HostV2) ConnectHostPeer(peer p2p.Peer) {
ctx := context.Background()

@ -105,6 +105,20 @@ func (mr *MockHostMockRecorder) GetP2PHost() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetP2PHost", reflect.TypeOf((*MockHost)(nil).GetP2PHost))
}
// GetPeerCount mocks base method
func (m *MockHost) GetPeerCount() int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPeerCount")
ret0, _ := ret[0].(int)
return ret0
}
// GetPeerCount indicates an expected call of GetPeerCount
func (mr *MockHostMockRecorder) GetPeerCount() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPeerCount", reflect.TypeOf((*MockHost)(nil).GetPeerCount))
}
// ConnectHostPeer mocks base method
func (m *MockHost) ConnectHostPeer(arg0 p2p.Peer) {
m.ctrl.T.Helper()

Loading…
Cancel
Save