Rename TxHash to Hash and move into its own module

Reason: we'll need hashes for more than just transactions
This commit is contained in:
Mark Spanbroek 2021-06-30 17:09:01 +02:00
parent 2b51646c8f
commit 1db30e9ee7
4 changed files with 30 additions and 22 deletions

16
abc/hash.nim Normal file
View File

@ -0,0 +1,16 @@
import std/hashes except Hash
import pkg/nimcrypto
type
Hash* = distinct MDigest[256]
func `==`*(a, b: Hash): bool {.borrow.}
func hash*(bytes: openArray[byte]): Hash =
Hash(sha256.digest(bytes))
func toBytes*(hash: Hash): array[32, byte] =
MDigest[256](hash).data
func hash*(hash: Hash): hashes.Hash =
hashes.hash(hash.toBytes)

View File

@ -5,9 +5,11 @@ import pkg/stint
import pkg/questionable
import ./keys
import ./helpers
import ./hash
export stint
export keys
export hash
type
Transaction* = object
@ -16,14 +18,11 @@ type
validator: PublicKey
signature: Signature
TxInput* = tuple
txHash: TxHash
transaction: Hash
owner: PublicKey
TxOutput* = tuple
owner: PublicKey
value: UInt256
TxHash* = distinct MDigest[256]
func `==`*(a, b: TxHash): bool {.borrow.}
func init*(_: type Transaction,
inputs: openArray[TxInput],
@ -57,9 +56,6 @@ func validator*(transaction: Transaction): PublicKey =
func add*(transaction: var Transaction, signature: Signature) =
transaction.signature = aggregate(transaction.signature, signature)
func toBytes*(hash: TxHash): array[32, byte] =
MDigest[256](hash).data
func toBytes*(transaction: Transaction): seq[byte] =
result.add(transaction.inputs.len.uint8)
for (txHash, owner) in transaction.inputs:
@ -71,8 +67,8 @@ func toBytes*(transaction: Transaction): seq[byte] =
result.add(value.toBytes)
result.add(transaction.validator.toBytes)
func hash*(transaction: Transaction): TxHash =
TxHash(sha256.digest(transaction.toBytes))
func hash*(transaction: Transaction): Hash =
hash(transaction.toBytes)
func sign*(key: PrivateKey, transaction: var Transaction) =
transaction.add(key.sign(transaction.hash.toBytes))

View File

@ -1,17 +1,13 @@
import std/tables
import std/hashes
import ./transactions
type
TxStore* = object
genesis: TxHash
transactions: Table[TxHash, Transaction]
genesis: Hash
transactions: Table[Hash, Transaction]
export transactions
func hash(h: TxHash): Hash =
h.toBytes.hash
func add*(store: var TxStore, transactions: varargs[Transaction]) =
for transaction in transactions:
store.transactions[transaction.hash] = transaction
@ -20,11 +16,11 @@ func init*(_: type TxStore, genesis: Transaction): TxStore =
result.genesis = genesis.hash
result.add(genesis)
func genesis*(store: TxStore): TxHash =
func genesis*(store: TxStore): Hash =
store.genesis
func hasTx*(store: TxStore, hash: TxHash): bool =
func hasTx*(store: TxStore, hash: Hash): bool =
store.transactions.hasKey(hash)
func `[]`*(store: TxStore, hash: TxHash): Transaction =
func `[]`*(store: TxStore, hash: Hash): Transaction =
store.transactions[hash]

View File

@ -13,14 +13,14 @@ func checkValue(store: TxStore, transaction: Transaction): bool =
valueIn == valueOut
func hasValidTx*(store: TxStore, hash: TxHash): bool =
if hash == store.genesis:
func hasValidTx*(store: TxStore, txHash: Hash): bool =
if txHash == store.genesis:
return true
if not store.hasTx(hash):
if not store.hasTx(txHash):
return false
let transaction = store[hash]
let transaction = store[txHash]
if not transaction.hasValidSignature:
return false