Skip to content

Commit

Permalink
Added nodeId to the ShardingTableStorage, removed redundant imports i…
Browse files Browse the repository at this point in the history
…n CommitManagerV2/V2U1, fixed bug in LinearSum score calculation, fixed unit tests for CommitManagerV2
  • Loading branch information
br-41n committed Feb 1, 2024
1 parent 4ba2bb5 commit 9160c83
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 166 deletions.
26 changes: 0 additions & 26 deletions abi/CommitManagerV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hashingProxy",
"outputs": [
{
"internalType": "contract HashingProxy",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hub",
Expand Down Expand Up @@ -724,19 +711,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "stakingContract",
"outputs": [
{
"internalType": "contract StakingV2",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "stakingStorage",
Expand Down
36 changes: 5 additions & 31 deletions abi/CommitManagerV2U1.json
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@
"anonymous": false,
"inputs": [
{
"indexed": true,
"indexed": false,
"internalType": "address",
"name": "assetContract",
"type": "address"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
Expand Down Expand Up @@ -489,7 +489,7 @@
"type": "uint256"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
Expand All @@ -508,13 +508,13 @@
"anonymous": false,
"inputs": [
{
"indexed": true,
"indexed": false,
"internalType": "address",
"name": "assetContract",
"type": "address"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
Expand Down Expand Up @@ -617,19 +617,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hashingProxy",
"outputs": [
{
"internalType": "contract HashingProxy",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "hub",
Expand Down Expand Up @@ -857,19 +844,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "stakingContract",
"outputs": [
{
"internalType": "contract StakingV2",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "stakingStorage",
Expand Down
35 changes: 35 additions & 0 deletions abi/ShardingTableStorageV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "identityId",
Expand Down Expand Up @@ -150,6 +155,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand Down Expand Up @@ -196,6 +206,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand All @@ -218,6 +233,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand All @@ -240,6 +260,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand Down Expand Up @@ -276,6 +301,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand Down Expand Up @@ -312,6 +342,11 @@
"name": "hashRingPosition",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"internalType": "uint72",
"name": "index",
Expand Down
55 changes: 15 additions & 40 deletions contracts/v2/CommitManagerV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

pragma solidity ^0.8.16;

import {HashingProxy} from "../v1/HashingProxy.sol";
import {Log2PLDSF} from "../v1/scoring/log2pldsf.sol";
import {LinearSum} from "./scoring/LinearSum.sol";
import {ProximityScoringProxy} from "./ProximityScoringProxy.sol";
import {StakingV2} from "./Staking.sol";
import {IdentityStorageV2} from "./storage/IdentityStorage.sol";
import {ParametersStorage} from "../v1/storage/ParametersStorage.sol";
import {ProfileStorage} from "../v1/storage/ProfileStorage.sol";
Expand All @@ -21,11 +18,8 @@ import {ServiceAgreementStructsV1} from "../v1/structs/ServiceAgreementStructsV1
import {ServiceAgreementStructsV2} from "./structs/ServiceAgreementStructsV2.sol";
import {ShardingTableStructsV2} from "../v2/structs/ShardingTableStructsV2.sol";
import {CommitManagerErrorsV2} from "./errors/CommitManagerErrorsV2.sol";
import {ContentAssetErrors} from "./errors/assets/ContentAssetErrors.sol";
import {GeneralErrors} from "../v1/errors/GeneralErrors.sol";
import {ServiceAgreementErrorsV1} from "../v1/errors/ServiceAgreementErrorsV1.sol";
import {ServiceAgreementErrorsV2} from "./errors/ServiceAgreementErrorsV2.sol";
import {NULL} from "../v1/constants/ShardingTableConstants.sol";

contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
event CommitSubmitted(
Expand All @@ -46,11 +40,8 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

bool[4] public reqs = [false, false, false, false];

HashingProxy public hashingProxy;

Log2PLDSF public log2pldsf;
LinearSum public linearSum;
StakingV2 public stakingContract;
IdentityStorageV2 public identityStorage;
ParametersStorage public parametersStorage;
ProfileStorage public profileStorage;
Expand All @@ -64,16 +55,8 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
constructor(address hubAddress) ContractStatus(hubAddress) {}

function initialize() public onlyHubOwner {
hashingProxy = HashingProxy(hub.getContractAddress("HashingProxy"));
log2pldsf = Log2PLDSF(
ProximityScoringProxy(hub.getContractAddress("ScoringProxy")).getScoreFunctionContractAddress(_LOG2PLDSF_ID)
);
linearSum = LinearSum(
ProximityScoringProxy(hub.getContractAddress("ScoringProxy")).getScoreFunctionContractAddress(
_LINEAR_SUM_ID
)
);
stakingContract = StakingV2(hub.getContractAddress("Staking"));
log2pldsf = Log2PLDSF(hub.getContractAddress("Log2PLDSF"));
linearSum = LinearSum(hub.getContractAddress("LinearSum"));
identityStorage = IdentityStorageV2(hub.getContractAddress("IdentityStorage"));
parametersStorage = ParametersStorage(hub.getContractAddress("ParametersStorage"));
profileStorage = ProfileStorage(hub.getContractAddress("ProfileStorage"));
Expand Down Expand Up @@ -165,10 +148,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
function submitCommit(ServiceAgreementStructsV1.CommitInputArgs calldata args) external {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

bytes32 agreementId = hashingProxy.callHashFunction(
args.hashFunctionId,
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);
bytes32 agreementId = sha256(abi.encodePacked(args.assetContract, args.tokenId, args.keyword));

if (!sasProxy.agreementV1Exists(agreementId))
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
Expand All @@ -183,7 +163,6 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) {
uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);

uint256 actualCommitWindowStart = (sasProxy.getAgreementStartTime(agreementId) + args.epoch * epochLength);

revert ServiceAgreementErrorsV1.CommitWindowClosed(
Expand Down Expand Up @@ -231,10 +210,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
function submitCommit(ServiceAgreementStructsV2.CommitInputArgs calldata args) external {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

bytes32 agreementId = hashingProxy.callHashFunction(
args.hashFunctionId,
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);
bytes32 agreementId = sha256(abi.encodePacked(args.assetContract, args.tokenId, args.keyword));

if (!sasProxy.agreementV1Exists(agreementId))
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
Expand All @@ -249,7 +225,6 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) {
uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);

uint256 actualCommitWindowStart = (sasProxy.getAgreementStartTime(agreementId) + args.epoch * epochLength);

revert ServiceAgreementErrorsV1.CommitWindowClosed(
Expand Down Expand Up @@ -354,9 +329,9 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {
(uint256 leftEdgeDistance, uint256 closestDistance, uint256 rightEdgeDistance) = ls
.calculateNeighborhoodBoundaryDistances(
hashFunctionId,
ps.getNodeId(leftEdgeNode.identityId),
ps.getNodeId(closestNode.identityId),
ps.getNodeId(rightEdgeNode.identityId),
leftEdgeNode.nodeId,
closestNode.nodeId,
rightEdgeNode.nodeId,
keyword
);

Expand Down Expand Up @@ -386,40 +361,40 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable {

// Verify that closestNode is indeed closest
if (
closestDistance > ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(closestPrevIdentityId)) ||
closestDistance > ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(closestNextIdentityId))
closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestPrevIdentityId), keyword) ||
closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestNextIdentityId), keyword)
)
revert CommitManagerErrorsV2.InvalidClosestNode(
agreementId,
epoch,
closestNodeIndex,
closestDistance,
ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(closestPrevIdentityId)),
ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(closestNextIdentityId)),
ls.calculateDistance(hashFunctionId, ps.getNodeId(closestPrevIdentityId), keyword),
ls.calculateDistance(hashFunctionId, ps.getNodeId(closestNextIdentityId), keyword),
block.timestamp
);

// Verify that leftNode is indeed the left edge of the Neighborhood
if (leftEdgeDistance > ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(rightEdgeNextIdentityId)))
if (leftEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(rightEdgeNextIdentityId), keyword))
revert CommitManagerErrorsV2.InvalidLeftEdgeNode(
agreementId,
epoch,
leftEdgeNodeIndex,
rightEdgeNodeIndex != nodesCount - 1 ? rightEdgeNodeIndex + 1 : 0,
leftEdgeDistance,
ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(rightEdgeNextIdentityId)),
ls.calculateDistance(hashFunctionId, ps.getNodeId(rightEdgeNextIdentityId), keyword),
block.timestamp
);

// Verify that rightNode is indeed the right edge of the Neighborhood
if (rightEdgeDistance > ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(leftEdgePrevIdentityId)))
if (rightEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword))
revert CommitManagerErrorsV2.InvalidRightEdgeNode(
agreementId,
epoch,
rightEdgeNodeIndex,
leftEdgeNodeIndex != 0 ? leftEdgeNodeIndex - 1 : nodesCount - 1,
rightEdgeDistance,
ls.calculateDistance(hashFunctionId, keyword, ps.getNodeId(leftEdgePrevIdentityId)),
ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword),
block.timestamp
);

Expand Down
Loading

0 comments on commit 9160c83

Please sign in to comment.