From de035bd3bcaa29e8d7fb71173c414d1fefd0a447 Mon Sep 17 00:00:00 2001
From: huyingcong <huyingcong@internetapi.cn>
Date: Mon, 25 Nov 2019 21:23:49 +0800
Subject: [PATCH] add hash query
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Summary:
add TransactionCount/GetTransactions
增加关于 transaction/block 相关的查询接口

Test Plan: none

Reviewers: nex, wuyi

Reviewed By: nex

Differential Revision: https://phabricator.internetapi.cn/D25
---
 bdledger/api/ac_chain.proto      | 84 ------------------------------
 bdledger/api/common.proto        | 26 ++++++++++
 bdledger/api/query_service.proto | 89 ++++++++++++++++++++++++++++++++
 bdledger/api/tx_ledger.proto     | 28 +---------
 gen-go.bat                       |  2 +-
 gen-go.sh                        |  2 +-
 gen-web.bat                      |  2 +-
 gen-web.sh                       |  2 +-
 8 files changed, 120 insertions(+), 115 deletions(-)
 delete mode 100644 bdledger/api/ac_chain.proto
 create mode 100644 bdledger/api/query_service.proto

diff --git a/bdledger/api/ac_chain.proto b/bdledger/api/ac_chain.proto
deleted file mode 100644
index 289e17f..0000000
--- a/bdledger/api/ac_chain.proto
+++ /dev/null
@@ -1,84 +0,0 @@
-syntax = "proto3";
-
-package bdledger.api.acchain;
-
-import "google/protobuf/empty.proto";
-import "bdledger/api/common.proto";
-
-option go_package = "bdware.org/bdledger/sdk/api/grpc/acchain";
-option java_package = "bdledger.api.grpc.acchain";
-option java_outer_classname = "AccountingChainProto";
-option java_multiple_files = true;
-
-service AccountingChain {
-  rpc ClientVersion (google.protobuf.Empty) returns (common.ClientVersionResponse);
-  rpc BlockNumber (BlockNumberRequest) returns (BlockNumberResponse);
-  rpc GetBlockByNumber (GetBlockByNumberRequest) returns (Block);
-  rpc GetBlockByHash (GetBlockByHashRequest) returns (Block);
-  rpc GetTransactionByHash (GetTransactionByHashRequest) returns (Transaction);
-  rpc GetTransactionByBlockNumberAndIndex (GetTransactionByBlockNumberAndIndexRequest) returns (Transaction);
-  rpc GetTransactionByBlockHashAndIndex (GetTransactionByBlockHashAndIndexRequest) returns (Transaction);
-}
-
-message Transaction {
-  uint64 block_number = 1; // 事务所在的区块的区块号,当事务处于待确认状态时为`null`
-  bytes block_hash = 2; // 事务所在的区块的哈希,当事务处于待确认状态时为`null`
-  uint32 index = 3; // 事务在区块中的位置index,当事务处于待确认状态时为`null`
-  bytes hash = 4; // 事务的哈希
-  common.TransactionType type = 5; // 事务类型
-  bytes from = 6; // 发送账户地址
-  uint64 nonce = 7; // 这条事务之前发送者所发送的事务数量
-  bytes to = 8; // 接收账户地址,或者调用的合约地址,或者`null`如为合约创建
-  bytes data = 9; // 数据或合约代码
-  bytes v = 10; // ECDSA recovery id
-  bytes r = 11; // ECDSA signature r
-  bytes s = 12; // ECDSA signature s
-}
-
-message Block {
-  uint64 number = 1; // 区块号,当区块处于待确认状态时为`null`
-  bytes hash = 2; // 区块的哈希,当区块处于待确认状态时为`null`
-  bytes parent_hash = 3; // 父区块的哈希
-  repeated bytes witnesses = 4; // 见证者账户地址的数组
-  int64 timestamp = 5; // 区块产生时的UNIX时间戳
-  uint64 size = 6; // 区块大小的字节数
-  bytes transactions_root = 7; // 区块的事务树根
-  repeated Transaction transactions = 8; // 事务对象的数组,或为空
-  repeated bytes transaction_hashes = 9; // 20字节的交易哈希的数组,或为空
-}
-
-message BlockNumberRequest {
-  string ledger = 1;
-}
-message BlockNumberResponse {
-  uint64 block_number = 1;
-}
-
-message GetBlockByNumberRequest {
-  string ledger = 1;
-  uint64 number = 2;
-  bool full_transaction = 3;
-}
-
-message GetBlockByHashRequest {
-  string ledger = 1;
-  bytes hash = 2;
-  bool full_transaction = 3;
-}
-
-message GetTransactionByHashRequest {
-  string ledger = 1;
-  bytes hash = 2;
-}
-
-message GetTransactionByBlockNumberAndIndexRequest {
-  string ledger = 1;
-  uint64 block_number = 2;
-  uint32 index = 3;
-}
-
-message GetTransactionByBlockHashAndIndexRequest {
-  string ledger = 1;
-  bytes block_hash = 2;
-  uint32 index = 3;
-}
diff --git a/bdledger/api/common.proto b/bdledger/api/common.proto
index 53c5d13..80ca0f7 100644
--- a/bdledger/api/common.proto
+++ b/bdledger/api/common.proto
@@ -19,3 +19,29 @@ enum TransactionType {
   CONTRACT_INVOCATION = 3; // 合约调用
   CONTRACT_STATUS = 4; // 合约状态
 }
