From 88e3788b806db39a66b396bb7b6ca765d0e82989 Mon Sep 17 00:00:00 2001 From: Aaron DeRuvo Date: Tue, 28 Nov 2023 12:25:32 +0200 Subject: [PATCH] use @celo/celo-devchain for sdk tests needing a devchain (#10763) * use @celo/celo-devchain package to as initial chain state rather than building it each time. * use new devchain * testing w/ contracts artifacts in devchain package * Bump version fix * bump celo-devchain version * BUMP celo-devchain to stable version * add back log feedback more accurate const names fix versions * LockedGold.getTotalPendingWithdrawalsCount requires contracts to be deployed at a higher version than us currently latest on celo mainnet. so note this to help developers understand why instead of just a VM revert message * dont ignore transaction-uri tests, these only take 3 seconds so its not worth separating them out. * bump celo/devchain version * latest beter all around --------- Co-authored-by: soloseng <102702451+soloseng@users.noreply.github.com> --- .changeset/gold-crabs-carry.md | 5 +++++ .github/workflows/celo-monorepo.yml | 9 --------- packages/cli/package.json | 3 +-- packages/cli/src/test-utils/setup.global.ts | 5 ++++- packages/dev-utils/src/ganache-setup.ts | 2 +- packages/sdk/contractkit/DEVELOP.md | 11 +++-------- packages/sdk/contractkit/package.json | 3 +-- .../contractkit/src/test-utils/setup.global.ts | 4 +++- .../src/wrappers/LockedGold.test.ts | 11 ++++++++--- .../sdk/contractkit/src/wrappers/LockedGold.ts | 16 +++++++++++++++- .../src/wrappers/MetaTransactionWallet.test.ts | 4 ++-- .../MetaTransactionWalletDeployer.test.ts | 8 ++++---- .../src/wrappers/SortedOracles.test.ts | 4 ++-- packages/sdk/transactions-uri/package.json | 3 +-- .../src/test-utils/setup.global.ts | 5 +++-- yarn.lock | 18 +++++++++++++++++- 16 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 .changeset/gold-crabs-carry.md diff --git a/.changeset/gold-crabs-carry.md b/.changeset/gold-crabs-carry.md new file mode 100644 index 00000000000..93c2639dad1 --- /dev/null +++ b/.changeset/gold-crabs-carry.md @@ -0,0 +1,5 @@ +--- +'@celo/contractkit': patch +--- + +add notice that LockedGold.getTotalPendingWithdrawalsCount is not yet available on all networks diff --git a/.github/workflows/celo-monorepo.yml b/.github/workflows/celo-monorepo.yml index ebb0b72857c..98e43854428 100644 --- a/.github/workflows/celo-monorepo.yml +++ b/.github/workflows/celo-monorepo.yml @@ -175,7 +175,6 @@ jobs: --ignore @celo/celotool \ --ignore @celo/celocli \ --ignore @celo/env-tests \ - --ignore @celo/transactions-uri \ --ignore '@celo/wallet-*' \ run test - name: Upload Jest Test Results @@ -325,10 +324,6 @@ jobs: uses: ./.github/actions/sync-workspace with: artifacts_to_cache: ${{ needs.install-dependencies.outputs.artifacts_to_cache }} - - name: Generate DevChain - run: | - cd packages/sdk/contractkit - yarn test:reset - name: Run tests run: | yarn --cwd=packages/sdk/contractkit test @@ -356,10 +351,6 @@ jobs: uses: ./.github/actions/sync-workspace with: artifacts_to_cache: ${{ needs.install-dependencies.outputs.artifacts_to_cache }} - - name: Generate DevChain - run: | - cd packages/cli - yarn test:reset - name: Run tests run: | yarn --cwd=packages/cli test diff --git a/packages/cli/package.json b/packages/cli/package.json index a05238a2658..569e6cae2c8 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -27,12 +27,11 @@ "lint": "tslint -c tslint.json --project tsconfig.json", "prepublish": "", "prepack": "yarn run build && oclif-dev manifest && oclif-dev readme", - "test:reset": "yarn --cwd ../protocol devchain:reset --migration_override ../dev-utils/src/migration-override.json --release_gold_contracts scripts/truffle/releaseGoldExampleConfigs.json", - "test:livechain": "yarn --cwd ../protocol devchain run-tar .tmp/devchain.tar.gz", "test": "TZ=UTC jest --runInBand" }, "dependencies": { "@celo/base": "^6.0.0", + "@celo/celo-devchain": "^6.0.3-beta.1", "@celo/connect": "^5.1.1", "@celo/contractkit": "^5.2.0", "@celo/explorer": "^5.0.6", diff --git a/packages/cli/src/test-utils/setup.global.ts b/packages/cli/src/test-utils/setup.global.ts index ac368faf7af..14e12f624d3 100644 --- a/packages/cli/src/test-utils/setup.global.ts +++ b/packages/cli/src/test-utils/setup.global.ts @@ -7,7 +7,10 @@ import * as path from 'path' // If there is not, then your editor probably deleted it automatically. export default async function globalSetup() { - await baseSetup(path.resolve(path.join(__dirname, '../../')), '.tmp/devchain.tar.gz', { + console.log('\nstarting ganache...') + const chainDataPath = path.join(path.dirname(require.resolve('@celo/celo-devchain')), '../chains') + // v10 refers to core contract release 10 + await baseSetup(path.resolve(chainDataPath), 'v10.tar.gz', { from_targz: true, }) } diff --git a/packages/dev-utils/src/ganache-setup.ts b/packages/dev-utils/src/ganache-setup.ts index 17b8fdd17c3..dd9746628dd 100644 --- a/packages/dev-utils/src/ganache-setup.ts +++ b/packages/dev-utils/src/ganache-setup.ts @@ -114,7 +114,7 @@ export default function setup( ;(global as any).stopGanache = stopGanache }) .catch((err) => { - console.error('Error starting ganache, Doing `yarn test:reset` might help') + console.error('Error starting ganache') console.error(err) process.exit(1) }) diff --git a/packages/sdk/contractkit/DEVELOP.md b/packages/sdk/contractkit/DEVELOP.md index c0fc7c967d0..7fe8fdfd578 100644 --- a/packages/sdk/contractkit/DEVELOP.md +++ b/packages/sdk/contractkit/DEVELOP.md @@ -2,21 +2,16 @@ ## Running Tests -To run test, we first need to generate a devchain snapshot: - -`yarn test:reset` - -and then: - `yarn test` ### Testing Azure HSM Signer -The tests include an in-memory mock implementation which tests a majority of the functionality. In the case that changes are made to any of the wallet/signing logic, the HSM signer should be tested end-to-end by using an Azure KeyVault. To test against the KeyVault, environment variables are used to provide the client ID and client secret for authentication. Please see the .env file for the required variables. After deploying your KeyVault and generating an ECDSA-SECP256k1 key, you must create a service principal account and provide it signing access to the KeyVault. Instructions on how to do this can be found here: https://www.npmjs.com/package/@azure/keyvault-keys#configuring-your-key-vault. If the .env variables are specified, the tests will automatically switch from using the mock client to the actual KeyVault. +The tests include an in-memory mock implementation which tests a majority of the functionality. In the case that changes are made to any of the wallet/signing logic, the HSM signer should be tested end-to-end by using an Azure KeyVault. To test against the KeyVault, environment variables are used to provide the client ID and client secret for authentication. Please see the .env file for the required variables. After deploying your KeyVault and generating an ECDSA-SECP256k1 key, you must create a service principal account and provide it signing access to the KeyVault. Instructions on how to do this can be found here: . If the .env variables are specified, the tests will automatically switch from using the mock client to the actual KeyVault. ## Testing Ledger Signer Similarly, the tests include an in-memory mock implementation of the Ledger signer. To run the tests using an actual Ledger device: + - Set `USE_PHYSICAL_LEDGER` to `true` in ledger-wallet.test.ts - Install libusb-dev `apt-get install lsb-release libudev-dev libusb-dev libusb-1.0-0 -y` - Add reference to package: `@ledgerhq/hw-transport-node-hid@5.11.0` @@ -28,4 +23,4 @@ To generate docs from [TSdoc](https://github.com/microsoft/tsdoc) annotations us `yarn docs` -To customize this generation, see the [linkdocs](./scripts/linkdocs.ts) script. \ No newline at end of file +To customize this generation, see the [linkdocs](./scripts/linkdocs.ts) script. diff --git a/packages/sdk/contractkit/package.json b/packages/sdk/contractkit/package.json index 362af9d8625..e216315ed54 100644 --- a/packages/sdk/contractkit/package.json +++ b/packages/sdk/contractkit/package.json @@ -23,8 +23,6 @@ "clean:all": "yarn clean && rm -rf src/generated", "prepublishOnly": "yarn build", "docs": "typedoc", - "test:reset": "yarn --cwd ../../protocol devchain:reset --migration_override ../../dev-utils/src/migration-override.json", - "test:livechain": "yarn --cwd ../../protocol devchain run-tar .tmp/devchain.tar.gz", "test": "jest --runInBand --forceExit", "lint": "tslint -c tslint.json --project ." }, @@ -45,6 +43,7 @@ "web3-core-helpers": "1.10.0" }, "devDependencies": { + "@celo/celo-devchain": "^6.0.3-beta.1", "@celo/odis-identifiers": "^1.0.0", "@celo/dev-utils": "0.0.1", "@celo/protocol": "1.0.2", diff --git a/packages/sdk/contractkit/src/test-utils/setup.global.ts b/packages/sdk/contractkit/src/test-utils/setup.global.ts index 5d50245d7ff..016be0d3266 100644 --- a/packages/sdk/contractkit/src/test-utils/setup.global.ts +++ b/packages/sdk/contractkit/src/test-utils/setup.global.ts @@ -9,7 +9,9 @@ import * as path from 'path' export default async function globalSetup() { console.log('\nstarting ganache...') - await baseSetup(path.resolve(path.join(__dirname, '../..')), '.tmp/devchain.tar.gz', { + const chainDataPath = path.join(path.dirname(require.resolve('@celo/celo-devchain')), '../chains') + // v10 refers to core contract release 10 + await baseSetup(path.resolve(chainDataPath), 'v10.tar.gz', { from_targz: true, }) await waitForPortOpen('localhost', 8545, 60) diff --git a/packages/sdk/contractkit/src/wrappers/LockedGold.test.ts b/packages/sdk/contractkit/src/wrappers/LockedGold.test.ts index 07950be8bce..fc2daf41ed2 100644 --- a/packages/sdk/contractkit/src/wrappers/LockedGold.test.ts +++ b/packages/sdk/contractkit/src/wrappers/LockedGold.test.ts @@ -43,8 +43,13 @@ testWithGanache('LockedGold Wrapper', (web3) => { await Promise.all(txos.map((txo) => txo.sendAndWaitForReceipt())) // }) - - test('should return the count of pending withdrawals', async () => { + // when this fails the 2 tests below should pass. + test('getTotalPendingWithdrawalsCount throws when version is below minimum', async () => { + expect(lockedGold.getTotalPendingWithdrawalsCount(account)).rejects.toThrowError( + 'getTotalPendingWithdrawalsCount not implemented for LockedGold version (1.1.3.0) deployed to this chain' + ) + }) + test.failing('should return the count of pending withdrawals', async () => { await lockedGold.lock().sendAndWaitForReceipt({ value: value * 2 }) await lockedGold.unlock(value).sendAndWaitForReceipt() await lockedGold.unlock(value).sendAndWaitForReceipt() @@ -53,7 +58,7 @@ testWithGanache('LockedGold Wrapper', (web3) => { expect(count).toEqBigNumber(2) }) - test('should return zero when there are no pending withdrawals', async () => { + test.failing('should return zero when there are no pending withdrawals', async () => { const count = await lockedGold.getTotalPendingWithdrawalsCount(account) expect(count).toEqBigNumber(0) }) diff --git a/packages/sdk/contractkit/src/wrappers/LockedGold.ts b/packages/sdk/contractkit/src/wrappers/LockedGold.ts index 4d0ba61231e..d36721a590c 100644 --- a/packages/sdk/contractkit/src/wrappers/LockedGold.ts +++ b/packages/sdk/contractkit/src/wrappers/LockedGold.ts @@ -7,6 +7,7 @@ import { import { Address, CeloTransactionObject, EventLog } from '@celo/connect' import BigNumber from 'bignumber.js' import { LockedGold } from '../generated/LockedGold' +import { ContractVersion } from '../versions' import { proxyCall, proxySend, @@ -400,9 +401,22 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning { /** * Returns the number of pending withdrawals for the specified account. * @param account The account. + * @notice This method is only available in version 1.1.4 or higher of the LockedGold contract. * @returns The count of pending withdrawals. */ - getTotalPendingWithdrawalsCount = proxyCall( + async getTotalPendingWithdrawalsCount(account: string) { + const minVersion = new ContractVersion(1, 1, 4, 0) + const version = await this.version() + if (version.isAtLeast(minVersion)) { + return this._getTotalPendingWithdrawalsCount(account) + } else { + throw new Error( + `getTotalPendingWithdrawalsCount not implemented for LockedGold version (${version.toString()}) deployed to this chain` + ) + } + } + + _getTotalPendingWithdrawalsCount = proxyCall( this.contract.methods.getTotalPendingWithdrawalsCount, undefined, valueToBigNumber diff --git a/packages/sdk/contractkit/src/wrappers/MetaTransactionWallet.test.ts b/packages/sdk/contractkit/src/wrappers/MetaTransactionWallet.test.ts index c0cb63ebd36..a231d601e79 100644 --- a/packages/sdk/contractkit/src/wrappers/MetaTransactionWallet.test.ts +++ b/packages/sdk/contractkit/src/wrappers/MetaTransactionWallet.test.ts @@ -1,7 +1,7 @@ import { Address } from '@celo/base/lib/address' import { Signature } from '@celo/base/lib/signatureUtils' +import MTWContractArtifacts from '@celo/celo-devchain/contracts/contracts-0.5/MetaTransactionWallet.json' import { testWithGanache } from '@celo/dev-utils/lib/ganache-test' -import MTWContract from '@celo/protocol/build/contracts/MetaTransactionWallet.json' import { generateTypedDataHash } from '@celo/utils/lib/sign-typed-data-utils' import { bufferToHex } from '@ethereumjs/util' import BigNumber from 'bignumber.js' @@ -15,7 +15,7 @@ import { } from './MetaTransactionWallet' const contract = require('@truffle/contract') -const MetaTransactionWallet = contract(MTWContract) +const MetaTransactionWallet = contract(MTWContractArtifacts) testWithGanache('MetaTransactionWallet Wrapper', (web3) => { MetaTransactionWallet.setProvider(web3.currentProvider) diff --git a/packages/sdk/contractkit/src/wrappers/MetaTransactionWalletDeployer.test.ts b/packages/sdk/contractkit/src/wrappers/MetaTransactionWalletDeployer.test.ts index 3c472bff308..335727826ff 100644 --- a/packages/sdk/contractkit/src/wrappers/MetaTransactionWalletDeployer.test.ts +++ b/packages/sdk/contractkit/src/wrappers/MetaTransactionWalletDeployer.test.ts @@ -1,16 +1,16 @@ import { Address, normalizeAddress } from '@celo/base' +import MTWContractArtifacts from '@celo/celo-devchain/contracts/contracts-0.5/MetaTransactionWallet.json' +import MTWDeployerContractArtifacts from '@celo/celo-devchain/contracts/contracts-0.5/MetaTransactionWalletDeployer.json' import { CeloTxReceipt, EventLog } from '@celo/connect' import { testWithGanache } from '@celo/dev-utils/lib/ganache-test' -import MTWContract from '@celo/protocol/build/contracts/MetaTransactionWallet.json' -import MTWDeployerContract from '@celo/protocol/build/contracts/MetaTransactionWalletDeployer.json' import { MetaTransactionWallet, newMetaTransactionWallet } from '../generated/MetaTransactionWallet' import { newProxy } from '../generated/Proxy' import { newKitFromWeb3 } from '../kit' import { MetaTransactionWalletDeployerWrapper } from './MetaTransactionWalletDeployer' const contract = require('@truffle/contract') -const MetaTransactionWalletDeployer = contract(MTWDeployerContract) -const MetaTransactionWallet = contract(MTWContract) +const MetaTransactionWalletDeployer = contract(MTWDeployerContractArtifacts) +const MetaTransactionWallet = contract(MTWContractArtifacts) testWithGanache('MetaTransactionWallet Wrapper', (web3) => { MetaTransactionWalletDeployer.setProvider(web3.currentProvider) diff --git a/packages/sdk/contractkit/src/wrappers/SortedOracles.test.ts b/packages/sdk/contractkit/src/wrappers/SortedOracles.test.ts index f456ccd048b..2befdbb9f9a 100644 --- a/packages/sdk/contractkit/src/wrappers/SortedOracles.test.ts +++ b/packages/sdk/contractkit/src/wrappers/SortedOracles.test.ts @@ -1,7 +1,7 @@ import { Address } from '@celo/connect' import { describeEach } from '@celo/dev-utils/lib/describeEach' import { NetworkConfig, testWithGanache, timeTravel } from '@celo/dev-utils/lib/ganache-test' -import SortedOraclesABI from '@celo/protocol/build/contracts/SortedOracles.json' +import SortedOraclesArtifacts from '@celo/celo-devchain/contracts/contracts-0.5/SortedOracles.json' import { CeloContract } from '../base' import { StableToken } from '../celo-tokens' import { newKitFromWeb3 } from '../kit' @@ -19,7 +19,7 @@ TEST NOTES: testWithGanache('SortedOracles Wrapper', (web3) => { const kit = newKitFromWeb3(web3) - const SortedOracles = truffleContract(SortedOraclesABI) + const SortedOracles = truffleContract(SortedOraclesArtifacts) SortedOracles.setProvider(web3.currentProvider) async function reportAsOracles( diff --git a/packages/sdk/transactions-uri/package.json b/packages/sdk/transactions-uri/package.json index fad02977d43..a0b5bcac9b2 100644 --- a/packages/sdk/transactions-uri/package.json +++ b/packages/sdk/transactions-uri/package.json @@ -17,8 +17,6 @@ "build": "tsc -b .", "clean": "tsc -b . --clean", "docs": "typedoc", - "test:reset": "yarn --cwd ../../protocol devchain:reset --migration_override ../../dev-utils/src/migration-override.json", - "test:livechain": "yarn --cwd ../../protocol devchain run-tar .tmp/devchain.tar.gz", "test": "jest --runInBand", "lint": "tslint -c tslint.json --project .", "prepublishOnly": "yarn build" @@ -33,6 +31,7 @@ "web3-eth-abi": "1.10.0" }, "devDependencies": { + "@celo/celo-devchain": "^6.0.3-beta.1", "@celo/dev-utils": "0.0.1", "@celo/contractkit": "^5.2.0", "dotenv": "^8.2.0" diff --git a/packages/sdk/transactions-uri/src/test-utils/setup.global.ts b/packages/sdk/transactions-uri/src/test-utils/setup.global.ts index 5d50245d7ff..c6605fd245c 100644 --- a/packages/sdk/transactions-uri/src/test-utils/setup.global.ts +++ b/packages/sdk/transactions-uri/src/test-utils/setup.global.ts @@ -8,8 +8,9 @@ import * as path from 'path' // If there is not, then your editor probably deleted it automatically. export default async function globalSetup() { - console.log('\nstarting ganache...') - await baseSetup(path.resolve(path.join(__dirname, '../..')), '.tmp/devchain.tar.gz', { + const chainDataPath = path.join(path.dirname(require.resolve('@celo/celo-devchain')), '../chains') + // v10 refers to core contract release 10 + await baseSetup(path.resolve(chainDataPath), 'v10.tar.gz', { from_targz: true, }) await waitForPortOpen('localhost', 8545, 60) diff --git a/yarn.lock b/yarn.lock index 0a639f1e419..798c757c77c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -986,6 +986,17 @@ "@stablelib/blake2xs" "0.10.4" big-integer "^1.6.44" +"@celo/celo-devchain@^6.0.3-beta.1": + version "6.0.3-beta.1" + resolved "https://registry.yarnpkg.com/@celo/celo-devchain/-/celo-devchain-6.0.3-beta.1.tgz#0c0637c4646904de578fa28ebe2dcb3f8ad35259" + integrity sha512-Pjqbooy1eJ9qyMyIka3GFP0UrLATtSHJtGj/TNtuJ5nlpaBMAy2oA1zjKh0AsqyhvNkjs4Os6mgFYVEm5XuKuw== + dependencies: + commander "^10.0.0" + ganache "npm:@celo/ganache@7.8.0-unofficial.0" + node-fetch "^3.3.0" + targz "^1.0.1" + tmp "^0.2.1" + "@celo/identity@^5.1.1": version "5.1.1" resolved "https://registry.yarnpkg.com/@celo/identity/-/identity-5.1.1.tgz#e56cea67d820c7deae147e50b353f3c76b3e6d4d" @@ -9764,6 +9775,11 @@ commander@3.0.2, commander@^3.0.0: resolved "https://registry.yarnpkg.com/commander/-/commander-3.0.2.tgz#6837c3fb677ad9933d1cfba42dd14d5117d6b39e" integrity sha512-Gar0ASD4BDyKC4hl4DwHqDrmvjoxWKZigVnAbn5H1owvm4CxCPdb0HQDehwNYMJpla5+M2tPmPARzhtYuwpHow== +commander@^10.0.0: + version "10.0.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06" + integrity sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug== + commander@^2.12.1, commander@^2.20.3, commander@^2.8.1: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" @@ -22853,7 +22869,7 @@ tmp@^0.1.0: dependencies: rimraf "^2.6.3" -tmp@~0.2.1: +tmp@^0.2.1, tmp@~0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.1.tgz#8457fc3037dcf4719c251367a1af6500ee1ccf14" integrity sha512-76SUhtfqR2Ijn+xllcI5P1oyannHNHByD80W1q447gU3mp9G9PSpGdWmjUOHRDPiHYacIk66W7ubDTuPF3BEtQ==