Minor: introduce Institution.ref() to get an InstitutionReference

This commit is contained in:
Mike Hearn 2015-11-27 14:49:08 +01:00
parent 88793644c8
commit de40a2082d
6 changed files with 15 additions and 10 deletions

View File

@ -33,6 +33,9 @@ data class StateAndRef<T : ContractState>(val state: T, val ref: ContractStateRe
/** An Institution is well known (name, pubkey) pair. In a real system this would probably be an X.509 certificate. */
data class Institution(val name: String, val owningKey: PublicKey) : SerializeableWithKryo {
override fun toString() = name
fun ref(bytes: OpaqueBytes) = InstitutionReference(this, bytes)
fun ref(vararg bytes: Byte) = ref(OpaqueBytes.of(*bytes))
}
/**

View File

@ -12,7 +12,7 @@ import kotlin.test.assertTrue
class CashTests {
val inState = Cash.State(
deposit = InstitutionReference(MEGA_CORP, OpaqueBytes.of(1)),
deposit = MEGA_CORP.ref(1),
amount = 1000.DOLLARS,
owner = DUMMY_PUBKEY_1
)
@ -79,7 +79,7 @@ class CashTests {
Cash.State(
amount = 1000.DOLLARS,
owner = DUMMY_PUBKEY_1,
deposit = InstitutionReference(MINI_CORP, OpaqueBytes.of(12, 34))
deposit = MINI_CORP.ref(12, 34)
)
}
tweak {
@ -91,7 +91,7 @@ class CashTests {
}
val ptx = PartialTransaction()
Cash().craftIssue(ptx, 100.DOLLARS, InstitutionReference(MINI_CORP, OpaqueBytes.of(12, 34)), owner = DUMMY_PUBKEY_1)
Cash().craftIssue(ptx, 100.DOLLARS, MINI_CORP.ref(12,34), owner = DUMMY_PUBKEY_1)
assertTrue(ptx.inputStates().isEmpty())
val s = ptx.outputStates()[0] as Cash.State
assertEquals(100.DOLLARS, s.amount)
@ -267,7 +267,7 @@ class CashTests {
fun multiCurrency() {
// Check we can do an atomic currency trade tx.
transaction {
val pounds = Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(3, 4, 5)), 658.POUNDS, DUMMY_PUBKEY_2)
val pounds = Cash.State(MINI_CORP.ref(3, 4, 5), 658.POUNDS, DUMMY_PUBKEY_2)
input { inState `owned by` DUMMY_PUBKEY_1 }
input { pounds }
output { inState `owned by` DUMMY_PUBKEY_2 }
@ -287,7 +287,7 @@ class CashTests {
fun makeCash(amount: Amount, corp: Institution, depositRef: Byte = 1) =
StateAndRef(
Cash.State(InstitutionReference(corp, OpaqueBytes.of(depositRef)), amount, OUR_PUBKEY_1),
Cash.State(corp.ref(depositRef), amount, OUR_PUBKEY_1),
ContractStateRef(SecureHash.randomSHA256(), Random().nextInt(32))
)

View File

@ -1,13 +1,15 @@
package contracts
import core.*
import core.Amount
import core.DOLLARS
import core.days
import core.testutils.*
import org.junit.Test
import java.time.Instant
class CommercialPaperTests {
val PAPER_1 = CommercialPaper.State(
issuance = InstitutionReference(MEGA_CORP, OpaqueBytes.of(123)),
issuance = MEGA_CORP.ref(123),
owner = MEGA_CORP_KEY,
faceValue = 1000.DOLLARS,
maturityDate = TEST_TX_TIME + 7.days

View File

@ -8,7 +8,7 @@ import kotlin.test.assertFailsWith
import kotlin.test.assertNotEquals
class TransactionGroupTests {
val A_THOUSAND_POUNDS = Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(1, 2, 3)), 1000.POUNDS, MINI_CORP_KEY)
val A_THOUSAND_POUNDS = Cash.State(MINI_CORP.ref(1, 2, 3), 1000.POUNDS, MINI_CORP_KEY)
@Test
fun success() {

View File

@ -13,7 +13,7 @@ import kotlin.test.assertFailsWith
class TransactionSerializationTests {
// Simple TX that takes 1000 pounds from me and sends 600 to someone else (with 400 change).
// It refers to a fake TX/state that we don't bother creating here.
val depositRef = InstitutionReference(MINI_CORP, OpaqueBytes.of(1))
val depositRef = MINI_CORP.ref(1)
val outputState = Cash.State(depositRef, 600.POUNDS, DUMMY_PUBKEY_1)
val changeState = Cash.State(depositRef, 400.POUNDS, TestUtils.keypair.public)

View File

@ -67,7 +67,7 @@ val TEST_PROGRAM_MAP: Map<SecureHash, Contract> = mapOf(
infix fun Cash.State.`owned by`(owner: PublicKey) = this.copy(owner = owner)
infix fun CommercialPaper.State.`owned by`(owner: PublicKey) = this.copy(owner = owner)
// Allows you to write 100.DOLLARS.CASH
val Amount.CASH: Cash.State get() = Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(1,2,3)), this, NullPublicKey)
val Amount.CASH: Cash.State get() = Cash.State(MINI_CORP.ref(1,2,3), this, NullPublicKey)
class LabeledOutput(val label: String?, val state: ContractState) {
override fun toString() = state.toString() + (if (label != null) " ($label)" else "")