Comments (1)
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2023-08-gmx-fv.