Fix build.

This commit is contained in:
Mike Hearn 2016-02-10 16:28:22 +01:00
parent ea2b18eb41
commit 5e30e1b94b
4 changed files with 50 additions and 97 deletions

View File

@ -26,7 +26,7 @@ import static kotlin.collections.CollectionsKt.*;
*
*/
public class JavaCommercialPaper implements Contract {
public static core.SecureHash JCP_PROGRAM_ID = SecureHash.Companion.sha256("java commercial paper (this should be a bytecode hash)");
public static SecureHash JCP_PROGRAM_ID = SecureHash.Companion.sha256("java commercial paper (this should be a bytecode hash)");
public static class State implements ContractState, ICommercialPaperState {
private PartyReference issuance;
@ -201,7 +201,7 @@ public class JavaCommercialPaper implements Contract {
Amount received = CashKt.sumCashBy(tx.getOutStates(), input.getOwner());
if (! received.equals(input.getFaceValue()))
throw new IllegalStateException(String.format("Failed Requirement: received amount equals the face value"));
throw new IllegalStateException("Failed Requirement: received amount equals the face value");
if (time.isBefore(input.getMaturityDate()))
throw new IllegalStateException("Failed requirement: the paper must have matured");
if (!input.getFaceValue().equals(received))
@ -220,21 +220,20 @@ public class JavaCommercialPaper implements Contract {
return SecureHash.Companion.sha256("https://en.wikipedia.org/wiki/Commercial_paper");
}
public TransactionBuilder craftIssue(@NotNull PartyReference issuance, @NotNull Amount faceValue, @Nullable Instant maturityDate) {
public TransactionBuilder generateIssue(@NotNull PartyReference issuance, @NotNull Amount faceValue, @Nullable Instant maturityDate) {
State state = new State(issuance,issuance.getParty().getOwningKey(), faceValue, maturityDate);
return new TransactionBuilder().withItems(state, new Command( new Commands.Issue(), issuance.getParty().getOwningKey()));
}
public void craftRedeem(TransactionBuilder tx, StateAndRef<State> paper, List<StateAndRef<Cash.State>> wallet) throws InsufficientBalanceException {
public void generateRedeem(TransactionBuilder tx, StateAndRef<State> paper, List<StateAndRef<Cash.State>> wallet) throws InsufficientBalanceException {
new Cash().craftSpend(tx, paper.getState().getFaceValue(), paper.getState().getOwner(), wallet, null);
tx.addInputState(paper.getRef());
tx.addCommand(new Command( new Commands.Redeem(), paper.getState().getOwner()));
}
public void craftMove(TransactionBuilder tx, StateAndRef<State> paper, PublicKey newOwner) {
public void generateMove(TransactionBuilder tx, StateAndRef<State> paper, PublicKey newOwner) {
tx.addInputState(paper.getRef());
tx.addOutputState(new State(paper.getState().getIssuance(), newOwner, paper.getState().getFaceValue(), paper.getState().getMaturityDate()));
tx.addCommand(new Command(new Commands.Move(), paper.getState().getOwner()));
}
}

View File

@ -12,6 +12,8 @@ import core.*
import core.node.TimestampingError
import core.testutils.*
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.time.Clock
import java.time.Instant
import java.time.ZoneOffset
@ -19,16 +21,46 @@ import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
interface ICommercialPaperTestTemplate {
open fun getPaper() : ICommercialPaperState
open fun getIssueCommand() : CommandData
open fun getRedeemCommand() : CommandData
open fun getMoveCommand() : CommandData
open fun getPaper(): ICommercialPaperState
open fun getIssueCommand(): CommandData
open fun getRedeemCommand(): CommandData
open fun getMoveCommand(): CommandData
}
open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTemplate) {
class JavaCommercialPaperTest() : ICommercialPaperTestTemplate {
override fun getPaper(): ICommercialPaperState = JavaCommercialPaper.State(
MEGA_CORP.ref(123),
MEGA_CORP_PUBKEY,
1000.DOLLARS,
TEST_TX_TIME + 7.days
)
val thisTest = templateToTest
val PAPER_1 = thisTest.getPaper()
override fun getIssueCommand(): CommandData = JavaCommercialPaper.Commands.Issue()
override fun getRedeemCommand(): CommandData = JavaCommercialPaper.Commands.Redeem()
override fun getMoveCommand(): CommandData = JavaCommercialPaper.Commands.Move()
}
class KotlinCommercialPaperTest() : ICommercialPaperTestTemplate {
override fun getPaper() : ICommercialPaperState = CommercialPaper.State(
issuance = MEGA_CORP.ref(123),
owner = MEGA_CORP_PUBKEY,
faceValue = 1000.DOLLARS,
maturityDate = TEST_TX_TIME + 7.days
)
override fun getIssueCommand(): CommandData = CommercialPaper.Commands.Issue()
override fun getRedeemCommand(): CommandData = CommercialPaper.Commands.Redeem()
override fun getMoveCommand(): CommandData = CommercialPaper.Commands.Move()
}
@RunWith(Parameterized::class)
class CommercialPaperTestsGeneric {
companion object {
@Parameterized.Parameters @JvmStatic
fun data() = listOf(JavaCommercialPaperTest(), KotlinCommercialPaperTest())
}
@Parameterized.Parameter
lateinit var thisTest: ICommercialPaperTestTemplate
@Test
fun ok() {
@ -44,7 +76,7 @@ open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTempl
fun `key mismatch at issue`() {
transactionGroup {
transaction {
output { PAPER_1 }
output { thisTest.getPaper() }
arg(DUMMY_PUBKEY_1) { thisTest.getIssueCommand() }
timestamp(TEST_TX_TIME)
}
@ -57,7 +89,7 @@ open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTempl
fun `face value is not zero`() {
transactionGroup {
transaction {
output { PAPER_1.withFaceValue(0.DOLLARS) }
output { thisTest.getPaper().withFaceValue(0.DOLLARS) }
arg(MEGA_CORP_PUBKEY) { thisTest.getIssueCommand() }
timestamp(TEST_TX_TIME)
}
@ -70,7 +102,7 @@ open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTempl
fun `maturity date not in the past`() {
transactionGroup {
transaction {
output { PAPER_1.withMaturityDate(TEST_TX_TIME - 10.days) }
output { thisTest.getPaper().withMaturityDate(TEST_TX_TIME - 10.days) }
arg(MEGA_CORP_PUBKEY) { thisTest.getIssueCommand() }
timestamp(TEST_TX_TIME)
}
@ -105,11 +137,11 @@ open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTempl
fun `issue cannot replace an existing state`() {
transactionGroup {
roots {
transaction(PAPER_1 label "paper")
transaction(thisTest.getPaper() label "paper")
}
transaction {
input("paper")
output { PAPER_1 }
output { thisTest.getPaper() }
arg(MEGA_CORP_PUBKEY) { thisTest.getIssueCommand() }
timestamp(TEST_TX_TIME)
}
@ -203,7 +235,7 @@ open class CommercialPaperTestsGeneric(templateToTest: ICommercialPaperTestTempl
// Some CP is issued onto the ledger by MegaCorp.
transaction("Issuance") {
output("paper") { PAPER_1 }
output("paper") { thisTest.getPaper() }
arg(MEGA_CORP_PUBKEY) { thisTest.getIssueCommand() }
timestamp(TEST_TX_TIME)
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2015 Distributed Ledger Group LLC. Distributed as Licensed Company IP to DLG Group Members
* pursuant to the August 7, 2015 Advisory Services Agreement and subject to the Company IP License terms
* set forth therein.
*
* All other rights reserved.
*/
package contracts
import core.CommandData
import core.DOLLARS
import core.days
import core.testutils.MEGA_CORP
import core.testutils.MEGA_CORP_PUBKEY
import core.testutils.TEST_TX_TIME
fun getJavaCommericalPaper() : ICommercialPaperState {
return JavaCommercialPaper.State(
MEGA_CORP.ref(123),
MEGA_CORP_PUBKEY,
1000.DOLLARS,
TEST_TX_TIME + 7.days
)
}
open class JavaCommercialPaperTest() : ICommercialPaperTestTemplate {
override fun getPaper() : ICommercialPaperState = getJavaCommericalPaper()
override fun getIssueCommand() : CommandData = JavaCommercialPaper.Commands.Issue()
override fun getRedeemCommand() : CommandData = JavaCommercialPaper.Commands.Redeem()
override fun getMoveCommand() : CommandData = JavaCommercialPaper.Commands.Move()
}
class CommercialPaperTestsJava() : CommercialPaperTestsGeneric(JavaCommercialPaperTest()) { }
fun main(args: Array<String>) {
CommercialPaperTestsJava().trade().visualise()
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2015 Distributed Ledger Group LLC. Distributed as Licensed Company IP to DLG Group Members
* pursuant to the August 7, 2015 Advisory Services Agreement and subject to the Company IP License terms
* set forth therein.
*
* All other rights reserved.
*/
package contracts
import core.CommandData
import core.DOLLARS
import core.days
import core.testutils.MEGA_CORP
import core.testutils.MEGA_CORP_PUBKEY
import core.testutils.TEST_TX_TIME
fun getKotlinCommercialPaper() : ICommercialPaperState {
return CommercialPaper.State(
issuance = MEGA_CORP.ref(123),
owner = MEGA_CORP_PUBKEY,
faceValue = 1000.DOLLARS,
maturityDate = TEST_TX_TIME + 7.days
)
}
open class KotlinCommercialPaperTest() : ICommercialPaperTestTemplate {
override fun getPaper() : ICommercialPaperState = getKotlinCommercialPaper()
override fun getIssueCommand() : CommandData = CommercialPaper.Commands.Issue()
override fun getRedeemCommand() : CommandData = CommercialPaper.Commands.Redeem()
override fun getMoveCommand() : CommandData = CommercialPaper.Commands.Move()
}
class CommercialPaperTestsKotlin() : CommercialPaperTestsGeneric( KotlinCommercialPaperTest()) { }
fun main(args: Array<String>) {
CommercialPaperTestsKotlin().trade().visualise()
}