Skip to content

Commit

Permalink
fix: properly register l2/l3 networks (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx authored Apr 10, 2024
1 parent 9ece5ed commit f53a042
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 88 deletions.
40 changes: 1 addition & 39 deletions src/createTokenBridge.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
zeroAddress,
parseAbi,
} from 'viem';
import { execSync } from 'node:child_process';

import { nitroTestnodeL1, nitroTestnodeL2, nitroTestnodeL3 } from './chains';
import { getNitroTestnodePrivateKeyAccounts } from './testHelpers';
import { getInformationFromTestnode, getNitroTestnodePrivateKeyAccounts } from './testHelpers';
import { createTokenBridgePrepareTransactionRequest } from './createTokenBridgePrepareTransactionRequest';
import { createTokenBridgePrepareTransactionReceipt } from './createTokenBridgePrepareTransactionReceipt';
import { deployTokenBridgeCreator } from './createTokenBridge-testHelpers';
Expand All @@ -20,43 +19,6 @@ import { erc20 } from './contracts';
import { createTokenBridgePrepareSetWethGatewayTransactionRequest } from './createTokenBridgePrepareSetWethGatewayTransactionRequest';
import { createTokenBridgePrepareSetWethGatewayTransactionReceipt } from './createTokenBridgePrepareSetWethGatewayTransactionReceipt';

type TestnodeInformation = {
rollup: `0x${string}`;
l3Rollup: `0x${string}`;
l3NativeToken: `0x${string}`;
};

function getInformationFromTestnode(): TestnodeInformation {
const containers = [
'nitro_sequencer_1',
'nitro-sequencer-1',
'nitro-testnode-sequencer-1',
'nitro-testnode_sequencer_1',
];

for (const container of containers) {
try {
const deploymentJson = JSON.parse(
execSync('docker exec ' + container + ' cat /config/deployment.json').toString(),
);

const l3DeploymentJson = JSON.parse(
execSync('docker exec ' + container + ' cat /config/l3deployment.json').toString(),
);

return {
rollup: deploymentJson['rollup'],
l3Rollup: l3DeploymentJson['rollup'],
l3NativeToken: l3DeploymentJson['native-token'],
};
} catch {
// empty on purpose
}
}

throw new Error('nitro-testnode sequencer not found');
}

const testnodeAccounts = getNitroTestnodePrivateKeyAccounts();
const l2RollupOwner = testnodeAccounts.l2RollupOwner;
const l3RollupOwner = testnodeAccounts.l3RollupOwner;
Expand Down
38 changes: 38 additions & 0 deletions src/testHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sha256, toBytes } from 'viem';
import { privateKeyToAccount, PrivateKeyAccount } from 'viem/accounts';
import { config } from 'dotenv';
import { execSync } from 'node:child_process';

import { sanitizePrivateKey } from './utils';

Expand Down Expand Up @@ -69,3 +70,40 @@ export function getNitroTestnodePrivateKeyAccounts(): NitroTestNodePrivateKeyAcc
},
};
}

type TestnodeInformation = {
rollup: `0x${string}`;
l3Rollup: `0x${string}`;
l3NativeToken: `0x${string}`;
};

export function getInformationFromTestnode(): TestnodeInformation {
const containers = [
'nitro_sequencer_1',
'nitro-sequencer-1',
'nitro-testnode-sequencer-1',
'nitro-testnode_sequencer_1',
];

for (const container of containers) {
try {
const deploymentJson = JSON.parse(
execSync('docker exec ' + container + ' cat /config/deployment.json').toString(),
);

const l3DeploymentJson = JSON.parse(
execSync('docker exec ' + container + ' cat /config/l3deployment.json').toString(),
);

return {
rollup: deploymentJson['rollup'],
l3Rollup: l3DeploymentJson['rollup'],
l3NativeToken: l3DeploymentJson['native-token'],
};
} catch {
// empty on purpose
}
}

throw new Error('nitro-testnode sequencer not found');
}
75 changes: 75 additions & 0 deletions src/utils/registerNewNetwork.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { it, expect } from 'vitest';
import { createPublicClient, http } from 'viem';

