From fc170d05ac62f98c63b492bb03d9f086c920ebbd Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 18 Nov 2020 22:33:03 +0400 Subject: [PATCH] Refactor RPC clients and interface --- consensus/miner.go | 10 +++--- node/node.go | 14 ++++---- rpc/ethereum/ethereum.go | 35 ++++++++++++++++++++ rpc/{ => filecoin}/filecoin.go | 19 ++++++----- rpc/rpc.go | 23 ++----------- solana/client.go => rpc/solana/solana.go | 17 +++++----- {solana => rpc/solana}/types/error.go | 0 {solana => rpc/solana}/types/program.go | 0 {solana => rpc/solana}/types/response.go | 0 {solana => rpc/solana}/types/subscription.go | 0 {solana => rpc/solana}/types/transaction.go | 0 rpc/types/rpc_request_body.go | 18 ++++++++++ 12 files changed, 87 insertions(+), 49 deletions(-) create mode 100644 rpc/ethereum/ethereum.go rename rpc/{ => filecoin}/filecoin.go (73%) rename solana/client.go => rpc/solana/solana.go (84%) rename {solana => rpc/solana}/types/error.go (100%) rename {solana => rpc/solana}/types/program.go (100%) rename {solana => rpc/solana}/types/response.go (100%) rename {solana => rpc/solana}/types/subscription.go (100%) rename {solana => rpc/solana}/types/transaction.go (100%) create mode 100644 rpc/types/rpc_request_body.go diff --git a/consensus/miner.go b/consensus/miner.go index 3326c46..8198b1d 100644 --- a/consensus/miner.go +++ b/consensus/miner.go @@ -5,11 +5,11 @@ import ( "encoding/json" "sync" + solana2 "github.com/Secured-Finance/dione/rpc/solana" + "github.com/Secured-Finance/dione/beacon" oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter" - "github.com/Secured-Finance/dione/solana" - solTypes "github.com/Secured-Finance/dione/solana/types" - + solTypes "github.com/Secured-Finance/dione/rpc/solana/types" "github.com/libp2p/go-libp2p-core/peer" "github.com/Secured-Finance/dione/ethclient" @@ -27,7 +27,7 @@ type Miner struct { mutex sync.Mutex beacon beacon.BeaconNetworks ethClient *ethclient.EthereumClient - solanaClient *solana.SolanaClient + solanaClient *solana2.SolanaClient minerStake types.BigInt networkStake types.BigInt } @@ -38,7 +38,7 @@ func NewMiner( api WalletAPI, beacon beacon.BeaconNetworks, ethClient *ethclient.EthereumClient, - solanaClient *solana.SolanaClient, + solanaClient *solana2.SolanaClient, ) *Miner { return &Miner{ address: address, diff --git a/node/node.go b/node/node.go index 616f946..f9738f4 100644 --- a/node/node.go +++ b/node/node.go @@ -9,7 +9,10 @@ import ( "os" "time" - "github.com/Secured-Finance/dione/solana" + solana2 "github.com/Secured-Finance/dione/rpc/solana" + + "github.com/Secured-Finance/dione/rpc/filecoin" + "github.com/Secured-Finance/dione/types" "github.com/Secured-Finance/dione/wallet" @@ -24,7 +27,6 @@ import ( "github.com/Secured-Finance/dione/consensus" "github.com/Secured-Finance/dione/ethclient" "github.com/Secured-Finance/dione/pb" - "github.com/Secured-Finance/dione/rpc" "github.com/libp2p/go-libp2p" crypto "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/host" @@ -43,9 +45,9 @@ type Node struct { GlobalCtxCancel context.CancelFunc OracleTopic string Config *config.Config - Lotus *rpc.LotusClient + Lotus *filecoin.LotusClient Ethereum *ethclient.EthereumClient - Solana *solana.SolanaClient + Solana *solana2.SolanaClient ConsensusManager *consensus.PBFTConsensusManager Miner *consensus.Miner Beacon beacon.BeaconNetworks @@ -141,12 +143,12 @@ func (n *Node) setupEthereumClient() error { } func (n *Node) setupFilecoinClient() { - lotus := rpc.NewLotusClient(n.Config.Filecoin.LotusHost, n.Config.Filecoin.LotusToken) + lotus := filecoin.NewLotusClient(n.Config.Filecoin.LotusHost, n.Config.Filecoin.LotusToken) n.Lotus = lotus } func (n *Node) setupSolanaClient() { - solana := solana.NewSolanaClient() + solana := solana2.NewSolanaClient() n.Solana = solana } diff --git a/rpc/ethereum/ethereum.go b/rpc/ethereum/ethereum.go new file mode 100644 index 0000000..399bb9f --- /dev/null +++ b/rpc/ethereum/ethereum.go @@ -0,0 +1,35 @@ +package ethereum + +import ( + "context" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" +) + +type EthereumRPCClient struct { + client *ethclient.Client +} + +func NewEthereumRPCClient(url string) (*EthereumRPCClient, error) { + client, err := ethclient.Dial(url) + if err != nil { + return nil, err + } + return &EthereumRPCClient{ + client: client, + }, nil +} + +func (erc *EthereumRPCClient) GetTransaction(txHash string) ([]byte, error) { + txHHash := common.HexToHash(txHash) + tx, _, err := erc.client.TransactionByHash(context.TODO(), txHHash) + if err != nil { + return nil, err + } + txRaw, err := tx.MarshalJSON() + if err != nil { + return nil, err + } + return txRaw, nil +} diff --git a/rpc/filecoin.go b/rpc/filecoin/filecoin.go similarity index 73% rename from rpc/filecoin.go rename to rpc/filecoin/filecoin.go index 49da63e..833a80d 100644 --- a/rpc/filecoin.go +++ b/rpc/filecoin/filecoin.go @@ -1,9 +1,11 @@ -package rpc +package filecoin import ( "encoding/json" "fmt" + "github.com/Secured-Finance/dione/rpc/types" + "github.com/Secured-Finance/dione/lib" "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" @@ -11,29 +13,29 @@ import ( var filecoinURL = "https://filecoin.infura.io/" -// client implements the `Client` interface. type LotusClient struct { host string projectID string projectSecret string + httpClient *fasthttp.Client } -// NewClient returns a new client. func NewLotusClient(pID, secret string) *LotusClient { return &LotusClient{ host: filecoinURL, projectID: pID, projectSecret: secret, + httpClient: &fasthttp.Client{}, } } -func (c *LotusClient) GetMessage(txHash string) (*fasthttp.Response, error) { +func (c *LotusClient) GetTransaction(txHash string) ([]byte, error) { req := fasthttp.AcquireRequest() req.SetRequestURI(c.host) req.Header.SetMethod("POST") req.Header.SetContentType("application/json") req.Header.Set("Authorization", "Basic "+lib.BasicAuth(c.projectID, c.projectSecret)) - requestBody := NewRequestBody("Filecoin.ChainGetMessage") + requestBody := types.NewRPCRequestBody("Filecoin.ChainGetMessage") requestBody.Params = append(requestBody.Params, txHash) body, err := json.Marshal(requestBody) if err != nil { @@ -41,12 +43,11 @@ func (c *LotusClient) GetMessage(txHash string) (*fasthttp.Response, error) { } req.AppendBody(body) resp := fasthttp.AcquireResponse() - client := &fasthttp.Client{} - if err = client.Do(req, resp); err != nil { + if err = c.httpClient.Do(req, resp); err != nil { logrus.Warn("Failed to construct filecoin node rpc request", err) return nil, err } bodyBytes := resp.Body() - logrus.Info(string(bodyBytes)) - return resp, nil + logrus.Debug(string(bodyBytes)) + return bodyBytes, nil } diff --git a/rpc/rpc.go b/rpc/rpc.go index ff273b0..717b7b4 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -1,24 +1,5 @@ package rpc -import "net/http" - -type RequestBody struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params []interface{} `json:"params"` - ID int `json:"id"` -} - -func NewRequestBody(method string) *RequestBody { - var i []interface{} - return &RequestBody{ - Jsonrpc: "2.0", - Method: method, - Params: i, - ID: 0, - } -} - -type Client interface { - HandleRequest(r *http.Request, data []byte) (*http.Response, error) +type RPCClient interface { + GetTransaction(txHash string) ([]byte, error) } diff --git a/solana/client.go b/rpc/solana/solana.go similarity index 84% rename from solana/client.go rename to rpc/solana/solana.go index 0c44022..cf9b0c1 100644 --- a/solana/client.go +++ b/rpc/solana/solana.go @@ -5,8 +5,9 @@ import ( "fmt" "log" - "github.com/Secured-Finance/dione/rpc" - "github.com/Secured-Finance/dione/solana/types" + "github.com/Secured-Finance/dione/rpc/types" + + stypes "github.com/Secured-Finance/dione/rpc/solana/types" ws "github.com/dgrr/fastws" "github.com/shengdoushi/base58" "github.com/sirupsen/logrus" @@ -38,12 +39,12 @@ func NewSolanaClient() *SolanaClient { } } -func (c *SolanaClient) GetTransaction(txHash string) (*fasthttp.Response, error) { +func (c *SolanaClient) GetTransaction(txHash string) ([]byte, error) { req := fasthttp.AcquireRequest() req.SetRequestURI(c.url) req.Header.SetMethod("POST") req.Header.SetContentType("application/json") - requestBody := rpc.NewRequestBody("getConfirmedTransaction") + requestBody := types.NewRPCRequestBody("getConfirmedTransaction") requestBody.Params = append(requestBody.Params, txHash, "json") body, err := json.Marshal(requestBody) if err != nil { @@ -58,17 +59,17 @@ func (c *SolanaClient) GetTransaction(txHash string) (*fasthttp.Response, error) } bodyBytes := resp.Body() logrus.Info(string(bodyBytes)) - return resp, nil + return bodyBytes, nil } -func (c *SolanaClient) subsctibeOnProgram(programID string) { +func (c *SolanaClient) subscribeOnProgram(programID string) { conn, err := ws.Dial(c.ws) if err != nil { log.Fatalln("Can't establish connection with Solana websocket: ", err) } defer conn.Close() - requestBody := rpc.NewRequestBody("programSubscribe") + requestBody := types.NewRPCRequestBody("programSubscribe") requestBody.Params = append(requestBody.Params, programID) p := NewSubParam("jsonParsed") requestBody.Params = append(requestBody.Params, p) @@ -86,7 +87,7 @@ func (c *SolanaClient) subsctibeOnProgram(programID string) { logrus.Info("Subscription ID to drop websocket connection:", subscriptionID) var msg []byte - var parsedSub *types.Subscription + var parsedSub *stypes.Subscription for { _, msg, err = conn.ReadMessage(msg[:0]) if err != nil { diff --git a/solana/types/error.go b/rpc/solana/types/error.go similarity index 100% rename from solana/types/error.go rename to rpc/solana/types/error.go diff --git a/solana/types/program.go b/rpc/solana/types/program.go similarity index 100% rename from solana/types/program.go rename to rpc/solana/types/program.go diff --git a/solana/types/response.go b/rpc/solana/types/response.go similarity index 100% rename from solana/types/response.go rename to rpc/solana/types/response.go diff --git a/solana/types/subscription.go b/rpc/solana/types/subscription.go similarity index 100% rename from solana/types/subscription.go rename to rpc/solana/types/subscription.go diff --git a/solana/types/transaction.go b/rpc/solana/types/transaction.go similarity index 100% rename from solana/types/transaction.go rename to rpc/solana/types/transaction.go diff --git a/rpc/types/rpc_request_body.go b/rpc/types/rpc_request_body.go new file mode 100644 index 0000000..5fd06f6 --- /dev/null +++ b/rpc/types/rpc_request_body.go @@ -0,0 +1,18 @@ +package types + +type RPCRequestBody struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` + ID int `json:"id"` +} + +func NewRPCRequestBody(method string) *RPCRequestBody { + var i []interface{} + return &RPCRequestBody{ + Jsonrpc: "2.0", + Method: method, + Params: i, + ID: 0, + } +}