# RPC鏈嶅姟

## 姒傝堪

`RPCServer`閲囩敤`gRPC`瀹炵幇鐨勮繙绋嬭繃绋嬭皟鐢ㄧ郴缁燂紝閲囩敤`HTTP/2` 浼犺緭鍗忚锛屼娇鐢╜Protobuf` 浣滀负鎺ュ彛鎻忚堪璇█锛屽疄鐜扮郴缁熼棿鐨勯珮鏁堜氦浜掋€�

鍔熻兘涓婃敮鎸佸鐞嗚妭鐐硅姹傘€佸熀浜庢祦妯″紡鐨勬秷鎭闃咃紝閫氫俊涓婃敮鎸乣TLS`鍗曞悜鍜屽弻鍚戣璇併€佹祦鎺ф満鍒剁瓑銆�

## 閰嶇疆璇存槑

```yml
rpc:
  # 妯″紡锛屽綋鍓嶄粎鏀寔grpc鏂瑰紡
  provider: grpc
  # 鐩戝惉绔彛
  port: 12301
  # 妫€鏌ラ摼閰嶇疆TrustRoots璇佷功鍙樺寲鏃堕棿闂撮殧锛屽疄鐜拌嚜鍔ㄥ埛鏂帮紝鍗曚綅锛歴锛屾渶灏忓€间负10s
  check_chain_conf_trust_roots_change_interval: 60
  # 娴侀噺鎺у埗閰嶇疆锛岄噰鐢ㄤ护鐗屾《闄愭祦
  ratelimit:
    # rpc鎺ュ彛闄愰€熷姛鑳藉紑鍏�
    enabled: true
    # 闄愰€熺被鍨嬶細0-鍏ㄥ眬闄愰€燂紱1-鍩轰簬鏉ユ簮IP闄愰€�
    type: 0
    # 姣忕琛ュ厖浠ょ墝鏁帮紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙10000锛�
    token_per_second: -1
    # 浠ょ墝妗跺ぇ灏忥紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙10000锛�
    token_bucket_size: -1
  subscriber:
    # 鍘嗗彶娑堟伅璁㈤槄娴佹帶锛屽疄鏃舵秷鎭闃呬笉浼氳繘琛屾祦鎺�
    ratelimit:
      # 姣忕琛ュ厖浠ょ墝鏁帮紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙1000锛�
      token_per_second: 100
      # 浠ょ墝妗跺ぇ灏忥紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙1000锛�
      token_bucket_size: 100
  tls:
    # TLS妯″紡:
    #   disable - 涓嶅惎鐢═LS
    #   oneway  - 鍗曞悜璁よ瘉
    #   twoway  - 鍙屽悜璁よ瘉
    mode:           twoway
    priv_key_file:  ./certs/node/consensus1/consensus1.tls.key
    cert_file:      ./certs/node/consensus1/consensus1.tls.crt
  blacklist:            
    addresses:          
      # IP榛戝悕鍗曞湴鍧€濉啓浜庢锛岃嫢娌℃湁璇蜂负绌�
      #- "127.0.0.1"
```

## 鎺ュ彛瀹氫箟

```protobuf
service RpcNode {
	// 浜ゆ槗娑堟伅璇锋眰澶勭悊
	rpc SendRequest(common.TxRequest) returns (common.TxResponse) {};

	// 娑堟伅璁㈤槄璇锋眰澶勭悊
	rpc Subscribe(common.TxRequest) returns (stream common.SubscribeResult) {};

	// 鏇存柊鏃ュ織绾у埆
	rpc RefreshLogLevelsConfig(config.LogLevelsRequest) returns (config.LogLevelsResponse) {};

	// 鑾峰彇闀垮畨閾剧増鏈�
	rpc GetChainMakerVersion(config.ChainMakerVersionRequest) returns(config.ChainMakerVersionResponse) {};

	// 妫€鏌ラ摼閰嶇疆骞跺姩鎬佸姞杞芥柊閾�
	rpc CheckNewBlockChainConfig(config.CheckNewBlockChainConfigRequest) returns (config.CheckNewBlockChainConfigResponse) {};
}
```

## 鍏抽敭閫昏緫

### 浜ゆ槗璇锋眰缁撴瀯璇存槑

浜ゆ槗璇锋眰锛坄TxRequest`锛夊寘鍚氦鏄撳ご锛坄TxHeader`锛夈€乣Payload`鍜岀鍚嶏紙`Signature`锛夛紝`Payload`鏄瓧鑺傛暟缁勶紝鏍规嵁涓嶅悓鐨刞TxType`鍙互瑙g爜鎴愬悇绉嶇被鍨嬬殑`Payload`銆�


<img loading="lazy" src="../images/RPC-TransactionRequestStructure.png" style="zoom:50%;" />

### 娑堟伅璁㈤槄锛堜簨浠堕€氱煡锛夊疄鐜板師鐞�

<img loading="lazy" src="../images/RPC-subscribe.png" style="zoom:50%;" />

锛�1锛夎闃呰€呭彂璧锋秷鎭闃呰姹傦紝褰撳墠鏀寔璁㈤槄鍖哄潡娑堟伅鍜屼氦鏄撴秷鎭�

锛�2锛夊鏋滃彧鏄闃呭巻鍙叉暟鎹紝鐩存帴浠庤处鏈瓨鍌紙`Store`锛変腑鑾峰彇鍚庤繑鍥炵粰璁㈤槄鑰�

