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

[TODO] chore: update smt.MerkleRoot#Sum() error handling #672

Merged
merged 18 commits into from
Jul 19, 2024

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Jul 10, 2024

Summary

Update smt.MerkleRoot#Sum() error handling post-refactoring (see: pokt-network/smt#50).

Issue

Outstanding

  • Cut a new release of the smt go module.
  • Update the smt version in go.mod.

Type of change

Select one or more:

  • New feature, functionality or library
  • Bug fix
  • Code health or cleanup
  • Documentation
  • Other (specify)

Testing

Documentation changes (only if making doc changes)

  • make docusaurus_start; only needed if you make doc changes

Local Testing (only if making code changes)

  • Unit Tests: make go_develop_and_test
  • LocalNet E2E Tests: make test_e2e
  • See quickstart guide for instructions

PR Testing (only if making code changes)

  • DevNet E2E Tests: Add the devnet-test-e2e label to the PR.
    • THIS IS VERY EXPENSIVE, so only do it after all the reviews are complete.
    • Optionally run make trigger_ci if you want to re-trigger tests without any code changes
    • If tests fail, try re-running failed tests only using the GitHub UI as shown here

Sanity Checklist

  • I have tested my changes using the available tooling
  • I have commented my code
  • I have performed a self-review of my own code; both comments & source code
  • I create and reference any new tickets, if applicable
  • I have left TODOs throughout the codebase, if applicable

Summary by CodeRabbit

  • New Features

    • Improved accuracy in computing the sum and count of compute units and relays for claims.
    • Introduced a new hashing mechanism for trie initialization and proof submissions.
  • Refactor

    • Enhanced error handling in session accounting processes.
    • Updated functions to utilize new hashing sizes and methods.
  • Dependencies

    • Updated github.com/pokt-network/smt module to version v0.12.0.
  • Tests

    • Modified test cases to reflect changes in hashing mechanisms and sum calculations, ensuring robust verification of new features.

@bryanchriswhite bryanchriswhite added bug Something isn't working code health Cleans up some code labels Jul 10, 2024
@bryanchriswhite bryanchriswhite self-assigned this Jul 10, 2024
Copy link

coderabbitai bot commented Jul 10, 2024

Walkthrough

This update primarily refactors cryptographic operations and Merkle tree handling across various files. Key changes include replacing sha256.New with protocol.NewTrieHasher for hashing tasks and shifting to MerkleSumRoot for enhanced accuracy in computations. These modifications aim to improve the system's reliability and efficiency in processing proofs and transactions.

Changes

Files/Groups Change Summary
x/proof/types/claim.go Updated GetNumComputeUnits and GetNumRelays methods to directly utilize MerkleSumRoot.
x/tokenomics/keeper/settle_session_accounting.go Added necessary imports, altered function signatures to return errors, and refactored internal logic.
go.mod Updated github.com/pokt-network/smt module version from v0.11.1 to v0.12.0.
testutil/proof/fixture_generators.go Adjusted functions to use MerkleSumRoot and introduced a new encodeSum helper function for hash encoding.
x/tokenomics/keeper/settle_session_accounting_test.go Modified root type and updated assignment logic in tests.
pkg/client/supplier/client_test.go Replaced sha256.New() with protocol.NewTrieHasher() in the TestSupplierClient_SubmitProof function.
pkg/crypto/protocol/hasher.go Introduced constants and a NewTrieHasher variable for hashing operations.
pkg/relayer/session/sessiontree.go Updated NewSessionTree to utilize protocol.NewTrieHasher() for trie initialization.
tests/integration/tokenomics/relay_mining_difficulty_test.go Changed prepareSMST function to use protocol.NewTrieHasher() for trie creation.
x/application/keeper/auto_undelegate.go Added an import statement for gatewaytypes from github.com/pokt-network/poktroll/x/gateway/types.

Poem

