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

堵凯
2023-12-01

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

 public static List<Map> getRWSetFromBlock(BlockInfo blockInfo) throws InvalidProtocolBufferException {
        List<Map> 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<String, Object> 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<Map> rwList = new ArrayList<Map>();

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

                            Map<String, Object> rwMap = new HashMap<>();
                            Map<String, String> 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"
}

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

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

 

 类似资料: