Skip to content

Smart Contact Access Control#188

Merged
Nitya-003 merged 8 commits intoNitya-003:mainfrom
Aditya8369:180
Mar 8, 2026
Merged

Smart Contact Access Control#188
Nitya-003 merged 8 commits intoNitya-003:mainfrom
Aditya8369:180

Conversation

@Aditya8369
Copy link
Contributor

Completed: Verify fixes with error check (7/7)

No errors. Here's a summary of all four fixes applied to CropChain.sol:


Fix 1 — transferOwnership: revoke/grant DEFAULT_ADMIN_ROLE atomically

Before: Transferred the owner variable but left DEFAULT_ADMIN_ROLE held by the previous owner forever, giving them permanent backdoor access to grantStakeholderRole, transferOwnership, etc.
After: _revokeRole(DEFAULT_ADMIN_ROLE, previousOwner) + _grantRole(DEFAULT_ADMIN_ROLE, newOwner) are called atomically. The previous owner's legacy roles[] entry is also cleared to None.


Fix 2 — setRole: keep OZ AccessControl in sync with the legacy roles[] map

Before: Setting roles[user] = ActorRole.Farmer had no effect on FARMER_ROLE in OZ AccessControl, so the user would fail createBatch (onlyRole(FARMER_ROLE)) and all _canUpdateStage checks.
After: Old OZ role is revoked and the new one is granted atomically. Also guards against assigning ActorRole.Admin through this function (must go through transferOwnership), and protects the owner's own role.


Fix 3 — cancelListing: replace stale owner reference with hasRole(DEFAULT_ADMIN_ROLE, ...)

Before: msg.sender == owner — after ownership transfer the old privileged account (who kept DEFAULT_ADMIN_ROLE) could no longer cancel listings; the new owner could.
After: hasRole(DEFAULT_ADMIN_ROLE, msg.sender) — consistent with the rest of the admin-guard pattern and always reflects who is actually privileged.


Fix 4 — grantStakeholderRole: sync legacy roles[] mapping when granting OZ roles

Before: A user granted MANDI_ROLE via this function would pass onlyRole(MANDI_ROLE) and _canUpdateStage, but would fail onlyAuthorized (legacy map check) and the createListing Mandi check.
After: The legacy roles[] entry is set alongside _grantRole, and RoleUpdated is emitted for full observability.

closes #180

Copilot AI review requested due to automatic review settings March 8, 2026 10:04
@netlify
Copy link

netlify bot commented Mar 8, 2026

Deploy Preview for roaring-ganache-c7465f failed. Why did it fail? →

Name Link
🔨 Latest commit a82bf56
🔍 Latest deploy log https://app.netlify.com/projects/roaring-ganache-c7465f/deploys/69ad66d3ae675a0008d37358

Copy link
Contributor

Copilot AI left a comment

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 combines (a) smart-contract access control hardening in CropChain.sol (role/owner synchronization) with (b) an end-to-end “Smart Planting” crop recommendation feature spanning a new Python ML microservice, a new backend API route, and a new frontend page + navigation.

Changes:

  • Harden CropChain.sol ownership/role transitions by syncing legacy roles[] with OZ AccessControl and updating admin checks.
  • Add a Python Flask ML microservice (training + /predict + Dockerization) and wire it into the Node backend via /api/recommend.
  • Add a new React page + service for crop recommendations and link it into the app route/header.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
contracts/CropChain.sol Sync legacy roles[] mapping with OZ AccessControl in setRole / transferOwnership; admin check update in cancelListing; legacy sync in grantStakeholderRole.
backend/server.js Adds POST /api/recommend endpoint (validated + rate-limited) that calls the ML microservice.
backend/services/cropRecommendationService.js Adds an axios client for calling the ML microservice (/predict, /health).
backend/validations/cropRecommendationSchema.js Adds Joi validation for recommendation inputs.
ml-service/app.py New Flask API exposing /health and /predict and loading/training the model.
ml-service/train.py New training script to generate synthetic data and persist model.joblib.
ml-service/requirements.txt Python dependencies for the microservice.
ml-service/Dockerfile Containerizes the ML service and pre-trains the model at build time.
docker-compose.yml Adds the ml-service container and wires backend env/dependencies.
src/services/cropRecommendationService.ts Frontend fetch wrapper for POST /api/recommend.
src/pages/CropRecommendation.tsx New UI page for input sliders and displaying recommendation results.
src/components/Header.tsx Adds nav link to the new crop recommendation page.
src/App.tsx Adds route for /crop-recommendation.

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

Aditya8369 and others added 6 commits March 8, 2026 16:27
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Aditya8369
Copy link
Contributor Author

@vedhapprakashni solved the suggestions arised

@Nitya-003 Nitya-003 added bug Something isn't working apertre3.0 Easy Easy to solve. labels Mar 8, 2026
Copy link
Owner

@Nitya-003 Nitya-003 left a comment

Choose a reason for hiding this comment

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

@Aditya8369 Resolve the conflicts please.

Image

@Aditya8369
Copy link
Contributor Author

@Nitya-003 resolved

@Nitya-003 Nitya-003 merged commit 93d06f1 into Nitya-003:main Mar 8, 2026
0 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apertre3.0 bug Something isn't working Easy Easy to solve.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] Smart Contract Access Control Audit

3 participants