From de5d9cf664a2cf68d4cd142276c160f694b1eff4 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 10 Jun 2021 23:32:39 +0300 Subject: [PATCH] Implement making RPC request & creating DioneTask for holding result of oracle request in transaction payload --- node/node.go | 34 ++++++++++++++++++++++++++++------ types/task.go | 36 ------------------------------------ 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/node/node.go b/node/node.go index bf4d649..3ab3af8 100644 --- a/node/node.go +++ b/node/node.go @@ -9,6 +9,10 @@ import ( "os" "time" + "github.com/fxamacker/cbor/v2" + + "github.com/Secured-Finance/dione/types" + "github.com/Secured-Finance/dione/blockchain" types2 "github.com/Secured-Finance/dione/blockchain/types" @@ -261,13 +265,31 @@ func (n *Node) subscribeOnEthContractsAsync(ctx context.Context) { EventLoop: for { select { - case <-eventChan: + case event := <-eventChan: { - logrus.Info("Let's wait a little so that all nodes have time to receive the request") - time.Sleep(5 * time.Second) - - // TODO make the rpc request and save response as tx payload - tx := types2.CreateTransaction([]byte{}) + rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType) + if rpcMethod == nil { + logrus.Errorf("Invalid RPC method name/type %d/%s for oracle request %s", event.OriginChain, event.RequestType, event.ReqID.String()) + continue + } + 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) if err != nil { logrus.Errorf("Failed to store tx in mempool: %s", err.Error()) diff --git a/types/task.go b/types/task.go index f567e7a..87ec8b8 100644 --- a/types/task.go +++ b/types/task.go @@ -1,48 +1,12 @@ package types -import ( - "github.com/ethereum/go-ethereum/common" - - "github.com/libp2p/go-libp2p-core/peer" -) - const TicketRandomnessLookback = 1 // DioneTask represents the values of task computation -// DEPRECATED! type DioneTask struct { OriginChain uint8 RequestType string RequestParams string - Miner peer.ID - MinerEth common.Address - ElectionProof *ElectionProof - BeaconEntries []BeaconEntry - DrandRound uint64 Payload []byte 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, - } }