In the code where hashes hum,
A rabbit's work is never done.
From roots of Merkle, sums do climb,
With new hashers, sleek and prime.
Errors handled, neat and clear,
Our blockchain hums without a fear.


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 testing code 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 testing code 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 testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@bryanchriswhite bryanchriswhite marked this pull request as ready for review July 10, 2024 21:29
@bryanchriswhite bryanchriswhite linked an issue Jul 10, 2024 that may be closed by this pull request
15 tasks
Copy link

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bdbc365 and fe59bcb.

Files selected for processing (2)
  • x/proof/types/claim.go (1 hunks)
  • x/tokenomics/keeper/settle_session_accounting.go (5 hunks)
Additional comments not posted (4)
x/proof/types/claim.go (2)

26-26: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to GetNumComputeUnits handle the potential error correctly.

Verification successful

Verification successful: Error handling for GetNumComputeUnits is correctly implemented.

The error handling for all calls to the GetNumComputeUnits function is properly implemented. No further changes are necessary.

  • x/tokenomics/keeper/settle_pending_claims.go
  • x/proof/keeper/msg_server_submit_proof.go
  • x/proof/keeper/msg_server_create_claim.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `GetNumComputeUnits` handle the potential error correctly.

# Test: Search for the function usage. Expect: Proper error handling.
rg --type go -A 5 $'GetNumComputeUnits()'

Length of output: 9509


Line range hint 35-35:
LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to GetNumRelays handle the potential error correctly.

Verification successful

Ensure all function calls to GetNumRelays handle errors correctly.

The function calls to GetNumRelays in the following files handle errors appropriately:

  • x/tokenomics/keeper/settle_pending_claims.go
  • x/proof/keeper/msg_server_submit_proof.go
  • x/proof/keeper/msg_server_create_claim.go

These instances show that the error returned by GetNumRelays is properly checked and handled.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `GetNumRelays` handle the potential error correctly.

# Test: Search for the function usage. Expect: Proper error handling.
rg --type go -A 5 $'GetNumRelays()'

Length of output: 8337

x/tokenomics/keeper/settle_session_accounting.go (2)

30-30: LGTM! The error handling updates are appropriate.

The code changes are approved.


182-182: LGTM! The error handling updates are appropriate.

The code changes are approved.

Copy link
Contributor

@red-0ne red-0ne left a comment

Choose a reason for hiding this comment

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

Good to see the panic case removed.

Copy link

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fe59bcb and aee4d0e.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (1)
  • go.mod (1 hunks)
Files skipped from review due to trivial changes (1)
  • go.mod

@bryanchriswhite bryanchriswhite added push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 12, 2024
Copy link

The CI will now also run the e2e tests on devnet, which increases the time it takes to complete all CI checks.

You may need to run make trigger_ci to submit an empty commit that'll trigger the tests.

GCP workloads (requires changing the namespace to 672)
Grafana network dashboard for devnet-issue-{issue-id}

Copy link

The image is going to be pushed after the next commit.

You can use make trigger_ci to push an empty commit.

If you also want to run E2E tests, please add devnet-test-e2e label.

