Skip to content

Commit

Permalink
Moved new contracts to v2, naming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed Jan 24, 2024
1 parent 942f444 commit dac7a27
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 66 deletions.
7 changes: 0 additions & 7 deletions contracts/v1/errors/ServiceAgreementErrorsV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,4 @@ library ServiceAgreementErrorsV1 {
uint256 challenge
);
error NodeAlreadySubmittedCommit(bytes32 agreementId, uint16 epoch, uint72 identityId, bytes nodeId);
error WrongScoreFunctionId(
bytes32 agreementId,
uint16 epoch,
uint8 agreementScoreFunctionId,
uint8 expectedScoreFunctionId,
uint256 timeNow
);
}
3 changes: 0 additions & 3 deletions contracts/v1/structs/ServiceAgreementStructsV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ library ServiceAgreementStructsV1 {
bytes keyword;
uint8 hashFunctionId;
uint16 epoch;
uint72 closestNode;
uint72 leftNeighbourHoodEdge;
uint72 rightNeighbourHoodEdge;
}

struct ProofInputArgs {
Expand Down
80 changes: 39 additions & 41 deletions contracts/v2/CommitManagerV1U1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import {Versioned} from "../v1/interface/Versioned.sol";
import {ContentAssetErrors} from "./errors/assets/ContentAssetErrors.sol";
import {GeneralErrors} from "../v1/errors/GeneralErrors.sol";
import {ServiceAgreementErrorsV1} from "../v1/errors/ServiceAgreementErrorsV1.sol";
import {ServiceAgreementStructsV1} from "../v1/structs/ServiceAgreementStructsV1.sol";
import {ServiceAgreementErrorsV2} from "./errors/ServiceAgreementErrorsV2.sol";
import {ServiceAgreementStructsV2} from "./structs/ServiceAgreementStructsV2.sol";
import {ShardingTableStructs} from "../v2/structs/ShardingTableStructs.sol";
import {CommitManagerErrorsV1} from "../v1/errors/CommitManagerErrorsV1.sol";
import {CommitManagerErrorsV2} from "./errors/CommitManagerErrorsV2.sol";

contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
event CommitSubmitted(
Expand Down Expand Up @@ -108,7 +109,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
function getTopCommitSubmissions(
bytes32 agreementId,
uint16 epoch
) external view returns (ServiceAgreementStructsV1.CommitSubmission[] memory) {
) external view returns (ServiceAgreementStructsV2.CommitSubmission[] memory) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (!sasProxy.agreementV1Exists(agreementId))
Expand All @@ -123,8 +124,8 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

uint32 r0 = parametersStorage.r0();

ServiceAgreementStructsV1.CommitSubmission[]
memory epochCommits = new ServiceAgreementStructsV1.CommitSubmission[](r0);
ServiceAgreementStructsV2.CommitSubmission[]
memory epochCommits = new ServiceAgreementStructsV2.CommitSubmission[](r0);

bytes32 epochSubmissionsHead = sasProxy.getV1AgreementEpochSubmissionHead(agreementId, epoch);

Expand All @@ -147,7 +148,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
return epochCommits;
}

function submitCommit(ServiceAgreementStructsV1.CommitInputArgs calldata args) external {
function submitCommit(ServiceAgreementStructsV2.CommitInputArgs calldata args) external {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

bytes32 agreementId = hashingProxy.callHashFunction(
Expand Down Expand Up @@ -188,7 +189,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
uint8 agreementScoreFunctionId = sasProxy.getAgreementScoreFunctionId(agreementId);

if (agreementScoreFunctionId != 2) {
revert ServiceAgreementErrorsV1.WrongScoreFunctionId(
revert ServiceAgreementErrorsV2.WrongScoreFunctionId(
agreementId,
args.epoch,
agreementScoreFunctionId,
Expand All @@ -208,68 +209,65 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
);
}

if (!shardingTableStorage.nodeExists(args.leftNeighbourHoodEdge)) {
if (!shardingTableStorage.nodeExists(args.leftNeighborhoodEdge)) {
ProfileStorage ps = profileStorage;

revert ServiceAgreementErrorsV1.NodeNotInShardingTable(
args.leftNeighbourHoodEdge,
ps.getNodeId(args.leftNeighbourHoodEdge),
ps.getAsk(args.leftNeighbourHoodEdge),
stakingStorage.totalStakes(args.leftNeighbourHoodEdge)
args.leftNeighborhoodEdge,
ps.getNodeId(args.leftNeighborhoodEdge),
ps.getAsk(args.leftNeighborhoodEdge),
stakingStorage.totalStakes(args.leftNeighborhoodEdge)
);
}

if (!shardingTableStorage.nodeExists(args.rightNeighbourHoodEdge)) {
if (!shardingTableStorage.nodeExists(args.rightNeighborhoodEdge)) {
ProfileStorage ps = profileStorage;

revert ServiceAgreementErrorsV1.NodeNotInShardingTable(
args.rightNeighbourHoodEdge,
ps.getNodeId(args.rightNeighbourHoodEdge),
ps.getAsk(args.rightNeighbourHoodEdge),
stakingStorage.totalStakes(args.rightNeighbourHoodEdge)
args.rightNeighborhoodEdge,
ps.getNodeId(args.rightNeighborhoodEdge),
ps.getAsk(args.rightNeighborhoodEdge),
stakingStorage.totalStakes(args.rightNeighborhoodEdge)
);
}

ShardingTableStructs.Node memory closestNode = shardingTableStorage.getNode(args.closestNode);
ShardingTableStructs.Node memory leftNeighbourhoodEdge = shardingTableStorage.getNode(
args.leftNeighbourHoodEdge
);
ShardingTableStructs.Node memory rightNeighbourhoodEdge = shardingTableStorage.getNode(
args.rightNeighbourHoodEdge
ShardingTableStructs.Node memory leftNeighborhoodEdge = shardingTableStorage.getNode(args.leftNeighborhoodEdge);
ShardingTableStructs.Node memory rightNeighborhoodEdge = shardingTableStorage.getNode(
args.rightNeighborhoodEdge
);

bool isBetween = (leftNeighbourhoodEdge.index > rightNeighbourhoodEdge.index)
? ((closestNode.index > leftNeighbourhoodEdge.index) || (closestNode.index < rightNeighbourhoodEdge.index))
: ((closestNode.index > leftNeighbourhoodEdge.index) && (closestNode.index < rightNeighbourhoodEdge.index));
bool isBetween = (leftNeighborhoodEdge.index > rightNeighborhoodEdge.index)
? ((closestNode.index > leftNeighborhoodEdge.index) || (closestNode.index < rightNeighborhoodEdge.index))
: ((closestNode.index > leftNeighborhoodEdge.index) && (closestNode.index < rightNeighborhoodEdge.index));

if (!isBetween) {
revert CommitManagerErrorsV1.closestNodeNotInNeighbourhood(
revert CommitManagerErrorsV2.closestNodeNotInNeighborhood(
agreementId,
args.leftNeighbourHoodEdge,
args.rightNeighbourHoodEdge,
args.leftNeighborhoodEdge,
args.rightNeighborhoodEdge,
args.closestNode,
args.epoch,
block.timestamp
);
}

uint256 numberOfNodes = shardingTableStorage.nodesCount();
uint256 clockwiseDistance = (rightNeighbourhoodEdge.index + numberOfNodes - leftNeighbourhoodEdge.index) %
uint256 clockwiseDistance = (rightNeighborhoodEdge.index + numberOfNodes - leftNeighborhoodEdge.index) %
numberOfNodes;
uint256 counterclockwiseDistance = (leftNeighborhoodEdge.index + numberOfNodes - rightNeighborhoodEdge.index) %
numberOfNodes;
uint256 counterclockwiseDistance = (leftNeighbourhoodEdge.index +
numberOfNodes -
rightNeighbourhoodEdge.index) % numberOfNodes;

uint256 indexDistance = (clockwiseDistance < counterclockwiseDistance)
? clockwiseDistance
: counterclockwiseDistance;

//distance between 20 nodes is 19 (this shold be constant)
if (!(indexDistance == 19)) {
revert CommitManagerErrorsV1.negihbourhoodWrongSize(
revert CommitManagerErrorsV2.negihbourhoodWrongSize(
agreementId,
args.leftNeighbourHoodEdge,
args.rightNeighbourHoodEdge,
args.leftNeighborhoodEdge,
args.rightNeighborhoodEdge,
numberOfNodes,
20,
indexDistance,
Expand All @@ -278,17 +276,17 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
);
}

uint256 hashRingNeighbourhoodDistance = calculateHashRingDistance(
leftNeighbourhoodEdge.hashRingPosition,
rightNeighbourhoodEdge.hashRingPosition
uint256 hashRingNeighborhoodDistance = calculateHashRingDistance(
leftNeighborhoodEdge.hashRingPosition,
rightNeighborhoodEdge.hashRingPosition
);

bytes32 keywordHash = hashingProxy.callHashFunction(args.hashFunctionId, args.keyword);
bytes32 nodeIdHash = hashingProxy.callHashFunction(args.hashFunctionId, profileStorage.getNodeId(identityId));

uint256 distance = calculateHashRingDistance(
leftNeighbourhoodEdge.hashRingPosition,
rightNeighbourhoodEdge.hashRingPosition
leftNeighborhoodEdge.hashRingPosition,
rightNeighborhoodEdge.hashRingPosition
);

// uint40 score = scoringProxy.callScoreFunction(
Expand Down Expand Up @@ -360,7 +358,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

sasProxy.createV1CommitSubmissionObject(commitId, identityId, prevIdentityId, nextIdentityId, score);

ServiceAgreementStructsV1.CommitSubmission memory refCommit = sasProxy.getCommitSubmission(refCommitId);
ServiceAgreementStructsV2.CommitSubmission memory refCommit = sasProxy.getCommitSubmission(refCommitId);

if ((i == 0) && (refCommit.identityId == 0)) {
// No head -> Setting new head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

pragma solidity ^0.8.16;

library CommitManagerErrorsV1 {
error closestNodeNotInNeighbourhood(
library CommitManagerErrorsV2 {
error ClosestNodeNotInNeighborhood(
bytes32 agreementId,
uint72 leftNeighbourhoodEdge,
uint72 rightNeighbourhoodEdge,
uint72 leftNeighborhoodEdge,
uint72 rightNeighborhoodEdge,
uint72 closestNode,
uint16 epoch,
uint256 timeNow
);
error negihbourhoodWrongSize(
error NegihbourhoodWrongSize(
bytes32 agreementId,
uint72 leftNeighbourhoodEdge,
uint72 rightNeighbourhoodEdge,
uint72 leftNeighborhoodEdge,
uint72 rightNeighborhoodEdge,
uint256 numberOfNodes,
uint256 negihbourhoodExpectedSize,
uint256 negihbourhoodActualSize,
Expand Down
13 changes: 13 additions & 0 deletions contracts/v2/errors/ServiceAgreementErrorsV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;

library ServiceAgreementErrorsV2 {
error WrongScoreFunctionId(
bytes32 agreementId,
uint16 epoch,
uint8 agreementScoreFunctionId,
uint8 expectedScoreFunctionId,
uint256 timeNow
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

pragma solidity ^0.8.16;

import {HashingProxy} from "../HashingProxy.sol";
import {ParametersStorage} from "../storage/ParametersStorage.sol";
import {HubDependent} from "../abstract/HubDependent.sol";
import {Indexable} from "../interface/Indexable.sol";
import {Initializable} from "../interface/Initializable.sol";
import {IScoreFunction} from "../interface/IScoreFunction.sol";
import {Named} from "../interface/Named.sol";
import {HashingProxy} from "../../v1/HashingProxy.sol";
import {ParametersStorage} from "../../v1/storage/ParametersStorage.sol";
import {HubDependent} from "../../v1/abstract/HubDependent.sol";
import {Indexable} from "../../v1/interface/Indexable.sol";
import {Initializable} from "../../v1/interface/Initializable.sol";
import {IScoreFunction} from "../../v1/interface/IScoreFunction.sol";
import {Named} from "../../v1/interface/Named.sol";
import {PRBMathUD60x18} from "@prb/math/contracts/PRBMathUD60x18.sol";

// Logarithmic Polynomial Long Division Score Function
contract LinearSum is IScoreFunction, Indexable, Named, HubDependent, Initializable {
using PRBMathUD60x18 for uint256;

Expand Down
71 changes: 71 additions & 0 deletions contracts/v2/structs/ServiceAgreementStructsV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;

library ServiceAgreementStructsV2 {
struct CommitSubmission {
uint72 identityId;
uint72 prevIdentityId;
uint72 nextIdentityId;
uint40 score;
}

struct ServiceAgreementInputArgs {
address assetCreator;
address assetContract;
uint256 tokenId;
bytes keyword;
uint8 hashFunctionId;
uint16 epochsNumber;
uint96 tokenAmount;
uint8 scoreFunctionId;
}

struct ServiceAgreement {
uint256 startTime;
uint16 epochsNumber;
uint128 epochLength;
uint96 tokenAmount;
uint8 scoreFunctionId;
uint8 proofWindowOffsetPerc;
// epoch => headCommitId
mapping(uint16 => bytes32) epochSubmissionHeads;
// epoch => number of nodes received rewards
mapping(uint16 => uint32) rewardedNodesNumber;
}

struct ExtendedServiceAgreement {
uint256 startTime;
uint16 epochsNumber;
uint128 epochLength;
uint96 tokenAmount;
uint96 updateTokenAmount;
uint8 scoreFunctionId;
uint8 proofWindowOffsetPerc;
// keccak256(epoch + stateIndex) => headCommitId
mapping(bytes32 => bytes32) epochSubmissionHeads;
// epoch => number of nodes received rewards
mapping(uint16 => uint32) rewardedNodesNumber;
}

struct CommitInputArgs {
address assetContract;
uint256 tokenId;
bytes keyword;
uint8 hashFunctionId;
uint16 epoch;
uint72 closestNode;
uint72 leftNeighborhoodEdge;
uint72 rightNeighborhoodEdge;
}

struct ProofInputArgs {
address assetContract;
uint256 tokenId;
bytes keyword;
uint8 hashFunctionId;
uint16 epoch;
bytes32[] proof;
bytes32 chunkHash;
}
}

0 comments on commit dac7a27

Please sign in to comment.