-
Notifications
You must be signed in to change notification settings - Fork 98
Add transaction filtering support to nitro-testnode #175
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
Open
Tristan-Wilson
wants to merge
3
commits into
master
Choose a base branch
from
tx-filtering
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+340
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| import * as fs from 'fs'; | ||
| import * as crypto from 'crypto'; | ||
| import * as consts from './consts' | ||
| import { ethers } from "ethers"; | ||
| import { namedAddress } from './accounts' | ||
| import { S3Client, PutObjectCommand, CreateBucketCommand, HeadBucketCommand } from "@aws-sdk/client-s3"; | ||
|
|
||
| const path = require("path"); | ||
|
|
||
| const S3_CONFIG = { | ||
| endpoint: "http://minio:9000", | ||
| region: "us-east-1", | ||
| credentials: { | ||
| accessKeyId: "minioadmin", | ||
| secretAccessKey: "minioadmin", | ||
| }, | ||
| forcePathStyle: true, | ||
| }; | ||
|
|
||
| const S3_BUCKET = "tx-filtering"; | ||
| const S3_OBJECT_KEY = "address-hashes.json"; | ||
|
|
||
| function writePrysmConfig(argv: any) { | ||
| const prysm = ` | ||
| CONFIG_NAME: interop | ||
|
|
@@ -184,6 +199,27 @@ function getChainInfo(): ChainInfo { | |
| return chainInfo; | ||
| } | ||
|
|
||
| function applyTxFilteringConfig(config: any) { | ||
| config.execution["address-filter"] = { | ||
| "enable": true, | ||
| "s3": { | ||
| "access-key": "minioadmin", | ||
| "secret-key": "minioadmin", | ||
| "region": "us-east-1", | ||
| "endpoint": "http://minio:9000", | ||
| "bucket": "tx-filtering", | ||
| "object-key": "address-hashes.json" | ||
| }, | ||
| "poll-interval": "30s" | ||
| }; | ||
| config.execution["transaction-filterer-rpc-client"] = { | ||
| "url": "http://transaction-filterer:8547" | ||
| }; | ||
| config["init"] = { | ||
| "transaction-filtering-enabled": true | ||
| }; | ||
| } | ||
|
|
||
| function writeConfigs(argv: any) { | ||
| const valJwtSecret = path.join(consts.configpath, "val_jwt.hex") | ||
| const chainInfoFile = path.join(consts.configpath, "l2_chain_info.json") | ||
|
|
@@ -315,6 +351,9 @@ function writeConfigs(argv: any) { | |
| if (argv.anytrust) { | ||
| simpleConfig.node["data-availability"]["rpc-aggregator"].enable = true | ||
| } | ||
| if (argv.txfiltering) { | ||
| applyTxFilteringConfig(simpleConfig); | ||
| } | ||
| fs.writeFileSync(path.join(consts.configpath, "sequencer_config.json"), JSON.stringify(simpleConfig)) | ||
| } else { | ||
| let validatorConfig = JSON.parse(baseConfJSON) | ||
|
|
@@ -338,6 +377,9 @@ function writeConfigs(argv: any) { | |
| "redis-url": argv.redisUrl | ||
| }; | ||
| } | ||
| if (argv.txfiltering) { | ||
| applyTxFilteringConfig(sequencerConfig); | ||
| } | ||
| fs.writeFileSync(path.join(consts.configpath, "sequencer_config.json"), JSON.stringify(sequencerConfig)) | ||
|
|
||
| let posterConfig = JSON.parse(baseConfJSON) | ||
|
|
@@ -418,7 +460,7 @@ function writeL2ChainConfig(argv: any) { | |
| "EnableArbOS": true, | ||
| "AllowDebugPrecompiles": true, | ||
| "DataAvailabilityCommittee": argv.anytrust, | ||
| "InitialArbOSVersion": 40, | ||
| "InitialArbOSVersion": argv.txfiltering ? 60 : 40, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to use constants from params/config_arbitrum? |
||
| "InitialChainOwner": argv.l2owner, | ||
| "GenesisBlockNum": 0 | ||
| } | ||
|
|
@@ -658,6 +700,11 @@ export const writeConfigCommand = { | |
| describe: "run sequencer in timeboost mode", | ||
| default: false | ||
| }, | ||
| txfiltering: { | ||
| boolean: true, | ||
| describe: "enable transaction filtering mode", | ||
| default: false | ||
| }, | ||
| }, | ||
| handler: (argv: any) => { | ||
| writeConfigs(argv) | ||
|
|
@@ -688,6 +735,11 @@ export const writeL2ChainConfigCommand = { | |
| boolean: true, | ||
| describe: "enable anytrust in chainconfig", | ||
| default: false | ||
| }, | ||
| txfiltering: { | ||
| boolean: true, | ||
| describe: "enable transaction filtering (requires ArbOS version 60)", | ||
| default: false | ||
| } | ||
| }, | ||
| handler: (argv: any) => { | ||
|
|
@@ -754,3 +806,176 @@ export const writeL2ReferenceDAConfigCommand = { | |
| writeL2ReferenceDAConfig(argv) | ||
| } | ||
| } | ||
|
|
||
| function writeTransactionFiltererConfig() { | ||
| const config = { | ||
| "chain-id": 412346, | ||
| "sequencer": { | ||
| "url": "http://sequencer:8547" | ||
| }, | ||
| "wallet": { | ||
| "account": namedAddress("filterer"), | ||
| "password": consts.l1passphrase, | ||
| "pathname": consts.l1keystore | ||
| }, | ||
| "http": { | ||
| "addr": "0.0.0.0", | ||
| "port": 8547 | ||
| } | ||
| }; | ||
| fs.writeFileSync(path.join(consts.configpath, "transaction_filterer_config.json"), JSON.stringify(config)); | ||
| } | ||
|
|
||
| export const writeTransactionFiltererConfigCommand = { | ||
| command: "write-tx-filterer-config", | ||
| describe: "writes transaction-filterer service config file", | ||
| handler: () => { | ||
| writeTransactionFiltererConfig() | ||
| } | ||
| } | ||
|
|
||
| export const initTxFilteringMinioCommand = { | ||
| command: "init-tx-filtering-minio", | ||
| describe: "initializes MinIO bucket and empty address hash list", | ||
| handler: async () => { | ||
| const salt = crypto.randomBytes(32).toString('hex'); | ||
| const initialAddressList = { | ||
| "salt": salt, | ||
| "hashing_scheme": "Sha256", | ||
| "address_hashes": [] | ||
| }; | ||
| fs.writeFileSync(path.join(consts.configpath, "initial_address_hashes.json"), JSON.stringify(initialAddressList, null, 2)); | ||
| fs.writeFileSync(path.join(consts.configpath, "tx_filtering_salt.hex"), salt); | ||
|
|
||
| const s3Client = new S3Client(S3_CONFIG); | ||
|
|
||
| try { | ||
| await s3Client.send(new HeadBucketCommand({ Bucket: S3_BUCKET })); | ||
| console.log("Bucket already exists:", S3_BUCKET); | ||
| } catch (err: any) { | ||
| if (err.name === "NotFound" || err.$metadata?.httpStatusCode === 404) { | ||
| await s3Client.send(new CreateBucketCommand({ Bucket: S3_BUCKET })); | ||
| console.log("Created bucket:", S3_BUCKET); | ||
| } else { | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| await uploadFilteredAddressesToMinio(); | ||
| console.log("Initialized tx-filtering bucket with empty address list."); | ||
| } | ||
| } | ||
|
|
||
| function computeAddressHash(address: string, salt: string): string { | ||
| const normalizedAddress = address.toLowerCase(); | ||
| const data = salt + normalizedAddress.replace('0x', ''); | ||
| const hash = crypto.createHash('sha256').update(Buffer.from(data, 'hex')).digest('hex'); | ||
| return hash; | ||
| } | ||
|
|
||
| async function uploadFilteredAddressesToMinio() { | ||
| console.log("Uploading address list to MinIO..."); | ||
| const s3Client = new S3Client(S3_CONFIG); | ||
|
|
||
| const addressListPath = path.join(consts.configpath, "initial_address_hashes.json"); | ||
| const content = fs.readFileSync(addressListPath).toString(); | ||
|
|
||
| await s3Client.send(new PutObjectCommand({ | ||
| Bucket: S3_BUCKET, | ||
| Key: S3_OBJECT_KEY, | ||
| Body: content, | ||
| ContentType: "application/json", | ||
| })); | ||
|
|
||
| console.log("Upload complete."); | ||
| } | ||
|
|
||
| export const hashAddressCommand = { | ||
| command: "hash-address", | ||
| describe: "computes SHA256 hash for an address with salt", | ||
| builder: { | ||
| address: { | ||
| string: true, | ||
| describe: "address to hash", | ||
| demandOption: true | ||
| }, | ||
| }, | ||
| handler: (argv: any) => { | ||
| const saltPath = path.join(consts.configpath, "tx_filtering_salt.hex"); | ||
| if (!fs.existsSync(saltPath)) { | ||
| console.error("Salt file not found. Run init-tx-filtering-minio first."); | ||
| process.exit(1); | ||
| } | ||
| const salt = fs.readFileSync(saltPath).toString().trim(); | ||
| const hash = computeAddressHash(argv.address, salt); | ||
| console.log(hash); | ||
| } | ||
| } | ||
|
|
||
| export const addFilteredAddressCommand = { | ||
| command: "add-filtered-address", | ||
| describe: "adds an address hash to the S3 filter list", | ||
| builder: { | ||
| address: { | ||
| string: true, | ||
| describe: "address to add to filter list", | ||
| demandOption: true | ||
| }, | ||
| }, | ||
| handler: async (argv: any) => { | ||
| const saltPath = path.join(consts.configpath, "tx_filtering_salt.hex"); | ||
| if (!fs.existsSync(saltPath)) { | ||
| console.error("Salt file not found. Run init-tx-filtering-minio first."); | ||
| process.exit(1); | ||
| } | ||
| const salt = fs.readFileSync(saltPath).toString().trim(); | ||
| const hash = computeAddressHash(argv.address, salt); | ||
|
|
||
| const addressListPath = path.join(consts.configpath, "initial_address_hashes.json"); | ||
| const addressList = JSON.parse(fs.readFileSync(addressListPath).toString()); | ||
|
|
||
| const exists = addressList.address_hashes.some((entry: any) => entry.hash === hash); | ||
| if (!exists) { | ||
| addressList.address_hashes.push({ hash: hash }); | ||
| fs.writeFileSync(addressListPath, JSON.stringify(addressList, null, 2)); | ||
| console.log("Added address hash:", hash); | ||
| await uploadFilteredAddressesToMinio(); | ||
| } else { | ||
| console.log("Address hash already in list:", hash); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const removeFilteredAddressCommand = { | ||
| command: "remove-filtered-address", | ||
| describe: "removes an address hash from the S3 filter list", | ||
| builder: { | ||
| address: { | ||
| string: true, | ||
| describe: "address to remove from filter list", | ||
| demandOption: true | ||
| }, | ||
| }, | ||
| handler: async (argv: any) => { | ||
| const saltPath = path.join(consts.configpath, "tx_filtering_salt.hex"); | ||
| if (!fs.existsSync(saltPath)) { | ||
| console.error("Salt file not found. Run init-tx-filtering-minio first."); | ||
| process.exit(1); | ||
| } | ||
| const salt = fs.readFileSync(saltPath).toString().trim(); | ||
| const hash = computeAddressHash(argv.address, salt); | ||
|
|
||
| const addressListPath = path.join(consts.configpath, "initial_address_hashes.json"); | ||
| const addressList = JSON.parse(fs.readFileSync(addressListPath).toString()); | ||
|
|
||
| const index = addressList.address_hashes.findIndex((entry: any) => entry.hash === hash); | ||
| if (index > -1) { | ||
| addressList.address_hashes.splice(index, 1); | ||
| fs.writeFileSync(addressListPath, JSON.stringify(addressList, null, 2)); | ||
| console.log("Removed address hash:", hash); | ||
| await uploadFilteredAddressesToMinio(); | ||
| } else { | ||
| console.log("Address hash not in list:", hash); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.