aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
Diffstat (limited to 'rpc')
-rw-r--r--rpc/args.go43
-rw-r--r--rpc/message.go62
-rw-r--r--rpc/packages.go82
3 files changed, 174 insertions, 13 deletions
diff --git a/rpc/args.go b/rpc/args.go
index 79519e7d2..aaa017c4e 100644
--- a/rpc/args.go
+++ b/rpc/args.go
@@ -69,10 +69,28 @@ func (a *PushTxArgs) requirementsPushTx() error {
type GetStorageArgs struct {
Address string
- Key string
}
func (obj *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
+ if err = json.Unmarshal(b, &obj.Address); err != nil {
+ return NewErrorResponse(ErrorDecodeArgs)
+ }
+ return
+}
+
+func (a *GetStorageArgs) requirements() error {
+ if len(a.Address) == 0 {
+ return NewErrorResponse("GetStorageAt requires an 'address' value as argument")
+ }
+ return nil
+}
+
+type GetStateArgs struct {
+ Address string
+ Key string
+}
+
+func (obj *GetStateArgs) UnmarshalJSON(b []byte) (err error) {
arg0 := ""
if err = json.Unmarshal(b, arg0); err == nil {
obj.Address = arg0
@@ -81,7 +99,7 @@ func (obj *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
return NewErrorResponse(ErrorDecodeArgs)
}
-func (a *GetStorageArgs) requirements() error {
+func (a *GetStateArgs) requirements() error {
if a.Address == "" {
return NewErrorResponse("GetStorageAt requires an 'address' value as argument")
}
@@ -92,9 +110,8 @@ func (a *GetStorageArgs) requirements() error {
}
type GetStorageAtRes struct {
- Key string `json:"key"`
- Value string `json:"value"`
- Address string `json:"address"`
+ Key string `json:"key"`
+ Value string `json:"value"`
}
type GetTxCountArgs struct {
@@ -218,3 +235,19 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions {
type FilterChangedArgs struct {
n int
}
+
+type DbArgs struct {
+ Database string
+ Key string
+ Value string
+}
+
+func (a *DbArgs) requirements() error {
+ if len(a.Database) == 0 {
+ return NewErrorResponse("DbPutArgs requires an 'Database' value as argument")
+ }
+ if len(a.Key) == 0 {
+ return NewErrorResponse("DbPutArgs requires an 'Key' value as argument")
+ }
+ return nil
+}
diff --git a/rpc/message.go b/rpc/message.go
index 440bf4f8f..5045adb8f 100644
--- a/rpc/message.go
+++ b/rpc/message.go
@@ -124,12 +124,12 @@ func (req *RpcRequest) ToPushTxArgs() (*PushTxArgs, error) {
return args, nil
}
-func (req *RpcRequest) ToGetStorageArgs() (*GetStorageArgs, error) {
- if len(req.Params) < 2 {
+func (req *RpcRequest) ToGetStateArgs() (*GetStateArgs, error) {
+ if len(req.Params) < 1 {
return nil, NewErrorResponse(ErrorArguments)
}
- args := new(GetStorageArgs)
+ args := new(GetStateArgs)
// TODO need to pass both arguments
r := bytes.NewReader(req.Params[0])
err := json.NewDecoder(r).Decode(args)
@@ -140,6 +140,21 @@ func (req *RpcRequest) ToGetStorageArgs() (*GetStorageArgs, error) {
return args, nil
}
+func (req *RpcRequest) ToStorageAtArgs() (*GetStorageArgs, error) {
+ if len(req.Params) < 1 {
+ return nil, NewErrorResponse(ErrorArguments)
+ }
+
+ args := new(GetStorageArgs)
+ r := bytes.NewReader(req.Params[0])
+ err := json.NewDecoder(r).Decode(args)
+ if err != nil {
+ return nil, NewErrorResponse(ErrorDecodeArgs)
+ }
+ rpclogger.DebugDetailf("%T %v", args, args)
+ return args, nil
+}
+
func (req *RpcRequest) ToGetTxCountArgs() (*GetTxCountArgs, error) {
if len(req.Params) < 1 {
return nil, NewErrorResponse(ErrorArguments)
@@ -214,3 +229,44 @@ func (req *RpcRequest) ToFilterChangedArgs() (int, error) {
rpclogger.DebugDetailf("%T %v", id, id)
return id, nil
}
+
+func (req *RpcRequest) ToDbPutArgs() (*DbArgs, error) {
+ if len(req.Params) < 3 {
+ return nil, NewErrorResponse(ErrorArguments)
+ }
+
+ var args DbArgs
+ err := json.Unmarshal(req.Params[0], &args.Database)
+ if err != nil {
+ return nil, NewErrorResponseWithError(ErrorDecodeArgs, err)
+ }
+ err = json.Unmarshal(req.Params[1], &args.Key)
+ if err != nil {
+ return nil, NewErrorResponseWithError(ErrorDecodeArgs, err)
+ }
+ err = json.Unmarshal(req.Params[2], &args.Value)
+ if err != nil {
+ return nil, NewErrorResponseWithError(ErrorDecodeArgs, err)
+ }
+ rpclogger.DebugDetailf("%T %v", args, args)
+ return &args, nil
+}
+
+func (req *RpcRequest) ToDbGetArgs() (*DbArgs, error) {
+ if len(req.Params) < 2 {
+ return nil, NewErrorResponse(ErrorArguments)
+ }
+
+ var args DbArgs
+ err := json.Unmarshal(req.Params[0], &args.Database)
+ if err != nil {
+ return nil, NewErrorResponseWithError(ErrorDecodeArgs, err)
+ }
+
+ err = json.Unmarshal(req.Params[1], &args.Key)
+ if err != nil {
+ return nil, NewErrorResponseWithError(ErrorDecodeArgs, err)
+ }
+ rpclogger.DebugDetailf("%T %v", args, args)
+ return &args, nil
+}
diff --git a/rpc/packages.go b/rpc/packages.go
index 53159d9ba..6136adf49 100644
--- a/rpc/packages.go
+++ b/rpc/packages.go
@@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event/filter"
"github.com/ethereum/go-ethereum/state"
@@ -45,13 +46,17 @@ type EthereumApi struct {
mut sync.RWMutex
logs map[int]state.Logs
+
+ db ethutil.Database
}
func NewEthereumApi(xeth *xeth.XEth) *EthereumApi {
+ db, _ := ethdb.NewLDBDatabase("dapps")
api := &EthereumApi{
xeth: xeth,
filterManager: filter.NewFilterManager(xeth.Backend().EventMux()),
logs: make(map[int]state.Logs),
+ db: db,
}
go api.filterManager.Start()
@@ -135,7 +140,7 @@ func (p *EthereumApi) PushTx(args *PushTxArgs, reply *interface{}) error {
return nil
}
-func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) error {
+func (p *EthereumApi) GetStateAt(args *GetStateArgs, reply *interface{}) error {
err := args.requirements()
if err != nil {
return err
@@ -143,6 +148,7 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) err
state := p.xeth.State().SafeGet(args.Address)
+ value := state.StorageString(args.Key)
var hx string
if strings.Index(args.Key, "0x") == 0 {
hx = string([]byte(args.Key)[2:])
@@ -151,9 +157,18 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) err
i, _ := new(big.Int).SetString(args.Key, 10)
hx = ethutil.Bytes2Hex(i.Bytes())
}
- rpclogger.Debugf("GetStorageAt(%s, %s)\n", args.Address, hx)
- value := state.Storage(ethutil.Hex2Bytes(hx))
- *reply = GetStorageAtRes{Address: args.Address, Key: args.Key, Value: value.Str()}
+ rpclogger.Debugf("GetStateAt(%s, %s)\n", args.Address, hx)
+ *reply = map[string]string{args.Key: value.Str()}
+ return nil
+}
+
+func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) error {
+ err := args.requirements()
+ if err != nil {
+ return err
+ }
+
+ *reply = p.xeth.State().SafeGet(args.Address).Storage()
return nil
}
@@ -172,11 +187,21 @@ func (p *EthereumApi) GetCoinbase(reply *interface{}) error {
return nil
}
+func (p *EthereumApi) Accounts(reply *interface{}) error {
+ *reply = p.xeth.Accounts()
+ return nil
+}
+
func (p *EthereumApi) GetIsMining(reply *interface{}) error {
*reply = p.xeth.IsMining()
return nil
}
+func (p *EthereumApi) BlockNumber(reply *interface{}) error {
+ *reply = p.xeth.Backend().ChainManager().CurrentBlock().Number()
+ return nil
+}
+
func (p *EthereumApi) GetTxCountAt(args *GetTxCountArgs, reply *interface{}) error {
err := args.requirements()
if err != nil {
@@ -210,6 +235,28 @@ func (p *EthereumApi) Sha3(args *Sha3Args, reply *interface{}) error {
return nil
}
+func (p *EthereumApi) DbPut(args *DbArgs, reply *interface{}) error {
+ err := args.requirements()
+ if err != nil {
+ return err
+ }
+
+ p.db.Put([]byte(args.Database+args.Key), []byte(args.Value))
+ *reply = true
+ return nil
+}
+
+func (p *EthereumApi) DbGet(args *DbArgs, reply *interface{}) error {
+ err := args.requirements()
+ if err != nil {
+ return err
+ }
+
+ res, _ := p.db.Get([]byte(args.Database + args.Key))
+ *reply = string(res)
+ return nil
+}
+
func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
// Spec at https://github.com/ethereum/wiki/wiki/Generic-ON-RPC
rpclogger.DebugDetailf("%T %s", req.Params, req.Params)
@@ -222,6 +269,10 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return p.GetIsMining(reply)
case "eth_peerCount":
return p.GetPeerCount(reply)
+ case "eth_number":
+ return p.BlockNumber(reply)
+ case "eth_accounts":
+ return p.Accounts(reply)
case "eth_countAt":
args, err := req.ToGetTxCountArgs()
if err != nil {
@@ -241,7 +292,13 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
}
return p.GetBalanceAt(args, reply)
case "eth_stateAt":
- args, err := req.ToGetStorageArgs()
+ args, err := req.ToGetStateArgs()
+ if err != nil {
+ return err
+ }
+ return p.GetStateAt(args, reply)
+ case "eth_storageAt":
+ args, err := req.ToStorageAtArgs()
if err != nil {
return err
}
@@ -276,12 +333,27 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err
}
return p.FilterChanged(args, reply)
+ case "eth_gasPrice":
+ *reply = "1000000000000000"
+ return nil
case "web3_sha3":
args, err := req.ToSha3Args()
if err != nil {
return err
}
return p.Sha3(args, reply)
+ case "db_put":
+ args, err := req.ToDbPutArgs()
+ if err != nil {
+ return err
+ }
+ return p.DbPut(args, reply)
+ case "db_get":
+ args, err := req.ToDbGetArgs()
+ if err != nil {
+ return err
+ }
+ return p.DbGet(args, reply)
default:
return NewErrorResponse(fmt.Sprintf("%v %s", ErrorNotImplemented, req.Method))
}