Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add vm version validation to tx validator #425

Merged
merged 7 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions es/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,8 @@ const VALIDATORS = {
insufficientBalanceForAmount: 'insufficientBalanceForAmount',
nonceUsed: 'nonceUsed',
nonceHigh: 'nonceHigh',
minGasPrice: 'minGasPrice'
minGasPrice: 'minGasPrice',
vmAndAbiVersion: 'vmAndAbiVersion'
}

const ERRORS = {
Expand All @@ -1029,7 +1030,8 @@ const ERRORS = {
insufficientBalanceForAmount: { key: 'InsufficientBalanceForAmount', type: ERROR_TYPE.WARNING, txKey: 'amount' },
nonceUsed: { key: 'NonceUsed', type: ERROR_TYPE.ERROR, txKey: 'nonce' },
nonceHigh: { key: 'NonceHigh', type: ERROR_TYPE.WARNING, txKey: 'nonce' },
minGasPrice: { key: 'minGasPrice', type: ERROR_TYPE.ERROR, txKey: 'gasPrice' }
minGasPrice: { key: 'minGasPrice', type: ERROR_TYPE.ERROR, txKey: 'gasPrice' },
vmAndAbiVersion: { key: 'vmAndAbiVersion', type: ERROR_TYPE.ERROR, txKey: 'ctVersion' }
}

export const SIGNATURE_VERIFICATION_SCHEMA = [
Expand Down Expand Up @@ -1074,5 +1076,10 @@ export const BASE_VERIFICATION_SCHEMA = [
() => `The gasPrice must be bigger then ${MIN_GAS_PRICE}`,
VALIDATORS.minGasPrice,
ERRORS.minGasPrice
),
VERIFICATION_FIELD(
({ ctVersion, consensusProtocolVersion, txType }) => `Wrong abi/vm version, Supported is: ${PROTOCOL_VM_ABI[consensusProtocolVersion] ? JSON.stringify(PROTOCOL_VM_ABI[consensusProtocolVersion][txType]) : ' None for this protocol ' + consensusProtocolVersion}`,
VALIDATORS.vmAndAbiVersion,
ERRORS.vmAndAbiVersion
)
]
23 changes: 20 additions & 3 deletions es/tx/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { encode } from '../tx/builder/helpers'
import { BigNumber } from 'bignumber.js'
import {
BASE_VERIFICATION_SCHEMA, MIN_GAS_PRICE, OBJECT_ID_TX_TYPE,
OBJECT_TAG_SIGNED_TRANSACTION,
OBJECT_TAG_SIGNED_TRANSACTION, PROTOCOL_VM_ABI,
SIGNATURE_VERIFICATION_SCHEMA
} from './builder/schema'
import { calculateFee, unpackTx } from './builder'
Expand Down Expand Up @@ -53,6 +53,21 @@ const VALIDATORS = {
},
minGasPrice ({ gasPrice }) {
return isNaN(gasPrice) || BigNumber(gasPrice).gte(BigNumber(MIN_GAS_PRICE))
},
// VM/ABI version validation based on consensus protocol version
vmAndAbiVersion ({ ctVersion, consensusProtocolVersion, txType }) {
// If not contract tx
if (!ctVersion) return true
const supportedProtocol = PROTOCOL_VM_ABI[consensusProtocolVersion]
// If protocol not implemented
if (!supportedProtocol) return true
// If protocol for tx type not implemented
const txProtocol = supportedProtocol[txType]

return !Object.entries(ctVersion)
.reduce((acc, [key, value]) =>
[...acc, value === undefined ? true : txProtocol[key].includes(parseInt(value))],
[]).includes(false)
}
}

Expand All @@ -68,7 +83,8 @@ const resolveDataForBase = async (chain, { ownerPublicKey }) => {
height: (await chain.api.getCurrentKeyBlockHeight()).height,
balance: accountBalance,
accountNonce,
ownerPublicKey
ownerPublicKey,
consensusProtocolVersion: chain.consensusProtocolVersion
}
}

