Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
[all] Introduction of AppRegistry and significant code improvements t…
Browse files Browse the repository at this point in the history
…o facilitate it (#163)

* wip

* wip

* passing tests2

* apps folder tests pass

* lint

* cleaner code

* cleaner code

* install protocol tests

* set state tests

* test improvements

* setup protocol tests

* some fixes

* tests pass for all cf ops

* touch ups

* code cleanup

* start of cleaning up proposer tests

* update waffle

* clean up tests for install proposer

* continue to ensure tests pass

* remove useless file

* remove bad test

* tests for all proposers

* parallelize tests

* rearrange deps

* still wip

* remove warning on yarn

* some cleanups to package

* considerable improvements to code quality in machine

* remove 4447 json network

* checkpointing stuff

* commitment tests passing

* mostly tested machine functionality

* better testing

* make explicit unused files

* nice script stuff

* fix tests after ethers update

* updates

* clean up tests to handle immutable

* code cleanup

* add comment

* remove files that should not be here

* remove files that should not be here

* remove yarn clean

* remove machine dependency on cf.js

* minor readme touch up

* fix lint

* remove bold and normal in build.sh

* Update packages/machine/src/models/state-channel.ts

Co-Authored-By: snario <[email protected]>

* Update packages/machine/src/ethereum/install.ts

Co-Authored-By: snario <[email protected]>

* Update packages/machine/src/models/app-instance.ts

Co-Authored-By: snario <[email protected]>

* move utils to module import

* typo

* add doc

* typo

* replace console.log with tests

* import from ethers/*

* change dependencies to devDepencies for apps package

* switch to resolver map

* type arbitrary state objects better

* use template literals for error messages

* inline Map mutations for cleaner code

* abridge if statement

* Delegatecall -> DelegateCall

* Update packages/machine/src/ethereum/utils/free-balance.ts

Co-Authored-By: snario <[email protected]>

* remove unused class

* Update packages/machine/src/ethereum/uninstall.ts

Co-Authored-By: snario <[email protected]>

* Update packages/machine/src/ethereum/uninstall.ts

Co-Authored-By: snario <[email protected]>

* Update packages/machine/src/ethereum/install.ts

Co-Authored-By: snario <[email protected]>

* symlink .soliumrc.json

* use ethers module imports some more

* shorten tsconfig in @apps

* add --detectOpenHandles back

* shorten lines in .gitignore

* re-sort .soliumrc.json file

* uncomment critical line in StateChannelTransaction

* Update packages/contracts/contracts/libs/LibStaticCall.sol

Co-Authored-By: snario <[email protected]>

* better documentation of appStates mapping

* better documentation of appResolutions mapping

* rename _id to id in all .sol files

* better docs on setState method

* remove test:windows because its the same

* re-version @types to 0.0.1

* rename TIMEOUT variable to ONCHAIN_CHALLENGE_TIMEOUT in a test

* import Wallet in constants.ts

* remove some commented out code

* fresh conflict-free yarn.lock file

* rename monotonicSeqNo according to Xuanjis's proposed scheme '#163 (comment)'

* use ReadonlyMap not Readonly<Map

* add ethereum-waffle typescript typings

* remove ts-ignore

* update ethers to 0.4.20

* remove unnecessary build step for @types

* remove some more tsignores

* remove unused imports

* reintroduce script to build types

* add apps to build string

* better variable naming

* fix @types build process

* add ganache-cli to test process of machine

* fix typo

*  undo export of cf

* Add infrastructure to run ganache-cli tests in machine

* Fix ordering of fields in tuple causing encoding to be incorrect

* Fix incorrect block.number / finalizesAt value comparison

* Add MinimumViableMultisig to the migrations (for use with proxyfactory)

* Remove --reset flag on truffle migrate for contracts

* Rename appState to encodedAppState on SetStateCommitment for clarity

* Use encodePacked not encode for getTransactionHash on Multisig

* Add encodedTerms method on AppInstance

* Change default timeouts to 10 not 100 (for tests)

* Update the types folder to correctly export types and enums

* Update yarn.lock

* Working blockchain test

* rename file

* Fix timeout test after value change

* Switch arrow notation to function for encodeTransactions

* Add missing variable usage in test.sh

* more descriptive test description

* Fix a linting error

* add types to front of build.sh string

* use relative importing in contracts as it turns out it is causing double compilation

* fix an incorrect merge from earlier of master

* nearly finished working merge of virtualAppAgreement test

* Remove multisig.ts util file

* testing rename of files to see how git treats the rename

* Rename files to old names to fix git history

* Add build symlink

* ensure tests pass

* fix some linting issues

* update circleci config

* dont delete .env when cleaning

* imrpoved usage of exit signals for bash trap

* ensure circleci runs tests correctly

* update some README

* Update README.md

* Rename truffle.js to truffle-config.js

* Replace ethers usage to always import from module (#364)

* Replace all ethers utils with module imports

* Fixed a few places I missed the first time

* Update rollup for cf.js to include ethers.constants

* Rename appCfAddress to appInstanceId (#372)

* Add @firebase/app-types to devDependencies on node

* fix some linting issues

* Add apps back to build file

* Update packages/machine/test/integration/setup-then-set-state.spec.ts

Co-Authored-By: snario <[email protected]>

* Revert syntax error from github suggestion

* Remove duplicate code

* yarn.lock

* fix solium to 1.1.8

* update waffle
  • Loading branch information
snario authored Dec 27, 2018
1 parent 0505908 commit 8e93be7
Show file tree
Hide file tree
Showing 232 changed files with 8,159 additions and 8,323 deletions.
9 changes: 3 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: 2

jobs:
build:
docker:
Expand All @@ -9,26 +10,22 @@ jobs:
- restore_cache:
key: dependency-cache-{{ checksum ".dependencies_checksum" }}
- run: yarn
- run: yarn build
- save_cache:
key: dependency-cache-{{ checksum ".dependencies_checksum" }}
paths:
- node_modules

run-tests:
docker:
- image: circleci/node:10-browsers
- image: circleci/node:10
steps:
- checkout
- run: cat packages/*/package.json | shasum > .dependencies_checksum
- restore_cache:
key: dependency-cache-{{ checksum ".dependencies_checksum" }}
- run: yarn # symlink packages' node_modules
- run: yarn build
- run:
name: "Run ganache"
command: yarn ganache:ci
background: true
- run: cd packages/contracts/ && yarn migrate --network ganache
- run: yarn test:ci-non-playground

run-playground-tests:
Expand Down
28 changes: 28 additions & 0 deletions .soliumrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": "solium:all",
"plugins": ["security"],
"rules": {
"arg-overflow": ["error", 8],
"array-declarations": 1,
"camelcase": 1,
"deprecated-suicide": 1,
"imports-on-top": 1,
"indentation": ["error", 2],
"lbrace": 0,
"max-len": ["error", 80],
"mixedcase": 0,
"no-empty-blocks": 1,
"no-experimental": 0,
"no-unused-vars": 1,
"operator-whitespace": 1,
"pragma-on-top": 1,
"quotes": 1,
"security/enforce-explicit-visibility": ["error"],
"security/no-block-members": ["warning"],
"security/no-inline-assembly": 0,
"security/no-low-level-calls": 0,
"uppercase": 1,
"variable-declarations": 1,
"whitespace": 1
}
}
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,7 @@ yarn lint:fix

