Derek解讀Bytom原始碼-protobuf生成比原核心程式碼

比原鏈Bytom發表於2018-08-23

作者:Derek

簡介

Github地址:https://github.com/Bytom/bytom

Gitee地址:https://gitee.com/BytomBlockchain/bytom

本章介紹bytom程式碼Api-Server介面服務

作者使用MacOS作業系統,其他平臺也大同小異

Golang Version: 1.8

protobuf生成比原核心程式碼

protobuf介紹

Protocol buffers是一個靈活的、高效的、自動化的用於對結構化資料進行序列化的協議。Protocol buffers序列化後的碼流更小、速度更快、操作更簡單。只需要將序列化的資料結構(.proto檔案),便可以生成的原始碼。

protobuf 3.0語法介紹

protobuf 語法

protobuf 安裝

安裝protobuf 3.4.0

protobuf download

./configure
make
make install
protoc —version

安裝grpc-go

export PATH=$PATH:$GOPATH/bin
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/protoc-gen-go

檢視比原bc.proto核心檔案

protocol/bc/bc.proto

syntax = "proto3";

package bc;

message Hash {
  fixed64 v0 = 1;
  fixed64 v1 = 2;
  fixed64 v2 = 3;
  fixed64 v3 = 4;
}

message Program {
  uint64 vm_version = 1;
  bytes  code       = 2;
}

// This message type duplicates Hash, above. One alternative is to
// embed a Hash inside an AssetID. But it`s useful for AssetID to be
// plain old data (without pointers). Another alternative is use Hash
// in any protobuf types where an AssetID is called for, but it`s
// preferable to have type safety.
message AssetID {
  fixed64 v0 = 1;
  fixed64 v1 = 2;
  fixed64 v2 = 3;
  fixed64 v3 = 4;
}

message AssetAmount {
  AssetID asset_id = 1;
  uint64  amount   = 2;
}

message AssetDefinition {
  Program issuance_program = 1;
  Hash    data             = 2;
}

message ValueSource {
  Hash        ref      = 1;
  AssetAmount value    = 2;
  uint64      position = 3;
}

message ValueDestination {
  Hash        ref      = 1;
  AssetAmount value    = 2;
  uint64      position = 3;
}

message BlockHeader {
  uint64            version                 = 1;
  uint64            height                  = 2;
  Hash              previous_block_id       = 3;
  uint64            timestamp               = 4;
  Hash              transactions_root       = 5;
  Hash              transaction_status_hash = 6;
  uint64            nonce                   = 7;
  uint64            bits                    = 8;
  TransactionStatus transaction_status      = 9;
}

message TxHeader {
  uint64        version         = 1;
  uint64        serialized_size = 2;
  uint64        time_range      = 3;
  repeated Hash result_ids      = 4;
}

message TxVerifyResult {
  bool status_fail = 1;
}

message TransactionStatus {
  uint64                  version        = 1;
  repeated TxVerifyResult verify_status  = 2;
}

message Mux {
  repeated ValueSource      sources              = 1; // issuances, spends, and muxes
  Program                   program              = 2;
  repeated ValueDestination witness_destinations = 3; // outputs, retirements, and muxes
  repeated bytes            witness_arguments    = 4;
}

message Coinbase {
  ValueDestination witness_destination = 1;
  bytes            arbitrary           = 2;
}

message Output {
  ValueSource source          = 1;
  Program     control_program = 2;
  uint64      ordinal         = 3;
}

message Retirement {
  ValueSource source   = 1;
  uint64      ordinal  = 2;
}

message Issuance {
  Hash             nonce_hash               = 1;
  AssetAmount      value                    = 2;
  ValueDestination witness_destination      = 3;
  AssetDefinition  witness_asset_definition = 4;
  repeated bytes   witness_arguments        = 5;
  uint64           ordinal                  = 6;
}

message Spend {
  Hash             spent_output_id     = 1;
  ValueDestination witness_destination = 2;
  repeated bytes   witness_arguments   = 3;
  uint64           ordinal             = 4;
}

根據bc.proto生成bc.pb.go程式碼

protoc -I/usr/local/include -I. 
      -I${GOPATH}/src 
      --go_out=plugins=grpc:. 
      ./*.proto

執行完上面命令,我們會看到當前目錄下生成的bc.pb.go檔案,該檔案在比原鏈中承載這block、transaction、coinbase等重要資料結構

相關文章