Skip to content

Instantly share code, notes, and snippets.

@Christewart
Created July 18, 2016 01:20
Show Gist options
  • Save Christewart/a8e0bd42e2b339ee8cdab349503cbc06 to your computer and use it in GitHub Desktop.
Save Christewart/a8e0bd42e2b339ee8cdab349503cbc06 to your computer and use it in GitHub Desktop.
trait BlockchainElementsGenerator {
def block : Gen[Block] = for {
header <- blockHeader
txs <- Gen.listOfN(randomNumber(10), TransactionGenerators.transactions)
} yield Block(header, txs)
def blockHeader : Gen[BlockHeader] = for {
version <- NumberGenerator.uInt32s
previousBlockHash <- CryptoGenerators.doubleSha256Digest
merkleRootHash <- CryptoGenerators.doubleSha256Digest
time <- NumberGenerator.uInt32s
nBits <- NumberGenerator.uInt32s
nonce <- NumberGenerator.uInt32s
} yield BlockHeader(version, previousBlockHash, merkleRootHash, time, nBits, nonce)
private def randomNumber(lessThan : Int) : Int = (scala.util.Random.nextInt() % lessThan).abs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment