2015-11-08 12:34:24 +00:00
|
|
|
import contracts.Cash
|
|
|
|
import contracts.InsufficientBalanceException
|
2015-11-06 13:46:19 +00:00
|
|
|
import core.*
|
2015-11-03 12:37:19 +00:00
|
|
|
import org.junit.Test
|
2015-11-03 16:38:11 +00:00
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFailsWith
|
2015-11-03 12:37:19 +00:00
|
|
|
|
2015-11-03 12:49:28 +00:00
|
|
|
// TODO: Some basic invariants should be enforced by the platform before contract execution:
|
|
|
|
// 1. No duplicate input states
|
|
|
|
// 2. There must be at least one input state (note: not "one of the type the contract wants")
|
|
|
|
|
2015-11-03 12:37:19 +00:00
|
|
|
class CashTests {
|
2015-11-08 12:34:24 +00:00
|
|
|
val inState = Cash.State(
|
2015-11-06 15:35:51 +00:00
|
|
|
deposit = InstitutionReference(MEGA_CORP, OpaqueBytes.of(1)),
|
2015-11-03 12:37:19 +00:00
|
|
|
amount = 1000.DOLLARS,
|
|
|
|
owner = DUMMY_PUBKEY_1
|
|
|
|
)
|
|
|
|
val outState = inState.copy(owner = DUMMY_PUBKEY_2)
|
2015-11-08 12:34:24 +00:00
|
|
|
val contract = Cash
|
2015-11-03 12:37:19 +00:00
|
|
|
|
2015-11-08 12:34:24 +00:00
|
|
|
fun Cash.State.editInstitution(institution: Institution) = copy(deposit = deposit.copy(institution = institution))
|
|
|
|
fun Cash.State.editDepositRef(ref: Byte) = copy(deposit = deposit.copy(reference = OpaqueBytes.of(ref)))
|
2015-11-03 17:05:21 +00:00
|
|
|
|
2015-11-03 12:37:19 +00:00
|
|
|
@Test
|
|
|
|
fun trivial() {
|
2015-11-03 12:49:28 +00:00
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
contract `fails requirement` "the amounts balance"
|
|
|
|
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState.copy(amount = 2000.DOLLARS )}
|
|
|
|
contract `fails requirement` "the amounts balance"
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState }
|
|
|
|
// No command arguments
|
|
|
|
contract `fails requirement` "the owning keys are the same as the signing keys"
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_2) { Cash.Commands.Move() }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract `fails requirement` "the owning keys are the same as the signing keys"
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState }
|
2015-11-03 17:05:21 +00:00
|
|
|
output { outState.editInstitution(MINI_CORP) }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract `fails requirement` "no output states are unaccounted for"
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
// Simple reallocation works.
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract.accepts()
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testMergeSplit() {
|
|
|
|
// Splitting value works.
|
|
|
|
transaction {
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 12:49:28 +00:00
|
|
|
for (i in 1..4) output { inState.copy(amount = inState.amount / 4) }
|
|
|
|
contract.accepts()
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
// Merging 4 inputs into 2 outputs works.
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
for (i in 1..4) input { inState.copy(amount = inState.amount / 4) }
|
|
|
|
output { inState.copy(amount = inState.amount / 2) }
|
|
|
|
output { inState.copy(amount = inState.amount / 2) }
|
|
|
|
contract.accepts()
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
// Merging 2 inputs into 1 works.
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
2015-11-03 12:49:28 +00:00
|
|
|
input { inState.copy(amount = inState.amount / 2) }
|
|
|
|
input { inState.copy(amount = inState.amount / 2) }
|
|
|
|
output { inState }
|
|
|
|
contract.accepts()
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun zeroSizedInputs() {
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
input { inState.copy(amount = 0.DOLLARS) }
|
|
|
|
contract `fails requirement` "zero sized inputs"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun trivialMismatches() {
|
|
|
|
// Can't change issuer.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
output { outState.editInstitution(MINI_CORP) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MegaCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
// Can't change deposit reference when splitting.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
output { outState.editDepositRef(0).copy(amount = inState.amount / 2) }
|
|
|
|
output { outState.editDepositRef(1).copy(amount = inState.amount / 2) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "for deposit [01] at issuer MegaCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
// Can't mix currencies.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
output { outState.copy(amount = 800.DOLLARS) }
|
|
|
|
output { outState.copy(amount = 200.POUNDS) }
|
|
|
|
contract `fails requirement` "all outputs use the currency of the inputs"
|
|
|
|
}
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
input {
|
|
|
|
inState.copy(
|
|
|
|
amount = 150.POUNDS,
|
|
|
|
owner = DUMMY_PUBKEY_2
|
|
|
|
)
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState.copy(amount = 1150.DOLLARS) }
|
|
|
|
contract `fails requirement` "all inputs use the same currency"
|
|
|
|
}
|
2015-11-03 15:09:02 +00:00
|
|
|
// Can't have superfluous input states from different issuers.
|
2015-11-03 12:49:28 +00:00
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
input { inState.editInstitution(MINI_CORP) }
|
2015-11-03 12:49:28 +00:00
|
|
|
output { outState }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MiniCorp the amounts balance"
|
|
|
|
}
|
|
|
|
// Can't combine two different deposits at the same issuer.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
input { inState.editDepositRef(3) }
|
|
|
|
output { outState.copy(amount = inState.amount * 2).editDepositRef(3) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "for deposit [01]"
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun exitLedger() {
|
2015-11-03 12:49:28 +00:00
|
|
|
// Single input/output straightforward case.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
output { outState.copy(amount = inState.amount - 200.DOLLARS) }
|
|
|
|
|
2015-11-03 12:37:19 +00:00
|
|
|
transaction {
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(MEGA_CORP_KEY) { Cash.Commands.Exit(100.DOLLARS) }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract `fails requirement` "the amounts balance"
|
|
|
|
}
|
2015-11-03 12:37:19 +00:00
|
|
|
|
2015-11-03 12:49:28 +00:00
|
|
|
transaction {
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(MEGA_CORP_KEY) { Cash.Commands.Exit(200.DOLLARS) }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract `fails requirement` "the owning keys are the same as the signing keys" // No move command.
|
2015-11-03 12:37:19 +00:00
|
|
|
|
|
|
|
transaction {
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract.accepts()
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 12:49:28 +00:00
|
|
|
// Multi-issuer case.
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
input { inState.editInstitution(MINI_CORP) }
|
2015-11-03 12:49:28 +00:00
|
|
|
|
2015-11-03 17:05:21 +00:00
|
|
|
output { inState.copy(amount = inState.amount - 200.DOLLARS).editInstitution(MINI_CORP) }
|
2015-11-03 12:49:28 +00:00
|
|
|
output { inState.copy(amount = inState.amount - 200.DOLLARS) }
|
|
|
|
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 12:49:28 +00:00
|
|
|
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MegaCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(MEGA_CORP_KEY) { Cash.Commands.Exit(200.DOLLARS) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MiniCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(MINI_CORP_KEY) { Cash.Commands.Exit(200.DOLLARS) }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract.accepts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun multiIssuer() {
|
|
|
|
transaction {
|
|
|
|
// Gather 2000 dollars from two different issuers.
|
|
|
|
input { inState }
|
2015-11-03 17:05:21 +00:00
|
|
|
input { inState.editInstitution(MINI_CORP) }
|
2015-11-03 12:49:28 +00:00
|
|
|
|
|
|
|
// Can't merge them together.
|
|
|
|
transaction {
|
|
|
|
output { inState.copy(owner = DUMMY_PUBKEY_2, amount = 2000.DOLLARS) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MegaCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
// Missing MiniCorp deposit
|
|
|
|
transaction {
|
|
|
|
output { inState.copy(owner = DUMMY_PUBKEY_2) }
|
|
|
|
output { inState.copy(owner = DUMMY_PUBKEY_2) }
|
2015-11-03 15:09:02 +00:00
|
|
|
contract `fails requirement` "at issuer MegaCorp the amounts balance"
|
2015-11-03 12:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This works.
|
|
|
|
output { inState.copy(owner = DUMMY_PUBKEY_2) }
|
2015-11-03 17:05:21 +00:00
|
|
|
output { inState.copy(owner = DUMMY_PUBKEY_2).editInstitution(MINI_CORP) }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(DUMMY_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 12:49:28 +00:00
|
|
|
contract.accepts()
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction {
|
|
|
|
input { inState }
|
|
|
|
input { inState }
|
|
|
|
}
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|
2015-11-03 16:38:11 +00:00
|
|
|
|
2015-11-03 18:03:03 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Spend crafting
|
|
|
|
|
2015-11-03 16:38:11 +00:00
|
|
|
val OUR_PUBKEY_1 = DUMMY_PUBKEY_1
|
|
|
|
val THEIR_PUBKEY_1 = DUMMY_PUBKEY_2
|
|
|
|
val WALLET = listOf(
|
2015-11-08 12:34:24 +00:00
|
|
|
Cash.State(InstitutionReference(MEGA_CORP, OpaqueBytes.of(1)), 100.DOLLARS, OUR_PUBKEY_1),
|
|
|
|
Cash.State(InstitutionReference(MEGA_CORP, OpaqueBytes.of(1)), 400.DOLLARS, OUR_PUBKEY_1),
|
|
|
|
Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(1)), 80.DOLLARS, OUR_PUBKEY_1),
|
|
|
|
Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(2)), 80.SWISS_FRANCS, OUR_PUBKEY_1)
|
2015-11-03 16:38:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun craftSimpleDirectSpend() {
|
|
|
|
assertEquals(
|
|
|
|
transaction {
|
|
|
|
input { WALLET[0] }
|
|
|
|
output { WALLET[0].copy(owner = THEIR_PUBKEY_1) }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(OUR_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 16:38:11 +00:00
|
|
|
},
|
|
|
|
contract.craftSpend(100.DOLLARS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun craftSimpleSpendWithChange() {
|
|
|
|
assertEquals(
|
|
|
|
transaction {
|
|
|
|
input { WALLET[0] }
|
|
|
|
output { WALLET[0].copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS) }
|
|
|
|
output { WALLET[0].copy(owner = OUR_PUBKEY_1, amount = 90.DOLLARS) }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(OUR_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 16:38:11 +00:00
|
|
|
},
|
|
|
|
contract.craftSpend(10.DOLLARS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun craftSpendWithTwoInputs() {
|
|
|
|
assertEquals(
|
|
|
|
transaction {
|
|
|
|
input { WALLET[0] }
|
|
|
|
input { WALLET[1] }
|
|
|
|
output { WALLET[0].copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS) }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(OUR_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 16:38:11 +00:00
|
|
|
},
|
|
|
|
contract.craftSpend(500.DOLLARS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun craftSpendMixedDeposits() {
|
|
|
|
assertEquals(
|
|
|
|
transaction {
|
|
|
|
input { WALLET[0] }
|
|
|
|
input { WALLET[1] }
|
|
|
|
input { WALLET[2] }
|
|
|
|
output { WALLET[0].copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS) }
|
|
|
|
output { WALLET[2].copy(owner = THEIR_PUBKEY_1) }
|
2015-11-08 12:34:24 +00:00
|
|
|
arg(OUR_PUBKEY_1) { Cash.Commands.Move() }
|
2015-11-03 16:38:11 +00:00
|
|
|
},
|
|
|
|
contract.craftSpend(580.DOLLARS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun craftSpendInsufficientBalance() {
|
|
|
|
assertFailsWith(InsufficientBalanceException::class) {
|
|
|
|
contract.craftSpend(1000.DOLLARS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
}
|
|
|
|
assertFailsWith(InsufficientBalanceException::class) {
|
|
|
|
contract.craftSpend(81.SWISS_FRANCS, THEIR_PUBKEY_1, WALLET)
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 12:37:19 +00:00
|
|
|
}
|