Skip to content

Commit

Permalink
Merge pull request #236 from OriginTrail/proof-fix-and-parantheses
Browse files Browse the repository at this point in the history
Fixed issue in addReward + small fixes with parentheses
  • Loading branch information
u-hubar authored Feb 12, 2024
2 parents 92fe763 + a026725 commit 8877334
Show file tree
Hide file tree
Showing 24 changed files with 623 additions and 348 deletions.
11 changes: 11 additions & 0 deletions abi/ShardingTableV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
"name": "InvalidIndexWithRespectToPreviousNode",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
}
],
"name": "NodeAlreadyInTheShardingTable",
"type": "error"
},
{
"anonymous": false,
"inputs": [
Expand Down
41 changes: 26 additions & 15 deletions contracts/v1/CommitManagerV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,30 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {
function isCommitWindowOpen(bytes32 agreementId, uint16 epoch) public view returns (bool) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (!sasProxy.agreementV1Exists(agreementId))
if (!sasProxy.agreementV1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
}

uint256 startTime = sasProxy.getAgreementStartTime(agreementId);

ParametersStorage params = parametersStorage;
uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);

if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired(
agreementId,
startTime,
sasProxy.getAgreementEpochsNumber(agreementId),
epochLength
);
}

uint256 timeNow = block.timestamp;
uint256 commitWindowDuration = (params.commitWindowDurationPerc() * epochLength) / 100;

if (epoch == 0) return timeNow < (startTime + commitWindowDuration);
if (epoch == 0) {
return timeNow < (startTime + commitWindowDuration);
}

return (timeNow >= (startTime + epochLength * epoch) &&
timeNow < (startTime + epochLength * epoch + commitWindowDuration));
Expand All @@ -107,15 +111,17 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {
) external view returns (ServiceAgreementStructsV1.CommitSubmission[] memory) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (!sasProxy.agreementV1Exists(agreementId))
if (!sasProxy.agreementV1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
}
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired(
agreementId,
sasProxy.getAgreementStartTime(agreementId),
sasProxy.getAgreementEpochsNumber(agreementId),
sasProxy.getAgreementEpochLength(agreementId)
);
}

uint32 r0 = parametersStorage.r0();

Expand Down Expand Up @@ -151,16 +157,18 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);

if (!sasProxy.agreementV1Exists(agreementId))
if (!sasProxy.agreementV1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
}

if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID)
if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID) {
revert ServiceAgreementErrorsV1.InvalidScoreFunctionId(
agreementId,
args.epoch,
sasProxy.getAgreementScoreFunctionId(agreementId),
block.timestamp
);
}

if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) {
uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);
Expand Down Expand Up @@ -226,13 +234,14 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {

bytes32 commitId = keccak256(abi.encodePacked(agreementId, epoch, identityId));

if (!reqs[2] && sasProxy.commitSubmissionExists(commitId))
if (!reqs[2] && sasProxy.commitSubmissionExists(commitId)) {
revert ServiceAgreementErrorsV1.NodeAlreadySubmittedCommit(
agreementId,
epoch,
identityId,
profileStorage.getNodeId(identityId)
);
}

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

Expand All @@ -250,26 +259,27 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {
}
}

if (!reqs[3] && (i >= r0))
if (!reqs[3] && (i >= r0)) {
revert ServiceAgreementErrorsV1.NodeNotAwarded(
agreementId,
epoch,
identityId,
profileStorage.getNodeId(identityId),
i
);
}

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

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

