From f7435a5d3b8b94483e893df500b25de5061772ab Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 5 Jul 2021 11:54:45 +0200 Subject: [PATCH] Store Acks in TxStore --- abc/txstore.nim | 15 ++++++++++++++- abc/txvalidation.nim | 4 ++-- tests/abc/testTxStore.nim | 12 ++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/abc/txstore.nim b/abc/txstore.nim index 317f0ff..977274e 100644 --- a/abc/txstore.nim +++ b/abc/txstore.nim @@ -1,17 +1,24 @@ import std/tables import ./transactions +import ./acks type TxStore* = object genesis: Hash transactions: Table[Hash, Transaction] + acks: Table[Hash, Ack] export transactions +export acks func add*(store: var TxStore, transactions: varargs[Transaction]) = for transaction in transactions: store.transactions[transaction.hash] = transaction +func add*(store: var TxStore, acks: varargs[Ack]) = + for ack in acks: + store.acks[ack.hash] = ack + func init*(_: type TxStore, genesis: Transaction): TxStore = result.genesis = genesis.hash result.add(genesis) @@ -22,5 +29,11 @@ func genesis*(store: TxStore): Hash = func hasTx*(store: TxStore, hash: Hash): bool = store.transactions.hasKey(hash) -func `[]`*(store: TxStore, hash: Hash): Transaction = +func hasAck*(store: TxStore, hash: Hash): bool = + store.acks.hasKey(hash) + +func getTx*(store: TxStore, hash: Hash): Transaction = store.transactions[hash] + +func getAck*(store: TxStore, hash: Hash): Ack = + store.acks[hash] diff --git a/abc/txvalidation.nim b/abc/txvalidation.nim index 8a2a3a9..65fa2fb 100644 --- a/abc/txvalidation.nim +++ b/abc/txvalidation.nim @@ -4,7 +4,7 @@ func checkValue(store: TxStore, transaction: Transaction): bool = var valueIn, valueOut = 0.u256 for (hash, owner) in transaction.inputs: - for output in store[hash].outputs: + for output in store.getTx(hash).outputs: if output.owner == owner: valueIn += output.value @@ -20,7 +20,7 @@ func hasValidTx*(store: TxStore, txHash: Hash): bool = if not store.hasTx(txHash): return false - let transaction = store[txHash] + let transaction = store.getTx(txHash) if not transaction.hasValidSignature: return false diff --git a/tests/abc/testTxStore.nim b/tests/abc/testTxStore.nim index e003d1d..6893071 100644 --- a/tests/abc/testTxStore.nim +++ b/tests/abc/testTxStore.nim @@ -6,15 +6,23 @@ suite "Transaction Store": let genesis = Transaction.genesis let transaction = Transaction.example + let ack = Ack.example test "is initialized with a genesis transaction": let store = TxStore.init(genesis) check store.hasTx(genesis.hash) - check store[genesis.hash] == genesis + check store.getTx(genesis.hash) == genesis test "stores transactions": var store = TxStore.init(genesis) check not store.hasTx(transaction.hash) store.add(transaction) check store.hasTx(transaction.hash) - check store[transaction.hash] == transaction + check store.getTx(transaction.hash) == transaction + + test "stores acks": + var store = TxStore.init(genesis) + check not store.hasAck(ack.hash) + store.add(ack) + check store.hasAck(ack.hash) + check store.getAck(ack.hash) == ack