mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-03 22:13:11 +00:00
lazy calculated transaction hash
This commit is contained in:
parent
135e8efbc6
commit
179f6e5564
@ -1,7 +1,10 @@
|
||||
import ./transaction/transaction
|
||||
import ./transaction/signed
|
||||
import ./transaction/serialization
|
||||
import ./transaction/hashing
|
||||
import ./transaction/signed
|
||||
|
||||
export transaction
|
||||
export signed
|
||||
export transaction except hash
|
||||
export serialization.toBytes
|
||||
export hashing.hash
|
||||
export hashing.toBytes
|
||||
export signed
|
||||
|
||||
12
codexvalidator/transaction/hashing.nim
Normal file
12
codexvalidator/transaction/hashing.nim
Normal file
@ -0,0 +1,12 @@
|
||||
import ../basics
|
||||
import ../hashing
|
||||
import ./transaction
|
||||
import ./serialization
|
||||
|
||||
export hashing.toBytes
|
||||
|
||||
func hash*(tx: Transaction): Hash =
|
||||
without var hash =? transaction.hash(tx):
|
||||
hash = Hash.hash(tx.toBytes())
|
||||
tx.hash = hash
|
||||
hash
|
||||
@ -1,6 +1,6 @@
|
||||
import ../signatures
|
||||
import ./transaction
|
||||
import ./serialization
|
||||
import ./hashing
|
||||
|
||||
type SignedTransaction* = object
|
||||
transaction: Transaction
|
||||
@ -20,7 +20,8 @@ func init*(
|
||||
)
|
||||
|
||||
func sign*(identity: Identity, transaction: Transaction): SignedTransaction =
|
||||
let signature = identity.sign(transaction.toBytes())
|
||||
let hash = hashing.hash(transaction)
|
||||
let signature = identity.sign(hash.toBytes())
|
||||
SignedTransaction.init(transaction, identity.identifier, signature)
|
||||
|
||||
func transaction*(signed: SignedTransaction): Transaction =
|
||||
@ -33,4 +34,5 @@ func signature*(signed: SignedTransaction): Signature =
|
||||
signed.signature
|
||||
|
||||
func verifySignature*(signed: SignedTransaction): bool =
|
||||
signed.signer.verify(signed.transaction.toBytes(), signed.signature)
|
||||
let hash = hashing.hash(signed.transaction)
|
||||
signed.signer.verify(hash.toBytes(), signed.signature)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import ../basics
|
||||
import ../hashing
|
||||
import ./storagerequest
|
||||
import ./period
|
||||
import ./groth16
|
||||
@ -13,7 +14,7 @@ type
|
||||
TransactionKind* {.pure.} = enum
|
||||
storageProof
|
||||
missingProof
|
||||
Transaction* = object
|
||||
Transaction* = ref object
|
||||
requestId: StorageRequestId
|
||||
slotIndex: uint32
|
||||
period: Period
|
||||
@ -24,6 +25,7 @@ type
|
||||
proof: Groth16Proof
|
||||
of missingProof:
|
||||
discard
|
||||
hash: ?Hash
|
||||
|
||||
func storageProof*(
|
||||
_: type Transaction,
|
||||
@ -85,6 +87,12 @@ func challenge*(transaction: Transaction): array[32, byte] =
|
||||
func proof*(transaction: Transaction): Groth16Proof =
|
||||
transaction.proof
|
||||
|
||||
func `hash=`*(transaction: Transaction, hash: Hash) =
|
||||
transaction.hash = some hash
|
||||
|
||||
func hash*(transaction: Transaction): ?Hash =
|
||||
transaction.hash
|
||||
|
||||
func `==`*(a, b: Transaction): bool =
|
||||
if a.kind != b.kind:
|
||||
return false
|
||||
|
||||
9
tests/codexvalidator/transaction/testHashing.nim
Normal file
9
tests/codexvalidator/transaction/testHashing.nim
Normal file
@ -0,0 +1,9 @@
|
||||
import ../basics
|
||||
import codexvalidator/transaction
|
||||
import codexvalidator/hashing
|
||||
|
||||
suite "Transaction hashing":
|
||||
|
||||
test "transactions have a hash derived from the serialized bytes":
|
||||
let transaction = Transaction.example
|
||||
check transaction.hash == Hash.hash(transaction.toBytes())
|
||||
@ -1,6 +1,7 @@
|
||||
import ../basics
|
||||
import codexvalidator/signatures
|
||||
import codexvalidator/transaction
|
||||
import codexvalidator/hashing
|
||||
|
||||
suite "Transaction signing":
|
||||
|
||||
@ -10,7 +11,7 @@ suite "Transaction signing":
|
||||
let signed = identity.sign(transaction)
|
||||
check signed.transaction == transaction
|
||||
check signed.signer == identity.identifier
|
||||
check signed.signature == identity.sign(transaction.toBytes())
|
||||
check signed.signature == identity.sign(transaction.hash.toBytes())
|
||||
|
||||
test "transaction signature can be verified":
|
||||
let identity = Identity.example
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import ./codexvalidator/testSignatures
|
||||
import ./codexvalidator/transaction/testTransaction
|
||||
import ./codexvalidator/transaction/testSigning
|
||||
import ./codexvalidator/transaction/testSerialization
|
||||
import ./codexvalidator/transaction/testHashing
|
||||
import ./codexvalidator/transaction/testSigning
|
||||
import ./codexvalidator/blocks/testBlock
|
||||
import ./codexvalidator/blocks/testSerialization
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user