Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VEN-2999]: add transient storage to Resilient oracle #239

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions contracts/ResilientOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "./interfaces/VBep20Interface.sol";
import "./interfaces/OracleInterface.sol";
import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
import "./lib/Transient.sol";

/**
* @title ResilientOracle
Expand Down Expand Up @@ -44,6 +45,8 @@ isValid = anchorRatio <= upperBoundAnchorRatio && anchorRatio >= lowerBoundAncho
* oracle to be stagnant and treat it like it's disabled.
*/
contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOracleInterface {
using Transient for bytes32;

/**
* @dev Oracle roles:
* **main**: The most trustworthy price source
Expand Down Expand Up @@ -86,9 +89,6 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
BoundValidatorInterface public immutable boundValidator;

/// Slot to cache the asset's price, used for transient storage
bytes32 public constant CACHE_SLOT = keccak256(abi.encode("venus-protocol/oracle/ResilientOracle/cache"));

mapping(address => TokenConfig) private tokenConfigs;

event TokenConfigAdded(
Expand Down Expand Up @@ -330,7 +330,7 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
* @param asset asset address
*/
function _updateAssetPrice(address asset) internal {
if (_readCachedPrice(asset) != 0) {
if (Transient.readCachedPrice(asset) != 0) {
return;
}

Expand All @@ -341,38 +341,14 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
}

uint256 price = _getPrice(asset);
_cachePrice(asset, price);
}

/**
* @notice Cache the asset price into transient storage
* @param key address of the asset
* @param value asset price
*/
function _cachePrice(address key, uint256 value) internal {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {
tstore(slot, value)
}
}

/**
* @notice Read cached price from transient storage
* @param key address of the asset
* @return value cached asset price
*/
function _readCachedPrice(address key) internal view returns (uint256 value) {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {
value := tload(slot)
}
Transient.cachePrice(asset, price);
}

function _getPrice(address asset) internal view returns (uint256) {
uint256 pivotPrice = INVALID_PRICE;
uint256 price;

price = _readCachedPrice(asset);
price = Transient.readCachedPrice(asset);
if (price != 0) {
return price;
}
Expand Down
31 changes: 31 additions & 0 deletions contracts/lib/Transient.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.8.25;

library Transient {
/// Slot to cache the asset's price, used for transient storage
bytes32 public constant CACHE_SLOT = keccak256(abi.encode("venus-protocol/oracle/ResilientOracle/cache"));
Copy link
Member

Choose a reason for hiding this comment

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

I would not put this in the library. It seems it's specific to the ResilientOracle contract. I would allow every contract using this library to define their own slot. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


/**
* @notice Cache the asset price into transient storage
* @param key address of the asset
* @param value asset price
*/
function cachePrice(address key, uint256 value) internal {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {

Check warning on line 15 in contracts/lib/Transient.sol

View workflow job for this annotation

GitHub Actions / Compile / Lint / Build

Avoid to use inline assembly. It is acceptable only in rare cases
tstore(slot, value)
}
}

/**
* @notice Read cached price from transient storage
* @param key address of the asset
* @return value cached asset price
*/
function readCachedPrice(address key) internal view returns (uint256 value) {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {

Check warning on line 27 in contracts/lib/Transient.sol

View workflow job for this annotation

GitHub Actions / Compile / Lint / Build

Avoid to use inline assembly. It is acceptable only in rare cases
value := tload(slot)
}
}
}
Loading