Copy link

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aee4d0e and f354f0e.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (1)
  • go.mod (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go.mod

@bryanchriswhite bryanchriswhite removed push-image CI related - pushes images to ghcr.io devnet devnet-test-e2e labels Jul 15, 2024
…-root

* pokt/main:
  [TODO] Blockers @okdas (#674)
  [TODO_BLOCKER] chore: Remove stale TODO_BLOCKERs (#694)
  [TODO] refactor: query expiring claims w/ index (#671)
  [TODOs] `grace_period_end_offset_blocks` validation & cleanup (#667)
  [Testing] add account balance coverage to account settlement (#670)
  [Application] Auto-undelegation of unstaked gateways (#692)
  [RelayMiner] Fix LeanClient mutiple suppliers support (#662)
Copy link

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3603bc5 and 5f089be.

Files selected for processing (4)
  • go.mod (1 hunks)
  • testutil/proof/fixture_generators.go (2 hunks)
  • x/tokenomics/keeper/settle_session_accounting.go (5 hunks)
  • x/tokenomics/keeper/settle_session_accounting_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (4)
  • go.mod
  • testutil/proof/fixture_generators.go
  • x/tokenomics/keeper/settle_session_accounting.go
  • x/tokenomics/keeper/settle_session_accounting_test.go

testutil/proof/fixture_generators.go Outdated Show resolved Hide resolved
testutil/proof/fixture_generators.go Outdated Show resolved Hide resolved
testutil/proof/fixture_generators.go Outdated Show resolved Hide resolved
x/tokenomics/keeper/settle_session_accounting.go Outdated Show resolved Hide resolved
x/tokenomics/keeper/settle_session_accounting.go Outdated Show resolved Hide resolved
Copy link

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5f089be and 35e253c.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (1)
  • go.mod (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go.mod

…-root

* pokt/main:
  [TODOs] add `EventApplicationOverserviced` event (#669)
  [Supplier] Deny supplier staking with unknown services (#693)
Copy link

@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.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 35e253c and 91f9cd9.

Files selected for processing (2)
  • x/tokenomics/keeper/settle_session_accounting.go (5 hunks)
  • x/tokenomics/keeper/settle_session_accounting_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/tokenomics/keeper/settle_session_accounting_test.go
Additional context used
GitHub Check: go-test
x/tokenomics/keeper/settle_session_accounting.go

[failure] 80-80:
undefined: types) (typecheck)


[failure] 80-80:
undefined: types) (typecheck)


[failure] 80-80:
undefined: types) (typecheck)


[failure] 80-80:
undefined: types) (typecheck)


[failure] 80-80:
undefined: types) (typecheck)

Additional comments not posted (4)
x/tokenomics/keeper/settle_session_accounting.go (4)

5-5: Approved addition of crypto/sha256 import.

This import is necessary for handling digest size checks in the SettleSessionAccounting function.


32-32: Approved changes to function signatures for better error handling.

The modifications to include error returns in these functions are a good practice, ensuring that errors are not silently ignored and can be handled upstream.

Also applies to: 111-111


40-45: Approved robust error handling and logging enhancements.

The addition of detailed error checks and logging improves the function's robustness and traceability, which is critical in a financial context.

Also applies to: 77-88


213-228: Approved changes to getCoinFromComputeUnits with a suggestion for further validation.

The changes to return an error and the new computation logic are good. However, ensure that the mathematical operations handle edge cases like overflow or underflow.

x/tokenomics/keeper/settle_session_accounting.go Outdated Show resolved Hide resolved
@bryanchriswhite bryanchriswhite added push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 18, 2024
Copy link

The image is going to be pushed after the next commit.

You can use make trigger_ci to push an empty commit.

If you also want to run E2E tests, please add devnet-test-e2e label.

Copy link

The CI will now also run the e2e tests on devnet, which increases the time it takes to complete all CI checks.

You may need to run make trigger_ci to submit an empty commit that'll trigger the tests.

GCP workloads (requires changing the namespace to 672)
Grafana network dashboard for devnet-issue-{issue-id}

Copy link

@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.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
pkg/crypto/protocol/hasher.go (1)

1-12: Consider exporting trieRootMetadataSize if needed.

The TODO comment suggests exporting trieRootMetadataSize from the SMT package. Ensure this is addressed if required.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 91f9cd9 and b5d1db8.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (8)
  • pkg/client/supplier/client_test.go (3 hunks)
  • pkg/crypto/protocol/hasher.go (1 hunks)
  • pkg/relayer/session/sessiontree.go (2 hunks)
  • tests/integration/tokenomics/relay_mining_difficulty_test.go (2 hunks)
  • testutil/proof/fixture_generators.go (2 hunks)
  • x/application/keeper/auto_undelegate.go (1 hunks)
  • x/tokenomics/keeper/settle_session_accounting.go (6 hunks)
  • x/tokenomics/keeper/settle_session_accounting_test.go (4 hunks)
Files skipped from review due to trivial changes (1)
  • x/application/keeper/auto_undelegate.go
Files skipped from review as they are similar to previous changes (3)
  • testutil/proof/fixture_generators.go
  • x/tokenomics/keeper/settle_session_accounting.go
  • x/tokenomics/keeper/settle_session_accounting_test.go
Additional comments not posted (5)
pkg/client/supplier/client_test.go (2)

16-16: LGTM!

The import statement for protocol from pkg/crypto is correct and necessary.


184-184: LGTM!

The change from sha256.New() to protocol.NewTrieHasher() is correct and consistent with the new implementation.

pkg/relayer/session/sessiontree.go (2)

14-14: LGTM!

The import statement for protocol from github.com/pokt-network/poktroll/pkg/crypto/protocol is correct and necessary.


89-89: LGTM!

The change from sha256.New() to protocol.NewTrieHasher() is correct and consistent with the new implementation.

tests/integration/tokenomics/relay_mining_difficulty_test.go (1)

195-195: LGTM!

The change from sha256.New() to protocol.NewTrieHasher() is correct and consistent with the new implementation.

Copy link
Contributor

@red-0ne red-0ne left a comment

Choose a reason for hiding this comment

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

Smol NIT, but LGTM

testutil/proof/fixture_generators.go Outdated Show resolved Hide resolved
Co-authored-by: Redouane Lakrache <[email protected]>
Copy link

@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.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b5d1db8 and 902f09b.

Files selected for processing (1)
  • testutil/proof/fixture_generators.go (2 hunks)
Additional comments not posted (3)
testutil/proof/fixture_generators.go (3)

56-58: LGTM!

The changes align with the new requirements and the function looks correct.


64-73: LGTM!

The changes align with the new requirements and the function looks correct.


75-88: LGTM!

The function looks correct and the implementation is straightforward.

Comment on lines +56 to +58
func SmstRootWithSum(sum uint64) smt.MerkleSumRoot {
root := [protocol.TrieRootSize]byte{}
return encodeSum(root, sum)
Copy link

Choose a reason for hiding this comment

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

Address TODO comments.

The TODO comments indicate potential tech debt and mainnet considerations.

Do you want me to help address these TODO comments or open a GitHub issue to track them?

Comment on lines +75 to +88
// encodeSum returns a copy of the given root, binary encodes the given sum,
// and stores the encoded sum in the root copy.
func encodeSum(r [protocol.TrieRootSize]byte, sum uint64) smt.MerkleSumRoot {
root := make([]byte, protocol.TrieRootSize)
copy(root, r[:])

// Insert the sum into the root hash
binary.BigEndian.PutUint64(root[protocol.TrieHasherSize:], sum)
// Insert the count into the root hash
// TODO_TECHDEBT: This is a hard-coded count of 1, but could be a parameter.
// TODO_TECHDEBT: We are assuming the sum takes up 8 bytes.
binary.BigEndian.PutUint64(root[smt.SmtRootSizeBytes+8:], 1)
return smt.MerkleRoot(root[:])
binary.BigEndian.PutUint64(root[protocol.TrieHasherSize+8:], 1)

return root
Copy link

Choose a reason for hiding this comment

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

Address TODO comments.

The TODO comments indicate potential tech debt.

Do you want me to help address these TODO comments or open a GitHub issue to track them?

@bryanchriswhite bryanchriswhite merged commit 1d75111 into main Jul 19, 2024
10 checks passed
@bryanchriswhite bryanchriswhite deleted the issues/584/refactor/smt-root branch July 19, 2024 10:16
bryanchriswhite added a commit that referenced this pull request Jul 19, 2024
bryanchriswhite added a commit that referenced this pull request Jul 19, 2024
…get-hash

* pokt/main:
  [TODO] chore: update `smt.MerkleRoot#Sum()` error handling (#672)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working code health Cleans up some code devnet devnet-test-e2e push-image CI related - pushes images to ghcr.io
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

[TODO] @Olshansk's (and now @Bryan's) Blocker TODOs
3 participants