【问题标题】:Question about attachment upload and download关于附件上传和下载的问题
【发布时间】:2019-01-04 23:14:27
【问题描述】:

首先我阅读了corda关于使用附件的文档。但是我仍然对上传和下载附件的过程有疑问。

我的任务是编写一个简单的 cordapp 用于将文件从 NodeA 传输到 NodeB。从 NodeA shell 上传 zip 文件后,我收到了一个哈希,然后将其包含在事务中。流是成功的。但是,在 NodeB 中,我无法取回文件。然后我尝试使用该哈希从 NodeA 取回文件。但是,shell 返回错误消息并说 invalidInputSteam。

但是当我运行 cordaftp (https://github.com/corda/cordaftp) 并尝试上传文件并从同一个 shell 下载它时,shell 正确地询问了存储路径。我已经阅读了各种帖子,并且知道我需要包含额外的代码才能成功下载。但是我不知道应该修改哪个文件以及应该编写什么代码。希望有人可以帮助我解决我的问题,因为我已经花了几天时间阅读以前的帖子和文档。

下面是流程部分:

@InitiatingFlow
@StartableByRPC
class FileInitiateFlow(
    val receiver: Party,
    val comment: String,
    val hash: SecureHash.SHA256) : FlowLogic<SignedTransaction>() {
    companion object {
    object GENERATING_TRANSACTION : Step("Generating transaction")
    object VERIFYING_TRANSACTION : Step("Verifying contract constraints.")
    object SIGNING_TRANSACTION : Step("Signing transaction with sender private key.")
    object GATHERING_SIGS : Step("Gathering the receiver's signature."){
        override fun childProgressTracker() = CollectSignaturesFlow.tracker()
    }

    object FINALISING_TRANSACTION : Step("Obtaining notary signature and recording transaction.") {
        override fun childProgressTracker() = FinalityFlow.tracker()
    }

    fun tracker() = ProgressTracker(
            GENERATING_TRANSACTION,
            VERIFYING_TRANSACTION,
            SIGNING_TRANSACTION,
            GATHERING_SIGS,
            FINALISING_TRANSACTION
    )
}

override val progressTracker = tracker()


@Suspendable
override fun call(): SignedTransaction {
    // Obtain a reference to the notary we want to use.
    val notary = serviceHub.networkMapCache.notaryIdentities[0]
    val sender = serviceHub.myInfo.legalIdentities.first()
    // Stage 1.
    progressTracker.currentStep = GENERATING_TRANSACTION
    // Generate an unsigned transaction.
    val fileState = FileState(sender, receiver,comment)
    val txCommand = Command(RoamingContract.Commands.FileInitiate(), fileState.participants.map { it.owningKey })
    val txBuilder = TransactionBuilder(notary)
            .addOutputState(fileState, ID)
            .addCommand(txCommand)
            .addAttachment(hash)

    // Stage 2.
    progressTracker.currentStep = VERIFYING_TRANSACTION
    // Verify that the transaction is valid.
    txBuilder.verify(serviceHub)

    // Stage 3.
    progressTracker.currentStep = SIGNING_TRANSACTION
    // Sign the transaction.
    val partSignedTx = serviceHub.signInitialTransaction(txBuilder)

    // Stage 4.
    progressTracker.currentStep = GATHERING_SIGS
    // Send the state to the counterparty, and receive it back with their signature.
    val otherPartyFlow = initiateFlow(receiver)
    val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, setOf(otherPartyFlow), GATHERING_SIGS.childProgressTracker()))

    // Stage 5.
    progressTracker.currentStep = FINALISING_TRANSACTION
    // Notarise and record the transaction in both parties' vaults.
    return subFlow(FinalityFlow(fullySignedTx, FINALISING_TRANSACTION.childProgressTracker()))
}
}

@InitiatedBy(FileInitiateFlow::class)
class FileInitiateRespond(val senderFlow: FlowSession) :         FlowLogic<SignedTransaction>() {
@Suspendable
override fun call(): SignedTransaction{
    val signedTransactionFlow = object : SignTransactionFlow(senderFlow) {
        override fun checkTransaction(stx: SignedTransaction) = requireThat {
            val output = stx.tx.outputs.single().data
            "This must be an File State" using (output is FileState)
        }
    }
    return subFlow(signedTransactionFlow)
}
}

所以我首先运行 uploadAttachment 来上传 zip 文件,我得到了哈希值,然后以哈希值作为输入启动流程。流程成功,但在接收方,我无法通过检查现有状态来获取上传文件的哈希键。

【问题讨论】:

  • 你能分享你的代码吗!上传和下载代码非常简单
  • @manish,嗨,我已经发布了流程部分的代码
  • 代码对我来说似乎没问题。如果你能分享我可以调试的 git hub 项目
  • @manish,这里是github链接,github.com/wyfok/Roaming_Project.git谢谢帮助
  • @hkgyyf 你正在运行什么命令来下载附件?你得到什么错误?

标签: corda


【解决方案1】:

SecureHash 实例的附件哈希。 Corda 3.2/3.3 中有一个错误,shell 无法将字符串转换为 SecureHash 对象。

修复在这里:https://github.com/corda/corda/pull/3248 并将在 Corda 4 中提供。

【讨论】:

  • 但是,如果我运行 cordaftp (github.com/corda/cordaftp),我可以在发件人 shell 中使用哈希下载附件。如果我也想在 shell 中修改 Kotlin CorDapp 模板,是否可以修改?
  • 这里是使用cordaftp上传和下载附件的截图i.imgur.com/nrl9ecO.jpg
  • 意识到cordaftp使用corda 3.0构建。现在可以下载corda 3.0 kotlin模板了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2018-08-26
  • 2011-07-05
  • 2014-01-05
  • 2022-10-07
  • 2021-05-25
相关资源
最近更新 更多