Store Acks in TxStore

This commit is contained in:
Mark Spanbroek 2021-07-05 11:54:45 +02:00
parent 64018dc97b
commit f7435a5d3b
3 changed files with 26 additions and 5 deletions

View File

@ -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]

View File

@ -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

View File

@ -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