/*
 * Copyright  2019 Blockchain Technology and Application Joint Lab, Linkel Technology Co., Ltd, Beijing, Fintech Research Center of ISCAS.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BA SIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

syntax = "proto3";
package rep.protos;
option java_package = "rep.protos";
import "google/protobuf/timestamp.proto";
/*
*/
//Event 用于图形展示的事件
message Event {
    //来源addr
    string from = 1;
    //发送addr，如果是广播（需要定义一个）
    string to = 2;
    //event事件
    enum Action {
        SUBSCRIBE_TOPIC = 0;
        TRANSACTION = 1;
        BLOCK_NEW = 2;
        BLOCK_ENDORSEMENT = 3;
        ENDORSEMENT = 4;
        MEMBER_UP = 5;
        MEMBER_DOWN = 6;
        CANDIDATOR = 7;
        GENESIS_BLOCK = 8;
        BLOCK_SYNC = 9;
        BLOCK_SYNC_DATA = 10;
        BLOCK_SYNC_SUC = 11;
    }
    Action action = 3;
    Block blk = 4;
}

//账户
message Signer {
    string name = 1; //注册者的名称或者昵称
    string credit_code = 2; //社会信用代码（个人或机构）或者DID，推荐使用DID
    string mobile = 3; // 手机号码,用于接收通知
    repeated string cert_names = 4; //存放用户所有证书的名称，用户具体的证书单独存放，方便证书的操作，用户在增加证书的时候，在这个列表中增加证书的名称，did_certname。
    //map<string, Certificate> certs = 4; //签名者证书标识，可用于下载证书
}

//账户证书标示
message CertId {
    string credit_code = 1;
    string cert_name = 2;//当前用户注册证书的名称
}

//账户证书标示
message Certificate {
    string 	certificate = 1; //内含签发机构
    string 	alg_type = 2;//直接填写具体算法，如:ECDSA下的SHA1withECDSA,SHA512withECDSA等
    bool   	cert_valid = 3;//证书是否有效，true 有效；false 无效；protobuf默认该值为false
    google.protobuf.Timestamp reg_Time = 4; //注册时间
    google.protobuf.Timestamp unreg_Time = 5; //注销时间
}

//背书，可用于Transaction和Block
message Signature {
    //证书标识
    CertId cert_id = 1;
    google.protobuf.Timestamp tm_local = 2; //签名时间
    //签名
     bytes signature = 3;
}

//合约方法调用及实际参数
message ChaincodeInput {
    string function = 1;
    repeated string args  = 2;
}
//指定合约名称和版本,确定唯一的合约代码
message ChaincodeId {
    string chaincodeName = 1;
    int32 version = 2;
}
// 合约定义,部署/升级
message ChaincodeDeploy {
	//Type only support javascript 
    int32 timeout = 1;
    //完整的代码内容
    string code_package = 2;
    //合约规则的法律描述文本
    string legal_prose =3;
    enum CodeType {
	CODE_UNDEFINED = 0;
        CODE_JAVASCRIPT = 1;
        CODE_SCALA = 2;
        CODE_SCALA_PARALLEL = 3;
    }
    CodeType ctype = 4;
} 

/*
* 交易
*/
message Transaction {
    string id = 1;
    enum Type {
        UNDEFINED = 0;
        // deploy a chaincode to the network and call `Init` function
        CHAINCODE_DEPLOY = 1;
        // call a chaincode `Invoke` function as a transaction
        CHAINCODE_INVOKE = 2;
        CHAINCODE_SET_STATE = 3;
    }
    Type type = 2;
    //store ChaincodeID as bytes so its encrypted value can be stored
    ChaincodeId cid = 3;
    oneof para {
        ChaincodeDeploy spec = 4;
        ChaincodeInput ipt = 5; 
        bool state = 6;
    }
    Signature signature = 7;
}

message Block {
    // version - 区块数据版本,以便数据格式升级
    uint32 version = 1;
    // 区块高度, 从1开始
    uint64 height = 2;
    //顺序排列的交易
    repeated Transaction transactions = 3;
    //交易执行日志及输出指令
    repeated TransactionResult transactionResults = 4;
    //区块的hash。在计算块hash的时候，不包括签名信息；在签名时需要包括块hash。
    bytes hashOfBlock = 5;
    // previousBlockHash - The hash of the previous block in the chain.
    bytes previousBlockHash = 6;
    //背书签名集合,必须包含出块人签名
    repeated Signature endorsements=7;
    bytes stateHash = 8;
}

//交易执行增量日志
message OperLog{
	string key =1;
	bytes oldValue =2;
	bytes newValue=3;
}
message ActionResult{
	int32 code = 1;
	string reason = 2;
}
//交易结果(背书中针对各个交易再进行单独背书)
//背书请求的应答（针对block中的各个交易）
message TransactionResult {
    //唯一标识一个Transaction
  string txId = 1;
  repeated OperLog ol = 2;
  ActionResult result = 3;
}


message BlockchainInfo {
    uint64 height = 1;
    uint64 totalTransactions = 2;
    bytes  currentBlockHash = 3;
    bytes  previousBlockHash = 4;
    bytes  currentStateHash = 5;
}


//zhj
message MPbftPrepare {
    Signature signature = 1;
}

//zhj
message MPbftCommit {
    repeated MPbftPrepare prepares = 1;
    Signature signature = 2;
}

//zhj
message MPbftReply {
    repeated MPbftCommit commits = 1;
    Signature signature = 2;
}