Expand Down Expand Up @@ -147,7 +163,8 @@ async function verifyTx ({ tx, signatures, rlpEncoded }, networkId) {
const resolvedData = {
minFee: calculateFee(0, OBJECT_ID_TX_TYPE[+tx.tag], { gas, params: tx, showWarning: false }),
...(await resolveDataForBase(this, { ownerPublicKey })),
...tx
...tx,
txType: OBJECT_ID_TX_TYPE[+tx.tag]
}
const signatureVerification = signatures && signatures.length
? verifySchema(SIGNATURE_VERIFICATION_SCHEMA, {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function waitForChannel (channel) {
)
}

describe.skip('Channel', function () {
describe('Channel', function () {
configure(this)

let initiator
Expand Down Expand Up @@ -661,7 +661,7 @@ describe.skip('Channel', function () {
).should.be.equal(true)
})

it.skip('can create a contract and accept', async () => {
it('can create a contract and accept', async () => {
initiatorCh = await Channel({
...sharedParams,
role: 'initiator',
Expand Down Expand Up @@ -689,7 +689,7 @@ describe.skip('Channel', function () {
contractEncodeCall = (method, args) => initiator.contractEncodeCallDataAPI(identityContract, method, args)
})

it.skip('can create a contract and reject', async () => {
it('can create a contract and reject', async () => {
responderShouldRejectUpdate = true
const code = await initiator.compileContractAPI(identityContract)
const callData = await initiator.contractEncodeCallDataAPI(identityContract, 'init', [])
Expand Down Expand Up @@ -777,7 +777,7 @@ describe.skip('Channel', function () {
}).should.eventually.be.rejectedWith('Rejected: Call not found')
})

it.skip('can get contract state', async () => {
it('can get contract state', async () => {
const result = await initiatorCh.getContractState(contractAddress)
result.should.eql({
contract: {
Expand Down
22 changes: 14 additions & 8 deletions test/integration/txVerification.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { configure, ready } from '.'
import { generateKeyPair } from '../../es/utils/crypto'
import { BASE_VERIFICATION_SCHEMA, SIGNATURE_VERIFICATION_SCHEMA } from '../../es/tx/builder/schema'

const WARNINGS = [...SIGNATURE_VERIFICATION_SCHEMA, ...BASE_VERIFICATION_SCHEMA].reduce((acc, [msg, v, error]) => error.type === 'warning' ? [...acc, error.txKey]: acc, [])
const ERRORS = [...BASE_VERIFICATION_SCHEMA, ...SIGNATURE_VERIFICATION_SCHEMA,].reduce((acc, [msg, v, error]) => error.type === 'error' ? [...acc, error.txKey]: acc, [])
const WARNINGS = [...SIGNATURE_VERIFICATION_SCHEMA, ...BASE_VERIFICATION_SCHEMA].reduce((acc, [msg, v, error]) => error.type === 'warning' ? [...acc, error.txKey] : acc, [])
const ERRORS = [...BASE_VERIFICATION_SCHEMA, ...SIGNATURE_VERIFICATION_SCHEMA,].reduce((acc, [msg, v, error]) => error.type === 'error' ? [...acc, error.txKey] : acc, [])

describe('Verify Transaction', function () {
configure(this)
Expand All @@ -31,12 +31,11 @@ describe('Verify Transaction', function () {
absoluteTtl: true
})

const {validation} = await client.unpackAndVerify(spendTx)
const { validation } = await client.unpackAndVerify(spendTx)
const warning = validation
.filter(({type}) => type === 'warning')
.filter(({ type }) => type === 'warning')
.map(({ txKey }) => txKey)


JSON.stringify(WARNINGS).should.be.equals(JSON.stringify(warning))
})
it('check errors', async () => {
Expand All @@ -54,12 +53,12 @@ describe('Verify Transaction', function () {
// Sign using another account
const signedTx = await client.signTransaction(spendTx)

const {validation} = await client.unpackAndVerify(signedTx)
const { validation } = await client.unpackAndVerify(signedTx)
const error = validation
.filter(({type}) => type === 'error')
.filter(({ type, txKey }) => type === 'error') // exclude contract vm/abi, has separated test for it
.map(({ txKey }) => txKey)

JSON.stringify(ERRORS.filter(e => e !== 'gasPrice')).should.be.equals(JSON.stringify(error))
JSON.stringify(ERRORS.filter(e => e !== 'gasPrice' && e !== 'ctVersion')).should.be.equals(JSON.stringify(error))
})
it('verify transaction before broadcast', async () => {
client = await ready(this)
Expand All @@ -78,4 +77,11 @@ describe('Verify Transaction', function () {
atLeastOneError.should.be.equal(true)
}
})
it('Verify vmVersion/abiVersion for contract transactions', async () => {
// Contract create transaction with wrong abi/vm version (vm: 3, abi: 0)
const contractCreateTx = 'tx_+QSaKgGhASLDuRmSBJZv91HE219uqXb2L0adh+bilzBWUi93m5blArkD+PkD9UYCoI2tdssfNdXZOclcaOwkTNB2S/SXIVsLDi7KUoxJ3Jki+QL7+QEqoGjyZ2M4/1CIOaukd0nv+ovofvKE8gf7PZmYcBzVOIfFhG1haW64wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACg//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPkBy6C5yVbyizFJqfWYeqUF89obIgnMVzkjQAYrtsG9n5+Z6oRpbml0uGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//////////////////////////////////////////+5AUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAP//////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//////////////////////////////////////////7jMYgAAZGIAAISRgICAUX+5yVbyizFJqfWYeqUF89obIgnMVzkjQAYrtsG9n5+Z6hRiAADAV1CAUX9o8mdjOP9QiDmrpHdJ7/qL6H7yhPIH+z2ZmHAc1TiHxRRiAACvV1BgARlRAFtgABlZYCABkIFSYCCQA2ADgVKQWWAAUVlSYABSYADzW2AAgFJgAPNbWVlgIAGQgVJgIJADYAAZWWAgAZCBUmAgkANgA4FSgVKQVltgIAFRUVlQgJFQUICQUJBWW1BQgpFQUGIAAIxWhTIuMS4wgwMAAIcF9clYKwgAAAAAgxgX+IQ7msoAuGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAILnJVvKLMUmp9Zh6pQXz2hsiCcxXOSNABiu2wb2fn5nqAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkansY'
const { validation } = await client.unpackAndVerify(contractCreateTx)
const vmAbiError = validation.find(el => el.txKey === 'ctVersion')
vmAbiError.msg.split(',')[0].should.be.equal('Wrong abi/vm version')
})
})