if ((i == 0) && (refCommit.identityId == 0))
if ((i == 0) && (refCommit.identityId == 0)) {
// No head -> Setting new head
sasProxy.setV1AgreementEpochSubmissionHead(agreementId, epoch, commitId);
else if ((i == 0) && (score <= refCommit.score))
} else if ((i == 0) && (score <= refCommit.score)) {
// There is a head with higher or equal score, add new commit on the right
_linkCommits(agreementId, epoch, refCommit.identityId, identityId);
else if ((i == 0) && (score > refCommit.score)) {
} else if ((i == 0) && (score > refCommit.score)) {
// There is a head with lower score, replace the head
sasProxy.setV1AgreementEpochSubmissionHead(agreementId, epoch, commitId);
_linkCommits(agreementId, epoch, identityId, refCommit.identityId);
Expand All @@ -283,10 +293,11 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable {
// [] <-> [H] <-> [X] ... [RC-] <-(NL)-> [NC] <-(NL)-> [RC] <-> [RC+] ... [C] <-> []
_linkCommits(agreementId, epoch, refCommit.prevIdentityId, identityId);
_linkCommits(agreementId, epoch, identityId, refCommit.identityId);
} else {
// [] <-> [H] <-> [RC] <-> []
// [] <-> [H] <-> [RC] <-(NL)-> [NC] <-> []
_linkCommits(agreementId, epoch, refCommit.identityId, identityId);
}
// [] <-> [H] <-> [RC] <-> []
// [] <-> [H] <-> [RC] <-(NL)-> [NC] <-> []
else _linkCommits(agreementId, epoch, refCommit.identityId, identityId);
}

function _linkCommits(
Expand Down
60 changes: 39 additions & 21 deletions contracts/v1/CommitManagerV1U1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,30 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
function isCommitWindowOpen(bytes32 agreementId, uint16 epoch) public view returns (bool) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
}

uint256 startTime = sasProxy.getAgreementStartTime(agreementId);

ParametersStorage params = parametersStorage;
uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);

if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired(
agreementId,
startTime,
sasProxy.getAgreementEpochsNumber(agreementId),
epochLength
);
}

uint256 timeNow = block.timestamp;
uint256 commitWindowDuration = (params.commitWindowDurationPerc() * epochLength) / 100;

if (epoch == 0) return timeNow < (startTime + commitWindowDuration);
if (epoch == 0) {
return timeNow < (startTime + commitWindowDuration);
}

return (timeNow >= (startTime + epochLength * epoch) &&
timeNow < (startTime + epochLength * epoch + commitWindowDuration));
Expand All @@ -128,15 +132,17 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {

uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId);

if (!sasProxy.agreementV1U1Exists(agreementId))
if (!sasProxy.agreementV1U1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
}
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired(
agreementId,
sasProxy.getAgreementStartTime(agreementId),
sasProxy.getAgreementEpochsNumber(agreementId),
epochLength
);
}

uint256 commitWindowEnd = sasProxy.getUpdateCommitsDeadline(
keccak256(abi.encodePacked(agreementId, stateIndex))
Expand All @@ -152,15 +158,17 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
) external view returns (ServiceAgreementStructsV1.CommitSubmission[] memory) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
}
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired(
agreementId,
sasProxy.getAgreementStartTime(agreementId),
sasProxy.getAgreementEpochsNumber(agreementId),
sasProxy.getAgreementEpochLength(agreementId)
);
}

uint32 r0 = parametersStorage.r0();

Expand Down Expand Up @@ -196,16 +204,18 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);

if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
}

if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID)
if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID) {
revert ServiceAgreementErrorsV1.InvalidScoreFunctionId(
agreementId,
args.epoch,
sasProxy.getAgreementScoreFunctionId(agreementId),
block.timestamp
);
}

uint256 latestFinalizedStateIndex = AbstractAsset(args.assetContract).getAssertionIdsLength(args.tokenId) - 1;

Expand Down Expand Up @@ -266,8 +276,9 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
bytes32 unfinalizedState = uss.getUnfinalizedState(args.tokenId);
uint256 unfinalizedStateIndex = generalAssetInterface.getAssertionIdsLength(args.tokenId);

if (uss.getUnfinalizedState(args.tokenId) == bytes32(0))
if (uss.getUnfinalizedState(args.tokenId) == bytes32(0)) {
revert ServiceAgreementErrorsV1U1.NoPendingUpdate(args.assetContract, args.tokenId);
}

ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

Expand All @@ -276,16 +287,18 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);

if (!sasProxy.agreementV1U1Exists(agreementId))
if (!sasProxy.agreementV1U1Exists(agreementId)) {
revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId);
}

