Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Aug 1, 2018
1 parent b5806fb commit 71b78a0
Show file tree
Hide file tree
Showing 39 changed files with 2,120 additions and 1,825 deletions.
21 changes: 14 additions & 7 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"os"

"github.com/dcb9/janus/pkg/qtum"
"github.com/dcb9/janus/pkg/server"
"github.com/dcb9/janus/pkg/transformer"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
Expand All @@ -28,15 +30,20 @@ func action(pc *kingpin.ParseContext) error {
logger = level.NewFilter(logger, level.AllowWarn())
}

s, err := server.New(
*qtumRPC,
addr,
server.SetLogger(logger),
server.SetDebug(*devMode),
)
qtumJSONRPC, err := qtum.NewClient(*qtumRPC, qtum.SetDebug(*devMode), qtum.SetLogger(logger))
if err != nil {
return errors.Wrap(err, "jsonrpc#New")
}
qtumClient := qtum.New(qtumJSONRPC)

t, err := transformer.New(qtumClient, transformer.DefaultProxies(qtumClient), transformer.SetDebug(*devMode), transformer.SetLogger(logger))
if err != nil {
return errors.Wrap(err, "transformer#New")
}

s, err := server.New(qtumClient, t, addr, server.SetLogger(logger), server.SetDebug(*devMode))
if err != nil {
return errors.Wrap(err, "new proxy")
return errors.Wrap(err, "server#New")
}

return s.Start()
Expand Down
191 changes: 28 additions & 163 deletions pkg/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,180 +2,45 @@ package eth

import (
"encoding/json"
"errors"
"math/big"
"strings"

"github.com/dcb9/janus/pkg/rpc"
"github.com/ethereum/go-ethereum/common/hexutil"
"fmt"
)

func NewJSONRPCResult(id, rawResult json.RawMessage, err *rpc.JSONRPCError) *rpc.JSONRPCResult {
return &rpc.JSONRPCResult{
JSONRPC: "2.0",
ID: id,
RawResult: rawResult,
Error: err,
}
}

// eth_sendTransaction
type TransactionReq struct {
From string `json:"from"`
To string `json:"to"`
Gas *EthInt `json:"gas"` // optional
GasPrice *EthInt `json:"gasPrice"` // optional
Value string `json:"value"` // optional
Data string `json:"data"` // optional
Nonce string `json:"nonce"` // optional
}

// see: https://ethereum.stackexchange.com/questions/8384/transfer-an-amount-between-two-ethereum-accounts-using-json-rpc
func (t *TransactionReq) IsSendEther() bool {
// data must be empty
return t.Value != "" && t.To != "" && t.From != "" && t.Data == ""
}

func (t *TransactionReq) IsCreateContract() bool {
return t.To == "" && t.Data != ""
}

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()
}

// eth_call
type TransactionCallReq struct {
From string `json:"from"`
To string `json:"to"`
Gas *EthInt `json:"gas"` // optional
GasPrice *EthInt `json:"gasPrice"` // optional
Value string `json:"value"` // optional
Data string `json:"data"` // optional
}

func (t *TransactionCallReq) GetGas() string {
return t.Gas.Hex()
}

func (t *TransactionCallReq) GetGasPrice() string {
return t.GasPrice.Hex()
}

