Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanProgrammer committed Sep 6, 2024
1 parent f4f9445 commit e7e7e98
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract ProviderRegistry is IProviderRegistry, DiamondOwnableStorage, ProviderS
if (!isProviderExists(provider_)) {
revert ProviderNotFound();
}
if (isProviderActiveBidsEmpty(provider_)) {
if (!isProviderActiveBidsEmpty(provider_)) {
revert ProviderHasActiveBids();
}

Expand Down
47 changes: 36 additions & 11 deletions smart-contracts/contracts/diamond/facets/SessionRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SafeERC20, IERC20 } from "@openzeppelin/contracts/token/ERC20/utils/Saf

import { DiamondOwnableStorage } from "../presets/DiamondOwnableStorage.sol";

import { BidStorage } from "../storages/BidStorage.sol";
import { BidStorage, EnumerableSet } from "../storages/BidStorage.sol";
import { StatsStorage } from "../storages/StatsStorage.sol";
import { SessionStorage } from "../storages/SessionStorage.sol";
import { ProviderStorage } from "../storages/ProviderStorage.sol";
Expand All @@ -29,6 +29,7 @@ contract SessionRouter is
using Math for uint256;
using LibSD for LibSD.SD;
using SafeERC20 for IERC20;
using EnumerableSet for EnumerableSet.Bytes32Set;

uint32 public constant MIN_SESSION_DURATION = 5 minutes;
uint32 public constant MAX_SESSION_DURATION = 1 days;
Expand Down Expand Up @@ -102,7 +103,6 @@ contract SessionRouter is

setActiveUserSession(_msgSender(), sessionIndex_, true);
setActiveProviderSession(bid_.provider, sessionIndex_, true);
incrementActiveSessionsCount();

// try to use locked stake first, but limit iterations to 20
// if user has more than 20 onHold entries, they will have to use withdrawUserStake separately
Expand Down Expand Up @@ -134,7 +134,6 @@ contract SessionRouter is
// update indexes
setActiveUserSession(session.user, sessionIndex_, false);
setActiveProviderSession(session.provider, sessionIndex_, false);
decrementActiveSessionsCount();

// update session record
session.closeoutReceipt = receiptEncoded_; //TODO: remove that field in favor of tps and ttftMs
Expand Down Expand Up @@ -212,7 +211,7 @@ contract SessionRouter is

