Skip to content

Commit

Permalink
Merge pull request #1641 from statechannels/ag-move-np
Browse files Browse the repository at this point in the history
Continue working on moving nitro protocol
  • Loading branch information
lalexgap authored Sep 6, 2023
2 parents eff1611 + c11c846 commit 02c3182
Show file tree
Hide file tree
Showing 22 changed files with 981 additions and 2,091 deletions.
17 changes: 3 additions & 14 deletions .github/workflows/node.yml → .github/workflows/contracts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node
name: Contracts

on:
push:
Expand All @@ -21,24 +21,13 @@ jobs:
run: yarn
- name: Compile contracts
run: yarn hardhat compile
- name: Run eslint (including prettier)
run: yarn lint:check
- name: Run gas benchmarks
run: yarn benchmark:diff
- name: Run contract tests
run: yarn test:contracts
- name: Archive logs
if: always()
uses: actions/upload-artifact@v2
with:
name: logs
path: ./**/*.log
- name: Run tests
run: yarn test
- name: Notify slack fail
if: ${{ failure() && github.ref == 'refs/heads/main'}}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_NOTIFICATIONS_BOT_TOKEN }}
uses: voxmedia/github-action-slack-notify-build@v1
with:
channel_id: C03G4AUGA7M
status: FAILED
color: danger
16 changes: 2 additions & 14 deletions packages/nitro-protocol/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/* global module */
module.exports = {
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'prettier',
// We enforce certain rules on how imports are handled
'import',
],
extends: [
'eslint:recommended',
'plugin:prettier/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
],
extends: ['../../.eslintrc.js'],
ignorePatterns: ['.prettierrc.js', '**/*.config.js', '**/scripts'],
rules: {
'no-self-compare': 'error',
'import/order': [
Expand Down
14 changes: 9 additions & 5 deletions packages/nitro-protocol/config/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ const root = resolve(__dirname, '../../');

// eslint-disable-next-line no-undef
module.exports = {
globals: {
'ts-jest': {
tsconfig: './test/tsconfig.json',
},
preset: 'ts-jest',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: './test/tsconfig.json',
},
],
},

rootDir: root,
collectCoverageFrom: ['**/*.{js,jsx,ts,tsx}'],
reporters: ['default'],
testMatch: ['<rootDir>/test/src/**/?(*.)test.ts?(x)', '<rootDir>/test/?(*.)test.ts?(x)'],
testEnvironment: 'node',
testURL: 'http://localhost',
preset: 'ts-jest',
};
9 changes: 5 additions & 4 deletions packages/nitro-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
"contracts:deploy-localhost": "yarn hardhat deploy --network localhost --export-all addresses.json && node ./scripts/postdeploy.ts",
"contracts:deploy-goerli": "yarn hardhat deploy --network goerli --export-all addresses.json && node ./scripts/postdeploy.js",
"contracts:node": "yarn hardhat node --no-deploy",
"test": "yarn test:contracts && yarn test:app --all",
"test": "yarn test:app",
"test:app": "yarn hardhat compile && jest -c ./config/jest/jest.config.js",
"test:contracts": "yarn hardhat compile && jest -c ./config/jest/jest.contracts.config.js",
"lint:check": "yarn eslint \"./\" --cache && yarn prettier:check",
"lint:write": "yarn eslint \"./\" --fix && yarn prettier:write",
"lint": "yarn lint:check",
"prepack": "yarn hardhat compile && yarn build:typescript",
"solhint:check": "yarn solhint -w 0 './contracts/**/*.sol'",
"solhint:write": "echo 'This command does not seem to work. Use yarn prettier:write ' && yarn solhint --fix './contracts/**/*.sol'",
Expand All @@ -44,7 +45,7 @@
"@statechannels/devtools": "^0.5.7",
"@typechain/ethers-v5": "^9.0.0",
"@typechain/hardhat": "^4.0.0",
"@types/jest": "^27.4.1",
"@types/jest": "29.5.0",
"@types/lodash.isequal": "^4.5.5",
"@types/lodash.shuffle": "^4.2.6",
"@types/mocha": "^9.1.0",
Expand All @@ -66,14 +67,14 @@
"hardhat-deploy-ethers": "^0.3.0-beta.13",
"hardhat-gas-reporter": "^1.0.7",
"hardhat-watcher": "^2.3.0",
"jest": "^27.5.1",
"jest": "29.5.0",
"lodash.isequal": "^4.5.0",
"lodash.shuffle": "^4.2.0",
"prettier": "^2.6.2",
"prettier-plugin-solidity": "^1.0.0-beta.19",
"solhint": "^3.3.7",
"solidity-coverage": "^0.8.4",
"ts-jest": "^27.1.3",
"ts-jest": "29.1.0",
"ts-node": "^10.4.0",
"typechain": "^8.0.0",
"typescript": "^4.6.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ const provider = getTestProvider();
const batchOperator = setupContract(
provider,
BatchOperatorArtifact,
process.env.BATCH_OPERATOR_ADDRESS
process.env.BATCH_OPERATOR_ADDRESS || ''
) as unknown as BatchOperator & Contract;

