Skip to content

Change time.now to ctx.BlockTime and remove genesis time validations#190

Merged
jhelison merged 4 commits into
mainfrom
fix/change-time
Dec 30, 2025
Merged

Change time.now to ctx.BlockTime and remove genesis time validations#190
jhelison merged 4 commits into
mainfrom
fix/change-time

Conversation

@jhelison

Copy link
Copy Markdown
Contributor

Description

This fixes issue #189

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Documentation (updates documentation on the project)
  • chore (Updates on dependencies, gitignore, etc)
  • test (For updates on tests)

How Has This Been Tested?

Related tests updated

Copilot AI review requested due to automatic review settings December 30, 2025 19:09
@coderabbitai

coderabbitai Bot commented Dec 30, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The diff refactors context and time handling in the x/rewards module: msg_server now unwraps the Cosmos SDK context via sdk.UnwrapSDKContext(ctx) and passes that sdk.Context to downstream calls. Validation functions were changed from context.Context to sdk.Context and now use ctx.BlockTime() instead of time.Now() for time comparisons. Two genesis validations were removed from ReleaseSchedule.ValidateGenesis (the EndTime past-check and LastReleaseTime future-check), and the corresponding tests were deleted. No exported/public function signatures were added or removed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main changes: replacing time.Now() with ctx.BlockTime() and removing genesis time validations, which aligns perfectly with the changeset.
Description check ✅ Passed The description is directly related to the changeset, referencing issue #189, identifying it as a bug fix, and noting that related tests were updated, which matches the PR objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6629852 and e7f114f.

📒 Files selected for processing (1)
  • CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: golangci-lint
  • GitHub Check: Analyze
  • GitHub Check: tests
  • GitHub Check: test-e2e
  • GitHub Check: liveness-test

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
x/rewards/keeper/msg_server.go (1)

80-90: Correct usage of SDK context throughout the method.

The SDK context is properly passed to all downstream calls. The changes align correctly with the updated validation function signatures.

Consider updating fundsAvailable signature for consistency

While fundsAvailable (line 49 in validation.go) currently doesn't require sdk.Context for its operations, updating its signature to match validateSchedule would improve consistency and future-proof the codebase:

-func (k Keeper) fundsAvailable(ctx context.Context, amount sdk.Coin) error {
+func (k Keeper) fundsAvailable(ctx sdk.Context, amount sdk.Coin) error {

This is purely optional—the current implementation works correctly since sdk.Context implements context.Context.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 359cfae and 87423c0.

📒 Files selected for processing (4)
  • x/rewards/keeper/msg_server.go
  • x/rewards/keeper/validation.go
  • x/rewards/types/release_schedule.go
  • x/rewards/types/release_schedule_test.go
💤 Files with no reviewable changes (2)
  • x/rewards/types/release_schedule.go
  • x/rewards/types/release_schedule_test.go
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: watarus
Repo: KiiChain/kiichain PR: 111
File: wasmbinding/tokenfactory/queries_test.go:185-192
Timestamp: 2025-08-23T19:37:59.730Z
Learning: In the current Cosmos SDK version used by the KiiChain codebase, sdk.Context directly implements the context.Context interface and can be passed directly to query methods without needing sdk.WrapSDKContext() wrapper. The test pattern in wasmbinding/tokenfactory/queries_test.go demonstrates this by passing ctx directly to query plugin methods.
Learnt from: watarus
Repo: KiiChain/kiichain PR: 111
File: wasmbinding/tokenfactory/queries_test.go:185-192
Timestamp: 2025-08-23T19:37:59.730Z
Learning: In the KiiChain codebase using the current Cosmos SDK version, sdk.Context directly implements the context.Context interface and should be passed directly to query methods without using sdk.WrapSDKContext() wrapper. The test pattern in wasmbinding/tokenfactory/queries_test.go demonstrates this by passing ctx directly to query plugin methods like GetTokenfactoryDenomAdmin.
📚 Learning: 2025-08-23T19:37:59.730Z
Learnt from: watarus
Repo: KiiChain/kiichain PR: 111
File: wasmbinding/tokenfactory/queries_test.go:185-192
Timestamp: 2025-08-23T19:37:59.730Z
Learning: In the current Cosmos SDK version used by the KiiChain codebase, sdk.Context directly implements the context.Context interface and can be passed directly to query methods without needing sdk.WrapSDKContext() wrapper. The test pattern in wasmbinding/tokenfactory/queries_test.go demonstrates this by passing ctx directly to query plugin methods.

Applied to files:

  • x/rewards/keeper/msg_server.go
📚 Learning: 2025-08-23T19:37:59.730Z
Learnt from: watarus
Repo: KiiChain/kiichain PR: 111
File: wasmbinding/tokenfactory/queries_test.go:185-192
Timestamp: 2025-08-23T19:37:59.730Z
Learning: In the KiiChain codebase using the current Cosmos SDK version, sdk.Context directly implements the context.Context interface and should be passed directly to query methods without using sdk.WrapSDKContext() wrapper. The test pattern in wasmbinding/tokenfactory/queries_test.go demonstrates this by passing ctx directly to query plugin methods like GetTokenfactoryDenomAdmin.

Applied to files:

  • x/rewards/keeper/msg_server.go
🧬 Code graph analysis (1)
x/rewards/keeper/msg_server.go (2)
x/rewards/keeper/keeper.go (1)
  • Keeper (18-32)
x/rewards/types/types.pb.go (3)
  • ReleaseSchedule (34-45)
  • ReleaseSchedule (49-49)
  • ReleaseSchedule (50-52)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: CodeQL analysis (go)
  • GitHub Check: Agent
  • GitHub Check: tests
  • GitHub Check: golangci-lint
  • GitHub Check: test-e2e
  • GitHub Check: Analyze
  • GitHub Check: liveness-test
🔇 Additional comments (3)
x/rewards/keeper/msg_server.go (1)

70-71: LGTM! Context unwrapping is correctly implemented.

The extraction of the SDK context is necessary for accessing BlockTime() in the validation functions. This follows the standard Cosmos SDK pattern for message servers that need SDK-specific features.

x/rewards/keeper/validation.go (2)

40-46: Excellent fix: Using BlockTime for deterministic validation.

Replacing time.Now() with ctx.BlockTime() is critical for blockchain consensus. Using system time would cause validators to get different results, leading to consensus failures. Block time ensures all validators agree on the same time reference.


66-127: Correctly updated for context-aware time validation.

The signature change to sdk.Context and the use of ctx.BlockTime() throughout the function ensure deterministic validation. All call sites are properly updated, and the validation logic remains sound.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #189 by replacing the use of time.Now() with blockchain-aware ctx.BlockTime() for time validations, and removes inappropriate time validations from genesis state validation.

Key changes:

  • Replaced time.Now() with ctx.BlockTime() for deterministic, blockchain-based time checks
  • Removed genesis-time validations that compared against current time (which is inappropriate for genesis state)
  • Updated function signatures to use sdk.Context where blockchain time access is needed

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

File Description
x/rewards/types/release_schedule.go Removed genesis validations for EndTime and LastReleaseTime that incorrectly compared against current time
x/rewards/types/release_schedule_test.go Removed test cases for the deleted genesis time validations
x/rewards/keeper/validation.go Updated validateEndTime to accept sdk.Context and use ctx.BlockTime(); updated validateSchedule signature and moved currentTime declaration closer to usage
x/rewards/keeper/msg_server.go Added SDK context unwrapping in ChangeSchedule to properly pass blockchain context to validation functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Dec 30, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
x/rewards/keeper/msg_server.go 75.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@jhelison jhelison merged commit 0137e1d into main Dec 30, 2025
7 checks passed
@jhelison jhelison deleted the fix/change-time branch December 30, 2025 20:05
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.

3 participants