if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID)
if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LOG2PLDSF_ID) {
revert ServiceAgreementErrorsV1.InvalidScoreFunctionId(
agreementId,
args.epoch,
sasProxy.getAgreementScoreFunctionId(agreementId),
block.timestamp
);
}

if (!reqs[2] && !isUpdateCommitWindowOpen(agreementId, args.epoch, unfinalizedStateIndex)) {
uint256 commitWindowEnd = sasProxy.getUpdateCommitsDeadline(
Expand Down Expand Up @@ -340,7 +353,9 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
sasProxy.getCommitsCount(keccak256(abi.encodePacked(agreementId, args.epoch, unfinalizedStateIndex))) ==
parametersStorage.finalizationCommitsNumber()
) {
if (sasProxy.agreementV1Exists(agreementId)) sasProxy.migrateV1ServiceAgreement(agreementId);
if (sasProxy.agreementV1Exists(agreementId)) {
sasProxy.migrateV1ServiceAgreement(agreementId);
}

sasProxy.setAgreementTokenAmount(
agreementId,
Expand Down Expand Up @@ -384,14 +399,15 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {

bytes32 commitId = keccak256(abi.encodePacked(agreementId, epoch, stateIndex, identityId));

if (!reqs[4] && sasProxy.commitSubmissionExists(commitId))
if (!reqs[4] && sasProxy.commitSubmissionExists(commitId)) {
revert ServiceAgreementErrorsV1U1.NodeAlreadySubmittedCommit(
agreementId,
epoch,
stateIndex,
identityId,
profileStorage.getNodeId(identityId)
);
}

bytes32 refCommitId = sasProxy.getV1U1AgreementEpochSubmissionHead(agreementId, epoch, stateIndex);

Expand All @@ -409,7 +425,7 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
}
}

if (!reqs[5] && (i >= r0))
if (!reqs[5] && (i >= r0)) {
revert ServiceAgreementErrorsV1U1.NodeNotAwarded(
agreementId,
epoch,
Expand All @@ -418,18 +434,19 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
profileStorage.getNodeId(identityId),
i
);
}

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

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

if ((i == 0) && (refCommit.identityId == 0))
if ((i == 0) && (refCommit.identityId == 0)) {
// No head -> Setting new head
sasProxy.setV1U1AgreementEpochSubmissionHead(agreementId, epoch, stateIndex, commitId);
else if ((i == 0) && (score <= refCommit.score))
} else if ((i == 0) && (score <= refCommit.score)) {
// There is a head with higher or equal score, add new commit on the right
_linkCommits(agreementId, epoch, stateIndex, refCommit.identityId, identityId);
else if ((i == 0) && (score > refCommit.score)) {
} else if ((i == 0) && (score > refCommit.score)) {
// There is a head with lower score, replace the head
sasProxy.setV1U1AgreementEpochSubmissionHead(agreementId, epoch, stateIndex, commitId);
_linkCommits(agreementId, epoch, stateIndex, identityId, refCommit.identityId);
Expand All @@ -443,10 +460,11 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
// [] <-> [H] <-> [X] ... [RC-] <-(NL)-> [NC] <-(NL)-> [RC] <-> [RC+] ... [C] <-> []
_linkCommits(agreementId, epoch, stateIndex, refCommit.prevIdentityId, identityId);
_linkCommits(agreementId, epoch, stateIndex, identityId, refCommit.identityId);
} else {
// [] <-> [H] <-> [RC] <-> []
// [] <-> [H] <-> [RC] <-(NL)-> [NC] <-> []
_linkCommits(agreementId, epoch, stateIndex, refCommit.identityId, identityId);
}
// [] <-> [H] <-> [RC] <-> []
// [] <-> [H] <-> [RC] <-(NL)-> [NC] <-> []
else _linkCommits(agreementId, epoch, stateIndex, refCommit.identityId, identityId);

sasProxy.incrementCommitsCount(keccak256(abi.encodePacked(agreementId, epoch, stateIndex)));
}
Expand Down
Loading

0 comments on commit 8877334

Please sign in to comment.