Skip to content

Commit

Permalink
update yoda to rely on request_id instead of event
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKSI committed Sep 12, 2023
1 parent a269f74 commit 3156c5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 116 deletions.
37 changes: 0 additions & 37 deletions yoda/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package yoda

import (
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -16,42 +15,6 @@ type rawRequest struct {
calldata string
}

// GetRawRequests returns the list of all raw data requests in the given log.
func GetRawRequests(log sdk.ABCIMessageLog) ([]rawRequest, error) {
dataSourceIDs := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyDataSourceID)
dataSourceHashList := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyDataSourceHash)
externalIDs := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyExternalID)
calldataList := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyCalldata)

if len(dataSourceIDs) != len(externalIDs) {
return nil, fmt.Errorf("Inconsistent data source count and external ID count")
}
if len(dataSourceIDs) != len(calldataList) {
return nil, fmt.Errorf("Inconsistent data source count and calldata count")
}

var reqs []rawRequest
for idx := range dataSourceIDs {
dataSourceID, err := strconv.Atoi(dataSourceIDs[idx])
if err != nil {
return nil, fmt.Errorf("Failed to parse data source id: %s", err.Error())
}

externalID, err := strconv.Atoi(externalIDs[idx])
if err != nil {
return nil, fmt.Errorf("Failed to parse external id: %s", err.Error())
}

reqs = append(reqs, rawRequest{
dataSourceID: types.DataSourceID(dataSourceID),
dataSourceHash: dataSourceHashList[idx],
externalID: types.ExternalID(externalID),
calldata: calldataList[idx],
})
}
return reqs, nil
}

// GetEventValues returns the list of all values in the given log with the given type and key.
func GetEventValues(log sdk.ABCIMessageLog, evType string, evKey string) (res []string) {
for _, ev := range log.Events {
Expand Down
101 changes: 22 additions & 79 deletions yoda/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,101 +45,44 @@ func handleTransaction(c *Context, l *Logger, tx abci.TxResult) {
}

func handleRequestLog(c *Context, l *Logger, log sdk.ABCIMessageLog) {
idStr, err := GetEventValue(log, types.EventTypeRequest, types.AttributeKeyID)
if err != nil {
l.Debug(":cold_sweat: Failed to parse request id with error: %s", err.Error())
return
}
idStrs := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyID)

id, err := strconv.Atoi(idStr)
if err != nil {
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, idStr, err.Error())
return
}
for _, idStr := range idStrs {
id, err := strconv.Atoi(idStr)
if err != nil {
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, idStr, err.Error())
return
}

l = l.With("rid", id)
// If id is in pending requests list, then skip it.
if c.pendingRequests[types.RequestID(id)] {
l.Debug(":eyes: Request is in pending list, then skip")
return
}

go handlePendingRequest(c, l, types.RequestID(id))
}
}

// If id is in pending requests list, then skip it.
if c.pendingRequests[types.RequestID(id)] {
l.Debug(":eyes: Request is in pending list, then skip")
func handlePendingRequest(c *Context, l *Logger, id types.RequestID) {
req, err := GetRequest(c, l, id)
if err != nil {
l.Error(":skull: Failed to get request with error: %s", c, err.Error())
return
}

// Skip if not related to this validator
validators := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyValidator)
hasMe := false
for _, validator := range validators {
if validator == c.validator.String() {
for _, val := range req.RequestedValidators {
if val == c.validator.String() {
hasMe = true
break
}
}

if !hasMe {
l.Debug(":next_track_button: Skip request not related to this validator")
return
}

l.Info(":delivery_truck: Processing incoming request event")

reqs, err := GetRawRequests(log)
if err != nil {
l.Error(":skull: Failed to parse raw requests with error: %s", c, err.Error())
}

keyIndex := c.nextKeyIndex()
key := c.keys[keyIndex]

reports, execVersions := handleRawRequests(c, l, types.RequestID(id), reqs, key)

rawAskCount := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyAskCount)
if len(rawAskCount) != 1 {
panic("Fail to get ask count")
}
askCount := MustAtoi(rawAskCount[0])

rawMinCount := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyMinCount)
if len(rawMinCount) != 1 {
panic("Fail to get min count")
}
minCount := MustAtoi(rawMinCount[0])

rawCallData := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyCalldata)
if len(rawCallData) != 1 {
panic("Fail to get call data")
}
callData, err := hex.DecodeString(rawCallData[0])
if err != nil {
l.Error(":skull: Fail to parse call data: %s", c, err.Error())
}

var clientID string
rawClientID := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyClientID)
if len(rawClientID) > 0 {
clientID = rawClientID[0]
}

c.pendingMsgs <- ReportMsgWithKey{
msg: types.NewMsgReportData(types.RequestID(id), reports, c.validator),
execVersion: execVersions,
keyIndex: keyIndex,
feeEstimationData: FeeEstimationData{
askCount: askCount,
minCount: minCount,
callData: callData,
rawRequests: reqs,
clientID: clientID,
},
}
}

func handlePendingRequest(c *Context, l *Logger, id types.RequestID) {
req, err := GetRequest(c, l, id)
if err != nil {
l.Error(":skull: Failed to get request with error: %s", c, err.Error())
return
}

l.Info(":delivery_truck: Processing pending request")

keyIndex := c.nextKeyIndex()
Expand Down

0 comments on commit 3156c5d

Please sign in to comment.