Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script for sharding table cleaning #241

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Hardhat has plenty of other useful commands, extended by the installed plugins.
- `decode`: Decodes encoded ABI data (e.g. input data of transaction).
- `encode_selector`: Calculates EVM function/event/error selector (sighash).
- `encode_data`: Encodes data needed for low-level contract calls.
- `clear_sharding_table`: Removes nodes from sharding table based on Node IDs from the given CSV file (example usage: `npx hardhat clear_sharding_table --file-path ./peers.csv --network otp_devnet`).

These tasks can be run using the `npx hardhat <task-name>` command. For example, to decode input data, you can run:

Expand Down
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { lazyObject } from 'hardhat/plugins';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

import './tasks/address_converter';
import './tasks/clear_sharding_table';
import './tasks/deploy_test_token';
import './tasks/low_level_call_data_encoder';
import './tasks/mint_test_tokens';
Expand Down
42 changes: 42 additions & 0 deletions tasks/clear_sharding_table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fs from 'fs/promises';

import { task } from 'hardhat/config';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

type ConverterParameters = {
filePath: string;
};

task('clear_sharding_table', 'Removed nodes from sharding table by node IDs from the CSV file')
.addParam<string>('filePath', 'Path to CSV file with Node IDs')
.setAction(async (taskArgs: ConverterParameters, hre: HardhatRuntimeEnvironment) => {
const { filePath } = taskArgs;

let nodeIdsToDelete: string[];
try {
const data = await fs.readFile(filePath, 'utf8');
nodeIdsToDelete = data
.trim()
.split('\n')
.map((row) => row.split(',')[0]);
} catch (err) {
console.error(err);
nodeIdsToDelete = [];
}

const ShardingTableABI = hre.helpers.getAbi('ShardingTable');
const shardingTableAddress = hre.helpers.contractDeployments.contracts['ShardingTable'].evmAddress;
const ShardingTable = await hre.ethers.getContractAt(ShardingTableABI, shardingTableAddress);

const table = await ShardingTable['getShardingTable()']();

for (const node of table) {
const nodeIdString = hre.ethers.utils.toUtf8String(node.nodeId);

if (nodeIdsToDelete.includes(nodeIdString)) {
console.log(`Deleting node with identityId: ${node.identityId}`);

await ShardingTable.removeNode(node.identityId);
}
}
});
Loading