import { nitroTestnodeL1, nitroTestnodeL2, nitroTestnodeL3 } from '../chains';
import { getInformationFromTestnode } from '../testHelpers';
import { registerNewNetwork } from './registerNewNetwork';
import { publicClientToProvider } from '../ethers-compat/publicClientToProvider';
import { L2Network, l1Networks, l2Networks } from '@arbitrum/sdk/dist/lib/dataEntities/networks';

const nitroTestnodeL1Client = createPublicClient({
chain: nitroTestnodeL1,
transport: http(nitroTestnodeL1.rpcUrls.default.http[0]),
});

const nitroTestnodeL2Client = createPublicClient({
chain: nitroTestnodeL2,
transport: http(nitroTestnodeL2.rpcUrls.default.http[0]),
});

const nitroTestnodeL3Client = createPublicClient({
chain: nitroTestnodeL3,
transport: http(nitroTestnodeL3.rpcUrls.default.http[0]),
});

const l1Provider = publicClientToProvider(nitroTestnodeL1Client);
const l2Provider = publicClientToProvider(nitroTestnodeL2Client);
const l3Provider = publicClientToProvider(nitroTestnodeL3Client);

it('should register L1 and L2 respectively as L1 and L2', async () => {
const testnodeInformation = getInformationFromTestnode();
const { parentNetwork, childNetwork } = await registerNewNetwork(
l1Provider,
l2Provider,
testnodeInformation.rollup,
);

expect(parentNetwork.chainID).toEqual(nitroTestnodeL1.id);
expect(childNetwork.chainID).toEqual(nitroTestnodeL2.id);
expect(Object.keys(l1Networks)).toContain(nitroTestnodeL1.id.toString());
expect(Object.keys(l2Networks)).toContain(nitroTestnodeL2.id.toString());
expect(parentNetwork.partnerChainIDs).toContain(nitroTestnodeL2.id);
expect(childNetwork.partnerChainID).toEqual(nitroTestnodeL1.id);
});

it('should register L2 and L3 as L2', async () => {
const testnodeInformation = getInformationFromTestnode();
const { parentNetwork, childNetwork } = await registerNewNetwork(
l2Provider,
l3Provider,
testnodeInformation.l3Rollup,
);

expect(parentNetwork.chainID).toEqual(nitroTestnodeL2.id);
expect(childNetwork.chainID).toEqual(nitroTestnodeL3.id);
expect(Object.keys(l2Networks)).toContain(nitroTestnodeL2.id.toString());
expect(Object.keys(l2Networks)).toContain(nitroTestnodeL3.id.toString());
expect(parentNetwork.partnerChainIDs).toContain(nitroTestnodeL3.id);
expect((parentNetwork as L2Network).partnerChainID).toEqual(nitroTestnodeL1.id);
expect(childNetwork.partnerChainID).toBe(parentNetwork.chainID);
expect(childNetwork.partnerChainID).toEqual(nitroTestnodeL2.id);
});

it('should not throw an error if registering L2/L3 after L1/L2', async () => {
const testnodeInformation = getInformationFromTestnode();
await registerNewNetwork(l1Provider, l2Provider, testnodeInformation.rollup);
await registerNewNetwork(l2Provider, l3Provider, testnodeInformation.l3Rollup);
await registerNewNetwork(l1Provider, l2Provider, testnodeInformation.rollup);

expect(() =>
registerNewNetwork(l2Provider, l3Provider, testnodeInformation.l3Rollup),
).not.toThrow();
expect(Object.keys(l2Networks)).toContain(nitroTestnodeL2.id.toString());
expect(Object.keys(l2Networks)).toContain(nitroTestnodeL3.id.toString());
expect(Object.keys(l1Networks)).not.toContain(nitroTestnodeL2.id.toString());
});
Loading

0 comments on commit f53a042

Please sign in to comment.