const nitroAdjudicator = setupContract(
provider,
NitroAdjudicatorArtifact,
process.env.NITRO_ADJUDICATOR_ADDRESS
process.env.NITRO_ADJUDICATOR_ADDRESS || ''
) as unknown as NitroAdjudicator & Contract;

const token = setupContract(
provider,
TokenArtifact,
process.env.TEST_TOKEN_ADDRESS
process.env.TEST_TOKEN_ADDRESS || ''
) as unknown as Token & Contract;

const badToken = setupContract(
provider,
BadTokenArtifact,
process.env.BAD_TOKEN_ADDRESS
process.env.BAD_TOKEN_ADDRESS || ''
) as unknown as BadToken & Contract;

const ETH = MAGIC_ADDRESS_INDICATING_ETH;
Expand Down Expand Up @@ -107,111 +107,103 @@ describe('deposit_batch', () => {
${description9} | ${ERC20} | ${[1, 1, 1]} | ${[1, 1, 1]} | ${[2, 2, 2]} | ${unexpectedHeld}
${description10} | ${BadERC20} | ${[0, 0, 0]} | ${[1, 1, 1]} | ${[1, 1, 1]} | ${''}
${description11} | ${ETH} | ${[0, 0]} | ${[1, 1, 1]} | ${[1, 1, 1]} | ${'Array lengths must match'}
`(
'$description',
async ({
description,
assetId,
expectedHelds,
amounts,
heldAfters,
reasonString,
}: testParams) => {
///////////////////////////////////////
//
// Construct deposit_batch parameters
//
///////////////////////////////////////
const channelIds = counterparties.map(counterparty =>
getChannelId({
channelNonce: getRandomNonce(description),
participants: [signerAddress, counterparty],
appDefinition: consensusApp,
challengeDuration: 100,
})
);
const expectedHeldsBN = expectedHelds.map(x => BigNumber.from(x));
const amountsBN = amounts.map(x => BigNumber.from(x));
const heldAftersBN = heldAfters.map(x => BigNumber.from(x));
const totalValue = sum(amountsBN);

if (assetId === ERC20) {
await (
await token.increaseAllowance(batchOperator.address, totalValue.add(totalValue))
).wait();
}
`('$description', async tc => {
const {description, assetId, expectedHelds, amounts, heldAfters, reasonString} =
tc as testParams;
///////////////////////////////////////
//
// Construct deposit_batch parameters
//
///////////////////////////////////////
const channelIds = counterparties.map(counterparty =>
getChannelId({
channelNonce: getRandomNonce(description),
participants: [signerAddress, counterparty],
appDefinition: consensusApp || '',
challengeDuration: 100,
})
);
const expectedHeldsBN = expectedHelds.map(x => BigNumber.from(x));
const amountsBN = amounts.map(x => BigNumber.from(x));
const heldAftersBN = heldAfters.map(x => BigNumber.from(x));
const totalValue = sum(amountsBN);

if (assetId === ERC20) {
await (
await token.increaseAllowance(batchOperator.address, totalValue.add(totalValue))
).wait();
}

if (assetId === BadERC20) {
await (
await badToken.increaseAllowance(batchOperator.address, totalValue.add(totalValue))
).wait();
}
if (assetId === BadERC20) {
await (
await badToken.increaseAllowance(batchOperator.address, totalValue.add(totalValue))
).wait();
}

///////////////////////////////////////
//
// Set up preexisting holdings (if any)
//
///////////////////////////////////////

await Promise.all(
expectedHeldsBN.map(async (expected, i) => {
const channelID = channelIds[i];
// apply incorrect amount if unexpectedHeld reasonString is set
const value = reasonString == unexpectedHeld ? expected.add(1) : expected;

if (assetId === ERC20) {
await (await token.increaseAllowance(nitroAdjudicator.address, value)).wait();
}
if (assetId === BadERC20) {
await (await badToken.increaseAllowance(nitroAdjudicator.address, value)).wait();
}

const {events} = await (
await nitroAdjudicator.deposit(assetId, channelID, 0, value, {
value: assetId === ETH ? value : 0,
})
).wait();
expect(events).not.toBe(undefined);
})
);

///////////////////////////////////////
//
// Execute deposit
//
///////////////////////////////////////

const tx =
assetId === ETH
? batchOperator.deposit_batch_eth(channelIds, expectedHeldsBN, amountsBN, {
value: totalValue,
})
: batchOperator.deposit_batch_erc20(
assetId,
channelIds,
expectedHeldsBN,
amountsBN,
totalValue
);

///////////////////////////////////////
//
// Check postconditions
//
///////////////////////////////////////
if (reasonString != '') {
await expectRevert(() => tx, reasonString);
} else {
await (await tx).wait();

for (let i = 0; i < channelIds.length; i++) {
const channelId = channelIds[i];
const expectedHoldings = heldAftersBN[i];

const holdings = await nitroAdjudicator.holdings(assetId, channelId);
expect(holdings).toEqual(expectedHoldings);
///////////////////////////////////////
//
// Set up preexisting holdings (if any)
//
///////////////////////////////////////

await Promise.all(
expectedHeldsBN.map(async (expected, i) => {
const channelID = channelIds[i];
// apply incorrect amount if unexpectedHeld reasonString is set
const value = reasonString == unexpectedHeld ? expected.add(1) : expected;

if (assetId === ERC20) {
await (await token.increaseAllowance(nitroAdjudicator.address, value)).wait();
}
if (assetId === BadERC20) {
await (await badToken.increaseAllowance(nitroAdjudicator.address, value)).wait();
}

const {events} = await (
await nitroAdjudicator.deposit(assetId, channelID, 0, value, {
value: assetId === ETH ? value : 0,
})
).wait();
expect(events).not.toBe(undefined);
})
);

///////////////////////////////////////
//
// Execute deposit
//
///////////////////////////////////////

const tx =
assetId === ETH
? batchOperator.deposit_batch_eth(channelIds, expectedHeldsBN, amountsBN, {
value: totalValue,
})
: batchOperator.deposit_batch_erc20(
assetId,
channelIds,
expectedHeldsBN,
amountsBN,
totalValue
);

///////////////////////////////////////
//
// Check postconditions
//
///////////////////////////////////////
if (reasonString != '') {
await expectRevert(() => tx, reasonString);
} else {
await (await tx).wait();

for (let i = 0; i < channelIds.length; i++) {
const channelId = channelIds[i];
const expectedHoldings = heldAftersBN[i];

const holdings = await nitroAdjudicator.holdings(assetId, channelId);
expect(holdings).toEqual(expectedHoldings);
}
}
);
});
});
Loading

0 comments on commit 02c3182

Please sign in to comment.