Implement dynamic RPC client method managing
This commit is contained in:
parent
52ec8dbd35
commit
b5893b03f4
@ -2,16 +2,12 @@ package consensus
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
fil "github.com/Secured-Finance/dione/rpc/filecoin"
|
"github.com/Secured-Finance/dione/rpc"
|
||||||
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"
|
||||||
solTypes "github.com/Secured-Finance/dione/rpc/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"
|
||||||
@ -23,16 +19,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Miner struct {
|
type Miner struct {
|
||||||
address peer.ID
|
address peer.ID
|
||||||
ethAddress common.Address
|
ethAddress common.Address
|
||||||
api WalletAPI
|
api WalletAPI
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
beacon beacon.BeaconNetworks
|
beacon beacon.BeaconNetworks
|
||||||
ethClient *ethclient.EthereumClient
|
ethClient *ethclient.EthereumClient
|
||||||
filecoinClient *fil.LotusClient
|
minerStake types.BigInt
|
||||||
solanaClient *solana2.SolanaClient
|
networkStake types.BigInt
|
||||||
minerStake types.BigInt
|
|
||||||
networkStake types.BigInt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMiner(
|
func NewMiner(
|
||||||
@ -41,17 +35,13 @@ func NewMiner(
|
|||||||
api WalletAPI,
|
api WalletAPI,
|
||||||
beacon beacon.BeaconNetworks,
|
beacon beacon.BeaconNetworks,
|
||||||
ethClient *ethclient.EthereumClient,
|
ethClient *ethclient.EthereumClient,
|
||||||
filecoinClient *fil.LotusClient,
|
|
||||||
solanaClient *solana2.SolanaClient,
|
|
||||||
) *Miner {
|
) *Miner {
|
||||||
return &Miner{
|
return &Miner{
|
||||||
address: address,
|
address: address,
|
||||||
ethAddress: ethAddress,
|
ethAddress: ethAddress,
|
||||||
api: api,
|
api: api,
|
||||||
beacon: beacon,
|
beacon: beacon,
|
||||||
ethClient: ethClient,
|
ethClient: ethClient,
|
||||||
filecoinClient: filecoinClient,
|
|
||||||
solanaClient: solanaClient,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,28 +97,25 @@ func (m *Miner) MineTask(ctx context.Context, event *oracleEmitter.OracleEmitter
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// res, err := m.solanaClient.GetTransaction(event.RequestParams)
|
rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType)
|
||||||
res, err := m.EventRequestType(event.RequestType, event.RequestParams)
|
if rpcMethod == nil {
|
||||||
|
return nil, xerrors.Errorf("invalid rpc method name/type")
|
||||||
|
}
|
||||||
|
res, err := rpcMethod(event.RequestParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("Couldn't get solana request: %w", err)
|
return nil, xerrors.Errorf("couldn't do rpc request: %w", err)
|
||||||
}
|
}
|
||||||
var txRes solTypes.TxResponse
|
bres := []byte(res)
|
||||||
if err = json.Unmarshal(res, &txRes); err != nil {
|
signature, err := sign(ctx, m.address, bres)
|
||||||
return nil, xerrors.Errorf("Couldn't unmarshal solana response: %w", err)
|
|
||||||
}
|
|
||||||
blockHash := txRes.Result.Transaction.Message.RecentBlockhash
|
|
||||||
signature, err := sign(ctx, m.address, res)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("Couldn't sign solana response: %w", err)
|
return nil, xerrors.Errorf("Couldn't sign solana response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.DioneTask{
|
return &types.DioneTask{
|
||||||
Miner: m.address,
|
|
||||||
Ticket: ticket,
|
Ticket: ticket,
|
||||||
ElectionProof: winner,
|
ElectionProof: winner,
|
||||||
BeaconEntries: bvals,
|
BeaconEntries: bvals,
|
||||||
Payload: res,
|
Payload: bres,
|
||||||
BlockHash: blockHash,
|
|
||||||
Signature: signature,
|
Signature: signature,
|
||||||
DrandRound: types.DrandRound(rbase.Round),
|
DrandRound: types.DrandRound(rbase.Round),
|
||||||
}, nil
|
}, nil
|
||||||
@ -156,24 +143,3 @@ func (m *Miner) computeTicket(ctx context.Context, brand *types.BeaconEntry) (*t
|
|||||||
VRFProof: vrfOut,
|
VRFProof: vrfOut,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Miner) EventRequestType(rType uint8, params string) ([]byte, error) {
|
|
||||||
switch rType {
|
|
||||||
case 1:
|
|
||||||
return m.filecoinClient.GetTransaction(params)
|
|
||||||
case 2:
|
|
||||||
return m.filecoinClient.GetBlock(params)
|
|
||||||
case 3:
|
|
||||||
i, err := strconv.ParseInt(params, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, xerrors.Errorf("Couldn't parse int from string request params: %w", err)
|
|
||||||
}
|
|
||||||
return m.filecoinClient.GetTipSetByHeight(i)
|
|
||||||
case 4:
|
|
||||||
return m.filecoinClient.GetChainHead()
|
|
||||||
case 5:
|
|
||||||
return m.filecoinClient.GetNodeVersion()
|
|
||||||
default:
|
|
||||||
return m.filecoinClient.GetTransaction(params)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -27,7 +27,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// OracleEmitterABI is the input ABI used to generate the binding from.
|
// OracleEmitterABI is the input ABI used to generate the binding from.
|
||||||
const OracleEmitterABI = "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"originChain\",\"type\":\"uint8\"},{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"requestType\",\"type\":\"uint8\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"requestParams\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"callbackAddress\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bytes4\",\"name\":\"callbackMethodID\",\"type\":\"bytes4\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"requestID\",\"type\":\"uint256\"}],\"name\":\"NewOracleRequest\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint8\",\"name\":\"originChain\",\"type\":\"uint8\"},{\"internalType\":\"uint8\",\"name\":\"requestType\",\"type\":\"uint8\"},{\"internalType\":\"string\",\"name\":\"requestParams\",\"type\":\"string\"},{\"internalType\":\"address\",\"name\":\"callbackAddress\",\"type\":\"address\"},{\"internalType\":\"bytes4\",\"name\":\"callbackMethodID\",\"type\":\"bytes4\"}],\"name\":\"requestOracles\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
|
const OracleEmitterABI = "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"originChain\",\"type\":\"uint8\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"requestType\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"requestParams\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"callbackAddress\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bytes4\",\"name\":\"callbackMethodID\",\"type\":\"bytes4\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"requestID\",\"type\":\"uint256\"}],\"name\":\"NewOracleRequest\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint8\",\"name\":\"originChain\",\"type\":\"uint8\"},{\"internalType\":\"string\",\"name\":\"requestType\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"requestParams\",\"type\":\"string\"},{\"internalType\":\"address\",\"name\":\"callbackAddress\",\"type\":\"address\"},{\"internalType\":\"bytes4\",\"name\":\"callbackMethodID\",\"type\":\"bytes4\"}],\"name\":\"requestOracles\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
|
||||||
|
|
||||||
// OracleEmitter is an auto generated Go binding around an Ethereum contract.
|
// OracleEmitter is an auto generated Go binding around an Ethereum contract.
|
||||||
type OracleEmitter struct {
|
type OracleEmitter struct {
|
||||||
@ -137,7 +137,7 @@ func bindOracleEmitter(address common.Address, caller bind.ContractCaller, trans
|
|||||||
// sets the output to result. The result type might be a single field for simple
|
// sets the output to result. The result type might be a single field for simple
|
||||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||||
// returns.
|
// returns.
|
||||||
func (_OracleEmitter *OracleEmitterRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
|
func (_OracleEmitter *OracleEmitterRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
||||||
return _OracleEmitter.Contract.OracleEmitterCaller.contract.Call(opts, result, method, params...)
|
return _OracleEmitter.Contract.OracleEmitterCaller.contract.Call(opts, result, method, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ func (_OracleEmitter *OracleEmitterRaw) Transact(opts *bind.TransactOpts, method
|
|||||||
// sets the output to result. The result type might be a single field for simple
|
// sets the output to result. The result type might be a single field for simple
|
||||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||||
// returns.
|
// returns.
|
||||||
func (_OracleEmitter *OracleEmitterCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
|
func (_OracleEmitter *OracleEmitterCallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
||||||
return _OracleEmitter.Contract.contract.Call(opts, result, method, params...)
|
return _OracleEmitter.Contract.contract.Call(opts, result, method, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,24 +171,24 @@ func (_OracleEmitter *OracleEmitterTransactorRaw) Transact(opts *bind.TransactOp
|
|||||||
return _OracleEmitter.Contract.contract.Transact(opts, method, params...)
|
return _OracleEmitter.Contract.contract.Transact(opts, method, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestOracles is a paid mutator transaction binding the contract method 0xa5bc5dfe.
|
// RequestOracles is a paid mutator transaction binding the contract method 0xe7c3712a.
|
||||||
//
|
//
|
||||||
// Solidity: function requestOracles(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
// Solidity: function requestOracles(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
||||||
func (_OracleEmitter *OracleEmitterTransactor) RequestOracles(opts *bind.TransactOpts, originChain uint8, requestType uint8, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
func (_OracleEmitter *OracleEmitterTransactor) RequestOracles(opts *bind.TransactOpts, originChain uint8, requestType string, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
||||||
return _OracleEmitter.contract.Transact(opts, "requestOracles", originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
return _OracleEmitter.contract.Transact(opts, "requestOracles", originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestOracles is a paid mutator transaction binding the contract method 0xa5bc5dfe.
|
// RequestOracles is a paid mutator transaction binding the contract method 0xe7c3712a.
|
||||||
//
|
//
|
||||||
// Solidity: function requestOracles(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
// Solidity: function requestOracles(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
||||||
func (_OracleEmitter *OracleEmitterSession) RequestOracles(originChain uint8, requestType uint8, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
func (_OracleEmitter *OracleEmitterSession) RequestOracles(originChain uint8, requestType string, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
||||||
return _OracleEmitter.Contract.RequestOracles(&_OracleEmitter.TransactOpts, originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
return _OracleEmitter.Contract.RequestOracles(&_OracleEmitter.TransactOpts, originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestOracles is a paid mutator transaction binding the contract method 0xa5bc5dfe.
|
// RequestOracles is a paid mutator transaction binding the contract method 0xe7c3712a.
|
||||||
//
|
//
|
||||||
// Solidity: function requestOracles(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
// Solidity: function requestOracles(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID) returns(uint256)
|
||||||
func (_OracleEmitter *OracleEmitterTransactorSession) RequestOracles(originChain uint8, requestType uint8, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
func (_OracleEmitter *OracleEmitterTransactorSession) RequestOracles(originChain uint8, requestType string, requestParams string, callbackAddress common.Address, callbackMethodID [4]byte) (*types.Transaction, error) {
|
||||||
return _OracleEmitter.Contract.RequestOracles(&_OracleEmitter.TransactOpts, originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
return _OracleEmitter.Contract.RequestOracles(&_OracleEmitter.TransactOpts, originChain, requestType, requestParams, callbackAddress, callbackMethodID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,7 +262,7 @@ func (it *OracleEmitterNewOracleRequestIterator) Close() error {
|
|||||||
// OracleEmitterNewOracleRequest represents a NewOracleRequest event raised by the OracleEmitter contract.
|
// OracleEmitterNewOracleRequest represents a NewOracleRequest event raised by the OracleEmitter contract.
|
||||||
type OracleEmitterNewOracleRequest struct {
|
type OracleEmitterNewOracleRequest struct {
|
||||||
OriginChain uint8
|
OriginChain uint8
|
||||||
RequestType uint8
|
RequestType string
|
||||||
RequestParams string
|
RequestParams string
|
||||||
CallbackAddress common.Address
|
CallbackAddress common.Address
|
||||||
CallbackMethodID [4]byte
|
CallbackMethodID [4]byte
|
||||||
@ -270,9 +270,9 @@ type OracleEmitterNewOracleRequest struct {
|
|||||||
Raw types.Log // Blockchain specific contextual infos
|
Raw types.Log // Blockchain specific contextual infos
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterNewOracleRequest is a free log retrieval operation binding the contract event 0xaa3cd18b7cba9d24d288b9b45616e263b5f40a939fe7f4341f789b955649ea4a.
|
// FilterNewOracleRequest is a free log retrieval operation binding the contract event 0x661106d02c3bf98eb59554e55d23c9517ad1f25020f83102a720ad27e3617760.
|
||||||
//
|
//
|
||||||
// Solidity: event NewOracleRequest(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
// Solidity: event NewOracleRequest(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
||||||
func (_OracleEmitter *OracleEmitterFilterer) FilterNewOracleRequest(opts *bind.FilterOpts) (*OracleEmitterNewOracleRequestIterator, error) {
|
func (_OracleEmitter *OracleEmitterFilterer) FilterNewOracleRequest(opts *bind.FilterOpts) (*OracleEmitterNewOracleRequestIterator, error) {
|
||||||
|
|
||||||
logs, sub, err := _OracleEmitter.contract.FilterLogs(opts, "NewOracleRequest")
|
logs, sub, err := _OracleEmitter.contract.FilterLogs(opts, "NewOracleRequest")
|
||||||
@ -282,9 +282,9 @@ func (_OracleEmitter *OracleEmitterFilterer) FilterNewOracleRequest(opts *bind.F
|
|||||||
return &OracleEmitterNewOracleRequestIterator{contract: _OracleEmitter.contract, event: "NewOracleRequest", logs: logs, sub: sub}, nil
|
return &OracleEmitterNewOracleRequestIterator{contract: _OracleEmitter.contract, event: "NewOracleRequest", logs: logs, sub: sub}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WatchNewOracleRequest is a free log subscription operation binding the contract event 0xaa3cd18b7cba9d24d288b9b45616e263b5f40a939fe7f4341f789b955649ea4a.
|
// WatchNewOracleRequest is a free log subscription operation binding the contract event 0x661106d02c3bf98eb59554e55d23c9517ad1f25020f83102a720ad27e3617760.
|
||||||
//
|
//
|
||||||
// Solidity: event NewOracleRequest(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
// Solidity: event NewOracleRequest(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
||||||
func (_OracleEmitter *OracleEmitterFilterer) WatchNewOracleRequest(opts *bind.WatchOpts, sink chan<- *OracleEmitterNewOracleRequest) (event.Subscription, error) {
|
func (_OracleEmitter *OracleEmitterFilterer) WatchNewOracleRequest(opts *bind.WatchOpts, sink chan<- *OracleEmitterNewOracleRequest) (event.Subscription, error) {
|
||||||
|
|
||||||
logs, sub, err := _OracleEmitter.contract.WatchLogs(opts, "NewOracleRequest")
|
logs, sub, err := _OracleEmitter.contract.WatchLogs(opts, "NewOracleRequest")
|
||||||
@ -319,9 +319,9 @@ func (_OracleEmitter *OracleEmitterFilterer) WatchNewOracleRequest(opts *bind.Wa
|
|||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseNewOracleRequest is a log parse operation binding the contract event 0xaa3cd18b7cba9d24d288b9b45616e263b5f40a939fe7f4341f789b955649ea4a.
|
// ParseNewOracleRequest is a log parse operation binding the contract event 0x661106d02c3bf98eb59554e55d23c9517ad1f25020f83102a720ad27e3617760.
|
||||||
//
|
//
|
||||||
// Solidity: event NewOracleRequest(uint8 originChain, uint8 requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
// Solidity: event NewOracleRequest(uint8 originChain, string requestType, string requestParams, address callbackAddress, bytes4 callbackMethodID, uint256 requestID)
|
||||||
func (_OracleEmitter *OracleEmitterFilterer) ParseNewOracleRequest(log types.Log) (*OracleEmitterNewOracleRequest, error) {
|
func (_OracleEmitter *OracleEmitterFilterer) ParseNewOracleRequest(log types.Log) (*OracleEmitterNewOracleRequest, error) {
|
||||||
event := new(OracleEmitterNewOracleRequest)
|
event := new(OracleEmitterNewOracleRequest)
|
||||||
if err := _OracleEmitter.contract.UnpackLog(event, "NewOracleRequest", log); err != nil {
|
if err := _OracleEmitter.contract.UnpackLog(event, "NewOracleRequest", log); err != nil {
|
||||||
|
@ -18,7 +18,7 @@ contract Mediator is Ownable {
|
|||||||
aggregator = _aggregator;
|
aggregator = _aggregator;
|
||||||
}
|
}
|
||||||
|
|
||||||
function request(uint8 originChain, uint8 requestType, string memory requestParams) public returns (uint256) {
|
function request(uint8 originChain, string memory requestType, string memory requestParams) public returns (uint256) {
|
||||||
return oracleEmitter.requestOracles(originChain, requestType, requestParams, address(this), bytes4(keccak256("_receiveDataCallback(uint256, string)")));
|
return oracleEmitter.requestOracles(originChain, requestType, requestParams, address(this), bytes4(keccak256("_receiveDataCallback(uint256, string)")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,14 +6,14 @@ contract OracleEmitter {
|
|||||||
|
|
||||||
event NewOracleRequest(
|
event NewOracleRequest(
|
||||||
uint8 originChain,
|
uint8 originChain,
|
||||||
uint8 requestType,
|
string requestType,
|
||||||
string requestParams,
|
string requestParams,
|
||||||
address callbackAddress,
|
address callbackAddress,
|
||||||
bytes4 callbackMethodID,
|
bytes4 callbackMethodID,
|
||||||
uint256 requestID
|
uint256 requestID
|
||||||
);
|
);
|
||||||
|
|
||||||
function requestOracles(uint8 originChain, uint8 requestType, string memory requestParams, address callbackAddress, bytes4 callbackMethodID) public returns (uint256) {
|
function requestOracles(uint8 originChain, string memory requestType, string memory requestParams, address callbackAddress, bytes4 callbackMethodID) public returns (uint256) {
|
||||||
requestCounter++;
|
requestCounter++;
|
||||||
emit NewOracleRequest(originChain, requestType, requestParams, callbackAddress, callbackMethodID, requestCounter);
|
emit NewOracleRequest(originChain, requestType, requestParams, callbackAddress, callbackMethodID, requestCounter);
|
||||||
return requestCounter;
|
return requestCounter;
|
||||||
|
35
node/node.go
35
node/node.go
@ -9,6 +9,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Secured-Finance/dione/rpc"
|
||||||
|
rtypes "github.com/Secured-Finance/dione/rpc/types"
|
||||||
|
|
||||||
solana2 "github.com/Secured-Finance/dione/rpc/solana"
|
solana2 "github.com/Secured-Finance/dione/rpc/solana"
|
||||||
|
|
||||||
"github.com/Secured-Finance/dione/rpc/filecoin"
|
"github.com/Secured-Finance/dione/rpc/filecoin"
|
||||||
@ -45,9 +48,7 @@ type Node struct {
|
|||||||
GlobalCtxCancel context.CancelFunc
|
GlobalCtxCancel context.CancelFunc
|
||||||
OracleTopic string
|
OracleTopic string
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
Lotus *filecoin.LotusClient
|
|
||||||
Ethereum *ethclient.EthereumClient
|
Ethereum *ethclient.EthereumClient
|
||||||
Solana *solana2.SolanaClient
|
|
||||||
ConsensusManager *consensus.PBFTConsensusManager
|
ConsensusManager *consensus.PBFTConsensusManager
|
||||||
Miner *consensus.Miner
|
Miner *consensus.Miner
|
||||||
Beacon beacon.BeaconNetworks
|
Beacon beacon.BeaconNetworks
|
||||||
@ -69,12 +70,16 @@ func NewNode(configPath string) (*Node, error) {
|
|||||||
|
|
||||||
func (n *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey, pexDiscoveryUpdateTime time.Duration) {
|
func (n *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey, pexDiscoveryUpdateTime time.Duration) {
|
||||||
n.setupLibp2pHost(context.TODO(), prvKey, pexDiscoveryUpdateTime)
|
n.setupLibp2pHost(context.TODO(), prvKey, pexDiscoveryUpdateTime)
|
||||||
//n.setupFilecoinClient()
|
|
||||||
err := n.setupEthereumClient()
|
err := n.setupEthereumClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
n.setupSolanaClient()
|
|
||||||
|
err = n.setupRPCClients()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
n.setupPubsub()
|
n.setupPubsub()
|
||||||
err = n.setupConsensusManager(prvKey, n.Config.ConsensusMinApprovals)
|
err = n.setupConsensusManager(prvKey, n.Config.ConsensusMinApprovals)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,7 +101,7 @@ func (n *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey, pexDiscover
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) setupMiner() error {
|
func (n *Node) setupMiner() error {
|
||||||
n.Miner = consensus.NewMiner(n.Host.ID(), *n.Ethereum.GetEthAddress(), n.Wallet, n.Beacon, n.Ethereum, n.Lotus, n.Solana)
|
n.Miner = consensus.NewMiner(n.Host.ID(), *n.Ethereum.GetEthAddress(), n.Wallet, n.Beacon, n.Ethereum)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,14 +147,18 @@ func (n *Node) setupEthereumClient() error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) setupFilecoinClient() {
|
func (n *Node) setupRPCClients() error {
|
||||||
lotus := filecoin.NewLotusClient()
|
fc := filecoin.NewLotusClient()
|
||||||
n.Lotus = lotus
|
rpc.RegisterRPC(rtypes.RPCTypeFilecoin, map[string]func(string) (string, error){
|
||||||
}
|
"getTransaction": fc.GetTransaction,
|
||||||
|
})
|
||||||
|
|
||||||
func (n *Node) setupSolanaClient() {
|
sl := solana2.NewSolanaClient()
|
||||||
solana := solana2.NewSolanaClient()
|
rpc.RegisterRPC(rtypes.RPCTypeSolana, map[string]func(string) (string, error){
|
||||||
n.Solana = solana
|
"getTransaction": sl.GetTransaction,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) setupPubsub() {
|
func (n *Node) setupPubsub() {
|
||||||
@ -300,5 +309,3 @@ func generatePrivateKey() (crypto.PrivKey, error) {
|
|||||||
}
|
}
|
||||||
return prvKey, nil
|
return prvKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO generate Miner for the node
|
|
||||||
|
@ -38,9 +38,10 @@ func (c *LotusClient) GetTipSetByHeight(chainEpoch int64) ([]byte, error) {
|
|||||||
return c.HandleRequest("Filecoin.ChainGetTipSetByHeight", i)
|
return c.HandleRequest("Filecoin.ChainGetTipSetByHeight", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LotusClient) GetTransaction(cid string) ([]byte, error) {
|
func (c *LotusClient) GetTransaction(cid string) (string, error) {
|
||||||
i := ftypes.NewCidParam(cid)
|
i := ftypes.NewCidParam(cid)
|
||||||
return c.HandleRequest("Filecoin.ChainGetMessage", i)
|
resp, err := c.HandleRequest("Filecoin.ChainGetMessage", i)
|
||||||
|
return string(resp), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LotusClient) GetNodeVersion() ([]byte, error) {
|
func (c *LotusClient) GetNodeVersion() ([]byte, error) {
|
||||||
|
18
rpc/rpc.go
18
rpc/rpc.go
@ -1,5 +1,19 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
type RPCClient interface {
|
var rpcs = map[uint8]map[string]func(string) (string, error){} // rpcType -> {rpcMethodName -> actual func var}
|
||||||
GetTransaction(txHash string) ([]byte, error)
|
|
||||||
|
func RegisterRPC(rpcType uint8, rpcMethods map[string]func(string) (string, error)) {
|
||||||
|
rpcs[rpcType] = rpcMethods
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRPCMethod(rpcType uint8, rpcMethodName string) func(string) (string, error) {
|
||||||
|
rpcMethods, ok := rpcs[rpcType]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
actualMethod, ok := rpcMethods[rpcMethodName]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return actualMethod
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func NewSolanaClient() *SolanaClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SolanaClient) GetTransaction(txHash string) ([]byte, error) {
|
func (c *SolanaClient) GetTransaction(txHash string) (string, error) {
|
||||||
req := fasthttp.AcquireRequest()
|
req := fasthttp.AcquireRequest()
|
||||||
req.SetRequestURI(c.url)
|
req.SetRequestURI(c.url)
|
||||||
req.Header.SetMethod("POST")
|
req.Header.SetMethod("POST")
|
||||||
@ -48,18 +48,18 @@ func (c *SolanaClient) GetTransaction(txHash string) ([]byte, error) {
|
|||||||
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 {
|
||||||
return nil, fmt.Errorf("Failed to marshal request body %v", err)
|
return "", fmt.Errorf("Failed to marshal request body %v", err)
|
||||||
}
|
}
|
||||||
req.AppendBody(body)
|
req.AppendBody(body)
|
||||||
resp := fasthttp.AcquireResponse()
|
resp := fasthttp.AcquireResponse()
|
||||||
client := &fasthttp.Client{}
|
client := &fasthttp.Client{}
|
||||||
if err = client.Do(req, resp); err != nil {
|
if err = client.Do(req, resp); err != nil {
|
||||||
logrus.Warn("Failed to construct solana node rpc request", err)
|
logrus.Warn("Failed to construct solana node rpc request", err)
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
bodyBytes := resp.Body()
|
bodyBytes := resp.Body()
|
||||||
logrus.Info(string(bodyBytes))
|
logrus.Info(string(bodyBytes))
|
||||||
return bodyBytes, nil
|
return string(bodyBytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SolanaClient) subscribeOnProgram(programID string) {
|
func (c *SolanaClient) subscribeOnProgram(programID string) {
|
||||||
|
8
rpc/types/rpc_types.go
Normal file
8
rpc/types/rpc_types.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
const (
|
||||||
|
RPCTypeEthereum = uint8(iota)
|
||||||
|
|
||||||
|
RPCTypeFilecoin
|
||||||
|
RPCTypeSolana
|
||||||
|
)
|
@ -29,14 +29,12 @@ func (e DrandRound) String() string {
|
|||||||
// Miner is an address of miner node
|
// Miner is an address of miner node
|
||||||
type DioneTask struct {
|
type DioneTask struct {
|
||||||
Type TaskType
|
Type TaskType
|
||||||
Miner peer.ID
|
|
||||||
Ticket *Ticket
|
Ticket *Ticket
|
||||||
ElectionProof *ElectionProof
|
ElectionProof *ElectionProof
|
||||||
BeaconEntries []BeaconEntry
|
BeaconEntries []BeaconEntry
|
||||||
Signature *Signature
|
Signature *Signature
|
||||||
DrandRound DrandRound
|
DrandRound DrandRound
|
||||||
Payload []byte
|
Payload []byte
|
||||||
BlockHash string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDioneTask(
|
func NewDioneTask(
|
||||||
@ -48,18 +46,15 @@ func NewDioneTask(
|
|||||||
sig *Signature,
|
sig *Signature,
|
||||||
drand DrandRound,
|
drand DrandRound,
|
||||||
payload []byte,
|
payload []byte,
|
||||||
blockHash string,
|
|
||||||
) *DioneTask {
|
) *DioneTask {
|
||||||
return &DioneTask{
|
return &DioneTask{
|
||||||
Type: t,
|
Type: t,
|
||||||
Miner: miner,
|
|
||||||
Ticket: ticket,
|
Ticket: ticket,
|
||||||
ElectionProof: electionProof,
|
ElectionProof: electionProof,
|
||||||
BeaconEntries: beacon,
|
BeaconEntries: beacon,
|
||||||
Signature: sig,
|
Signature: sig,
|
||||||
DrandRound: drand,
|
DrandRound: drand,
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
BlockHash: blockHash,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user