Find all transactions that precede an acknowledgement

This commit is contained in:
Mark Spanbroek 2021-07-08 11:43:33 +02:00
parent 9d79abb6e6
commit cdaf1b22fa
2 changed files with 23 additions and 4 deletions

View File

@ -1,11 +1,20 @@
import ./txstore 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]: if transaction =? store[txHash]:
for (hash, _) in transaction.inputs: for (hash, _) in transaction.inputs:
if not accumulator.contains hash: if not accumulator.contains hash:
accumulator.add(hash) accumulator.add(hash)
store.past(hash, accumulator) store.past(hash, accumulator)
func past*(store: TxStore, txHash: TxHash): seq[TxHash] = func past(store: TxStore, ackHash: AckHash, accumulator: var seq[TxHash]) =
store.past(txHash, result) 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)

View File

@ -3,13 +3,14 @@ import abc/past
import ./basics import ./basics
import ./alicebob import ./alicebob
suite "Past": suite "Past transactions and acknowledgements":
let genesis = Transaction.genesis let genesis = Transaction.genesis
let alice = PublicKey.alice let alice = PublicKey.alice
let bob = PublicKey.bob let bob = PublicKey.bob
let victor = PublicKey.victor let victor = PublicKey.victor
var tx1, tx2, tx3: Transaction var tx1, tx2, tx3: Transaction
var ack1, ack2: Ack
setup: setup:
tx1 = !Transaction.init({genesis.hash: alice}, {bob: 100.u256}, victor) tx1 = !Transaction.init({genesis.hash: alice}, {bob: 100.u256}, victor)
@ -19,6 +20,8 @@ suite "Past":
{alice: 200.u256}, {alice: 200.u256},
victor 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": test "finds all transactions that precede a transaction":
var store = TxStore.init(genesis) var store = TxStore.init(genesis)
@ -30,3 +33,10 @@ suite "Past":
test "past is empty when transaction cannot be found": test "past is empty when transaction cannot be found":
let store = TxStore.init(genesis) let store = TxStore.init(genesis)
check store.past(tx1.hash) == [] 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]