This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathObjectGenerators.scala
More file actions
75 lines (53 loc) · 2.71 KB
/
ObjectGenerators.scala
File metadata and controls
75 lines (53 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package scorex
import java.net.{InetAddress, InetSocketAddress}
import org.scalacheck.{Arbitrary, Gen}
import scorex.core.app.Version
import scorex.core.network.message.BasicMsgDataTypes._
import scorex.core.transaction.box.proposition.PublicKey25519Proposition
import scorex.core.transaction.state.{PrivateKey25519, PrivateKey25519Companion}
import scorex.core.{ModifierId, ModifierTypeId, NodeViewModifier}
import scorex.core.crypto.signatures.Curve25519
trait ObjectGenerators {
val MaxVersion = 999
val MaxIp = 255
val MaxPort = 65535
lazy val smallInt: Gen[Int] = Gen.choose(0, 20)
lazy val nonEmptyBytesGen: Gen[Array[Byte]] = Gen.nonEmptyListOf(Arbitrary.arbitrary[Byte])
.map(_.toArray).suchThat(_.length > 0)
def genBoundedBytes(minSize: Int, maxSize: Int): Gen[Array[Byte]] = {
Gen.choose(minSize, maxSize) flatMap { sz => Gen.listOfN(sz, Arbitrary.arbitrary[Byte]).map(_.toArray) }
}
def genBytes(size: Int): Gen[Array[Byte]] = genBoundedBytes(size, size)
lazy val positiveLongGen: Gen[Long] = Gen.choose(1, Long.MaxValue)
lazy val positiveByteGen: Gen[Byte] = Gen.choose(1, Byte.MaxValue)
lazy val modifierIdGen: Gen[ModifierId] = Gen.listOfN(NodeViewModifier.ModifierIdSize, Arbitrary.arbitrary[Byte])
.map(id => ModifierId @@ id.toArray)
lazy val modifierTypeIdGen: Gen[ModifierTypeId] = Arbitrary.arbitrary[Byte].map(t => ModifierTypeId @@ t)
lazy val invDataGen: Gen[InvData] = for {
modifierTypeId: ModifierTypeId <- modifierTypeIdGen
modifierIds: Seq[ModifierId] <- Gen.nonEmptyListOf(modifierIdGen) if modifierIds.nonEmpty
} yield modifierTypeId -> modifierIds
lazy val modifierWithIdGen: Gen[(ModifierId, Array[Byte])] = for {
id <- modifierIdGen
mod <- nonEmptyBytesGen
} yield id -> mod
lazy val modifiersGen: Gen[ModifiersData] = for {
modifierTypeId: ModifierTypeId <- modifierTypeIdGen
modifiers: Map[ModifierId, Array[Byte]] <- Gen.nonEmptyMap(modifierWithIdGen).suchThat(_.nonEmpty)
} yield modifierTypeId -> modifiers
lazy val appVersionGen = for {
fd <- Gen.choose(0: Byte, Byte.MaxValue)
sd <- Gen.choose(0: Byte, Byte.MaxValue)
td <- Gen.choose(0: Byte, Byte.MaxValue)
} yield Version(fd, sd, td)
lazy val inetSocketAddressGen = for {
ip1 <- Gen.choose(0, MaxIp)
ip2 <- Gen.choose(0, MaxIp)
ip3 <- Gen.choose(0, MaxIp)
ip4 <- Gen.choose(0, MaxIp)
port <- Gen.choose(0, MaxPort)
} yield new InetSocketAddress(InetAddress.getByName(s"$ip1.$ip2.$ip3.$ip4"), port)
lazy val key25519Gen: Gen[(PrivateKey25519, PublicKey25519Proposition)] = genBytes(Curve25519.KeyLength)
.map(s => PrivateKey25519Companion.generateKeys(s))
lazy val propositionGen: Gen[PublicKey25519Proposition] = key25519Gen.map(_._2)
}