mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-03-27 06:03:08 +00:00
split examples module
This commit is contained in:
parent
d1dd428560
commit
dbdc837e0f
@ -1,98 +1,11 @@
|
||||
import std/random
|
||||
import codexvalidator/hashing
|
||||
import codexvalidator/transaction
|
||||
import codexvalidator/signatures
|
||||
import codexvalidator/blocks
|
||||
import ./basics
|
||||
import ./examples/basics
|
||||
import ./examples/blocks
|
||||
import ./examples/hashing
|
||||
import ./examples/signatures
|
||||
import ./examples/transaction
|
||||
|
||||
proc example*[T: SomeInteger](_: type T): T =
|
||||
rand(T)
|
||||
|
||||
proc example*(_: type UInt256): UInt256 =
|
||||
UInt256.fromBytesBE(array[32, byte].example)
|
||||
|
||||
proc example*[T, length](_: type array[length, T]): array[length, T] =
|
||||
for i in result.low..result.high:
|
||||
result[i] = T.example
|
||||
|
||||
proc example*[T](_: type seq[T], length = 0..10): seq[T] =
|
||||
let len = rand(length)
|
||||
newSeqWith(len, T.example)
|
||||
|
||||
proc example*(_: type StorageRequestId): StorageRequestId =
|
||||
StorageRequestId(array[32, byte].example)
|
||||
|
||||
proc example*(_: type Period): Period =
|
||||
Period(uint64.example)
|
||||
|
||||
proc example*(_: type G1Point): G1Point =
|
||||
G1Point.init(UInt256.example, UInt256.example)
|
||||
|
||||
proc example*(_: type Fp2Element): Fp2Element =
|
||||
Fp2Element.init(UInt256.example, UInt256.example)
|
||||
|
||||
proc example*(_: type G2Point): G2Point =
|
||||
G2Point.init(
|
||||
Fp2Element.example,
|
||||
Fp2Element.example
|
||||
)
|
||||
|
||||
proc example*(_: type Groth16Proof): Groth16Proof =
|
||||
Groth16Proof.init(
|
||||
G1Point.example,
|
||||
G2Point.example,
|
||||
G1Point.example
|
||||
)
|
||||
|
||||
proc example*(_: type StorageProofInput): StorageProofInput =
|
||||
let requestId = StorageRequestId.example
|
||||
let slotIndex = uint32.example
|
||||
let period = Period.example
|
||||
let merkleRoot = array[32, byte].example
|
||||
let challenge = array[32, byte].example
|
||||
StorageProofInput.init(
|
||||
requestId,
|
||||
slotIndex,
|
||||
period,
|
||||
merkleRoot,
|
||||
challenge
|
||||
)
|
||||
|
||||
proc example*(_: type Transaction): Transaction =
|
||||
let kind = [TransactionKind.storageProof, TransactionKind.missingProof].sample
|
||||
let proofInput = StorageProofInput.example
|
||||
case kind
|
||||
of TransactionKind.missingProof:
|
||||
Transaction.missingProof(proofInput)
|
||||
of TransactionKind.storageProof:
|
||||
let proof = Groth16Proof.example
|
||||
Transaction.storageProof(proofInput, proof)
|
||||
|
||||
proc example*(_: type Identity): Identity =
|
||||
Identity.random(result)
|
||||
|
||||
proc example*(_: type Identifier): Identifier =
|
||||
Identity.example.identifier
|
||||
|
||||
proc example*(_: type Signature): Signature =
|
||||
Identity.example.sign(seq[byte].example)
|
||||
|
||||
proc example*[T](_: type Signed[T]): Signed[T] =
|
||||
Signed.sign(Identity.example, T.example)
|
||||
|
||||
proc example*(_: type CommitteeMember): CommitteeMember =
|
||||
CommitteeMember(uint32.example.int)
|
||||
|
||||
proc example*(_: type Hash): Hash =
|
||||
Hash.hash(seq[byte].example)
|
||||
|
||||
proc example*(_: type BlockId): BlockId =
|
||||
BlockId.init(CommitteeMember.example, uint64.example, Hash.example)
|
||||
|
||||
proc example*(_: type Block): Block =
|
||||
Block(
|
||||
author: CommitteeMember.example,
|
||||
round: uint64.example,
|
||||
parents: seq[BlockId].example,
|
||||
transactions: seq[Signed[Transaction]].example
|
||||
)
|
||||
export basics
|
||||
export blocks
|
||||
export hashing
|
||||
export signatures
|
||||
export transaction
|
||||
|
||||
19
tests/codexvalidator/examples/basics.nim
Normal file
19
tests/codexvalidator/examples/basics.nim
Normal file
@ -0,0 +1,19 @@
|
||||
import std/random
|
||||
import codexvalidator/basics
|
||||
|
||||
export basics.UInt256
|
||||
|
||||
proc example*[T: SomeInteger](_: type T): T =
|
||||
rand(T)
|
||||
|
||||
proc example*(_: type UInt256): UInt256 =
|
||||
UInt256.fromBytesBE(array[32, byte].example)
|
||||
|
||||
proc example*[T, length](_: type array[length, T]): array[length, T] =
|
||||
for i in result.low..result.high:
|
||||
result[i] = T.example
|
||||
|
||||
proc example*[T](_: type seq[T], length = 0..10): seq[T] =
|
||||
let len = rand(length)
|
||||
newSeqWith(len, T.example)
|
||||
|
||||
18
tests/codexvalidator/examples/blocks.nim
Normal file
18
tests/codexvalidator/examples/blocks.nim
Normal file
@ -0,0 +1,18 @@
|
||||
import codexvalidator/blocks
|
||||
import ./hashing
|
||||
import ./signatures
|
||||
import ./transaction
|
||||
|
||||
proc example*(_: type CommitteeMember): CommitteeMember =
|
||||
CommitteeMember(uint32.example.int)
|
||||
|
||||
proc example*(_: type BlockId): BlockId =
|
||||
BlockId.init(CommitteeMember.example, uint64.example, Hash.example)
|
||||
|
||||
proc example*(_: type Block): Block =
|
||||
Block(
|
||||
author: CommitteeMember.example,
|
||||
round: uint64.example,
|
||||
parents: seq[BlockId].example,
|
||||
transactions: seq[Signed[Transaction]].example
|
||||
)
|
||||
7
tests/codexvalidator/examples/hashing.nim
Normal file
7
tests/codexvalidator/examples/hashing.nim
Normal file
@ -0,0 +1,7 @@
|
||||
import codexvalidator/hashing
|
||||
import ./basics
|
||||
|
||||
export hashing.Hash
|
||||
|
||||
proc example*(_: type Hash): Hash =
|
||||
Hash.hash(seq[byte].example)
|
||||
18
tests/codexvalidator/examples/signatures.nim
Normal file
18
tests/codexvalidator/examples/signatures.nim
Normal file
@ -0,0 +1,18 @@
|
||||
import codexvalidator/signatures
|
||||
|
||||
export signatures.Identity
|
||||
export signatures.Identifier
|
||||
export signatures.Signature
|
||||
export signatures.Signed
|
||||
|
||||
proc example*(_: type Identity): Identity =
|
||||
Identity.random(result)
|
||||
|
||||
proc example*(_: type Identifier): Identifier =
|
||||
Identity.example.identifier
|
||||
|
||||
proc example*(_: type Signature): Signature =
|
||||
Identity.example.sign(seq[byte].example)
|
||||
|
||||
proc example*[T](_: type Signed[T]): Signed[T] =
|
||||
Signed.sign(Identity.example, T.example)
|
||||
54
tests/codexvalidator/examples/transaction.nim
Normal file
54
tests/codexvalidator/examples/transaction.nim
Normal file
@ -0,0 +1,54 @@
|
||||
import std/random
|
||||
import codexvalidator/transaction
|
||||
import ./basics
|
||||
|
||||
export transaction.Transaction
|
||||
|
||||
proc example*(_: type StorageRequestId): StorageRequestId =
|
||||
StorageRequestId(array[32, byte].example)
|
||||
|
||||
proc example*(_: type Period): Period =
|
||||
Period(uint64.example)
|
||||
|
||||
proc example*(_: type G1Point): G1Point =
|
||||
G1Point.init(UInt256.example, UInt256.example)
|
||||
|
||||
proc example*(_: type Fp2Element): Fp2Element =
|
||||
Fp2Element.init(UInt256.example, UInt256.example)
|
||||
|
||||
proc example*(_: type G2Point): G2Point =
|
||||
G2Point.init(
|
||||
Fp2Element.example,
|
||||
Fp2Element.example
|
||||
)
|
||||
|
||||
proc example*(_: type Groth16Proof): Groth16Proof =
|
||||
Groth16Proof.init(
|
||||
G1Point.example,
|
||||
G2Point.example,
|
||||
G1Point.example
|
||||
)
|
||||
|
||||
proc example*(_: type StorageProofInput): StorageProofInput =
|
||||
let requestId = StorageRequestId.example
|
||||
let slotIndex = uint32.example
|
||||
let period = Period.example
|
||||
let merkleRoot = array[32, byte].example
|
||||
let challenge = array[32, byte].example
|
||||
StorageProofInput.init(
|
||||
requestId,
|
||||
slotIndex,
|
||||
period,
|
||||
merkleRoot,
|
||||
challenge
|
||||
)
|
||||
|
||||
proc example*(_: type Transaction): Transaction =
|
||||
let kind = [TransactionKind.storageProof, TransactionKind.missingProof].sample
|
||||
let proofInput = StorageProofInput.example
|
||||
case kind
|
||||
of TransactionKind.missingProof:
|
||||
Transaction.missingProof(proofInput)
|
||||
of TransactionKind.storageProof:
|
||||
let proof = Groth16Proof.example
|
||||
Transaction.storageProof(proofInput, proof)
|
||||
Loading…
x
Reference in New Issue
Block a user