Separate Hash into TxHash and AckHash

This commit is contained in:
Mark Spanbroek 2021-07-08 11:21:30 +02:00
parent 07fbd31a65
commit 67e515120c
7 changed files with 35 additions and 32 deletions

View File

@ -7,13 +7,13 @@ export keys
type
Ack* = object
previous: ?Hash
transactions: seq[Hash]
previous: ?AckHash
transactions: seq[TxHash]
validator: PublicKey
signature: ?Signature
func init*(_: type Ack,
transactions: openArray[Hash],
transactions: openArray[TxHash],
validator: PublicKey): ?Ack =
if transactions.len == 0:
return none Ack
@ -21,8 +21,8 @@ func init*(_: type Ack,
some Ack(transactions: @transactions, validator: validator)
func init*(_: type Ack,
previous: Hash,
transactions: openArray[Hash],
previous: AckHash,
transactions: openArray[TxHash],
validator: PublicKey): ?Ack =
without var ack =? Ack.init(transactions, validator):
return none Ack
@ -30,10 +30,10 @@ func init*(_: type Ack,
ack.previous = previous.some
some ack
func previous*(ack: Ack): ?Hash =
func previous*(ack: Ack): ?AckHash =
ack.previous
func transactions*(ack: Ack): seq[Hash] =
func transactions*(ack: Ack): seq[TxHash] =
ack.transactions
func validator*(ack: Ack): PublicKey =
@ -46,15 +46,15 @@ func `signature=`*(ack: var Ack, signature: Signature) =
ack.signature = signature.some
func toBytes*(ack: Ack): seq[byte] =
let previous = ack.previous |? Hash.default
let previous = ack.previous |? AckHash.default
result.add(previous.toBytes)
result.add(ack.transactions.len.uint8)
for transaction in ack.transactions:
result.add(transaction.toBytes)
result.add(ack.validator.toBytes)
func hash*(ack: Ack): Hash =
hash(ack.toBytes)
func hash*(ack: Ack): AckHash =
AckHash.hash(ack.toBytes)
func sign*(key: PrivateKey, ack: var Ack) =
ack.signature = key.sign(ack.hash.toBytes).some

View File

@ -1,17 +1,20 @@
import std/hashes except Hash
import std/hashes
import pkg/nimcrypto
type
Hash* = distinct MDigest[256]
TxHash* = distinct MDigest[256]
AckHash* = distinct MDigest[256]
func `==`*(a, b: Hash): bool {.borrow.}
func `$`*(h: Hash): string {.borrow.}
func `==`*(a, b: TxHash): bool {.borrow.}
func `==`*(a, b: AckHash): bool {.borrow.}
func `$`*(h: TxHash): string {.borrow.}
func `$`*(h: AckHash): string {.borrow.}
func hash*(bytes: openArray[byte]): Hash =
Hash(sha256.digest(bytes))
func hash*[H: TxHash|AckHash](_: type H, bytes: openArray[byte]): H =
H(sha256.digest(bytes))
func toBytes*(hash: Hash): array[32, byte] =
func toBytes*(hash: TxHash|AckHash): array[32, byte] =
MDigest[256](hash).data
func hash*(hash: Hash): hashes.Hash =
func hash*(hash: TxHash|AckHash): Hash =
hashes.hash(hash.toBytes)

View File

@ -1,11 +1,11 @@
import ./txstore
func past*(store: TxStore, txHash: Hash, accumulator: var seq[Hash]) =
func past*(store: TxStore, txHash: TxHash, accumulator: var seq[TxHash]) =
if transaction =? store.getTx(txHash):
for (hash, _) in transaction.inputs:
if not accumulator.contains hash:
accumulator.add(hash)
store.past(hash, accumulator)
func past*(store: TxStore, txHash: Hash): seq[Hash] =
func past*(store: TxStore, txHash: TxHash): seq[TxHash] =
store.past(txHash, result)

View File

@ -18,7 +18,7 @@ type
validator: PublicKey
signature: Signature
TxInput* = tuple
transaction: Hash
transaction: TxHash
owner: PublicKey
TxOutput* = tuple
owner: PublicKey
@ -67,8 +67,8 @@ func toBytes*(transaction: Transaction): seq[byte] =
result.add(value.toBytes)
result.add(transaction.validator.toBytes)
func hash*(transaction: Transaction): Hash =
hash(transaction.toBytes)
func hash*(transaction: Transaction): TxHash =
TxHash.hash(transaction.toBytes)
func sign*(key: PrivateKey, transaction: var Transaction) =
transaction.add(key.sign(transaction.hash.toBytes))

View File

@ -5,9 +5,9 @@ import ./acks
type
TxStore* = object
genesis: Hash
transactions: Table[Hash, Transaction]
acks: Table[Hash, Ack]
genesis: TxHash
transactions: Table[TxHash, Transaction]
acks: Table[AckHash, Ack]
export questionable
export transactions
@ -25,11 +25,11 @@ func init*(_: type TxStore, genesis: Transaction): TxStore =
result.genesis = genesis.hash
result.add(genesis)
func genesis*(store: TxStore): Hash =
func genesis*(store: TxStore): TxHash =
store.genesis
func getTx*(store: TxStore, hash: Hash): ?Transaction =
func getTx*(store: TxStore, hash: TxHash): ?Transaction =
store.transactions.?[hash]
func getAck*(store: TxStore, hash: Hash): ?Ack =
func getAck*(store: TxStore, hash: AckHash): ?Ack =
store.acks.?[hash]

View File

@ -15,7 +15,7 @@ func checkValue(store: TxStore, transaction: Transaction): bool =
valueIn == valueOut
func hasValidTx*(store: TxStore, txHash: Hash): bool =
func hasValidTx*(store: TxStore, txHash: TxHash): bool =
if txHash == store.genesis:
return true
@ -31,7 +31,7 @@ func hasValidTx*(store: TxStore, txHash: Hash): bool =
store.checkValue(transaction)
func hasValidAck*(store: TxStore, ackHash: Hash): bool =
func hasValidAck*(store: TxStore, ackHash: AckHash): bool =
without ack =? store.getAck(ackHash):
return false

View File

@ -11,7 +11,7 @@ suite "Acknowledgements":
let ack = Ack.init([tx1.hash, tx2.hash], victor)
check ack.isSome
check ack.?transactions == @[tx1.hash, tx2.hash].some
check ack.?previous == Hash.none
check ack.?previous == AckHash.none
check ack.?validator == victor.some
test "an acknowledgement has a hash":