Refactor RPC clients and interface
This commit is contained in:
parent
d1a1922ded
commit
fc170d05ac
@ -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,
|
||||
|
14
node/node.go
14
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
|
||||
}
|
||||
|
||||
|
35
rpc/ethereum/ethereum.go
Normal file
35
rpc/ethereum/ethereum.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
23
rpc/rpc.go
23
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)
|
||||
}
|
||||
|
@ -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 {
|
18
rpc/types/rpc_request_body.go
Normal file
18
rpc/types/rpc_request_body.go
Normal 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,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user