Skip to content

Commit 253ecdc

Browse files
committed
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
2 parents 9be7853 + 3f6e1b2 commit 253ecdc

4 files changed

Lines changed: 181 additions & 15 deletions

File tree

‎rpc/api.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ type EthereumApi struct {
2121

2222
func NewEthereumApi(xeth *xeth.XEth, dataDir string) *EthereumApi {
2323
// What about when dataDir is empty?
24-
db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps"))
24+
db, err := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps"))
25+
if err != nil {
26+
panic(err)
27+
}
2528
api := &EthereumApi{
2629
eth: xeth,
2730
db: db,
@@ -361,7 +364,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
361364
return err
362365
}
363366

364-
api.db.Put([]byte(args.Database+args.Key), []byte(args.Value))
367+
api.db.Put([]byte(args.Database+args.Key), args.Value)
365368
*reply = true
366369
case "db_getString":
367370
args := new(DbArgs)
@@ -375,8 +378,30 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
375378

376379
res, _ := api.db.Get([]byte(args.Database + args.Key))
377380
*reply = string(res)
378-
case "db_putHex", "db_getHex":
379-
return NewNotImplementedError(req.Method)
381+
case "db_putHex":
382+
args := new(DbHexArgs)
383+
if err := json.Unmarshal(req.Params, &args); err != nil {
384+
return err
385+
}
386+
387+
if err := args.requirements(); err != nil {
388+
return err
389+
}
390+
391+
api.db.Put([]byte(args.Database+args.Key), args.Value)
392+
*reply = true
393+
case "db_getHex":
394+
args := new(DbHexArgs)
395+
if err := json.Unmarshal(req.Params, &args); err != nil {
396+
return err
397+
}
398+
399+
if err := args.requirements(); err != nil {
400+
return err
401+
}
402+
403+
res, _ := api.db.Get([]byte(args.Database + args.Key))
404+
*reply = common.ToHex(res)
380405
case "shh_post":
381406
args := new(WhisperMessageArgs)
382407
if err := json.Unmarshal(req.Params, &args); err != nil {

‎rpc/api_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
// "sync"
66
"testing"
77
// "time"
8+
9+
"github.com/ethereum/go-ethereum/xeth"
810
)
911

1012
func TestWeb3Sha3(t *testing.T) {
@@ -24,6 +26,50 @@ func TestWeb3Sha3(t *testing.T) {
2426
}
2527
}
2628

29+
func TestDbStr(t *testing.T) {
30+
jsonput := `{"jsonrpc":"2.0","method":"db_putString","params":["testDB","myKey","myString"],"id":64}`
31+
jsonget := `{"jsonrpc":"2.0","method":"db_getString","params":["testDB","myKey"],"id":64}`
32+
expected := "myString"
33+
34+
xeth := &xeth.XEth{}
35+
api := NewEthereumApi(xeth, "")
36+
defer api.db.Close()
37+
var response interface{}
38+
39+
var req RpcRequest
40+
json.Unmarshal([]byte(jsonput), &req)
41+
_ = api.GetRequestReply(&req, &response)
42+
43+
json.Unmarshal([]byte(jsonget), &req)
44+
_ = api.GetRequestReply(&req, &response)
45+
46+
if response.(string) != expected {
47+
t.Errorf("Expected %s got %s", expected, response)
48+
}
49+
}
50+
51+
func TestDbHexStr(t *testing.T) {
52+
jsonput := `{"jsonrpc":"2.0","method":"db_putHex","params":["testDB","beefKey","0xbeef"],"id":64}`
53+
jsonget := `{"jsonrpc":"2.0","method":"db_getHex","params":["testDB","beefKey"],"id":64}`
54+
expected := "0xbeef"
55+
56+
xeth := &xeth.XEth{}
57+
api := NewEthereumApi(xeth, "")
58+
defer api.db.Close()
59+
var response interface{}
60+
61+
var req RpcRequest
62+
json.Unmarshal([]byte(jsonput), &req)
63+
_ = api.GetRequestReply(&req, &response)
64+
65+
json.Unmarshal([]byte(jsonget), &req)
66+
_ = api.GetRequestReply(&req, &response)
67+
68+
if response.(string) != expected {
69+
t.Errorf("Expected %s got %s", expected, response)
70+
}
71+
}
72+
2773
// func TestFilterClose(t *testing.T) {
2874
// t.Skip()
2975
// api := &EthereumApi{

‎rpc/args.go‎

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,39 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) {
497497
type DbArgs struct {
498498
Database string
499499
Key string
500-
Value string
500+
Value []byte
501501
}
502502

503503
func (args *DbArgs) UnmarshalJSON(b []byte) (err error) {
504504
var obj []interface{}
505-
r := bytes.NewReader(b)
506-
if err := json.NewDecoder(r).Decode(&obj); err != nil {
505+
if err := json.Unmarshal(b, &obj); err != nil {
507506
return NewDecodeParamError(err.Error())
508507
}
509508

510509
if len(obj) < 2 {
511510
return NewInsufficientParamsError(len(obj), 2)
512511
}
513-
args.Database = obj[0].(string)
514-
args.Key = obj[1].(string)
512+
513+
var objstr string
514+
var ok bool
515+
516+
if objstr, ok = obj[0].(string); !ok {
517+
return NewDecodeParamError("Database is not a string")
518+
}
519+
args.Database = objstr
520+
521+
if objstr, ok = obj[1].(string); !ok {
522+
return NewDecodeParamError("Key is not a string")
523+
}
524+
args.Key = objstr
515525

516526
if len(obj) > 2 {
517-
args.Value = obj[2].(string)
527+
objstr, ok = obj[2].(string)
528+
if !ok {
529+
return NewDecodeParamError("Value is not a string")
530+
}
531+
532+
args.Value = []byte(objstr)
518533
}
519534

520535
return nil
@@ -530,6 +545,57 @@ func (a *DbArgs) requirements() error {
530545
return nil
531546
}
532547

548+
type DbHexArgs struct {
549+
Database string
550+
Key string
551+
Value []byte
552+
}
553+
554+
func (args *DbHexArgs) UnmarshalJSON(b []byte) (err error) {
555+
var obj []interface{}
556+
if err := json.Unmarshal(b, &obj); err != nil {
557+
return NewDecodeParamError(err.Error())
558+
}
559+
560+
if len(obj) < 2 {
561+
return NewInsufficientParamsError(len(obj), 2)
562+
}
563+
564+
var objstr string
565+
var ok bool
566+
567+
if objstr, ok = obj[0].(string); !ok {
568+
return NewDecodeParamError("Database is not a string")
569+
}
570+
args.Database = objstr
571+
572+
if objstr, ok = obj[1].(string); !ok {
573+
return NewDecodeParamError("Key is not a string")
574+
}
575+
args.Key = objstr
576+
577+
if len(obj) > 2 {
578+
objstr, ok = obj[2].(string)
579+
if !ok {
580+
return NewDecodeParamError("Value is not a string")
581+
}
582+
583+
args.Value = common.FromHex(objstr)
584+
}
585+
586+
return nil
587+
}
588+
589+
func (a *DbHexArgs) requirements() error {
590+
if len(a.Database) == 0 {
591+
return NewValidationError("Database", "cannot be blank")
592+
}
593+
if len(a.Key) == 0 {
594+
return NewValidationError("Key", "cannot be blank")
595+
}
596+
return nil
597+
}
598+
533599
type WhisperMessageArgs struct {
534600
Payload string
535601
To string

‎rpc/args_test.go‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ func TestBlockFilterArgsEmptyArgs(t *testing.T) {
485485
}
486486

487487
func TestDbArgs(t *testing.T) {
488-
input := `["0x74657374","0x6b6579","0x6d79537472696e67"]`
488+
input := `["testDB","myKey","0xbeef"]`
489489
expected := new(DbArgs)
490-
expected.Database = "0x74657374"
491-
expected.Key = "0x6b6579"
492-
expected.Value = "0x6d79537472696e67"
490+
expected.Database = "testDB"
491+
expected.Key = "myKey"
492+
expected.Value = []byte("0xbeef")
493493

494494
args := new(DbArgs)
495495
if err := json.Unmarshal([]byte(input), &args); err != nil {
@@ -508,7 +508,36 @@ func TestDbArgs(t *testing.T) {
508508
t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key)
509509
}
510510

511-
if expected.Value != args.Value {
511+
if bytes.Compare(expected.Value, args.Value) != 0 {
512+
t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
513+
}
514+
}
515+
516+
func TestDbHexArgs(t *testing.T) {
517+
input := `["testDB","myKey","0xbeef"]`
518+
expected := new(DbHexArgs)
519+
expected.Database = "testDB"
520+
expected.Key = "myKey"
521+
expected.Value = []byte{0xbe, 0xef}
522+
523+
args := new(DbHexArgs)
524+
if err := json.Unmarshal([]byte(input), &args); err != nil {
525+
t.Error(err)
526+
}
527+
528+
if err := args.requirements(); err != nil {
529+
t.Error(err)
530+
}
531+
532+
if expected.Database != args.Database {
533+
t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database)
534+
}
535+
536+
if expected.Key != args.Key {
537+
t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key)
538+
}
539+
540+
if bytes.Compare(expected.Value, args.Value) != 0 {
512541
t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
513542
}
514543
}

0 commit comments

Comments
 (0)