锛�3锛夊鏋滈渶瑕佽闃呭疄鏃舵暟鎹紝鍒欎細鏈塦Subscriber`鍙戣捣璁㈤槄浜嬩欢锛屽皢`chan`娉ㄥ唽鍒拌闃呰€呭垪琛ㄤ腑锛屽綋`Core`妯″潡鏈夋柊鍖哄潡浜х敓锛屼細鍙戦€佷簨浠堕€氱煡锛岄€氳繃`chan`閫氱煡鍒癭Subscriber`锛岄€氳繃`RPCServer`杩斿洖缁欒闃呰€�

锛�4锛夊鏋滈渶瑕佸悓鏃惰闃呭巻鍙插拰瀹炴椂鏁版嵁锛屽垯浼氬垎鍒粠璐︽湰瀛樺偍锛坄Store`锛変互鍙婃秷鎭闃呭彂甯冭€呰幏鍙栵紝鑰屽悗杩斿洖缁欒闃呰€�

锛�5锛夎嫢璁㈤槄娑堟伅鍙戦€佸畬锛宍RPCServer`浼氫富鍔ㄥ叧闂闃呴€氶亾锛岄伩鍏嶈祫婧愭氮璐�

### 闄愭祦璇存槑

`RPCServer`閲囩敤浠ょ墝妗舵柟寮忚繘琛岄檺娴侊紝鍙互瀵�**鎺ュ彛璇锋眰璋冪敤**鍜�**娑堟伅璁㈤槄浜嬩欢鎺ㄩ€�**鍒嗗埆杩涜闄愭祦锛屽悗鑰呬粎浼氬鍘嗗彶娑堟伅杩涜娴佹帶锛屽瀹炴椂娑堟伅鏈仛闄愬埗锛堝疄鏃舵秷鎭彲浠ラ€氳繃鎺ュ彛璇锋眰璋冪敤闄愭祦鏉ヨ繘琛屾祦鎺э級銆�

- 鎺ュ彛璇锋眰璋冪敤闄愭祦閰嶇疆

  > 鍏充簬闄愰€熺被鍨嬭鏄庯細
  >
  > - 0-鍏ㄥ眬闄愰€燂細鏋勯€犱竴涓叏灞€鐨勪护鐗屾《瀵硅薄锛屼笉鍖哄垎鏉ユ簮鏂癸紝绮掑害姣旇緝绮�
  > - 1-鍩轰簬鏉ユ簮IP闄愰€燂細涓轰笉鍚岀殑鏉ユ簮IP鏋勯€犲崟鐙殑浠ょ墝妗跺璞★紝鍙互姣旇緝绮剧‘鐨勯拡瀵规潵婧愭柟杩涜闄愰€�

```yaml
  # 娴侀噺鎺у埗閰嶇疆锛岄噰鐢ㄤ护鐗屾《闄愭祦
  ratelimit:
    # rpc鎺ュ彛闄愰€熷姛鑳藉紑鍏�
    enabled: true
    # 闄愰€熺被鍨嬶細0-鍏ㄥ眬闄愰€燂紱1-鍩轰簬鏉ユ簮IP闄愰€�
    type: 0
    # 姣忕琛ュ厖浠ょ墝鏁帮紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙10000锛�
    token_per_second: -1
    # 浠ょ墝妗跺ぇ灏忥紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙10000锛�
    token_bucket_size: -1
```

- 娑堟伅璁㈤槄浜嬩欢鎺ㄩ€侀檺娴侀厤缃�

```yaml
  subscriber:
    # 鍘嗗彶娑堟伅璁㈤槄娴佹帶锛屽疄鏃舵秷鎭闃呬笉浼氳繘琛屾祦鎺�
    ratelimit:
      # 姣忕琛ュ厖浠ょ墝鏁帮紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙1000锛�
      token_per_second: 100
      # 浠ょ墝妗跺ぇ灏忥紝鍙栧€硷細-1-涓嶅彈闄愶紱0-榛樿鍊硷紙1000锛�
      token_bucket_size: 100
```

### 璇锋眰瀹¤鏃ュ織

榛樿灏嗗湪`brief.log`鏃ュ織鏂囦欢杈撳嚭璇锋眰瀹¤鏃ュ織銆�

- 鏍煎紡

`LogDateTime|IP:Port|OrgId|ChainId|TxType|TxId|Timestamp|ContractName|Method|RetCode|RetCodeMsg|RetMsg`

- 涓句緥

```bash
2021-10-20 15:27:13.955 |127.0.0.1:43916|wx-org1.chainmaker.org|chain1|INVOKE_CONTRACT|1ddcc164dc594d75b2ca4fae7f7a6f5bcdc896cc00944b00b145a3eca8978521|1634714833|CONTRACT_MANAGE|INIT_CONTRACT|0|SUCCESS|OK

2021-10-20 15:27:15.995 |127.0.0.1:43916|wx-org1.chainmaker.org|chain1|INVOKE_CONTRACT|5ea9ee9d873d4b9b9e2d2456624ad1571d5ef4a2560d4af9aa1d627c3147d3d9|1634714835|claim001|save|0|SUCCESS|OK

2021-10-20 15:27:18.006 |127.0.0.1:43916|wx-org1.chainmaker.org|chain1|QUERY_CONTRACT|61219a5f85974b61954ab2027f42e174ca7ac969d08f445e8b0bc128f1047d04|1634714838|claim001|find_by_file_hash|0|SUCCESS|SUCCESS
```

- 鍏抽棴鏂规硶

瀹¤鏃ュ織閲囩敤`INFO`绾у埆杈撳嚭锛屽鏋滈渶瑕佸叧闂姹傚璁℃棩蹇楃殑杈撳嚭锛岃淇敼`log.yml`閰嶇疆鏂囦欢锛屽皢`brief`鏃ュ織绾у埆`log_level_default`璁剧疆涓篳WARN`鎴朻ERROR`銆�

```yaml
  brief:
    log_level_default: INFO
    file_path: ../log/brief.log
```

<br><br>