Skip to content

Commit cde67e4

Browse files
author
Éloïs
committed
Merge branch '1277_fix' into '1.6'
[fix] #1277 G1-Test is stuck because of wrong generated blocks (chained transactions) Closes #1277 See merge request nodes/typescript/duniter!1248
2 parents 62a7d6c + f97e182 commit cde67e4

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

app/lib/rules/global_rules.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {CommonConstants} from "../common-libs/constants"
99
import {IdentityDTO} from "../dto/IdentityDTO"
1010
import {hashf} from "../common"
1111
import {Indexer} from "../indexer"
12+
import {DBTx} from "../dal/sqliteDAL/TxsDAL"
1213

1314
const _ = require('underscore')
1415

@@ -80,7 +81,7 @@ export const GLOBAL_RULES_FUNCTIONS = {
8081
return true;
8182
},
8283

83-
checkSourcesAvailability: async (block:{ transactions:TransactionDTO[], medianTime: number }, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean) => {
84+
checkSourcesAvailability: async (block:{ transactions:TransactionDTO[], medianTime: number }, conf:ConfDTO, dal:FileDAL, findSourceTx:(txHash:string) => Promise<DBTx|null>) => {
8485
const txs = block.transactions
8586
const current = await dal.getCurrentBlockOrNull();
8687
for (const tx of txs) {
@@ -98,12 +99,12 @@ export const GLOBAL_RULES_FUNCTIONS = {
9899
let src = inputs[k];
99100
let dbSrc = await dal.getSource(src.identifier, src.pos);
100101
logger.debug('Source %s:%s:%s:%s = %s', src.amount, src.base, src.identifier, src.pos, dbSrc && dbSrc.consumed);
101-
if (!dbSrc && alsoCheckPendingTransactions) {
102+
if (!dbSrc) {
102103
// For chained transactions which are checked on sandbox submission, we accept them if there is already
103104
// a previous transaction of the chain already recorded in the pool
104105
dbSrc = await (async () => {
105106
let hypotheticSrc:any = null;
106-
let targetTX = await dal.getTxByHash(src.identifier);
107+
let targetTX = await findSourceTx(src.identifier);
107108
if (targetTX) {
108109
let outputStr = targetTX.outputs[src.pos];
109110
if (outputStr) {
@@ -193,10 +194,15 @@ export const GLOBAL_RULES_HELPERS = {
193194

194195
checkExistsPubkey: (pub:string, dal:FileDAL) => dal.getWrittenIdtyByPubkey(pub),
195196

196-
checkSingleTransaction: (tx:TransactionDTO, block:{ medianTime: number }, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean = false) => GLOBAL_RULES_FUNCTIONS.checkSourcesAvailability({
197+
checkSingleTransaction: (
198+
tx:TransactionDTO,
199+
block:{ medianTime: number },
200+
conf:ConfDTO,
201+
dal:FileDAL,
202+
findSourceTx:(txHash:string) => Promise<DBTx|null>) => GLOBAL_RULES_FUNCTIONS.checkSourcesAvailability({
197203
transactions: [tx],
198204
medianTime: block.medianTime
199-
}, conf, dal, alsoCheckPendingTransactions),
205+
}, conf, dal, findSourceTx),
200206

201207
checkTxBlockStamp: async (tx:TransactionDTO, dal:FileDAL) => {
202208
const number = parseInt(tx.blockstamp.split('-')[0])

app/modules/prover/lib/blockGenerator.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export class BlockGenerator {
105105
}
106106

107107
private async findTransactions(current:DBBlock, options:{ dontCareAboutChaining?:boolean }) {
108-
const ALSO_CHECK_PENDING_TXS = true
109108
const versionMin = current ? Math.min(CommonConstants.LAST_VERSION_FOR_TX, current.version) : CommonConstants.DOCUMENTS_VERSION;
110109
const txs = await this.dal.getTransactionsPending(versionMin);
111110
const transactions = [];
@@ -116,7 +115,9 @@ export class BlockGenerator {
116115
try {
117116
await LOCAL_RULES_HELPERS.checkBunchOfTransactions(passingTxs.concat(tx), this.conf, options)
118117
const nextBlockWithFakeTimeVariation = { medianTime: current.medianTime + 1 };
119-
await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, nextBlockWithFakeTimeVariation, this.conf, this.dal, ALSO_CHECK_PENDING_TXS);
118+
await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, nextBlockWithFakeTimeVariation, this.conf, this.dal, async (txHash:string) => {
119+
return _.findWhere(passingTxs, { hash: txHash }) || null
120+
});
120121
await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal);
121122
transactions.push(tx);
122123
passingTxs.push(tx);
@@ -779,12 +780,12 @@ class ManualRootGenerator implements BlockGeneratorInterface {
779780

780781
if (newcomers.length > 0) {
781782
const answers = await inquirer.prompt([{
782-
type: "checkbox",
783-
name: "uids",
784-
message: "Newcomers to add",
785-
choices: uids,
786-
default: uids[0]
787-
}]);
783+
type: "checkbox",
784+
name: "uids",
785+
message: "Newcomers to add",
786+
choices: uids,
787+
default: uids[0]
788+
}]);
788789
newcomers.forEach((newcomer:string) => {
789790
if (~answers.uids.indexOf(preJoinData[newcomer].ms.userid))
790791
filtered[newcomer] = preJoinData[newcomer];

app/service/BlockchainService.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { IdentityForRequirements } from './BlockchainService';
2-
"use strict";
1+
import {IdentityForRequirements} from './BlockchainService';
32
import {Server} from "../../server"
43
import {GlobalFifoPromise} from "./GlobalFifoPromise"
54
import {BlockchainContext} from "../lib/computation/BlockchainContext"
65
import {ConfDTO} from "../lib/dto/ConfDTO"
76
import {FileDAL} from "../lib/dal/fileDAL"
87
import {QuickSynchronizer} from "../lib/computation/QuickSync"
98
import {BlockDTO} from "../lib/dto/BlockDTO"
10-
import {DBIdentity} from "../lib/dal/sqliteDAL/IdentityDAL"
119
import {DBBlock} from "../lib/db/DBBlock"
1210
import {GLOBAL_RULES_HELPERS} from "../lib/rules/global_rules"
1311
import {parsers} from "../lib/common-libs/parsers/index"
@@ -18,6 +16,8 @@ import {LOCAL_RULES_FUNCTIONS} from "../lib/rules/local_rules"
1816
import {Switcher, SwitcherDao} from "../lib/blockchain/Switcher"
1917
import {OtherConstants} from "../lib/other_constants"
2018

19+
"use strict";
20+
2121
const _ = require('underscore');
2222
const constants = require('../lib/constants');
2323

@@ -206,6 +206,11 @@ export class BlockchainService extends FIFOService {
206206
while (!added && i < potentials.length) {
207207
const dto = BlockDTO.fromJSONObject(potentials[i])
208208
try {
209+
if (dto.issuer === this.conf.pair.pub) {
210+
for (const tx of dto.transactions) {
211+
await this.dal.removeTxByHash(tx.hash);
212+
}
213+
}
209214
const addedBlock = await this.mainContext.checkAndAddBlock(dto)
210215
added = true
211216
this.push({

app/service/TransactionsService.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {FIFOService} from "./FIFOService";
99
import {GlobalFifoPromise} from "./GlobalFifoPromise";
1010

1111
const constants = require('../lib/constants');
12-
const CHECK_PENDING_TRANSACTIONS = true
1312

1413
export class TransactionService extends FIFOService {
1514

@@ -43,7 +42,7 @@ export class TransactionService extends FIFOService {
4342
const dto = TransactionDTO.fromJSONObject(tx)
4443
await LOCAL_RULES_HELPERS.checkSingleTransactionLocally(dto, this.conf)
4544
await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal);
46-
await GLOBAL_RULES_HELPERS.checkSingleTransaction(dto, nextBlockWithFakeTimeVariation, this.conf, this.dal, CHECK_PENDING_TRANSACTIONS);
45+
await GLOBAL_RULES_HELPERS.checkSingleTransaction(dto, nextBlockWithFakeTimeVariation, this.conf, this.dal, this.dal.getTxByHash.bind(this.dal));
4746
const server_pubkey = this.conf.pair && this.conf.pair.pub;
4847
if (!(await this.dal.txsDAL.sandbox.acceptNewSandBoxEntry({
4948
issuers: tx.issuers,

0 commit comments

Comments
 (0)