Refactor RPC clients and interface

This commit is contained in:
ChronosX88 2020-11-18 22:33:03 +04:00
parent d1a1922ded
commit fc170d05ac
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
12 changed files with 87 additions and 49 deletions

View File

@ -5,11 +5,11 @@ import (
"encoding/json" "encoding/json"
"sync" "sync"
solana2 "github.com/Secured-Finance/dione/rpc/solana"
"github.com/Secured-Finance/dione/beacon" "github.com/Secured-Finance/dione/beacon"
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter" oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
"github.com/Secured-Finance/dione/solana" solTypes "github.com/Secured-Finance/dione/rpc/solana/types"
solTypes "github.com/Secured-Finance/dione/solana/types"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/Secured-Finance/dione/ethclient" "github.com/Secured-Finance/dione/ethclient"
@ -27,7 +27,7 @@ type Miner struct {
mutex sync.Mutex mutex sync.Mutex
beacon beacon.BeaconNetworks beacon beacon.BeaconNetworks
ethClient *ethclient.EthereumClient ethClient *ethclient.EthereumClient
solanaClient *solana.SolanaClient solanaClient *solana2.SolanaClient
minerStake types.BigInt minerStake types.BigInt
networkStake types.BigInt networkStake types.BigInt
} }
@ -38,7 +38,7 @@ func NewMiner(
api WalletAPI, api WalletAPI,
beacon beacon.BeaconNetworks, beacon beacon.BeaconNetworks,
ethClient *ethclient.EthereumClient, ethClient *ethclient.EthereumClient,
solanaClient *solana.SolanaClient, solanaClient *solana2.SolanaClient,
) *Miner { ) *Miner {
return &Miner{ return &Miner{
address: address, address: address,

View File

@ -9,7 +9,10 @@ import (
"os" "os"
"time" "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/types"
"github.com/Secured-Finance/dione/wallet" "github.com/Secured-Finance/dione/wallet"
@ -24,7 +27,6 @@ import (
"github.com/Secured-Finance/dione/consensus" "github.com/Secured-Finance/dione/consensus"
"github.com/Secured-Finance/dione/ethclient" "github.com/Secured-Finance/dione/ethclient"
"github.com/Secured-Finance/dione/pb" "github.com/Secured-Finance/dione/pb"
"github.com/Secured-Finance/dione/rpc"
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-core/crypto" crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
@ -43,9 +45,9 @@ type Node struct {
GlobalCtxCancel context.CancelFunc GlobalCtxCancel context.CancelFunc
OracleTopic string OracleTopic string
Config *config.Config Config *config.Config
Lotus *rpc.LotusClient Lotus *filecoin.LotusClient
Ethereum *ethclient.EthereumClient Ethereum *ethclient.EthereumClient
Solana *solana.SolanaClient Solana *solana2.SolanaClient
ConsensusManager *consensus.PBFTConsensusManager ConsensusManager *consensus.PBFTConsensusManager
Miner *consensus.Miner Miner *consensus.Miner
Beacon beacon.BeaconNetworks Beacon beacon.BeaconNetworks
@ -141,12 +143,12 @@ func (n *Node) setupEthereumClient() error {
} }
func (n *Node) setupFilecoinClient() { 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 n.Lotus = lotus
} }
func (n *Node) setupSolanaClient() { func (n *Node) setupSolanaClient() {
solana := solana.NewSolanaClient() solana := solana2.NewSolanaClient()
n.Solana = solana n.Solana = solana
} }

35
rpc/ethereum/ethereum.go Normal file
View File

@ -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
}

View File

@ -1,9 +1,11 @@
package rpc package filecoin
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Secured-Finance/dione/rpc/types"
"github.com/Secured-Finance/dione/lib" "github.com/Secured-Finance/dione/lib"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -11,29 +13,29 @@ import (
var filecoinURL = "https://filecoin.infura.io/" var filecoinURL = "https://filecoin.infura.io/"
// client implements the `Client` interface.
type LotusClient struct { type LotusClient struct {
host string host string
projectID string projectID string
projectSecret string projectSecret string
httpClient *fasthttp.Client
} }
// NewClient returns a new client.
func NewLotusClient(pID, secret string) *LotusClient { func NewLotusClient(pID, secret string) *LotusClient {
return &LotusClient{ return &LotusClient{
host: filecoinURL, host: filecoinURL,
projectID: pID, projectID: pID,
projectSecret: secret, 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 := fasthttp.AcquireRequest()
req.SetRequestURI(c.host) req.SetRequestURI(c.host)
req.Header.SetMethod("POST") req.Header.SetMethod("POST")
req.Header.SetContentType("application/json") req.Header.SetContentType("application/json")
req.Header.Set("Authorization", "Basic "+lib.BasicAuth(c.projectID, c.projectSecret)) 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) requestBody.Params = append(requestBody.Params, txHash)
body, err := json.Marshal(requestBody) body, err := json.Marshal(requestBody)
if err != nil { if err != nil {
@ -41,12 +43,11 @@ func (c *LotusClient) GetMessage(txHash string) (*fasthttp.Response, error) {
} }
req.AppendBody(body) req.AppendBody(body)
resp := fasthttp.AcquireResponse() resp := fasthttp.AcquireResponse()
client := &fasthttp.Client{} if err = c.httpClient.Do(req, resp); err != nil {
if err = client.Do(req, resp); err != nil {
logrus.Warn("Failed to construct filecoin node rpc request", err) logrus.Warn("Failed to construct filecoin node rpc request", err)
return nil, err return nil, err
} }
bodyBytes := resp.Body() bodyBytes := resp.Body()
logrus.Info(string(bodyBytes)) logrus.Debug(string(bodyBytes))
return resp, nil return bodyBytes, nil
} }

View File

@ -1,24 +1,5 @@
package rpc package rpc
import "net/http" type RPCClient interface {
GetTransaction(txHash string) ([]byte, error)
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)
} }

View File

@ -5,8 +5,9 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/Secured-Finance/dione/rpc" "github.com/Secured-Finance/dione/rpc/types"
"github.com/Secured-Finance/dione/solana/types"
stypes "github.com/Secured-Finance/dione/rpc/solana/types"
ws "github.com/dgrr/fastws" ws "github.com/dgrr/fastws"
"github.com/shengdoushi/base58" "github.com/shengdoushi/base58"
"github.com/sirupsen/logrus" "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 := fasthttp.AcquireRequest()
req.SetRequestURI(c.url) req.SetRequestURI(c.url)
req.Header.SetMethod("POST") req.Header.SetMethod("POST")
req.Header.SetContentType("application/json") req.Header.SetContentType("application/json")
requestBody := rpc.NewRequestBody("getConfirmedTransaction") requestBody := types.NewRPCRequestBody("getConfirmedTransaction")
requestBody.Params = append(requestBody.Params, txHash, "json") requestBody.Params = append(requestBody.Params, txHash, "json")
body, err := json.Marshal(requestBody) body, err := json.Marshal(requestBody)
if err != nil { if err != nil {
@ -58,17 +59,17 @@ func (c *SolanaClient) GetTransaction(txHash string) (*fasthttp.Response, error)
} }
bodyBytes := resp.Body() bodyBytes := resp.Body()
logrus.Info(string(bodyBytes)) 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) conn, err := ws.Dial(c.ws)
if err != nil { if err != nil {
log.Fatalln("Can't establish connection with Solana websocket: ", err) log.Fatalln("Can't establish connection with Solana websocket: ", err)
} }
defer conn.Close() defer conn.Close()
requestBody := rpc.NewRequestBody("programSubscribe") requestBody := types.NewRPCRequestBody("programSubscribe")
requestBody.Params = append(requestBody.Params, programID) requestBody.Params = append(requestBody.Params, programID)
p := NewSubParam("jsonParsed") p := NewSubParam("jsonParsed")
requestBody.Params = append(requestBody.Params, p) 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) logrus.Info("Subscription ID to drop websocket connection:", subscriptionID)
var msg []byte var msg []byte
var parsedSub *types.Subscription var parsedSub *stypes.Subscription
for { for {
_, msg, err = conn.ReadMessage(msg[:0]) _, msg, err = conn.ReadMessage(msg[:0])
if err != nil { if err != nil {

View File

@ -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,
}
}