Implement making RPC request & creating DioneTask for holding result of oracle request in transaction payload

This commit is contained in:
ChronosX88 2021-06-10 23:32:39 +03:00
parent ac2bf637a8
commit de5d9cf664
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 28 additions and 42 deletions

View File

@ -9,6 +9,10 @@ import (
"os" "os"
"time" "time"
"github.com/fxamacker/cbor/v2"
"github.com/Secured-Finance/dione/types"
"github.com/Secured-Finance/dione/blockchain" "github.com/Secured-Finance/dione/blockchain"
types2 "github.com/Secured-Finance/dione/blockchain/types" types2 "github.com/Secured-Finance/dione/blockchain/types"
@ -261,13 +265,31 @@ func (n *Node) subscribeOnEthContractsAsync(ctx context.Context) {
EventLoop: EventLoop:
for { for {
select { select {
case <-eventChan: case event := <-eventChan:
{ {
logrus.Info("Let's wait a little so that all nodes have time to receive the request") rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType)
time.Sleep(5 * time.Second) if rpcMethod == nil {
logrus.Errorf("Invalid RPC method name/type %d/%s for oracle request %s", event.OriginChain, event.RequestType, event.ReqID.String())
// TODO make the rpc request and save response as tx payload continue
tx := types2.CreateTransaction([]byte{}) }
res, err := rpcMethod(event.RequestParams)
if err != nil {
logrus.Errorf("Failed to invoke RPC method for oracle request %s: %s", event.ReqID.String(), err.Error())
continue
}
task := &types.DioneTask{
OriginChain: event.OriginChain,
RequestType: event.RequestType,
RequestParams: event.RequestParams,
Payload: res,
RequestID: event.ReqID.String(),
}
data, err := cbor.Marshal(task)
if err != nil {
logrus.Errorf("Failed to marshal RPC response for oracle request %s: %s", event.ReqID.String(), err.Error())
continue
}
tx := types2.CreateTransaction(data)
err = n.MemPool.StoreTx(tx) err = n.MemPool.StoreTx(tx)
if err != nil { if err != nil {
logrus.Errorf("Failed to store tx in mempool: %s", err.Error()) logrus.Errorf("Failed to store tx in mempool: %s", err.Error())

View File

@ -1,48 +1,12 @@
package types package types
import (
"github.com/ethereum/go-ethereum/common"
"github.com/libp2p/go-libp2p-core/peer"
)
const TicketRandomnessLookback = 1 const TicketRandomnessLookback = 1
// DioneTask represents the values of task computation // DioneTask represents the values of task computation
// DEPRECATED!
type DioneTask struct { type DioneTask struct {
OriginChain uint8 OriginChain uint8
RequestType string RequestType string
RequestParams string RequestParams string
Miner peer.ID
MinerEth common.Address
ElectionProof *ElectionProof
BeaconEntries []BeaconEntry
DrandRound uint64
Payload []byte Payload []byte
RequestID string RequestID string
ConsensusID string
Signature []byte `hash:"-"`
}
func NewDioneTask(
originChain uint8,
requestType string,
requestParams string,
miner peer.ID,
electionProof *ElectionProof,
beacon []BeaconEntry,
drandRound uint64,
payload []byte,
) *DioneTask {
return &DioneTask{
OriginChain: originChain,
RequestType: requestType,
RequestParams: requestParams,
Miner: miner,
ElectionProof: electionProof,
BeaconEntries: beacon,
DrandRound: drandRound,
Payload: payload,
}
} }