mirror of https://github.com/status-im/nim-abc.git
Separate Hash into TxHash and AckHash
This commit is contained in:
parent
07fbd31a65
commit
67e515120c
20
abc/acks.nim
20
abc/acks.nim
|
@ -7,13 +7,13 @@ export keys
|
||||||
|
|
||||||
type
|
type
|
||||||
Ack* = object
|
Ack* = object
|
||||||
previous: ?Hash
|
previous: ?AckHash
|
||||||
transactions: seq[Hash]
|
transactions: seq[TxHash]
|
||||||
validator: PublicKey
|
validator: PublicKey
|
||||||
signature: ?Signature
|
signature: ?Signature
|
||||||
|
|
||||||
func init*(_: type Ack,
|
func init*(_: type Ack,
|
||||||
transactions: openArray[Hash],
|
transactions: openArray[TxHash],
|
||||||
validator: PublicKey): ?Ack =
|
validator: PublicKey): ?Ack =
|
||||||
if transactions.len == 0:
|
if transactions.len == 0:
|
||||||
return none Ack
|
return none Ack
|
||||||
|
@ -21,8 +21,8 @@ func init*(_: type Ack,
|
||||||
some Ack(transactions: @transactions, validator: validator)
|
some Ack(transactions: @transactions, validator: validator)
|
||||||
|
|
||||||
func init*(_: type Ack,
|
func init*(_: type Ack,
|
||||||
previous: Hash,
|
previous: AckHash,
|
||||||
transactions: openArray[Hash],
|
transactions: openArray[TxHash],
|
||||||
validator: PublicKey): ?Ack =
|
validator: PublicKey): ?Ack =
|
||||||
without var ack =? Ack.init(transactions, validator):
|
without var ack =? Ack.init(transactions, validator):
|
||||||
return none Ack
|
return none Ack
|
||||||
|
@ -30,10 +30,10 @@ func init*(_: type Ack,
|
||||||
ack.previous = previous.some
|
ack.previous = previous.some
|
||||||
some ack
|
some ack
|
||||||
|
|
||||||
func previous*(ack: Ack): ?Hash =
|
func previous*(ack: Ack): ?AckHash =
|
||||||
ack.previous
|
ack.previous
|
||||||
|
|
||||||
func transactions*(ack: Ack): seq[Hash] =
|
func transactions*(ack: Ack): seq[TxHash] =
|
||||||
ack.transactions
|
ack.transactions
|
||||||
|
|
||||||
func validator*(ack: Ack): PublicKey =
|
func validator*(ack: Ack): PublicKey =
|
||||||
|
@ -46,15 +46,15 @@ func `signature=`*(ack: var Ack, signature: Signature) =
|
||||||
ack.signature = signature.some
|
ack.signature = signature.some
|
||||||
|
|
||||||
func toBytes*(ack: Ack): seq[byte] =
|
func toBytes*(ack: Ack): seq[byte] =
|
||||||
let previous = ack.previous |? Hash.default
|
let previous = ack.previous |? AckHash.default
|
||||||
result.add(previous.toBytes)
|
result.add(previous.toBytes)
|
||||||
result.add(ack.transactions.len.uint8)
|
result.add(ack.transactions.len.uint8)
|
||||||
for transaction in ack.transactions:
|
for transaction in ack.transactions:
|
||||||
result.add(transaction.toBytes)
|
result.add(transaction.toBytes)
|
||||||
result.add(ack.validator.toBytes)
|
result.add(ack.validator.toBytes)
|
||||||
|
|
||||||
func hash*(ack: Ack): Hash =
|
func hash*(ack: Ack): AckHash =
|
||||||
hash(ack.toBytes)
|
AckHash.hash(ack.toBytes)
|
||||||
|
|
||||||
func sign*(key: PrivateKey, ack: var Ack) =
|
func sign*(key: PrivateKey, ack: var Ack) =
|
||||||
ack.signature = key.sign(ack.hash.toBytes).some
|
ack.signature = key.sign(ack.hash.toBytes).some
|
||||||
|
|
19
abc/hash.nim
19
abc/hash.nim
|
@ -1,17 +1,20 @@
|
||||||
import std/hashes except Hash
|
import std/hashes
|
||||||
import pkg/nimcrypto
|
import pkg/nimcrypto
|
||||||
|
|
||||||
type
|
type
|
||||||
Hash* = distinct MDigest[256]
|
TxHash* = distinct MDigest[256]
|
||||||
|
AckHash* = distinct MDigest[256]
|
||||||
|
|
||||||
func `==`*(a, b: Hash): bool {.borrow.}
|
func `==`*(a, b: TxHash): bool {.borrow.}
|
||||||
func `$`*(h: Hash): string {.borrow.}
|
func `==`*(a, b: AckHash): bool {.borrow.}
|
||||||
|
func `$`*(h: TxHash): string {.borrow.}
|
||||||
|
func `$`*(h: AckHash): string {.borrow.}
|
||||||
|
|
||||||
func hash*(bytes: openArray[byte]): Hash =
|
func hash*[H: TxHash|AckHash](_: type H, bytes: openArray[byte]): H =
|
||||||
Hash(sha256.digest(bytes))
|
H(sha256.digest(bytes))
|
||||||
|
|
||||||
func toBytes*(hash: Hash): array[32, byte] =
|
func toBytes*(hash: TxHash|AckHash): array[32, byte] =
|
||||||
MDigest[256](hash).data
|
MDigest[256](hash).data
|
||||||
|
|
||||||
func hash*(hash: Hash): hashes.Hash =
|
func hash*(hash: TxHash|AckHash): Hash =
|
||||||
hashes.hash(hash.toBytes)
|
hashes.hash(hash.toBytes)
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import ./txstore
|
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):
|
if transaction =? store.getTx(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: Hash): seq[Hash] =
|
func past*(store: TxStore, txHash: TxHash): seq[TxHash] =
|
||||||
store.past(txHash, result)
|
store.past(txHash, result)
|
||||||
|
|
|
@ -18,7 +18,7 @@ type
|
||||||
validator: PublicKey
|
validator: PublicKey
|
||||||
signature: Signature
|
signature: Signature
|
||||||
TxInput* = tuple
|
TxInput* = tuple
|
||||||
transaction: Hash
|
transaction: TxHash
|
||||||
owner: PublicKey
|
owner: PublicKey
|
||||||
TxOutput* = tuple
|
TxOutput* = tuple
|
||||||
owner: PublicKey
|
owner: PublicKey
|
||||||
|
@ -67,8 +67,8 @@ func toBytes*(transaction: Transaction): seq[byte] =
|
||||||
result.add(value.toBytes)
|
result.add(value.toBytes)
|
||||||
result.add(transaction.validator.toBytes)
|
result.add(transaction.validator.toBytes)
|
||||||
|
|
||||||
func hash*(transaction: Transaction): Hash =
|
func hash*(transaction: Transaction): TxHash =
|
||||||
hash(transaction.toBytes)
|
TxHash.hash(transaction.toBytes)
|
||||||
|
|
||||||
func sign*(key: PrivateKey, transaction: var Transaction) =
|
func sign*(key: PrivateKey, transaction: var Transaction) =
|
||||||
transaction.add(key.sign(transaction.hash.toBytes))
|
transaction.add(key.sign(transaction.hash.toBytes))
|
||||||
|
|
|
@ -5,9 +5,9 @@ import ./acks
|
||||||
|
|
||||||
type
|
type
|
||||||
TxStore* = object
|
TxStore* = object
|
||||||
genesis: Hash
|
genesis: TxHash
|
||||||
transactions: Table[Hash, Transaction]
|
transactions: Table[TxHash, Transaction]
|
||||||
acks: Table[Hash, Ack]
|
acks: Table[AckHash, Ack]
|
||||||
|
|
||||||
export questionable
|
export questionable
|
||||||
export transactions
|
export transactions
|
||||||
|
@ -25,11 +25,11 @@ func init*(_: type TxStore, genesis: Transaction): TxStore =
|
||||||
result.genesis = genesis.hash
|
result.genesis = genesis.hash
|
||||||
result.add(genesis)
|
result.add(genesis)
|
||||||
|
|
||||||
func genesis*(store: TxStore): Hash =
|
func genesis*(store: TxStore): TxHash =
|
||||||
store.genesis
|
store.genesis
|
||||||
|
|
||||||
func getTx*(store: TxStore, hash: Hash): ?Transaction =
|
func getTx*(store: TxStore, hash: TxHash): ?Transaction =
|
||||||
store.transactions.?[hash]
|
store.transactions.?[hash]
|
||||||
|
|
||||||
func getAck*(store: TxStore, hash: Hash): ?Ack =
|
func getAck*(store: TxStore, hash: AckHash): ?Ack =
|
||||||
store.acks.?[hash]
|
store.acks.?[hash]
|
||||||
|
|
|
@ -15,7 +15,7 @@ func checkValue(store: TxStore, transaction: Transaction): bool =
|
||||||
|
|
||||||
valueIn == valueOut
|
valueIn == valueOut
|
||||||
|
|
||||||
func hasValidTx*(store: TxStore, txHash: Hash): bool =
|
func hasValidTx*(store: TxStore, txHash: TxHash): bool =
|
||||||
if txHash == store.genesis:
|
if txHash == store.genesis:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ func hasValidTx*(store: TxStore, txHash: Hash): bool =
|
||||||
|
|
||||||
store.checkValue(transaction)
|
store.checkValue(transaction)
|
||||||
|
|
||||||
func hasValidAck*(store: TxStore, ackHash: Hash): bool =
|
func hasValidAck*(store: TxStore, ackHash: AckHash): bool =
|
||||||
without ack =? store.getAck(ackHash):
|
without ack =? store.getAck(ackHash):
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ suite "Acknowledgements":
|
||||||
let ack = Ack.init([tx1.hash, tx2.hash], victor)
|
let ack = Ack.init([tx1.hash, tx2.hash], victor)
|
||||||
check ack.isSome
|
check ack.isSome
|
||||||
check ack.?transactions == @[tx1.hash, tx2.hash].some
|
check ack.?transactions == @[tx1.hash, tx2.hash].some
|
||||||
check ack.?previous == Hash.none
|
check ack.?previous == AckHash.none
|
||||||
check ack.?validator == victor.some
|
check ack.?validator == victor.some
|
||||||
|
|
||||||
test "an acknowledgement has a hash":
|
test "an acknowledgement has a hash":
|
||||||
|
|
Loading…
Reference in New Issue