From c907cbb52de51d29ac0509c5c73f4b32f986ffeb Mon Sep 17 00:00:00 2001 From: Nick <148735107+nick-bisonai@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:18:34 +0900 Subject: [PATCH] (API) Fix panic on hash failure (#1259) * 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 --- api/adapter/controller.go | 14 ++++++++------ api/aggregator/controller.go | 16 +++++++++------- api/utils/utils.go | 4 ++-- cli/package.json | 2 +- cli/src/adapter.ts | 2 +- cli/src/aggregator.ts | 3 ++- cli/test/mockData.ts | 18 +++++++++--------- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/api/adapter/controller.go b/api/adapter/controller.go index 4018d877a..9d15a53ab 100644 --- a/api/adapter/controller.go +++ b/api/adapter/controller.go @@ -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" @@ -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{ @@ -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) } @@ -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 diff --git a/api/aggregator/controller.go b/api/aggregator/controller.go index 1bd288776..c8378d3df 100644 --- a/api/aggregator/controller.go +++ b/api/aggregator/controller.go @@ -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" @@ -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{ @@ -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) @@ -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 diff --git a/api/utils/utils.go b/api/utils/utils.go index 7e8b517ec..a8c035a90 100644 --- a/api/utils/utils.go +++ b/api/utils/utils.go @@ -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 } diff --git a/cli/package.json b/cli/package.json index cb47a29fc..92f498361 100644 --- a/cli/package.json +++ b/cli/package.json @@ -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" diff --git a/cli/src/adapter.ts b/cli/src/adapter.ts index 0ad701825..b4b72b2fd 100644 --- a/cli/src/adapter.ts +++ b/cli/src/adapter.ts @@ -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 diff --git a/cli/src/aggregator.ts b/cli/src/aggregator.ts index b00850617..c35def6e5 100644 --- a/cli/src/aggregator.ts +++ b/cli/src/aggregator.ts @@ -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 } diff --git a/cli/test/mockData.ts b/cli/test/mockData.ts index d8cf67dc9..98f33e099 100644 --- a/cli/test/mockData.ts +++ b/cli/test/mockData.ts @@ -83,7 +83,7 @@ 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', @@ -91,7 +91,7 @@ export const DATAFEED_BULK_0 = { } }, { - 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', @@ -99,7 +99,7 @@ export const DATAFEED_BULK_0 = { } }, { - 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', @@ -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'