在Corda中创建抽象和具体的流程

时间:2018-11-07 11:26:54

标签: corda

我正在使用抽象类作为启动流,用Java编写Corda流,但是有一个具体的实现。我收到一条日志,告诉我抽象流已注册以启动响应者流,但日志中没有看到任何错误。但是,当我创建流列表时,在列表中看不到该流,并且在尝试创建rpcOps.registeredFlows列表时也看不到。这种差异的原因可能是什么?

   @InitiatingFlow
   @StartableByRPC
   public abstract class AbstractExampleFlow extends 
   FlowLogic<SignedTransaction> {
   private final int iouValue;
   private final Party otherParty;

   public AbstractExampleFlow(int iouValue, Party otherParty) {
        this.iouValue = iouValue;
        this.otherParty = otherParty;
   }


   protected SignedTransaction verifyAndSignQuoteRequest (TransactionBuilder txBuilder) throws FlowException {

        txBuilder.verify(getServiceHub());

       // Sign the transaction.
        final SignedTransaction partSignedTx = getServiceHub().signInitialTransaction(txBuilder);
        return partSignedTx;
    }

   @Suspendable
   protected SignedTransaction collectQuotePartySignatures
   (SignedTransaction partSignedTx) throws FlowException {
    FlowSession otherPartySession = initiateFlow(otherParty);
     final SignedTransaction fullySignedTx = subFlow(
                new CollectSignaturesFlow(partSignedTx,
                        ImmutableSet.of(otherPartySession), CollectSignaturesFlow.Companion.tracker()));
        return fullySignedTx;
    };

    protected Party obtainNotary () {
        return getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);
    }

    protected abstract SignedTransaction performAction () throws FlowException;

    protected TransactionBuilder buildQuoteRequestTransaction() {
        Party me = getServiceHub().getMyInfo().getLegalIdentities().get(0);
        IOUState iouState = new IOUState(iouValue, me, otherParty, new UniqueIdentifier());
        final Command<IOUContract.Commands.Create> txCommand = new Command<>(
                new IOUContract.Commands.Create(),
                ImmutableList.of(iouState.getLender().getOwningKey(), iouState.getBorrower().getOwningKey()));
        final TransactionBuilder txBuilder = new TransactionBuilder(obtainNotary())
                .addOutputState(iouState, IOU_CONTRACT_ID)
                .addCommand(txCommand);
        return txBuilder;
    } ;

    /**
     * The flow logic is encapsulated within the call() method.
     */
    @Suspendable
    @Override
    public SignedTransaction call() throws FlowException {
        return performAction();

    } .      

具体的(默认实现)如下。

    public class ConcreteDefaultExampleFlow extends AbstractExampleFlow {
public ConcreteDefaultExampleFlow(int iouValue, Party otherParty) {
        super (iouValue, otherParty);
    }
    /**
     * The flow logic is encapsulated within the performAction() method.
     */
    @Suspendable
    @Override
    public SignedTransaction performAction () throws FlowException {
        System.out.println ("In the concrete default flow");
        return collectQuotePartySignatures(verifyAndSignQuoteRequest(buildQuoteRequestTransaction()));


    }


} .   

1 个答案:

答案 0 :(得分:0)

您需要在具体实现中添加@StartableByRPC注释,而不是抽象超类。例如:

@InitiatingFlow
abstract class AbstractInitiator : FlowLogic<Unit>()

@StartableByRPC
class Initiator(val party: Party) : AbstractInitiator() {
    override val progressTracker = ProgressTracker()

    @Suspendable
    override fun call() {
        val session = initiateFlow(party)
        val string = session.receive<String>().unwrap { it -> it }
        logger.info(string)
    }
}

@InitiatedBy(AbstractInitiator::class)
class Responder(val counterpartySession: FlowSession) : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        counterpartySession.send("Joel1234")
    }
}