Trying to fix EnclaveletTest.kt

Trying to fix EnclaveletTest.kt

Trying to fix EnclaveletTest.kt

Trying to fix EnclaveletTest.kt

EnclaveletTest passes.

Restored incorrectly removed function in TransactionDSLInterpreter.

Fixed NativeSgxApiTest
This commit is contained in:
sollecitom 2017-09-26 18:19:49 +01:00
parent 9284e731c0
commit 85bc0a38a2
3 changed files with 29 additions and 19 deletions

View File

@ -21,30 +21,35 @@ class NativeSgxApiTest {
val enclavePath = "../sgx-jvm/jvm-enclave/enclave/build/cordaenclave.signed.so" val enclavePath = "../sgx-jvm/jvm-enclave/enclave/build/cordaenclave.signed.so"
} }
private val stubbedCashContractBytes = Cash.PROGRAM_ID.toByteArray()
@Ignore("The SGX code is not part of the standard build yet") @Ignore("The SGX code is not part of the standard build yet")
@Test @Test
fun `verification of valid transaction works`() { fun `verification of valid transaction works`() {
ledger { ledger {
// Issue a couple of cash states and spend them. // Issue a couple of cash states and spend them.
val wtx1 = transaction { val wtx1 = transaction {
output("c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx2 = transaction { val wtx2 = transaction {
output("c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx3 = transaction { val wtx3 = transaction {
attachments(Cash.PROGRAM_ID)
input("c1") input("c1")
input("c2") input("c2")
output(Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c3", Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY)))
command(MEGA_CORP_PUBKEY, Cash.Commands.Move()) command(MEGA_CORP_PUBKEY, Cash.Commands.Move())
verifies() verifies()
} }
val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), emptyArray()) val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), arrayOf(stubbedCashContractBytes))
val serialized = req.serialize() val serialized = req.serialize()
assertNull(NativeSgxApi.verify(enclavePath, serialized.bytes)) assertNull(NativeSgxApi.verify(enclavePath, serialized.bytes))
} }

View File

@ -3,15 +3,17 @@
package com.r3.enclaves.txverify package com.r3.enclaves.txverify
import com.esotericsoftware.minlog.Log import com.esotericsoftware.minlog.Log
import net.corda.core.contracts.HashAttachmentConstraint
import net.corda.core.crypto.sha256
import net.corda.core.serialization.CordaSerializable import net.corda.core.serialization.CordaSerializable
import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.SerializedBytes
import net.corda.core.serialization.deserialize import net.corda.core.serialization.deserialize
import net.corda.core.transactions.WireTransaction import net.corda.core.transactions.WireTransaction
import net.corda.nodeapi.internal.serialization.GeneratedAttachment
import java.io.File import java.io.File
// This file implements the functionality of the SGX transaction verification enclave. // This file implements the functionality of the SGX transaction verification enclave.
/** This is just used to simplify marshalling across the enclave boundary (EDL is a bit awkward) */ /** This is just used to simplify marshalling across the enclave boundary (EDL is a bit awkward) */
@CordaSerializable @CordaSerializable
class TransactionVerificationRequest(val wtxToVerify: SerializedBytes<WireTransaction>, class TransactionVerificationRequest(val wtxToVerify: SerializedBytes<WireTransaction>,
@ -35,9 +37,9 @@ fun verifyInEnclave(reqBytes: ByteArray) {
val dependencies = req.dependencies.map { it.deserialize() }.associateBy { it.id } val dependencies = req.dependencies.map { it.deserialize() }.associateBy { it.id }
val ltx = wtxToVerify.toLedgerTransaction( val ltx = wtxToVerify.toLedgerTransaction(
resolveIdentity = { null }, resolveIdentity = { null },
resolveAttachment = { null }, resolveAttachment = { secureHash -> req.attachments.filter { it.sha256() == secureHash }.map { GeneratedAttachment(it) }.singleOrNull() },
resolveStateRef = { dependencies[it.txhash]?.outputs?.get(it.index) }, resolveStateRef = { dependencies[it.txhash]?.outputs?.get(it.index) },
resolveContractAttachment = { null } resolveContractAttachment = { (it.constraint as HashAttachmentConstraint).attachmentId }
) )
ltx.verify() ltx.verify()
} }

View File

@ -18,31 +18,33 @@ import kotlin.test.assertTrue
class EnclaveletTest { class EnclaveletTest {
private val stubbedCashContractBytes = Cash.PROGRAM_ID.toByteArray()
@Test @Test
fun success() { fun success() {
ledger { ledger {
// Issue a couple of cash states and spend them. // Issue a couple of cash states and spend them.
val wtx1 = transaction { val wtx1 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx2 = transaction { val wtx2 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx3 = transaction { val wtx3 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
input(Cash.PROGRAM_ID, "c1") input("c1")
input(Cash.PROGRAM_ID, "c2") input("c2")
output(Cash.PROGRAM_ID, "c3", Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c3", Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY)))
command(MEGA_CORP_PUBKEY, Cash.Commands.Move()) command(MEGA_CORP_PUBKEY, Cash.Commands.Move())
verifies() verifies()
} }
val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), emptyArray()) val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), arrayOf(stubbedCashContractBytes))
val serialized = req.serialize() val serialized = req.serialize()
Files.write(Paths.get("/tmp/req"), serialized.bytes) Files.write(Paths.get("/tmp/req"), serialized.bytes)
verifyInEnclave(serialized.bytes) verifyInEnclave(serialized.bytes)
@ -54,26 +56,27 @@ class EnclaveletTest {
ledger { ledger {
// Issue a couple of cash states and spend them. // Issue a couple of cash states and spend them.
val wtx1 = transaction { val wtx1 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c1", Cash.State(1000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx2 = transaction { val wtx2 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
output(Cash.PROGRAM_ID, "c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c2", Cash.State(2000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MEGA_CORP_PUBKEY)))
command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue()) command(DUMMY_CASH_ISSUER.party.owningKey, Cash.Commands.Issue())
verifies() verifies()
} }
val wtx3 = transaction { val wtx3 = transaction {
attachment(Cash.PROGRAM_ID) attachments(Cash.PROGRAM_ID)
input(Cash.PROGRAM_ID, "c1") input("c1")
input(Cash.PROGRAM_ID, "c2") input("c2")
command(DUMMY_CASH_ISSUER.party.owningKey, DummyCommandData) command(DUMMY_CASH_ISSUER.party.owningKey, DummyCommandData)
output(Cash.PROGRAM_ID, "c3", Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY))) output(Cash.PROGRAM_ID, "c3", Cash.State(3000.POUNDS `issued by` DUMMY_CASH_ISSUER, AnonymousParty(MINI_CORP_PUBKEY)))
failsWith("Required ${Cash.Commands.Move::class.java.canonicalName} command") failsWith("Required ${Cash.Commands.Move::class.java.canonicalName} command")
} }
val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), emptyArray()) // TODO need to add the CashContract Attachment Bytes
val req = TransactionVerificationRequest(wtx3.serialize(), arrayOf(wtx1.serialize(), wtx2.serialize()), arrayOf(stubbedCashContractBytes))
val e = assertFailsWith<Exception> { verifyInEnclave(req.serialize().bytes) } val e = assertFailsWith<Exception> { verifyInEnclave(req.serialize().bytes) }
assertTrue(e.message!!.contains("Required ${Cash.Commands.Move::class.java.canonicalName} command")) assertTrue(e.message!!.contains("Required ${Cash.Commands.Move::class.java.canonicalName} command"))
} }