Skip to content

chore: audit epoch#24610

Merged
aljo242 merged 7 commits into
mainfrom
chore/audit-epoch
Apr 29, 2025
Merged

chore: audit epoch#24610
aljo242 merged 7 commits into
mainfrom
chore/audit-epoch

Conversation

@aljo242
Copy link
Copy Markdown
Contributor

@aljo242 aljo242 commented Apr 29, 2025

addresses OS-CDK-ADV-01

Summary by CodeRabbit

  • Bug Fixes
    • Improved accuracy in epoch handling by ensuring epochs do not start prematurely and by preventing block calculations before an epoch begins.
  • Documentation
    • Updated documentation to accurately describe the genesis state function for epochs.
  • Chores
    • Removed an unused constant related to default indexing.
  • Tests
    • Adjusted test expectations to reflect corrected epoch start behavior and improved test organization.

aljo242 added 2 commits April 29, 2025 12:26
@ironbird-prod
Copy link
Copy Markdown

ironbird-prod Bot commented Apr 29, 2025

Ironbird - launch a network To use Ironbird, you can use the following commands:
  • /ironbird start OR /ironbird start --load-test-config= - Launch a testnet with the specified chain and load test configuration.
  • /ironbird chains - List of chain images that ironbird can use to spin-up testnet
  • /ironbird loadtests - List of load test modes that ironbird can run against testnet
Custom Load Test Configuration You can provide a custom load test configuration using the `--load-test-config=` flag:
/ironbird start cosmos --load-test-config={
  "block_gas_limit_target": 0.75,
  "num_of_blocks": 50,
  "msgs": [
    {"weight": 0.3, "type": "MsgSend"},
    {"weight": 0.3, "type": "MsgMultiSend"},
	{"weight": 0.4, "type": "MsgArr", "ContainedType": "MsgSend", "NumMsgs": 3300}
  ]
}

Use /ironbird loadtests to see more examples.

aljo242 added 2 commits April 29, 2025 12:47
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2025

📝 Walkthrough

Walkthrough

The changes update the epochs module to use direct calls to obtain the current block time and height, replacing previous usage of the block header object. Logic related to epoch start timing and block height assignment is adjusted to prevent premature setting of epoch start height. Tests are updated to reflect the new logic, specifically the expected start height in certain cases. Additionally, a redundant constant is removed from the genesis types, and comments are corrected to accurately describe function behavior. No changes are made to the signatures of exported functions, except for the removal of an unused constant.

Changes

File(s) Change Summary
x/epochs/keeper/abci.go Replaced usage of ctx.BlockHeader() with direct calls to ctx.BlockTime() and ctx.BlockHeight(). Updated logic to use these values for epoch timing and height checks.
x/epochs/keeper/abci_test.go Grouped related constants in a single block. Updated expected CurrentEpochStartHeight from 1 to 0 in a specific test case to reflect new logic.
x/epochs/keeper/epoch.go Modified AddEpochInfo to set CurrentEpochStartHeight only if appropriate. Changed slice initialization in AllEpochInfos. Added error handling in NumBlocksSinceEpochStart for pre-epoch cases.
x/epochs/types/genesis.go Removed the DefaultIndex constant. Updated function comment for DefaultGenesis to refer to Epochs instead of Capability.

Sequence Diagram(s)

sequenceDiagram
    participant BlockContext
    participant EpochKeeper

    BlockContext->>EpochKeeper: Get current block time and height
    EpochKeeper->>EpochKeeper: Check if epoch start time is after block time
    EpochKeeper->>EpochKeeper: If not, set CurrentEpochStartHeight
    EpochKeeper->>EpochKeeper: If block time before epoch start, prevent premature start
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f9e0f72 and 7586c1f.

📒 Files selected for processing (4)
  • x/epochs/keeper/abci.go (1 hunks)
  • x/epochs/keeper/abci_test.go (2 hunks)
  • x/epochs/keeper/epoch.go (2 hunks)
  • x/epochs/types/genesis.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
x/epochs/keeper/epoch.go (2)
testutil/simsx/context.go (1)
  • BlockTime (11-14)
x/epochs/keeper/keeper.go (1)
  • Keeper (11-19)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test-e2e
  • GitHub Check: Gosec
  • GitHub Check: Summary
🔇 Additional comments (10)
x/epochs/keeper/abci.go (4)

16-17: Improved accessor pattern for block data

This change replaces retrieving the block header and then accessing its fields with direct calls to the context methods. This approach is more direct and explicit, making the code cleaner and potentially slightly more efficient by avoiding the creation of an intermediate header object.


24-24: Block time check refactoring aligns with new pattern

The condition has been updated to use the previously stored blockTime variable instead of accessing time through the header. This maintains the same logic while making the code more consistent.


31-31: Consistent use of blockTime variable

The epoch start condition now uses the blockTime variable consistently with the other changes. The logic remains the same, but the implementation is more direct.


36-36: Setting epoch start height using direct block height access

Using the blockHeight variable directly is consistent with the other changes and follows the same pattern of accessing block information directly from the context.

x/epochs/keeper/abci_test.go (2)

18-23: Improved code organization with const block

Grouping related constants (defaultIdentifier, defaultDuration, and eps) into a single const block improves code organization and readability.


74-75: Fixed expected behavior for future epoch start

The test was updated to correctly reflect that an epoch with a future start time should have a CurrentEpochStartHeight of 0 rather than 1. This change aligns with the implementation in x/epochs/keeper/epoch.go where the start height is only set when the current time is not before the epoch start time.

The added comment also improves code clarity by explaining the reasoning behind the expected value.

x/epochs/types/genesis.go (1)

12-12: Fixed incorrect documentation comment

The comment now correctly states that the function returns the default Epochs genesis state instead of incorrectly referring to the Capability genesis state. This improves documentation accuracy.

x/epochs/keeper/epoch.go (3)

36-36: Added condition to prevent premature start height assignment

This change improves the logic by only setting CurrentEpochStartHeight when the epoch's StartTime is not after the current block time. This prevents setting the start height prematurely for epochs that are scheduled to start in the future.

This aligns with the test change in abci_test.go where the expected CurrentEpochStartHeight was changed from 1 to 0 for a future epoch.


44-44: Simplified slice initialization

Changed from a literal empty slice epochs := []types.EpochInfo{} to a nil slice declaration var epochs []types.EpochInfo. Both are functionally equivalent when used with append(), but the nil slice is more idiomatic in Go.


65-67: Added validation for epoch start time

This improvement adds a check to prevent calculating blocks since epoch start before the epoch has actually begun. This handles a previously unhandled edge case and provides a more informative error message.

This change complements other time-related checks in the codebase and ensures consistent behavior.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@aljo242 aljo242 added this pull request to the merge queue Apr 29, 2025
Merged via the queue into main with commit 1732077 Apr 29, 2025
46 checks passed
@aljo242 aljo242 deleted the chore/audit-epoch branch April 29, 2025 18:15
mergify Bot pushed a commit that referenced this pull request Apr 29, 2025
(cherry picked from commit 1732077)
@aljo242
Copy link
Copy Markdown
Contributor Author

aljo242 commented Apr 29, 2025

@Mergifyio backport release/v0.53.x

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 29, 2025

backport release/v0.53.x

✅ Backports have been created

Details

warpbuild-benchmark-bot Bot added a commit to WarpBuilds/cosmos-sdk that referenced this pull request Apr 29, 2025
aljo242 pushed a commit that referenced this pull request Apr 29, 2025
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants