From 72122facdcec9921202b210c387c19509b26e578 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Wed, 27 Oct 2021 14:36:37 +0300 Subject: [PATCH] refactor!: invert and rename forceCodeCheck option to validateByteCode --- docs/guides/contracts.md | 4 ++-- src/contract/aci/index.js | 6 +++--- test/integration/contract-aci.js | 20 ++++++++------------ 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/docs/guides/contracts.md b/docs/guides/contracts.md index cb2cbe880c..d5799f07de 100644 --- a/docs/guides/contracts.md +++ b/docs/guides/contracts.md @@ -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. diff --git a/src/contract/aci/index.js b/src/contract/aci/index.js index 349af97cbd..3e68e003ba 100644 --- a/src/contract/aci/index.js +++ b/src/contract/aci/index.js @@ -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) @@ -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 = { @@ -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') diff --git a/test/integration/contract-aci.js b/test/integration/contract-aci.js index b2af6affba..44e2fd2ec2 100644 --- a/test/integration/contract-aci.js +++ b/test/integration/contract-aci.js @@ -238,23 +238,19 @@ describe('Contract ACI Interface', function () { }) it('Generate ACI object with not corresponding bytecode', async () => { - try { - await sdk.getContractInstance( - identityContract, - { contractAddress: contractObject.deployInfo.address, ttl: 0 } - ) - } catch (e) { - e.message.should.be.equal('Contract source do not correspond to the contract bytecode deployed on the chain') - } - }) - - it('Generate ACI object with not corresponding bytecode and force this check', async () => { await sdk.getContractInstance( identityContract, - { forceCodeCheck: true, contractAddress: contractObject.deployInfo.address, ttl: 0 } + { contractAddress: contractObject.deployInfo.address, ttl: 0 } ) }) + it('Generate ACI object with not corresponding bytecode and check is code the same', async () => { + await expect(sdk.getContractInstance( + identityContract, + { validateByteCode: true, contractAddress: contractObject.deployInfo.address, ttl: 0 } + )).to.be.rejectedWith('Contract source do not correspond to the contract bytecode deployed on the chain') + }) + it('Throw error on creating contract instance with invalid contractAddress', async () => { await expect(sdk.getContractInstance( testContract,