Skip to content

Commit

Permalink
fix(validator)!: recursive validator, simplify schema
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 24, 2021
1 parent 9e1fde7 commit 95a2a23
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 362 deletions.
16 changes: 9 additions & 7 deletions src/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as R from 'ramda'

import Chain from './'
import { AE_AMOUNT_FORMATS, formatAmount } from '../utils/amount-formatter'
import TransactionValidator from '../tx/validator'
import verifyTransaction from '../tx/validator'
import NodePool from '../node-pool'
import { assertedType } from '../utils/crypto'
import { pause } from '../utils/other'
Expand All @@ -37,12 +37,14 @@ import { DRY_RUN_ACCOUNT, NAME_ID_KEY } from '../tx/builder/schema'
async function sendTransaction (tx, options = {}) {
const { waitMined, verify } = R.merge(this.Ae.defaults, options)
if (verify) {
const { validation, tx: txObject, txType } = await this.unpackAndVerify(tx)
const validation = await verifyTransaction(tx, this.selectedNode.instance)
if (validation.length) {
throw Object.assign(new Error('Transaction verification error: ' + JSON.stringify(validation)), {
const message = 'Transaction verification errors: ' +
validation.map(v => v.message).join(', ')
throw Object.assign(new Error(message), {
code: 'TX_VERIFICATION_ERROR',
errorData: { validation, tx: txObject, txType },
txHash: tx
validation,
transaction: tx
})
}
}
Expand All @@ -60,7 +62,7 @@ async function sendTransaction (tx, options = {}) {
}
return { hash: txHash, rawTx: tx }
} catch (error) {
throw Object.assign(error, { rawTx: tx, verifyTx: () => this.unpackAndVerify(tx) })
throw Object.assign(error, { rawTx: tx, verifyTx: () => verifyTransaction(tx, this.selectedNode.instance) })
}
}

Expand Down Expand Up @@ -258,7 +260,7 @@ async function resolveName (nameOrId, prefix, { verify, resolveByNode } = {}) {
* @return {Object} ChainNode instance
* @example ChainNode({url: 'https://testnet.aeternity.io/'})
*/
const ChainNode = Chain.compose(TransactionValidator, NodePool, {
const ChainNode = Chain.compose(NodePool, {
methods: {
sendTransaction,
balance,
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Node from './node'
import NodePool from './node-pool'
import Tx from './tx'
import Transaction from './tx/tx'
import TransactionValidator from './tx/validator'
import verifyTransaction from './tx/validator'
import AccountBase from './account/base'
import AccountMultiple from './account/multiple'
import MemoryAccount from './account/memory'
Expand Down Expand Up @@ -81,7 +81,7 @@ export {
Oracle,
genSwaggerClient,
Transaction,
TransactionValidator,
verifyTransaction,
Tx,
TxBuilder,
TxBuilderHelper,
Expand Down
90 changes: 0 additions & 90 deletions src/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,93 +1161,3 @@ export const TX_DESERIALIZATION_SCHEMA = {
3: TX_SCHEMA_FIELD(CONTRACT_BYTE_CODE_LIMA, OBJECT_TAG_SOPHIA_BYTE_CODE)
}
}

// VERIFICATION SCHEMA

const ERROR_TYPE = { ERROR: 'error', WARNING: 'warning' }
const VERIFICATION_FIELD = (msg, verificationFn, error) => [msg, verificationFn, error]

const VALIDATORS = {
signature: 'signature',
insufficientFee: 'insufficientFee',
expiredTTL: 'expiredTTL',
insufficientBalanceForAmountFee: 'insufficientBalanceForAmountFee',
insufficientBalanceForAmount: 'insufficientBalanceForAmount',
nonceUsed: 'nonceUsed',
nonceHigh: 'nonceHigh',
minGasPrice: 'minGasPrice',
vmAndAbiVersion: 'vmAndAbiVersion',
insufficientBalanceForFeeNameFee: 'insufficientBalanceForFeeNameFee'
}

const ERRORS = {
invalidSignature: { key: 'InvalidSignature', type: ERROR_TYPE.ERROR, txKey: 'signature' },
insufficientFee: { key: 'InsufficientFee', type: ERROR_TYPE.ERROR, txKey: 'fee' },
expiredTTL: { key: 'ExpiredTTL', type: ERROR_TYPE.ERROR, txKey: 'ttl' },
insufficientBalanceForAmountFee: { key: 'InsufficientBalanceForAmountFee', type: ERROR_TYPE.WARNING, txKey: 'fee' },
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' },
vmAndAbiVersion: { key: 'vmAndAbiVersion', type: ERROR_TYPE.ERROR, txKey: 'ctVersion' },
insufficientBalanceForFeeNameFee: { key: 'insufficientBalanceForFeeNameFee', type: ERROR_TYPE.ERROR, txKey: 'nameFee' }
}

export const SIGNATURE_VERIFICATION_SCHEMA = [
VERIFICATION_FIELD(
() => 'The signature cannot be verified, please verify that you used the correct network id and the correct private key for the sender address',
VALIDATORS.signature,
ERRORS.invalidSignature
)
]
export const CONTRACT_VERIFICATION_SCHEMA = [
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
),
VERIFICATION_FIELD(
() => `The gasPrice must be bigger then ${MIN_GAS_PRICE}`,
VALIDATORS.minGasPrice,
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}`,
VALIDATORS.insufficientFee,
ERRORS.insufficientFee
),
VERIFICATION_FIELD(
({ height }) => `The TTL is already expired, the current height is ${height}`,
VALIDATORS.expiredTTL,
ERRORS.expiredTTL
),
VERIFICATION_FIELD(
({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
VALIDATORS.insufficientBalanceForAmountFee,
ERRORS.insufficientBalanceForAmountFee
),
VERIFICATION_FIELD(
({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
VALIDATORS.insufficientBalanceForAmount,
ERRORS.insufficientBalanceForAmount
),
VERIFICATION_FIELD(
({ accountNonce }) => `The nonce is invalid (already used). Next valid nonce is ${accountNonce + 1})`,
VALIDATORS.nonceUsed,
ERRORS.nonceUsed
),
VERIFICATION_FIELD(
({ accountNonce }) => `The nonce is technically valid but will not be processed immediately by the node (next valid nonce is ${accountNonce + 1})`,
VALIDATORS.nonceHigh,
ERRORS.nonceHigh
)
]
Loading

0 comments on commit 95a2a23

Please sign in to comment.