Minor: some more renamings for concision and consistency

This commit is contained in:
Mike Hearn 2016-02-05 17:40:36 +01:00
parent 6f3b07a600
commit 8d1b318370
7 changed files with 46 additions and 43 deletions

View File

@ -87,7 +87,7 @@ object TwoPartyTradeProtocol {
val wtx: WireTransaction = partialTX.txBits.deserialize()
requireThat {
"transaction sends us the right amount of cash" by (wtx.outputStates.sumCashBy(myKeyPair.public) == price)
"transaction sends us the right amount of cash" by (wtx.outputs.sumCashBy(myKeyPair.public) == price)
// There are all sorts of funny games a malicious secondary might play here, we should fix them:
//
// - This tx may attempt to send some assets we aren't intending to sell to the secondary, if

View File

@ -38,8 +38,8 @@ class TransactionGroup(val transactions: Set<LedgerTransaction>, val nonVerified
val resolved = HashSet<TransactionForVerification>(transactions.size)
for (tx in transactions) {
val inputs = ArrayList<ContractState>(tx.inStateRefs.size)
for (ref in tx.inStateRefs) {
val inputs = ArrayList<ContractState>(tx.inputs.size)
for (ref in tx.inputs) {
val conflict = refToConsumingTXMap[ref]
if (conflict != null)
throw TransactionConflictException(ref, tx, conflict)
@ -48,9 +48,9 @@ class TransactionGroup(val transactions: Set<LedgerTransaction>, val nonVerified
// Look up the connecting transaction.
val ltx = hashToTXMap[ref.txhash]?.single() ?: throw TransactionResolutionException(ref.txhash)
// Look up the output in that transaction by index.
inputs.add(ltx.outStates[ref.index])
inputs.add(ltx.outputs[ref.index])
}
resolved.add(TransactionForVerification(inputs, tx.outStates, tx.commands, tx.hash))
resolved.add(TransactionForVerification(inputs, tx.outputs, tx.commands, tx.hash))
}
for (tx in resolved)

View File

@ -54,22 +54,22 @@ import java.util.*
*/
/** Transaction ready for serialisation, without any signatures attached. */
data class WireTransaction(val inputStates: List<StateRef>,
val outputStates: List<ContractState>,
data class WireTransaction(val inputs: List<StateRef>,
val outputs: List<ContractState>,
val commands: List<Command>) {
fun toLedgerTransaction(identityService: IdentityService, originalHash: SecureHash): LedgerTransaction {
val authenticatedArgs = commands.map {
val institutions = it.pubkeys.mapNotNull { pk -> identityService.partyFromKey(pk) }
AuthenticatedObject(it.pubkeys, institutions, it.data)
}
return LedgerTransaction(inputStates, outputStates, authenticatedArgs, originalHash)
return LedgerTransaction(inputs, outputs, authenticatedArgs, originalHash)
}
override fun toString(): String {
val buf = StringBuilder()
buf.appendln("Transaction:")
for (input in inputStates) buf.appendln("${Emoji.rightArrow}INPUT: $input")
for (output in outputStates) buf.appendln("${Emoji.leftArrow}OUTPUT: $output")
for (input in inputs) buf.appendln("${Emoji.rightArrow}INPUT: $input")
for (output in outputs) buf.appendln("${Emoji.leftArrow}OUTPUT: $output")
for (command in commands) buf.appendln("${Emoji.diamond}COMMAND: $command")
return buf.toString()
}
@ -127,8 +127,8 @@ data class SignedWireTransaction(val txBits: SerializedBytes<WireTransaction>, v
}
/** A mutable transaction that's in the process of being built, before all signatures are present. */
class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayListOf(),
private val outputStates: MutableList<ContractState> = arrayListOf(),
class TransactionBuilder(private val inputs: MutableList<StateRef> = arrayListOf(),
private val outputs: MutableList<ContractState> = arrayListOf(),
private val commands: MutableList<Command> = arrayListOf()) {
val time: TimestampCommand? get() = commands.mapNotNull { it.data as? TimestampCommand }.singleOrNull()
@ -155,8 +155,8 @@ class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayL
public fun withItems(vararg items: Any): TransactionBuilder {
for (t in items) {
when (t) {
is StateRef -> inputStates.add(t)
is ContractState -> outputStates.add(t)
is StateRef -> inputs.add(t)
is ContractState -> outputs.add(t)
is Command -> commands.add(t)
else -> throw IllegalArgumentException("Wrong argument type: ${t.javaClass}")
}
@ -214,7 +214,7 @@ class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayL
currentSigs.add(sig)
}
fun toWireTransaction() = WireTransaction(ArrayList(inputStates), ArrayList(outputStates), ArrayList(commands))
fun toWireTransaction() = WireTransaction(ArrayList(inputs), ArrayList(outputs), ArrayList(commands))
fun toSignedTransaction(checkSufficientSignatures: Boolean = true): SignedWireTransaction {
if (checkSufficientSignatures) {
@ -229,12 +229,12 @@ class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayL
fun addInputState(ref: StateRef) {
check(currentSigs.isEmpty())
inputStates.add(ref)
inputs.add(ref)
}
fun addOutputState(state: ContractState) {
check(currentSigs.isEmpty())
outputStates.add(state)
outputs.add(state)
}
fun addCommand(arg: Command) {
@ -248,8 +248,8 @@ class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayL
fun addCommand(data: CommandData, keys: List<PublicKey>) = addCommand(Command(data, keys))
// Accessors that yield immutable snapshots.
fun inputStates(): List<StateRef> = ArrayList(inputStates)
fun outputStates(): List<ContractState> = ArrayList(outputStates)
fun inputStates(): List<StateRef> = ArrayList(inputs)
fun outputStates(): List<ContractState> = ArrayList(outputs)
fun commands(): List<Command> = ArrayList(commands)
}
@ -260,19 +260,19 @@ class TransactionBuilder(private val inputStates: MutableList<StateRef> = arrayL
*/
data class LedgerTransaction(
/** The input states which will be consumed/invalidated by the execution of this transaction. */
val inStateRefs: List<StateRef>,
val inputs: List<StateRef>,
/** The states that will be generated by the execution of this transaction. */
val outStates: List<ContractState>,
val outputs: List<ContractState>,
/** Arbitrary data passed to the program of each input state. */
val commands: List<AuthenticatedObject<CommandData>>,
/** The hash of the original serialised SignedTransaction */
val hash: SecureHash
) {
@Suppress("UNCHECKED_CAST")
fun <T : ContractState> outRef(index: Int) = StateAndRef(outStates[index] as T, StateRef(hash, index))
fun <T : ContractState> outRef(index: Int) = StateAndRef(outputs[index] as T, StateRef(hash, index))
fun <T : ContractState> outRef(state: T): StateAndRef<T> {
val i = outStates.indexOf(state)
val i = outputs.indexOf(state)
if (i == -1)
throw IllegalArgumentException("State not found in this transaction")
return outRef(i)

View File

@ -60,7 +60,7 @@ class E2ETestWalletService(private val services: ServiceHub) : WalletService {
}
val statesAndRefs = transactions.map {
StateAndRef(it.tx.outputStates[0] as OwnableState, StateRef(it.id, 0))
StateAndRef(it.tx.outputs[0] as OwnableState, StateRef(it.id, 0))
}
mutex.locked {

View File

@ -321,8 +321,8 @@ class CashTests {
@Test
fun generateSimpleDirectSpend() {
val wtx = makeSpend(100.DOLLARS, THEIR_PUBKEY_1)
assertEquals(WALLET[0].ref, wtx.inputStates[0])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1), wtx.outputStates[0])
assertEquals(WALLET[0].ref, wtx.inputs[0])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1), wtx.outputs[0])
assertEquals(OUR_PUBKEY_1, wtx.commands[0].pubkeys[0])
}
@ -336,29 +336,29 @@ class CashTests {
@Test
fun generateSimpleSpendWithChange() {
val wtx = makeSpend(10.DOLLARS, THEIR_PUBKEY_1)
assertEquals(WALLET[0].ref, wtx.inputStates[0])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS), wtx.outputStates[0])
assertEquals(WALLET[0].state.copy(amount = 90.DOLLARS), wtx.outputStates[1])
assertEquals(WALLET[0].ref, wtx.inputs[0])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS), wtx.outputs[0])
assertEquals(WALLET[0].state.copy(amount = 90.DOLLARS), wtx.outputs[1])
assertEquals(OUR_PUBKEY_1, wtx.commands[0].pubkeys[0])
}
@Test
fun generateSpendWithTwoInputs() {
val wtx = makeSpend(500.DOLLARS, THEIR_PUBKEY_1)
assertEquals(WALLET[0].ref, wtx.inputStates[0])
assertEquals(WALLET[1].ref, wtx.inputStates[1])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS), wtx.outputStates[0])
assertEquals(WALLET[0].ref, wtx.inputs[0])
assertEquals(WALLET[1].ref, wtx.inputs[1])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS), wtx.outputs[0])
assertEquals(OUR_PUBKEY_1, wtx.commands[0].pubkeys[0])
}
@Test
fun generateSpendMixedDeposits() {
val wtx = makeSpend(580.DOLLARS, THEIR_PUBKEY_1)
assertEquals(WALLET[0].ref, wtx.inputStates[0])
assertEquals(WALLET[1].ref, wtx.inputStates[1])
assertEquals(WALLET[2].ref, wtx.inputStates[2])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS), wtx.outputStates[0])
assertEquals(WALLET[2].state.copy(owner = THEIR_PUBKEY_1), wtx.outputStates[1])
assertEquals(WALLET[0].ref, wtx.inputs[0])
assertEquals(WALLET[1].ref, wtx.inputs[1])
assertEquals(WALLET[2].ref, wtx.inputs[2])
assertEquals(WALLET[0].state.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS), wtx.outputs[0])
assertEquals(WALLET[2].state.copy(owner = THEIR_PUBKEY_1), wtx.outputs[1])
assertEquals(OUR_PUBKEY_1, wtx.commands[0].pubkeys[0])
}

View File

@ -11,7 +11,10 @@ package core.serialization
import contracts.Cash
import core.*
import core.crypto.SecureHash
import core.testutils.*
import core.testutils.DUMMY_PUBKEY_1
import core.testutils.MINI_CORP
import core.testutils.TEST_TX_TIME
import core.testutils.TestUtils
import org.junit.Before
import org.junit.Test
import java.security.SignatureException
@ -92,8 +95,8 @@ class TransactionSerializationTests {
val stx = tx.toSignedTransaction()
val ltx = stx.verifyToLedgerTransaction(MockIdentityService)
assertEquals(tx.commands().map { it.data }, ltx.commands.map { it.value })
assertEquals(tx.inputStates(), ltx.inStateRefs)
assertEquals(tx.outputStates(), ltx.outStates)
assertEquals(tx.inputStates(), ltx.inputs)
assertEquals(tx.outputStates(), ltx.outputs)
assertEquals(TEST_TX_TIME, ltx.commands.getTimestampBy(DUMMY_TIMESTAMPER.identity)!!.midpoint)
}
}

View File

@ -34,9 +34,9 @@ class GraphVisualiser(val dsl: TransactionGroupDSL<in ContractState>) {
txNode.styleClass = "tx"
// Now create a vertex for each output state.
for (outIndex in tx.outStates.indices) {
for (outIndex in tx.outputs.indices) {
val node = graph.addNode<Node>(tx.outRef<ContractState>(outIndex).ref.toString())
val state = tx.outStates[outIndex]
val state = tx.outputs[outIndex]
node.label = stateToLabel(state)
node.styleClass = stateToCSSClass(state) + ",state"
node.setAttribute("state", state)
@ -55,7 +55,7 @@ class GraphVisualiser(val dsl: TransactionGroupDSL<in ContractState>) {
}
// And now all states and transactions were mapped to graph nodes, hook up the input edges.
for ((txIndex, tx) in tg.transactions.withIndex()) {
for ((inputIndex, ref) in tx.inStateRefs.withIndex()) {
for ((inputIndex, ref) in tx.inputs.withIndex()) {
val edge = graph.addEdge<Edge>("tx$txIndex-in$inputIndex", ref.toString(), "tx$txIndex", true)
edge.weight = 1.2
}