diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-27 23:03:49 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-27 23:03:49 +0800 |
commit | 0ac346f7078dba597d60f991c32ddbfd7be167ba (patch) | |
tree | 33a690fa6ef7cbc8584ff2a1f40f85b28b42ac7f /rpc | |
parent | 9f84c78eb5cceb5f413fbdeafe63786f1b958e83 (diff) | |
parent | eb102bf4bb0bff773824ff467fbb2e49c1f6939b (diff) | |
download | dexon-0ac346f7078dba597d60f991c32ddbfd7be167ba.tar.gz dexon-0ac346f7078dba597d60f991c32ddbfd7be167ba.tar.zst dexon-0ac346f7078dba597d60f991c32ddbfd7be167ba.zip |
Merge branch 'develop' into rpcargs
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 23 | ||||
-rw-r--r-- | rpc/api_test.go | 69 | ||||
-rw-r--r-- | rpc/http.go | 4 | ||||
-rw-r--r-- | rpc/responses.go | 137 | ||||
-rw-r--r-- | rpc/responses_test.go | 123 |
5 files changed, 244 insertions, 112 deletions
diff --git a/rpc/api.go b/rpc/api.go index 534b6fc5d..f755f07bd 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -3,13 +3,11 @@ 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" ) @@ -19,15 +17,9 @@ type EthereumApi struct { db common.Database } -func NewEthereumApi(xeth *xeth.XEth, dataDir string) *EthereumApi { - // What about when dataDir is empty? - db, err := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps")) - if err != nil { - panic(err) - } +func NewEthereumApi(xeth *xeth.XEth) *EthereumApi { api := &EthereumApi{ eth: xeth, - db: db, } return api @@ -44,10 +36,6 @@ func (api *EthereumApi) xethAtStateNum(num int64) *xeth.XEth { return api.xeth().AtStateNum(num) } -func (api *EthereumApi) Close() { - api.db.Close() -} - 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) @@ -343,7 +331,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - api.db.Put([]byte(args.Database+args.Key), args.Value) + api.xeth().DbPut([]byte(args.Database+args.Key), args.Value) + *reply = true case "db_getString": args := new(DbArgs) @@ -355,7 +344,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - res, _ := api.db.Get([]byte(args.Database + args.Key)) + res, _ := api.xeth().DbGet([]byte(args.Database + args.Key)) *reply = string(res) case "db_putHex": args := new(DbHexArgs) @@ -367,7 +356,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - api.db.Put([]byte(args.Database+args.Key), args.Value) + api.xeth().DbPut([]byte(args.Database+args.Key), args.Value) *reply = true case "db_getHex": args := new(DbHexArgs) @@ -379,7 +368,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - res, _ := api.db.Get([]byte(args.Database + args.Key)) + res, _ := api.xeth().DbGet([]byte(args.Database + args.Key)) *reply = common.ToHex(res) case "shh_version": *reply = api.xeth().WhisperVersion() diff --git a/rpc/api_test.go b/rpc/api_test.go index a00c2f3f1..ac9b67fac 100644 --- a/rpc/api_test.go +++ b/rpc/api_test.go @@ -6,7 +6,7 @@ import ( "testing" // "time" - "github.com/ethereum/go-ethereum/xeth" + // "github.com/ethereum/go-ethereum/xeth" ) func TestWeb3Sha3(t *testing.T) { @@ -26,49 +26,48 @@ func TestWeb3Sha3(t *testing.T) { } } -func TestDbStr(t *testing.T) { - jsonput := `{"jsonrpc":"2.0","method":"db_putString","params":["testDB","myKey","myString"],"id":64}` - jsonget := `{"jsonrpc":"2.0","method":"db_getString","params":["testDB","myKey"],"id":64}` - expected := "myString" +// func TestDbStr(t *testing.T) { +// jsonput := `{"jsonrpc":"2.0","method":"db_putString","params":["testDB","myKey","myString"],"id":64}` +// jsonget := `{"jsonrpc":"2.0","method":"db_getString","params":["testDB","myKey"],"id":64}` +// expected := "myString" - xeth := &xeth.XEth{} - api := NewEthereumApi(xeth, "") - defer api.db.Close() - var response interface{} +// xeth := &xeth.XEth{} +// api := NewEthereumApi(xeth) +// var response interface{} - var req RpcRequest - json.Unmarshal([]byte(jsonput), &req) - _ = api.GetRequestReply(&req, &response) +// var req RpcRequest +// json.Unmarshal([]byte(jsonput), &req) +// _ = api.GetRequestReply(&req, &response) - json.Unmarshal([]byte(jsonget), &req) - _ = api.GetRequestReply(&req, &response) +// json.Unmarshal([]byte(jsonget), &req) +// _ = api.GetRequestReply(&req, &response) - if response.(string) != expected { - t.Errorf("Expected %s got %s", expected, response) - } -} +// if response.(string) != expected { +// t.Errorf("Expected %s got %s", expected, response) +// } +// } -func TestDbHexStr(t *testing.T) { - jsonput := `{"jsonrpc":"2.0","method":"db_putHex","params":["testDB","beefKey","0xbeef"],"id":64}` - jsonget := `{"jsonrpc":"2.0","method":"db_getHex","params":["testDB","beefKey"],"id":64}` - expected := "0xbeef" +// func TestDbHexStr(t *testing.T) { +// jsonput := `{"jsonrpc":"2.0","method":"db_putHex","params":["testDB","beefKey","0xbeef"],"id":64}` +// jsonget := `{"jsonrpc":"2.0","method":"db_getHex","params":["testDB","beefKey"],"id":64}` +// expected := "0xbeef" - xeth := &xeth.XEth{} - api := NewEthereumApi(xeth, "") - defer api.db.Close() - var response interface{} +// xeth := &xeth.XEth{} +// api := NewEthereumApi(xeth) +// defer api.db.Close() +// var response interface{} - var req RpcRequest - json.Unmarshal([]byte(jsonput), &req) - _ = api.GetRequestReply(&req, &response) +// var req RpcRequest +// json.Unmarshal([]byte(jsonput), &req) +// _ = api.GetRequestReply(&req, &response) - json.Unmarshal([]byte(jsonget), &req) - _ = api.GetRequestReply(&req, &response) +// json.Unmarshal([]byte(jsonget), &req) +// _ = api.GetRequestReply(&req, &response) - if response.(string) != expected { - t.Errorf("Expected %s got %s", expected, response) - } -} +// if response.(string) != expected { +// t.Errorf("Expected %s got %s", expected, response) +// } +// } // func TestFilterClose(t *testing.T) { // t.Skip() diff --git a/rpc/http.go b/rpc/http.go index 3dfb67781..879ffce3b 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -18,8 +18,8 @@ const ( ) // JSONRPC returns a handler that implements the Ethereum JSON-RPC API. -func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { - api := NewEthereumApi(pipe, dataDir) +func JSONRPC(pipe *xeth.XEth) http.Handler { + api := NewEthereumApi(pipe) return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { // TODO this needs to be configurable diff --git a/rpc/responses.go b/rpc/responses.go index 993f467ea..f5f3a33f3 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -6,14 +6,14 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" ) type BlockRes struct { fullTx bool - BlockNumber int64 `json:"number"` + BlockNumber *big.Int `json:"number"` BlockHash common.Hash `json:"hash"` ParentHash common.Hash `json:"parentHash"` Nonce [8]byte `json:"nonce"` @@ -22,13 +22,13 @@ type BlockRes struct { TransactionRoot common.Hash `json:"transactionRoot"` StateRoot common.Hash `json:"stateRoot"` Miner common.Address `json:"miner"` - Difficulty int64 `json:"difficulty"` - TotalDifficulty int64 `json:"totalDifficulty"` - Size int64 `json:"size"` + Difficulty *big.Int `json:"difficulty"` + TotalDifficulty *big.Int `json:"totalDifficulty"` + Size *big.Int `json:"size"` ExtraData []byte `json:"extraData"` - GasLimit int64 `json:"gasLimit"` + GasLimit *big.Int `json:"gasLimit"` MinGasPrice int64 `json:"minGasPrice"` - GasUsed int64 `json:"gasUsed"` + GasUsed *big.Int `json:"gasUsed"` UnixTimestamp int64 `json:"timestamp"` Transactions []*TransactionRes `json:"transactions"` Uncles []common.Hash `json:"uncles"` @@ -58,7 +58,7 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { } // convert strict types to hexified strings - ext.BlockNumber = common.ToHex(big.NewInt(b.BlockNumber).Bytes()) + ext.BlockNumber = common.ToHex(b.BlockNumber.Bytes()) ext.BlockHash = b.BlockHash.Hex() ext.ParentHash = b.ParentHash.Hex() ext.Nonce = common.ToHex(b.Nonce[:]) @@ -67,13 +67,13 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { ext.TransactionRoot = b.TransactionRoot.Hex() ext.StateRoot = b.StateRoot.Hex() ext.Miner = b.Miner.Hex() - ext.Difficulty = common.ToHex(big.NewInt(b.Difficulty).Bytes()) - ext.TotalDifficulty = common.ToHex(big.NewInt(b.TotalDifficulty).Bytes()) - ext.Size = common.ToHex(big.NewInt(b.Size).Bytes()) + ext.Difficulty = common.ToHex(b.Difficulty.Bytes()) + ext.TotalDifficulty = common.ToHex(b.TotalDifficulty.Bytes()) + ext.Size = common.ToHex(b.Size.Bytes()) // ext.ExtraData = common.ToHex(b.ExtraData) - ext.GasLimit = common.ToHex(big.NewInt(b.GasLimit).Bytes()) + ext.GasLimit = common.ToHex(b.GasLimit.Bytes()) // ext.MinGasPrice = common.ToHex(big.NewInt(b.MinGasPrice).Bytes()) - ext.GasUsed = common.ToHex(big.NewInt(b.GasUsed).Bytes()) + ext.GasUsed = common.ToHex(b.GasUsed.Bytes()) ext.UnixTimestamp = common.ToHex(big.NewInt(b.UnixTimestamp).Bytes()) ext.Transactions = make([]interface{}, len(b.Transactions)) if b.fullTx { @@ -99,7 +99,7 @@ func NewBlockRes(block *types.Block) *BlockRes { } res := new(BlockRes) - res.BlockNumber = block.Number().Int64() + res.BlockNumber = block.Number() res.BlockHash = block.Hash() res.ParentHash = block.ParentHash() res.Nonce = block.Header().Nonce @@ -108,15 +108,13 @@ func NewBlockRes(block *types.Block) *BlockRes { res.TransactionRoot = block.Header().TxHash res.StateRoot = block.Root() res.Miner = block.Header().Coinbase - res.Difficulty = block.Difficulty().Int64() - if block.Td != nil { - res.TotalDifficulty = block.Td.Int64() - } - res.Size = int64(block.Size()) + res.Difficulty = block.Difficulty() + res.TotalDifficulty = block.Td + res.Size = big.NewInt(int64(block.Size())) // res.ExtraData = - res.GasLimit = block.GasLimit().Int64() + res.GasLimit = block.GasLimit() // res.MinGasPrice = - res.GasUsed = block.GasUsed().Int64() + res.GasUsed = block.GasUsed() res.UnixTimestamp = block.Time() res.Transactions = make([]*TransactionRes, len(block.Transactions())) for i, tx := range block.Transactions() { @@ -135,47 +133,47 @@ func NewBlockRes(block *types.Block) *BlockRes { type TransactionRes struct { Hash common.Hash `json:"hash"` - Nonce int64 `json:"nonce"` + Nonce uint64 `json:"nonce"` BlockHash common.Hash `json:"blockHash,omitempty"` BlockNumber int64 `json:"blockNumber,omitempty"` TxIndex int64 `json:"transactionIndex,omitempty"` From common.Address `json:"from"` To *common.Address `json:"to"` - Value int64 `json:"value"` - Gas int64 `json:"gas"` - GasPrice int64 `json:"gasPrice"` + Value *big.Int `json:"value"` + Gas *big.Int `json:"gas"` + GasPrice *big.Int `json:"gasPrice"` Input []byte `json:"input"` } func (t *TransactionRes) MarshalJSON() ([]byte, error) { var ext struct { - Hash string `json:"hash"` - Nonce string `json:"nonce"` - BlockHash string `json:"blockHash,omitempty"` - BlockNumber string `json:"blockNumber,omitempty"` - TxIndex string `json:"transactionIndex,omitempty"` - From string `json:"from"` - To string `json:"to"` - Value string `json:"value"` - Gas string `json:"gas"` - GasPrice string `json:"gasPrice"` - Input string `json:"input"` + Hash string `json:"hash"` + Nonce string `json:"nonce"` + BlockHash string `json:"blockHash,omitempty"` + BlockNumber string `json:"blockNumber,omitempty"` + TxIndex string `json:"transactionIndex,omitempty"` + From string `json:"from"` + To interface{} `json:"to"` + Value string `json:"value"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + Input string `json:"input"` } ext.Hash = t.Hash.Hex() - ext.Nonce = common.ToHex(big.NewInt(t.Nonce).Bytes()) + ext.Nonce = common.ToHex(big.NewInt(int64(t.Nonce)).Bytes()) ext.BlockHash = t.BlockHash.Hex() ext.BlockNumber = common.ToHex(big.NewInt(t.BlockNumber).Bytes()) ext.TxIndex = common.ToHex(big.NewInt(t.TxIndex).Bytes()) ext.From = t.From.Hex() if t.To == nil { - ext.To = "0x00" + ext.To = nil } else { ext.To = t.To.Hex() } - ext.Value = common.ToHex(big.NewInt(t.Value).Bytes()) - ext.Gas = common.ToHex(big.NewInt(t.Gas).Bytes()) - ext.GasPrice = common.ToHex(big.NewInt(t.GasPrice).Bytes()) + ext.Value = common.ToHex(t.Value.Bytes()) + ext.Gas = common.ToHex(t.Gas.Bytes()) + ext.GasPrice = common.ToHex(t.GasPrice.Bytes()) ext.Input = common.ToHex(t.Input) return json.Marshal(ext) @@ -184,12 +182,12 @@ func (t *TransactionRes) MarshalJSON() ([]byte, error) { func NewTransactionRes(tx *types.Transaction) *TransactionRes { var v = new(TransactionRes) v.Hash = tx.Hash() - v.Nonce = int64(tx.Nonce()) + v.Nonce = tx.Nonce() v.From, _ = tx.From() v.To = tx.To() - v.Value = tx.Value().Int64() - v.Gas = tx.Gas().Int64() - v.GasPrice = tx.GasPrice().Int64() + v.Value = tx.Value() + v.Gas = tx.Gas() + v.GasPrice = tx.GasPrice() v.Input = tx.Data() return v } @@ -218,25 +216,48 @@ type FilterWhisperRes struct { } type LogRes struct { - Address string `json:"address"` - Topics []string `json:"topics"` - Data string `json:"data"` - Number uint64 `json:"number"` + Address common.Address `json:"address"` + Topics []common.Hash `json:"topics"` + Data []byte `json:"data"` + Number uint64 `json:"number"` +} + +func NewLogRes(log state.Log) LogRes { + var l LogRes + l.Topics = make([]common.Hash, len(log.Topics())) + l.Address = log.Address() + l.Data = log.Data() + l.Number = log.Number() + for j, topic := range log.Topics() { + l.Topics[j] = topic + } + return l +} + +func (l *LogRes) MarshalJSON() ([]byte, error) { + var ext struct { + Address string `json:"address"` + Topics []string `json:"topics"` + Data string `json:"data"` + Number string `json:"number"` + } + + ext.Address = l.Address.Hex() + ext.Data = common.Bytes2Hex(l.Data) + ext.Number = common.Bytes2Hex(big.NewInt(int64(l.Number)).Bytes()) + ext.Topics = make([]string, len(l.Topics)) + for i, v := range l.Topics { + ext.Topics[i] = v.Hex() + } + + return json.Marshal(ext) } func NewLogsRes(logs state.Logs) (ls []LogRes) { ls = make([]LogRes, len(logs)) for i, log := range logs { - var l LogRes - l.Topics = make([]string, len(log.Topics())) - l.Address = log.Address().Hex() - l.Data = common.ToHex(log.Data()) - l.Number = log.Number() - for j, topic := range log.Topics() { - l.Topics[j] = topic.Hex() - } - ls[i] = l + ls[i] = NewLogRes(log) } return diff --git a/rpc/responses_test.go b/rpc/responses_test.go new file mode 100644 index 000000000..278939830 --- /dev/null +++ b/rpc/responses_test.go @@ -0,0 +1,123 @@ +package rpc + +import ( + "encoding/json" + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" +) + +func TestNewBlockRes(t *testing.T) { + parentHash := common.HexToHash("0x01") + coinbase := common.HexToAddress("0x01") + root := common.HexToHash("0x01") + difficulty := common.Big1 + nonce := uint64(1) + extra := "" + block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) + + _ = NewBlockRes(block) +} + +func TestBlockRes(t *testing.T) { + v := &BlockRes{ + BlockNumber: big.NewInt(0), + BlockHash: common.HexToHash("0x0"), + ParentHash: common.HexToHash("0x0"), + Nonce: [8]byte{0, 0, 0, 0, 0, 0, 0, 0}, + Sha3Uncles: common.HexToHash("0x0"), + LogsBloom: types.BytesToBloom([]byte{0}), + TransactionRoot: common.HexToHash("0x0"), + StateRoot: common.HexToHash("0x0"), + Miner: common.HexToAddress("0x0"), + Difficulty: big.NewInt(0), + TotalDifficulty: big.NewInt(0), + Size: big.NewInt(0), + ExtraData: []byte{}, + GasLimit: big.NewInt(0), + MinGasPrice: int64(0), + GasUsed: big.NewInt(0), + UnixTimestamp: int64(0), + // Transactions []*TransactionRes `json:"transactions"` + // Uncles []common.Hash `json:"uncles"` + } + + _, _ = json.Marshal(v) + + // fmt.Println(string(j)) + +} + +func TestTransactionRes(t *testing.T) { + a := common.HexToAddress("0x0") + v := &TransactionRes{ + Hash: common.HexToHash("0x0"), + Nonce: uint64(0), + BlockHash: common.HexToHash("0x0"), + BlockNumber: int64(0), + TxIndex: int64(0), + From: common.HexToAddress("0x0"), + To: &a, + Value: big.NewInt(0), + Gas: big.NewInt(0), + GasPrice: big.NewInt(0), + Input: []byte{0}, + } + + _, _ = json.Marshal(v) +} + +func TestNewTransactionRes(t *testing.T) { + to := common.HexToAddress("0x02") + amount := big.NewInt(1) + gasAmount := big.NewInt(1) + gasPrice := big.NewInt(1) + data := []byte{1, 2, 3} + tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data) + + _ = NewTransactionRes(tx) +} + +func TestLogRes(t *testing.T) { + topics := make([]common.Hash, 3) + topics = append(topics, common.HexToHash("0x00")) + topics = append(topics, common.HexToHash("0x10")) + topics = append(topics, common.HexToHash("0x20")) + + v := &LogRes{ + Topics: topics, + Address: common.HexToAddress("0x0"), + Data: []byte{1, 2, 3}, + Number: uint64(5), + } + + _, _ = json.Marshal(v) +} + +func MakeStateLog(num int) state.Log { + address := common.HexToAddress("0x0") + data := []byte{1, 2, 3} + number := uint64(num) + topics := make([]common.Hash, 3) + topics = append(topics, common.HexToHash("0x00")) + topics = append(topics, common.HexToHash("0x10")) + topics = append(topics, common.HexToHash("0x20")) + log := state.NewLog(address, topics, data, number) + return log +} + +func TestNewLogRes(t *testing.T) { + log := MakeStateLog(0) + _ = NewLogRes(log) +} + +func TestNewLogsRes(t *testing.T) { + logs := make([]state.Log, 3) + logs[0] = MakeStateLog(1) + logs[1] = MakeStateLog(2) + logs[2] = MakeStateLog(3) + _ = NewLogsRes(logs) +} |