Skip to content

Commit

Permalink
Merge pull request #1310 from aeternity/feature/require-6-compiler
Browse files Browse the repository at this point in the history
Rewrite exception handling in tests and require compiler 6 and above
  • Loading branch information
mradkov authored Oct 28, 2021
2 parents ac805e8 + 72122fa commit 6d9003b
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 619 deletions.
4 changes: 2 additions & 2 deletions docs/guides/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Note:
- To be used if a contract is already deployed.
- `filesystem` (default: {})
- Key-value map with name of the include as key and source code of the include as value.
- `forceCodeCheck` (default: true)
- Don't check source code.
- `validateByteCode` (default: false)
- Compare source code with on-chain version.
- `opt` (default: {})
- Object with other [transaction options](../transaction-options.md) which will be provided to **every transaction** that is initiated using the contract instance. You should be aware that:
- For most of these additional options it doesn't make sense to define them at contract instance level.
Expand Down
6 changes: 3 additions & 3 deletions src/contract/aci/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import TxObject from '../../tx/tx-object'
* @param {String} [options.aci] Contract ACI
* @param {String} [options.contractAddress] Contract address
* @param {Object} [options.filesystem] Contact source external namespaces map
* @param {Object} [options.forceCodeCheck=true] Don't check contract code
* @param {Boolean} [options.validateByteCode] Compare source code with on-chain version
* @return {ContractInstance} JS Contract API
* @example
* const contractIns = await client.getContractInstance(sourceCode)
Expand All @@ -48,7 +48,7 @@ import TxObject from '../../tx/tx-object'
* Also you can call contract like: await contractIns.methods.setState(123, options)
* Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is stateful or not
*/
export default async function getContractInstance (source, { aci, contractAddress, filesystem = {}, forceCodeCheck = true, ...otherOptions } = {}) {
export default async function getContractInstance (source, { aci, contractAddress, filesystem = {}, validateByteCode, ...otherOptions } = {}) {
aci = aci || await this.contractGetACI(source, { filesystem })
if (contractAddress) contractAddress = await this.resolveName(contractAddress, 'ct', { resolveByNode: true })
const instance = {
Expand All @@ -71,7 +71,7 @@ export default async function getContractInstance (source, { aci, contractAddres
if (contractAddress) {
const contract = await this.getContract(contractAddress).catch(e => null)
if (!contract || !contract.active) throw new Error(`Contract with address ${contractAddress} not found on-chain or not active`)
if (!forceCodeCheck) {
if (validateByteCode) {
const onChanByteCode = (await this.getContractByteCode(contractAddress)).bytecode
const isCorrespondingBytecode = await this.validateByteCodeAPI(onChanByteCode, instance.source, instance.options).catch(e => false)
if (!isCorrespondingBytecode) throw new Error('Contract source do not correspond to the contract bytecode deployed on the chain')
Expand Down
4 changes: 1 addition & 3 deletions src/contract/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default AsyncInit.compose(ContractBase, {
this.compilerVersion = client.spec.info.version
this._compilerApi = client.api

this._isCompiler6 = semverSatisfies(this.compilerVersion, COMPILER_6_GE_VERSION, COMPILER_LT_VERSION)
if (ignoreVersion) return
if (!semverSatisfies(this.compilerVersion, COMPILER_GE_VERSION, COMPILER_LT_VERSION)) {
throw new Error(`Unsupported compiler version ${this.compilerVersion}. ` +
Expand Down Expand Up @@ -170,6 +169,5 @@ export default AsyncInit.compose(ContractBase, {
}
})

const COMPILER_GE_VERSION = '4.1.0'
const COMPILER_6_GE_VERSION = '6.0.0'
const COMPILER_GE_VERSION = '6.0.0'
const COMPILER_LT_VERSION = '7.0.0'
6 changes: 3 additions & 3 deletions src/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const REQUESTS = {
}
}
} catch (e) {
console.error(e)
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
}
},
Expand Down Expand Up @@ -112,7 +112,7 @@ const REQUESTS = {
try {
onAcc = resolveOnAccount(instance.addresses(), address, opt)
} catch (e) {
console.error(e)
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
}
try {
Expand Down Expand Up @@ -156,7 +156,7 @@ const REQUESTS = {
}
}
} catch (e) {
console.error(e)
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/utils/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const parseBigNumber = (number: string | number | BigNumber): string =>
* @return {Boolean}
*/
export const isBigNumber = (number: string | number | BigNumber): boolean =>
!isNaN(number as number) || Number.isInteger(number) || BigNumber.isBigNumber(number)
['number', 'object', 'string', 'bigint'].includes(typeof number) &&
(!isNaN(number as number) || Number.isInteger(number) || BigNumber.isBigNumber(number))

/**
* BigNumber ceil operation
Expand Down
59 changes: 16 additions & 43 deletions test/integration/accounts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
* Copyright (c) 2021 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,6 +16,7 @@
*/

import { describe, it, before } from 'mocha'
import { expect } from 'chai'
import { getSdk, BaseAe, networkId } from './'
import { generateKeyPair } from '../../src/utils/crypto'
import BigNumber from 'bignumber.js'
Expand Down Expand Up @@ -53,13 +54,8 @@ describe('Accounts', function () {
return wallet.spend(1, receiver).should.be.rejectedWith(Error)
})

it('spending minus amount of tokens', async () => {
try {
await wallet.spend(-1, receiver)
} catch (e) {
e.message.should.be.equal('Transaction build error. {"amount":"-1 must be >= 0"}')
}
})
it('spending negative amount of tokens', () => expect(wallet.spend(-1, receiver))
.to.be.rejectedWith('Transaction build error. {"amount":"-1 must be >= 0"}'))
})

it('determines the balance using `balance`', async () => {
Expand Down Expand Up @@ -155,45 +151,30 @@ describe('Accounts', function () {
const current = await sdk.address()
const accounts = sdk.addresses()
const onAccount = accounts.find(acc => acc !== current)
// SPEND

const { tx } = await sdk.spend(1, await sdk.address(), { onAccount })
tx.senderId.should.be.equal(onAccount)
current.should.be.equal(current)
})

it('Fail on invalid account', async () => {
// SPEND
try {
await sdk.spend(1, await sdk.address(), { onAccount: 1 })
} catch (e) {
e.message.should.be.equal('Unknown account type: number (account: 1)')
}
return expect(sdk.spend(1, await sdk.address(), { onAccount: 1 }))
.to.be.rejectedWith('Unknown account type: number (account: 1)')
})

it('Fail on non exist account', async () => {
// SPEND
try {
await sdk.spend(1, await sdk.address(), { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' })
} catch (e) {
e.message.should.be.equal('Account for ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk not available')
}
return expect(sdk.spend(1, await sdk.address(), { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' }))
.to.be.rejectedWith('Account for ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk not available')
})

it('Fail on no accounts', async () => {
// SPEND
try {
await openClient.spend(1, await sdk.address())
} catch (e) {
e.message.should.be.equal('No account or wallet configured')
}
return expect(openClient.spend(1, await sdk.address()))
.to.be.rejectedWith('No account or wallet configured')
})

it('Invalid on account options', async () => {
try {
await sdk.sign('tx_Aasdasd', { onAccount: 123 })
} catch (e) {
e.message.should.be.equal('Unknown account type: number (account: 123)')
}
it('Invalid on account options', () => {
return expect(sdk.sign('tx_Aasdasd', { onAccount: 123 }))
.to.be.rejectedWith('Unknown account type: number (account: 123)')
})
it('Make operation on account using keyPair/MemoryAccount', async () => {
const keypair = generateKeyPair()
Expand All @@ -214,16 +195,8 @@ describe('Accounts', function () {
const keypair = generateKeyPair()
keypair.publicKey = 'ak_bev1aPMdAeJTuUiCJ7mHbdQiAizrkRGgoV9FfxHYb6pAxo5WY'
const data = 'Hello'
try {
await sdk.sign(data, { onAccount: keypair })
} catch (e) {
e.message.should.be.equal('Invalid Key Pair')
}
try {
await sdk.address({ onAccount: keypair })
} catch (e) {
e.message.should.be.equal('Invalid Key Pair')
}
await expect(sdk.sign(data, { onAccount: keypair })).to.be.rejectedWith('Invalid Key Pair')
await expect(sdk.address({ onAccount: keypair })).to.be.rejectedWith('Invalid Key Pair')
})
})

Expand Down
8 changes: 3 additions & 5 deletions test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { describe, it, before } from 'mocha'
import { expect } from 'chai'
import { getSdk } from './'
import { randomName } from '../utils'
import * as R from 'ramda'
Expand Down Expand Up @@ -66,11 +67,8 @@ describe('Aens', function () {
const onAccount = sdk.addresses().find(acc => acc !== current)
const { pointers } = await sdk.getName(name)
pointers.length.should.be.equal(0)
try {
await sdk.spend(100, name, { onAccount })
} catch (e) {
e.message.should.be.equal(`Name ${name} don't have pointers for ak`)
}
await expect(sdk.spend(100, name, { onAccount }))
.to.be.rejectedWith(`Name ${name} don't have pointers for ak`)
})
it('Call contract using AENS name', async () => {
const identityContract = `
Expand Down
9 changes: 3 additions & 6 deletions test/integration/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import { describe, it, before } from 'mocha'
import { expect } from 'chai'
import { getSdk } from './'
import { generateKeyPair } from '../../src/utils/crypto'

Expand Down Expand Up @@ -59,12 +60,8 @@ describe('Node Chain', function () {
return sdk.height().should.eventually.be.at.least(target)
})
it('Can verify transaction from broadcast error', async () => {
try {
await sdkAccount.spend(0, publicKey, { fee: 100, verify: false })
} catch (e) {
const validation = await e.verifyTx()
validation.should.has.lengthOf(1)
}
const error = await sdkAccount.spend(0, publicKey, { fee: 100, verify: false }).catch(e => e)
expect(await error.verifyTx()).to.have.lengthOf(1)
})
it('Get top block', async () => {
const top = await sdk.topBlock()
Expand Down
Loading

0 comments on commit 6d9003b

Please sign in to comment.