Restructure project

This commit is contained in:
Nex
2020-04-07 21:28:09 +08:00
parent af67a473c9
commit 935d130a98
16 changed files with 114 additions and 84 deletions

63
grpc/README.md Normal file
View File

@@ -0,0 +1,63 @@
## BDLedger API
### Documentation
See [API documentation](..\docs\apis.md)
### Development
#### Generating gRPC code in languages
##### Installling tools
Download the [Protocol Buffers compiler](https://github.com/protocolbuffers/protobuf/releases) (protoc-3.11.4-*) for your platform and add `protoc` to `PATH` environment variable
For generating Go code:
```bash
GIT_TAG="v1.4.0-rc.4"
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go
```
(Update to google.golang.org/protobuf when https://github.com/grpc/grpc-go/pull/3435 resolves)
For generating Java code: From `bdledger-sdk-java` run `gradle generateProto`
For generating Node.js code, install Node.js then run:
```bash
npm install -g grpc-tools
```
For generating TypeScript definitions, also run:
```bash
npm install -g protoc-gen-ts
```
For frontend JavaScript:
```bash
git clone https://github.com/grpc/grpc-web
cd grpc-web
sudo make install-plugin
```
References:
- [Go Quick Start](https://grpc.io/docs/quickstart/go.html#prerequisites)
- [gRPC Go FAQ](https://github.com/grpc/grpc-go#faq)
**TODO: Look into [protobuf.js](https://github.com/protobufjs/protobuf.js) for Nodes.js and frontend**
**TODO: Look into [Kroto+](https://github.com/marcoferrer/kroto-plus) for Kotlin**
##### Generating code
Run:
```bash
./scripts/gen [go] [nodejs] [ts]
```
#### Generateing documentation
Install Go then run:
```bash
go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
./scripts/gen docs
```

View File

@@ -0,0 +1,65 @@
syntax = "proto3";
package bdware.bdledger.api;
option go_package = "bdware.org/bdledger/api/grpc/pb";
option java_package = "org.bdware.bdledger.api.grpc.pb";
/* 事务类型 */
enum TransactionType {
RECORD = 0; // 通用数据记录
MESSAGE = 1; // 消息
CONTRACT_CREATION = 2; // 合约创建
CONTRACT_INVOCATION = 3; // 合约调用
CONTRACT_STATUS = 4; // 合约状态
}
message Transaction {
bytes block_hash = 1; // 事务所在的区块的哈希,当事务处于待确认状态时为`null`
uint32 index = 2; // 事务在区块中的位置 index当事务处于待确认状态时为`null`
bytes hash = 3; // 事务的哈希
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 {
bytes hash = 1; // 区块的哈希,当区块处于待确认状态时为`null`
repeated bytes parent_hashes = 2; // 父区块的哈希
repeated bytes witnesses = 3; // 见证者账户地址
int64 timestamp = 4; // 区块产生时的 UNIX 时间戳,单位为秒
uint64 size = 5; // 区块大小的字节数
uint32 transaction_count = 6; // 区块包含的事务数量
bytes transactions_root = 7; // 区块的事务默克尔树根
repeated Transaction transactions = 8; // 事务对象的数组,或为空
repeated bytes transaction_hashes = 9; // 20字节的交易哈希的数组或为空
}
message Contract {
bytes contractName = 1; //合约名称
uint32 randomNum = 2; //合约执行的节点数量
bytes operation = 3; //合约方法
bytes arg = 4; //合约方法参数
bytes path = 5; //合约文件路径(合约在IDE工程的相对路径)
bytes content = 6; //合约内容(可为合约文件相对路径/合约脚本)
bytes pubkey = 7; //用户公钥
enum ContractUnitRequestType{
START = 0;
STOP = 1;
EXECUTE = 2;
REPLY = 3;
REQUEST = 4;
PREPREPARE = 5;
PREPARE = 6;
COMMIT = 7;
ADDPEER = 8;
DROPPEER = 9;
STATESYNC = 10;
}
}

View File

@@ -0,0 +1,26 @@
syntax = "proto3";
package bdware.bdledger.api;
option go_package = "bdware.org/bdledger/api/grpc/pb";
option java_package = "org.bdware.bdledger.api.grpc.pb";
// InvalidArgument indicates client specified an invalid argument.
// Note that this differs from FailedPrecondition. It indicates arguments
// that are problematic regardless of the state of the system
// (e.g., a malformed file name).
message InvalidArgument {
// A message type used to describe a single invalid field.
message FieldViolation {
// A path leading to a field in the request body. The value will be a
// sequence of dot-separated identifiers that identify a protocol buffer
// field. E.g., "field_violations.field" would identify this field.
string field = 1;
// A description of why the request element is bad.
string description = 2;
}
// Describes all violations in a client request.
repeated FieldViolation field_violations = 1;
}

View File

@@ -0,0 +1,40 @@
syntax = "proto3";
package bdware.bdledger.api;
import "google/protobuf/empty.proto";
import "bdware/bdledger/api/common.proto";
option go_package = "bdware.org/bdledger/api/grpc/pb";
option java_package = "org.bdware.bdledger.api.grpc.pb";
service Ledger {
rpc CreateLedger (CreateLedgerRequest) returns (CreateLedgerResponse);
rpc GetLedgers (google.protobuf.Empty) returns (GetLedgersResponse);
rpc SendTransaction (SendTransactionRequest) returns (SendTransactionResponse);
}
message CreateLedgerRequest {
string name = 1;
}
message CreateLedgerResponse {
bool ok = 1;
}
message GetLedgersResponse {
repeated string ledgers = 1;
}
message SendTransactionRequest {
string ledger = 1;
message Transaction {
TransactionType type = 1;
bytes from = 2;
bytes to = 3;
bytes data = 4;
}
Transaction transaction = 2;
}
message SendTransactionResponse {
bytes hash = 1;
}

View File

@@ -0,0 +1,16 @@
syntax = "proto3";
package bdware.bdledger.api;
import "google/protobuf/empty.proto";
option go_package = "bdware.org/bdledger/api/grpc/pb";
option java_package = "org.bdware.bdledger.api.grpc.pb";
service Node {
rpc ClientVersion (google.protobuf.Empty) returns (ClientVersionResponse);
}
message ClientVersionResponse {
string version = 1; // 节点客户端版本
}

View File

@@ -0,0 +1,97 @@
syntax = "proto3";
package bdware.bdledger.api;
import "bdware/bdledger/api/common.proto";
option go_package = "bdware.org/bdledger/api/grpc/pb";
option java_package = "org.bdware.bdledger.api.grpc.pb";
service Query {
rpc GetBlockByHash (GetBlockByHashRequest) returns (GetBlockByHashResponse);
rpc GetBlocks (BlocksRequest) returns (GetBlocksResponse); // start_timestamp is required
rpc CountBlocks (BlocksRequest) returns (CountBlocksResponse);
rpc GetTransactionByHash (GetTransactionByHashRequest) returns (GetTransactionByHashResponse);
rpc GetTransactionByBlockHashAndIndex (GetTransactionByBlockHashAndIndexRequest) returns (GetTransactionByBlockHashAndIndexResponse);
rpc GetTransactions (TransactionsRequest) returns (GetTransactionsResponse);
rpc CountTransactions (TransactionsRequest) returns (CountTransactionsResponse);
}
message BlockFilter {
bytes hash = 1;
int64 timestamp = 2;
}
// repeated Transaction/BlockFilters are combined by "&&"(and) operator;
message TransactionFilter {
bytes hash = 1;
bytes from = 2;
bytes to = 3;
bytes timestamp = 4;
}
message GetBlockByHashRequest {
string ledger = 1;
bytes hash = 2;
bool full_transactions = 3;
}
message GetBlockByHashResponse {
Block block = 1;
}
message BlocksRequest {
string ledger = 1;
int64 start_timestamp = 2;
int64 end_timestamp = 3;
repeated BlockFilter filters = 4;
enum IncludeTransactions {
NONE = 0; // 不包含交易数据
HASH = 1; // 包含交易哈希列表
FULL = 2; // 包含完整交易列表
}
IncludeTransactions include_transactions = 5;
}
message GetBlocksResponse {
repeated Block blocks = 1;
int64 start_timestamp = 2;
int64 end_timestamp = 3;
}
message CountBlocksResponse {
uint64 count = 1;
int64 start_timestamp = 2;
int64 end_timestamp = 3;
}
message GetTransactionByHashRequest {
string ledger = 1;
bytes hash = 2;
}
message GetTransactionByHashResponse {
Transaction transaction = 1;
}
message GetTransactionByBlockHashAndIndexRequest {
string ledger = 1;
bytes block_hash = 2;
uint32 index = 3;
}
message GetTransactionByBlockHashAndIndexResponse {
Transaction transaction = 1;
}
message TransactionsRequest {
string ledger = 1;
int64 start_timestamp = 2; // required
int64 end_timestamp = 3;
repeated TransactionFilter filters = 4;
}
message GetTransactionsResponse {
repeated Transaction transactions = 1;
int64 start_timestamp = 2;
int64 end_timestamp = 3;
}
message CountTransactionsResponse {
uint64 count = 1;
int64 start_timestamp = 2;
int64 end_timestamp = 3;
}

View File

@@ -0,0 +1,52 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto3";
package google.protobuf;
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option go_package = "github.com/golang/protobuf/ptypes/empty";
option java_package = "com.google.protobuf";
option java_outer_classname = "EmptyProto";
option java_multiple_files = true;
option objc_class_prefix = "GPB";
option cc_enable_arenas = true;
// A generic empty message that you can re-use to avoid defining duplicated
// empty messages in your APIs. A typical example is to use it as the request
// or the response type of an API method. For instance:
//
// service Foo {
// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
// }
//
// The JSON representation for `Empty` is empty JSON object `{}`.
message Empty {}

11
grpc/scripts/gen-go.sh Normal file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
cd "${0%/*}/.."
dir=gen/nodejs
mkdir -p $dir
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/common.proto
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/error_details.proto
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/node.proto
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/ledger.proto
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/query.proto
protoc -I . --go_out=plugins=grpc:$dir bdledger/api/contract.proto

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
cd "${0%/*}/.."
dir=gen/nodejs
pluginPath=$(which grpc_tools_node_protoc_plugin)
mkdir -p $dir
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/common.proto
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/error_details.proto
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/node.proto
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/ledger.proto
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/query.proto
protoc -I . --js_out=import_style=commonjs,binary:$dir --grpc_out=$dir --plugin=protoc-gen-grpc="$pluginPath" bdledger/api/contract.proto

18
grpc/scripts/gen-web.bat Normal file
View File

@@ -0,0 +1,18 @@
@echo off
cd /d "%~dp0.."
set dir=.\gen\web
set exe=protoc
set gen=protoc-protoc-gen-grpc-web
where /q %exe% || echo missing %exe% && exit /B
where /q %gen% || echo missing %gen% && exit /B
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/node.proto
%exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/ledger.proto
%exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/query.proto
%exe% -I . --js_out=import_style=commonjs:%dir% --grpc-web_out=import_style=commonjs,mode=grpcwebtext:%dir% bdledger/api/contract.proto
echo all done

10
grpc/scripts/gen-web.sh Normal file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
cd "${0%/*}/.."
dir=gen/web
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/node.proto
protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/ledger.proto
protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/query.proto
protoc -I . --js_out=import_style=commonjs:$dir --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$dir bdledger/api/contract.proto

47
grpc/scripts/gen.bat Normal file
View File

@@ -0,0 +1,47 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0..\pb"
where /q protoc || echo Missing protoc && exit /b
set gen=..\gen
set pbs=bdware/bdledger/api/*.proto
REM set protos=%pkg%/common.proto %pkg%/error_details.proto %pkg%/node.proto %pkg%/ledger.proto %pkg%/query.proto %pkg%/contract.proto
for %%A in (%*) do (
if "%%A"=="go" (
set plugin=protoc-gen-go
where /q !plugin! || echo Missing !plugin! && exit /b
set out=%gen%\go
if not exist !out! mkdir !out!
echo Generating Go code
start /b protoc --go_out=plugins=grpc:!out! %pbs%
)
if "%%A"=="nodejs" (
set plugin=grpc_tools_node_protoc_plugin
where /q !plugin! || echo missing !plugin! && exit /b
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
if not exist !out! mkdir !out!
echo Generating Node.js code
start /b protoc --js_out=import_style=commonjs,binary:!out! --grpc_out=!out! --plugin=protoc-gen-grpc=!pluginPath! %pbs% google/protobuf/empty.proto
)
if "%%A"=="ts" (
set plugin=protoc-gen-ts
where /q !plugin! || echo missing !plugin! && exit /B
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
if not exist !out! mkdir !out!
echo Generating TypeScript definitions
start /b protoc --ts_out=!out! --plugin=protoc-gen-ts=!pluginPath! %pbs% google/protobuf/empty.proto
)
if "%%A"=="docs" (
set plugin=protoc-gen-ts
where /q !plugin! || echo missing !plugin! && exit /B
set out=..\..\docs
if not exist !out! mkdir !out!
echo Generating documentation
start /b protoc --doc_out=!out! --doc_opt=html,apis.html %pbs% google/protobuf/empty.proto
start /b protoc --doc_out=!out! --doc_opt=markdown,apis.md %pbs% google/protobuf/empty.proto
start /b protoc --doc_out=!out! --doc_opt=json,apis.json %pbs% google/protobuf/empty.proto
)
)

5
grpc/scripts/gen.sh Normal file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
cd "${0%/*}/.."
# TODO