+
+message Transaction {
+  bytes block_hash = 1;     // 事务所在的区块的哈希,当事务处于待确认状态时为`null`
+  uint32 index = 2;         // 事务在区块中的位置 index,当事务处于待确认状态时为`null`
+  bytes hash = 3;           // 事务的哈希
+  common.TransactionType type = 4; // 事务类型
+  bytes from = 5;           // 发送账户地址
+  uint64 nonce = 6;         // 这条事务之前发送者所发送的事务数量
+  bytes to = 7;             // 接收账户地址,或者调用的合约地址,或者`null`如为合约创建
+  bytes data = 8;           // 数据或合约代码
+  bytes v = 9;              // ECDSA recovery id
+  bytes r = 10;             // ECDSA signature r
+  bytes s = 11;             // ECDSA signature s
+}
+
+message Block {
+  uint64 index = 1;                      // 事务链本地区块索引,当区块处于待确认状态时为`null`
+  bytes hash = 2;                        // 区块的哈希,当区块处于待确认状态时为`null`
+  repeated bytes parent_hashes = 3;      // 父区块的哈希
+  repeated bytes witnesses = 4;          // 见证者账户地址
+  int64 timestamp = 5;                   // 区块产生时的 UNIX 时间戳,单位为秒
+  uint64 size = 6;                       // 区块大小的字节数
+  bytes transactions_root = 7;           // 区块的事务树根
+  repeated Transaction transactions = 8; // 事务对象的数组,或为空
+  repeated bytes transaction_hashes = 9; // 20字节的交易哈希的数组,或为空
+}
\ No newline at end of file
diff --git a/bdledger/api/query_service.proto b/bdledger/api/query_service.proto
new file mode 100644
index 0000000..3474d41
--- /dev/null
+++ b/bdledger/api/query_service.proto
@@ -0,0 +1,89 @@
+syntax = "proto3";
+
+package bdledger.api.txledger;
+
+import "bdledger/api/common.proto";
+
+option go_package = "bdware.org/bdledger/sdk/api/grpc/txledger";
+option java_package = "bdledger.api.grpc.txledger";
+option java_outer_classname = "TransactionLedgerProto";
+option java_multiple_files = true;
+
+service Query {
+    rpc GetBlocks (BlocksRequest) returns (GetBlocksResponse);
+    rpc CountBlocks (BlocksRequest) returns (CountBlocksResponse);
+    rpc GetTransactions(TransactionsRequest) returns (GetTransactionsResponse);
+    rpc CountTransactions(TransactionsRequest) returns (CountTransactionsResponse);
+    rpc GetBlockByHash (GetBlockByHashRequest) returns (GetBlockByHashResponse);
+    rpc GetTransactionByHash (GetTransactionByHashRequest) returns (GetTransactionByHashResponse);
+    rpc GetTransactionByBlockHashAndIndex (GetTransactionByBlockHashAndIndexRequest) returns (GetTransactionByBlockHashAndIndexResponse);
+}
+
+// repeated Transaction/BlockFilters are combined by "&&"(and) operator;
+message TransactionFilter {
+    bytes hash = 1;
+    bytes from = 2;
+    bytes to = 3;
+    bytes timestamp = 4;
+}
+
+message BlockFilter {
+    bytes hash = 1;
+    int64 timestamp = 2;
+}
+
+message BlocksRequest {
+    string ledger = 1;
+    repeated BlockFilter filters = 2;
+    required int64 start_timestamp = 3;
+    int64 end_timestamp = 4;
+}
+message GetBlocksResponse {
+    repeated common.Block blocks= 1;
+    int64 start_timestamp = 2;
+    int64 end_timestamp = 3;
+}
+message CountBlocksResponse {
+    uint64 count = 1;
+}
+
+message TransactionsRequest {
+    string ledger = 1;
+    repeated TransactionFilter filters = 2;
+    required int64 start_timestamp = 3;
+    int64 end_timestamp = 4;
+}
+message GetTransactionsResponse {
+    repeated common.Transaction Transactions = 1;
+    int64 start_timestamp = 2;
+    int64 end_timestamp = 3;
+}
+message CountTransactionsResponse {
+    uint64 count = 1;
+}
+
+message GetBlockByHashRequest {
+    string ledger = 1;
+    bytes hash = 2;
+    bool full_transaction = 3;
+}
+message GetBlockByHashResponse {
+    common.Block block = 1;
+}
+
+message GetTransactionByHashRequest {
+    string ledger = 1;
+    bytes hash = 2;
+}
+message GetTransactionByHashResponse {
+    common.Transaction transaction = 1;
+}
+
+message GetTransactionByBlockHashAndIndexRequest {
+    string ledger = 1;
+    bytes block_hash = 2;
+    uint32 index = 3;
+}
+message GetTransactionByBlockHashAndIndexResponse {
+    common.Transaction transaction = 1;
+}
diff --git a/bdledger/api/tx_ledger.proto b/bdledger/api/tx_ledger.proto
index 87c3424..ab11ac9 100644
--- a/bdledger/api/tx_ledger.proto
+++ b/bdledger/api/tx_ledger.proto
@@ -19,32 +19,6 @@ service TransactionLedger {
   rpc SendTransaction (SendTransactionRequest) returns (SendTransactionResponse);
 }
 
