Skip to content

Commit

Permalink
fixmes
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed Jul 30, 2018
1 parent caefd29 commit b5806fb
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 29 deletions.
8 changes: 8 additions & 0 deletions pkg/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ func (t *TransactionReq) IsCallContract() bool {
return t.To != "" && t.Data != ""
}

// FIXME: GetGas -> GasHex
func (t *TransactionReq) GetGas() string {
return t.Gas.Hex()
}

// FIXME: GetGasPrice -> GasPriceHex
func (t *TransactionReq) GetGasPrice() string {
return t.GasPrice.Hex()
}
Expand Down Expand Up @@ -123,6 +125,7 @@ type (
}
)

// FIXME: ETHInt
type EthInt big.Int

func (i *EthInt) Hex() string {
Expand All @@ -139,6 +142,11 @@ func (i *EthInt) MarshalJSON() ([]byte, error) {
return json.Marshal(i.ToBigInt())
}

// FIXME: extract parsers into constructors:
// ETHIntFromNumber
// ETHIntFromIntger

// UnmarshalJSON needs to be able to parse ETHInt from both hex string or number
func (i *EthInt) UnmarshalJSON(data []byte) (err error) {
if len(data) == 0 {
return errors.New("data must not be empty")
Expand Down
16 changes: 16 additions & 0 deletions pkg/qtum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import (
"github.com/pkg/errors"
)

// FIXME: Abstract all API calls into a RPC call helper. See:
// https://github.com/hayeah/eostools/blob/8e1d0d48c74b7ca6a74b132d619a4e7f8673d26a/eos-actions/main.go#L16

// FIXME: We can probably remove all methods from Client, and only provide a single `Request` method

// FIXME: Define all RPC types in rpcTypes.go

// FIXME: rename Client -> RPC or RPCClient
type Client struct {
rpcURL string
doer doer
Expand Down Expand Up @@ -68,6 +76,7 @@ func SetLogger(l log.Logger) func(*Client) error {
}
}

// FIXME: GetHexAddress -> Base58AddressToHex
func (c *Client) GetHexAddress(addr string) (string, error) {
r := c.NewRPCRequest(MethodGethexaddress)
r.Params = json.RawMessage(fmt.Sprintf(`["%s"]`, addr))
Expand Down Expand Up @@ -102,6 +111,12 @@ func (c *Client) FromHexAddress(addr string) (string, error) {
return qtumAddr, nil
}

// FIXME: Define the types for all API methods: [methodName]Request, [methodName]Response
//
// type GetTransactionReceiptResponse []struct {
// // A int `json:"a"`
// }

func (c *Client) GetTransactionReceipt(txHash string) (*TransactionReceipt, error) {
r := c.NewRPCRequest(MethodGettransactionreceipt)
r.Params = json.RawMessage(fmt.Sprintf(`["%s"]`, txHash))
Expand All @@ -127,6 +142,7 @@ func (c *Client) GetTransactionReceipt(txHash string) (*TransactionReceipt, erro

return receipt, nil
}

func (c *Client) DecodeRawTransaction(hex string) (*DecodedRawTransaction, error) {
r := c.NewRPCRequest(MethodDecoderawtransaction)
r.Params = json.RawMessage(fmt.Sprintf(`["%s"]`, hex))
Expand Down
26 changes: 23 additions & 3 deletions pkg/qtum/qtum.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package qtum

import (
"errors"
"fmt"
"math/big"
"strings"

"github.com/pkg/errors"
)

const (
Version = "1.0"
)

const (
// FIXME: camel case. MethodGethexaddress -> MethodGetHexAddress
MethodGethexaddress = "gethexaddress"
MethodFromhexaddress = "fromhexaddress"
MethodSendtocontract = "sendtocontract"
Expand All @@ -27,7 +29,16 @@ const (
MethodWaitforlogs = "waitforlogs"
)

// FIXME: rename TransactionReceipt -> GetTransactionReceiptResponse
// FIXME: rename all other qtum RPC types
// FIXME: move all RPC types into its own file.
// FIXME: (optional) add RPC examples to each struct
type (
/* example:
{
"blockhash": "afafafa..."
}
*/
TransactionReceipt struct {
BlockHash string `json:"blockHash"`
BlockNumber uint64 `json:"blockNumber"`
Expand Down Expand Up @@ -108,6 +119,9 @@ type (
Hex string `json:"hex"`
}

// FIXME: extract asm to its own file. btcasm.go

// ASM is Bitcoin Script extended by qtum to support smart contracts
ASM struct {
VMVersion string
GasLimit string
Expand All @@ -116,12 +130,13 @@ type (
}
CallASM struct {
ASM
// FIXME: EncodedABI -> CallData
EncodedABI string
ContractAddress string
}

CreateASM struct {
ASM
// FIXME: EncodedABI -> CallData
EncodedABI string
}

Expand Down Expand Up @@ -162,6 +177,7 @@ type (
func ParseCallASM(asm string) (*CallASM, error) {
parts := strings.Split(asm, " ")
if len(parts) < 6 {
// FIXME: typo: sam -> ASM
return nil, errors.New("invalid call sam")
}

Expand All @@ -180,6 +196,7 @@ func ParseCallASM(asm string) (*CallASM, error) {
func ParseCreateASM(asm string) (*CreateASM, error) {
parts := strings.Split(asm, " ")
if len(parts) < 5 {
// FIXME: typo: sam -> ASM
return nil, errors.New("invalid create sam")
}

Expand All @@ -194,10 +211,12 @@ func ParseCreateASM(asm string) (*CreateASM, error) {
}, nil
}

// FIXME: rename GetGasPrice -> GasPrice
func (asm *ASM) GetGasPrice() (*big.Int, error) {
return stringToBigInt(asm.GasPrice)
}

// FIXME: rename GetGasLimit -> GasLimit
func (asm *ASM) GetGasLimit() (*big.Int, error) {
return stringToBigInt(asm.GasLimit)
}
Expand All @@ -214,7 +233,8 @@ func stringToBigInt(str string) (*big.Int, error) {
var success bool
v := new(big.Int)
if v, success = v.SetString(str, 10); !success {
return nil, errors.New(fmt.Sprintf("failed to parse str: %s to big.Int", str))
// FIXME: use errors.Errorf
return nil, errors.New(fmt.Sprintf("Failed to parse big.Int: %s", str))
}
return v, nil
}
6 changes: 6 additions & 0 deletions pkg/rpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (
"fmt"
)

// FIXME: move to qtum package
const (
ErrInvalid = 150
ErrUnknownOperation = 151
)

// FIXME: rename package to jsonrpc
// FIXME: remove JSONRPC prefix for JSONRPCRequest, JSONRPCResult
// FIXME: this package seems kinda pointless

type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Expand All @@ -30,6 +35,7 @@ type SuccessJSONRPCResult struct {
ID json.RawMessage `json:"id"`
}

// FIXME: move this to qtum package
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import (
"github.com/pkg/errors"
)

// FIXME: refactor this method. move the `request -> transform -> response` to transformer package
func httpHandler(c *myCtx) (interface{}, error) {
rpcReq := c.rpcReq

// FIXME: move to transformer
switch rpcReq.Method {
case "personal_unlockAccount":
return true, nil
}

// FIXME:
// proxyRes, err = TransformRequest.Proxy(rpcReq)
// output proxyRes as JSON. done.

level.Info(c.logger).Log("msg", "before transform request", "method", rpcReq.Method, "params", []byte(rpcReq.Params))
responseTransformer, err := c.server.transformerManager.TransformRequest(rpcReq)

Expand Down
4 changes: 4 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Server struct {
echo *echo.Echo
}

// FIXME: define a StartServer/main function to do dependency injection. Constructor should be simple.

func New(qtumRPC string, addr string, opts ...Option) (*Server, error) {
opts = append(opts, setQtumRPC(qtumRPC), setAddress(addr))

Expand All @@ -35,6 +37,8 @@ func New(qtumRPC string, addr string, opts ...Option) (*Server, error) {
}
}

// FIXME: dependency injection: pass client an transformer into constructor as parameters

p.qtumClient, err = qtum.NewClient(
qtumRPC,
qtum.SetLogger(p.logger),
Expand Down
84 changes: 58 additions & 26 deletions pkg/transformer/callcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@ import (
"github.com/dcb9/janus/pkg/rpc"
)

// method: eth_call
//
// eth request -> qtum request
// call qtum rpc
// qtum reponse -> eth response

func (m *Manager) proxyRPC(req *rpc.JSONRPCRequest) (interface{}, error) {
switch req.Method {
case "eth_call":
var req eth.CallRequest
err := unmarshalRequest(req.Params, &req)
if err != nil {
return nil, err
}
return m.proxyETHCall(req)
default:
return errors.New("unsupported method")
}
}

type Proxy interface {
request(ethreq interface{}) (interface{}, error)
}

// FIXME: rename file to eth_call.go
// proxy eth_call

type ProxyETHCall struct{}

func (p *ProxyETHCall) request(rawreq *rpc.JSONRPCRequest) (interface{}, error) {
var req eth.CallRequest
err := unmarshalRequest(rawreq.Params, &req)
if err != nil {
return nil, err
}
return p.proxyInternal(req)
}

func (p *ProxyETHCall) requestInternal(ethreq *eth.CallRequest) (*eth.CallResponse, error) {
// eth req -> qtum req
qtumreq, err = p.ToRequest(ethreq)

var qtumres qtum.CallContract
err = p.rpc.Request(qtumreq, &qtumres)

// qtum res -> eth res
ethres, err = p.ToResponse(ethreq)

return ethres, err
}

func (p *ProxyETHCall) ToRequest(ethreq *eth.CallRequest) (*qtum.CallContract, error) {
}

func (p *ProxyETHCall) ToResponse(res *qtum.CallResponse) (*eth.CallResponse, error) {
}

func (m *Manager) Call(req *rpc.JSONRPCRequest) (ResponseTransformerFunc, error) {
var params []json.RawMessage
if err := unmarshalRequest(req.Params, &params); err != nil {
Expand All @@ -19,36 +76,11 @@ func (m *Manager) Call(req *rpc.JSONRPCRequest) (ResponseTransformerFunc, error)
return nil, errors.New("params must be set")
}

// FIXME: rename to eth.CallRequest
var tx eth.TransactionCallReq
if err := unmarshalRequest(params[0], &tx); err != nil {
return nil, err
}
gasLimit, _, err := EthGasToQtum(&tx)
if err != nil {
return nil, err
}

from := tx.From

if IsEthHexAddress(from) {
from, err = m.qtumClient.FromHexAddress(RemoveHexPrefix(from))
if err != nil {
return nil, err
}
}

newParams, err := json.Marshal([]interface{}{
RemoveHexPrefix(tx.To),
RemoveHexPrefix(tx.Data),
from,
gasLimit,
})
if err != nil {
return nil, err
}

req.Params = newParams
req.Method = qtum.MethodCallcontract

//Qtum RPC
// callcontract "address" "data" ( address )
Expand Down

0 comments on commit b5806fb

Please sign in to comment.