mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-07 16:03: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/transaction
|
||||||
import ./transaction/signed
|
|
||||||
import ./transaction/serialization
|
import ./transaction/serialization
|
||||||
|
import ./transaction/hashing
|
||||||
|
import ./transaction/signed
|
||||||
|
|
||||||
export transaction
|
export transaction except hash
|
||||||
export signed
|
|
||||||
export serialization.toBytes
|
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 ../signatures
|
||||||
import ./transaction
|
import ./transaction
|
||||||
import ./serialization
|
import ./hashing
|
||||||
|
|
||||||
type SignedTransaction* = object
|
type SignedTransaction* = object
|
||||||
transaction: Transaction
|
transaction: Transaction
|
||||||
@ -20,7 +20,8 @@ func init*(
|
|||||||
)
|
)
|
||||||
|
|
||||||
func sign*(identity: Identity, transaction: Transaction): SignedTransaction =
|
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)
|
SignedTransaction.init(transaction, identity.identifier, signature)
|
||||||
|
|
||||||
func transaction*(signed: SignedTransaction): Transaction =
|
func transaction*(signed: SignedTransaction): Transaction =
|
||||||
@ -33,4 +34,5 @@ func signature*(signed: SignedTransaction): Signature =
|
|||||||
signed.signature
|
signed.signature
|
||||||
|
|
||||||
func verifySignature*(signed: SignedTransaction): bool =
|
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 ../basics
|
||||||
|
import ../hashing
|
||||||
import ./storagerequest
|
import ./storagerequest
|
||||||
import ./period
|
import ./period
|
||||||
import ./groth16
|
import ./groth16
|
||||||
@ -13,7 +14,7 @@ type
|
|||||||
TransactionKind* {.pure.} = enum
|
TransactionKind* {.pure.} = enum
|
||||||
storageProof
|
storageProof
|
||||||
missingProof
|
missingProof
|
||||||
Transaction* = object
|
Transaction* = ref object
|
||||||
requestId: StorageRequestId
|
requestId: StorageRequestId
|
||||||
slotIndex: uint32
|
slotIndex: uint32
|
||||||
period: Period
|
period: Period
|
||||||
@ -24,6 +25,7 @@ type
|
|||||||
proof: Groth16Proof
|
proof: Groth16Proof
|
||||||
of missingProof:
|
of missingProof:
|
||||||
discard
|
discard
|
||||||
|
hash: ?Hash
|
||||||
|
|
||||||
func storageProof*(
|
func storageProof*(
|
||||||
_: type Transaction,
|
_: type Transaction,
|
||||||
@ -85,6 +87,12 @@ func challenge*(transaction: Transaction): array[32, byte] =
|
|||||||
func proof*(transaction: Transaction): Groth16Proof =
|
func proof*(transaction: Transaction): Groth16Proof =
|
||||||
transaction.proof
|
transaction.proof
|
||||||
|
|
||||||
|
func `hash=`*(transaction: Transaction, hash: Hash) =
|
||||||
|
transaction.hash = some hash
|
||||||
|
|
||||||
|
func hash*(transaction: Transaction): ?Hash =
|
||||||
|
transaction.hash
|
||||||
|
|
||||||
func `==`*(a, b: Transaction): bool =
|
func `==`*(a, b: Transaction): bool =
|
||||||
if a.kind != b.kind:
|
if a.kind != b.kind:
|
||||||
return false
|
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 ../basics
|
||||||
import codexvalidator/signatures
|
import codexvalidator/signatures
|
||||||
import codexvalidator/transaction
|
import codexvalidator/transaction
|
||||||
|
import codexvalidator/hashing
|
||||||
|
|
||||||
suite "Transaction signing":
|
suite "Transaction signing":
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ suite "Transaction signing":
|
|||||||
let signed = identity.sign(transaction)
|
let signed = identity.sign(transaction)
|
||||||
check signed.transaction == transaction
|
check signed.transaction == transaction
|
||||||
check signed.signer == identity.identifier
|
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":
|
test "transaction signature can be verified":
|
||||||
let identity = Identity.example
|
let identity = Identity.example
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
import ./codexvalidator/testSignatures
|
import ./codexvalidator/testSignatures
|
||||||
import ./codexvalidator/transaction/testTransaction
|
import ./codexvalidator/transaction/testTransaction
|
||||||
import ./codexvalidator/transaction/testSigning
|
|
||||||
import ./codexvalidator/transaction/testSerialization
|
import ./codexvalidator/transaction/testSerialization
|
||||||
|
import ./codexvalidator/transaction/testHashing
|
||||||
|
import ./codexvalidator/transaction/testSigning
|
||||||
import ./codexvalidator/blocks/testBlock
|
import ./codexvalidator/blocks/testBlock
|
||||||
import ./codexvalidator/blocks/testSerialization
|
import ./codexvalidator/blocks/testSerialization
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user