本文介紹了訪問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.
privatevoiddecodeChaincodeInvocationSpec() {
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區塊數據的方法.