Skip to content

fix(pallet-tfgrid): enforce node_certification in farming policy limits#1087

Open
sameh-farouk wants to merge 1 commit intodevelopmentfrom
fix/farming-policy-node-certification
Open

fix(pallet-tfgrid): enforce node_certification in farming policy limits#1087
sameh-farouk wants to merge 1 commit intodevelopmentfrom
fix/farming-policy-node-certification

Conversation

@sameh-farouk
Copy link
Copy Markdown
Member

@sameh-farouk sameh-farouk commented Apr 9, 2026

Summary

Enforces the node_certification field in FarmingPolicyLimit — 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:

Certification. If set, only certified nodes can get this policy. Non certified nodes get a default policy.

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:

  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() 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:

    • 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
  • Replace get_default_farming_policy() with get_default_farming_policy_for(node, farm) that filters by both node_certification and farm_certification
  • This affects all 5 fallback paths: certification check (new), end expired, cu exceeded, su exceeded, node_count exhausted

Tests (115 pass, 0 fail)

3 new test cases:

  • diy_node_gets_default_policy_when_limit_requires_certification — Diy node on Gold farm with node_certification=true gets 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_requirednode_certification=false doesn'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.

  • 6768, 6968 and 6656 Last seen within days
  • 7873 last seen 243 days ago

This fix only affects future policy assignments. Existing nodes need a storage migration or manual set_node_certification call to trigger re-evaluation through get_farming_policy.

Fixes #429

## 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
@sameh-farouk sameh-farouk requested a review from LeeSmet as a code owner April 9, 2026 16:23
@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · -2 duplication

Metric Results
Complexity 4
Duplication -2

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

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.

Handle node_certification filter in farming policy limits

1 participant