在 corda 中使用账户编写流程
Writing flows using accounts in corda
我正在使用 Corda 中的帐户。在我的代码中成功创建了新帐户,但是我在做这两件事时遇到了困难。
1.) 如何检查帐户是否实际创建并存在于节点中,意味着我们是否可以检查 Corda 中所有帐户的列表。
2.) 如何为帐户编写响应程序流程,意味着我的交易流程无法正常工作。如果我们现在开始使用帐户库,响应程序流经典代码有什么要更改的吗?
我的代码如下:
@InitiatedBy(Proposal.class)
public static class ProposalAcceptance extends FlowLogic<Void> {
//private variable
private FlowSession counterpartySession;
//Constructor
public ProposalAcceptance(FlowSession counterpartySession) {
this.counterpartySession = counterpartySession;
}
@Suspendable
@Override
public Void call() throws FlowException {
SignedTransaction signedTransaction = subFlow(new SignTransactionFlow(counterpartySession) {
@Suspendable
@Override
protected void checkTransaction(SignedTransaction stx) throws FlowException {
/*
* SignTransactionFlow will automatically verify the transaction and its signatures before signing it.
* However, just because a transaction is contractually valid doesn’t mean we necessarily want to sign.
* What if we don’t want to deal with the counterparty in question, or the value is too high,
* or we’re not happy with the transaction’s structure? checkTransaction
* allows us to define these additional checks. If any of these conditions are not met,
* we will not sign the transaction - even if the transaction and its signatures are contractually valid.
* ----------
* For this hello-world cordapp, we will not implement any aditional checks.
* */
}
});
//Stored the transaction into data base.
subFlow(new ReceiveFinalityFlow(counterpartySession));
return null;
}
}
帐户内部只是 AccountInfo
类型的 corda 状态,因此您可以查询保险库以列出节点知道使用的所有帐户:
run vaultQuery contractStateType: com.r3.corda.lib.accounts.contracts.states.AccountInfo
响应程序流程中没有任何具体变化,请确保您在发起程序流程中使用了正确的会话。在此处查看示例存储库中可用的一些示例:https://github.com/corda/samples-java/tree/master/Accounts
- 检查账号是否创建,需要写流测试;一个很好的学习方法是查看 R3 工程师如何进行测试。
- 例如,您可以找到 here
CreateAccount
流的测试场景。
- 要获得一个帐户,图书馆有一个非常有用的服务
KeyManagementBackedAccountService
,它有不同的方法来获得一个帐户(通过 name
、UUID
或 PublicKey
) ;看看here.
- 现在关于请求帐户签名,需要了解的重要一件事是它不是签署交易的帐户,而是
host
代表帐户签名的节点帐号。
- 假设您有 3 个节点(A、B 和 C); A发起一个流程,请求10个账号签名(5个托管在B,5个托管在C)。
- A 签署初始交易后,它将创建
FlowSession
s 来收集签名。
- 因为代表账户签名的是
host
个节点,那么在我们的例子中你只需要2FlowSession
s;一个与节点 B(因此它代表它托管的 5 个帐户签名)和一个与节点 C 的会话(对于其他 5 个帐户)。
- 在响应者流程中,节点 B 和 C 将接收发起者签署的交易。
- 开箱即用,当节点收到交易时,它会查看所有必需的签名,并查看每个必需的签名(如果它拥有私钥);它将提供该签名。
- 意思是,节点B什么时候会收到交易;它会看到 10 个必需的签名,并且因为它拥有 5 个帐户(意味着它拥有这 5 个帐户的私钥),所以它会自动提供 5 个签名。
我正在使用 Corda 中的帐户。在我的代码中成功创建了新帐户,但是我在做这两件事时遇到了困难。 1.) 如何检查帐户是否实际创建并存在于节点中,意味着我们是否可以检查 Corda 中所有帐户的列表。
2.) 如何为帐户编写响应程序流程,意味着我的交易流程无法正常工作。如果我们现在开始使用帐户库,响应程序流经典代码有什么要更改的吗?
我的代码如下:
@InitiatedBy(Proposal.class)
public static class ProposalAcceptance extends FlowLogic<Void> {
//private variable
private FlowSession counterpartySession;
//Constructor
public ProposalAcceptance(FlowSession counterpartySession) {
this.counterpartySession = counterpartySession;
}
@Suspendable
@Override
public Void call() throws FlowException {
SignedTransaction signedTransaction = subFlow(new SignTransactionFlow(counterpartySession) {
@Suspendable
@Override
protected void checkTransaction(SignedTransaction stx) throws FlowException {
/*
* SignTransactionFlow will automatically verify the transaction and its signatures before signing it.
* However, just because a transaction is contractually valid doesn’t mean we necessarily want to sign.
* What if we don’t want to deal with the counterparty in question, or the value is too high,
* or we’re not happy with the transaction’s structure? checkTransaction
* allows us to define these additional checks. If any of these conditions are not met,
* we will not sign the transaction - even if the transaction and its signatures are contractually valid.
* ----------
* For this hello-world cordapp, we will not implement any aditional checks.
* */
}
});
//Stored the transaction into data base.
subFlow(new ReceiveFinalityFlow(counterpartySession));
return null;
}
}
帐户内部只是
AccountInfo
类型的 corda 状态,因此您可以查询保险库以列出节点知道使用的所有帐户:run vaultQuery contractStateType: com.r3.corda.lib.accounts.contracts.states.AccountInfo
响应程序流程中没有任何具体变化,请确保您在发起程序流程中使用了正确的会话。在此处查看示例存储库中可用的一些示例:https://github.com/corda/samples-java/tree/master/Accounts
- 检查账号是否创建,需要写流测试;一个很好的学习方法是查看 R3 工程师如何进行测试。
- 例如,您可以找到 here
CreateAccount
流的测试场景。 - 要获得一个帐户,图书馆有一个非常有用的服务
KeyManagementBackedAccountService
,它有不同的方法来获得一个帐户(通过name
、UUID
或PublicKey
) ;看看here. - 现在关于请求帐户签名,需要了解的重要一件事是它不是签署交易的帐户,而是
host
代表帐户签名的节点帐号。 - 假设您有 3 个节点(A、B 和 C); A发起一个流程,请求10个账号签名(5个托管在B,5个托管在C)。
- A 签署初始交易后,它将创建
FlowSession
s 来收集签名。 - 因为代表账户签名的是
host
个节点,那么在我们的例子中你只需要2FlowSession
s;一个与节点 B(因此它代表它托管的 5 个帐户签名)和一个与节点 C 的会话(对于其他 5 个帐户)。 - 在响应者流程中,节点 B 和 C 将接收发起者签署的交易。
- 开箱即用,当节点收到交易时,它会查看所有必需的签名,并查看每个必需的签名(如果它拥有私钥);它将提供该签名。
- 意思是,节点B什么时候会收到交易;它会看到 10 个必需的签名,并且因为它拥有 5 个帐户(意味着它拥有这 5 个帐户的私钥),所以它会自动提供 5 个签名。