From b5bb44d6467006997347d1586738312f298e0b2e Mon Sep 17 00:00:00 2001 From: naz_dou Date: Wed, 6 Nov 2019 17:26:13 +0200 Subject: [PATCH] feat(AENS): Add nameFee validation to TxValidator --- es/tx/builder/schema.js | 13 +++++++++++-- es/tx/validator.js | 17 ++++++++++++++--- test/integration/channel.js | 2 +- test/integration/txVerification.js | 12 ++++++++++-- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/es/tx/builder/schema.js b/es/tx/builder/schema.js index 57b46f2244..0722bd502c 100644 --- a/es/tx/builder/schema.js +++ b/es/tx/builder/schema.js @@ -1212,7 +1212,8 @@ const VALIDATORS = { nonceUsed: 'nonceUsed', nonceHigh: 'nonceHigh', minGasPrice: 'minGasPrice', - vmAndAbiVersion: 'vmAndAbiVersion' + vmAndAbiVersion: 'vmAndAbiVersion', + insufficientBalanceForFeeNameFee: 'insufficientBalanceForFeeNameFee' } const ERRORS = { @@ -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 = [ @@ -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}`, diff --git a/es/tx/validator.js b/es/tx/validator.js index d9b7ed8268..419f4be847 100644 --- a/es/tx/validator.js +++ b/es/tx/validator.js @@ -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' @@ -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)) } } @@ -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 [] } diff --git a/test/integration/channel.js b/test/integration/channel.js index 95fbfab144..b8735610fc 100644 --- a/test/integration/channel.js +++ b/test/integration/channel.js @@ -43,7 +43,7 @@ function waitForChannel (channel) { ) } -describe.only('Channel', function () { +describe('Channel', function () { configure(this) this.timeout(120000) diff --git a/test/integration/txVerification.js b/test/integration/txVerification.js index 4dcab23fd7..57403e0c9c 100644 --- a/test/integration/txVerification.js +++ b/test/integration/txVerification.js @@ -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 () { @@ -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) + }) })