Skip to content

Commit

Permalink
refactor!: invert and rename forceCodeCheck option to validateByteCode
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Oct 27, 2021
1 parent 6b6a44e commit 72122fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 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
20 changes: 8 additions & 12 deletions test/integration/contract-aci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 72122fa

Please sign in to comment.