-message Transaction {
-  bytes block_hash = 1;     // 事务所在的区块的哈希,当事务处于待确认状态时为`null`
-  uint32 index = 2;         // 事务在区块中的位置 index,当事务处于待确认状态时为`null`
-  bytes hash = 3;           // 事务的哈希
-  common.TransactionType type = 4; // 事务类型
-  bytes from = 5;           // 发送账户地址
-  uint64 nonce = 6;         // 这条事务之前发送者所发送的事务数量
-  bytes to = 7;             // 接收账户地址,或者调用的合约地址,或者`null`如为合约创建
-  bytes data = 8;           // 数据或合约代码
-  bytes v = 9;              // ECDSA recovery id
-  bytes r = 10;             // ECDSA signature r
-  bytes s = 11;             // ECDSA signature s
-}
-
-message Block {
-  uint64 index = 1;                      // 事务链本地区块索引,当区块处于待确认状态时为`null`
-  bytes hash = 2;                        // 区块的哈希,当区块处于待确认状态时为`null`
-  repeated bytes parent_hashes = 3;      // 父区块的哈希
-  repeated bytes witnesses = 4;          // 见证者账户地址
-  int64 timestamp = 5;                   // 区块产生时的 UNIX 时间戳,单位为秒
-  uint64 size = 6;                       // 区块大小的字节数
-  bytes transactions_root = 7;           // 区块的事务树根
-  repeated Transaction transactions = 8; // 事务对象的数组,或为空
-  repeated bytes transaction_hashes = 9; // 20字节的交易哈希的数组,或为空
-}
-
 message CreateLedgerRequest {
   string name = 1;
 }
@@ -70,7 +44,7 @@ message GetBlocksRequest {
   bool full_transaction = 4;
 }
 message GetBlocksResponse {
-  repeated Block blocks = 1;
+  repeated common.Block blocks = 1;
 }
 
 message SendTransactionRequest {
diff --git a/gen-go.bat b/gen-go.bat
index c4a07f2..e9314cf 100644
--- a/gen-go.bat
+++ b/gen-go.bat
@@ -12,6 +12,6 @@ if not exist %dir% mkdir %dir%
 %exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/common.proto
 %exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/error_details.proto
 %exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/tx_ledger.proto
-%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/ac_chain.proto
+%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/query_service.proto
 
 echo all done
diff --git a/gen-go.sh b/gen-go.sh
index 2fe8e90..1ecec3b 100644
--- a/gen-go.sh
+++ b/gen-go.sh
@@ -3,4 +3,4 @@ mkdir -p gen/go
 protoc -I . --go_out=plugins=grpc:gen/go bdledger/api/common.proto
 protoc -I . --go_out=plugins=grpc:gen/go bdledger/api/error_details.proto
 protoc -I . --go_out=plugins=grpc:gen/go bdledger/api/tx_ledger.proto
-protoc -I . --go_out=plugins=grpc:gen/go bdledger/api/ac_chain.proto
+protoc -I . --go_out=plugins=grpc:gen/go bdledger/api/query_service.proto
diff --git a/gen-web.bat b/gen-web.bat
index d70b493..6a7d7f3 100644
--- a/gen-web.bat
+++ b/gen-web.bat
@@ -10,6 +10,6 @@ if not exist %dir% mkdir %dir%
 %exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/common.proto
 %exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/error_details.proto
 %exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/tx_ledger.proto
-%exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/ac_chain.proto
+%exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/query_service.proto
 
 echo all done
diff --git a/gen-web.sh b/gen-web.sh
index 34761df..341c76a 100644
--- a/gen-web.sh
+++ b/gen-web.sh
@@ -4,4 +4,4 @@ mkdir -p $dir
 protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/common.proto
 protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/error_details.proto
 protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/tx_ledger.proto
-protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/ac_chain.proto
+protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/query_service.proto