/// @notice allows provider to claim their funds
function claimProviderBalance(bytes32 sessionId_, uint256 amountToWithdraw_) external {
Session storage session = getSession(sessionId_);
Session storage session = _getSession(sessionId_);
if (!_ownerOrProvider(session.provider)) {
revert NotOwnerOrProvider();
}
Expand All @@ -230,7 +229,7 @@ contract SessionRouter is

/// @notice deletes session from the history
function deleteHistory(bytes32 sessionId_) external {
Session storage session = getSession(sessionId_);
Session storage session = _getSession(sessionId_);
if (!_ownerOrUser(session.user)) {
revert NotOwnerOrUser();
}
Expand Down Expand Up @@ -300,10 +299,12 @@ contract SessionRouter is
/// @dev parameters should be the same as in Ethereum L1 Distribution contract
/// @dev at address 0x47176B2Af9885dC6C4575d4eFd63895f7Aaa4790
/// @dev call 'Distribution.pools(3)' where '3' is a poolId
// function setPoolConfig(uint256 index, Pool calldata pool) public {
// LibOwner._onlyOwner();
// s.pools[index] = pool;
// }
function setPoolConfig(uint256 index, Pool calldata pool) public onlyOwner {
if (index >= getPools().length) {
revert PoolIndexOutOfBounds();
}
_getSessionStorage().pools[index] = pool;
}

function _maybeResetProviderRewardLimiter(Provider storage provider) private {
if (block.timestamp > provider.limitPeriodEnd) {
Expand Down Expand Up @@ -434,6 +435,29 @@ contract SessionRouter is
return totalSupply_ + totalClaimed();
}

function getActiveBidsRatingByModelAgent(
bytes32 modelAgentId_,
uint256 offset_,
uint8 limit_
) external view returns (bytes32[] memory, Bid[] memory, ProviderModelStats[] memory) {
bytes32[] memory modelAgentBidsSet_ = modelAgentActiveBids(modelAgentId_, offset_, limit_);
uint256 length_ = modelAgentBidsSet_.length;

Bid[] memory bids_ = new Bid[](length_);
bytes32[] memory bidIds_ = new bytes32[](length_);
ProviderModelStats[] memory stats_ = new ProviderModelStats[](length_);

for (uint i = 0; i < length_; i++) {
bytes32 id_ = modelAgentBidsSet_[i];
bidIds_[i] = id_;
Bid memory bid_ = getBid(id_);
bids_[i] = bid_;
stats_[i] = _getProviderModelStats(modelAgentId_, bid_.provider);
}

return (bidIds_, bids_, stats_);
}

function startOfTheDay(uint256 timestamp_) public pure returns (uint256) {
return timestamp_ - (timestamp_ % 1 days);
}
Expand Down Expand Up @@ -489,8 +513,8 @@ contract SessionRouter is
}

/// @notice returns total claimanble balance for the provider for particular session
function _getProviderClaimableBalance(bytes32 sessionId_) public view returns (uint256) {
Session memory session_ = getSession(sessionId_);
function getProviderClaimableBalance(bytes32 sessionId_) public view returns (uint256) {
Session memory session_ = _getSession(sessionId_);
if (session_.openedAt == 0) {
revert SessionNotFound();
}
Expand All @@ -509,6 +533,7 @@ contract SessionRouter is
}

bytes32 receiptHash_ = ECDSA.toEthSignedMessageHash(keccak256(receipt_));

return ECDSA.recover(receiptHash_, signature_) == signer_;
}

Expand Down
9 changes: 6 additions & 3 deletions smart-contracts/contracts/diamond/storages/BidStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IBidStorage } from "../../interfaces/storage/IBidStorage.sol";

contract BidStorage is IBidStorage {
using Paginator for *;
using EnumerableSet for EnumerableSet.Bytes32Set;

struct BDStorage {
IERC20 token; // MOR token
Expand All @@ -22,8 +23,6 @@ contract BidStorage is IBidStorage {
mapping(address => bytes32[]) providerBids; // provider => all bidIds
}

using EnumerableSet for EnumerableSet.Bytes32Set;

bytes32 public constant BID_STORAGE_SLOT = keccak256("diamond.standard.bid.storage");

function bidMap(bytes32 bidId) external view returns (Bid memory) {
Expand All @@ -42,7 +41,7 @@ contract BidStorage is IBidStorage {
bytes32 modelAgentId_,
uint256 offset_,
uint256 limit_
) external view returns (bytes32[] memory) {
) public view returns (bytes32[] memory) {
return _getBidStorage().modelAgentActiveBids[modelAgentId_].part(offset_, limit_);
}

Expand Down Expand Up @@ -82,6 +81,10 @@ contract BidStorage is IBidStorage {
_getBidStorage().providerActiveBids[provider].remove(bidId);
}

function getModelAgentActiveBids(bytes32 modelAgentId) internal view returns (EnumerableSet.Bytes32Set storage) {
return _getBidStorage().modelAgentActiveBids[modelAgentId];
}

function removeModelAgentActiveBids(bytes32 modelAgentId, bytes32 bidId) internal {
_getBidStorage().modelAgentActiveBids[modelAgentId].remove(bidId);
}
Expand Down
8 changes: 4 additions & 4 deletions smart-contracts/contracts/diamond/storages/ModelStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ contract ModelStorage is IModelStorage {
return _getModelStorage().models[index];
}

function modelMinimumStake() public view returns (uint256) {
return _getModelStorage().modelMinimumStake;
}

function setModelActive(bytes32 modelId, bool isActive) internal {
_getModelStorage().activeModels[modelId] = isActive;
}
Expand All @@ -46,10 +50,6 @@ contract ModelStorage is IModelStorage {
return _getModelStorage().activeModels[modelId];
}

function modelMinimumStake() internal view returns (uint256) {
return _getModelStorage().modelMinimumStake;
}

function _getModelStorage() internal pure returns (MDLStorage storage _ds) {
bytes32 slot_ = MODEL_STORAGE_SLOT;

Expand Down
16 changes: 8 additions & 8 deletions smart-contracts/contracts/diamond/storages/ProviderStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ contract ProviderStorage is IProviderStorage {
return _getProviderStorage().providerMap[provider];
}

function providers(uint256 index) public view returns (address) {
return _getProviderStorage().providers[index];
}

function providerMinimumStake() public view returns (uint256) {
return _getProviderStorage().providerMinimumStake;
}

function setActiveProvider(address provider, bool isActive) internal {
_getProviderStorage().activeProviders[provider] = isActive;
}
Expand All @@ -39,18 +47,10 @@ contract ProviderStorage is IProviderStorage {
return _getProviderStorage().providerMap[addr];
}

function providers(uint256 index) internal view returns (address) {
return _getProviderStorage().providers[index];
}

function isProviderActive(address provider) internal view returns (bool) {
return _getProviderStorage().activeProviders[provider];
}

function providerMinimumStake() internal view returns (uint256) {
return _getProviderStorage().providerMinimumStake;
}

function providerExists(address provider) internal view returns (bool) {
return _getProviderStorage().activeProviders[provider];
}
Expand Down
31 changes: 17 additions & 14 deletions smart-contracts/contracts/diamond/storages/SessionStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ pragma solidity ^0.8.24;

import { ISessionStorage } from "../../interfaces/storage/ISessionStorage.sol";

import { Paginator } from "@solarity/solidity-lib/libs/arrays/Paginator.sol";

contract SessionStorage is ISessionStorage {
using Paginator for *;

struct SNStorage {
// all sessions
Session[] sessions;
Expand All @@ -17,7 +21,6 @@ contract SessionStorage is ISessionStorage {
mapping(address => mapping(uint256 => bool)) providerActiveSessions; // provider address => active session indexes
mapping(address => OnHold[]) userOnHold; // user address => balance
mapping(bytes => bool) approvalMap; // provider approval => true if approval was already used
uint64 activeSessionsCount;
uint256 totalClaimed; // total amount of MOR claimed by providers
// other
address fundingAccount; // account which stores the MOR tokens with infinite allowance for this contract
Expand All @@ -30,6 +33,18 @@ contract SessionStorage is ISessionStorage {
return _getSessionStorage().sessionMap[sessionId];
}

function sessions(uint256 sessionIndex) external view returns (Session memory) {
return _getSessionStorage().sessions[sessionIndex];
}

function getSession(bytes32 sessionId) external view returns (Session memory) {
return _getSessionStorage().sessions[getSessionIndex(sessionId)];
}

function getSessionsByUser(address user, uint256 offset_, uint256 limit_) external view returns (uint256[] memory) {
return _getSessionStorage().userSessions[user].part(offset_, limit_);
}

function getPools() internal view returns (Pool[] storage) {
return _getSessionStorage().pools;
}
Expand Down Expand Up @@ -58,14 +73,6 @@ contract SessionStorage is ISessionStorage {
_getSessionStorage().providerActiveSessions[provider][sessionIndex] = active;
}

function incrementActiveSessionsCount() internal {
_getSessionStorage().activeSessionsCount++;
}

function decrementActiveSessionsCount() internal {
_getSessionStorage().activeSessionsCount--;
}

function setSession(bytes32 sessionId, uint256 sessionIndex) internal {
_getSessionStorage().sessionMap[sessionId] = sessionIndex;
}
Expand Down Expand Up @@ -102,11 +109,7 @@ contract SessionStorage is ISessionStorage {
return _getSessionStorage().userOnHold[user];
}

function getActiveSessionsCount() internal view returns (uint256) {
return _getSessionStorage().activeSessionsCount;
}

function getSession(bytes32 sessionId) internal view returns (Session storage) {
function _getSession(bytes32 sessionId) internal view returns (Session storage) {
return _getSessionStorage().sessions[getSessionIndex(sessionId)];
}

Expand Down
13 changes: 3 additions & 10 deletions smart-contracts/contracts/diamond/storages/StatsStorage.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { LibSD } from "../../libs/LibSD.sol";
import { IStatsStorage } from "../../interfaces/storage/IStatsStorage.sol";

contract StatsStorage {
struct ProviderModelStats {
LibSD.SD tpsScaled1000; // tokens per second running average
LibSD.SD ttftMs; // time to first token running average in milliseconds
uint32 totalDuration; // total duration of sessions
uint32 successCount; // number of observations
uint32 totalCount;
// TODO: consider adding SD with weldford algorithm
}
import { LibSD } from "../../libs/LibSD.sol";

contract StatsStorage is IStatsStorage {
struct ModelStats {
LibSD.SD tpsScaled1000;
LibSD.SD ttftMs;
Expand Down
15 changes: 15 additions & 0 deletions smart-contracts/contracts/interfaces/facets/ISessionRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity ^0.8.24;

import { ISessionStorage } from "../storage/ISessionStorage.sol";
import { IBidStorage } from "../storage/IBidStorage.sol";
import { IStatsStorage } from "../storage/IStatsStorage.sol";

interface ISessionRouter is ISessionStorage {
event SessionOpened(address indexed user, bytes32 indexed sessionId, address indexed providerId);
Expand All @@ -26,6 +28,7 @@ interface ISessionRouter is ISessionStorage {
error AmountToWithdrawIsZero();
error NotOwnerOrProvider();
error NotOwnerOrUser();
error PoolIndexOutOfBounds();

function __SessionRouter_init(address fundingAccount_, Pool[] memory pools_) external;

Expand Down Expand Up @@ -72,4 +75,16 @@ interface ISessionRouter is ISessionStorage {
function totalMORSupply(uint256 timestamp_) external view returns (uint256);

function startOfTheDay(uint256 timestamp_) external pure returns (uint256);

function getProviderClaimableBalance(bytes32 sessionId_) external view returns (uint256);

function setPoolConfig(uint256 index, Pool calldata pool) external;

function SIGNATURE_TTL() external view returns (uint32);

function getActiveBidsRatingByModelAgent(
bytes32 modelAgentId_,
uint256 offset_,
uint8 limit_
) external view returns (bytes32[] memory, IBidStorage.Bid[] memory, IStatsStorage.ProviderModelStats[] memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface IModelStorage {
function getModel(bytes32 modelId) external view returns (Model memory);

function models(uint256 index) external view returns (bytes32);

function modelMinimumStake() external view returns (uint256);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ interface IProviderStorage {
}

function getProvider(address provider) external view returns (Provider memory);

function providers(uint256 index) external view returns (address);

function providerMinimumStake() external view returns (uint256);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ interface ISessionStorage {
}

function sessionMap(bytes32 sessionId) external view returns (uint256);

function sessions(uint256 sessionIndex) external view returns (Session memory);

function getSession(bytes32 sessionId) external view returns (Session memory);

function getSessionsByUser(address user, uint256 offset_, uint256 limit_) external view returns (uint256[] memory);
}
15 changes: 15 additions & 0 deletions smart-contracts/contracts/interfaces/storage/IStatsStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { LibSD } from "../../libs/LibSD.sol";

interface IStatsStorage {
struct ProviderModelStats {
LibSD.SD tpsScaled1000; // tokens per second running average
LibSD.SD ttftMs; // time to first token running average in milliseconds
uint32 totalDuration; // total duration of sessions
uint32 successCount; // number of observations
uint32 totalCount;
// TODO: consider adding SD with weldford algorithm
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity ^0.8.24;

import { LinearDistributionIntervalDecrease } from "morpheus-smart-contracts/contracts/libs/LinearDistributionIntervalDecrease.sol";

import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract LinearDistributionIntervalDecreaseMock {
function getPeriodReward(
uint256 initialAmount_,
Expand Down
Loading

0 comments on commit e7e7e98

Please sign in to comment.