fix(pallet-tfgrid): enforce node_certification in farming policy limits#1087
Open
sameh-farouk wants to merge 1 commit intodevelopmentfrom
Open
fix(pallet-tfgrid): enforce node_certification in farming policy limits#1087sameh-farouk wants to merge 1 commit intodevelopmentfrom
sameh-farouk wants to merge 1 commit intodevelopmentfrom
Conversation
## Problem The FarmingPolicyLimit.node_certification field was never enforced in get_farming_policy(). Per the spec (docs/misc/minimal_DAO.md): > Certification. If set, only certified nodes can get this policy. > Non certified nodes get a default policy. A Diy node on a farm with node_certification=true in its limits would receive the certified-only policy instead of falling back to a default. On mainnet, farm 2995 (GoldAndGreen) has 4 Diy nodes (2 currently online) earning certified-level farming rewards on policy 3. ## Root Cause Two issues: 1. No certification check in the FarmingPolicyLimit path of get_farming_policy() — limits only checked end, cu, su, node_count. 2. get_default_farming_policy() (called when limits are exhausted) ignored node/farm certification entirely — it just picked the highest-ranked default policy regardless of the node's actual certification. Per the spec, default policy selection should follow: - First: highest farm cert + certified nodes - Then: highest farm cert + non-certified nodes - Then: no farm cert + certified nodes - Last: no certification at all ## Fix - Add node_certification check at the start of the limits path: Diy nodes on certified-only farms fall back to a matching default. - Replace get_default_farming_policy() with get_default_farming_policy_for() that filters by both node_certification and farm_certification. This affects all 4 existing fallback paths (end expired, cu exceeded, su exceeded, node_count exhausted) plus the new certification check. ## Tests - diy_node_gets_default_policy_when_limit_requires_certification: Diy node on Gold farm with node_certification=true gets policy 1 (Diy+Gold default) instead of policy 3 (Certified+Gold limited) - certified_node_gets_limited_policy_when_limit_requires_certification: Certified node correctly receives the limited policy - diy_node_gets_limited_policy_when_no_certification_required: Diy node gets limited policy when node_certification=false ## Migration Note This fix only affects future policy assignments. The 4 Diy nodes on mainnet farm 2995 currently assigned policy 3 need a migration or manual set_node_certification call to trigger re-evaluation. Fixes #429
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | -2 |
TIP This summary will be updated as you push new changes. Give us feedback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enforces the
node_certificationfield inFarmingPolicyLimit— Diy nodes on farms with certified-only policy limits now correctly fall back to a matching default policy instead of receiving the certified-only policy.Also fixes
get_default_farming_policy()which ignored node/farm certification entirely when selecting fallback defaults.Problem
Per the spec:
This was never enforced. On mainnet, farm 2995 (GoldAndGreen) has 4 Diy nodes (2 currently online) earning certified-level farming rewards on policy 3 instead of a Diy default policy.
Root Cause
Two issues in
pricing.rs:No certification check in the
FarmingPolicyLimitpath ofget_farming_policy()— limits only checkedend,cu,su,node_countget_default_farming_policy()ignored certification — when limits were exhausted (or now when certification fails), it picked the highest-ranked default regardless of the node's actual certification. Per the spec, default selection should consider both node and farm certification:Fix
node_certificationcheck at the start of the limits pathget_default_farming_policy()withget_default_farming_policy_for(node, farm)that filters by bothnode_certificationandfarm_certificationTests (115 pass, 0 fail)
3 new test cases:
diy_node_gets_default_policy_when_limit_requires_certification— Diy node on Gold farm withnode_certification=truegets policy 1 (Diy+Gold) instead of policy 3 (Certified+Gold)certified_node_gets_limited_policy_when_limit_requires_certification— Certified node correctly receives the limited policy (positive case)diy_node_gets_limited_policy_when_no_certification_required—node_certification=falsedoesn't filter (no restriction)All 112 existing tests continue to pass.
Mainnet Impact
Currently affected: 4 Diy nodes on farm 2995 affected by the bug (wrong policy assigned) with
farming_policy_id: 3(certified-only policy, no end date) But only 3 are currently earning wrong rewards.This fix only affects future policy assignments. Existing nodes need a storage migration or manual
set_node_certificationcall to trigger re-evaluation throughget_farming_policy.Fixes #429