From cdaf1b22faaa85a82e3f85609369433408c57504 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 8 Jul 2021 11:43:33 +0200 Subject: [PATCH] Find all transactions that precede an acknowledgement --- abc/past.nim | 15 ++++++++++++--- tests/abc/testPast.nim | 12 +++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/abc/past.nim b/abc/past.nim index eaa8982..78d8422 100644 --- a/abc/past.nim +++ b/abc/past.nim @@ -1,11 +1,20 @@ import ./txstore -func past*(store: TxStore, txHash: TxHash, accumulator: var seq[TxHash]) = +func past(store: TxStore, txHash: TxHash, accumulator: var seq[TxHash]) = if transaction =? store[txHash]: for (hash, _) in transaction.inputs: if not accumulator.contains hash: accumulator.add(hash) store.past(hash, accumulator) -func past*(store: TxStore, txHash: TxHash): seq[TxHash] = - store.past(txHash, result) +func past(store: TxStore, ackHash: AckHash, accumulator: var seq[TxHash]) = + if ack =? store[ackHash]: + if previous =? ack.previous: + store.past(previous, accumulator) + for txHash in ack.transactions: + if not accumulator.contains txHash: + accumulator.add(txHash) + store.past(txHash, accumulator) + +func past*(store: TxStore, hash: TxHash|AckHash): seq[TxHash] = + store.past(hash, result) diff --git a/tests/abc/testPast.nim b/tests/abc/testPast.nim index c40d85b..0dcc15b 100644 --- a/tests/abc/testPast.nim +++ b/tests/abc/testPast.nim @@ -3,13 +3,14 @@ import abc/past import ./basics import ./alicebob -suite "Past": +suite "Past transactions and acknowledgements": let genesis = Transaction.genesis let alice = PublicKey.alice let bob = PublicKey.bob let victor = PublicKey.victor var tx1, tx2, tx3: Transaction + var ack1, ack2: Ack setup: tx1 = !Transaction.init({genesis.hash: alice}, {bob: 100.u256}, victor) @@ -19,6 +20,8 @@ suite "Past": {alice: 200.u256}, victor ) + ack1 = !Ack.init([tx1.hash, tx2.hash], victor) + ack2 = !Ack.init(ack1.hash, [tx3.hash], victor) test "finds all transactions that precede a transaction": var store = TxStore.init(genesis) @@ -30,3 +33,10 @@ suite "Past": test "past is empty when transaction cannot be found": let store = TxStore.init(genesis) check store.past(tx1.hash) == [] + + test "finds all transactions that precede an acknowledgement": + var store = TxStore.init(genesis) + store.add(tx1, tx2, tx3) + store.add(ack1, ack2) + check store.past(ack1.hash) == [tx1.hash, genesis.hash, tx2.hash] + check store.past(ack2.hash) == [tx1.hash, genesis.hash, tx2.hash, tx3.hash]