replace slot id by request id and slot index

This commit is contained in:
Mark Spanbroek 2024-12-02 16:07:47 +01:00
parent 943aa07cd7
commit f051273d01
5 changed files with 80 additions and 34 deletions

View File

@ -1,9 +1,9 @@
import ./basics
import ./transaction/slotid
import ./transaction/storagerequest
import ./transaction/period
import ./transaction/groth16
export slotid
export storagerequest
export period
export groth16
@ -14,9 +14,11 @@ type
storageProof
missingProof
Transaction* = object
slotId: SlotId
requestId: StorageRequestId
slotIndex: uint32
period: Period
inputs: seq[UInt256]
merkleRoot: UInt256
challenge: UInt256
case kind: TransactionKind
of storageProof:
proof: Groth16Proof
@ -25,43 +27,57 @@ type
proc storageProof*(
_: type Transaction,
slotId: SlotId,
requestId: StorageRequestId,
slotIndex: uint32,
period: Period,
inputs: seq[UInt256],
merkleRoot: UInt256,
challenge: UInt256,
proof: Groth16Proof
): Transaction =
Transaction(
kind: TransactionKind.storageProof,
slotId: slotId,
requestId: requestId,
period: period,
inputs: inputs,
slotIndex: slotIndex,
merkleRoot: merkleRoot,
challenge: challenge,
proof: proof
)
proc missingProof*(
_: type Transaction,
slotId: SlotId,
requestId: StorageRequestId,
slotIndex: uint32,
period: Period,
inputs: seq[UInt256],
merkleRoot: UInt256,
challenge: UInt256,
): Transaction =
Transaction(
kind: TransactionKind.missingProof,
slotId: slotId,
requestId: requestId,
slotIndex: slotIndex,
period: period,
inputs: inputs
merkleRoot: merkleRoot,
challenge: challenge
)
func version*(transaction: Transaction): TransactionVersion =
TransactionVersion.version0
func slotId*(transaction: Transaction): SlotId =
transaction.slotId
func requestId*(transaction: Transaction): StorageRequestId =
transaction.requestId
func slotIndex*(transaction: Transaction): uint32 =
transaction.slotIndex
func period*(transaction: Transaction): Period =
transaction.period
func inputs*(transaction: Transaction): seq[UInt256] =
transaction.inputs
func merkleRoot*(transaction: Transaction): UInt256 =
transaction.merkleRoot
func challenge*(transaction: Transaction): UInt256 =
transaction.challenge
func proof*(transaction: Transaction): Groth16Proof =
transaction.proof

View File

@ -1,4 +0,0 @@
type SlotId* = distinct array[32, byte]
func `$`*(slotId: SlotId): string {.borrow.}
func `==`*(a, b: SlotId): bool {.borrow.}

View File

@ -0,0 +1,4 @@
type StorageRequestId* = distinct array[32, byte]
func `$`*(slotId: StorageRequestId): string {.borrow.}
func `==`*(a, b: StorageRequestId): bool {.borrow.}

View File

@ -16,8 +16,8 @@ proc example*[T](_: type seq[T], length = 0..10): seq[T] =
let len = rand(length)
newSeqWith(len, T.example)
proc example*(_: type SlotId): SlotId =
SlotId(array[32, byte].example)
proc example*(_: type StorageRequestId): StorageRequestId =
StorageRequestId(array[32, byte].example)
proc example*(_: type Period): Period =
Period(uint64.example)
@ -43,12 +43,27 @@ proc example*(_: type Groth16Proof): Groth16Proof =
proc example*(_: type Transaction): Transaction =
let kind = [TransactionKind.storageProof, TransactionKind.missingProof].sample
let slotId = SlotId.example
let requestId = StorageRequestId.example
let slotIndex = uint32.example
let period = Period.example
let inputs = seq[UInt256].example
let merkleRoot = UInt256.example
let challenge = UInt256.example
case kind
of TransactionKind.missingProof:
Transaction.missingProof(slotId, period, inputs)
Transaction.missingProof(
requestId,
slotIndex,
period,
merkleRoot,
challenge
)
of TransactionKind.storageProof:
let proof = Groth16Proof.example
Transaction.storageProof(slotId, period, inputs, proof)
Transaction.storageProof(
requestId,
slotIndex,
period,
merkleRoot,
challenge,
proof
)

View File

@ -6,21 +6,36 @@ import ./examples
suite "Transaction":
test "a transaction can contain a storage proof":
let slotId = SlotId.example
let requestId = StorageRequestId.example
let slotIndex = uint32.example
let period = Period.example
let inputs = seq[UInt256].example
let merkleRoot = UInt256.example
let challenge = UInt256.example
let proof = Groth16Proof.example
let transaction = Transaction.storageProof(slotId, period, inputs, proof)
let transaction = Transaction.storageProof(
requestId, slotIndex, period, merkleRoot, challenge, proof
)
check transaction.requestId == requestId
check transaction.slotIndex == slotIndex
check transaction.period == period
check transaction.merkleRoot == merkleRoot
check transaction.challenge == challenge
check transaction.proof == proof
test "a transaction can indicate a missing storage proof":
let slotId = SlotId.example
let requestId = StorageRequestId.example
let slotIndex = uint32.example
let period = Period.example
let inputs = seq[UInt256].example
let transaction = Transaction.missingProof(slotId, period, inputs)
check transaction.slotId == slotId
let merkleRoot = UInt256.example
let challenge = UInt256.example
let transaction = Transaction.missingProof(
requestId, slotIndex, period, merkleRoot, challenge
)
check transaction.requestId == requestId
check transaction.slotIndex == slotIndex
check transaction.period == period
check transaction.inputs == inputs
check transaction.merkleRoot == merkleRoot
check transaction.challenge == challenge
test "transactions have a fixed version":
let transaction = Transaction.example