Revert "core, contracts: Rewrite Generators to use functions instead of classes"

This reverts commit deddcc91c7f6ad436c44daf69b4cb81a406cb28e.
This commit is contained in:
Andras Slemmer 2016-09-06 13:53:42 +01:00
parent abdc45176d
commit bd59022e8f
3 changed files with 115 additions and 91 deletions

View File

@ -16,55 +16,73 @@ import java.util.*
* This file contains generators for quickcheck style testing. The idea is that we can write random instance generators
* for each type we have in the code and test against those instead of predefined mock data. This style of testing can
* catch corner case bugs and test algebraic properties of the code, for example deserialize(serialize(generatedThing)) == generatedThing
*
* TODO add combinators for easier Generator writing
*/
val contractStateGenerator = generator { random, status ->
Cash.State(
amount = amountGenerator(issuedGenerator(currencyGenerator)).generate(random, status),
owner = publicKeyGenerator.generate(random, status)
)
class ContractStateGenerator : Generator<ContractState>(ContractState::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): ContractState {
return Cash.State(
amount = AmountGenerator(IssuedGenerator(CurrencyGenerator())).generate(random, status),
owner = PublicKeyGenerator().generate(random, status)
)
}
}
val moveGenerator = generator { random, status ->
Cash.Commands.Move(secureHashGenerator.generate(random, status))
class MoveGenerator : Generator<Cash.Commands.Move>(Cash.Commands.Move::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Cash.Commands.Move {
return Cash.Commands.Move(SecureHashGenerator().generate(random, status))
}
}
val issueGenerator = generator { random, status ->
Cash.Commands.Issue(random.nextLong())
class IssueGenerator : Generator<Cash.Commands.Issue>(Cash.Commands.Issue::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Cash.Commands.Issue {
return Cash.Commands.Issue(random.nextLong())
}
}
val exitGenerator = generator { random, status ->
Cash.Commands.Exit(amountGenerator(issuedGenerator(currencyGenerator)).generate(random, status))
class ExitGenerator : Generator<Cash.Commands.Exit>(Cash.Commands.Exit::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Cash.Commands.Exit {
return Cash.Commands.Exit(AmountGenerator(IssuedGenerator(CurrencyGenerator())).generate(random, status))
}
}
val commandDataGenerator = generator { random, status ->
val generators = listOf(moveGenerator, issueGenerator, exitGenerator)
generators[random.nextInt(0, generators.size - 1)].generate(random, status)
class CommandDataGenerator : Generator<CommandData>(CommandData::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): CommandData {
val generators = listOf(MoveGenerator(), IssueGenerator(), ExitGenerator())
return generators[random.nextInt(0, generators.size - 1)].generate(random, status)
}
}
val commandGenerator = generator { random, status ->
val signersGenerator = ArrayListGenerator()
signersGenerator.addComponentGenerators(listOf(publicKeyGenerator))
Command(commandDataGenerator.generate(random, status), publicKeyGenerator.generate(random, status))
class CommandGenerator : Generator<Command>(Command::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Command {
val signersGenerator = ArrayListGenerator()
signersGenerator.addComponentGenerators(listOf(PublicKeyGenerator()))
return Command(CommandDataGenerator().generate(random, status), PublicKeyGenerator().generate(random, status))
}
}
val wiredTransactionGenerator = generator { random, status ->
val commands = commandGenerator.generateList(random, status) + listOf(commandGenerator.generate(random, status))
WireTransaction(
inputs = stateRefGenerator.generateList(random, status),
attachments = secureHashGenerator.generateList(random, status),
outputs = transactionStateGenerator(contractStateGenerator).generateList(random, status),
commands = commands,
notary = partyGenerator.generate(random, status),
signers = commands.flatMap { it.signers },
type = TransactionType.General(),
timestamp = timestampGenerator.generate(random, status)
)
class WiredTransactionGenerator: Generator<WireTransaction>(WireTransaction::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): WireTransaction {
val commands = CommandGenerator().generateList(random, status) + listOf(CommandGenerator().generate(random, status))
return WireTransaction(
inputs = StateRefGenerator().generateList(random, status),
attachments = SecureHashGenerator().generateList(random, status),
outputs = TransactionStateGenerator(ContractStateGenerator()).generateList(random, status),
commands = commands,
notary = PartyGenerator().generate(random, status),
signers = commands.flatMap { it.signers },
type = TransactionType.General(),
timestamp = TimestampGenerator().generate(random, status)
)
}
}
val signedTransactionGenerator = generator { random, status ->
val wireTransaction = wiredTransactionGenerator.generate(random, status)
SignedTransaction(
txBits = wireTransaction.serialized,
sigs = listOf(NullSignature)
)
class SignedTransactionGenerator: Generator<SignedTransaction>(SignedTransaction::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): SignedTransaction {
val wireTransaction = WiredTransactionGenerator().generate(random, status)
return SignedTransaction(
txBits = wireTransaction.serialized,
sigs = listOf(NullSignature)
)
}
}

View File

@ -3,8 +3,6 @@ package com.r3corda.core.testing
import com.pholser.junit.quickcheck.generator.GenerationStatus
import com.pholser.junit.quickcheck.generator.Generator
import com.pholser.junit.quickcheck.generator.java.lang.StringGenerator
import com.pholser.junit.quickcheck.generator.java.time.DurationGenerator
import com.pholser.junit.quickcheck.generator.java.time.InstantGenerator
import com.pholser.junit.quickcheck.generator.java.util.ArrayListGenerator
import com.pholser.junit.quickcheck.random.SourceOfRandomness
import com.r3corda.core.contracts.*
@ -29,71 +27,84 @@ fun <A> Generator<A>.generateList(random: SourceOfRandomness, status: Generation
return arrayGenerator.generate(random, status) as List<A>
}
inline fun <reified B : Any> generator(
crossinline generatorFunction: (SourceOfRandomness, GenerationStatus) -> B
): Generator<B> {
return object : Generator<B>(B::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus) = generatorFunction(random, status)
class PrivateKeyGenerator: Generator<PrivateKey>(PrivateKey::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): PrivateKey {
return entropyToKeyPair(random.nextBigInteger(32)).private
}
}
val privateKeyGenerator = generator { random, status ->
entropyToKeyPair(random.nextBigInteger(32)).private
}
class PrivateKeyGenerator : Generator<PrivateKey>(PrivateKey::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus) = privateKeyGenerator.generate(random, status)
class PublicKeyGenerator: Generator<PublicKey>(PublicKey::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): PublicKey {
return entropyToKeyPair(random.nextBigInteger(32)).public
}
}
val publicKeyGenerator = generator { random, status ->
entropyToKeyPair(random.nextBigInteger(32)).public
}
class PublicKeyGenerator : Generator<PublicKey>(PublicKey::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus) = publicKeyGenerator.generate(random, status)
class PartyGenerator: Generator<Party>(Party::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Party {
return Party(StringGenerator().generate(random, status), PublicKeyGenerator().generate(random, status))
}
}
val partyGenerator = generator { random, status ->
Party(StringGenerator().generate(random, status), publicKeyGenerator.generate(random, status))
class PartyAndReferenceGenerator: Generator<PartyAndReference>(PartyAndReference::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): PartyAndReference {
return PartyAndReference(PartyGenerator().generate(random, status), OpaqueBytes(random.nextBytes(16)))
}
}
val partyAndReferenceGenerator = generator { random, status ->
PartyAndReference(partyGenerator.generate(random, status), OpaqueBytes(random.nextBytes(16)))
class SecureHashGenerator: Generator<SecureHash>(SecureHash::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): SecureHash {
return SecureHash.Companion.sha256(random.nextBytes(16))
}
}
val secureHashGenerator = generator { random, status ->
SecureHash.Companion.sha256(random.nextBytes(16))
class StateRefGenerator: Generator<StateRef>(StateRef::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): StateRef {
return StateRef(SecureHash.Companion.sha256(random.nextBytes(16)), random.nextInt(0, 10))
}
}
val stateRefGenerator = generator { random, status ->
StateRef(SecureHash.Companion.sha256(random.nextBytes(16)), random.nextInt(0, 10))
class TransactionStateGenerator<T : ContractState>(val stateGenerator: Generator<T>) : Generator<TransactionState<T>>(TransactionState::class.java as Class<TransactionState<T>>) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): TransactionState<T> {
return TransactionState(stateGenerator.generate(random, status), PartyGenerator().generate(random, status))
}
}
fun <T : ContractState> transactionStateGenerator(stateGenerator: Generator<T>) = generator { random, status ->
TransactionState(stateGenerator.generate(random, status), partyGenerator.generate(random, status))
class IssuedGenerator<T>(val productGenerator: Generator<T>) : Generator<Issued<T>>(Issued::class.java as Class<Issued<T>>) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Issued<T> {
return Issued(PartyAndReferenceGenerator().generate(random, status), productGenerator.generate(random, status))
}
}
fun <T> issuedGenerator(productGenerator: Generator<T>) = generator { random, status ->
Issued(partyAndReferenceGenerator.generate(random, status), productGenerator.generate(random, status))
class AmountGenerator<T>(val tokenGenerator: Generator<T>) : Generator<Amount<T>>(Amount::class.java as Class<Amount<T>>) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Amount<T> {
return Amount(random.nextLong(0, 1000000), tokenGenerator.generate(random, status))
}
}
fun <T> amountGenerator(tokenGenerator: Generator<T>) = generator { random, status ->
Amount(random.nextLong(0, 1000000), tokenGenerator.generate(random, status))
class CurrencyGenerator() : Generator<Currency>(Currency::class.java) {
companion object {
val currencies = Currency.getAvailableCurrencies().toList()
}
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Currency {
return currencies[random.nextInt(0, currencies.size - 1)]
}
}
private val currencies = Currency.getAvailableCurrencies().toList()
val currencyGenerator = generator { random, status ->
currencies[random.nextInt(0, currencies.size - 1)]
class InstantGenerator : Generator<Instant>(Instant::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Instant {
return Instant.ofEpochMilli(random.nextLong(0, 1000000))
}
}
val instantGenerator = generator { random, status ->
Instant.ofEpochMilli(random.nextLong(0, 1000000))
class DurationGenerator : Generator<Duration>(Duration::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Duration {
return Duration.ofMillis(random.nextLong(0, 1000000))
}
}
val durationGenerator = generator { random, status ->
Duration.ofMillis(random.nextLong(0, 1000000))
}
val timestampGenerator = generator { random, status ->
Timestamp(InstantGenerator().generate(random, status), DurationGenerator().generate(random, status))
class TimestampGenerator : Generator<Timestamp>(Timestamp::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Timestamp {
return Timestamp(InstantGenerator().generate(random, status), DurationGenerator().generate(random, status))
}
}

View File

@ -2,17 +2,15 @@ package com.r3corda.core.protocols
import com.esotericsoftware.kryo.io.Input
import com.pholser.junit.quickcheck.From
import com.pholser.junit.quickcheck.Produced
import com.pholser.junit.quickcheck.Property
import com.pholser.junit.quickcheck.generator.GenerationStatus
import com.pholser.junit.quickcheck.generator.Generator
import com.pholser.junit.quickcheck.random.SourceOfRandomness
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck
import com.r3corda.contracts.testing.signedTransactionGenerator
import com.r3corda.contracts.testing.SignedTransactionGenerator
import com.r3corda.core.serialization.createKryo
import com.r3corda.core.serialization.serialize
import com.r3corda.core.testing.generator
import com.r3corda.core.testing.partyGenerator
import com.r3corda.core.testing.PartyGenerator
import com.r3corda.protocols.BroadcastTransactionProtocol
import org.junit.runner.RunWith
import kotlin.test.assertEquals
@ -20,18 +18,15 @@ import kotlin.test.assertEquals
@RunWith(JUnitQuickcheck::class)
class BroadcastTransactionProtocolTest {
companion object {
val notifyTxRequestMessageGenerator = generator { random, status ->
BroadcastTransactionProtocol.NotifyTxRequestMessage(
tx = signedTransactionGenerator.generate(random, status),
class NotifyTxRequestMessageGenerator : Generator<BroadcastTransactionProtocol.NotifyTxRequestMessage>(BroadcastTransactionProtocol.NotifyTxRequestMessage::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): BroadcastTransactionProtocol.NotifyTxRequestMessage {
return BroadcastTransactionProtocol.NotifyTxRequestMessage(
tx = SignedTransactionGenerator().generate(random, status),
events = setOf(),
replyToParty = partyGenerator.generate(random, status),
replyToParty = PartyGenerator().generate(random, status),
sessionID = random.nextLong()
)
}
class NotifyTxRequestMessageGenerator: Generator<BroadcastTransactionProtocol.NotifyTxRequestMessage>(BroadcastTransactionProtocol.NotifyTxRequestMessage::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus) = notifyTxRequestMessageGenerator.generate(random, status)
}
}
@Property