Corda:合同附件如何在交易中转移?

Corda: How Contract Attachments are being transferred in Transaction?

Using Attachments 页面状态:

Attachments are ZIP/JAR files referenced from a transaction by hash, but not included in the transaction itself.

但是,API: Contract Constraints page 指出:

The JAR containing the state and contract classes, and optionally its dependencies, are all attached to the transaction.

还有一个代码片段展示了如何添加契约约束:

transaction.addOutputState(state, constraint = HashAttachmentConstraint(serviceHub.cordappProvider.getContractAttachmentID(CONTRACT_ID)!!))

但是,在检查 HashAttachment 代码时,我没有看到它包含 Contract Jar 文件的内部结构。

我的假设是我们不会通过交易转移 Contract Jar。可以描述发生的事情:

  1. 在节点启动期间,Corda 扫描所有 CorDapps 并将包含 Contract 类 的 jar 加载到本地附件存储中。
  2. 交易中的每个输出状态都可以有一个合约约束。
  3. 在验证阶段,将调用 verifyConstraints(contractAttachmentsByContract) 并根据 Node 在其本地存储中的附件验证这些约束(例如 HashAttachmentConstraint)。

问题:

  1. 交易是否包括合同附件?
  2. 该合同附件是否会通过网络传输或本地附件存储中的附件将用于验证?
  3. 我的假设遗漏了什么?

附件没有在交易中传输是正确的。交易仅包含对附件的哈希引用,用于数据引用目的。这也意味着附件可以 re-used 跨越许多事务,因为它们在自己的数据库 table.

中维护 off-chain

当一个节点收到一个包含它之前从未见过的附件的交易时,Corda 将自动从交易对手那里获取附件:https://docs.corda.net/tutorial-attachments.html#protocol

Normally attachments on transactions are fetched automatically via the ReceiveTransactionFlow.

只要附件小于网络参数的 maxTransactionSize,此传输将在没有任何开发工作的情况下发生:https://docs.corda.net/network-map.html#network-parameters

maxTransactionSize: Maximum allowed size in bytes of a transaction. This is the size of the transaction object and its attachments.

此时的合约附件一般是pre-distributed,由Cordapp 开发者签名,由Corda 节点的所有者部署。目前,Cordapp 通常不会以编程方式安装。