Audit name:

[SCA] CratD2C / StakeManager + Vesting / Oct2024

Date:

Oct 28, 2024

Table of Content

Introduction

Audit Summary

System Overview

Potential Risks

Findings

Appendix 1. Definitions

Appendix 2. Scope

Appendix 3. Additional Valuables

Disclaimer

Introduction

We express our gratitude to the CratD2C team for the collaborative engagement that enabled the execution of this Smart Contract Security Assessment.

CratD2C is a blockchain platform utilizing Delegated Proof of Stake (DPoS) and Layer-1 architecture to enhance security, speed, and scalability. Its ecosystem supports e-commerce, real estate, and luxury services through decentralized applications and a universal payment gateway.

  • Document

    Name
    Smart Contract Code Review and Security Analysis Report for CratD2C
    Audited By
    Niccolò Pozzolini, Kornel Światłowski
    Approved By
    Grzegorz Trawinski
    Changelog
    17/10/2024 - Preliminary Report; 28/10/2024 - Second Review; 20/12/2024 Final Report
    Platform
    CratD2C
    Language
    Solidity
    Tags
    Vesting, Staking

Audit Summary

10Total Findings
10Resolved
0Accepted
0Mitigated

The system users should acknowledge all the risks summed up in the risks section of the report

Documentation quality

  • Functional requirements are complete.

  • Technical description is detailed.

Code quality

  • The development environment is configured.

Test coverage

Code coverage of the project is 99.18% (branch coverage).

  • Deployment and basic user interactions are covered with tests.

  • Not every negative cases coverage are covered.

System Overview

The CRATVesting contract is a token vesting contract designed to distribute a predefined supply of tokens over a set period. It allows administrators to start the distribution to various recipients (allocators) with predefined schedules for each, outlining how and when tokens are unlocked. The contract uses a time-based vesting schedule that spans from 2024 to 2038, releasing tokens incrementally to the allocators. Administrators can claim unlocked tokens for the recipients, ensuring a controlled release according to the vesting rules.

The CRATStakeManager contract is responsible for managing staking, slashing, and reward distribution among validators and their delegators. It includes functionalities for depositing funds as a validator or delegator, distributing rewards, and slashing validators when necessary. Validators and delegators can claim or restake rewards, withdraw deposits.

Privileged roles

The CRATVesting contract uses the AccessControl library from OpenZeppelin to restrict access to some functions. The only role defined in the contract is DEFAULT_ADMIN_ROLE, addresses with the role can:

  • Partially claim available tokens.

  • Claim all available tokens.

The CRATVesting contract uses the AccessControlUpgradeable library from OpenZeppelin to restrict access to some functions. The roles defined in this contract are: DEFAULT_ADMIN_ROLE, DISTRIBUTOR_ROLE and SWAP_ROLE.

Addresses with the DEFAULT_ADMIN_ROLE can:

  • Change slash receiver address.

  • Change validators limit.

  • Change validators and delegators withdrawal cooldown.

  • Change validators and delegators minimum amount to deposit.

  • Change validators token amount to slash.

  • Change delegators percentage to slash.

  • Change validators and delegators fixed APR.

  • Change validators and delegators claim cooldown.

  • Withdraw excess reward tokens from the contract.

Addresses with the DISTRIBUTOR_ROLE can:

  • Distribute rewards to validators.

  • Slash validators.

Addresses with the SWAP_ROLE can:

  • Make deposit for exact user as validator.

Potential Risks

Dynamic Array Iteration Gas Limit Risks: The project iterates over large dynamic arrays, which leads to excessive gas costs, risking denial of service due to out-of-gas errors, directly impacting contract usability and reliability.

Owner's Unrestricted State Modification: The absence of restrictions on state variable modifications by the owner leads to arbitrary changes, affecting contract integrity and user trust.

Absence of Time-lock Mechanisms for Critical Operations: Without time-locks on critical operations, there is no buffer to review or revert potentially harmful actions, increasing the risk of rapid exploitation and irreversible changes.

Administrative Key Control Risks: The digital contract architecture relies on administrative keys for critical operations. Centralized control over these keys presents a significant security risk, as compromise or misuse can lead to unauthorized actions or loss of funds.

Penalizing Slashing Mechanism: If a validator gets slashed twice in 90 days, all of his fixed APR reward accumulated from the last shashing is withdrawn from his deposit amount as a penalty. The 90 days threshold is variable and can be adjusted by the team.

Findings

Code
Title
Status
Severity
F-2024-7875
[DualDefense] Potential Denial of Service in Validator Withdrawals
Fixed

High
F-2024-6642
Potential Denial of Service in slash Function Due to High Gas Consumption
Fixed

Medium
F-2024-6643
Potential Funds Lock Due to Insufficient Fixed Rewards Balance
Fixed

Low
F-2024-6656
Missing _disableInitializers() in Upgradable Contract Constructor
Fixed

Observation
F-2024-6653
Unoptimized Storage Access
Fixed

Observation
F-2024-6650
Missing Events For Key Actions
Fixed

Observation
F-2024-6648
Gas Inefficiency Due to Missing Usage of Solidity Custom Errors
Fixed

Observation
F-2024-6644
Missing Validator Parameter in DelegatorWithdrawed Event
Fixed

Observation
F-2024-6641
Redundant Code in Conditional Branches Reduces Readability
Fixed

Observation
F-2024-6638
Lack of Upper Bound on Cooldown Values for Claim and Withdraw Actions
Fixed

Observation
1-10 of 10 findings

Appendix 1. Definitions

Severities

When auditing smart contracts, Hacken is using a risk-based approach that considers Likelihood, Impact, Exploitability and Complexity metrics to evaluate findings and score severities.

Reference on how risk scoring is done is available through the repository in our Github organization:

  • Severity

    Critical

    Description

    Critical vulnerabilities are usually straightforward to exploit and can lead to the loss of user funds or contract state manipulation.

    Severity

    High

    Description

    High vulnerabilities are usually harder to exploit, requiring specific conditions, or have a more limited scope, but can still lead to the loss of user funds or contract state manipulation.

    Severity

    Medium

    Description

    Medium vulnerabilities are usually limited to state manipulations and, in most cases, cannot lead to asset loss. Contradictions and requirements violations. Major deviations from best practices are also in this category.

    Severity

    Low

    Description

    Major deviations from best practices or major Gas inefficiency. These issues will not have a significant impact on code execution.

Potential Risks

The "Potential Risks" section identifies issues that are not direct security vulnerabilities but could still affect the project’s performance, reliability, or user trust. These risks arise from design choices, architectural decisions, or operational practices that, while not immediately exploitable, may lead to problems under certain conditions. Additionally, potential risks can impact the quality of the audit itself, as they may involve external factors or components beyond the scope of the audit, leading to incomplete assessments or oversight of key areas. This section aims to provide a broader perspective on factors that could affect the project's long-term security, functionality, and the comprehensiveness of the audit findings.

Appendix 2. Scope

The scope of the project includes the following smart contracts from the provided repository:

Assets in Scope

CRATStakeManager.sol - CRATStakeManager.sol
CRATVesting.sol - CRATVesting.sol

Appendix 3. Additional Valuables

Verification of System Invariants

During the audit of CratStakeManager, Hacken followed its methodology by performing fuzz-testing on the project's main functions. Medusa a tool used for fuzz-testing, was employed to check how the protocol behaves under various inputs. Due to the complex and dynamic interactions within the protocol, unexpected edge cases might arise. Therefore, it was important to use fuzz-testing to ensure that several system invariants hold true in all situations.

Fuzz-testing allows the input of many random data points into the system, helping to identify issues that regular testing might miss. A specific Echidna fuzzing suite was prepared for this task, and throughout the assessment, 9 invariants were tested over 60,000,000 runs. This thorough testing ensured that the system works correctly even with unexpected or unusual inputs.

  • Invariant

    totalValidatorsPool is aligned with the sum of validators' deposited amounts

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    totalDelegatorsPool is aligned with the sum of delegators' deposited amounts

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    stoppedDelegatorsPool is aligned with the sum of stopped delegators' deposited amounts

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    Validators upper limit is always satisfied

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    Minimum deposit amount is always satisfied for validators

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    Delegators' structures always contain their validators

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    Minimum deposit amount is always satisfied for delegators

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    At any point the  contract holds enough balance to cover for dynamic rewards to be claimed by validators and delegators

    Test Result

    Passed

    Run Count

    60M+

    Invariant

    Validator's delegated amount is the sum of its delegators' amounts

    Test Result

    Passed

    Run Count

    60M+

Additional Recommendations

The smart contracts in the scope of this audit could benefit from the introduction of automatic emergency actions for critical activities, such as unauthorized operations like ownership changes or proxy upgrades, as well as unexpected fund manipulations, including large withdrawals or minting events. Adding such mechanisms would enable the protocol to react automatically to unusual activity, ensuring that the contract remains secure and functions as intended.

To improve functionality, these emergency actions could be designed to trigger under specific conditions, such as:

  • Detecting changes to ownership or critical permissions.

  • Monitoring large or unexpected transactions and minting events.

  • Pausing operations when irregularities are identified.

These enhancements would provide an added layer of security, making the contract more robust and better equipped to handle unexpected situations while maintaining smooth operations.

Disclaimer