本文介绍了访问Hyperledger区块数据的几种方法.
Hyperledger提供了多个REST API (https://hyperledger-fabric.readthedocs.io/en/latest/API/CoreAPI/#rest-api) 用于访问Hyperledger区块数据.
请求
http://192.168.99.100:7050/chain/响应
{ "height": 5, "currentBlockHash": "9UIs99F9Qq0RvwOzCEDG0SHvBrdoOHy0rexOICJ56aiIQbnszEO9y2QpjbMnrzIp3ehGh6W8FWY8eqbBFqaOVA==", "previousBlockHash": "AABHooEnVMuv4JO4roHqgcNAOf3mcUqOV6sTrCWeuPcKf/ji4tYXJ5cONgcQTwoIowf1wAqhZObFmjkT1QLmsQ==" }请求
http://192.168.99.100:7050/chain/blocks/4响应
{ "transactions": [ { "type": 2, "chaincodeID": "EgRteWNj", "payload": "CigIARIGEgRteWNjGhwKBmludm9rZQoHdGVzdEtleQoJdGVzdFZhbHVl", "txid": "4a05fbd2-f17d-46eb-abec-a6218b404117", "timestamp": { "seconds": 1476704491, "nanos": 740300402 }, "nonce": "jYFNo67ks3JhWh3PwxCJG0qK0rnBojwe", "cert": "MIICQTCCAeegAwIBAgIQMQ6RNvEkQKqWqqMx6vVJujAKBggqhkjOPQQDAzAxMQswCQYDVQQGEwJVUzEUMBIGA1UEChMLSHlwZXJsZWRnZXIxDDAKBgNVBAMTA3RjYTAeFw0xNjEwMDUxMTE5MjdaFw0xNzAxMDMxMTE5MjdaMEUxCzAJBgNVBAYTAlVTMRQwEgYDVQQKEwtIeXBlcmxlZGdlcjEgMB4GA1UEAxMXVHJhbnNhY3Rpb24gQ2VydGlmaWNhdGUwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASzqDkDtyveHNTcz0tD73PV5xvXFU0JpDxVvN8LEAAKtnP+TfkPT42EmERcSP+oOlzpfjBD8iGaOq4wQGbPckWKo4HMMIHJMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMA0GA1UdDgQGBAQBAgMEMA8GA1UdIwQIMAaABAECAwQwTQYGKgMEBQYHAQH/BECqjFoEQcSb6N1SGRE8uc03WMz4ccjSFfplLUrSXxdUUA81LciUswvKeDz8m3abk8z/pVc06nNFAcRWXkHfqzEJMDoGBioDBAUGCAQwN90tistCDXKOkUCQUjzKbZsrEy2A/iOgGtiHru7yVoIawPIeHfnfdfP+LEtCgMrkMAoGCCqGSM49BAMDA0gAMEUCIQC5ClRb0pPCdL+peOq/Cc6Z+Wy+KTq0mIB6FAEvy5gMcwIgFI1HWhlGGGjeqLTrLvcIeWamck2ISYImcIBZIfE+xlI=", "signature": "MEUCIHRnWCQmt+4QoiY01aiDU+0567xUgaiEZ6zZr+gDg8D2AiEA+OgCGGxWbQTkmC0IB4nJoiU65Dv/pR27XDirhtXgStU=" } ], "stateHash": "cxUZAjaQar85ZGvgt4z3soRMpQjJpDrpLBkFbwqQSVb1rebXcMT1kMzNA45MX9ybB/sILXCslN1C9yprmhAVng==", "previousBlockHash": "9UIs99F9Qq0RvwOzCEDG0SHvBrdoOHy0rexOICJ56aiIQbnszEO9y2QpjbMnrzIp3ehGh6W8FWY8eqbBFqaOVA==", "nonHashData": { "localLedgerCommitTimestamp": { "seconds": 1476704492, "nanos": 779081508 }, "chaincodeEvents": [ {} ] } }Hyperledger Java Client API (https://github.com/hyperledger/fabric-api)提供较为底层的接口来访问Hyperledger区块数据. 以下示例代码显示如何解析Invocation类的区块中的payload.
private void decodeChaincodeInvocationSpec() { try { String base64String = "CigIARIGEgRteWNjGhwKBmludm9rZQoHdGVzdEtleQoJdGVzdFZhbHVl"; byte[] decoded = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64String); Chaincode.ChaincodeInvocationSpec invocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(decoded); Map<Descriptors.FieldDescriptor, Object> fields = invocationSpec.getAllFields(); for (Descriptors.FieldDescriptor key : fields.keySet()) { Chaincode.ChaincodeSpec value = (Chaincode.ChaincodeSpec) fields.get(key); System.out.println("key:" + key); System.out.println("value:" + value); } } catch (Exception ex) { ex.printStackTrace(); } }相应的输出为:
key:protos.ChaincodeInvocationSpec.chaincodeSpec value:type: GOLANG chaincodeID { name: "mycc" } ctorMsg { args: "invoke" args: "testKey" args: "testValue" }本文介绍了使用Hyperledger REST和Java Client API访问Hyperledger区块数据的方法.
转载于:https://www.cnblogs.com/huyouhengbc/p/5989705.html