Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package com.magiadigital.cobranzas.contract
  2.  
  3. import com.magiadigital.cobranzas.state.DocumentState
  4. import net.corda.core.contracts.*
  5. import net.corda.core.crypto.SecureHash
  6. import net.corda.core.node.ServiceHub
  7. import net.corda.core.random63BitValue
  8.  
  9. /**
  10. * A implementation of a basic smart contract in Corda.
  11. *
  12. * This contract enforces rules regarding the creation of a valid [DocumentState], which in turn encapsulates a [Document].
  13. *
  14. * For a new [Document] to be issued onto the ledger, a transaction is required which takes:
  15. * - Zero input states.
  16. * - One output state: the new [Document].
  17. * - An Create() command with the public keys of both the sender and the recipient.
  18. *
  19. * All contracts must sub-class the [Contract] interface.
  20. */
  21. open class DocumentContract : Contract {
  22. /**
  23. * The verify() function of all the states' contracts must not throw an exception for a transaction to be
  24. * considered valid.
  25. */
  26.  
  27. /** This is a reference to the underlying legal contract template and associated parameters. */
  28. override val legalContractReference: SecureHash = SecureHash.sha256("Contrato de recaudacion y cobranzas Globokas - Magic Chain")
  29.  
  30. /**
  31. * This contract only implements two command, Create and Collect.
  32. */
  33. interface Commands : CommandData {
  34. class Create : TypeOnlyCommandData(), Commands
  35. class Collect : TypeOnlyCommandData(), Commands
  36. }
  37.  
  38. override fun verify(tx: TransactionForContract) {
  39. // Group by everything except owner: any modification to the CP at all is considered changing it fundamentally.
  40. //val groups = tx.groupStates(DocumentState::linearId)
  41.  
  42. println("tx.inputs.size = " + tx.inputs.size)
  43. println("tx.outputs.size = " + tx.outputs.size)
  44.  
  45. val command = tx.commands.requireSingleCommand<DocumentContract.Commands>()
  46. when (command.value) {
  47. is Commands.Create -> {
  48. requireThat {
  49. // Generic constraints around the Collect transaction.
  50. "No inputs should be consumed when issuing a Collection Document." by (tx.inputs.isEmpty())
  51. "Only one output state should be created." by (tx.outputs.size == 1)
  52. val out = tx.outputs.single() as DocumentState
  53. "The sender and the recipient cannot be the same entity." by (out.company != out.collector)
  54. //"All of the participants must be signers." by (command.signers.containsAll(out.participants))
  55.  
  56. // Document-specific constraints.
  57. "The Document's value must be non-negative." by (out.document.amount > 0)
  58. "The collect document flagCharged must be false." by (out.flagCharged == false)
  59. }
  60. }
  61.  
  62. is Commands.Collect -> {
  63. val stateGroups = tx.groupStates(DocumentState::class.java) { it.linearId }
  64. require(stateGroups.size == 1) { "Must be only a single document collect in transaction" }
  65. requireThat {
  66. val input = tx.inputs.single() as DocumentState
  67. // Generic constraints around the Collect transaction.
  68. "the collect document is propagated" by (tx.outputs.size == 1)
  69. "This document its not collect" by (input.flagCharged != true)
  70. }
  71. }
  72.  
  73. // TODO: Think about how to evolve contracts over time with new commands.
  74. else -> throw IllegalArgumentException("Unrecognised command $command")
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement