Git Product home page Git Product logo

Comments (1)

FawazMalik-jjj avatar FawazMalik-jjj commented on September 16, 2024

It seems like you're facing an issue related to gas grieving in your Solidity code. Gas griefing is a technique where a malicious user can manipulate the gas cost of a transaction in order to disrupt its execution. In your case, it seems that the getTokensWithPrices and getPrimaryPrice functions are vulnerable to this kind of attack.

To address this issue, you can implement a gas stipend mechanism in your contract to prevent gas griefing. A gas stipend is a predefined amount of gas that is allocated to sub-calls, preventing the caller from manipulating the gas cost to their advantage. Here's how you can modify your code to add a gas stipend:

solidity
Copy code
contract Oracle {
// ... (other contract functions and state variables)

modifier limitedGas(uint256 gasLimit) {
    require(gasleft() >= gasLimit, "Insufficient gas");
    _;
}

function getTokensWithPrices(uint256 start, uint256 end) external view returns (address[] memory) {
    // Set a reasonable gas stipend for the sub-call
    uint256 gasStipend = 20000; // Adjust the stipend value as needed

    // Use the limitedGas modifier to enforce the stipend
    return tokensWithPrices.valuesAtLimitedGas(gasStipend, start, end);
}

function getPrimaryPrice(address token) external view returns (Price.Props memory) {
    if (token == address(0)) { return Price.Props(0, 0); }

    Price.Props memory price = primaryPrices[token];
    if (price.isEmpty()) {
        revert Errors.EmptyPrimaryPrice(token);
    }

    // Set a reasonable gas stipend for the sub-call
    uint256 gasStipend = 20000; // Adjust the stipend value as needed

    // Use the limitedGas modifier to enforce the stipend
    return price.valuesAtLimitedGas(gasStipend);
}

}
In this example, I've introduced a new modifier named limitedGas that checks if the remaining gas is above a certain threshold before allowing the function to execute. I've also added gas stipends to the sub-calls to ensure that they have enough gas to execute properly.

Please note that you should adjust the gasStipend value based on the complexity of the sub-calls and the gas consumption patterns of your contract. It's important to strike a balance between providing enough gas for sub-calls to succeed and preventing excessive gas consumption by malicious users

from 2023-08-gmx-fv.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.