From 080f32dda9058697035a0724f1741e0f40831bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Moreau?= <cem.moreau@gmail.com> Date: Mon, 8 Jan 2018 21:13:40 +0100 Subject: [PATCH] [fix] #1248 Could not have chained txs in a block --- app/lib/indexer.ts | 44 ++++++++++++++++++++++---------------------- doc/Protocol.md | 22 ++++++++++++++++++---- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts index ddb1cfa99..b5f1c47be 100644 --- a/app/lib/indexer.ts +++ b/app/lib/indexer.ts @@ -865,21 +865,12 @@ export class Indexer { } })) - // BR_G46 - await Promise.all(_.where(sindex, { op: constants.IDX_UPDATE }).map(async (ENTRY: SindexEntry) => { - const reducable = await dal.sindexDAL.sqlFind({ - identifier: ENTRY.identifier, - pos: ENTRY.pos, - amount: ENTRY.amount, - base: ENTRY.base - }); - ENTRY.conditions = reduce(reducable).conditions; // We valuate the input conditions, so we can map these records to a same account - ENTRY.available = reduce(reducable).consumed === false; - })) - - // BR_G47 - await Promise.all(_.where(sindex, { op: constants.IDX_UPDATE }).map(async (ENTRY: SindexEntry) => { - let source = _.filter(sindex, (src:SindexEntry) => src.identifier == ENTRY.identifier && src.pos == ENTRY.pos && src.conditions && src.op === constants.IDX_CREATE)[0]; + const getInputLocalFirstOrFallbackGlobally = async (sindex:SindexEntry[], ENTRY:SindexEntry) => { + let source = _.filter(sindex, (src:SindexEntry) => + src.identifier == ENTRY.identifier + && src.pos == ENTRY.pos + && src.conditions + && src.op === constants.IDX_CREATE)[0]; if (!source) { const reducable = await dal.sindexDAL.sqlFind({ identifier: ENTRY.identifier, @@ -887,20 +878,29 @@ export class Indexer { amount: ENTRY.amount, base: ENTRY.base }); - source = reduce(reducable); + source = reduce(reducable) } + return source + } + + // BR_G46 + await Promise.all(_.where(sindex, { op: constants.IDX_UPDATE }).map(async (ENTRY: SindexEntry) => { + const source = await getInputLocalFirstOrFallbackGlobally(sindex, ENTRY) + ENTRY.conditions = source.conditions; // We valuate the input conditions, so we can map these records to a same account + ENTRY.available = source.consumed === false; + })) + + // BR_G47 + await Promise.all(_.where(sindex, { op: constants.IDX_UPDATE }).map(async (ENTRY: SindexEntry) => { + const source = await getInputLocalFirstOrFallbackGlobally(sindex, ENTRY) ENTRY.conditions = source.conditions; ENTRY.isLocked = !txSourceUnlock(ENTRY, source, HEAD); })) // BR_G48 await Promise.all(_.where(sindex, { op: constants.IDX_UPDATE }).map(async (ENTRY: SindexEntry) => { - ENTRY.isTimeLocked = ENTRY.written_time - reduce(await dal.sindexDAL.sqlFind({ - identifier: ENTRY.identifier, - pos: ENTRY.pos, - amount: ENTRY.amount, - base: ENTRY.base - })).written_time < ENTRY.locktime; + const source = await getInputLocalFirstOrFallbackGlobally(sindex, ENTRY) + ENTRY.isTimeLocked = ENTRY.written_time - source.written_time < ENTRY.locktime; })) return HEAD; diff --git a/doc/Protocol.md b/doc/Protocol.md index b657553f4..bc424f2d7 100644 --- a/doc/Protocol.md +++ b/doc/Protocol.md @@ -2244,7 +2244,7 @@ Else: ####### BR_G102 - ENTRY.age -For each ENTRY in local IINDEX where `op = 'UPDATE'`: +For each ENTRY in local SINDEX where `op = 'UPDATE'`: REF_BLOCK = HEAD~<HEAD~1.number + 1 - NUMBER(ENTRY.hash)>[hash=HASH(ENTRY.created_on)] @@ -2266,17 +2266,31 @@ EndIf For each `LOCAL_SINDEX[op='UPDATE'] as ENTRY`: - INPUT = REDUCE(GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base]) + INPUT_ENTRIES = LOCAL_SINDEX[op='CREATE',identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + If COUNT(INPUT_ENTRIES) == 0 Then + INPUT_ENTRIES = GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + EndIf + INPUT = REDUCE(INPUT_ENTRIES) ENTRY.conditions = INPUT.conditions ENTRY.available = INPUT.consumed == false ####### BR_G47 - ENTRY.isLocked - ENTRY.isLocked = TX_SOURCE_UNLOCK(REDUCE(GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base]).conditions, ENTRY) + INPUT_ENTRIES = LOCAL_SINDEX[op='CREATE',identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + If COUNT(INPUT_ENTRIES) == 0 Then + INPUT_ENTRIES = GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + EndIf + INPUT = REDUCE(INPUT_ENTRIES) + ENTRY.isLocked = TX_SOURCE_UNLOCK(INPUT.conditions, ENTRY) ####### BR_G48 - ENTRY.isTimeLocked - ENTRY.isTimeLocked = ENTRY.written_time - REDUCE(GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base]).written_time < ENTRY.locktime + INPUT_ENTRIES = LOCAL_SINDEX[op='CREATE',identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + If COUNT(INPUT_ENTRIES) == 0 Then + INPUT_ENTRIES = GLOBAL_SINDEX[identifier=ENTRY.identifier,pos=ENTRY.pos,amount=ENTRY.amount,base=ENTRY.base] + EndIf + INPUT = REDUCE(INPUT_ENTRIES) + ENTRY.isTimeLocked = ENTRY.written_time - INPUT.written_time < ENTRY.locktime ##### Rules -- GitLab