diff --git a/abc/past.nim b/abc/past.nim index bde5f22..93ab7ba 100644 --- a/abc/past.nim +++ b/abc/past.nim @@ -1,24 +1,28 @@ import ./txstore -func past(store: TxStore, txHash: TxHash, accumulator: var seq[TxHash]) = +type + History* = object + transactions*: seq[TxHash] + +func past(store: TxStore, txHash: TxHash, history: var History) = if transaction =? store[txHash]: for (hash, _) in transaction.inputs: - if not accumulator.contains hash: - accumulator.add(hash) - store.past(hash, accumulator) + if not history.transactions.contains hash: + history.transactions.add(hash) + store.past(hash, history) -func past(store: TxStore, ackHash: AckHash, accumulator: var seq[TxHash]) = +func past(store: TxStore, ackHash: AckHash, history: var History) = if ack =? store[ackHash]: if previous =? ack.previous: - store.past(previous, accumulator) + store.past(previous, history) for txHash in ack.transactions: - if not accumulator.contains txHash: - accumulator.add(txHash) - store.past(txHash, accumulator) + if not history.transactions.contains txHash: + history.transactions.add(txHash) + store.past(txHash, history) -func past*(store: TxStore, hash: TxHash|AckHash): seq[TxHash] = +func past*(store: TxStore, hash: TxHash|AckHash): History = store.past(hash, result) -func past*(store: TxStore, hashes: varargs[AckHash]): seq[TxHash] = +func past*(store: TxStore, hashes: varargs[AckHash]): History = for hash in hashes: store.past(hash, result) diff --git a/tests/abc/testPast.nim b/tests/abc/testPast.nim index eef4a3a..0d57e32 100644 --- a/tests/abc/testPast.nim +++ b/tests/abc/testPast.nim @@ -27,24 +27,30 @@ suite "Past transactions and acknowledgements": test "finds all transactions that precede a transaction": var store = TxStore.init(genesis) store.add(tx1, tx2, tx3) - check store.past(tx1.hash) == [genesis.hash] - check store.past(tx2.hash) == [genesis.hash] - check store.past(tx3.hash) == [tx1.hash, genesis.hash, tx2.hash] + check store.past(tx1.hash).transactions == [genesis.hash] + check store.past(tx2.hash).transactions == [genesis.hash] + check store.past(tx3.hash).transactions == [ + tx1.hash, genesis.hash, tx2.hash + ] test "past is empty when transaction cannot be found": let store = TxStore.init(genesis) - check store.past(tx1.hash) == [] + check store.past(tx1.hash).transactions == [] test "finds all transactions that precede an acknowledgement": var store = TxStore.init(genesis) store.add(tx1, tx2, tx3) store.add(ack1, ack2, ack3) - check store.past(ack1.hash) == [tx1.hash, genesis.hash] - check store.past(ack2.hash) == [tx2.hash, genesis.hash] - check store.past(ack3.hash) == [tx1.hash, genesis.hash, tx2.hash, tx3.hash] + check store.past(ack1.hash).transactions == [tx1.hash, genesis.hash] + check store.past(ack2.hash).transactions == [tx2.hash, genesis.hash] + check store.past(ack3.hash).transactions == [ + tx1.hash, genesis.hash, tx2.hash, tx3.hash + ] test "finds all transactions that precede a set of acknowledgements": var store = TxStore.init(genesis) store.add(tx1, tx2, tx3) store.add(ack1, ack2, ack3) - check store.past(ack1.hash, ack2.hash) == [tx1.hash, genesis.hash, tx2.hash] + check store.past(ack1.hash, ack2.hash).transactions == [ + tx1.hash, genesis.hash, tx2.hash + ]