From 56a21e1af6d0350429033aeb9e2b0cb4e1e6cd5a Mon Sep 17 00:00:00 2001 From: Alex Gap Date: Fri, 19 Feb 2021 14:51:30 -0800 Subject: [PATCH] test: use real chain id --- .../e2e-testing/__tests__/bad-network.test.ts | 6 ---- packages/e2e-testing/__tests__/e2e.test.ts | 27 +++++++------- packages/e2e-testing/chain-setup.ts | 27 ++++++++------ packages/e2e-testing/src/constants.ts | 9 ++--- packages/e2e-testing/src/payment-server.ts | 35 ++++++++++--------- packages/e2e-testing/src/receipt-server.ts | 4 ++- .../src/__tests__/payment-manager.test.ts | 4 +-- 7 files changed, 60 insertions(+), 52 deletions(-) diff --git a/packages/e2e-testing/__tests__/bad-network.test.ts b/packages/e2e-testing/__tests__/bad-network.test.ts index cf9f4f3..3df286d 100644 --- a/packages/e2e-testing/__tests__/bad-network.test.ts +++ b/packages/e2e-testing/__tests__/bad-network.test.ts @@ -50,13 +50,7 @@ const indexerServer = createReceiptServer([ // keys for attestation signing but no action is taken on startup `--numAllocations ${NUM_ALLOCATIONS}` ]); - const baseConfig = defaultTestConfig({ - networkConfiguration: { - chainNetworkID: process.env.CHAIN_ID - ? parseInt(process.env.CHAIN_ID) - : defaultTestConfig().networkConfiguration.chainNetworkID - }, chainServiceConfiguration: { attachChainService: false } diff --git a/packages/e2e-testing/__tests__/e2e.test.ts b/packages/e2e-testing/__tests__/e2e.test.ts index 5c0937b..b0b041c 100644 --- a/packages/e2e-testing/__tests__/e2e.test.ts +++ b/packages/e2e-testing/__tests__/e2e.test.ts @@ -17,7 +17,8 @@ import { PAYER_SERVER_DB_NAME, REQUEST_CID, RESPONSE_CID, - TEST_SUBGRAPH_ID + TEST_SUBGRAPH_ID, + CHAIN_ID } from '../src/constants'; import {createPaymentServer, createReceiptServer} from '../src/external-server'; @@ -55,18 +56,18 @@ const serverArgs = [ const gatewayServer = createPaymentServer(serverArgs); const indexerServer = createReceiptServer(serverArgs); -const baseConfig = defaultTestConfig({ - networkConfiguration: { - chainNetworkID: process.env.CHAIN_ID - ? parseInt(process.env.CHAIN_ID) - : defaultTestConfig().networkConfiguration.chainNetworkID - }, - chainServiceConfiguration: { - attachChainService: useChain, - provider: process.env.RPC_ENDPOINT, - pk: ETHERLIME_ACCOUNTS[0].privateKey - } -}); +const baseConfig = { + ...defaultTestConfig({ + chainServiceConfiguration: { + attachChainService: useChain, + provider: process.env.RPC_ENDPOINT, + pk: ETHERLIME_ACCOUNTS[0].privateKey + }, + networkConfiguration: { + chainNetworkID: CHAIN_ID + } + }) +}; const payerConfig = overwriteConfigWithDatabaseConnection(baseConfig, { database: PAYER_SERVER_DB_NAME diff --git a/packages/e2e-testing/chain-setup.ts b/packages/e2e-testing/chain-setup.ts index b1b5a14..675cae4 100644 --- a/packages/e2e-testing/chain-setup.ts +++ b/packages/e2e-testing/chain-setup.ts @@ -3,33 +3,40 @@ import {ETHERLIME_ACCOUNTS, GanacheServer} from '@statechannels/devtools'; import {utils} from 'ethers'; import {deploy as deployNitro} from '@statechannels/server-wallet/lib/deployment/deploy'; import {deploy as deployGraph} from '@graphprotocol/statechannels-contracts/deployment/deploy'; -import _ from 'lodash'; export default async function setup(): Promise { + if (process.env.CHAIN_NETWORK_ID) { + console.log( + `CHAIN_NETWORK_ID defined as ${process.env.CHAIN_NETWORK_ID}. Assuming chain env vars are set by caller` + ); + return; + } + + process.env['CHAIN_NETWORK_ID'] = '9002'; process.env['GANACHE_HOST'] = '0.0.0.0'; process.env['GANACHE_PORT'] = '8545'; process.env[ 'RPC_ENDPOINT' ] = `http://${process.env['GANACHE_HOST']}:${process.env['GANACHE_PORT']}`; - const ethPerAccount = utils.parseEther('100').toString(); const etherlimeAccounts = ETHERLIME_ACCOUNTS.map((account) => ({ ...account, amount: ethPerAccount })); - const defaultServerWalletAccount = { - privateKey: process.env.ETHEREUM_PRIVATE_KEY // useful if using rinkeby account for example - ? process.env.ETHEREUM_PRIVATE_KEY - : ETHERLIME_ACCOUNTS[0].privateKey, - amount: ethPerAccount - }; - const accounts = _.unionBy(etherlimeAccounts, [defaultServerWalletAccount], 'privateKey'); if (!process.env.GANACHE_PORT) { throw new Error('process.env.GANACHE_PORT must be defined'); } - const ganacheServer = new GanacheServer(parseInt(process.env.GANACHE_PORT), 1337, accounts); + const ganacheServer = new GanacheServer( + parseInt(process.env.GANACHE_PORT), + Number(process.env.CHAIN_NETWORK_ID), + etherlimeAccounts, + 10_000, // timeout + 10_00_000_000, // gasLimit + 1 // gasPrice + ); await ganacheServer.ready(); + const deployedNitroArtifacts = await deployNitro(); const deployedAttestationApp = await deployGraph(); diff --git a/packages/e2e-testing/src/constants.ts b/packages/e2e-testing/src/constants.ts index 900a3bd..78c7266 100644 --- a/packages/e2e-testing/src/constants.ts +++ b/packages/e2e-testing/src/constants.ts @@ -1,5 +1,5 @@ import {SubgraphDeploymentID} from '@graphprotocol/common-ts'; -import {BigNumber, constants, utils} from 'ethers'; +import {constants, utils} from 'ethers'; import * as base58 from 'bs58'; import {defaultTestConfig} from '@statechannels/server-wallet'; export const RECEIPT_SERVER_PORT = 5198; @@ -29,9 +29,10 @@ export const TEST_SUBGRAPH_ID = new SubgraphDeploymentID( export const REQUEST_CID = utils.hexZeroPad('0x1', 32); export const RESPONSE_CID = utils.hexZeroPad('0x2', 32); // We want to use the same chainId that we use in the payment manager/ receipt manager -export const CHAIN_ID = BigNumber.from( - defaultTestConfig().networkConfiguration.chainNetworkID -).toNumber(); +export const CHAIN_ID = process.env.CHAIN_NETWORK_ID + ? parseInt(process.env.CHAIN_NETWORK_ID) + : defaultTestConfig().networkConfiguration.chainNetworkID; + export const VERIFYING_CONTRACT = constants.AddressZero; export const TEST_GRAPHQL_RESPONSE = 'OK'; diff --git a/packages/e2e-testing/src/payment-server.ts b/packages/e2e-testing/src/payment-server.ts index 41513d3..83dd675 100644 --- a/packages/e2e-testing/src/payment-server.ts +++ b/packages/e2e-testing/src/payment-server.ts @@ -19,7 +19,8 @@ import {BigNumber, constants, Wallet} from 'ethers'; import {BN} from '@statechannels/wallet-core'; import { defaultTestConfig, - overwriteConfigWithDatabaseConnection + overwriteConfigWithDatabaseConnection, + ServerWalletConfig } from '@statechannels/server-wallet'; import {Address} from '@graphprotocol/statechannels-contracts'; import {ETHERLIME_ACCOUNTS} from '@statechannels/devtools'; @@ -32,7 +33,8 @@ import { PAYER_PRIVATE_KEY, REQUEST_CID, TEST_SUBGRAPH_ID, - TEST_ATTESTATION_APP_ADDRESS + TEST_ATTESTATION_APP_ADDRESS, + CHAIN_ID } from './constants'; type MessageSenderConfig = { @@ -148,6 +150,20 @@ const createTasks = (logger: Logger) => ({ logFile }: AnyArgs & {messageSenderConfig: MessageSenderConfig}) => { const messageSender = constructMessageSender(messageSenderConfig); + const walletConfig: ServerWalletConfig = defaultTestConfig({ + workerThreadAmount: Number(amountOfWorkerThreads), + databaseConfiguration: {connection: {database: pgDatabase}}, + chainServiceConfiguration: { + attachChainService: !!process.env.RPC_ENDPOINT, + provider: process.env.RPC_ENDPOINT, + pk: ETHERLIME_ACCOUNTS[0].privateKey + }, + loggingConfiguration: { + logDestination: logFile, + logLevel: 'debug' + }, + networkConfiguration: {chainNetworkID: CHAIN_ID} + }); const channelManager = await ChannelManager.create({ logger: logger.child({module: 'PaymentManager'}) as any, @@ -161,20 +177,7 @@ const createTasks = (logger: Logger) => ({ useLedger, syncOpeningChannelsPollIntervalMS: 2500, ensureAllocationsConcurrency: 10, - - walletConfig: defaultTestConfig({ - workerThreadAmount: Number(amountOfWorkerThreads), - databaseConfiguration: {connection: {database: pgDatabase}}, - chainServiceConfiguration: { - attachChainService: !!process.env.RPC_ENDPOINT, - provider: process.env.RPC_ENDPOINT, - pk: ETHERLIME_ACCOUNTS[0].privateKey - }, - loggingConfiguration: { - logDestination: logFile, - logLevel: 'debug' - } - }), + walletConfig, backoffStrategy: { numAttempts: 1, initialDelay: 50 diff --git a/packages/e2e-testing/src/receipt-server.ts b/packages/e2e-testing/src/receipt-server.ts index 17b4c22..c655de9 100644 --- a/packages/e2e-testing/src/receipt-server.ts +++ b/packages/e2e-testing/src/receipt-server.ts @@ -14,6 +14,7 @@ import {Logger, NetworkContracts} from '@graphprotocol/common-ts'; import {createTestLogger, generateAttestations} from './utils'; import { + CHAIN_ID, RECEIPT_PRIVATE_KEY, RECEIPT_SERVER_PORT, RECEIPT_SERVER_URL, @@ -70,7 +71,8 @@ const commands = { loggingConfiguration: { logDestination: args.logFile, logLevel: 'debug' - } + }, + networkConfiguration: {chainNetworkID: CHAIN_ID} } as const; const receiptManager = await ReceiptManager.create( diff --git a/packages/payments/src/__tests__/payment-manager.test.ts b/packages/payments/src/__tests__/payment-manager.test.ts index 7e2cf80..7a90bc2 100644 --- a/packages/payments/src/__tests__/payment-manager.test.ts +++ b/packages/payments/src/__tests__/payment-manager.test.ts @@ -97,8 +97,8 @@ beforeAll(async (done) => { defaultTestConfig({ databaseConfiguration: {connection: {database: PAYMENT_MANAGER_TEST_DB_NAME}}, networkConfiguration: { - chainNetworkID: process.env.CHAIN_ID - ? parseInt(process.env.CHAIN_ID) + chainNetworkID: process.env.CHAIN_NETWORK_ID + ? parseInt(process.env.CHAIN_NETWORK_ID) : defaultTestConfig().networkConfiguration.chainNetworkID }, chainServiceConfiguration: {