aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'rpc/api.go')
-rw-r--r--rpc/api.go255
1 files changed, 98 insertions, 157 deletions
diff --git a/rpc/api.go b/rpc/api.go
index d5e18eec8..78e464c99 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -3,13 +3,10 @@ package rpc
import (
"encoding/json"
"math/big"
- "path"
"sync"
"github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/xeth"
)
@@ -17,37 +14,29 @@ type EthereumApi struct {
eth *xeth.XEth
xethMu sync.RWMutex
db common.Database
-
- // Miner agent
- agent *Agent
}
-func NewEthereumApi(eth *xeth.XEth, dataDir string) *EthereumApi {
- // What about when dataDir is empty?
- db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps"))
+func NewEthereumApi(xeth *xeth.XEth) *EthereumApi {
api := &EthereumApi{
- eth: eth,
- db: db,
- agent: NewAgent(),
+ eth: xeth,
}
- eth.Backend().Miner().Register(api.agent)
return api
}
-func (self *EthereumApi) xeth() *xeth.XEth {
- self.xethMu.RLock()
- defer self.xethMu.RUnlock()
+func (api *EthereumApi) xeth() *xeth.XEth {
+ api.xethMu.RLock()
+ defer api.xethMu.RUnlock()
- return self.eth
+ return api.eth
}
-func (self *EthereumApi) xethAtStateNum(num int64) *xeth.XEth {
- return self.xeth().AtStateNum(num)
+func (api *EthereumApi) xethAtStateNum(num int64) *xeth.XEth {
+ return api.xeth().AtStateNum(num)
}
-func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
- // Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC
+func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
+ // Spec at https://github.com/ethereum/wiki/wiki/JSON-RPC
rpclogger.Debugf("%s %s", req.Method, req.Params)
switch req.Method {
@@ -58,31 +47,33 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
}
*reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data)))
case "web3_clientVersion":
- *reply = p.xeth().Backend().Version()
+ *reply = api.xeth().ClientVersion()
case "net_version":
- return NewNotImplementedError(req.Method)
+ *reply = api.xeth().NetworkVersion()
case "net_listening":
- *reply = p.xeth().IsListening()
+ *reply = api.xeth().IsListening()
case "net_peerCount":
- v := p.xeth().PeerCount()
+ v := api.xeth().PeerCount()
*reply = common.ToHex(big.NewInt(int64(v)).Bytes())
+ case "eth_version":
+ *reply = api.xeth().EthVersion()
case "eth_coinbase":
// TODO handling of empty coinbase due to lack of accounts
- res := p.xeth().Coinbase()
+ res := api.xeth().Coinbase()
if res == "0x" || res == "0x0" {
*reply = nil
} else {
*reply = res
}
case "eth_mining":
- *reply = p.xeth().IsMining()
+ *reply = api.xeth().IsMining()
case "eth_gasPrice":
- v := p.xeth().DefaultGas()
+ v := api.xeth().DefaultGas()
*reply = common.ToHex(v.Bytes())
case "eth_accounts":
- *reply = p.xeth().Accounts()
+ *reply = api.xeth().Accounts()
case "eth_blockNumber":
- v := p.xeth().Backend().ChainManager().CurrentBlock().Number()
+ v := api.xeth().CurrentBlock().Number()
*reply = common.ToHex(v.Bytes())
case "eth_getBalance":
args := new(GetBalanceArgs)
@@ -90,11 +81,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
-
- v := p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
+ v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
*reply = common.ToHex(v.Bytes())
case "eth_getStorage", "eth_storageAt":
args := new(GetStorageArgs)
@@ -102,21 +89,14 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
-
- *reply = p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
+ *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
case "eth_getStorageAt":
args := new(GetStorageAtArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
- state := p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address)
+ state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address)
value := state.StorageString(args.Key)
*reply = common.Bytes2Hex(value.Bytes())
@@ -126,19 +106,15 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- err := args.requirements()
- if err != nil {
- return err
- }
-
- *reply = p.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
+ count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
+ *reply = common.ToHex(big.NewInt(int64(count)).Bytes())
case "eth_getBlockTransactionCountByHash":
args := new(GetBlockByHashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- block := NewBlockRes(p.xeth().EthBlockByHash(args.BlockHash))
+ block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash))
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
case "eth_getBlockTransactionCountByNumber":
args := new(GetBlockByNumberArgs)
@@ -146,7 +122,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := NewBlockRes(p.xeth().EthBlockByNumber(args.BlockNumber))
+ block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber))
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
case "eth_getUncleCountByBlockHash":
args := new(GetBlockByHashArgs)
@@ -154,7 +130,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByHash(args.BlockHash)
+ block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block)
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
case "eth_getUncleCountByBlockNumber":
@@ -163,7 +139,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByNumber(args.BlockNumber)
+ block := api.xeth().EthBlockByNumber(args.BlockNumber)
br := NewBlockRes(block)
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
case "eth_getData", "eth_getCode":
@@ -171,21 +147,14 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
- *reply = p.xethAtStateNum(args.BlockNumber).CodeAt(args.Address)
+ *reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address)
case "eth_sendTransaction", "eth_transact":
args := new(NewTxArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
-
- v, err := p.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
+ v, err := api.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
if err != nil {
return err
}
@@ -196,7 +165,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- v, err := p.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
+ v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
if err != nil {
return err
}
@@ -210,7 +179,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByHash(args.BlockHash)
+ block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block)
br.fullTx = args.IncludeTxs
@@ -221,7 +190,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByNumber(args.BlockNumber)
+ block := api.xeth().EthBlockByNumber(args.BlockNumber)
br := NewBlockRes(block)
br.fullTx = args.IncludeTxs
@@ -231,7 +200,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
args := new(HashIndexArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
}
- tx := p.xeth().EthTransactionByHash(args.Hash)
+ tx := api.xeth().EthTransactionByHash(args.Hash)
if tx != nil {
*reply = NewTransactionRes(tx)
}
@@ -241,7 +210,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByHash(args.Hash)
+ block := api.xeth().EthBlockByHash(args.Hash)
br := NewBlockRes(block)
br.fullTx = true
@@ -255,7 +224,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByNumber(args.BlockNumber)
+ block := api.xeth().EthBlockByNumber(args.BlockNumber)
v := NewBlockRes(block)
v.fullTx = true
@@ -269,14 +238,14 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- br := NewBlockRes(p.xeth().EthBlockByHash(args.Hash))
+ br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash))
if args.Index > int64(len(br.Uncles)) || args.Index < 0 {
return NewValidationError("Index", "does not exist")
}
- uhash := br.Uncles[args.Index].Hex()
- uncle := NewBlockRes(p.xeth().EthBlockByHash(uhash))
+ uhash := br.Uncles[args.Index]
+ uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle
case "eth_getUncleByBlockNumberAndIndex":
@@ -285,7 +254,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- block := p.xeth().EthBlockByNumber(args.BlockNumber)
+ block := api.xeth().EthBlockByNumber(args.BlockNumber)
v := NewBlockRes(block)
v.fullTx = true
@@ -293,8 +262,8 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return NewValidationError("Index", "does not exist")
}
- uhash := v.Uncles[args.Index].Hex()
- uncle := NewBlockRes(p.xeth().EthBlockByHash(uhash))
+ uhash := v.Uncles[args.Index]
+ uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle
case "eth_getCompilers":
@@ -308,53 +277,48 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- opts := toFilterOptions(args)
- id := p.xeth().RegisterFilter(opts)
+ id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "eth_newBlockFilter":
args := new(FilterStringArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- if err := args.requirements(); err != nil {
- return err
- }
-
- id := p.xeth().NewFilterString(args.Word)
+ id := api.xeth().NewFilterString(args.Word)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "eth_uninstallFilter":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = p.xeth().UninstallFilter(args.Id)
+ *reply = api.xeth().UninstallFilter(args.Id)
case "eth_getFilterChanges":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = NewLogsRes(p.xeth().FilterChanged(args.Id))
+ *reply = NewLogsRes(api.xeth().FilterChanged(args.Id))
case "eth_getFilterLogs":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = NewLogsRes(p.xeth().Logs(args.Id))
+ *reply = NewLogsRes(api.xeth().Logs(args.Id))
case "eth_getLogs":
args := new(BlockFilterArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- opts := toFilterOptions(args)
- *reply = NewLogsRes(p.xeth().AllLogs(opts))
+ *reply = NewLogsRes(api.xeth().AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics))
case "eth_getWork":
- *reply = p.getWork()
+ api.xeth().SetMining(true)
+ *reply = api.xeth().RemoteMining().GetWork()
case "eth_submitWork":
- // TODO what is the reply here?
- // TODO what are the arguments?
- p.agent.SetResult(0, common.Hash{}, common.Hash{})
-
- return NewNotImplementedError(req.Method)
+ args := new(SubmitWorkArgs)
+ if err := json.Unmarshal(req.Params, &args); err != nil {
+ return err
+ }
+ *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
case "db_putString":
args := new(DbArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -365,7 +329,8 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- p.db.Put([]byte(args.Database+args.Key), []byte(args.Value))
+ api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
+
*reply = true
case "db_getString":
args := new(DbArgs)
@@ -377,36 +342,60 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
- res, _ := p.db.Get([]byte(args.Database + args.Key))
+ res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
*reply = string(res)
- case "db_putHex", "db_getHex":
- return NewNotImplementedError(req.Method)
+ case "db_putHex":
+ args := new(DbHexArgs)
+ if err := json.Unmarshal(req.Params, &args); err != nil {
+ return err
+ }
+
+ if err := args.requirements(); err != nil {
+ return err
+ }
+
+ api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
+ *reply = true
+ case "db_getHex":
+ args := new(DbHexArgs)
+ if err := json.Unmarshal(req.Params, &args); err != nil {
+ return err
+ }
+
+ if err := args.requirements(); err != nil {
+ return err
+ }
+
+ res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
+ *reply = common.ToHex(res)
+ case "shh_version":
+ *reply = api.xeth().WhisperVersion()
case "shh_post":
args := new(WhisperMessageArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- err := p.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
+ err := api.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
if err != nil {
return err
}
*reply = true
case "shh_newIdentity":
- *reply = p.xeth().Whisper().NewIdentity()
+ *reply = api.xeth().Whisper().NewIdentity()
// case "shh_removeIdentity":
// args := new(WhisperIdentityArgs)
// if err := json.Unmarshal(req.Params, &args); err != nil {
// return err
// }
- // *reply = p.xeth().Whisper().RemoveIdentity(args.Identity)
+ // *reply = api.xeth().Whisper().RemoveIdentity(args.Identity)
case "shh_hasIdentity":
args := new(WhisperIdentityArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = p.xeth().Whisper().HasIdentity(args.Identity)
+ *reply = api.xeth().Whisper().HasIdentity(args.Identity)
case "shh_newGroup", "shh_addToGroup":
return NewNotImplementedError(req.Method)
case "shh_newFilter":
@@ -415,29 +404,29 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
opts := new(xeth.Options)
- opts.From = args.From
+ // opts.From = args.From
opts.To = args.To
opts.Topics = args.Topics
- id := p.xeth().NewWhisperFilter(opts)
+ id := api.xeth().NewWhisperFilter(opts)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "shh_uninstallFilter":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = p.xeth().UninstallWhisperFilter(args.Id)
+ *reply = api.xeth().UninstallWhisperFilter(args.Id)
case "shh_getFilterChanges":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = p.xeth().MessagesChanged(args.Id)
+ *reply = api.xeth().MessagesChanged(args.Id)
case "shh_getMessages":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- *reply = p.xeth().Whisper().Messages(args.Id)
+ *reply = api.xeth().Whisper().Messages(args.Id)
// case "eth_register":
// // Placeholder for actual type
@@ -445,19 +434,19 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
// if err := json.Unmarshal(req.Params, &args); err != nil {
// return err
// }
- // *reply = p.xeth().Register(args.Hash)
+ // *reply = api.xeth().Register(args.Hash)
// case "eth_unregister":
// args := new(HashIndexArgs)
// if err := json.Unmarshal(req.Params, &args); err != nil {
// return err
// }
- // *reply = p.xeth().Unregister(args.Hash)
+ // *reply = api.xeth().Unregister(args.Hash)
// case "eth_watchTx":
// args := new(HashIndexArgs)
// if err := json.Unmarshal(req.Params, &args); err != nil {
// return err
// }
- // *reply = p.xeth().PullWatchTx(args.Hash)
+ // *reply = api.xeth().PullWatchTx(args.Hash)
default:
return NewNotImplementedError(req.Method)
}
@@ -465,51 +454,3 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
return nil
}
-
-func (p *EthereumApi) getWork() string {
- p.xeth().SetMining(true)
- return p.agent.GetWork().Hex()
-}
-
-func toFilterOptions(options *BlockFilterArgs) *core.FilterOptions {
- var opts core.FilterOptions
-
- // Convert optional address slice/string to byte slice
- if str, ok := options.Address.(string); ok {
- opts.Address = []common.Address{common.HexToAddress(str)}
- } else if slice, ok := options.Address.([]interface{}); ok {
- bslice := make([]common.Address, len(slice))
- for i, addr := range slice {
- if saddr, ok := addr.(string); ok {
- bslice[i] = common.HexToAddress(saddr)
- }
- }
- opts.Address = bslice
- }
-
- opts.Earliest = options.Earliest
- opts.Latest = options.Latest
-
- topics := make([][]common.Hash, len(options.Topics))
- for i, topicDat := range options.Topics {
- if slice, ok := topicDat.([]interface{}); ok {
- topics[i] = make([]common.Hash, len(slice))
- for j, topic := range slice {
- topics[i][j] = common.HexToHash(topic.(string))
- }
- } else if str, ok := topicDat.(string); ok {
- topics[i] = []common.Hash{common.HexToHash(str)}
- }
- }
- opts.Topics = topics
-
- return &opts
-}
-
-/*
- Work() chan<- *types.Block
- SetWorkCh(chan<- Work)
- Stop()
- Start()
- Rate() uint64
-*/