Corda block chain development of training camp notes 12: Flow exercise answers

1、IOUIssueFlow

First, the members of the class constructor

flow is used to define the transfer process tx, tx to New, and then people need to be signed, this is only the first step to generate the flow, the most important thing is the new flow, for conversational

First, we declare that extends the abstract class flowLogic:

kjkjpublic class IOUIssueFlow extends FlowLogic<SignedTransaction> 

If I want to issue a bond, you need to specify a given, how much money, why not issuer, who is it who is calling issuer?

Bond is doing? It is that you owe me how much Well, how much money is owed to the specified owner issuer, then the response of the class flow is doing?

So we can specify owe people money owed

private final Party owner;
private final int amunt;

Then the constructor:

public IOUIssueFlow(Party owner, int amount) {
        this.owner = owner;UIssueFlow
一、类的成员与构造器
flow是用来定义tx的流转过程的,用来新建tx,然后交给需要的人进行签名,这只是第一步用来生成flow,最关键的还是新建flow,来进行会话

首先我们先进行声明,它扩展了抽象类flowLogic:


kjkjpublic class IOUIssueFlow extends FlowLogic<SignedTransaction> 
如果我现在想issue一个债券,需要指定给定、多少钱,为什么没有issuer,就是谁调用它谁就是issuer?

债券是干嘛的?就是表示你欠我多少嘛,就是指定owner欠issuer多少钱,那么响应flow的那个class是干嘛的?

所以我们可以指定欠钱的人和欠的钱


private final Party owner;
private final int amunt;
然后是构造器:


        this.amount = amount;
    }

This class has a total of three data members, and the third is a progressTracker

private final ProgressTracker progressTracker = new ProgressTracker();

It does not need to specify a user, the user does not know these things, you can not claim to understand what these underlying! But he has a get method

@Override
public ProgressTracker getProgressTracker() {
    return progressTracker;
}

Two, class () method

We have expanded flowLogic class, the most important thing is to call the method, the following is a statement of this function

@Suspendable
@Override
public signedTransaction call() throws FlowException 

The next big step is divided into three:

  1. New state
    • The state added to its contract to go tx
    • notrary need to be placed before outputState
  2. New tx
    • The command is added to tx go
  3. tx circulation

I, Shindachi state

In addition, we also need to rely on notaries public consensus

Party issuer = getOurIdentity();

Next is the first step, the establishment of state:

A iou need issuer, who started the stream, who is the issuer, the flow is from the party started!

IOUState iouState = new IOUState(issuer, owner, amount);
IOUContract.Commands.Issue command = new IOUContract.Commands.Issue();

II, New tx

Personally, I feel, from the perspective of ease of understanding, the whole corda, as well as all of the block chain network, tx are based on the core.

corda in tx need these things:

  • Notary Public
  • state input and output
  • contract inspection command reference
  • Command and signer
Party notrary = getServiceHub().getNetworkMapCache().getNotraryIdentities(0);
tx.setNotrary(notrary);
notrary.setOutputState(iouState, IOUContact.ID);
List<PublicKey> requiredSigner = ImmutableList.of(IOUState.getIssuer().getOwingKey());
tx.addCommnad(command,requiredSigner);

III, sending flow update books

Roughly divided into the following steps:

  • To verify tx by servicehub
  • Create a new session
  • The signature verification tx
  • (-------------------- -------------- energetic front)
  • This should begin to interact and responder

Tx full signature is not meant to be signed after the other, so we need to establish a sub-stream and interact with other nodes,

It is necessary to collect signatures in front of all the streams + session,

Finally, returning fully signed with new stream + Example session terminator single stream formed

And so on, this picture is not that sum up the previous picture is of? ? ? ? ? ? ? ? It is likely to ah! ! ! As long as I can understand the response program flow & flow of this exchange process & read the literature I could get through the whole corda!

According to verify the legality of contract of tx

txBuidler.verify(getServiceHub())

Generating a streaming session

Flow Session = initiateFlow(owner);

The deal signed with the private key it can not be changed

SignedTransaction signedTransaction = getSeriveHub().signInitialTransaction(txBuilder);

2、IOUIssueFlowResponder

2.1, the class declaration and constructor

As the stream is started, it must be marked with the comment:

@InitiatedBy(IOUIssueFlow.class)

This class extends flowLogic, the return type of call is the Void

public class IOUIssueFlowResponder extends FlowLogic<Void> {
    
    @Override
    @Suspendable
    public Void call() throws Exception {
        
    }
}

It is only a private member, is sent to the other side of the conversation?

private final FlowSession otherSide

Alt + Insert then generate the constructor

public IOUIssuerFlowResponder(FlowSession otherSide) {
    this.other = otherSide
}

2.2 call () method

Strange to say, this call method I totally do not understand le ,,,

STX is a first generation, the generation method is meant subFlow ,,, subFlow call sub-streams, once sub-stream and a method call is completed inside it, it is called to generate the results chant

/*
*
*/
@Suspendable
    @Throws(FlowException::class)
    open fun <R> subFlow(subLogic: FlowLogic<R>): R {
        subLogic.stateMachine = stateMachine
        maybeWireUpProgressTracking(subLogic)
        logger.debug { "Calling subflow: $subLogic" }
        val result = stateMachine.subFlow(subLogic)
        logger.debug { "Subflow finished with result ${result.toString().abbreviate(300)}" }
        return result
    }

Also pass in flowLogic

3, complete code

3.1 IOUIssueFlow

package bootcamp;

import co.paralleluniverse.fibers.Suspendable;
import com.google.common.collect.ImmutableList;
import net.corda.core.flows.*;
import net.corda.core.identity.Party;
import net.corda.core.transactions.SignedTransaction;
import net.corda.core.transactions.TransactionBuilder;
import net.corda.core.utilities.ProgressTracker;

import java.security.PublicKey;
import java.util.List;

import static java.util.Collections.singletonList;

@InitiatingFlow
@StartableByRPC
public class IOUIssueFlow extends FlowLogic<SignedTransaction> {
    private final Party owner;
    private final int amount;

    public IOUIssueFlow(Party owner, int amount) {
        this.owner = owner;
        this.amount = amount;
    }

    private final ProgressTracker progressTracker = new ProgressTracker();

    @Override
    public ProgressTracker getProgressTracker() {
        return progressTracker;
    }

    @Suspendable
    @Override
    public SignedTransaction call() throws FlowException {
        // We choose our transaction's notary (the notary prevents double-spends).
        Party notary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);
        // We get a reference to our own identity.
        Party issuer = getOurIdentity();

        /* ============================================================================
         *         TODO 1 - Create our TokenState to represent on-ledger tokens!
         * ===========================================================================*/
        // We create our new TokenState.
        IOUState IOUState = new IOUState(issuer, owner, amount);
        IOUContract.Commands.Issue command = new IOUContract.Commands.Issue();


        /* ============================================================================
         *      TODO 3 - Build our token issuance transaction to update the ledger!
         * ====================ommand(command, IOUState.getIssuer().getOwningKey());    //step5,把命令加到tx
        transactionBuilder.setN=======================================================*/
        // We build our transaction.
        TransactionBuilder transactionBuilder = new TransactionBuilder();
        transactionBuilder.setNotary(notary);
        transactionBuilder.addOutputState(IOUState, IOUContract.ID);    //把IOUState加上跟IOUStateContract的参考值,然后加到transactionBuilder
        List<PublicKey> requiredSigners = ImmutableList.of(IOUState.getIssuer().getOwningKey(), owner.getOwningKey());
        transactionBuilder.addCommand(command, requiredSigners);    //step5,把命令加到tx


        /* ============================================================================
         *          TODO 2 - Write our TokenContract to control token issuance!
         * ===========================================================================*/
        transactionBuilder.verify(getServiceHub());
        FlowSession session = initiateFlow(owner);
        SignedTransaction signedTransaction = getServiceHub().signInitialTransaction(transactionBuilder);

        SignedTransaction fullySignedTransaction = subFlow(new CollectSignaturesFlow(signedTransaction,
                singletonList(session)));
        return subFlow(new FinalityFlow(fullySignedTransaction, singletonList(session)));
    }
}

3.2 IOUIssueFlowResponder

package bootcamp;

import co.paralleluniverse.fibers.Suspendable;
import net.corda.core.flows.*;
import net.corda.core.transactions.SignedTransaction;

@InitiatedBy(IOUIssueFlow.class)
public class IOUIssuerFlowResponder extends FlowLogic<Void> {

    private final FlowSession otherSide;

    public IOUIssuerFlowResponder(FlowSession otherSide) {
        this.otherSide = otherSide;
    }

    @Override
    @Suspendable
    public Void call() throws FlowException {
        SignedTransaction signedTransaction = subFlow(new SignTransactionFlow(otherSide) {
            @Suspendable
            @Override
            protected void checkTransaction(SignedTransaction stx) throws FlowException {

            }
        });
        subFlow(new ReceiveFinalityFlow(otherSide, signedTransaction.getId()));
        return null;
    }
}

Guess you like

Origin www.cnblogs.com/huangming-zzz/p/11521108.html