Skip to content

Commit

Permalink
Fixed bug in the ContractSetV2 library, minor bug fixes for other con…
Browse files Browse the repository at this point in the history
…tracts / deployments scripts, helper functions, added tests for V2 contracts and additional tests for V1 versions of these contracts, added task to calculate sighash of the given function
  • Loading branch information
u-hubar committed Nov 20, 2023
1 parent bce2b71 commit 6e70f03
Show file tree
Hide file tree
Showing 40 changed files with 691 additions and 61 deletions.
26 changes: 13 additions & 13 deletions abi/ContentAssetStorageV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,6 @@
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "baseURI",
"type": "string"
}
],
"name": "_setBaseURI",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -679,6 +666,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "baseURI",
"type": "string"
}
],
"name": "setBaseURI",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down
6 changes: 3 additions & 3 deletions contracts/v1/CommitManagerV1U1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract CommitManagerV1U1 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) || !sasProxy.agreementV1U1Exists(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementDoesntExist(agreementId);

uint256 startTime = sasProxy.getAgreementStartTime(agreementId);
Expand Down Expand Up @@ -151,7 +151,7 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
) external view returns (ServiceAgreementStructsV1.CommitSubmission[] memory) {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;

if (sasProxy.agreementV1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementDoesntExist(agreementId);
if (epoch >= sasProxy.getAgreementEpochsNumber(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementHasBeenExpired(
Expand Down Expand Up @@ -195,7 +195,7 @@ contract CommitManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);

if (sasProxy.agreementV1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementDoesntExist(agreementId);

uint256 latestFinalizedStateIndex = AbstractAsset(args.assetContract).getAssertionIdsLength(args.tokenId) - 1;
Expand Down
4 changes: 2 additions & 2 deletions contracts/v1/HubController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract HubController is Named, Versioned, ContractStatus, Ownable {
string private constant _NAME = "HubController";
string private constant _VERSION = "1.0.0";
string private constant _VERSION = "1.0.1";

// solhint-disable-next-line no-empty-blocks
constructor(address hubAddress) ContractStatus(hubAddress) {}
Expand Down Expand Up @@ -43,7 +43,7 @@ contract HubController is Named, Versioned, ContractStatus, Ownable {
*/
function forwardCall(address target, bytes calldata data) public onlyOwnerOrMultiSigOwner returns (bytes memory) {
// Check if the target contract is registered in the Hub
require(hub.isContract(target), "Target contract isn't in the Hub");
require(hub.isContract(target) || hub.isAssetStorage(target), "Target contract isn't in the Hub");

// Perform the function call to the target contract with the specified calldata
(bool success, bytes memory result) = target.call{value: 0}(data);
Expand Down
4 changes: 2 additions & 2 deletions contracts/v1/ProofManagerV1U1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ contract ProofManagerV1U1 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) || !sasProxy.agreementV1U1Exists(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementDoesntExist(agreementId);

uint256 startTime = sasProxy.getAgreementStartTime(agreementId);
Expand Down Expand Up @@ -110,7 +110,7 @@ contract ProofManagerV1U1 is Named, Versioned, ContractStatus, Initializable {
abi.encodePacked(args.assetContract, args.tokenId, args.keyword)
);

if (sasProxy.agreementV1Exists(agreementId))
if (sasProxy.agreementV1Exists(agreementId) || !sasProxy.agreementV1U1Exists(agreementId))
revert ServiceAgreementErrorsV1U1.ServiceAgreementDoesntExist(agreementId);

uint256 latestFinalizedStateIndex = AbstractAsset(args.assetContract).getAssertionIdsLength(args.tokenId) - 1;
Expand Down
4 changes: 2 additions & 2 deletions contracts/v1/storage/assets/ContentAssetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ contract ContentAssetStorage is AbstractAsset, ERC721 {
// solhint-disable-next-line no-empty-blocks
constructor(address hubAddress) AbstractAsset(hubAddress) ERC721("ContentAssetStorage", "DKG") {}

function name() public view override(Named, ERC721) returns (string memory) {
function name() public view virtual override(Named, ERC721) returns (string memory) {
return ERC721.name();
}

function version() external pure override returns (string memory) {
function version() external pure virtual override returns (string memory) {
return _VERSION;
}

Expand Down
18 changes: 15 additions & 3 deletions contracts/v2/storage/assets/ContentAssetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 {
using Strings for address;
using Strings for uint256;

string private constant _NAME = "ContentAssetStorage";
string private constant _VERSION = "2.0.0";

// Interface ID as defined in ERC-4906. This does not correspond to a traditional interface ID as ERC-4906 only
Expand All @@ -30,6 +29,10 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 {
blockchainName = blockchainName_;
}

function version() external pure override returns (string memory) {
return _VERSION;
}

/**
* @dev See {IERC165-supportsInterface}
*/
Expand Down Expand Up @@ -57,7 +60,16 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 {
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
string memory base = tokenBaseURI;
string memory _ual = string(
abi.encodePacked("dkg:", blockchainName, ":", block.chainid, "/", address(this), "/", tokenId.toString())
abi.encodePacked(
"did:dkg:",
blockchainName,
":",
Strings.toString(block.chainid),
"/",
address(this).toHexString(),
"/",
Strings.toString(tokenId)
)
);

// If there is no base URI, return the Knowledge Asset UAL.
Expand All @@ -68,7 +80,7 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 {
return string.concat(base, _ual);
}

function _setBaseURI(string memory baseURI) external virtual onlyHubOwner {
function setBaseURI(string memory baseURI) external virtual onlyHubOwner {
tokenBaseURI = baseURI;
emit BatchMetadataUpdate(0, lastTokenId());
}
Expand Down
12 changes: 10 additions & 2 deletions contracts/v2/utils/UnorderedNamedContractDynamicSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ library UnorderedNamedContractDynamicSetLibV2 {
);
require(addr != address(0), "NamedContractSet: Address cannot be 0x0");
require(!exists(self, name), "NamedContractSet: Contract with given name already exists");
require(self.addressIndexPointers[addr] == 0, "NamedContractSet: Address already in the set");
require(
(self.contractList.length == 0) ||
((self.addressIndexPointers[addr] == 0) && (self.contractList[0].addr != addr)),
"NamedContractSet: Address already in the set"
);
self.stringIndexPointers[name] = size(self);
self.addressIndexPointers[addr] = size(self);
self.contractList.push(UnorderedNamedContractDynamicSetStructs.Contract(name, addr));
Expand All @@ -29,7 +33,11 @@ library UnorderedNamedContractDynamicSetLibV2 {
) internal {
require(addr != address(0), "NamedContractSet: Address cannot be 0x0");
require(exists(self, name), "NamedContractSet: Contract with given name doesn't exists");
require(self.addressIndexPointers[addr] == 0, "NamedContractSet: Address already in the set");
require(
(self.contractList.length == 0) ||
((self.addressIndexPointers[addr] == 0) && (self.contractList[0].addr != addr)),
"NamedContractSet: Address already in the set"
);
delete self.addressIndexPointers[self.contractList[self.stringIndexPointers[name]].addr];
self.addressIndexPointers[addr] = self.stringIndexPointers[name];
self.contractList[self.stringIndexPointers[name]].addr = addr;
Expand Down
7 changes: 7 additions & 0 deletions deploy/011_deploy_identity_storage_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { DeployFunction } from 'hardhat-deploy/types';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
if (
hre.helpers.isDeployed('IdentityStorage') &&
hre.helpers.contractDeployments.contracts['IdentityStorage'].version.startsWith('1.')
) {
return;
}

console.log('Deploying IdentityStorage V2...');

await hre.helpers.deploy({
Expand Down
5 changes: 4 additions & 1 deletion deploy/012_deploy_identity_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { DeployFunction } from 'hardhat-deploy/types';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
if (hre.helpers.isDeployed('IdentityStorageV2')) {
if (
hre.helpers.isDeployed('IdentityStorage') &&
hre.helpers.contractDeployments.contracts['IdentityStorage'].version.startsWith('2.')
) {
return;
}

Expand Down
8 changes: 8 additions & 0 deletions deploy/019_deploy_content_asset_storage_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import { DeployFunction } from 'hardhat-deploy/types';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
if (
hre.helpers.isDeployed('ContentAssetStorage') &&
hre.helpers.contractDeployments.contracts['ContentAssetStorage'].version.startsWith('1.')
) {
return;
}

console.log('Deploying ContentAssetStorage V2...');

await hre.helpers.deploy({
newContractName: 'ContentAssetStorageV2',
newContractNameInHub: 'ContentAssetStorage',
passHubInConstructor: true,
setContractInHub: false,
setAssetStorageInHub: true,
additionalArgs: [hre.network.name.split('_')[0]],
Expand Down
6 changes: 5 additions & 1 deletion deploy/020_deploy_content_asset_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { DeployFunction } from 'hardhat-deploy/types';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
if (hre.helpers.isDeployed('ContentAssetStorageV2')) {
if (
hre.helpers.isDeployed('ContentAssetStorage') &&
hre.helpers.contractDeployments.contracts['ContentAssetStorage'].version.startsWith('2.')
) {
return;
}

console.log('Deploying ContentAssetStorage V1...');

await hre.helpers.deploy({
newContractName: 'ContentAssetStorage',
passHubInConstructor: true,
setContractInHub: false,
setAssetStorageInHub: true,
});
Expand Down
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HardhatRuntimeEnvironment } from 'hardhat/types';

import './tasks/address_converter';
import './tasks/low_level_call_data_encoder';
import './tasks/selector_encoder';
import './tasks/send_otp';
import './utils/type-extensions';
import config from './hardhat.node.config';
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
"test:integration": "hardhat test --network hardhat --grep '@integration'",
"test:trace": "hardhat test --network hardhat --trace",
"test:unit": "hardhat test --network hardhat --grep '@unit'",
"test:v1": "hardhat test --network hardhat --grep '@v1'",
"test:v2": "hardhat test --network hardhat --grep '@v2'",
"test": "hardhat test --network hardhat",
"typechain": "hardhat typechain"
}
Expand Down
18 changes: 18 additions & 0 deletions tasks/selector_encoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { task } from 'hardhat/config';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

type EncoderParameters = {
signature: string;
};

task('encode_selector', 'Calculates EVM function/error selector (sighash)')
.addParam<string>('signature')
.setAction(async (taskArgs: EncoderParameters, hre: HardhatRuntimeEnvironment) => {
const sighash = hre.ethers.utils.hexDataSlice(
hre.ethers.utils.keccak256(hre.ethers.utils.toUtf8Bytes(taskArgs.signature)),
0,
4,
);

console.log(`Selector (sighash) for ${taskArgs.signature}: ${sighash}`);
});
2 changes: 1 addition & 1 deletion test/v1/integration/ContentAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ContentAssetFixture = {
UnfinalizedStateStorage: UnfinalizedStateStorage;
};

describe('@integration ContentAsset contract', function () {
describe('@v1 @integration ContentAsset contract', function () {
let accounts: SignerWithAddress[];
let AssertionStorage: AssertionStorage;
let ParametersStorage: ParametersStorage;
Expand Down
3 changes: 2 additions & 1 deletion test/v1/unit/AssertionStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type AssertionStorageFixture = {
AssertionStorage: AssertionStorage;
};

describe('@unit AssertionStorage contract', function () {
describe('@v1 @unit AssertionStorage contract', function () {
const assertionId = '0x74657374696e6720617373657274696f6e2069640209100f5047b080c0440ae1';
const nonExistingAssertionId = '0x23457374696e6720617373657274696f6e2069640209100f5047b080c0440ae1';
const size = 20;
Expand All @@ -30,6 +30,7 @@ describe('@unit AssertionStorage contract', function () {
}

beforeEach(async () => {
hre.helpers.resetDeploymentsJson();
({ accounts, AssertionStorage } = await loadFixture(deployAssertionStorageFixture));
});

Expand Down
3 changes: 2 additions & 1 deletion test/v1/unit/CommitManagerV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type CommitManagerV1Fixture = {
CommitManagerV1: CommitManagerV1;
};

describe('@unit CommitManagerV1 contract', function () {
describe('@v1 @unit CommitManagerV1 contract', function () {
let accounts: SignerWithAddress[];
let Token: Token;
let ServiceAgreementV1: ServiceAgreementV1;
Expand Down Expand Up @@ -103,6 +103,7 @@ describe('@unit CommitManagerV1 contract', function () {
}

beforeEach(async () => {
hre.helpers.resetDeploymentsJson();
({ accounts, CommitManagerV1 } = await loadFixture(deployCommitManagerV1Fixture));
});

Expand Down
Loading

0 comments on commit 6e70f03

Please sign in to comment.