diff --git a/contracts/v1/errors/ServiceAgreementErrorsV1.sol b/contracts/v1/errors/ServiceAgreementErrorsV1.sol index 7f1fa693..0867f502 100644 --- a/contracts/v1/errors/ServiceAgreementErrorsV1.sol +++ b/contracts/v1/errors/ServiceAgreementErrorsV1.sol @@ -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 - ); } diff --git a/contracts/v1/structs/ServiceAgreementStructsV1.sol b/contracts/v1/structs/ServiceAgreementStructsV1.sol index 22f4f3b7..b052f67c 100644 --- a/contracts/v1/structs/ServiceAgreementStructsV1.sol +++ b/contracts/v1/structs/ServiceAgreementStructsV1.sol @@ -54,9 +54,6 @@ library ServiceAgreementStructsV1 { bytes keyword; uint8 hashFunctionId; uint16 epoch; - uint72 closestNode; - uint72 leftNeighbourHoodEdge; - uint72 rightNeighbourHoodEdge; } struct ProofInputArgs { diff --git a/contracts/v2/CommitManagerV1U1.sol b/contracts/v2/CommitManagerV1U1.sol index e18d855a..58d926cb 100644 --- a/contracts/v2/CommitManagerV1U1.sol +++ b/contracts/v2/CommitManagerV1U1.sol @@ -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( @@ -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)) @@ -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); @@ -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( @@ -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, @@ -208,45 +209,43 @@ 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 @@ -254,11 +253,10 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { } 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 @@ -266,10 +264,10 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { //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, @@ -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( @@ -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 diff --git a/contracts/v1/errors/CommitManagerErrorsV1.sol b/contracts/v2/errors/CommitManagerErrorsV2.sol similarity index 58% rename from contracts/v1/errors/CommitManagerErrorsV1.sol rename to contracts/v2/errors/CommitManagerErrorsV2.sol index 8242df6f..5b0523ba 100644 --- a/contracts/v1/errors/CommitManagerErrorsV1.sol +++ b/contracts/v2/errors/CommitManagerErrorsV2.sol @@ -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, diff --git a/contracts/v2/errors/ServiceAgreementErrorsV2.sol b/contracts/v2/errors/ServiceAgreementErrorsV2.sol new file mode 100644 index 00000000..58d39914 --- /dev/null +++ b/contracts/v2/errors/ServiceAgreementErrorsV2.sol @@ -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 + ); +} diff --git a/contracts/v1/scoring/linearSum.sol b/contracts/v2/scoring/LinearSum.sol similarity index 90% rename from contracts/v1/scoring/linearSum.sol rename to contracts/v2/scoring/LinearSum.sol index 42f202d1..b82ea6d1 100644 --- a/contracts/v1/scoring/linearSum.sol +++ b/contracts/v2/scoring/LinearSum.sol @@ -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; diff --git a/contracts/v2/structs/ServiceAgreementStructsV2.sol b/contracts/v2/structs/ServiceAgreementStructsV2.sol new file mode 100644 index 00000000..c59eedd9 --- /dev/null +++ b/contracts/v2/structs/ServiceAgreementStructsV2.sol @@ -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; + } +}