hyperledger fabric fabric-sdk-java 从区块中解析交易内容及读写集 (7)

官方示例其实是有完整例子的, 但是我想你可能对那几千行代码不是很感兴趣, 所以把解析读写集部分给摘出来了, 代码奉上 

 public static List getRWSetFromBlock(BlockInfo blockInfo) throws InvalidProtocolBufferException {
        List transactionList = new ArrayList<>();
        for (BlockInfo.EnvelopeInfo envelopeInfo : blockInfo.getEnvelopeInfos()) {
            String id = envelopeInfo.getCreator().getId();
            String mspid = envelopeInfo.getCreator().getMspid();

            if (envelopeInfo.getType() == TRANSACTION_ENVELOPE) {
                Date timestamp = envelopeInfo.getTimestamp();
                BlockInfo.TransactionEnvelopeInfo transactionEnvelopeInfo = (BlockInfo.TransactionEnvelopeInfo) envelopeInfo;
                String transactionID = transactionEnvelopeInfo.getTransactionID();
                boolean valid = transactionEnvelopeInfo.isValid();
                byte validationCode = transactionEnvelopeInfo.getValidationCode();
                for (BlockInfo.TransactionEnvelopeInfo.TransactionActionInfo transactionActionInfo : transactionEnvelopeInfo.getTransactionActionInfos()) {
                    Map transactionMap = new HashMap<>();
                    transactionMap.put("transactionID", transactionID);
                    transactionMap.put("timestamp", timestamp.getTime());
                    transactionMap.put("isValid", valid);
                    transactionMap.put("MSPID", mspid);
                    transactionMap.put("usercert", id);
                    transactionMap.put("validationCode", validationCode);
                    int index = validationCode;
					if (index <= 24) {
						transactionMap.put("validationCodeName", TransactionValidCode.transactionValidCode[index]);
					} else {
						transactionMap.put("validationCodeName", TransactionValidCode.transactionValidCode[index-229]);
					}
                    int chaincodeInputArgsCount = transactionActionInfo.getChaincodeInputArgsCount();
                    String[] argus = new String[chaincodeInputArgsCount];
                    for (int i = 0; i < chaincodeInputArgsCount; i++) {
                        argus[i] = new String(transactionActionInfo.getChaincodeInputArgs(i));
                    }
                    transactionMap.put("argus", argus);
                    transactionMap.put("status", transactionActionInfo.getResponseStatus());
                    transactionMap.put("endorsementsCount", transactionActionInfo.getEndorsementsCount());
                    String chaincodeIDName = transactionActionInfo.getChaincodeIDName();
                    transactionMap.put("chaincodeName", chaincodeIDName);
                    String chaincodeIDVersion = transactionActionInfo.getChaincodeIDVersion();
                    transactionMap.put("chaincodeVersion", chaincodeIDVersion);
                    TxReadWriteSetInfo rwsetInfo = transactionActionInfo.getTxReadWriteSet();
                    if (null != rwsetInfo) {
                        List rwList = new ArrayList();

                        for (TxReadWriteSetInfo.NsRwsetInfo nsRwsetInfo : rwsetInfo.getNsRwsetInfos()) {

                            Map rwMap = new HashMap<>();
                            Map writeMap = new HashMap<>();
                            KvRwset.KVRWSet rws = nsRwsetInfo.getRwset();
                            String[] readSet = new String[rws.getReadsCount()];
                            int i = 0;
                            for (KvRwset.KVRead readList : rws.getReadsList()) {
                                String key = readList.getKey();
                                readSet[i++] = key;
                            }
                            rwMap.put("read", readSet);
                            for (KvRwset.KVWrite writeList : rws.getWritesList()) {
                                String valAsString = printableString(new String(writeList.getValue().toByteArray(), UTF_8));
                                writeList.getKey();
                                writeMap.put(writeList.getKey(), valAsString);
                            }
                            rwMap.put("write", writeMap);
                            rwList.add(rwMap);

                        }
                        transactionMap.put("RWSet", rwList);
                    }
                    transactionList.add(transactionMap);
                }
            }

        }
        return transactionList;
    }



    static String printableString(String string) {
        int maxLogStringLength = 64;
        if (string == null || string.length() == 0) {
            return string;
        }
        String ret = string.replaceAll("[^\\p{Print}]", "?");
        ret = ret.substring(0, Math.min(ret.length(), maxLogStringLength)) + (ret.length() > maxLogStringLength ? "..." : "");
        return ret;

    }




    public static String[] transactionValidCode=new String[]
            {"VALID","NIL_ENVELOPE_VALUE","BAD_PAYLOAD_VALUE","BAD_COMMON_HEADER_VALUE",
              "BAD_CREATOR_SIGNATURE_VALUE","INVALID_ENDORSER_TRANSACTION_VALUE","INVALID_CONFIG_TRANSACTION_VALUE","UNSUPPORTED_TX_PAYLOAD_VALUE",
              "BAD_PROPOSAL_TXID_VALUE","DUPLICATE_TXID_VALUE","ENDORSEMENT_POLICY_FAILURE_VALUE","MVCC_READ_CONFLICT_VALUE",
              "PHANTOM_READ_CONFLICT_VALUE","UNKNOWN_TX_TYPE_VALUE","TARGET_CHAIN_NOT_FOUND_VALUE","MARSHAL_TX_ERROR_VALUE",
              "NIL_TXACTION_VALUE","EXPIRED_CHAINCODE_VALUE","CHAINCODE_VERSION_CONFLICT_VALUE","BAD_HEADER_EXTENSION_VALUE",
              "BAD_CHANNEL_HEADER_VALUE","BAD_RESPONSE_PAYLOAD_VALUE","BAD_RWSET_VALUE","ILLEGAL_WRITESET_VALUE",
              "INVALID_WRITESET_VALUE","NOT_VALIDATED_VALUE","INVALID_OTHER_REASON_VALUE"
            };

