Skip to content

Commit

Permalink
feat(AENS): Add nameFee validation to TxValidator (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Nov 11, 2019
1 parent 07cb0e7 commit 5250e75
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
13 changes: 11 additions & 2 deletions es/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,8 @@ const VALIDATORS = {
nonceUsed: 'nonceUsed',
nonceHigh: 'nonceHigh',
minGasPrice: 'minGasPrice',
vmAndAbiVersion: 'vmAndAbiVersion'
vmAndAbiVersion: 'vmAndAbiVersion',
insufficientBalanceForFeeNameFee: 'insufficientBalanceForFeeNameFee'
}

const ERRORS = {
Expand All @@ -1224,7 +1225,8 @@ const ERRORS = {
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' },
vmAndAbiVersion: { key: 'vmAndAbiVersion', type: ERROR_TYPE.ERROR, txKey: 'ctVersion' }
vmAndAbiVersion: { key: 'vmAndAbiVersion', type: ERROR_TYPE.ERROR, txKey: 'ctVersion' },
insufficientBalanceForFeeNameFee: { key: 'insufficientBalanceForFeeNameFee', type: ERROR_TYPE.ERROR, txKey: 'nameFee' }
}

export const SIGNATURE_VERIFICATION_SCHEMA = [
Expand All @@ -1246,6 +1248,13 @@ export const CONTRACT_VERIFICATION_SCHEMA = [
ERRORS.minGasPrice
)
]
export const NAME_CLAIM_VERIFICATION_SCHEMA = [
VERIFICATION_FIELD(
({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
VALIDATORS.insufficientBalanceForFeeNameFee,
ERRORS.insufficientBalanceForFeeNameFee
)
]
export const BASE_VERIFICATION_SCHEMA = [
VERIFICATION_FIELD(
({ minFee }) => `The fee for the transaction is too low, the minimum fee for this transaction is ${minFee}`,
Expand Down
17 changes: 14 additions & 3 deletions es/tx/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { encode } from '../tx/builder/helpers'

import { BigNumber } from 'bignumber.js'
import {
BASE_VERIFICATION_SCHEMA, CONTRACT_VERIFICATION_SCHEMA, MIN_GAS_PRICE, OBJECT_ID_TX_TYPE,
OBJECT_TAG_SIGNED_TRANSACTION, PROTOCOL_VM_ABI,
SIGNATURE_VERIFICATION_SCHEMA, TX_TYPE
BASE_VERIFICATION_SCHEMA,
CONTRACT_VERIFICATION_SCHEMA,
MIN_GAS_PRICE,
NAME_CLAIM_VERIFICATION_SCHEMA,
OBJECT_ID_TX_TYPE,
OBJECT_TAG_SIGNED_TRANSACTION,
PROTOCOL_VM_ABI,
SIGNATURE_VERIFICATION_SCHEMA,
TX_TYPE
} from './builder/schema'
import { calculateFee, unpackTx } from './builder'
import { NodePool } from '../node-pool'
Expand Down Expand Up @@ -68,6 +74,9 @@ const VALIDATORS = {
.reduce((acc, [key, value]) =>
[...acc, value === undefined ? true : txProtocol[key].includes(parseInt(value))],
[]).includes(false)
},
insufficientBalanceForFeeNameFee ({ nameFee, fee, balance, VSN }) {
return VSN === 1 || BigNumber(balance).gt(BigNumber(nameFee).plus(fee))
}
}

Expand Down Expand Up @@ -188,6 +197,8 @@ function customVerification (txType, data) {
case TX_TYPE.contractCall:
case TX_TYPE.oracleRegister:
return verifySchema(CONTRACT_VERIFICATION_SCHEMA, data)
case TX_TYPE.nameClaim:
return verifySchema(NAME_CLAIM_VERIFICATION_SCHEMA, data)
default:
return []
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function waitForChannel (channel) {
)
}

describe.only('Channel', function () {
describe('Channel', function () {
configure(this)
this.timeout(120000)

Expand Down
12 changes: 10 additions & 2 deletions test/integration/txVerification.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { before, describe } from 'mocha'
import { before, describe, it } from 'mocha'
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 ERRORS = [...BASE_VERIFICATION_SCHEMA, ...SIGNATURE_VERIFICATION_SCHEMA].reduce((acc, [msg, v, error]) => error.type === 'error' ? [...acc, error.txKey] : acc, [])
const channelCreate = 'tx_+NkLAfhCuECIIeWttRUiZ32uriBdmM1t+dCg90KuG2ABxOiuXqzpAul6uTWvsyfx3EFJDah6trudrityh+6XSX3mkPEimhgGuJH4jzIBoQELtO15J/l7UeG8teE0DRIzWyorEsi8UiHWPEvLOdQeYYgbwW1nTsgAAKEB6bv2BOYRtUYKOzmZ6Xcbb2BBfXPOfFUZ4S9+EnoSJcqIG8FtZ07IAACIAWNFeF2KAAAKAIYSMJzlQADAoDBrIcoop8JfZ4HOD9p3nDTiNthj7jjl+ArdHwEMUrvQgitwOr/v3Q=='

describe('Verify Transaction', function () {
Expand Down Expand Up @@ -88,4 +88,12 @@ describe('Verify Transaction', function () {
const res = await client.unpackAndVerify(channelCreate)
Array.isArray(res.validation).should.be.equal(true)
})
it('Verify nameFee for nameClaim transaction', async () => {
const tx = 'tx_+KILAfhCuEAtbc38n/FH8jZHO0DkEkiLZZm8ypEzZEhbjyHtaoEYkENOE9tD+Xp6smFMou9X521oI4gkFBQGwSQaQk6Z7XMNuFr4WCACoQHkWpoidhJW2EZEega88I1P9Ktw1DFBUWwrzkr5jC5zUAORc29tZUF1Y3Rpb24uY2hhaW6HDwTrMteR15AJQ0VVyE5TcqKSstgfbGV6hg9HjghAAAAGpIPS'
const res = await client.unpackAndVerify(tx)
const nameFeeError = res.validation.find(err => err.txKey === 'nameFee')
nameFeeError.should.be.an('object')
nameFeeError.type.should.be.equal('error')
nameFeeError.msg.indexOf('The account balance').should.not.be.equal(-1)
})
})

0 comments on commit 5250e75

Please sign in to comment.