type (
Log struct {
Removed string `json:"removed,omitempty"` // TAG - true when the log was removed, due to a chain reorganization. false if its a valid log.
LogIndex string `json:"logIndex"` // QUANTITY - integer of the log index position in the block. null when its pending log.
TransactionIndex string `json:"transactionIndex"` // QUANTITY - integer of the transactions index position log was created from. null when its pending log.
TransactionHash string `json:"transactionHash"` // DATA, 32 Bytes - hash of the transactions this log was created from. null when its pending log.
BlockHash string `json:"blockHash"` // DATA, 32 Bytes - hash of the block where this log was in. null when its pending. null when its pending log.
BlockNumber string `json:"blockNumber"` // QUANTITY - the block number where this log was in. null when its pending. null when its pending log.
Address string `json:"address"` // DATA, 20 Bytes - address from which this log originated.
Data string `json:"data"` // DATA - contains one or more 32 Bytes non-indexed arguments of the log.
Topics []string `json:"topics"` // Array of DATA - Array of 0 to 4 32 Bytes DATA of indexed log arguments.
Type string `json:"type,omitempty"`
}

TransactionReceipt struct {
TransactionHash string `json:"transactionHash"` // DATA, 32 Bytes - hash of the transaction.
TransactionIndex string `json:"transactionIndex"` // QUANTITY - integer of the transactions index position in the block.
BlockHash string `json:"blockHash"` // DATA, 32 Bytes - hash of the block where this transaction was in.
BlockNumber string `json:"blockNumber"` // QUANTITY - block number where this transaction was in.
From string `json:"from,omitempty"` // DATA, 20 Bytes - address of the sender.
To string `json:"to,omitempty"` // DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
CumulativeGasUsed string `json:"cumulativeGasUsed"` // QUANTITY - The total amount of gas used when this transaction was executed in the block.
GasUsed string `json:"gasUsed"` // QUANTITY - The amount of gas used by this specific transaction alone.
ContractAddress string `json:"contractAddress"` // DATA, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null.
Logs []Log `json:"logs"` // Array - Array of log objects, which this transaction generated.
LogsBloom string `json:"logsBloom"` // DATA, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs.
Root string `json:"root,omitempty"` // DATA 32 bytes of post-transaction stateroot (pre Byzantium)
Status string `json:"status"` // QUANTITY either 1 (success) or 0 (failure)
}

TransactionResponse struct {
Hash string `json:"hash"` // DATA, 32 Bytes - hash of the transaction.
Nonce string `json:"nonce"` // QUANTITY - the number of transactions made by the sender prior to this one.
BlockHash string `json:"blockHash"` // DATA, 32 Bytes - hash of the block where this transaction was in. null when its pending.
BlockNumber string `json:"blockNumber"` // QUANTITY - block number where this transaction was in. null when its pending.
TransactionIndex string `json:"transactionIndex"` // QUANTITY - integer of the transactions index position in the block. null when its pending.
From string `json:"from"` // DATA, 20 Bytes - address of the sender.
To string `json:"to"` // DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
Value string `json:"value"` // QUANTITY - value transferred in Wei.
GasPrice string `json:"gasPrice"` // QUANTITY - gas price provided by the sender in Wei.
Gas string `json:"gas"` // QUANTITY - gas provided by the sender.
Input string `json:"input"` // DATA - the data send along with the transaction.
}

GetLogsFilter struct {
FromBlock json.RawMessage `json:"fromBlock"`
ToBlock json.RawMessage `json:"toBlock"`
Address json.RawMessage `json:"address"` // string or []string
Topics []string `json:"topics"`
Blockhash string `json:"blockhash"`
}
const (
RPCVersion = "2.0"
)

// FIXME: ETHInt
type EthInt big.Int

func (i *EthInt) Hex() string {
return hexutil.EncodeBig(i.ToBigInt())
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
ID json.RawMessage `json:"id"`
Params json.RawMessage `json:"params"`
}

func (i *EthInt) ToBigInt() *big.Int {
v := *i
vv := big.Int(v)
return &vv
type JSONRPCResult struct {
JSONRPC string `json:"jsonrpc"`
RawResult json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
ID json.RawMessage `json:"id"`
}

func (i *EthInt) MarshalJSON() ([]byte, error) {
return json.Marshal(i.ToBigInt())
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}

// 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")
}

if data[0] != '"' && data[len(data)-1] != '"' {
var v *big.Int
if err = json.Unmarshal(data, &v); err != nil {
return err
}
vv := *v
*i = EthInt(vv)
return
}

// hex
var val string
if err = json.Unmarshal(data, &val); err != nil {
return err
}
if !strings.HasPrefix(val, "0x") {
val = "0x" + val
}
func (err *JSONRPCError) Error() string {
return fmt.Sprintf("eth [code: %d] %s", err.Code, err.Message)
}

v, err := hexutil.DecodeBig(val)
func NewJSONRPCResult(id json.RawMessage, res interface{}) (*JSONRPCResult, error) {
rawResult, err := json.Marshal(res)
if err != nil {
return err
return nil, err
}
vv := *v
*i = EthInt(vv)
return err

return &JSONRPCResult{
JSONRPC: RPCVersion,
ID: id,
RawResult: rawResult,
}, nil
}
63 changes: 63 additions & 0 deletions pkg/eth/eth_int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package eth

import (
"encoding/json"
"errors"
"math/big"

"github.com/dcb9/janus/pkg/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type ETHInt struct {
*big.Int
}

func (i *ETHInt) Hex() string {
return hexutil.EncodeBig(i.Int)
}

func (i *ETHInt) MarshalJSON() ([]byte, error) {
return json.Marshal(i.Int)
}

// 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")
}

isNumber := func(data []byte) bool {
return data[0] != '"' && data[len(data)-1] != '"'
}

if isNumber(data) {
i.Int, err = bigIntFromNumber(data)
return err
}

i.Int, err = bigIntFromHex(data)
return err
}

func bigIntFromNumber(data json.RawMessage) (*big.Int, error) {
var v *big.Int
if err := json.Unmarshal(data, &v); err != nil {
return nil, err
}
return v, nil
}

func bigIntFromHex(data json.RawMessage) (*big.Int, error) {
var val string

if err := json.Unmarshal(data, &val); err != nil {
return nil, err
}

i, err := utils.DecodeBig(val)
if err != nil {
return nil, err
}
return i, nil
}
Loading

0 comments on commit 71b78a0

Please sign in to comment.