Skip to content
Snippets Groups Projects
Unverified Commit 6a3c1992 authored by Éloïs's avatar Éloïs
Browse files

[fix] #1277 add tx to block only if its valid

parent aeaab097
No related branches found
No related tags found
No related merge requests found
...@@ -346,9 +346,13 @@ export const LOCAL_RULES_FUNCTIONS = { ...@@ -346,9 +346,13 @@ export const LOCAL_RULES_FUNCTIONS = {
}, },
checkTxAmounts: async (block:BlockDTO) => { checkTxAmounts: async (block:BlockDTO) => {
let result = true;
for (const tx of block.transactions) { for (const tx of block.transactions) {
LOCAL_RULES_HELPERS.checkTxAmountsValidity(tx); if (!LOCAL_RULES_HELPERS.checkTxAmountsValidity(tx)) {
result = false;
}
} }
return result;
}, },
checkTxRecipients: async (block:BlockDTO) => { checkTxRecipients: async (block:BlockDTO) => {
...@@ -430,21 +434,26 @@ function checkSingleMembershipSignature(ms:any) { ...@@ -430,21 +434,26 @@ function checkSingleMembershipSignature(ms:any) {
return verify(ms.getRaw(), ms.signature, ms.issuer); return verify(ms.getRaw(), ms.signature, ms.issuer);
} }
function checkBunchOfTransactions(transactions:TransactionDTO[], conf:ConfDTO, options?:{ dontCareAboutChaining?:boolean }){ function checkBunchOfTransactions(transactions:TransactionDTO[], conf:ConfDTO, options?:{ dontCareAboutChaining?:boolean }):Promise<boolean>{
const block:any = { transactions, identities: [], joiners: [], actives: [], leavers: [], revoked: [], excluded: [], certifications: [] }; const block:any = { transactions, identities: [], joiners: [], actives: [], leavers: [], revoked: [], excluded: [], certifications: [] };
const index = Indexer.localIndex(block, conf) const index = Indexer.localIndex(block, conf)
return (async () => { let local_rule = LOCAL_RULES_FUNCTIONS;
let local_rule = LOCAL_RULES_FUNCTIONS; return Promise.all([
await local_rule.checkTxLen(block); local_rule.checkTxLen(block),
await local_rule.checkTxIssuers(block); local_rule.checkTxIssuers(block),
await local_rule.checkTxSources(block); local_rule.checkTxSources(block),
await local_rule.checkTxRecipients(block); local_rule.checkTxRecipients(block),
await local_rule.checkTxAmounts(block); local_rule.checkTxAmounts(block),
await local_rule.checkTxSignature(block); local_rule.checkTxSignature(block),
if (!options || !options.dontCareAboutChaining) { local_rule.checkMaxTransactionChainingDepth(block, conf, index),
await local_rule.checkMaxTransactionChainingDepth(block, conf, index); ]).then(function(values) {
} for (const value of values) {
})() if (!value) {
return false;
}
}
return true;
});
} }
export const LOCAL_RULES_HELPERS = { export const LOCAL_RULES_HELPERS = {
...@@ -509,6 +518,7 @@ export const LOCAL_RULES_HELPERS = { ...@@ -509,6 +518,7 @@ export const LOCAL_RULES_HELPERS = {
} }
deltas[i] = delta; deltas[i] = delta;
} }
return true;
}, },
getMaxPossibleVersionNumber: async (current:DBBlock) => { getMaxPossibleVersionNumber: async (current:DBBlock) => {
......
...@@ -104,7 +104,7 @@ export class BlockGenerator { ...@@ -104,7 +104,7 @@ export class BlockGenerator {
return [cur, newWoTMembers, finalJoinData, leavers, updates]; return [cur, newWoTMembers, finalJoinData, leavers, updates];
} }
private async findTransactions(current:DBBlock, options:{ dontCareAboutChaining?:boolean }) { private async findTransactions(current:DBBlock, options:{ dontCareAboutChaining:boolean }) {
const ALSO_CHECK_PENDING_TXS = true const ALSO_CHECK_PENDING_TXS = true
const versionMin = current ? Math.min(CommonConstants.LAST_VERSION_FOR_TX, current.version) : CommonConstants.DOCUMENTS_VERSION; const versionMin = current ? Math.min(CommonConstants.LAST_VERSION_FOR_TX, current.version) : CommonConstants.DOCUMENTS_VERSION;
const txs = await this.dal.getTransactionsPending(versionMin); const txs = await this.dal.getTransactionsPending(versionMin);
...@@ -113,26 +113,23 @@ export class BlockGenerator { ...@@ -113,26 +113,23 @@ export class BlockGenerator {
for (const obj of txs) { for (const obj of txs) {
obj.currency = this.conf.currency obj.currency = this.conf.currency
const tx = TransactionDTO.fromJSONObject(obj); const tx = TransactionDTO.fromJSONObject(obj);
try {
/*const tx_check_1 = Promise.all([await LOCAL_RULES_HELPERS.checkBunchOfTransactions(passingTxs.concat(tx), this.conf, options)])
const nextBlockWithFakeTimeVariation = { medianTime: current.medianTime + 1 }; const nextBlockWithFakeTimeVariation = { medianTime: current.medianTime + 1 };
const tx_check_2 = Promise.all([await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, nextBlockWithFakeTimeVariation, this.conf, this.dal, ALSO_CHECK_PENDING_TXS)]) let logger = this.logger;
const tx_check_3 = Promise.all([await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal)]) Promise.all([
if (tx_check_1 && tx_check_2 && tx_check_3) { await LOCAL_RULES_HELPERS.checkBunchOfTransactions(passingTxs.concat(tx), this.conf, options),
await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, nextBlockWithFakeTimeVariation, this.conf, this.dal, ALSO_CHECK_PENDING_TXS),
await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal)
]).then(function(values) {
if (values[0] && values[1])
transactions.push(tx); transactions.push(tx);
passingTxs.push(tx); passingTxs.push(tx);
this.logger.info('Transaction %s added to block', tx.hash); logger.info('Transaction %s added to block', tx.hash);
}*/ }).catch (function(err) {
} catch (err) { logger.error(err);
this.logger.error(err); const currentNumber = (current && current.number) || 0;
const currentNumber = (current && current.number) || 0; const blockstamp = tx.blockstamp || (currentNumber + '-');
const blockstamp = tx.blockstamp || (currentNumber + '-'); const txBlockNumber = parseInt(blockstamp.split('-')[0]);
const txBlockNumber = parseInt(blockstamp.split('-')[0]); });
// 10 blocks before removing the transaction
if (currentNumber - txBlockNumber + 1 >= CommonConstants.TRANSACTION_MAX_TRIES) {
await this.dal.removeTxByHash(tx.hash);
}
}
} }
return transactions; return transactions;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment