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

Conversation

nick-bisonai
Copy link
Collaborator

@nick-bisonai nick-bisonai commented Mar 15, 2024

Description

fix panic failure on hash error for adapter and aggregator

AS-IS

goroutine 29 [running]:
runtime/debug.Stack()
        /opt/homebrew/Cellar/go/1.21.5/libexec/src/runtime/debug/stack.go:24 +0x64
bisonai.com/orakl/api/utils.CustomStackTraceHandler(0x14000442cc0?, {0x104da9fe0?, 0x1052f2fa0})
        /Users/kersner/git/orakl/api/utils/utils.go:301 +0x1d8
bisonai.com/orakl/api/utils.Setup.New.func2.1()
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/middleware/recover/recover.go:31 +0x70
panic({0x0?, 0x0?})
        /opt/homebrew/Cellar/go/1.21.5/libexec/src/runtime/panic.go:914 +0x218
bisonai.com/orakl/api/aggregator.computeAggregatorHash(0x140000af748, 0x1)
        /Users/kersner/git/orakl/api/aggregator/controller.go:322 +0x190
bisonai.com/orakl/api/aggregator.hash(0x104d9c820?)
        /Users/kersner/git/orakl/api/aggregator/controller.go:208 +0x338
github.com/gofiber/fiber/v2.(*App).next(0x14000483400, 0x14000478c00)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/router.go:145 +0x188
github.com/gofiber/fiber/v2.(*Ctx).Next(0x104e5bb00?)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/ctx.go:1030 +0x5c
bisonai.com/orakl/api/utils.Setup.func1(0x100?)
        /Users/kersner/git/orakl/api/utils/utils.go:180 +0x104
github.com/gofiber/fiber/v2.(*Ctx).Next(0x1400001c330?)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/ctx.go:1027 +0x48
github.com/gofiber/fiber/v2/middleware/cors.New.func1(0x14000478c00)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/middleware/cors/cors.go:166 +0x318
github.com/gofiber/fiber/v2.(*Ctx).Next(0x140000afaf8?)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/ctx.go:1027 +0x48
bisonai.com/orakl/api/utils.Setup.New.func2(0x104d9c820?)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/middleware/recover/recover.go:43 +0xa4
github.com/gofiber/fiber/v2.(*App).next(0x14000483400, 0x14000478c00)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/router.go:145 +0x188
github.com/gofiber/fiber/v2.(*App).handler(0x14000483400, 0x1049f6e88?)
        /Users/kersner/.go/pkg/mod/github.com/gofiber/fiber/v2@v2.52.0/router.go:172 +0x74
github.com/valyala/fasthttp.(*Server).serveConn(0x14000480fc0, {0x104e73b28?, 0x140005060b0})
        /Users/kersner/.go/pkg/mod/github.com/valyala/fasthttp@v1.52.0/server.go:2374 +0xdd0
github.com/valyala/fasthttp.(*workerPool).workerFunc(0x140004900a0, 0x1400028c040)
        /Users/kersner/.go/pkg/mod/github.com/valyala/fasthttp@v1.52.0/workerpool.go:224 +0x70
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        /Users/kersner/.go/pkg/mod/github.com/valyala/fasthttp@v1.52.0/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh in goroutine 1
        /Users/kersner/.go/pkg/mod/github.com/valyala/fasthttp@v1.52.0/workerpool.go:195 +0x208

2024/03/13 20:07:12 | 500 | 127.0.0.1 | POST | /api/v1/aggregator/hash | panic called with nil argument

TO-BE

Screenshot 2024-03-15 at 1 33 30 PM Screenshot 2024-03-15 at 1 33 22 PM

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.

Deployment

  • Should publish npm package
  • Should publish Docker image

Summary by CodeRabbit

  • Refactor
    • Adjusted URLs for assets to include the path /baobab/ and modified wallet information for different assets.
  • Refactor
    • Improved error handling in API components to enhance stability and provide clearer error messages.
  • Refactor
    • Enhanced error message handling in CLI scripts for improved readability and debugging.

@nick-bisonai nick-bisonai self-assigned this Mar 15, 2024
@nick-bisonai nick-bisonai requested a review from a team as a code owner March 15, 2024 04:36
Copy link
Contributor

coderabbitai bot commented Mar 15, 2024

Walkthrough

Walkthrough

The updates involve adjusting URLs for assets, specifically modifying adapterSource and aggregatorSource URLs to include the /baobab/ path in cli/test/mockData.ts. Furthermore, changes were made to walletAddress and walletPrivateKey values for different assets. In api/utils/utils.go, the logic in the QueryRows function was refined to handle empty result sets more effectively. Error handling and import statement reordering were also improved in api/adapter/controller.go and api/aggregator/controller.go.

Changes

