Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(API) Fix panic on hash failure #1259

Merged
merged 9 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 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,23 +117,23 @@ export const DATAFEED_BULK_1 = {
eventName: 'NewRoundV2',
bulk: [
{
adapterSource: 'https://config.orakl.network/adapter/bnb-usdt.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/bnb-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bnb-usdt.aggregator.json',
reporter: {
walletAddress: '0x0',
walletPrivateKey: '0x1'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/bora-krw.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/bora-krw.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bora-krw.aggregator.json',
reporter: {
walletAddress: '0x2',
walletPrivateKey: '0x3'
}
},
{
adapterSource: 'https://config.orakl.network/adapter/eth-usdt.adapter.json',
adapterSource: 'https://config.orakl.network/adapter/baobab/eth-usdt.adapter.json',
aggregatorSource: 'https://config.orakl.network/aggregator/baobab/eth-usdt.aggregator.json',
reporter: {
walletAddress: '0x4',
Expand Down
Loading