Skip to content

Commit

Permalink
chore(contract)(sdk): Stop supporting unused arrays in Registries (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyajeetkolhapure authored Nov 19, 2024
1 parent af2042b commit 6725ef2
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 267 deletions.
12 changes: 1 addition & 11 deletions contracts/src/ModuleRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract ModuleRegistry is OwnableUpgradeable {
IRouter public router;
/// @dev The list of Modules, accessed by their address
mapping(address id => Module module) public modules;
/// @dev The list of Module addresses
/// @dev Deprecated: The `moduleAddresses` variable is no longer used. It was used to store the modules addresses.
address[] public moduleAddresses;

/// @notice Error thrown when an invalid Router address is given
Expand Down Expand Up @@ -117,7 +117,6 @@ contract ModuleRegistry is OwnableUpgradeable {
if (bytes(modules[moduleAddress].name).length > 0) revert ModuleAlreadyExists();

modules[moduleAddress] = Module(moduleAddress, name, description);
moduleAddresses.push(moduleAddress);
emit ModuleRegistered(name, description, moduleAddress);
}

Expand Down Expand Up @@ -239,15 +238,6 @@ contract ModuleRegistry is OwnableUpgradeable {
}
}

/**
* @notice Get the number of Modules managed by the contract
* @return The number of Modules already registered
* @dev Returns the length of the `moduleAddresses` array
*/
function getModulesNumber() public view returns (uint256) {
return moduleAddresses.length;
}

/**
* @notice Checks that a module is registered in the module registry
* @param moduleAddress The address of the Module to check
Expand Down
30 changes: 2 additions & 28 deletions contracts/src/PortalRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract PortalRegistry is OwnableUpgradeable {

mapping(address issuerAddress => bool isIssuer) private issuers;

/// @dev Deprecated: The `portalAddresses` variable is no longer used. It was used to store the portals addresses.
address[] private portalAddresses;

bool private isTestnet;
Expand Down Expand Up @@ -170,7 +171,6 @@ contract PortalRegistry is OwnableUpgradeable {
// Add portal to mapping
Portal memory newPortal = Portal(id, msg.sender, modules, isRevocable, name, description, ownerName);
portals[id] = newPortal;
portalAddresses.push(id);

// Emit event
emit PortalRegistered(name, description, id);
Expand All @@ -184,24 +184,7 @@ contract PortalRegistry is OwnableUpgradeable {
function revoke(address id) public onlyOwner {
if (!isRegistered(id)) revert PortalNotRegistered();

portals[id] = Portal(address(0), address(0), new address[](0), false, "", "", "");

bool found = false;
uint256 portalAddressIndex;
for (uint256 i = 0; i < portalAddresses.length; i = uncheckedInc256(i)) {
if (portalAddresses[i] == id) {
portalAddressIndex = i;
found = true;
break;
}
}

if (!found) {
revert PortalNotRegistered();
}

portalAddresses[portalAddressIndex] = portalAddresses[portalAddresses.length - 1];
portalAddresses.pop();
delete portals[id];

emit PortalRevoked(id);
}
Expand Down Expand Up @@ -243,15 +226,6 @@ contract PortalRegistry is OwnableUpgradeable {
return portals[id].id != address(0);
}

/**
* @notice Get the number of Portals managed by the contract
* @return The number of Portals already registered
* @dev Returns the length of the `portalAddresses` array
*/
function getPortalsCount() public view returns (uint256) {
return portalAddresses.length;
}

/**
* @notice Checks if the caller is allowlisted.
* @return A flag indicating whether the Verax instance is running on testnet
Expand Down
12 changes: 1 addition & 11 deletions contracts/src/SchemaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract SchemaRegistry is OwnableUpgradeable {
IRouter public router;
/// @dev The list of Schemas, accessed by their ID
mapping(bytes32 id => Schema schema) private schemas;
/// @dev The list of Schema IDs
/// @dev Deprecated: The `schemaIds` variable is no longer used. It was used to store the ids of schemas.
bytes32[] public schemaIds;
/// @dev Associates a Schema ID with the address of the Issuer who created it
mapping(bytes32 id => address issuer) private schemasIssuers;
Expand Down Expand Up @@ -155,7 +155,6 @@ contract SchemaRegistry is OwnableUpgradeable {
}

schemas[schemaId] = Schema(name, description, context, schemaString);
schemaIds.push(schemaId);
schemasIssuers[schemaId] = msg.sender;
emit SchemaCreated(schemaId, name, description, context, schemaString);
}
Expand Down Expand Up @@ -184,15 +183,6 @@ contract SchemaRegistry is OwnableUpgradeable {
return schemas[schemaId];
}

/**
* @notice Get the number of Schemas managed by the contract
* @return The number of Schemas already registered
* @dev Returns the length of the `schemaIds` array
*/
function getSchemasNumber() public view returns (uint256) {
return schemaIds.length;
}

/**
* @notice Check if a Schema is registered
* @param schemaId The ID of the Schema
Expand Down
18 changes: 0 additions & 18 deletions contracts/test/ModuleRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,6 @@ contract ModuleRegistryTest is Test {
moduleRegistry.register(expectedName, expectedDescription, expectedAddress);
}

function test_getModulesNumber() public {
uint256 modulesNumber = moduleRegistry.getModulesNumber();
assertEq(modulesNumber, 0);
vm.prank(user);
moduleRegistry.register(expectedName, expectedDescription, expectedAddress);

modulesNumber = moduleRegistry.getModulesNumber();
assertEq(modulesNumber, 1);
}

function test_runModules() public {
// Register 2 modules
address[] memory moduleAddresses = new address[](2);
Expand Down Expand Up @@ -343,14 +333,6 @@ contract ModuleRegistryTest is Test {
vm.stopPrank();
}

function test_getModuleAddress() public {
vm.prank(user);
moduleRegistry.register(expectedName, expectedDescription, expectedAddress);

address moduleAddress = moduleRegistry.moduleAddresses(0);
assertEq(moduleAddress, expectedAddress);
}

function test_isRegistered() public {
bool isRegistered = moduleRegistry.isRegistered(expectedAddress);
assertFalse(isRegistered);
Expand Down
16 changes: 8 additions & 8 deletions contracts/test/PortalRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ contract PortalRegistryTest is Test {
vm.prank(user);
portalRegistry.register(address(validPortalMock), expectedName, expectedDescription, true, expectedOwnerName);

uint256 portalCount = portalRegistry.getPortalsCount();
assertEq(portalCount, 1);
bool isRegistered = portalRegistry.isRegistered(address(validPortalMock));
assertEq(isRegistered, true);

// Register a portal implementing IPortal
vm.expectEmit();
Expand All @@ -194,8 +194,8 @@ contract PortalRegistryTest is Test {
expectedOwnerName
);

portalCount = portalRegistry.getPortalsCount();
assertEq(portalCount, 2);
isRegistered = portalRegistry.isRegistered(address(iPortalImplementation));
assertEq(isRegistered, true);

Portal memory expectedPortal = Portal(
address(validPortalMock),
Expand Down Expand Up @@ -272,8 +272,8 @@ contract PortalRegistryTest is Test {
expectedOwnerName
);

uint256 portalCount = portalRegistry.getPortalsCount();
assertEq(portalCount, 1);
bool isRegistered = portalRegistry.isRegistered(portalAddress);
assertEq(isRegistered, true);

Portal memory expectedPortal = Portal(
portalAddress,
Expand All @@ -292,8 +292,8 @@ contract PortalRegistryTest is Test {
emit PortalRevoked(portalAddress);
portalRegistry.revoke(portalAddress);

portalCount = portalRegistry.getPortalsCount();
assertEq(portalCount, 0);
isRegistered = portalRegistry.isRegistered(portalAddress);
assertEq(isRegistered, false);

vm.expectRevert(PortalRegistry.PortalNotRegistered.selector);
portalRegistry.getPortalByAddress(portalAddress);
Expand Down
20 changes: 0 additions & 20 deletions contracts/test/SchemaRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,6 @@ contract SchemaRegistryTest is Test {
schemaRegistry.getSchema(bytes32("not registered"));
}

function test_getSchemasNumber() public {
uint256 schemasNumber = schemaRegistry.getSchemasNumber();
assertEq(schemasNumber, 0);
vm.startPrank(user);
schemaRegistry.createSchema(expectedName, expectedDescription, expectedContext, expectedString);

schemasNumber = schemaRegistry.getSchemasNumber();
assertEq(schemasNumber, 1);
vm.stopPrank();
}

function test_getSchemaIds() public {
vm.startPrank(user);
schemaRegistry.createSchema(expectedName, expectedDescription, expectedContext, expectedString);

bytes32 schemaId = schemaRegistry.schemaIds(0);
assertEq(schemaId, expectedId);
vm.stopPrank();
}

function test_isRegistered() public {
bool isRegistered = schemaRegistry.isRegistered(expectedId);
assertFalse(isRegistered);
Expand Down
6 changes: 1 addition & 5 deletions sdk/doc/cli-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

pnpm portal isPortalRegistered '{\"portalAddress\":\"0x8b833796869b5debb9b06370d6d47016f0d7973b\"}'

pnpm portal getPortalsCount
pnpm portal getPortalsNumber
```

</details>
Expand Down Expand Up @@ -117,8 +117,6 @@

pnpm module isRegistered "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5"

pnpm module getModuleAddress 0

pnpm module getModule "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5"
```

Expand Down Expand Up @@ -151,8 +149,6 @@
pnpm schema getSchemasNumber

pnpm schema isRegistered "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738"

pnpm schema getSchemaIds 0
```

</details>
5 changes: 0 additions & 5 deletions sdk/examples/module/moduleExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ export default class ModuleExamples {
console.log(await this.veraxSdk.module.isRegistered(moduleAddress));
}

if (methodName.toLowerCase() == "getModuleAddress".toLowerCase() || methodName == "") {
const index: number = argv === "" ? 0 : (argv as unknown as number);
console.log(await this.veraxSdk.module.getModuleAddress(index));
}

if (methodName.toLowerCase() == "getModule".toLowerCase() || methodName == "") {
const moduleAddress: Address = argv === "" ? "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5" : (argv as Address);
console.log(await this.veraxSdk.module.getModule(moduleAddress));
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/portal/portalExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ export default class PortalExamples {
console.log(await this.veraxSdk.portal.getPortalByAddress(portalAddress));
}

if (methodName.toLowerCase() == "getPortalsNumber".toLowerCase() || methodName == "") {
console.log(await this.veraxSdk.portal.getPortalsNumber());
}

if (methodName.toLowerCase() == "isPortalRegistered".toLowerCase() || methodName == "") {
let params;
if (argv !== "") params = JSON.parse(argv);
Expand Down
5 changes: 0 additions & 5 deletions sdk/examples/schema/schemaExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,5 @@ export default class SchemaExamples {
argv === "" ? "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738" : argv;
console.log(await this.veraxSdk.schema.isRegistered(schemaId));
}

if (methodName.toLowerCase() == "getSchemaIds".toLowerCase() || methodName == "") {
const index: number = argv === "" ? 0 : (argv as unknown as number);
console.log(await this.veraxSdk.schema.getSchemaIds(index));
}
}
}
2 changes: 1 addition & 1 deletion sdk/examples/utils/countUniqueSubjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fetchSubjectsFromFile = async (fileSuffix: number): Promise<string[]> => {

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationNumber = await veraxSdk.utils.getAttestationIdCounter();
const attestationNumber = await veraxSdk.attestation.getAttestationIdCounter();
const filesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE);

const allSubjects: string[][] = [];
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/utils/getAllAttestations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) =>

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationNumber = await veraxSdk.utils.getAttestationIdCounter();
const attestationNumber = await veraxSdk.attestation.getAttestationIdCounter();
const batchesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE);

console.log(`Creating ${batchesNumber} batches of ${BATCH_SIZE} items to get all ${attestationNumber} attestations.`);
Expand Down
5 changes: 0 additions & 5 deletions sdk/examples/utils/getAttestationIdCounter.ts

This file was deleted.

5 changes: 0 additions & 5 deletions sdk/examples/utils/getModulesNumber.ts

This file was deleted.

5 changes: 0 additions & 5 deletions sdk/examples/utils/getPortalsCount.ts

This file was deleted.

5 changes: 0 additions & 5 deletions sdk/examples/utils/getSchemasNumber.ts

This file was deleted.

13 changes: 0 additions & 13 deletions sdk/src/abi/ModuleRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,6 @@ export const abiModuleRegistry = [
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [],
name: "getModulesNumber",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "initialize",
Expand Down
13 changes: 0 additions & 13 deletions sdk/src/abi/PortalRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,6 @@ export const abiPortalRegistry = [
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "getPortalsCount",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "initialize",
Expand Down
13 changes: 0 additions & 13 deletions sdk/src/abi/SchemaRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,6 @@ export const abiSchemaRegistry = [
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "getSchemasNumber",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "initialize",
Expand Down
Loading

0 comments on commit 6725ef2

Please sign in to comment.