Files Change Summary
cli/test/mockData.ts Updated adapterSource and aggregatorSource URLs to include /baobab/, adjusted asset paths. Modified walletAddress and walletPrivateKey values for various assets.
api/utils/utils.go Enhanced QueryRows function to return an empty slice of type T when the result is nil and no error is present.
api/adapter/controller.go Reordered import statements. Added error handling with specific responses in insert, hash, and computeAdapterHash functions.
api/aggregator/controller.go Reordered import statements. Added error handling and adjusted error messages in insert, hash, and computeAggregatorHash functions.
cli/src/adapter.ts Modified error message handling in hashHandler function to log the entire response.data object when computing the adapter hash fails.
cli/src/aggregator.ts Modified error handling logic in hashHandler function to simplify retrieval of error messages from the response data.

🐰✨
Changes here, changes there,
URL paths in the air.
Wallet secrets now beware,
Assets updated with care.
CodeRabbit hops without fail,
In our code ship, we set sail.
🌟🚀

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@nick-bisonai nick-bisonai linked an issue Mar 15, 2024 that may be closed by this pull request
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 113744d and 8d33f97.
Files selected for processing (4)
  • api/adapter/controller.go (4 hunks)
  • api/aggregator/controller.go (4 hunks)
  • cli/src/adapter.ts (1 hunks)
  • cli/src/aggregator.ts (1 hunks)
Additional comments: 4
cli/src/adapter.ts (1)
  • 122-124: The modification to log the entire response.data object instead of just the message property when an error occurs in the hashHandler function is a good improvement for debugging purposes. However, please ensure that logging this information does not inadvertently expose any sensitive data.
api/adapter/controller.go (3)
  • 62-62: Replacing panic with a specific error response in the insert function when computeAdapterHash fails is a significant improvement. This change enhances the robustness of the application by preventing crashes and providing more informative feedback to the client.
  • 109-109: Replacing panic with a specific error response in the hash function when computeAdapterHash fails is another excellent improvement. It enhances error handling by preventing application crashes and providing clearer feedback to the client.
  • 156-156: Updating the computeAdapterHash function to return an error instead of panicking is a commendable change. It forms the basis for the improvements in error handling observed in the insert and hash functions, making the application more resilient.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8d33f97 and f377b2b.
Files ignored due to path filters (1)
  • cli/package.json is excluded by: !**/*.json
Files selected for processing (1)
  • cli/test/mockData.ts (2 hunks)
Additional comments: 2
cli/test/mockData.ts (2)
  • 86-102: The updates to adapterSource and aggregatorSource URLs to include the /baobab/ path are consistent across the DATAFEED_BULK_0 object. This change aligns with the PR's objective to adjust URLs for different assets. Ensure that the new URLs are correct and accessible.
  • 120-136: Similar to DATAFEED_BULK_0, the DATAFEED_BULK_1 object's updates to adapterSource and aggregatorSource URLs to include the /baobab/ path are consistent and align with the PR's objectives. Additionally, the changes to walletAddress and walletPrivateKey values are noted. Verify that these credentials are mock or test values and not actual private keys.

@nick-bisonai nick-bisonai changed the title (API)Fix panic on hash failure (API) Fix panic on hash failure Mar 15, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f377b2b and 824447b.
Files selected for processing (1)
  • api/utils/utils.go (1 hunks)
Additional comments: 1
api/utils/utils.go (1)
  • 126-127: The adjustment to return an empty slice of type T when the result set is empty and there's no error is a good practice for robust error handling. This change ensures that the absence of data is not treated as an error condition, which is particularly useful for queries that might legitimately return no rows under normal circumstances.

Please ensure that all parts of the application that call QueryRows are updated to correctly handle an empty slice as a valid response, not indicative of an error.

@@ -152,7 +153,7 @@ func computeAdapterHash(data *AdapterInsertModel, verify bool) error {

out, err := json.Marshal(input)
if err != nil {
panic(err)
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor. Rather than repeating the same error message everywhere where computAdapterHash is used, we could return it here.

Suggested change
return err
return fmt.Sprintf(failed to compute adapter hash: %w", err.Error())

And then just call as

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

Copy link
Member

@martinkersner martinkersner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 824447b and ea46572.
Files selected for processing (4)
  • api/adapter/controller.go (4 hunks)
  • api/aggregator/controller.go (4 hunks)
  • cli/src/adapter.ts (1 hunks)
  • cli/src/aggregator.ts (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • api/adapter/controller.go
  • api/aggregator/controller.go
  • cli/src/adapter.ts
  • cli/src/aggregator.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ea46572 and 1811580.
Files selected for processing (1)
  • cli/test/mockData.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cli/test/mockData.ts

@nick-bisonai nick-bisonai merged commit c907cbb into master Mar 15, 2024
2 checks passed
@nick-bisonai nick-bisonai deleted the feat/api-no-panic-on-hash-failure branch March 15, 2024 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API returns panic when fail to verify adapter or aggregator
2 participants