Skip to content

Commit

Permalink
(API) Fix panic on hash failure (#1259)
Browse files Browse the repository at this point in the history
* fix: update error handling

* fix: update err msg handling from cli

* fix: fix jest library reference error

* fix: fix data src url for updated orakl-config

* fix: bug fix to return empty arr

* fix: update utils for parsing db with multiple rows

* fix: use variable declared

* fix: update error handling based on feedback

* fix: update mockdata
  • Loading branch information
nick-bisonai authored Mar 15, 2024
1 parent e6a56d8 commit c907cbb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 27 deletions.
14 changes: 8 additions & 6 deletions api/adapter/controller.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package adapter

import (
"bisonai.com/orakl/api/feed"
"bisonai.com/orakl/api/utils"
"encoding/json"
"fmt"
"strconv"

"bisonai.com/orakl/api/feed"
"bisonai.com/orakl/api/utils"

"github.com/ethereum/go-ethereum/crypto"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -58,7 +59,7 @@ func insert(c *fiber.Ctx) error {

err := computeAdapterHash(payload, true)
if err != nil {
panic(err)
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}

row, err := utils.QueryRow[AdapterIdModel](c, InsertAdapter, map[string]any{
Expand Down Expand Up @@ -105,7 +106,7 @@ func hash(c *fiber.Ctx) error {

err = computeAdapterHash(&payload, verify)
if err != nil {
panic(err)
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(payload)
}
Expand Down Expand Up @@ -152,13 +153,14 @@ func computeAdapterHash(data *AdapterInsertModel, verify bool) error {

out, err := json.Marshal(input)
if err != nil {
panic(err)
return fmt.Errorf("failed to compute adapter hash: %s", err.Error())
}

hash := crypto.Keccak256Hash([]byte(out))
hashString := fmt.Sprintf("0x%x", hash)
if verify && data.AdapterHash != hashString {
return fmt.Errorf("hashes do not match!\nexpected %s, received %s", hashString, data.AdapterHash)
hashComputeErr := fmt.Errorf("hashes do not match!\nexpected %s, received %s", hashString, data.AdapterHash)
return fmt.Errorf("failed to compute adapter hash: %s", hashComputeErr.Error())
}

data.AdapterHash = hashString
Expand Down
16 changes: 9 additions & 7 deletions api/aggregator/controller.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package aggregator

import (
"encoding/json"
"fmt"
"strconv"

"bisonai.com/orakl/api/adapter"
"bisonai.com/orakl/api/chain"
"bisonai.com/orakl/api/feed"
"bisonai.com/orakl/api/utils"
"encoding/json"
"fmt"
"strconv"

"github.com/ethereum/go-ethereum/crypto"
"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -118,7 +119,7 @@ func insert(c *fiber.Ctx) error {
}
err = computeAggregatorHash(&hashComputeParam, true)
if err != nil {
panic(err)
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}

insertParam := _AggregatorInsertModel{
Expand Down Expand Up @@ -207,7 +208,7 @@ func hash(c *fiber.Ctx) error {

err = computeAggregatorHash(&hashComputeParam, verify)
if err != nil {
panic(err)
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}

return c.JSON(hashComputeParam)
Expand Down Expand Up @@ -313,13 +314,14 @@ func computeAggregatorHash(data *AggregatorHashComputeInputModel, verify bool) e
processData := input.AggregatorHashComputeProcessModel
out, err := json.Marshal(processData)
if err != nil {
panic(err)
return fmt.Errorf("failed to compute adapter hash: %s", err.Error())
}

hash := crypto.Keccak256Hash([]byte(out))
hashString := fmt.Sprintf("0x%x", hash)
if verify && data.AggregatorHash != hashString {
panic(err)
hashComputeErr := fmt.Errorf("hashes do not match!\nexpected %s, received %s", hashString, data.AggregatorHash)
return fmt.Errorf("failed to compute adapter hash: %s", hashComputeErr.Error())
}

data.AggregatorHash = hashString
Expand Down
4 changes: 2 additions & 2 deletions api/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func QueryRows[T any](c *fiber.Ctx, query string, args map[string]any) ([]T, err
}

results, err = pgx.CollectRows(rows, pgx.RowToStructByName[T])
if errors.Is(err, pgx.ErrNoRows) {
return results, nil
if errors.Is(err, pgx.ErrNoRows) || (results == nil && err == nil) {
return []T{}, nil
}
return results, err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"build": "yarn clean && tsc",
"cli": "node --no-warnings --import=specifier-resolution-node/register --experimental-json-modules dist/index.js",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --testPathIgnorePatterns mockData.ts --silent"
"test": "node --no-warnings --experimental-vm-modules node_modules/.bin/jest --testPathIgnorePatterns mockData.ts --silent"
},
"bin": {
"cli": "dist/index.js"
Expand Down
2 changes: 1 addition & 1 deletion cli/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function hashHandler() {
return adapterWithCorrectHash
} catch (e) {
console.error('Adapter hash could not be computed. Reason:')
const errMsg = e?.response?.data?.message ? e.response.data.message : e.message
const errMsg = e?.response?.data ? e.response.data : e.message

console.error(errMsg)
return errMsg
Expand Down
3 changes: 2 additions & 1 deletion cli/src/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ export function hashHandler() {
return aggregatorWithCorrectHash
} catch (e) {
console.error('Aggregator hash could not be computed. Reason:')
const errMsg = e?.response?.data?.message ? e.response.data.message : e.message
const errMsg = e?.response?.data ? e.response.data : e.message

console.error(errMsg)
return errMsg
}
Expand Down
18 changes: 9 additions & 9 deletions cli/test/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ export const VRF_1 = {
export const DATAFEED_BULK_0 = {
bulk: [
{
adapterSource: 'https://config.orakl.network/adapter/ada-usdt.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/ada-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/ada-usdt.aggregator.json',
reporter: {
walletAddress: '0xa',
walletPrivateKey: '0xb'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/atom-usdt.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/atom-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/atom-usdt.aggregator.json',
reporter: {
walletAddress: '0xc',
walletPrivateKey: '0xd'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/avax-usdt.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/avax-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/avax-usdt.aggregator.json',
reporter: {
walletAddress: '0xe',
Expand All @@ -117,24 +117,24 @@ export const DATAFEED_BULK_1 = {
eventName: 'NewRoundV2',
bulk: [
{
adapterSource: 'https://config.orakl.network/adapter/bnb-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bnb-usdt.aggregator.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/btc-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/btc-usdt.aggregator.json',
reporter: {
walletAddress: '0x0',
walletPrivateKey: '0x1'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/bora-krw.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bora-krw.aggregator.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/ltc-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/ltc-usdt.aggregator.json',
reporter: {
walletAddress: '0x2',
walletPrivateKey: '0x3'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/eth-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/eth-usdt.aggregator.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/klay-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/klay-usdt.aggregator.json',
reporter: {
walletAddress: '0x4',
walletPrivateKey: '0x5'
Expand Down

0 comments on commit c907cbb

Please sign in to comment.