diff --git a/.travis_e2e_test.sh b/.travis_e2e_test.sh index 7deb9549..8ab06f78 100755 --- a/.travis_e2e_test.sh +++ b/.travis_e2e_test.sh @@ -6,6 +6,8 @@ eval "$(GIMME_GO_VERSION=1.10.2 gimme)" export BUILD_ID=build-1046 +bash e2e_user_deployer_whitelist_test.sh + bash e2e_tests.sh if [ "${PLASMA_TEST:-}" ]; then diff --git a/e2e_support/user-deployer-whitelist/genesis.json b/e2e_support/user-deployer-whitelist/genesis.json new file mode 100644 index 00000000..cc375996 --- /dev/null +++ b/e2e_support/user-deployer-whitelist/genesis.json @@ -0,0 +1,114 @@ +{ + "contracts": [ + { + "vm": "plugin", + "format": "plugin", + "name": "coin", + "location": "coin:1.0.0", + "init": { + "accounts": [ + { + "owner": { + "chain_id": "default", + "local": "dHVZ95Bqnn3dsget6tyq72cJS3E=" + }, + "balance": 10000 + } + ] + } + }, + { + "vm": "plugin", + "format": "plugin", + "name": "dposV2", + "location": "dposV2:2.0.0", + "init": { + "params": { + "validatorCount": "21", + "electionCycleLength": "604800" + }, + "validators": [ + { + "pubKey": "XvkW4a0U6o8x/SYtDx5RFTWcATdf9212+OodZWFR0mc=", + "power": "10" + }, + { + "pubKey": "YOVrdKhgPrRp0oy2NCHkJv0MQBdVWbbsnwH2FUkOPng=", + "power": "10" + } + ] + } + }, + { + "vm": "plugin", + "format": "plugin", + "name": "chainconfig", + "location": "chainconfig:1.0.0", + "init": { + "owner": { + "chainId": "default", + "local": "S7Mg3vyaNSnqchAyvZWoYF7S1UM=" + }, + "params": { + "voteThreshold":"100", + "numBlockConfirmations":"1" + }, + "features": [ + { + "name":"mw:deploy-wl", + "status":"WAITING", + "autoEnable": true + }, + { + "name":"mw:userdeploy-wl", + "status":"WAITING", + "autoEnable": true + } + ] + } + }, + { + "vm": "plugin", + "format": "plugin", + "name": "deployerwhitelist", + "location": "deployerwhitelist:1.0.0", + "init": { + "owner": { + "chainId": "default", + "local": "S7Mg3vyaNSnqchAyvZWoYF7S1UM=" + }, + "deployers": [ + { + "address": { + "chainId":"default", + "local":"duzR9yYfz0xoTil74+3wO4JeAcQ=" + }, + "flags": 2 + } + ] + } + }, + { + "vm": "plugin", + "format": "plugin", + "name": "user-deployer-whitelist", + "location": "user-deployer-whitelist:1.0.0", + "init": { + "owner": { + "chainId": "default", + "local": "S7Mg3vyaNSnqchAyvZWoYF7S1UM=" + }, + "tier_info": [ + { + "id": 0, + "fee": "10", + "name": "tier1" + } + ] + } + + } + + ] +} + diff --git a/e2e_support/user-deployer-whitelist/loom.yaml b/e2e_support/user-deployer-whitelist/loom.yaml new file mode 100644 index 00000000..1b18b6ee --- /dev/null +++ b/e2e_support/user-deployer-whitelist/loom.yaml @@ -0,0 +1,16 @@ +ChainID: "default" +RegistryVersion: 2 +ReceiptsVersion: 2 +DPOSVersion: 2 +DeployerWhitelist: + ContractEnabled: true +ChainConfig: + ContractEnabled: true +UserDeployerWhitelist: + ContractEnabled: true +ContractLogLevel: "debug" +LoomLogLevel: "debug" +BootLegacyDPoS: true +GoContractDeployerWhitelist: + Enabled: true +DeployEnabled: true diff --git a/e2e_user_deployer_whitelist_test.sh b/e2e_user_deployer_whitelist_test.sh new file mode 100644 index 00000000..82ff836b --- /dev/null +++ b/e2e_user_deployer_whitelist_test.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +# To run this script with a locally built loom binary set the LOOM_BIN env var to point to your loom +# binary. + +set -euxo pipefail + +# Prepare env +DEFAULT_GOPATH=$GOPATH +REPO_ROOT=`pwd` +LOOM_DIR=`pwd`/tmp/e2e +LOOM_BIN=`pwd`/loom + +# Check available platforms +PLATFORM='unknown' +unamestr=`uname` +if [[ "$unamestr" == 'Linux' ]]; then + PLATFORM='linux' +elif [[ "$unamestr" == 'Darwin' ]]; then + PLATFORM='osx' +else + echo "Platform not supported on this script yet" + exit -1 +fi + +download_dappchain() { + cd $LOOM_DIR + wget https://private.delegatecall.com/loom/linux/latest/loom + chmod +x loom + LOOM_BIN=`pwd`/loom +} + +setup_dappchain() { + cd $LOOM_DIR + $LOOM_BIN init -f + cp -R $REPO_ROOT/e2e_support/* . + cp -R $REPO_ROOT/e2e_support/tm-config/* chaindata/config/ + mkdir -p contracts + } + +init_dappchain() { + cd $LOOM_DIR + rm -rf app.db + rm -rf chaindata + cp $REPO_ROOT/e2e_support/user-deployer-whitelist/loom.yml loom.yml + $LOOM_BIN init -f + cp $REPO_ROOT/e2e_support/user-deployer-whitelist/genesis.json genesis.json + echo 'Loom DAppChain initialized in ' $LOOM_DIR +} + +start_chains() { + cd $LOOM_DIR + $LOOM_BIN run & + LOOM_PID=$! + sleep 5 +} + +stop_chains() { + if [[ -n "$LOOM_PID" ]]; then + kill -9 $LOOM_PID + LOOM_PID="" + fi +} + +run_tests() { + yarn test:node + yarn test:browser + yarn e2e:node +} + +cleanup() { + stop_chains + export GOPATH=$DEFAULT_GOPATH +} + +if [ "${TRAVIS:-}" ]; then + mkdir -p $LOOM_DIR +fi + +#download_dappchain + +setup_dappchain + +init_dappchain + +trap cleanup EXIT + +start_chains +cd $LOOM_DIR + +yarn e2e:user-deployer-whitelist + +stop_chains + +cleanup + +sleep 1 + +if [ "${TRAVIS:-}" ]; then + rm -rf $LOOM_DIR +fi diff --git a/package.json b/package.json index 38588578..6ae7ff96 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "e2e:browser": "yarn proto && yarn abi && tsc && webpack --config webpack.e2e.test.config.js | tap-spec", "e2e:plasma-cash:honest": "yarn proto && yarn abi && yarn copy-contracts && tsc && yarn tape && tape -r dotenv/config dotenv_config_path=./.env.test dist/tests/e2e/plasma-cash/test-honest-operator.js | tap-spec", "e2e:plasma-cash:hostile": "yarn proto && yarn abi && yarn copy-contracts && tsc && yarn tape && tape -r dotenv/config dotenv_config_path=./.env.test dist/tests/e2e/plasma-cash/test-hostile-operator.js | tap-spec", + "e2e:user-deployer-whitelist": "yarn proto && yarn abi && yarn copy-contracts && tsc && yarn tape && tape -r dotenv/config dotenv_config_path=./.env.test dist/tests/e2e/user-deployer-whitelist-tests.js | tap-spec", "proto": "node ./scripts/gen-proto.js", "abi": "node ./scripts/abi.js && bash ./scripts/fix_types.sh", "copy-contracts": "node ./scripts/copy-contracts.js", diff --git a/src/tests/e2e/user-deployer-whitelist-tests.ts b/src/tests/e2e/user-deployer-whitelist-tests.ts new file mode 100644 index 00000000..96579010 --- /dev/null +++ b/src/tests/e2e/user-deployer-whitelist-tests.ts @@ -0,0 +1,132 @@ +import test from 'tape' +import BN from 'bn.js' +import { + Address, + Contracts, + CryptoUtils, + createDefaultTxMiddleware, + Client, + LocalAddress +} from '../../index' +import { createTestHttpClient, waitForMillisecondsAsync } from '../helpers' +import { B64ToUint8Array } from '../../crypto-utils' + +const toCoinE18 = (amount: number): BN => { + return new BN(10).pow(new BN(18)).mul(new BN(amount)) +} + +async function getClientAndContractCoin( + createClient: () => Client +): Promise<{ + acct1Client: Client + acct1Coin: Contracts.Coin + acct1PubKey: Uint8Array +}> { + const acct1PrivKey = B64ToUint8Array( + 'Hz9P3aHH62mO75A6uMVW3mn0U1KkZSq3t03jfOZfyZxjyJoJctNDY6awaVqOpjCGTjHZZxkc23Z3l39EjLOIFQ==' + ) + + const acct1PubKey = CryptoUtils.publicKeyFromPrivateKey(acct1PrivKey) + const acct1Client = createClient() + + acct1Client.txMiddleware = createDefaultTxMiddleware(acct1Client, acct1PrivKey) + + const acct1Coin = await Contracts.Coin.createAsync( + acct1Client, + new Address(acct1Client.chainId, LocalAddress.fromPublicKey(acct1PubKey)) + ) + + return { acct1Client, acct1Coin, acct1PubKey, } +} + +async function testBalanceOf(t: test.Test, createClient: () => Client) { + const { acct1Client, acct1Coin, acct1PubKey, } = await getClientAndContractCoin(createClient) + + const acct1Owner = new Address(acct1Client.chainId, LocalAddress.fromPublicKey(acct1PubKey)) + const acct1Balance = await acct1Coin.getBalanceOfAsync(acct1Owner) + t.assert(acct1Balance.eq(toCoinE18(10000)), 'Acct 1 balance should be 10000e18') + acct1Client.disconnect() + +} + +async function testApprove(t: test.Test, createClient: () => Client) { + const { acct1Client, acct1Coin, acct1PubKey } = await getClientAndContractCoin(createClient) + + const contractAddr = await acct1Client.getContractAddressAsync('user-deployer-whitelist') + console.log("Contract Address", contractAddr) + await acct1Coin.approveAsync(contractAddr!, toCoinE18(10000)) + + acct1Client.disconnect() +} + + +async function getClientAndContract( + createClient: () => Client +): Promise<{ + client: Client + userdeployerwhitelist: Contracts.UserDeployerWhitelist + pubKey: Uint8Array + deployerAddress: Address +}> { + const privKey = B64ToUint8Array( + 'Hz9P3aHH62mO75A6uMVW3mn0U1KkZSq3t03jfOZfyZxjyJoJctNDY6awaVqOpjCGTjHZZxkc23Z3l39EjLOIFQ==' + ) + const pubKey = CryptoUtils.publicKeyFromPrivateKey(privKey) + const client = createClient() + client.txMiddleware = createDefaultTxMiddleware(client, privKey) + + const userdeployerwhitelist = await Contracts.UserDeployerWhitelist.createAsync( + client, + new Address(client.chainId, LocalAddress.fromPublicKey(pubKey)) + ) + + const privKeyDeployer = B64ToUint8Array( + 'ZGTsP8LUJkEWiqEZq3hqOKfCHCeV+CbYgbZK2/y53aDAaCJPBla4uLTsEtzm/Dczk8Ml8TL5+rAwKNfbuRZihg==' + ) + const pubKeyDeployer = CryptoUtils.publicKeyFromPrivateKey(privKey) + + const deployerAddress = new Address(client.chainId, LocalAddress.fromPublicKey(pubKeyDeployer)) + + return { client, userdeployerwhitelist, pubKey, deployerAddress } +} + + +async function addUserDeployer(t: test.Test, createClient: () => Client) { + const { client, userdeployerwhitelist, pubKey, deployerAddress } = await getClientAndContract(createClient) + await userdeployerwhitelist.addDeployerAsync(deployerAddress, 0) + client.disconnect() +} + +async function getUserDeployer(t: test.Test, createClient: () => Client) { + const { client, userdeployerwhitelist, pubKey } = await getClientAndContract(createClient) + await userdeployerwhitelist.getDeployersAsync(new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))) + + client.disconnect() +} + +async function getTierInfo(t: test.Test, createClient: () => Client) { + const { client, userdeployerwhitelist, pubKey } = await getClientAndContract(createClient) + + await userdeployerwhitelist.getTierInfoAsync(0) + + client.disconnect() +} + +test('user-deployer-whitelist', async t => { + try { + await testBalanceOf(t, createTestHttpClient) + await waitForMillisecondsAsync(1000) + await testApprove(t, createTestHttpClient) + await waitForMillisecondsAsync(1000) + await addUserDeployer(t, createTestHttpClient) + await waitForMillisecondsAsync(1000) + await getUserDeployer(t, createTestHttpClient) + await waitForMillisecondsAsync(1000) + await getTierInfo(t, createTestHttpClient) + await waitForMillisecondsAsync(1000) + + } catch (err) { + t.fail(err) + } + t.end() +}) diff --git a/src/tests/e2e_tests.ts b/src/tests/e2e_tests.ts index 14292d5c..51eca8b5 100644 --- a/src/tests/e2e_tests.ts +++ b/src/tests/e2e_tests.ts @@ -26,6 +26,8 @@ import './e2e/multiple-events-nd-tests' // Contracts import './e2e/coin-tests' import './e2e/address-mapper-tests' +import './e2e/user-deployer-whitelist-tests' + // TODO: Re-enable once this is updated to DPOSv2 //import './e2e/dpos-tests'