Skip to content

Commit

Permalink
use @celo/celo-devchain for sdk tests needing a devchain (#10763)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aaronmgdr and soloseng authored Nov 28, 2023
1 parent 379f920 commit 88e3788
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-crabs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@celo/contractkit': patch
---

add notice that LockedGold.getTotalPendingWithdrawalsCount is not yet available on all networks
9 changes: 0 additions & 9 deletions .github/workflows/celo-monorepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/test-utils/setup.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
2 changes: 1 addition & 1 deletion packages/dev-utils/src/ganache-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
11 changes: 3 additions & 8 deletions packages/sdk/contractkit/DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <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.

## 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`
Expand All @@ -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.
To customize this generation, see the [linkdocs](./scripts/linkdocs.ts) script.
3 changes: 1 addition & 2 deletions packages/sdk/contractkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
},
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/contractkit/src/test-utils/setup.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions packages/sdk/contractkit/src/wrappers/LockedGold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
Expand Down
16 changes: 15 additions & 1 deletion packages/sdk/contractkit/src/wrappers/LockedGold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -400,9 +401,22 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning<LockedGold> {
/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/contractkit/src/wrappers/SortedOracles.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions packages/sdk/transactions-uri/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/transactions-uri/src/test-utils/setup.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 88e3788

Please sign in to comment.