### Tests

Presently for some of the tests to work, you need to have a `ganache-cli` instance running in the background. To do this, run using:

```shell
yarn ganache
```

You also need to migrate the contracts in the contracts package to generate a `networks` file which the `machine` package directly consume (for now).

```shell
cd packages/contracts
yarn migrate --network ganache
```

Finally, to run all tests:
To run all tests:

```shell
yarn test
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

set -e

packages="contracts common-types cf.js machine node node-provider playground-server playground dapp-high-roller"
packages="types apps contracts common-types cf.js machine node node-provider playground-server playground dapp-high-roller"

for package in $packages; do
echo ">>> Building package: $package"
cd packages/$package
echo "⚙️ Building package: ${package}"
cd packages/${package}
yarn build
cd -
done
24 changes: 4 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,20 @@
},
"scripts": {
"build": "sh build.sh",
"clean": "lerna run clean --parallel --no-bail",
"clean": "git clean -Xdf --exclude=\"!.env\"",
"clean:dry": "git clean -Xdn --exclude=\"!.env\"",
"test": "lerna run --stream --concurrency 1 test",
"test:ci-non-playground": "lerna run --ignore @counterfactual/playground --ignore @counterfactual/dapp-high-roller --stream test",
"ganache": "ganache-cli --networkId ${npm_package_config_ganacheNetworkID} --verbose --gasLimit ${npm_package_config_ganacheGasLimit} --gasPrice ${npm_package_config_ganacheGasPrice} --port ${npm_package_config_ganachePort} --deterministic --account=\"${npm_package_config_unlockedAccount0},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount1},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount2},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount3},${npm_package_config_etherBalance}\" &> /dev/null &",
"ganache:windows": "ganache-cli --networkId %npm_package_config_ganacheNetworkID% --verbose --gasLimit %npm_package_config_ganacheGasLimit% --gasPrice %npm_package_config_ganacheGasPrice% --port %npm_package_config_ganachePort% --deterministic --account=\"%npm_package_config_unlockedAccount0%,%npm_package_config_etherBalance%\" --account=\"%npm_package_config_unlockedAccount1%,%npm_package_config_etherBalance%\" --account=\"%npm_package_config_unlockedAccount2%,%npm_package_config_etherBalance%\" --account=\"%npm_package_config_unlockedAccount3%,%npm_package_config_etherBalance%\"",
"ganache:ci": "ganache-cli --networkId ${npm_package_config_ganacheNetworkID} --gasLimit ${npm_package_config_ganacheGasLimit} --gasPrice ${npm_package_config_ganacheGasPrice} --port ${npm_package_config_ganachePort} --deterministic --account=\"${npm_package_config_unlockedAccount0},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount1},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount2},${npm_package_config_etherBalance}\" --account=\"${npm_package_config_unlockedAccount3},${npm_package_config_etherBalance}\"",
"ganache:stop": "ps aux | grep ganache-cli | grep -v grep | awk '{print $2}' | xargs kill -9",
"test:ci-non-playground": "lerna run --ignore @counterfactual/playground --ignore @counterfactual/dapp-high-roller --stream --concurrency 1 test",
"lint": "lerna run lint --parallel --no-bail",
"lint:fix": "lerna run lint:fix --parallel --no-bail",
"dev:playground": "lerna run start --parallel --scope=**/playground --scope=**/dapp-high-roller"
},
"config": {
"ganacheNetworkID": 7777777,
"ganachePort": 9545,
"ganacheGasLimit": "0xfffffffffff",
"ganacheGasPrice": "0x01",
"unlockedAccount0": "0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257a",
"unlockedAccount1": "0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257b",
"unlockedAccount2": "0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257c",
"unlockedAccount3": "0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d",
"etherBalance": "1000000000000000000000000"
},
"keywords": [
"ethereum",
"counterfactual",
"state channels",
"off chain"
"state channels"
],
"devDependencies": {
"ganache-cli": "6.2.5",
"lerna": "^3.4.3",
"prettier": "1.15.3",
"tslint": "^5.11.0",
Expand Down
1 change: 1 addition & 0 deletions packages/apps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
11 changes: 11 additions & 0 deletions packages/apps/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
testCommand: 'truffle test --network coverage lib/**/*.spec.js',
skipFiles: [
"external/Proxy.sol",
"external/ProxyFactory.sol",
"lib/StaticCall.sol",
"delegateTargets/MultiSend.sol",
"Registry.sol",
"test/loadContracts.sol"
]
};
2 changes: 2 additions & 0 deletions packages/apps/.soliumignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverageEnv
1 change: 1 addition & 0 deletions packages/apps/.soliumrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import "../lib/Transfer.sol";
import "@counterfactual/contracts/contracts/libs/Transfer.sol";


contract CommitRevealApp {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import "../lib/Transfer.sol";
import "@counterfactual/contracts/contracts/libs/Transfer.sol";


contract CountingApp {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import "../lib/Transfer.sol";
import "@counterfactual/contracts/contracts/libs/Transfer.sol";


/*
Normal-form Nim
https://en.wikipedia.org/wiki/Nim
*/
contract Nim {
contract NimApp {

struct Action {
uint256 pileIdx;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import "../lib/Transfer.sol";
import "@counterfactual/contracts/contracts/libs/Transfer.sol";


contract PaymentApp {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import "../lib/Transfer.sol";
import "@counterfactual/contracts/contracts/libs/Transfer.sol";


contract TicTacToe {
contract TicTacToeApp {

enum ActionType {
PLAY,
Expand Down
34 changes: 34 additions & 0 deletions packages/apps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@counterfactual/apps",
"version": "0.0.1",
"description": "Collection of various apps built on Counterfactual",
"repository": "github.com/counterfactual/monorepo",
"license": "MIT",
"private": true,
"files": [
"build"
],
"scripts": {
"build": "waffle waffle.json",
"test": "ts-mocha test/*",
"lint:fix": "yarn lint:ts:fix && yarn lint:sol:fix",
"lint": "yarn lint:ts && yarn lint:sol",
"lint:sol:fix": "solium -d contracts/ --fix",
"lint:sol": "solium -d .",
"lint:ts:fix": "tslint -c tslint.json -p . --fix",
"lint:ts": "tslint -c tslint.json -p ."
},
"devDependencies": {
"@counterfactual/contracts": "0.0.3",
"@counterfactual/typescript-typings": "0.0.1",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"chai": "^4.2.0",
"ethereum-waffle": "1.2.0",
"ethers": "4.0.20",
"mocha": "^4.1.0",
"solium": "1.1.8",
"ts-mocha": "^2.0.0",
"tslint": "^5.11.0"
}
}
117 changes: 117 additions & 0 deletions packages/apps/test/nim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import chai from "chai";
import * as waffle from "ethereum-waffle";
import { Contract } from "ethers";
import { AddressZero } from "ethers/constants";
import { BigNumber, defaultAbiCoder } from "ethers/utils";

import NimApp from "../build/NimApp.json";

chai.use(waffle.solidity);

const { expect } = chai;

type NimAppState = {
players: string[];
turnNum: BigNumber;
pileHeights: BigNumber[];
};

function decodeAppState(encodedAppState: string): NimAppState {
return defaultAbiCoder.decode(
["tuple(address[2] players, uint256 turnNum, uint256[3] pileHeights)"],
encodedAppState
)[0];
}

describe("Nim", () => {
let nim: Contract;

before(async () => {
const provider = waffle.createMockProvider();
const wallet = (await waffle.getWallets(provider))[0];
nim = await waffle.deployContract(wallet, NimApp);
});

describe("applyAction", () => {
it("can take from a pile", async () => {
const preState = {
players: [AddressZero, AddressZero],
turnNum: 0,
pileHeights: [6, 5, 12]
};

const action = {
pileIdx: 0,
takeAmnt: 5
};

const ret = await nim.functions.applyAction(preState, action);

const postState = decodeAppState(ret);

expect(postState.pileHeights[0]).to.eq(1);
expect(postState.pileHeights[1]).to.eq(5);
expect(postState.pileHeights[2]).to.eq(12);
expect(postState.turnNum).to.eq(1);
});

it("can take to produce an empty pile", async () => {
const preState = {
players: [AddressZero, AddressZero],
turnNum: 0,
pileHeights: [6, 5, 12]
};

const action = {
pileIdx: 0,
takeAmnt: 6
};

const ret = await nim.functions.applyAction(preState, action);

const postState = decodeAppState(ret);

expect(postState.pileHeights[0]).to.eq(0);
expect(postState.pileHeights[1]).to.eq(5);
expect(postState.pileHeights[2]).to.eq(12);
expect(postState.turnNum).to.eq(1);
});

it("should fail for taking too much", async () => {
const preState = {
players: [AddressZero, AddressZero],
turnNum: 0,
pileHeights: [6, 5, 12]
};

const action = {
pileIdx: 0,
takeAmnt: 7
};

await expect(
nim.functions.applyAction(preState, action)
).to.be.revertedWith("invalid pileIdx");
});
});

describe("isFinal", () => {
it("empty state is final", async () => {
const preState = {
players: [AddressZero, AddressZero],
turnNum: 49,
pileHeights: [0, 0, 0]
};
expect(await nim.functions.isStateTerminal(preState)).to.eq(true);
});

it("nonempty state is not final", async () => {
const preState = {
players: [AddressZero, AddressZero],
turnNum: 49,
pileHeights: [0, 1, 0]
};
expect(await nim.functions.isStateTerminal(preState)).to.eq(false);
});
});
});
Loading

0 comments on commit 8e93be7

Please sign in to comment.