Skip to content

Commit

Permalink
Fixed linter errors, added function to clear old commits metadata for…
Browse files Browse the repository at this point in the history
… failed updates
  • Loading branch information
br-41n committed Mar 21, 2024
1 parent b9d4c9c commit 3810c7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
47 changes: 38 additions & 9 deletions contracts/v1/assets/ContentAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,6 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable {
block.timestamp + parametersStorage.updateCommitWindowDuration()
);

uint16 currentEpoch = uint16((block.timestamp - startTime) / epochLength);
bytes32 epochStateId = keccak256(abi.encodePacked(agreementId, currentEpoch, unfinalizedStateIndex));
if (sasProxy.getCommitsCount(epochStateId) != 0) {
sasProxy.deleteCommitsCount(epochStateId);
}
if (sasProxy.getV1U1AgreementEpochSubmissionHead(agreementId, currentEpoch, unfinalizedStateIndex) != 0) {
sasProxy.setV1U1AgreementEpochSubmissionHead(agreementId, currentEpoch, unfinalizedStateIndex, 0);
}

emit AssetStateUpdated(contentAssetStorageAddress, tokenId, unfinalizedStateIndex, updateTokenAmount);
}

Expand Down Expand Up @@ -315,6 +306,44 @@ contract ContentAsset is Named, Versioned, HubDependent, Initializable {
);
}

function clearOldCommitsMetadata(uint256 tokenId) external onlyAssetOwner(tokenId) {
ContentAssetStorage cas = contentAssetStorage;
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

address contentAssetStorageAddress = address(cas);

bytes memory keyword = abi.encodePacked(contentAssetStorageAddress, cas.getAssertionIdByIndex(tokenId, 0));

bytes32 agreementId = hashingProxy.callHashFunction(
HASH_FUNCTION_ID,
abi.encodePacked(contentAssetStorageAddress, tokenId, keyword)
);

uint256 unfinalizedStateIndex = cas.getAssertionIdsLength(tokenId);

if (
block.timestamp <=
sasProxy.getUpdateCommitsDeadline(keccak256(abi.encodePacked(agreementId, unfinalizedStateIndex)))
) {
revert ContentAssetErrors.PendingUpdateFinalization(
contentAssetStorageAddress,
tokenId,
unfinalizedStateIndex
);
}

uint256 startTime;
uint16 currentEpoch;
uint128 epochLength;
(startTime, , epochLength, , ) = sasProxy.getAgreementData(agreementId);

currentEpoch = uint16((block.timestamp - startTime) / epochLength);

sasProxy.deleteCommitsCount(keccak256(abi.encodePacked(agreementId, currentEpoch, unfinalizedStateIndex)));
sasProxy.deleteUpdateCommitsDeadline(keccak256(abi.encodePacked(agreementId, unfinalizedStateIndex)));
sasProxy.setV1U1AgreementEpochSubmissionHead(agreementId, currentEpoch, unfinalizedStateIndex, 0);
}

function extendAssetStoringPeriod(
uint256 tokenId,
uint16 epochsNumber,
Expand Down
2 changes: 1 addition & 1 deletion test/v1/unit/ContentAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ describe('@v1 @unit ContentAsset contract', function () {
const updateCommitDeadline = await ServiceAgreementStorageProxy.getUpdateCommitsDeadline(stateId);

await expect(commitCount).to.be.equal(0);
await expect(updateCommitDeadline.toNumber()).to.be.equal(0);
await expect(updateCommitDeadline).to.be.equal(0);
});

it('Cancel asset state update using non-owner account, expect to be reverted', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/v2/unit/CommitManagerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../../typechain';
import { ContentAssetStructs } from '../../../typechain/contracts/v1/assets/ContentAsset';
import { ServiceAgreementStructsV1 } from '../../../typechain/contracts/v1/CommitManagerV1';
import { ServiceAgreementStructsV2 } from '../../../typechain/contracts/v2/CommitManagerV2';
import { ServiceAgreementStructsV2 } from '../../../typechain/contracts/v2/CommitManagerV1.sol/CommitManagerV2';

type CommitManagerV2Fixture = {
accounts: SignerWithAddress[];
Expand Down
2 changes: 1 addition & 1 deletion test/v2/unit/CommitManagerV2U1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '../../../typechain';
import { ContentAssetStructs } from '../../../typechain/contracts/v1/assets/ContentAsset';
import { ServiceAgreementStructsV1 } from '../../../typechain/contracts/v1/CommitManagerV1U1';
import { ServiceAgreementStructsV2 } from '../../../typechain/contracts/v2/CommitManagerV2U1';
import { ServiceAgreementStructsV2 } from '../../../typechain/contracts/v2/CommitManagerV1U1.sol/CommitManagerV2U1';

const UINT256_MAX_BN = BigNumber.from(2).pow(256).sub(1);
const UINT64_MAX_BN = BigNumber.from(2).pow(64).sub(1);
Expand Down

0 comments on commit 3810c7a

Please sign in to comment.