mirror of
https://github.com/corda/corda.git
synced 2025-06-21 08:40:03 +00:00
Enabled warnings as errors (#3514)
This commit is contained in:
@ -2,12 +2,12 @@ package net.corda.finance.contracts.universal
|
||||
|
||||
import net.corda.core.crypto.toStringShort
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.internal.uncheckedCast
|
||||
import java.math.BigDecimal
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
|
||||
private class PrettyPrint(arr : Arrangement) {
|
||||
|
||||
val parties = involvedParties(arr)
|
||||
|
||||
private val sb = StringBuilder()
|
||||
@ -16,21 +16,21 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
private var atStart = true
|
||||
private fun print(msg: String) {
|
||||
if (atStart)
|
||||
repeat(indentLevel, { sb.append(' ') })
|
||||
repeat(indentLevel) { sb.append(' ') }
|
||||
sb.append(msg)
|
||||
atStart = false
|
||||
}
|
||||
|
||||
private fun println(message: Any?) {
|
||||
if (atStart)
|
||||
repeat(indentLevel, { sb.append(' ') })
|
||||
repeat(indentLevel) { sb.append(' ') }
|
||||
sb.appendln(message)
|
||||
atStart = true
|
||||
}
|
||||
|
||||
private fun print(msg: Any?) {
|
||||
if (atStart)
|
||||
repeat(indentLevel, { sb.append(' ') })
|
||||
repeat(indentLevel) { sb.append(' ') }
|
||||
sb.append(msg)
|
||||
atStart = false
|
||||
}
|
||||
@ -45,8 +45,7 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
val partyMap = mutableMapOf<PublicKey, String>()
|
||||
val usedPartyNames = mutableSetOf<String>()
|
||||
|
||||
fun createPartyName(party : Party) : String
|
||||
{
|
||||
fun createPartyName(party : Party): String {
|
||||
val parts = party.name.organisation.toLowerCase().split(' ')
|
||||
|
||||
var camelName = parts.drop(1).fold(parts.first()) {
|
||||
@ -69,38 +68,38 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
}
|
||||
}
|
||||
|
||||
fun prettyPrint(per: Perceivable<Boolean>, x: Boolean? = null) {
|
||||
fun prettyPrintBoolean(per: Perceivable<Boolean>) {
|
||||
when (per) {
|
||||
is Const -> print("\"${per.value}\"")
|
||||
is PerceivableOr -> {
|
||||
prettyPrint(per.left)
|
||||
prettyPrintBoolean(per.left)
|
||||
print(" or ")
|
||||
prettyPrint(per.right)
|
||||
prettyPrintBoolean(per.right)
|
||||
}
|
||||
is PerceivableAnd -> {
|
||||
prettyPrint(per.left)
|
||||
prettyPrintBoolean(per.left)
|
||||
print(" and ")
|
||||
prettyPrint(per.right)
|
||||
prettyPrintBoolean(per.right)
|
||||
}
|
||||
is TimePerceivable -> {
|
||||
when (per.cmp) {
|
||||
Comparison.GT, Comparison.GTE -> {
|
||||
print("after(")
|
||||
prettyPrint(per.instant)
|
||||
prettyPrintInstant(per.instant)
|
||||
print(")")
|
||||
}
|
||||
Comparison.LT, Comparison.LTE -> {
|
||||
print("before(")
|
||||
prettyPrint(per.instant)
|
||||
prettyPrintInstant(per.instant)
|
||||
print(")")
|
||||
}
|
||||
}
|
||||
}
|
||||
is PerceivableComparison<*> -> {
|
||||
when (per.type) {
|
||||
BigDecimal::class.java -> prettyPrint(per.left as Perceivable<BigDecimal>)
|
||||
Instant::class.java -> prettyPrint(per.left as Perceivable<Instant>)
|
||||
Boolean::class.java -> prettyPrint(per.left as Perceivable<Boolean>)
|
||||
BigDecimal::class.java -> prettyPrintBigDecimal(uncheckedCast(per.left))
|
||||
Instant::class.java -> prettyPrintInstant(uncheckedCast(per.left))
|
||||
Boolean::class.java -> prettyPrintBoolean(uncheckedCast(per.left))
|
||||
}
|
||||
when (per.cmp) {
|
||||
Comparison.GT -> print(" > ")
|
||||
@ -109,9 +108,9 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
Comparison.LTE -> print(" <= ")
|
||||
}
|
||||
when (per.type) {
|
||||
BigDecimal::class.java -> prettyPrint(per.right as Perceivable<BigDecimal>)
|
||||
Instant::class.java -> prettyPrint(per.right as Perceivable<Instant>)
|
||||
Boolean::class.java -> prettyPrint(per.right as Perceivable<Boolean>)
|
||||
BigDecimal::class.java -> prettyPrintBigDecimal(uncheckedCast(per.right))
|
||||
Instant::class.java -> prettyPrintInstant(uncheckedCast(per.right))
|
||||
Boolean::class.java -> prettyPrintBoolean(uncheckedCast(per.right))
|
||||
}
|
||||
}
|
||||
is TerminalEvent -> print("TerminalEvent(${partyMap[per.reference.owningKey]}, \"${per.source}\")")
|
||||
@ -120,7 +119,7 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
}
|
||||
}
|
||||
|
||||
fun prettyPrint(per: Perceivable<Instant>, x: Instant? = null) {
|
||||
fun prettyPrintInstant(per: Perceivable<Instant>) {
|
||||
when (per) {
|
||||
is Const -> print("\"${per.value}\"")
|
||||
is StartDate -> print("startDate")
|
||||
@ -129,34 +128,33 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
}
|
||||
}
|
||||
|
||||
fun prettyPrint(per: Perceivable<BigDecimal>, x: BigDecimal? = null) {
|
||||
fun prettyPrintBigDecimal(per: Perceivable<BigDecimal>) {
|
||||
when (per) {
|
||||
is PerceivableOperation<BigDecimal> -> {
|
||||
prettyPrint(per.left)
|
||||
prettyPrintBigDecimal(per.left)
|
||||
when (per.op) {
|
||||
Operation.PLUS -> print(" + ")
|
||||
Operation.MINUS -> print(" - ")
|
||||
Operation.DIV -> print(" / ")
|
||||
Operation.TIMES -> print(" * ")
|
||||
else -> print(per.op)
|
||||
}
|
||||
prettyPrint(per.right)
|
||||
prettyPrintBigDecimal(per.right)
|
||||
}
|
||||
is UnaryPlus -> {
|
||||
print("(")
|
||||
prettyPrint(per.arg)
|
||||
prettyPrintBigDecimal(per.arg)
|
||||
print(".).plus()")
|
||||
}
|
||||
is Const -> print(per.value)
|
||||
is Interest -> {
|
||||
print("Interest(")
|
||||
prettyPrint(per.amount)
|
||||
prettyPrintBigDecimal(per.amount)
|
||||
print(", \"${per.dayCountConvention}\", ")
|
||||
prettyPrint(per.amount)
|
||||
prettyPrintBigDecimal(per.amount)
|
||||
print(", ")
|
||||
prettyPrint(per.start)
|
||||
prettyPrintInstant(per.start)
|
||||
print(", ")
|
||||
prettyPrint(per.end)
|
||||
prettyPrintInstant(per.end)
|
||||
print(")")
|
||||
}
|
||||
is CurrencyCross -> print("${per.foreign}/${per.domestic}")
|
||||
@ -165,7 +163,6 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
}
|
||||
|
||||
fun prettyPrint(arr: Arrangement) {
|
||||
|
||||
when (arr) {
|
||||
is Zero -> println("zero")
|
||||
is RollOut -> {
|
||||
@ -183,7 +180,7 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
is Continuation -> println("next()")
|
||||
is Obligation -> {
|
||||
print("${partyMap[arr.from.owningKey]}.gives( ${partyMap[arr.to.owningKey]}, ")
|
||||
prettyPrint(arr.amount)
|
||||
prettyPrintBigDecimal(arr.amount)
|
||||
println(", ${arr.currency})")
|
||||
}
|
||||
is Actions -> {
|
||||
@ -191,7 +188,7 @@ private class PrettyPrint(arr : Arrangement) {
|
||||
indent {
|
||||
for ((name, condition, arrangement) in arr.actions) {
|
||||
print("\"$name\".givenThat(")
|
||||
prettyPrint(condition)
|
||||
prettyPrintBoolean(condition)
|
||||
println(") {")
|
||||
indent {
|
||||
prettyPrint(arrangement)
|
||||
|
@ -36,46 +36,45 @@ class UniversalContract : Contract {
|
||||
class Split(val ratio: BigDecimal) : Commands
|
||||
}
|
||||
|
||||
fun eval(@Suppress("UNUSED_PARAMETER") tx: LedgerTransaction, expr: Perceivable<Instant>): Instant? = when (expr) {
|
||||
fun evalInstant(expr: Perceivable<Instant>): Instant? = when (expr) {
|
||||
is Const -> expr.value
|
||||
is StartDate -> null
|
||||
is EndDate -> null
|
||||
else -> throw Error("Unable to evaluate")
|
||||
}
|
||||
|
||||
fun eval(tx: LedgerTransaction, expr: Perceivable<Boolean>): Boolean = when (expr) {
|
||||
is PerceivableAnd -> eval(tx, expr.left) && eval(tx, expr.right)
|
||||
is PerceivableOr -> eval(tx, expr.left) || eval(tx, expr.right)
|
||||
fun evalBoolean(tx: LedgerTransaction, expr: Perceivable<Boolean>): Boolean = when (expr) {
|
||||
is PerceivableAnd -> evalBoolean(tx, expr.left) && evalBoolean(tx, expr.right)
|
||||
is PerceivableOr -> evalBoolean(tx, expr.left) || evalBoolean(tx, expr.right)
|
||||
is Const<Boolean> -> expr.value
|
||||
is TimePerceivable -> when (expr.cmp) {
|
||||
Comparison.LTE -> tx.timeWindow!!.fromTime!! <= eval(tx, expr.instant)
|
||||
Comparison.GTE -> tx.timeWindow!!.untilTime!! >= eval(tx, expr.instant)
|
||||
Comparison.LTE -> tx.timeWindow!!.fromTime!! <= evalInstant(expr.instant)
|
||||
Comparison.GTE -> tx.timeWindow!!.untilTime!! >= evalInstant(expr.instant)
|
||||
else -> throw NotImplementedError("eval special")
|
||||
}
|
||||
is ActorPerceivable -> tx.commands.single().signers.contains(expr.actor.owningKey)
|
||||
else -> throw NotImplementedError("eval - Boolean - " + expr.javaClass.name)
|
||||
}
|
||||
|
||||
fun eval(tx: LedgerTransaction, expr: Perceivable<BigDecimal>): BigDecimal =
|
||||
fun evalBigDecimal(tx: LedgerTransaction, expr: Perceivable<BigDecimal>): BigDecimal =
|
||||
when (expr) {
|
||||
is Const<BigDecimal> -> expr.value
|
||||
is UnaryPlus -> {
|
||||
val x = eval(tx, expr.arg)
|
||||
val x = evalBigDecimal(tx, expr.arg)
|
||||
if (x > BigDecimal.ZERO)
|
||||
x
|
||||
else
|
||||
BigDecimal.ZERO
|
||||
}
|
||||
is PerceivableOperation -> {
|
||||
val l = eval(tx, expr.left)
|
||||
val r = eval(tx, expr.right)
|
||||
val l = evalBigDecimal(tx, expr.left)
|
||||
val r = evalBigDecimal(tx, expr.right)
|
||||
|
||||
when (expr.op) {
|
||||
Operation.DIV -> l / r
|
||||
Operation.MINUS -> l - r
|
||||
Operation.PLUS -> l + r
|
||||
Operation.TIMES -> l * r
|
||||
else -> throw NotImplementedError("eval - amount - operation " + expr.op)
|
||||
}
|
||||
}
|
||||
is Fixing -> {
|
||||
@ -83,8 +82,8 @@ class UniversalContract : Contract {
|
||||
0.0.bd
|
||||
}
|
||||
is Interest -> {
|
||||
val a = eval(tx, expr.amount)
|
||||
val i = eval(tx, expr.interest)
|
||||
val a = evalBigDecimal(tx, expr.amount)
|
||||
val i = evalBigDecimal(tx, expr.interest)
|
||||
|
||||
//TODO
|
||||
|
||||
@ -95,7 +94,7 @@ class UniversalContract : Contract {
|
||||
|
||||
fun validateImmediateTransfers(tx: LedgerTransaction, arrangement: Arrangement): Arrangement = when (arrangement) {
|
||||
is Obligation -> {
|
||||
val amount = eval(tx, arrangement.amount)
|
||||
val amount = evalBigDecimal(tx, arrangement.amount)
|
||||
requireThat { "transferred quantity is non-negative" using (amount >= BigDecimal.ZERO) }
|
||||
Obligation(const(amount), arrangement.currency, arrangement.from, arrangement.to)
|
||||
}
|
||||
@ -210,7 +209,7 @@ class UniversalContract : Contract {
|
||||
"action must have a time-window" using (tx.timeWindow != null)
|
||||
// "action must be authorized" by (cmd.signers.any { action.actors.any { party -> party.owningKey == it } })
|
||||
// todo perhaps merge these two requirements?
|
||||
"condition must be met" using (eval(tx, action.condition))
|
||||
"condition must be met" using evalBoolean(tx, action.condition)
|
||||
}
|
||||
|
||||
// verify that any resulting transfers can be resolved
|
||||
@ -287,7 +286,7 @@ class UniversalContract : Contract {
|
||||
perceivable.dayCountConvention, replaceFixing(tx, perceivable.interest, fixings, unusedFixings),
|
||||
perceivable.start, perceivable.end))
|
||||
is Fixing -> {
|
||||
val dt = eval(tx, perceivable.date)
|
||||
val dt = evalInstant(perceivable.date)
|
||||
if (dt != null && fixings.containsKey(FixOf(perceivable.source, dt.toLocalDate(), perceivable.tenor))) {
|
||||
unusedFixings.remove(FixOf(perceivable.source, dt.toLocalDate(), perceivable.tenor))
|
||||
uncheckedCast(Const(fixings[FixOf(perceivable.source, dt.toLocalDate(), perceivable.tenor)]!!))
|
||||
|
@ -14,13 +14,12 @@ class IRS {
|
||||
@Rule
|
||||
@JvmField
|
||||
val testSerialization = SerializationEnvironmentRule()
|
||||
val TEST_TX_TIME_1: Instant get() = Instant.parse("2017-09-02T12:00:00.00Z")
|
||||
|
||||
val notional = 50.M
|
||||
val currency = EUR
|
||||
|
||||
val tradeDate: LocalDate = LocalDate.of(2016, 9, 1)
|
||||
private val testTxTime1: Instant = Instant.parse("2017-09-02T12:00:00.00Z")
|
||||
private val notional = 50.M
|
||||
private val currency = EUR
|
||||
|
||||
private val tradeDate: LocalDate = LocalDate.of(2016, 9, 1)
|
||||
|
||||
/*
|
||||
|
||||
@ -33,7 +32,7 @@ class IRS {
|
||||
|
||||
*/
|
||||
|
||||
val contractInitial = arrange {
|
||||
private val contractInitial = arrange {
|
||||
rollOut("2016-09-01".ld, "2018-09-01".ld, Frequency.Quarterly) {
|
||||
actions {
|
||||
(acmeCorp or highStreetBank) may {
|
||||
@ -52,7 +51,8 @@ class IRS {
|
||||
}
|
||||
}
|
||||
}
|
||||
val contractAfterFixingFirst = arrange {
|
||||
|
||||
private val contractAfterFixingFirst = arrange {
|
||||
actions {
|
||||
(acmeCorp or highStreetBank) may {
|
||||
val floating = interest(notional, "act/365", 1.0.bd, "2016-09-01", "2016-12-01")
|
||||
@ -83,15 +83,15 @@ class IRS {
|
||||
rollOut("2016-12-01".ld, "2018-09-01".ld, Frequency.Quarterly) {
|
||||
actions {
|
||||
(acmeCorp or highStreetBank) may {
|
||||
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
|
||||
val fixed = interest(notional, "act/365", 0.5.bd, start, end)
|
||||
val nextFloating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
|
||||
val nextFixed = interest(notional, "act/365", 0.5.bd, start, end)
|
||||
|
||||
"pay floating" anytime {
|
||||
highStreetBank.owes(acmeCorp, floating - fixed, currency)
|
||||
highStreetBank.owes(acmeCorp, nextFloating - nextFixed, currency)
|
||||
next()
|
||||
}
|
||||
"pay fixed" anytime {
|
||||
highStreetBank.owes(acmeCorp, fixed - floating, currency)
|
||||
highStreetBank.owes(acmeCorp, nextFixed - nextFloating, currency)
|
||||
next()
|
||||
}
|
||||
}
|
||||
@ -102,7 +102,7 @@ class IRS {
|
||||
}
|
||||
}
|
||||
|
||||
val contractAfterExecutionFirst = arrange {
|
||||
private val contractAfterExecutionFirst = arrange {
|
||||
rollOut("2016-12-01".ld, "2018-09-01".ld, Frequency.Quarterly) {
|
||||
actions {
|
||||
(acmeCorp or highStreetBank) may {
|
||||
@ -122,19 +122,20 @@ class IRS {
|
||||
}
|
||||
}
|
||||
|
||||
val paymentFirst = arrange { highStreetBank.owes(acmeCorp, 250.K, EUR) }
|
||||
private val paymentFirst = arrange { highStreetBank.owes(acmeCorp, 250.K, EUR) }
|
||||
|
||||
val stateInitial = UniversalContract.State(listOf(DUMMY_NOTARY), contractInitial)
|
||||
private val stateInitial = UniversalContract.State(listOf(DUMMY_NOTARY), contractInitial)
|
||||
|
||||
val stateAfterFixingFirst = UniversalContract.State(listOf(DUMMY_NOTARY), contractAfterFixingFirst)
|
||||
val stateAfterExecutionFirst = UniversalContract.State(listOf(DUMMY_NOTARY), contractAfterExecutionFirst)
|
||||
private val stateAfterFixingFirst = UniversalContract.State(listOf(DUMMY_NOTARY), contractAfterFixingFirst)
|
||||
private val stateAfterExecutionFirst = UniversalContract.State(listOf(DUMMY_NOTARY), contractAfterExecutionFirst)
|
||||
|
||||
private val statePaymentFirst = UniversalContract.State(listOf(DUMMY_NOTARY), paymentFirst)
|
||||
|
||||
val statePaymentFirst = UniversalContract.State(listOf(DUMMY_NOTARY), paymentFirst)
|
||||
@Test
|
||||
fun issue() {
|
||||
transaction {
|
||||
output(UNIVERSAL_PROGRAM_ID, stateInitial)
|
||||
timeWindow(TEST_TX_TIME_1)
|
||||
timeWindow(testTxTime1)
|
||||
|
||||
tweak {
|
||||
command(acmeCorp.owningKey, UniversalContract.Commands.Issue())
|
||||
@ -150,7 +151,7 @@ class IRS {
|
||||
transaction {
|
||||
input(UNIVERSAL_PROGRAM_ID, stateInitial)
|
||||
output(UNIVERSAL_PROGRAM_ID, stateAfterFixingFirst)
|
||||
timeWindow(TEST_TX_TIME_1)
|
||||
timeWindow(testTxTime1)
|
||||
|
||||
tweak {
|
||||
command(highStreetBank.owningKey, UniversalContract.Commands.Action("some undefined name"))
|
||||
@ -190,7 +191,7 @@ class IRS {
|
||||
input(UNIVERSAL_PROGRAM_ID, stateAfterFixingFirst)
|
||||
output(UNIVERSAL_PROGRAM_ID, stateAfterExecutionFirst)
|
||||
output(UNIVERSAL_PROGRAM_ID, statePaymentFirst)
|
||||
timeWindow(TEST_TX_TIME_1)
|
||||
timeWindow(testTxTime1)
|
||||
|
||||
tweak {
|
||||
command(highStreetBank.owningKey, UniversalContract.Commands.Action("some undefined name"))
|
||||
|
Reference in New Issue
Block a user