解析出来的结果: 

{
    "MSPID": "Org1MSP",
    "validationCodeName": "VALID",
    "isValid": true,
    "endorsementsCount": 2,
    "validationCode": 0,
    "usercert": "-----BEGIN CERTIFICATE-----\nMIICKzCCAdGgAwIBAgIRAIF2IpfrV1NNOxn4LvKa2wAwCgYIKoZIzj0EAwIwczEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\nLm9yZzEuZXhhbXBsZS5jb20wHhcNMjAwNjE5MTI0MDAwWhcNMzAwNjE3MTI0MDAw\nWjBsMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN\nU2FuIEZyYW5jaXNjbzEPMA0GA1UECxMGY2xpZW50MR8wHQYDVQQDDBZVc2VyMUBv\ncmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzYjdYD03\n5MyUfJMu6jXFLGBHJLYUsIUM5FbW+0x6x/6ncT921QDZKuKRvGZY9X/NmNerzAri\nf12OFnfKEhfLBaNNMEswDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwKwYD\nVR0jBCQwIoAgIlJzB2U8NvdZvMWgATkcePQFA9u7ELRjZy66MSZLE7cwCgYIKoZI\nzj0EAwIDSAAwRQIhALSjWg1uKJkM/HseDRwbGDrrX3OnnQpx7/3eYWydz9vFAiBE\npMOUnQUtwNHb+S39qJV+6lnuaBbbYGVytcCu4KPa9Q==\n-----END CERTIFICATE-----\n",
    "transactionID": "48369bb2bedf4a404e19cde5e57566b090a524ffdc497cec709440b422847f7b",
    "RWSet": [
        {
            "read": [
                "mycc2"
            ],
            "write": {}
        },
        {
            "read": [
                "a",
                "b"
            ],
            "write": {
                "a": "142",
                "b": "413"
            }
        }
    ],
    "argus": [
        "invoke",
        "a",
        "b",
        "5"
    ],
    "chaincodeVersion": "1.0",
    "timestamp": 1593584729787,
    "status": 200,
    "chaincodeName": "mycc2"
}

 另外附上官方给的验证结果表

  /**
     * VALID = 0;
     */
    public static final int VALID_VALUE = 0;
    /**
     * NIL_ENVELOPE = 1;
     */
    public static final int NIL_ENVELOPE_VALUE = 1;
    /**
     * BAD_PAYLOAD = 2;
     */
    public static final int BAD_PAYLOAD_VALUE = 2;
    /**
     * BAD_COMMON_HEADER = 3;
     */
    public static final int BAD_COMMON_HEADER_VALUE = 3;
    /**
     * BAD_CREATOR_SIGNATURE = 4;
     */
    public static final int BAD_CREATOR_SIGNATURE_VALUE = 4;
    /**
     * INVALID_ENDORSER_TRANSACTION = 5;
     */
    public static final int INVALID_ENDORSER_TRANSACTION_VALUE = 5;
    /**
     * INVALID_CONFIG_TRANSACTION = 6;
     */
    public static final int INVALID_CONFIG_TRANSACTION_VALUE = 6;
    /**
     * UNSUPPORTED_TX_PAYLOAD = 7;
     */
    public static final int UNSUPPORTED_TX_PAYLOAD_VALUE = 7;
    /**
     * BAD_PROPOSAL_TXID = 8;
     */
    public static final int BAD_PROPOSAL_TXID_VALUE = 8;
    /**
     * DUPLICATE_TXID = 9;
     */
    public static final int DUPLICATE_TXID_VALUE = 9;
    /**
     * ENDORSEMENT_POLICY_FAILURE = 10;
     */
    public static final int ENDORSEMENT_POLICY_FAILURE_VALUE = 10;
    /**
     * MVCC_READ_CONFLICT = 11;
     */
    public static final int MVCC_READ_CONFLICT_VALUE = 11;
    /**
     * PHANTOM_READ_CONFLICT = 12;
     */
    public static final int PHANTOM_READ_CONFLICT_VALUE = 12;
    /**
     * UNKNOWN_TX_TYPE = 13;
     */
    public static final int UNKNOWN_TX_TYPE_VALUE = 13;
    /**
     * TARGET_CHAIN_NOT_FOUND = 14;
     */
    public static final int TARGET_CHAIN_NOT_FOUND_VALUE = 14;
    /**
     * MARSHAL_TX_ERROR = 15;
     */
    public static final int MARSHAL_TX_ERROR_VALUE = 15;
    /**
     * NIL_TXACTION = 16;
     */
    public static final int NIL_TXACTION_VALUE = 16;
    /**
     * EXPIRED_CHAINCODE = 17;
     */
    public static final int EXPIRED_CHAINCODE_VALUE = 17;
    /**
     * CHAINCODE_VERSION_CONFLICT = 18;
     */
    public static final int CHAINCODE_VERSION_CONFLICT_VALUE = 18;
    /**
     * BAD_HEADER_EXTENSION = 19;
     */
    public static final int BAD_HEADER_EXTENSION_VALUE = 19;
    /**
     * BAD_CHANNEL_HEADER = 20;
     */
    public static final int BAD_CHANNEL_HEADER_VALUE = 20;
    /**
     * BAD_RESPONSE_PAYLOAD = 21;
     */
    public static final int BAD_RESPONSE_PAYLOAD_VALUE = 21;
    /**
     * BAD_RWSET = 22;
     */
    public static final int BAD_RWSET_VALUE = 22;
    /**
     * ILLEGAL_WRITESET = 23;
     */
    public static final int ILLEGAL_WRITESET_VALUE = 23;
    /**
     * INVALID_WRITESET = 24;
     */
    public static final int INVALID_WRITESET_VALUE = 24;
    /**
     * NOT_VALIDATED = 254;
     */
    public static final int NOT_VALIDATED_VALUE = 254;
    /**
     * INVALID_OTHER_REASON = 255;
     */
    public static final int INVALID_OTHER_REASON_VALUE = 255;

 

你可能感兴趣的:(代码示例)