diff --git a/abi/ShardingTableV2.json b/abi/ShardingTableV2.json index 476d7bba..3d816fda 100644 --- a/abi/ShardingTableV2.json +++ b/abi/ShardingTableV2.json @@ -52,6 +52,17 @@ "name": "InvalidIndexWithRespectToPreviousNode", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint72", + "name": "identityId", + "type": "uint72" + } + ], + "name": "NodeAlreadyInTheShardingTable", + "type": "error" + }, { "anonymous": false, "inputs": [ diff --git a/contracts/v1/CommitManagerV1.sol b/contracts/v1/CommitManagerV1.sol index 32b378ab..e3883ade 100644 --- a/contracts/v1/CommitManagerV1.sol +++ b/contracts/v1/CommitManagerV1.sol @@ -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)); @@ -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(); @@ -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); @@ -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); @@ -250,7 +259,7 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable { } } - if (!reqs[3] && (i >= r0)) + if (!reqs[3] && (i >= r0)) { revert ServiceAgreementErrorsV1.NodeNotAwarded( agreementId, epoch, @@ -258,18 +267,19 @@ contract CommitManagerV1 is Named, Versioned, ContractStatus, Initializable { 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); @@ -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( diff --git a/contracts/v1/CommitManagerV1U1.sol b/contracts/v1/CommitManagerV1U1.sol index f198fa0f..0b6ab08e 100644 --- a/contracts/v1/CommitManagerV1U1.sol +++ b/contracts/v1/CommitManagerV1U1.sol @@ -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)); @@ -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)) @@ -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(); @@ -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; @@ -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; @@ -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( @@ -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, @@ -384,7 +399,7 @@ 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, @@ -392,6 +407,7 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable { identityId, profileStorage.getNodeId(identityId) ); + } bytes32 refCommitId = sasProxy.getV1U1AgreementEpochSubmissionHead(agreementId, epoch, stateIndex); @@ -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, @@ -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); @@ -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))); } diff --git a/contracts/v1/HubController.sol b/contracts/v1/HubController.sol index b26158bf..539c147e 100644 --- a/contracts/v1/HubController.sol +++ b/contracts/v1/HubController.sol @@ -89,14 +89,16 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { ) external onlyOwnerOrMultiSigOwner { if (hub.isContract(contractName)) { address oldContractAddress = hub.getContractAddress(contractName); - if (_isContract(oldContractAddress)) + if (_isContract(oldContractAddress)) { // solhint-disable-next-line no-empty-blocks try ContractStatus(hub.getContractAddress(contractName)).setStatus(false) {} catch {} + } } hub.setContractAddress(contractName, newContractAddress); - if (_isContract(newContractAddress)) + if (_isContract(newContractAddress)) { // solhint-disable-next-line no-empty-blocks try ContractStatus(newContractAddress).setStatus(true) {} catch {} + } } function setAssetStorageAddress( @@ -118,14 +120,16 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { for (uint i; i < newContracts.length; ) { if (hub.isContract(newContracts[i].name)) { address oldContractAddress = hub.getContractAddress(newContracts[i].name); - if (_isContract(oldContractAddress)) + if (_isContract(oldContractAddress)) { // solhint-disable-next-line no-empty-blocks try ContractStatus(oldContractAddress).setStatus(false) {} catch {} + } } hub.setContractAddress(newContracts[i].name, newContracts[i].addr); - if (_isContract(newContracts[i].addr)) + if (_isContract(newContracts[i].addr)) { // solhint-disable-next-line no-empty-blocks try ContractStatus(newContracts[i].addr).setStatus(true) {} catch {} + } unchecked { i++; } @@ -178,7 +182,9 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { } function _setHashFunctions(address[] calldata newHashFunctions) internal { - if (newHashFunctions.length == 0) return; + if (newHashFunctions.length == 0) { + return; + } HashingProxy hashingProxy = HashingProxy(hub.getContractAddress("HashingProxy")); for (uint i; i < newHashFunctions.length; ) { hashingProxy.setContractAddress(Indexable(newHashFunctions[i]).id(), newHashFunctions[i]); @@ -189,7 +195,9 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { } function _setScoreFunctions(address[] calldata newScoreFunctions) internal { - if (newScoreFunctions.length == 0) return; + if (newScoreFunctions.length == 0) { + return; + } ScoringProxy scoringProxy = ScoringProxy(hub.getContractAddress("ScoringProxy")); for (uint i; i < newScoreFunctions.length; ) { scoringProxy.setContractAddress(Indexable(newScoreFunctions[i]).id(), newScoreFunctions[i]); @@ -210,7 +218,9 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { function _isMultiSigOwner(address multiSigAddress) internal view returns (bool) { try ICustodian(multiSigAddress).getOwners() returns (address[] memory multiSigOwners) { for (uint i = 0; i < multiSigOwners.length; i++) { - if (msg.sender == multiSigOwners[i]) return true; + if (msg.sender == multiSigOwners[i]) { + return true; + } } // solhint-disable-next-line no-empty-blocks } catch {} diff --git a/contracts/v1/Identity.sol b/contracts/v1/Identity.sol index 18895bde..afffe0ad 100644 --- a/contracts/v1/Identity.sol +++ b/contracts/v1/Identity.sol @@ -111,7 +111,9 @@ contract Identity is Named, Versioned, ContractStatus, Initializable { ids.removeKey(identityId, key); - if (purpose == OPERATIONAL_KEY) ids.removeOperationalKeyIdentityId(key); + if (purpose == OPERATIONAL_KEY) { + ids.removeOperationalKeyIdentityId(key); + } } function addOperationalWallets(uint72 identityId, address[] calldata operationalWallets) external onlyContracts { diff --git a/contracts/v1/Profile.sol b/contracts/v1/Profile.sol index c6900ad2..bfded4b1 100644 --- a/contracts/v1/Profile.sol +++ b/contracts/v1/Profile.sol @@ -92,22 +92,33 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { ProfileStorage ps = profileStorage; Identity id = identityContract; - if (ids.getIdentityId(msg.sender) != 0) + if (ids.getIdentityId(msg.sender) != 0) { revert ProfileErrors.IdentityAlreadyExists(ids.getIdentityId(msg.sender), msg.sender); - if (operationalWallets.length > parametersStorage.opWalletsLimitOnProfileCreation()) + } + if (operationalWallets.length > parametersStorage.opWalletsLimitOnProfileCreation()) { revert ProfileErrors.TooManyOperationalWallets( parametersStorage.opWalletsLimitOnProfileCreation(), uint16(operationalWallets.length) ); - if (nodeId.length == 0) revert ProfileErrors.EmptyNodeId(); - if (ps.nodeIdsList(nodeId)) revert ProfileErrors.NodeIdAlreadyExists(nodeId); - if (keccak256(abi.encodePacked(sharesTokenName)) == keccak256(abi.encodePacked(""))) + } + if (nodeId.length == 0) { + revert ProfileErrors.EmptyNodeId(); + } + if (ps.nodeIdsList(nodeId)) { + revert ProfileErrors.NodeIdAlreadyExists(nodeId); + } + if (keccak256(abi.encodePacked(sharesTokenName)) == keccak256(abi.encodePacked(""))) { revert ProfileErrors.EmptySharesTokenName(); - if (keccak256(abi.encodePacked(sharesTokenSymbol)) == keccak256(abi.encodePacked(""))) + } + if (keccak256(abi.encodePacked(sharesTokenSymbol)) == keccak256(abi.encodePacked(""))) { revert ProfileErrors.EmptySharesTokenSymbol(); - if (ps.sharesNames(sharesTokenName)) revert ProfileErrors.SharesTokenNameAlreadyExists(sharesTokenName); - if (ps.sharesSymbols(sharesTokenSymbol)) revert ProfileErrors.SharesTokenSymbolAlreadyExists(sharesTokenSymbol); - + } + if (ps.sharesNames(sharesTokenName)) { + revert ProfileErrors.SharesTokenNameAlreadyExists(sharesTokenName); + } + if (ps.sharesSymbols(sharesTokenSymbol)) { + revert ProfileErrors.SharesTokenSymbolAlreadyExists(sharesTokenSymbol); + } uint72 identityId = id.createIdentity(msg.sender, adminWallet); id.addOperationalWallets(identityId, operationalWallets); @@ -122,8 +133,9 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { } function setAsk(uint72 identityId, uint96 ask) external onlyIdentityOwner(identityId) { - if (ask == 0) revert ProfileErrors.ZeroAsk(); - + if (ask == 0) { + revert ProfileErrors.ZeroAsk(); + } ProfileStorage ps = profileStorage; ps.setAsk(identityId, ask); @@ -154,8 +166,9 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { ProfileStorage ps = profileStorage; uint96 accumulatedOperatorFee = ps.getAccumulatedOperatorFee(identityId); - if (accumulatedOperatorFee == 0) revert ProfileErrors.NoOperatorFees(identityId); - + if (accumulatedOperatorFee == 0) { + revert ProfileErrors.NoOperatorFees(identityId); + } ps.setAccumulatedOperatorFee(identityId, 0); stakingContract.addStake(msg.sender, identityId, accumulatedOperatorFee); } @@ -165,8 +178,9 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { uint96 accumulatedOperatorFee = ps.getAccumulatedOperatorFee(identityId); - if (accumulatedOperatorFee == 0) revert ProfileErrors.NoOperatorFees(identityId); - + if (accumulatedOperatorFee == 0) { + revert ProfileErrors.NoOperatorFees(identityId); + } ps.setAccumulatedOperatorFee(identityId, 0); ps.setAccumulatedOperatorFeeWithdrawalAmount( identityId, @@ -183,13 +197,15 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { uint96 withdrawalAmount = ps.getAccumulatedOperatorFeeWithdrawalAmount(identityId); - if (withdrawalAmount == 0) revert StakingErrors.WithdrawalWasntInitiated(); - if (ps.getAccumulatedOperatorFeeWithdrawalTimestamp(identityId) >= block.timestamp) + if (withdrawalAmount == 0) { + revert StakingErrors.WithdrawalWasntInitiated(); + } + if (ps.getAccumulatedOperatorFeeWithdrawalTimestamp(identityId) >= block.timestamp) { revert StakingErrors.WithdrawalPeriodPending( block.timestamp, ps.getAccumulatedOperatorFeeWithdrawalTimestamp(identityId) ); - + } ps.setAccumulatedOperatorFeeWithdrawalAmount(identityId, 0); ps.setAccumulatedOperatorFeeWithdrawalTimestamp(identityId, 0); ps.transferAccumulatedOperatorFee(msg.sender, withdrawalAmount); @@ -199,22 +215,27 @@ contract Profile is Named, Versioned, ContractStatus, Initializable { if ( !identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY) && !identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), OPERATIONAL_KEY) - ) revert GeneralErrors.OnlyProfileAdminOrOperationalAddressesFunction(msg.sender); + ) { + revert GeneralErrors.OnlyProfileAdminOrOperationalAddressesFunction(msg.sender); + } } function _checkAdmin(uint72 identityId) internal view virtual { - if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY)) + if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY)) { revert GeneralErrors.OnlyProfileAdminFunction(msg.sender); + } } function _checkOperational(uint72 identityId) internal view virtual { - if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), OPERATIONAL_KEY)) + if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), OPERATIONAL_KEY)) { revert GeneralErrors.OnlyProfileOperationalWalletFunction(msg.sender); + } } function _checkWhitelist() internal view virtual { WhitelistStorage ws = whitelistStorage; - if (ws.whitelistingEnabled() && !ws.whitelisted(msg.sender)) + if (ws.whitelistingEnabled() && !ws.whitelisted(msg.sender)) { revert GeneralErrors.OnlyWhitelistedAddressesFunction(msg.sender); + } } } diff --git a/contracts/v1/ProofManagerV1.sol b/contracts/v1/ProofManagerV1.sol index 811f1c07..53badfe7 100644 --- a/contracts/v1/ProofManagerV1.sol +++ b/contracts/v1/ProofManagerV1.sol @@ -70,19 +70,19 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { function isProofWindowOpen(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); - if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) + if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) { revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired( agreementId, startTime, sasProxy.getAgreementEpochsNumber(agreementId), sasProxy.getAgreementEpochLength(agreementId) ); - + } uint256 timeNow = block.timestamp; uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId); uint8 proofWindowOffsetPerc = sasProxy.getAgreementProofWindowOffsetPerc(agreementId); @@ -120,9 +120,9 @@ contract ProofManagerV1 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 (!reqs[0] && !isProofWindowOpen(agreementId, args.epoch)) { uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId); @@ -144,14 +144,14 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { if ( !reqs[1] && (sasProxy.getCommitSubmissionScore(keccak256(abi.encodePacked(agreementId, args.epoch, identityId))) == 0) - ) + ) { revert ServiceAgreementErrorsV1.NodeAlreadyRewarded( agreementId, args.epoch, identityId, profileStorage.getNodeId(identityId) ); - + } bytes32 nextCommitId = sasProxy.getV1AgreementEpochSubmissionHead(agreementId, args.epoch); uint32 r0 = parametersStorage.r0(); uint8 i; @@ -164,7 +164,7 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { } } - if (!reqs[2] && (i >= r0)) + if (!reqs[2] && (i >= r0)) { revert ServiceAgreementErrorsV1.NodeNotAwarded( agreementId, args.epoch, @@ -172,7 +172,7 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { profileStorage.getNodeId(identityId), i ); - + } bytes32 merkleRoot; uint256 challenge; (merkleRoot, challenge) = getChallenge(msg.sender, args.assetContract, args.tokenId, args.epoch); @@ -180,7 +180,7 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { if ( !reqs[3] && !MerkleProof.verify(args.proof, merkleRoot, keccak256(abi.encodePacked(args.chunkHash, challenge))) - ) + ) { revert ServiceAgreementErrorsV1.WrongMerkleProof( agreementId, args.epoch, @@ -191,6 +191,7 @@ contract ProofManagerV1 is Named, Versioned, ContractStatus, Initializable { args.chunkHash, challenge ); + } uint96 reward = sasProxy.getAgreementTokenAmount(agreementId) / ((r0 - sasProxy.getAgreementRewardedNodesNumber(agreementId, args.epoch)) + diff --git a/contracts/v1/ProofManagerV1U1.sol b/contracts/v1/ProofManagerV1U1.sol index e3586ad6..84a27e6f 100644 --- a/contracts/v1/ProofManagerV1U1.sol +++ b/contracts/v1/ProofManagerV1U1.sol @@ -72,19 +72,19 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { function isProofWindowOpen(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); - if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) + if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId)) { revert ServiceAgreementErrorsV1.ServiceAgreementHasBeenExpired( agreementId, startTime, sasProxy.getAgreementEpochsNumber(agreementId), sasProxy.getAgreementEpochLength(agreementId) ); - + } uint256 timeNow = block.timestamp; uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId); uint8 proofWindowOffsetPerc = sasProxy.getAgreementProofWindowOffsetPerc(agreementId); @@ -112,9 +112,9 @@ contract ProofManagerV1U1 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); - + } uint256 latestFinalizedStateIndex = AbstractAsset(args.assetContract).getAssertionIdsLength(args.tokenId) - 1; if (!reqs[0] && !isProofWindowOpen(agreementId, args.epoch)) { @@ -141,7 +141,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { uint72 identityId = ids.getIdentityId(msg.sender); bytes32 commitId = keccak256(abi.encodePacked(agreementId, args.epoch, latestFinalizedStateIndex, identityId)); - if (!reqs[1] && (sasProxy.getCommitSubmissionScore(commitId) == 0)) + if (!reqs[1] && (sasProxy.getCommitSubmissionScore(commitId) == 0)) { revert ServiceAgreementErrorsV1U1.NodeAlreadyRewarded( agreementId, args.epoch, @@ -149,7 +149,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { identityId, profileStorage.getNodeId(identityId) ); - + } bytes32 nextCommitId = sasProxy.getV1U1AgreementEpochSubmissionHead( agreementId, args.epoch, @@ -171,7 +171,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { } } - if (!reqs[2] && (i >= r0)) + if (!reqs[2] && (i >= r0)) { revert ServiceAgreementErrorsV1U1.NodeNotAwarded( agreementId, args.epoch, @@ -180,7 +180,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { profileStorage.getNodeId(identityId), i ); - + } bytes32 merkleRoot; uint256 challenge; (merkleRoot, challenge) = _getChallenge(msg.sender, args.assetContract, args.tokenId, args.epoch); @@ -188,7 +188,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { if ( !reqs[3] && !MerkleProof.verify(args.proof, merkleRoot, keccak256(abi.encodePacked(args.chunkHash, challenge))) - ) + ) { revert ServiceAgreementErrorsV1U1.WrongMerkleProof( agreementId, args.epoch, @@ -200,6 +200,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable { args.chunkHash, challenge ); + } uint96 reward = sasProxy.getAgreementTokenAmount(agreementId) / ((r0 - sasProxy.getAgreementRewardedNodesNumber(agreementId, args.epoch)) + diff --git a/contracts/v1/ServiceAgreementV1.sol b/contracts/v1/ServiceAgreementV1.sol index dfb54c99..3f919f73 100644 --- a/contracts/v1/ServiceAgreementV1.sol +++ b/contracts/v1/ServiceAgreementV1.sol @@ -80,11 +80,15 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { function createServiceAgreement( ServiceAgreementStructsV1.ServiceAgreementInputArgs calldata args ) external onlyContracts { - if (args.epochsNumber == 0) revert ServiceAgreementErrorsV1.ZeroEpochsNumber(); - if (args.tokenAmount == 0) revert ServiceAgreementErrorsV1.ZeroTokenAmount(); - if (!scoringProxy.isScoreFunction(args.scoreFunctionId)) + if (args.epochsNumber == 0) { + revert ServiceAgreementErrorsV1.ZeroEpochsNumber(); + } + if (args.tokenAmount == 0) { + revert ServiceAgreementErrorsV1.ZeroTokenAmount(); + } + if (!scoringProxy.isScoreFunction(args.scoreFunctionId)) { revert ServiceAgreementErrorsV1.ScoreFunctionDoesntExist(args.scoreFunctionId); - + } bytes32 agreementId = hashingProxy.callHashFunction( args.hashFunctionId, abi.encodePacked(args.assetContract, args.tokenId, args.keyword) @@ -107,11 +111,12 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { ); IERC20 tknc = tokenContract; - if (tknc.allowance(args.assetCreator, address(this)) < args.tokenAmount) + if (tknc.allowance(args.assetCreator, address(this)) < args.tokenAmount) { revert TokenErrors.TooLowAllowance(address(tknc), tknc.allowance(args.assetCreator, address(this))); - if (tknc.balanceOf(args.assetCreator) < args.tokenAmount) + } + if (tknc.balanceOf(args.assetCreator) < args.tokenAmount) { revert TokenErrors.TooLowBalance(address(tknc), tknc.balanceOf(args.assetCreator)); - + } tknc.transferFrom(args.assetCreator, sasProxy.agreementV1StorageAddress(), args.tokenAmount); emit ServiceAgreementV1Created( @@ -144,17 +149,19 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { uint16 epochsNumber, uint96 tokenAmount ) external onlyContracts { - if (epochsNumber == 0) revert ServiceAgreementErrorsV1.ZeroEpochsNumber(); - + if (epochsNumber == 0) { + revert ServiceAgreementErrorsV1.ZeroEpochsNumber(); + } ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy; sasProxy.setAgreementEpochsNumber(agreementId, sasProxy.getAgreementEpochsNumber(agreementId) + epochsNumber); sasProxy.setAgreementTokenAmount(agreementId, sasProxy.getAgreementTokenAmount(agreementId) + tokenAmount); - if (sasProxy.agreementV1Exists(agreementId)) + if (sasProxy.agreementV1Exists(agreementId)) { _addTokens(assetOwner, sasProxy.agreementV1StorageAddress(), tokenAmount); - else _addTokens(assetOwner, sasProxy.agreementV1U1StorageAddress(), tokenAmount); - + } else { + _addTokens(assetOwner, sasProxy.agreementV1U1StorageAddress(), tokenAmount); + } emit ServiceAgreementV1Extended(agreementId, epochsNumber); } @@ -163,10 +170,11 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { sasProxy.setAgreementTokenAmount(agreementId, sasProxy.getAgreementTokenAmount(agreementId) + tokenAmount); - if (sasProxy.agreementV1Exists(agreementId)) + if (sasProxy.agreementV1Exists(agreementId)) { _addTokens(assetOwner, sasProxy.agreementV1StorageAddress(), tokenAmount); - else _addTokens(assetOwner, sasProxy.agreementV1U1StorageAddress(), tokenAmount); - + } else { + _addTokens(assetOwner, sasProxy.agreementV1U1StorageAddress(), tokenAmount); + } emit ServiceAgreementV1RewardRaised(agreementId, tokenAmount); } @@ -184,18 +192,22 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { } function isCommitWindowOpen(bytes32 agreementId, uint16 epoch) public view returns (bool) { - if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) + if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) { return commitManagerV1.isCommitWindowOpen(agreementId, epoch); - else return commitManagerV1U1.isCommitWindowOpen(agreementId, epoch); + } else { + return commitManagerV1U1.isCommitWindowOpen(agreementId, epoch); + } } function getTopCommitSubmissions( bytes32 agreementId, uint16 epoch ) external view returns (ServiceAgreementStructsV1.CommitSubmission[] memory) { - if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) + if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) { return commitManagerV1.getTopCommitSubmissions(agreementId, epoch); - else return commitManagerV1U1.getTopCommitSubmissions(agreementId, epoch, 0); + } else { + return commitManagerV1U1.getTopCommitSubmissions(agreementId, epoch, 0); + } } function submitCommit(ServiceAgreementStructsV1.CommitInputArgs calldata args) external { @@ -204,14 +216,19 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { abi.encodePacked(args.assetContract, args.tokenId, args.keyword) ); - if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) commitManagerV1.submitCommit(args); - else commitManagerV1U1.submitCommit(args); + if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) { + commitManagerV1.submitCommit(args); + } else { + commitManagerV1U1.submitCommit(args); + } } function isProofWindowOpen(bytes32 agreementId, uint16 epoch) public view returns (bool) { - if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) + if (serviceAgreementStorageProxy.agreementV1Exists(agreementId)) { return proofManagerV1.isProofWindowOpen(agreementId, epoch); - else return proofManagerV1U1.isProofWindowOpen(agreementId, epoch); + } else { + return proofManagerV1U1.isProofWindowOpen(agreementId, epoch); + } } function getChallenge( @@ -237,15 +254,17 @@ contract ServiceAgreementV1 is Named, Versioned, ContractStatus, Initializable { } function _addTokens(address assetOwner, address sasAddress, uint96 tokenAmount) internal virtual { - if (tokenAmount == 0) revert ServiceAgreementErrorsV1.ZeroTokenAmount(); - + if (tokenAmount == 0) { + revert ServiceAgreementErrorsV1.ZeroTokenAmount(); + } IERC20 tknc = tokenContract; - if (tknc.allowance(assetOwner, address(this)) < tokenAmount) + if (tknc.allowance(assetOwner, address(this)) < tokenAmount) { revert TokenErrors.TooLowAllowance(address(tknc), tknc.allowance(assetOwner, address(this))); - if (tknc.balanceOf(assetOwner) < tokenAmount) + } + if (tknc.balanceOf(assetOwner) < tokenAmount) { revert TokenErrors.TooLowBalance(address(tknc), tknc.balanceOf(assetOwner)); - + } tknc.transferFrom(assetOwner, sasAddress, tokenAmount); } diff --git a/contracts/v1/ShardingTable.sol b/contracts/v1/ShardingTable.sol index bcc56127..7c7f5818 100644 --- a/contracts/v1/ShardingTable.sol +++ b/contracts/v1/ShardingTable.sol @@ -60,12 +60,14 @@ contract ShardingTable is Named, Versioned, ContractStatus, Initializable { sts.createNodeObject(identityId, NULL, NULL); - if (sts.tail() != NULL) sts.link(sts.tail(), identityId); - + if (sts.tail() != NULL) { + sts.link(sts.tail(), identityId); + } sts.setTail(identityId); - if (sts.head() == NULL) sts.setHead(identityId); - + if (sts.head() == NULL) { + sts.setHead(identityId); + } sts.incrementNodesCount(); emit NodeAdded( @@ -84,12 +86,14 @@ contract ShardingTable is Named, Versioned, ContractStatus, Initializable { sts.createNodeObject(identityId, NULL, NULL); - if (sts.head() != NULL) sts.link(identityId, sts.head()); - + if (sts.head() != NULL) { + sts.link(identityId, sts.head()); + } shardingTableStorage.setHead(identityId); - if (sts.tail() == NULL) sts.setTail(identityId); - + if (sts.tail() == NULL) { + sts.setTail(identityId); + } sts.incrementNodesCount(); emit NodeAdded( @@ -120,8 +124,9 @@ contract ShardingTable is Named, Versioned, ContractStatus, Initializable { } else if (head == identityId) { sts.setHead(nodeToRemove.nextIdentityId); sts.setPrevIdentityId(head, NULL); - } else sts.link(nodeToRemove.prevIdentityId, nodeToRemove.nextIdentityId); - + } else { + sts.link(nodeToRemove.prevIdentityId, nodeToRemove.nextIdentityId); + } sts.deleteNodeObject(identityId); sts.decrementNodesCount(); diff --git a/contracts/v1/Staking.sol b/contracts/v1/Staking.sol index 4e2bd863..c2445cfd 100644 --- a/contracts/v1/Staking.sol +++ b/contracts/v1/Staking.sol @@ -115,8 +115,9 @@ contract Staking is Named, Versioned, ContractStatus, Initializable { ss.setTotalStake(identityId, newStake); sharesContract.burnFrom(msg.sender, sharesToBurn); - if (shardingTableStorage.nodeExists(identityId) && (newStake < params.minimumStake())) + if (shardingTableStorage.nodeExists(identityId) && (newStake < params.minimumStake())) { shardingTableContract.removeNode(identityId); + } emit StakeWithdrawalStarted( identityId, @@ -169,8 +170,9 @@ contract Staking is Named, Versioned, ContractStatus, Initializable { ss.setTotalStake(identityId, oldStake + delegatorsReward); sasProxy.transferAgreementTokens(agreementId, address(ss), delegatorsReward); - if (!shardingTableStorage.nodeExists(identityId) && oldStake >= parametersStorage.minimumStake()) + if (!shardingTableStorage.nodeExists(identityId) && oldStake >= parametersStorage.minimumStake()) { shardingTableContract.pushBack(identityId); + } } emit AccumulatedOperatorFeeIncreased( @@ -181,8 +183,11 @@ contract Staking is Named, Versioned, ContractStatus, Initializable { ); address sasAddress; - if (sasProxy.agreementV1Exists(agreementId)) sasAddress = sasProxy.agreementV1StorageAddress(); - else sasAddress = sasProxy.agreementV1U1StorageAddress(); + if (sasProxy.agreementV1Exists(agreementId)) { + sasAddress = sasProxy.agreementV1StorageAddress(); + } else { + sasAddress = sasProxy.agreementV1U1StorageAddress(); + } emit StakeIncreased(identityId, ps.getNodeId(identityId), sasAddress, oldStake, oldStake + delegatorsReward); } @@ -215,16 +220,20 @@ contract Staking is Named, Versioned, ContractStatus, Initializable { Shares sharesContract = Shares(ps.getSharesContractAddress(identityId)); uint256 sharesMinted; - if (sharesContract.totalSupply() == 0) sharesMinted = stakeAmount; - else sharesMinted = ((stakeAmount * sharesContract.totalSupply()) / oldStake); + if (sharesContract.totalSupply() == 0) { + sharesMinted = stakeAmount; + } else { + sharesMinted = ((stakeAmount * sharesContract.totalSupply()) / oldStake); + } sharesContract.mint(sender, sharesMinted); ss.setTotalStake(identityId, newStake); tknc.transferFrom(sender, address(ss), stakeAmount); - if (!shardingTableStorage.nodeExists(identityId) && newStake >= params.minimumStake()) + if (!shardingTableStorage.nodeExists(identityId) && newStake >= params.minimumStake()) { shardingTableContract.pushBack(identityId); + } emit StakeIncreased(identityId, ps.getNodeId(identityId), sender, oldStake, newStake); } diff --git a/contracts/v1/assets/ContentAsset.sol b/contracts/v1/assets/ContentAsset.sol index 7591331e..c7952e98 100644 --- a/contracts/v1/assets/ContentAsset.sol +++ b/contracts/v1/assets/ContentAsset.sol @@ -135,8 +135,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { bytes32 unfinalizedState = unfinalizedStateStorage.getUnfinalizedState(tokenId); - if (unfinalizedState != bytes32(0)) + if (unfinalizedState != bytes32(0)) { revert ContentAssetErrors.UpdateIsNotFinalized(contentAssetStorageAddress, tokenId, unfinalizedState); + } ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy; ParametersStorage params = parametersStorage; @@ -153,11 +154,13 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { ); uint32 r0 = params.r0(); - if ((timeNow < commitPhaseEnd) && (commitsCount < r0)) + if ((timeNow < commitPhaseEnd) && (commitsCount < r0)) { revert ContentAssetErrors.CommitPhaseOngoing(agreementId); - else if ((timeNow < epochEnd) && (commitsCount >= r0)) + } else if ((timeNow < epochEnd) && (commitsCount >= r0)) { revert ContentAssetErrors.CommitPhaseSucceeded(agreementId); - else if (timeNow > epochEnd) revert ContentAssetErrors.FirstEpochHasAlreadyEnded(agreementId); + } else if (timeNow > epochEnd) { + revert ContentAssetErrors.FirstEpochHasAlreadyEnded(agreementId); + } uint96 tokenAmount = sasProxy.getAgreementTokenAmount(agreementId); @@ -197,18 +200,21 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { agreementId ); - if (block.timestamp > startTime + epochsNumber * epochLength) revert ContentAssetErrors.AssetExpired(tokenId); + if (block.timestamp > startTime + epochsNumber * epochLength) { + revert ContentAssetErrors.AssetExpired(tokenId); + } bytes32 unfinalizedState = uss.getUnfinalizedState(tokenId); - if (unfinalizedState != bytes32(0)) + if (unfinalizedState != bytes32(0)) { revert ContentAssetErrors.UpdateIsNotFinalized(contentAssetStorageAddress, tokenId, unfinalizedState); + } assertionContract.createAssertion(assertionId, size, triplesNumber, chunksNumber); uss.setUnfinalizedState(tokenId, assertionId); uss.setIssuer(tokenId, msg.sender); - if (!sasProxy.agreementV1U1Exists(agreementId)) + if (!sasProxy.agreementV1U1Exists(agreementId)) { sasProxy.createV1U1ServiceAgreementObject( agreementId, startTime, @@ -218,8 +224,11 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { scoreFunctionIdAndProofWindowOffsetPerc[0], scoreFunctionIdAndProofWindowOffsetPerc[1] ); + } - if (updateTokenAmount != 0) serviceAgreementV1.addUpdateTokens(msg.sender, agreementId, updateTokenAmount); + if (updateTokenAmount != 0) { + serviceAgreementV1.addUpdateTokens(msg.sender, agreementId, updateTokenAmount); + } uint256 unfinalizedStateIndex = cas.getAssertionIdsLength(tokenId); sasProxy.setUpdateCommitsDeadline( @@ -249,26 +258,32 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { uint128 epochLength; (startTime, epochsNumber, epochLength, , ) = sasProxy.getAgreementData(agreementId); - if (block.timestamp > startTime + epochsNumber * epochLength) revert ContentAssetErrors.AssetExpired(tokenId); + if (block.timestamp > startTime + epochsNumber * epochLength) { + revert ContentAssetErrors.AssetExpired(tokenId); + } bytes32 unfinalizedState = uss.getUnfinalizedState(tokenId); uint256 unfinalizedStateIndex = cas.getAssertionIdsLength(tokenId); - if (unfinalizedState == bytes32(0)) + if (unfinalizedState == bytes32(0)) { revert ContentAssetErrors.NoPendingUpdate(contentAssetStorageAddress, tokenId); - else if ( + } else if ( block.timestamp <= sasProxy.getUpdateCommitsDeadline(keccak256(abi.encodePacked(agreementId, unfinalizedStateIndex))) - ) + ) { revert ContentAssetErrors.PendingUpdateFinalization( contentAssetStorageAddress, tokenId, unfinalizedStateIndex ); + } uint96 updateTokenAmount = sasProxy.getAgreementUpdateTokenAmount(agreementId); - if (sasProxy.agreementV1Exists(agreementId)) sasProxy.deleteServiceAgreementV1U1Object(agreementId); - else sasProxy.setAgreementUpdateTokenAmount(agreementId, 0); + if (sasProxy.agreementV1Exists(agreementId)) { + sasProxy.deleteServiceAgreementV1U1Object(agreementId); + } else { + sasProxy.setAgreementUpdateTokenAmount(agreementId, 0); + } sasProxy.transferV1U1AgreementTokens(msg.sender, updateTokenAmount); @@ -295,8 +310,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { bytes32 unfinalizedState = unfinalizedStateStorage.getUnfinalizedState(tokenId); - if (unfinalizedState != bytes32(0)) + if (unfinalizedState != bytes32(0)) { revert ContentAssetErrors.UpdateIsNotFinalized(contentAssetStorageAddress, tokenId, unfinalizedState); + } bytes memory keyword = abi.encodePacked(contentAssetStorageAddress, cas.getAssertionIdByIndex(tokenId, 0)); @@ -310,8 +326,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { uint128 epochLength; (startTime, oldEpochsNumber, epochLength, , ) = serviceAgreementStorageProxy.getAgreementData(agreementId); - if (block.timestamp > startTime + oldEpochsNumber * epochLength) + if (block.timestamp > startTime + oldEpochsNumber * epochLength) { revert ContentAssetErrors.AssetExpired(tokenId); + } sasV1.extendStoringPeriod(msg.sender, agreementId, epochsNumber, tokenAmount); @@ -326,8 +343,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { bytes32 unfinalizedState = unfinalizedStateStorage.getUnfinalizedState(tokenId); - if (unfinalizedState != bytes32(0)) + if (unfinalizedState != bytes32(0)) { revert ContentAssetErrors.UpdateIsNotFinalized(contentAssetStorageAddress, tokenId, unfinalizedState); + } bytes memory keyword = abi.encodePacked(contentAssetStorageAddress, cas.getAssertionIdByIndex(tokenId, 0)); @@ -341,7 +359,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { uint128 epochLength; (startTime, epochsNumber, epochLength, , ) = serviceAgreementStorageProxy.getAgreementData(agreementId); - if (block.timestamp > startTime + epochsNumber * epochLength) revert ContentAssetErrors.AssetExpired(tokenId); + if (block.timestamp > startTime + epochsNumber * epochLength) { + revert ContentAssetErrors.AssetExpired(tokenId); + } sasV1.addTokens(msg.sender, agreementId, tokenAmount); @@ -356,8 +376,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { bytes32 unfinalizedState = unfinalizedStateStorage.getUnfinalizedState(tokenId); - if (unfinalizedState == bytes32(0)) + if (unfinalizedState == bytes32(0)) { revert ContentAssetErrors.NoPendingUpdate(contentAssetStorageAddress, tokenId); + } bytes memory keyword = abi.encodePacked(contentAssetStorageAddress, cas.getAssertionIdByIndex(tokenId, 0)); @@ -371,7 +392,9 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable { uint128 epochLength; (startTime, epochsNumber, epochLength, , ) = serviceAgreementStorageProxy.getAgreementData(agreementId); - if (block.timestamp > startTime + epochsNumber * epochLength) revert ContentAssetErrors.AssetExpired(tokenId); + if (block.timestamp > startTime + epochsNumber * epochLength) { + revert ContentAssetErrors.AssetExpired(tokenId); + } sasV1.addUpdateTokens(msg.sender, agreementId, tokenAmount); diff --git a/contracts/v1/storage/ServiceAgreementStorageProxy.sol b/contracts/v1/storage/ServiceAgreementStorageProxy.sol index fbed9867..cc216ea1 100644 --- a/contracts/v1/storage/ServiceAgreementStorageProxy.sol +++ b/contracts/v1/storage/ServiceAgreementStorageProxy.sol @@ -93,8 +93,11 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial } function deleteServiceAgreementObject(bytes32 agreementId) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.deleteServiceAgreementObject(agreementId); - else storageV1U1.deleteServiceAgreementObject(agreementId); + if (this.agreementV1Exists(agreementId)) { + storageV1.deleteServiceAgreementObject(agreementId); + } else { + storageV1U1.deleteServiceAgreementObject(agreementId); + } } function getAgreementData( @@ -121,47 +124,73 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial [tokenAmount, storageV1U1.getAgreementUpdateTokenAmount(agreementId)], scoreFunctionIdAndProofWindowOffsetPerc ); - } else return storageV1U1.getAgreementData(agreementId); + } else { + return storageV1U1.getAgreementData(agreementId); + } } function getAgreementStartTime(bytes32 agreementId) external view returns (uint256) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementStartTime(agreementId); - else return storageV1U1.getAgreementStartTime(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementStartTime(agreementId); + } else { + return storageV1U1.getAgreementStartTime(agreementId); + } } function setAgreementStartTime(bytes32 agreementId, uint256 startTime) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementStartTime(agreementId, startTime); - else storageV1U1.setAgreementStartTime(agreementId, startTime); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementStartTime(agreementId, startTime); + } else { + storageV1U1.setAgreementStartTime(agreementId, startTime); + } } function getAgreementEpochsNumber(bytes32 agreementId) external view returns (uint16) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementEpochsNumber(agreementId); - else return storageV1U1.getAgreementEpochsNumber(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementEpochsNumber(agreementId); + } else { + return storageV1U1.getAgreementEpochsNumber(agreementId); + } } function setAgreementEpochsNumber(bytes32 agreementId, uint16 epochsNumber) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementEpochsNumber(agreementId, epochsNumber); - else storageV1U1.setAgreementEpochsNumber(agreementId, epochsNumber); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementEpochsNumber(agreementId, epochsNumber); + } else { + storageV1U1.setAgreementEpochsNumber(agreementId, epochsNumber); + } } function getAgreementEpochLength(bytes32 agreementId) external view returns (uint128) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementEpochLength(agreementId); - else return storageV1U1.getAgreementEpochLength(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementEpochLength(agreementId); + } else { + return storageV1U1.getAgreementEpochLength(agreementId); + } } function setAgreementEpochLength(bytes32 agreementId, uint128 epochLength) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementEpochLength(agreementId, epochLength); - else storageV1U1.setAgreementEpochLength(agreementId, epochLength); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementEpochLength(agreementId, epochLength); + } else { + storageV1U1.setAgreementEpochLength(agreementId, epochLength); + } } function getAgreementTokenAmount(bytes32 agreementId) external view returns (uint96) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementTokenAmount(agreementId); - else return storageV1U1.getAgreementTokenAmount(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementTokenAmount(agreementId); + } else { + return storageV1U1.getAgreementTokenAmount(agreementId); + } } function setAgreementTokenAmount(bytes32 agreementId, uint96 tokenAmount) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementTokenAmount(agreementId, tokenAmount); - else storageV1U1.setAgreementTokenAmount(agreementId, tokenAmount); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementTokenAmount(agreementId, tokenAmount); + } else { + storageV1U1.setAgreementTokenAmount(agreementId, tokenAmount); + } } function getAgreementUpdateTokenAmount(bytes32 agreementId) external view returns (uint96) { @@ -173,27 +202,38 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial } function getAgreementScoreFunctionId(bytes32 agreementId) external view returns (uint8) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementScoreFunctionId(agreementId); - else return storageV1U1.getAgreementScoreFunctionId(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementScoreFunctionId(agreementId); + } else { + return storageV1U1.getAgreementScoreFunctionId(agreementId); + } } function setAgreementScoreFunctionId(bytes32 agreementId, uint8 newScoreFunctionId) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementScoreFunctionId(agreementId, newScoreFunctionId); - else storageV1U1.setAgreementScoreFunctionId(agreementId, newScoreFunctionId); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementScoreFunctionId(agreementId, newScoreFunctionId); + } else { + storageV1U1.setAgreementScoreFunctionId(agreementId, newScoreFunctionId); + } } function getAgreementProofWindowOffsetPerc(bytes32 agreementId) external view returns (uint8) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementProofWindowOffsetPerc(agreementId); - else return storageV1U1.getAgreementProofWindowOffsetPerc(agreementId); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementProofWindowOffsetPerc(agreementId); + } else { + return storageV1U1.getAgreementProofWindowOffsetPerc(agreementId); + } } function setAgreementProofWindowOffsetPerc( bytes32 agreementId, uint8 proofWindowOffsetPerc ) external onlyContracts { - if (this.agreementV1Exists(agreementId)) + if (this.agreementV1Exists(agreementId)) { storageV1.setAgreementProofWindowOffsetPerc(agreementId, proofWindowOffsetPerc); - else storageV1U1.setAgreementProofWindowOffsetPerc(agreementId, proofWindowOffsetPerc); + } else { + storageV1U1.setAgreementProofWindowOffsetPerc(agreementId, proofWindowOffsetPerc); + } } function getV1U1AgreementEpochSubmissionHead( @@ -226,18 +266,27 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial } function incrementAgreementRewardedNodesNumber(bytes32 agreementId, uint16 epoch) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.incrementAgreementRewardedNodesNumber(agreementId, epoch); - else storageV1U1.incrementAgreementRewardedNodesNumber(agreementId, epoch); + if (this.agreementV1Exists(agreementId)) { + storageV1.incrementAgreementRewardedNodesNumber(agreementId, epoch); + } else { + storageV1U1.incrementAgreementRewardedNodesNumber(agreementId, epoch); + } } function decrementAgreementRewardedNodesNumber(bytes32 agreementId, uint16 epoch) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.decrementAgreementRewardedNodesNumber(agreementId, epoch); - else storageV1U1.decrementAgreementRewardedNodesNumber(agreementId, epoch); + if (this.agreementV1Exists(agreementId)) { + storageV1.decrementAgreementRewardedNodesNumber(agreementId, epoch); + } else { + storageV1U1.decrementAgreementRewardedNodesNumber(agreementId, epoch); + } } function getAgreementRewardedNodesNumber(bytes32 agreementId, uint16 epoch) external view returns (uint32) { - if (this.agreementV1Exists(agreementId)) return storageV1.getAgreementRewardedNodesNumber(agreementId, epoch); - else return storageV1U1.getAgreementRewardedNodesNumber(agreementId, epoch); + if (this.agreementV1Exists(agreementId)) { + return storageV1.getAgreementRewardedNodesNumber(agreementId, epoch); + } else { + return storageV1U1.getAgreementRewardedNodesNumber(agreementId, epoch); + } } function setAgreementRewardedNodesNumber( @@ -245,14 +294,19 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial uint16 epoch, uint32 rewardedNodesNumber ) external onlyContracts { - if (this.agreementV1Exists(agreementId)) + if (this.agreementV1Exists(agreementId)) { storageV1.setAgreementRewardedNodesNumber(agreementId, epoch, rewardedNodesNumber); - else storageV1U1.setAgreementRewardedNodesNumber(agreementId, epoch, rewardedNodesNumber); + } else { + storageV1U1.setAgreementRewardedNodesNumber(agreementId, epoch, rewardedNodesNumber); + } } function deleteAgreementRewardedNodesNumber(bytes32 agreementId, uint16 epoch) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.setAgreementRewardedNodesNumber(agreementId, epoch, 0); - else storageV1U1.deleteAgreementRewardedNodesNumber(agreementId, epoch); + if (this.agreementV1Exists(agreementId)) { + storageV1.setAgreementRewardedNodesNumber(agreementId, epoch, 0); + } else { + storageV1U1.deleteAgreementRewardedNodesNumber(agreementId, epoch); + } } function createV1CommitSubmissionObject( @@ -276,62 +330,93 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial } function deleteCommitSubmissionsObject(bytes32 commitId) external onlyContracts { - if (this.commitV1U1Exists(commitId)) storageV1U1.deleteEpochStateCommitSubmissionsObject(commitId); - else storageV1.deleteCommitSubmissionsObject(commitId); + if (this.commitV1U1Exists(commitId)) { + storageV1U1.deleteEpochStateCommitSubmissionsObject(commitId); + } else { + storageV1.deleteCommitSubmissionsObject(commitId); + } } function getCommitSubmission( bytes32 commitId ) external view returns (ServiceAgreementStructsV1.CommitSubmission memory) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.getEpochStateCommitSubmission(commitId); - else return storageV1.getCommitSubmission(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.getEpochStateCommitSubmission(commitId); + } else { + return storageV1.getCommitSubmission(commitId); + } } function getCommitSubmissionIdentityId(bytes32 commitId) external view returns (uint72) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.getEpochStateCommitSubmissionIdentityId(commitId); - else return storageV1.getCommitSubmissionIdentityId(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.getEpochStateCommitSubmissionIdentityId(commitId); + } else { + return storageV1.getCommitSubmissionIdentityId(commitId); + } } function setCommitSubmissionIdentityId(bytes32 commitId, uint72 identityId) external onlyContracts { - if (this.commitV1U1Exists(commitId)) storageV1U1.setEpochStateCommitSubmissionIdentityId(commitId, identityId); - else storageV1.setCommitSubmissionIdentityId(commitId, identityId); + if (this.commitV1U1Exists(commitId)) { + storageV1U1.setEpochStateCommitSubmissionIdentityId(commitId, identityId); + } else { + storageV1.setCommitSubmissionIdentityId(commitId, identityId); + } } function getCommitSubmissionPrevIdentityId(bytes32 commitId) external view returns (uint72) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.getEpochStateCommitSubmissionPrevIdentityId(commitId); - else return storageV1.getCommitSubmissionPrevIdentityId(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.getEpochStateCommitSubmissionPrevIdentityId(commitId); + } else { + return storageV1.getCommitSubmissionPrevIdentityId(commitId); + } } function setCommitSubmissionPrevIdentityId(bytes32 commitId, uint72 prevIdentityId) external onlyContracts { - if (this.commitV1U1Exists(commitId)) + if (this.commitV1U1Exists(commitId)) { storageV1U1.setEpochStateCommitSubmissionPrevIdentityId(commitId, prevIdentityId); - else storageV1.setCommitSubmissionPrevIdentityId(commitId, prevIdentityId); + } else { + storageV1.setCommitSubmissionPrevIdentityId(commitId, prevIdentityId); + } } function getCommitSubmissionNextIdentityId(bytes32 commitId) external view returns (uint72) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.getEpochStateCommitSubmissionNextIdentityId(commitId); - else return storageV1.getCommitSubmissionNextIdentityId(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.getEpochStateCommitSubmissionNextIdentityId(commitId); + } else { + return storageV1.getCommitSubmissionNextIdentityId(commitId); + } } function setCommitSubmissionNextIdentityId(bytes32 commitId, uint72 nextIdentityId) external onlyContracts { - if (this.commitV1U1Exists(commitId)) + if (this.commitV1U1Exists(commitId)) { storageV1U1.setEpochStateCommitSubmissionNextIdentityId(commitId, nextIdentityId); - else storageV1.setCommitSubmissionNextIdentityId(commitId, nextIdentityId); + } else { + storageV1.setCommitSubmissionNextIdentityId(commitId, nextIdentityId); + } } function getCommitSubmissionScore(bytes32 commitId) external view returns (uint40) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.getEpochStateCommitSubmissionScore(commitId); - else return storageV1.getCommitSubmissionScore(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.getEpochStateCommitSubmissionScore(commitId); + } else { + return storageV1.getCommitSubmissionScore(commitId); + } } function setCommitSubmissionScore(bytes32 commitId, uint40 score) external onlyContracts { - if (this.commitV1U1Exists(commitId)) storageV1U1.setEpochStateCommitSubmissionScore(commitId, score); - else storageV1.setCommitSubmissionScore(commitId, score); + if (this.commitV1U1Exists(commitId)) { + storageV1U1.setEpochStateCommitSubmissionScore(commitId, score); + } else { + storageV1.setCommitSubmissionScore(commitId, score); + } } function commitSubmissionExists(bytes32 commitId) external view returns (bool) { - if (this.commitV1U1Exists(commitId)) return storageV1U1.epochStateCommitSubmissionExists(commitId); - else return storageV1.commitSubmissionExists(commitId); + if (this.commitV1U1Exists(commitId)) { + return storageV1U1.epochStateCommitSubmissionExists(commitId); + } else { + return storageV1.commitSubmissionExists(commitId); + } } function incrementCommitsCount(bytes32 epochStateId) external onlyContracts { @@ -367,8 +452,11 @@ contract ServiceAgreementStorageProxy is Named, Versioned, HubDependent, Initial } function transferAgreementTokens(bytes32 agreementId, address receiver, uint96 tokenAmount) external onlyContracts { - if (this.agreementV1Exists(agreementId)) storageV1.transferAgreementTokens(receiver, tokenAmount); - else storageV1U1.transferAgreementTokens(receiver, tokenAmount); + if (this.agreementV1Exists(agreementId)) { + storageV1.transferAgreementTokens(receiver, tokenAmount); + } else { + storageV1U1.transferAgreementTokens(receiver, tokenAmount); + } } function transferV1AgreementTokens(address receiver, uint96 tokenAmount) external onlyContracts { diff --git a/contracts/v1/utils/ByteArr.sol b/contracts/v1/utils/ByteArr.sol index 642bed16..2989ad55 100644 --- a/contracts/v1/utils/ByteArr.sol +++ b/contracts/v1/utils/ByteArr.sol @@ -5,7 +5,9 @@ pragma solidity ^0.8.16; library ByteArr { function indexOf(bytes32[] storage self, bytes32 item) internal view returns (uint index, bool isThere) { for (uint i; i < self.length; i++) { - if (self[i] == item) return (i, true); + if (self[i] == item) { + return (i, true); + } } return (0, false); } diff --git a/contracts/v1/utils/UnorderedIndexableContractDynamicSet.sol b/contracts/v1/utils/UnorderedIndexableContractDynamicSet.sol index d072f291..abb9ae88 100644 --- a/contracts/v1/utils/UnorderedIndexableContractDynamicSet.sol +++ b/contracts/v1/utils/UnorderedIndexableContractDynamicSet.sol @@ -60,7 +60,9 @@ library UnorderedIndexableContractDynamicSetLib { } function exists(Set storage self, uint8 id) internal view returns (bool) { - if (size(self) == 0) return false; + if (size(self) == 0) { + return false; + } return self.contractList[self.indexPointers[id]].id == id; } diff --git a/contracts/v1/utils/UnorderedNamedContractDynamicSet.sol b/contracts/v1/utils/UnorderedNamedContractDynamicSet.sol index c8c15e02..39f42788 100644 --- a/contracts/v1/utils/UnorderedNamedContractDynamicSet.sol +++ b/contracts/v1/utils/UnorderedNamedContractDynamicSet.sol @@ -93,14 +93,18 @@ library UnorderedNamedContractDynamicSetLib { } function exists(Set storage self, string calldata name) internal view returns (bool) { - if (size(self) == 0) return false; + if (size(self) == 0) { + return false; + } return keccak256(abi.encodePacked(self.contractList[self.stringIndexPointers[name]].name)) == keccak256(abi.encodePacked(name)); } function exists(Set storage self, address addr) internal view returns (bool) { - if (size(self) == 0) return false; + if (size(self) == 0) { + return false; + } return addr == self.contractList[self.addressIndexPointers[addr]].addr; } diff --git a/contracts/v2/CommitManagerV1.sol b/contracts/v2/CommitManagerV1.sol index 1f2aecb0..b792e18e 100644 --- a/contracts/v2/CommitManagerV1.sol +++ b/contracts/v2/CommitManagerV1.sol @@ -76,27 +76,28 @@ contract CommitManagerV2 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)); } @@ -107,16 +108,17 @@ contract CommitManagerV2 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(); ServiceAgreementStructsV1.CommitSubmission[] @@ -148,17 +150,17 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { bytes32 agreementId = sha256(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); uint256 actualCommitWindowStart = (sasProxy.getAgreementStartTime(agreementId) + args.epoch * epochLength); @@ -210,17 +212,17 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { bytes32 agreementId = sha256(abi.encodePacked(args.assetContract, args.tokenId, args.keyword)); - if (!sasProxy.agreementV1Exists(agreementId)) + if (!sasProxy.agreementV1Exists(agreementId)) { revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId); - - if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LINEAR_SUM_ID) + } + if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LINEAR_SUM_ID) { revert ServiceAgreementErrorsV2.InvalidProximityScoreFunctionsPairId( agreementId, args.epoch, sasProxy.getAgreementScoreFunctionId(agreementId), block.timestamp ); - + } if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) { uint128 epochLength = sasProxy.getAgreementEpochLength(agreementId); uint256 actualCommitWindowStart = (sasProxy.getAgreementStartTime(agreementId) + args.epoch * epochLength); @@ -334,7 +336,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { ); // Verify that closestNode is in smaller arc between leftNode and rightNode - if (!isBetween) + if (!isBetween) { revert CommitManagerErrorsV2.ClosestNodeNotInNeighborhood( agreementId, epoch, @@ -343,9 +345,9 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { rightEdgeNodeIndex, block.timestamp ); - + } // Verify number of nodes between leftNode and rightNode (should be R2) - if (neighborhoodSize != parametersStorage.r2()) + if (neighborhoodSize != parametersStorage.r2()) { revert CommitManagerErrorsV2.InvalidNeighborhoodSize( agreementId, epoch, @@ -356,12 +358,12 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { neighborhoodSize, block.timestamp ); - + } // Verify that closestNode is indeed closest if ( closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestPrevIdentityId), keyword) || closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestNextIdentityId), keyword) - ) + ) { revert CommitManagerErrorsV2.InvalidClosestNode( agreementId, epoch, @@ -371,9 +373,9 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { 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, ps.getNodeId(rightEdgeNextIdentityId), keyword)) + if (leftEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(rightEdgeNextIdentityId), keyword)) { revert CommitManagerErrorsV2.InvalidLeftEdgeNode( agreementId, epoch, @@ -383,9 +385,9 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { 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, ps.getNodeId(leftEdgePrevIdentityId), keyword)) + if (rightEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword)) { revert CommitManagerErrorsV2.InvalidRightEdgeNode( agreementId, epoch, @@ -395,7 +397,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword), block.timestamp ); - + } return (nodesCount, (leftEdgeDistance > rightEdgeDistance) ? leftEdgeDistance : rightEdgeDistance); } @@ -411,14 +413,14 @@ contract CommitManagerV2 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); ParametersStorage params = parametersStorage; @@ -435,7 +437,7 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { } } - if (!reqs[3] && (i >= r0)) + if (!reqs[3] && (i >= r0)) { revert ServiceAgreementErrorsV1.NodeNotAwarded( agreementId, epoch, @@ -443,18 +445,18 @@ contract CommitManagerV2 is Named, Versioned, ContractStatus, Initializable { 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); @@ -468,10 +470,11 @@ contract CommitManagerV2 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( diff --git a/contracts/v2/CommitManagerV1U1.sol b/contracts/v2/CommitManagerV1U1.sol index 99ca4be0..89a4beee 100644 --- a/contracts/v2/CommitManagerV1U1.sol +++ b/contracts/v2/CommitManagerV1U1.sol @@ -95,27 +95,28 @@ contract CommitManagerV2U1 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)); } @@ -129,16 +130,17 @@ contract CommitManagerV2U1 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)) ); @@ -153,15 +155,17 @@ contract CommitManagerV2U1 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(); @@ -195,17 +199,17 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { bytes32 agreementId = sha256(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; if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) { @@ -260,17 +264,17 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { bytes32 agreementId = sha256(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) != _LINEAR_SUM_ID) + } + if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LINEAR_SUM_ID) { revert ServiceAgreementErrorsV2.InvalidProximityScoreFunctionsPairId( agreementId, args.epoch, sasProxy.getAgreementScoreFunctionId(agreementId), block.timestamp ); - + } uint256 latestFinalizedStateIndex = AbstractAsset(args.assetContract).getAssertionIdsLength(args.tokenId) - 1; if (!reqs[0] && !isCommitWindowOpen(agreementId, args.epoch)) { @@ -340,22 +344,22 @@ contract CommitManagerV2U1 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); - + } bytes32 agreementId = sha256(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( keccak256(abi.encodePacked(agreementId, unfinalizedStateIndex)) @@ -442,22 +446,22 @@ contract CommitManagerV2U1 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); - + } bytes32 agreementId = sha256(abi.encodePacked(args.assetContract, args.tokenId, args.keyword)); - if (!sasProxy.agreementV1U1Exists(agreementId)) + if (!sasProxy.agreementV1U1Exists(agreementId)) { revert ServiceAgreementErrorsV1.ServiceAgreementDoesntExist(agreementId); - - if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LINEAR_SUM_ID) + } + if (sasProxy.getAgreementScoreFunctionId(agreementId) != _LINEAR_SUM_ID) { revert ServiceAgreementErrorsV2.InvalidProximityScoreFunctionsPairId( agreementId, args.epoch, sasProxy.getAgreementScoreFunctionId(agreementId), block.timestamp ); - + } if (!reqs[2] && !isUpdateCommitWindowOpen(agreementId, args.epoch, unfinalizedStateIndex)) { uint256 commitWindowEnd = sasProxy.getUpdateCommitsDeadline( keccak256(abi.encodePacked(agreementId, unfinalizedStateIndex)) @@ -520,8 +524,9 @@ contract CommitManagerV2U1 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, sasProxy.getAgreementTokenAmount(agreementId) + sasProxy.getAgreementUpdateTokenAmount(agreementId) @@ -601,7 +606,7 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { ); // Verify that closestNode is in smaller arc between leftNode and rightNode - if (!isBetween) + if (!isBetween) { revert CommitManagerErrorsV2.ClosestNodeNotInNeighborhood( agreementId, epoch, @@ -610,9 +615,9 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { rightEdgeNodeIndex, block.timestamp ); - + } // Verify number of nodes between leftNode and rightNode (should be R2) - if (neighborhoodSize != parametersStorage.r2()) + if (neighborhoodSize != parametersStorage.r2()) { revert CommitManagerErrorsV2.InvalidNeighborhoodSize( agreementId, epoch, @@ -623,12 +628,12 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { neighborhoodSize, block.timestamp ); - + } // Verify that closestNode is indeed closest if ( closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestPrevIdentityId), keyword) || closestDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(closestNextIdentityId), keyword) - ) + ) { revert CommitManagerErrorsV2.InvalidClosestNode( agreementId, epoch, @@ -638,9 +643,9 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { 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, ps.getNodeId(rightEdgeNextIdentityId), keyword)) + if (leftEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(rightEdgeNextIdentityId), keyword)) { revert CommitManagerErrorsV2.InvalidLeftEdgeNode( agreementId, epoch, @@ -650,9 +655,9 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { 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, ps.getNodeId(leftEdgePrevIdentityId), keyword)) + if (rightEdgeDistance > ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword)) { revert CommitManagerErrorsV2.InvalidRightEdgeNode( agreementId, epoch, @@ -662,7 +667,7 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { ls.calculateDistance(hashFunctionId, ps.getNodeId(leftEdgePrevIdentityId), keyword), block.timestamp ); - + } return (nodesCount, (leftEdgeDistance > rightEdgeDistance) ? leftEdgeDistance : rightEdgeDistance); } @@ -679,7 +684,7 @@ contract CommitManagerV2U1 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, @@ -687,7 +692,7 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { identityId, profileStorage.getNodeId(identityId) ); - + } bytes32 refCommitId = sasProxy.getV1U1AgreementEpochSubmissionHead(agreementId, epoch, stateIndex); ParametersStorage params = parametersStorage; @@ -704,7 +709,7 @@ contract CommitManagerV2U1 is Named, Versioned, ContractStatus, Initializable { } } - if (!reqs[5] && (i >= r0)) + if (!reqs[5] && (i >= r0)) { revert ServiceAgreementErrorsV1U1.NodeNotAwarded( agreementId, epoch, @@ -713,18 +718,18 @@ contract CommitManagerV2U1 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); @@ -738,10 +743,11 @@ contract CommitManagerV2U1 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))); } diff --git a/contracts/v2/ShardingTable.sol b/contracts/v2/ShardingTable.sol index c651ce07..23497770 100644 --- a/contracts/v2/ShardingTable.sol +++ b/contracts/v2/ShardingTable.sol @@ -132,30 +132,35 @@ contract ShardingTableV2 is Named, Versioned, ContractStatus, Initializable { ShardingTableStorageV2 sts = shardingTableStorage; ProfileStorage ps = profileStorage; + if (sts.nodeExists(identityId)) { + revert ShardingTableErrors.NodeAlreadyInTheShardingTable(identityId); + } + if (index != 0) { uint256 prevNodeHashRingPosition = sts.getHashRingPositionByIndex(index - 1); // Check that the new Node is indeed on the right from the prevNode // Also allows new Head insertion as prevNode.hashRingPosition will be 0 in such case - if (newNodeHashRingPosition < prevNodeHashRingPosition) + if (newNodeHashRingPosition < prevNodeHashRingPosition) { revert ShardingTableErrors.InvalidIndexWithRespectToPreviousNode( identityId, newNodeHashRingPosition, prevNodeHashRingPosition ); + } } ShardingTableStructsV2.Node memory nextNode = sts.getNodeByIndex(index); // Check that the new Node is indeed on the left from the nextNode // Check is skipped when inserting new Tail - if (nextNode.identityId != NULL && newNodeHashRingPosition > nextNode.hashRingPosition) + if (nextNode.identityId != NULL && newNodeHashRingPosition > nextNode.hashRingPosition) { revert ShardingTableErrors.InvalidIndexWithRespectToNextNode( identityId, newNodeHashRingPosition, nextNode.hashRingPosition ); - + } // Create node object + set index pointer to new identityId + increment total nodes count sts.createNodeObject(newNodeHashRingPosition, ps.getNodeId(identityId), identityId, index); sts.setIdentityId(index, identityId); @@ -191,8 +196,9 @@ contract ShardingTableV2 is Named, Versioned, ContractStatus, Initializable { ShardingTableStructsV1.NodeInfo[] memory nodesPage; ShardingTableStorageV2 sts = shardingTableStorage; - if ((sts.nodesCount() == 0) || (nodesNumber == 0)) return nodesPage; - + if ((sts.nodesCount() == 0) || (nodesNumber == 0)) { + return nodesPage; + } ShardingTableStructsV2.Node memory startingNode = sts.getNode(startingIdentityId); require((startingIdentityId == NULL) || (startingNode.identityId != NULL), "Wrong starting Identity ID"); diff --git a/contracts/v2/Staking.sol b/contracts/v2/Staking.sol index 191b836c..19581b03 100644 --- a/contracts/v2/Staking.sol +++ b/contracts/v2/Staking.sol @@ -125,17 +125,22 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { } function startStakeWithdrawal(uint72 identityId, uint96 sharesToBurn) external { - if (sharesToBurn == 0) revert StakingErrors.ZeroSharesAmount(); + if (sharesToBurn == 0) { + revert StakingErrors.ZeroSharesAmount(); + } ProfileStorage ps = profileStorage; StakingStorage ss = stakingStorage; - if (!ps.profileExists(identityId)) revert ProfileErrors.ProfileDoesntExist(identityId); + if (!ps.profileExists(identityId)) { + revert ProfileErrors.ProfileDoesntExist(identityId); + } Shares sharesContract = Shares(ps.getSharesContractAddress(identityId)); - if (sharesToBurn > sharesContract.balanceOf(msg.sender)) + if (sharesToBurn > sharesContract.balanceOf(msg.sender)) { revert TokenErrors.TooLowBalance(address(sharesContract), sharesContract.balanceOf(msg.sender)); + } uint96 oldStake = ss.totalStakes(identityId); uint96 stakeWithdrawalAmount = uint96((uint256(oldStake) * sharesToBurn) / sharesContract.totalSupply()); @@ -149,8 +154,9 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { ss.setTotalStake(identityId, newStake); sharesContract.burnFrom(msg.sender, sharesToBurn); - if (shardingTableStorage.nodeExists(identityId) && (newStake < params.minimumStake())) + if (shardingTableStorage.nodeExists(identityId) && (newStake < params.minimumStake())) { shardingTableContract.removeNode(identityId); + } emit StakeWithdrawalStarted( identityId, @@ -166,17 +172,21 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { function withdrawStake(uint72 identityId) external { ProfileStorage ps = profileStorage; - if (!ps.profileExists(identityId)) revert ProfileErrors.ProfileDoesntExist(identityId); - + if (!ps.profileExists(identityId)) { + revert ProfileErrors.ProfileDoesntExist(identityId); + } StakingStorage ss = stakingStorage; uint96 stakeWithdrawalAmount; uint256 withdrawalTimestamp; (stakeWithdrawalAmount, withdrawalTimestamp) = ss.withdrawalRequests(identityId, msg.sender); - if (stakeWithdrawalAmount == 0) revert StakingErrors.WithdrawalWasntInitiated(); - if (block.timestamp < withdrawalTimestamp) + if (stakeWithdrawalAmount == 0) { + revert StakingErrors.WithdrawalWasntInitiated(); + } + if (block.timestamp < withdrawalTimestamp) { revert StakingErrors.WithdrawalPeriodPending(block.timestamp, withdrawalTimestamp); + } ss.deleteWithdrawalRequest(identityId, msg.sender); ss.transferStake(msg.sender, stakeWithdrawalAmount); @@ -207,11 +217,12 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { ShardingTableStorageV2 sts = shardingTableStorage; ParametersStorage params = parametersStorage; - if (!sts.nodeExists(identityId) && oldStake >= params.minimumStake()) - if (sts.nodesCount() >= params.shardingTableSizeLimit()) + if (!sts.nodeExists(identityId) && oldStake + delegatorsReward >= params.minimumStake()) { + if (sts.nodesCount() >= params.shardingTableSizeLimit()) { revert ShardingTableErrors.ShardingTableIsFull(); - - shardingTableContract.insertNode(identityId); + } + shardingTableContract.insertNode(identityId); + } } emit AccumulatedOperatorFeeIncreased( @@ -222,9 +233,11 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { ); address sasAddress; - if (sasProxy.agreementV1Exists(agreementId)) sasAddress = sasProxy.agreementV1StorageAddress(); - else sasAddress = sasProxy.agreementV1U1StorageAddress(); - + if (sasProxy.agreementV1Exists(agreementId)) { + sasAddress = sasProxy.agreementV1StorageAddress(); + } else { + sasAddress = sasProxy.agreementV1U1StorageAddress(); + } emit StakeIncreased(identityId, ps.getNodeId(identityId), sasAddress, oldStake, oldStake + delegatorsReward); emit RewardCollected( agreementId, @@ -238,12 +251,13 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { // solhint-disable-next-line no-empty-blocks function slash(uint72 identityId) external onlyContracts { - // TBD + // To be implemented } function startOperatorFeeChange(uint72 identityId, uint8 newOperatorFee) external onlyAdmin(identityId) { - if (newOperatorFee > 100) revert StakingErrors.InvalidOperatorFee(); - + if (newOperatorFee > 100) { + revert StakingErrors.InvalidOperatorFee(); + } NodeOperatorFeeChangesStorage nofcs = nodeOperatorFeeChangesStorage; uint256 feeChangeDelayEnd = block.timestamp > nofcs.delayFreePeriodEnd() @@ -266,8 +280,9 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { uint256 feeChangeDelayEnd; (newFee, feeChangeDelayEnd) = nofcs.operatorFeeChangeRequests(identityId); - if (block.timestamp < feeChangeDelayEnd) + if (block.timestamp < feeChangeDelayEnd) { revert StakingErrors.OperatorFeeChangeDelayPending(block.timestamp, feeChangeDelayEnd); + } stakingStorage.setOperatorFee(identityId, newFee); nofcs.deleteOperatorFeeChangeRequest(identityId); @@ -285,34 +300,42 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable { uint96 oldStake = ss.totalStakes(identityId); uint96 newStake = oldStake + stakeAmount; - if (!ps.profileExists(identityId)) revert ProfileErrors.ProfileDoesntExist(identityId); - if (stakeAmount > tknc.allowance(sender, address(this))) + if (!ps.profileExists(identityId)) { + revert ProfileErrors.ProfileDoesntExist(identityId); + } + if (stakeAmount > tknc.allowance(sender, address(this))) { revert TokenErrors.TooLowAllowance(address(tknc), tknc.allowance(sender, address(this))); - if (newStake > params.maximumStake()) revert StakingErrors.MaximumStakeExceeded(params.maximumStake()); + } + if (newStake > params.maximumStake()) { + revert StakingErrors.MaximumStakeExceeded(params.maximumStake()); + } Shares sharesContract = Shares(ps.getSharesContractAddress(identityId)); uint256 sharesMinted; - if (sharesContract.totalSupply() == 0) sharesMinted = stakeAmount; - else sharesMinted = ((uint256(stakeAmount) * sharesContract.totalSupply()) / oldStake); - + if (sharesContract.totalSupply() == 0) { + sharesMinted = stakeAmount; + } else { + sharesMinted = ((uint256(stakeAmount) * sharesContract.totalSupply()) / oldStake); + } sharesContract.mint(sender, sharesMinted); ss.setTotalStake(identityId, newStake); tknc.transferFrom(sender, address(ss), stakeAmount); if (!sts.nodeExists(identityId) && newStake >= params.minimumStake()) { - if (sts.nodesCount() >= params.shardingTableSizeLimit()) revert ShardingTableErrors.ShardingTableIsFull(); - + if (sts.nodesCount() >= params.shardingTableSizeLimit()) { + revert ShardingTableErrors.ShardingTableIsFull(); + } shardingTableContract.insertNode(identityId); } - emit StakeIncreased(identityId, ps.getNodeId(identityId), sender, oldStake, newStake); emit SharesMinted(identityId, address(sharesContract), sender, sharesMinted, sharesContract.totalSupply()); } function _checkAdmin(uint72 identityId) internal view virtual { - if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY)) + if (!identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY)) { revert GeneralErrors.OnlyProfileAdminFunction(msg.sender); + } } } diff --git a/contracts/v2/errors/ShardingTableErrors.sol b/contracts/v2/errors/ShardingTableErrors.sol index 167a5000..9b4b894b 100644 --- a/contracts/v2/errors/ShardingTableErrors.sol +++ b/contracts/v2/errors/ShardingTableErrors.sol @@ -3,6 +3,7 @@ pragma solidity ^0.8.16; library ShardingTableErrors { + error NodeAlreadyInTheShardingTable(uint72 identityId); error InvalidIndexWithRespectToPreviousNode( uint72 identityId, uint256 hashRingPosition, diff --git a/contracts/v2/scoring/LinearSum.sol b/contracts/v2/scoring/LinearSum.sol index 3b1ffe30..dfb95d99 100644 --- a/contracts/v2/scoring/LinearSum.sol +++ b/contracts/v2/scoring/LinearSum.sol @@ -106,8 +106,9 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende } function normalizeDistance(uint256 distance, uint256 maxDistance, uint72 nodesCount) public view returns (uint64) { - if (distance == 0) return 0; - + if (distance == 0) { + return 0; + } uint256 idealMaxDistance = (HASH_RING_SIZE / nodesCount) * ((parametersStorage.r2() + 1) / 2); uint256 divisor = (maxDistance <= idealMaxDistance) ? maxDistance : idealMaxDistance; diff --git a/contracts/v2/storage/assets/ContentAssetStorage.sol b/contracts/v2/storage/assets/ContentAssetStorage.sol index d3b191f5..3f72bfdc 100644 --- a/contracts/v2/storage/assets/ContentAssetStorage.sol +++ b/contracts/v2/storage/assets/ContentAssetStorage.sol @@ -44,7 +44,9 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 { } function lastTokenId() public view virtual returns (uint256) { - if (_tokenId == 1) revert ContentAssetErrors.NoMintedAssets(); + if (_tokenId == 1) { + revert ContentAssetErrors.NoMintedAssets(); + } unchecked { return _tokenId - 1; @@ -54,7 +56,9 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 { function setBaseURI(string memory baseURI) external virtual onlyHubOwner { tokenBaseURI = baseURI; - if (_tokenId > 1) emit BatchMetadataUpdate(1, lastTokenId()); + if (_tokenId > 1) { + emit BatchMetadataUpdate(1, lastTokenId()); + } } function _baseURI() internal view virtual override returns (string memory) { diff --git a/contracts/v2/utils/UnorderedNamedContractDynamicSet.sol b/contracts/v2/utils/UnorderedNamedContractDynamicSet.sol index 1de12ef1..916ef0f3 100644 --- a/contracts/v2/utils/UnorderedNamedContractDynamicSet.sol +++ b/contracts/v2/utils/UnorderedNamedContractDynamicSet.sol @@ -122,7 +122,9 @@ library UnorderedNamedContractDynamicSetLibV2 { UnorderedNamedContractDynamicSetStructs.Set storage self, string calldata name ) internal view returns (bool) { - if (size(self) == 0) return false; + if (size(self) == 0) { + return false; + } return keccak256(abi.encodePacked(self.contractList[self.stringIndexPointers[name]].name)) == keccak256(abi.encodePacked(name)); @@ -132,7 +134,9 @@ library UnorderedNamedContractDynamicSetLibV2 { UnorderedNamedContractDynamicSetStructs.Set storage self, address addr ) internal view returns (bool) { - if (size(self) == 0) return false; + if (size(self) == 0) { + return false; + } return addr == self.contractList[self.addressIndexPointers[addr]].addr; }