# 棰勮█鏈烘湇鍔℃妧鏈柟妗�

## 姒傝堪
`闀垮畨閾韭烽瑷€鏈篳v1.0.0_alpha鐗堟湰鏄竴绉嶄腑蹇冨寲鐨勫彲鍒嗗竷寮忛儴缃茬殑棰勮█鏈猴紝涓洪暱瀹夐摼閾句笂銆侀摼涓嬫暟鎹殑浜や簰鎻愪緵浜嗕竴涓ˉ姊併€傜敤鎴峰彲浠ラ€氳繃鏅鸿兘鍚堢害璋冪敤鏉ヨ幏鍙栭摼涓嬬殑鎺ュ彛鏁版嵁銆佹寚瀹氱殑鏁版嵁搴撴暟鎹€乂RF锛堝彲楠岃瘉闅忔満鏁版暟鎹級浠ュ強鍊熷姪鏅鸿兘鍚堢害鍋氳法閾炬暟鎹殑鏌ヨ锛屽悓鏃朵篃鎻愪緵浜嗘敞鍐屾煡璇opic/鎸夌収topic鏌ヨ鐨勫姛鑳戒互绠€鍖栫敤鎴蜂娇鐢ㄣ€�

## 璁捐  

### 鏁翠綋娴佺▼  
- 鐢ㄦ埛鍦ㄦ櫤鑳藉悎绾︿腑璋冪敤`棰勮█鏈烘櫤鑳藉悎绾涓殑鍙栨暟鎹柟娉曪紝`棰勮█鏈烘櫤鑳藉悎绾鏍¢獙鐩稿簲鍙傛暟锛屽彂閫佺浉搴旂殑閾句簨浠�
- `棰勮█鏈烘湇鍔鐩戝惉鍒伴摼涓婂彇鏁版嵁浜嬩欢锛屾牴鎹悎绾︿腑鎸囧畾鐨勫彇鏁版嵁閰嶇疆锛岃繘琛屾暟鎹殑鑾峰彇
- `棰勮█鏈烘湇鍔灏嗗彇鍒版暟鎹€氳繃璋冪敤`棰勮█鏈烘櫤鑳藉悎绾鏂规硶鏉ュ皢鏁版嵁鍥炰紶锛宍棰勮█鏈烘櫤鑳藉悎绾閫氳繃鍥炶皟鐢ㄦ埛鏅鸿兘鍚堢害鍛婄煡鐢ㄦ埛鏁版嵁鍙栧埌锛岀敤鎴峰悎绾﹀仛鐩稿簲涓氬姟澶勭悊鍗冲彲     
![棰勮█鏈烘暣浣撴祦绋媇(../images/oracle-flow.png)  
<center> 棰勮█鏈烘暣浣撴祦绋�</center>

### 棰勮█鏈烘櫤鑳藉悎绾﹁璁�
- 棰勮█鏈哄姛鑳界敱`棰勮█鏈烘櫤鑳藉悎绾鎻愪緵鎺ュ彛鐨勬柟寮忔潵瀹炵幇銆俙鐢ㄦ埛鏅鸿兘鍚堢害`閫氳繃璋冪敤鎺ュ彛鏉ヨ幏鍙栨暟鎹€備负鏂逛究鐢ㄦ埛浣跨敤锛岀郴缁熸彁渚涗簡澶嶆潅鏌ヨ鐨勮鍒跺寲鍔熻兘锛屽彲浠ョ敱`绠$悊鍛榒鏉ヨ繘琛屾煡璇㈢殑璁㈠埗锛岀畝鍖栨櫘閫氱敤鎴风殑鏌ヨ銆�  
![棰勮█鏈烘櫤鑳藉悎绾︿氦浜抅(../images/oracle-contract-interact.png)    
<center> 棰勮█鏈烘櫤鑳藉悎绾︿氦浜�</center>      

`绠$悊鍛榒鍗充负鏄瑷€鏈哄悎绾﹀畨瑁呬汉鍛�,濡備笅鍥句负棰勮█鏈烘櫤鑳藉悎绾︾殑閴存潈鏂规       
![棰勮█鏈烘櫤鑳藉悎绾︾殑閴存潈鏂规](../images/oracle-contract-rbac.png)
<center> 棰勮█鏈烘櫤鑳藉悎绾︾殑閴存潈鏂规</center>

### 棰勮█鏈烘湇鍔¤璁�
   `棰勮█鏈烘湇鍔鏁翠綋鍙互鍒嗕负鍥涗釜妯″潡銆傜敱鐩戝惉鍒扮殑閾句笂`棰勮█鏈烘櫤鑳藉悎绾浜嬩欢杩涜椹卞姩,鍚勪釜妯″潡閰嶅悎杩愯瀹屾垚鍙栨暟鎹姛鑳姐€傛湇鍔″皢杩愯涓墍闇€鏁版嵁淇濆瓨鍒版湰鍦扮殑`mysql`鏁版嵁搴撲腑銆�
- 棰勮█鏈虹姸鎬佹祦杞ā鍧�
- 鍙栨暟鎹ā鍧�
- 棰勮█鏈烘帴鍙fā鍧�  
- 棰勮█鏈哄憡璀︽ā鍧�   
![棰勮█鏈篠erver缁撴瀯](../images/oracle-server-arch.png)
<center> 棰勮█鏈篠erver缁撴瀯</center>   

#### 棰勮█鏈鸿〃璁捐
`棰勮█鏈烘湇鍔灏嗘湇鍔¤繍琛屾墍闇€鏁版嵁瀛樺偍鍒颁簡`mysql`鏁版嵁搴撲腑銆�  
- `鍚堢害琛�(oracle_contracts)`涓昏璁板綍閫氳繃鎺ュ彛瀹夎鐨勯瑷€鏈哄悎绾︿俊鎭€傚悎绾︾殑瀹夎銆佸崌绾ч兘浼氬啓鍏ュ埌琛ㄤ腑銆俙棰勮█鏈烘湇鍔鐩戝惉閾句笂鐨勪簨浠堕渶瑕佹彁渚涘簭鍙凤紝鍥犳鍦╜棰勮█鏈烘湇鍔澶勭悊瀹岀浉鍏充簨浠跺悗锛屼篃浼氬皢浜嬩欢搴忓彿鐨勫彉鍖栨洿鏂板埌琛ㄤ腑
```sql
CREATE TABLE  if NOT exists `oracle_contracts`(
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '鑷id',
  `chain_alias` varchar(128) NOT NULL COMMENT '閾惧埆鍚�',
  `chain_id` varchar(128) NOT NULL COMMENT '閾緄d',
  `contract_name` varchar(128) NOT NULL COMMENT '鍚堢害鍚嶇О',
  `contract_version` int NOT NULL DEFAULT '1' COMMENT '鍚堢害鐗堟湰',
  `deployed` tinyint NOT NULL DEFAULT '0' COMMENT '鏄惁閮ㄧ讲杩囷紝榛樿涓�0鏈儴缃诧紝1涓哄凡缁忛儴缃�',
  `runtime_type` tinyint NOT NULL DEFAULT '6' COMMENT '1 涓虹郴缁熷悎绾︼紱2 涓簉ust; 3 涓篶++; 4涓簍inygo; 5涓簊olidity; 6涓篻o',
  `topic_query_start` bigint unsigned NOT NULL COMMENT '璁㈤槄鈥滄煡璇簨浠垛€濈殑璧峰浣嶇疆',
  `topic_finish_start` bigint unsigned NOT NULL COMMENT '璁㈤槄鈥滅粨鏉熶簨浠垛€濈殑璧峰浣嶇疆',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '鍒涘缓鏃堕棿',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_c_c` (`chain_alias`,`contract_name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='鍚堢害鎯呭喌琛ㄦ牸'; 
```
- `浜嬩欢鐘舵€佽〃(event_state)`璁板綍浜嗕粠閾句笂鐩戝惉浜嬩欢鍒版暣涓簨浠舵祦杞畬鏁寸殑鍚勪釜闃舵
```sql
CREATE TABLE  if NOT exists `event_state`(
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '鑷id',
  `chain_alias` varchar(128) NOT NULL,
  `request_id` varchar(256) NOT NULL COMMENT '璇锋眰id',
  `tx_id` varchar(128) NOT NULL COMMENT '浜ゆ槗id',
  `contract_name` varchar(128) NOT NULL COMMENT '鍚堢害鍚嶇О',
  `state` tinyint NOT NULL DEFAULT '0' COMMENT '0锛歋tateInit 锛�1锛歋tateFetchDataFailed 锛� 2锛� StateFetchDataSucess 锛� 3锛�  StateCallbackFailed 锛� 4锛歋tateCallbackSuccess 锛� 5锛歋tateResultOnChainSuccess锛堢粓姝㈡€侊級 锛�6锛歋tateCallbackFailedFibnoacci锛堝娆¤皟閾惧け璐ョ粓姝㈡€侊級',
  `query_type` tinyint NOT NULL DEFAULT '1' COMMENT '鏌ヨ绫诲瀷鏋氫妇 1锛歶se_http锛�2锛歶se_db ',
  `query_body` mediumblob NOT NULL COMMENT '鍏蜂綋鐨勬煡璇㈡柟妗堬紝鏆傚畾涓哄簭鍒楀寲鍚庣殑json瀛楃涓�',
  `next_fibonacci` bigint unsigned NOT NULL DEFAULT '0' COMMENT '涓婇摼澶辫触鏃跺€檉eibonacci閲嶈瘯璁板綍',
  `block_height` bigint unsigned NOT NULL DEFAULT '1' COMMENT '褰撳墠浜嬩欢鐨勫尯鍧楅珮搴�',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '鍒涘缓鏃堕棿',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '鏇存柊鏃堕棿',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_s_c_r` (`request_id`) COMMENT '''璇锋眰id鍞竴绱㈠紩''',
  KEY `updated_state` (`updated_at`,`state`),
  KEY `created_state` (`created_at`,`state`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='浜嬩欢鐘舵€佽〃鏍�' ;
```
- `棰勮█鏈哄彇鏁版嵁杩斿洖鍊煎瓨鍌ㄨ〃(event_result)`瀛樺偍浜嗛瑷€鏈哄彇鍒扮殑鏁版嵁缁撴灉
```sql
CREATE TABLE if NOT exists `event_result` (
  `request_id` varchar(256) NOT NULL COMMENT '璇锋眰id锛屽敮涓€绱㈠紩',
  `event_result` mediumblob NOT NULL COMMENT '鍙栧埌鐨勬暟鎹�',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '閾緄d',
  PRIMARY KEY (`request_id`),
  KEY `created_at` (`created_at`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='棰勮█鏈哄彇鏁版嵁杩斿洖鍊煎瓨鍌ㄨ〃' ;

```
- `浜嬩欢閿佽〃(event_lock)`鐢ㄤ簬鍗忚皟澶氫釜`棰勮█鏈烘湇鍔″疄渚媊杩涜宸ヤ綔
```sql
CREATE TABLE if NOT exists `event_lock` (
  `request_id` varchar(256) NOT NULL COMMENT '璇锋眰id',
  `locked_version` int NOT NULL DEFAULT '0' COMMENT '閿�',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '鏇存柊鏃惰褰�',
  PRIMARY KEY (`request_id`) COMMENT '璇锋眰id鍞竴绱㈠紩',
  KEY `updated_at` (`updated_at`),
  KEY `request_updated` (`request_id`,`updated_at`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='浜嬩欢閿�' ; 
```
- `vrf琛ㄦ牸(vrfs)`璁板綍浜唙rf鐨勫叕閽ュ拰绉侀挜淇℃伅锛屽叾涓璼rv_name涓哄敮涓€绱㈠紩锛堢郴缁熷啓鐨勶級銆傚涓疄渚嬪鏋滆繛鎺ュ埌鍚屼竴涓猰ysql瀹炰緥锛岄偅涔堜粬浠叡浜悓涓€涓獀rf鍏閽ワ紝鍚勪釜瀹炰緥鐢熸垚vrf鏄竴鑷寸殑
```sql
CREATE TABLE if NOT exists  `vrfs`(
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '涓婚敭',
  `privatek` blob NOT NULL COMMENT '绉侀挜',
  `publick` blob NOT NULL COMMENT '鍏挜',
  `srv_name` varchar(128) NOT NULL COMMENT '鏈嶅姟鍚嶇О锛屽敮涓€绱㈠紩',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq-s` (`srv_name`) COMMENT '鍒╃敤鏈嶅姟鍚嶇О浣滃敮涓€绱㈠紩'
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='vrf琛ㄦ牸';
```
#### 棰勮█鏈虹姸鎬佹祦杞ā鍧�
`棰勮█鏈烘湇鍔鐩戝惉浜嬩欢锛屽彇鏁版嵁锛屽洖浼犳暟鎹埌閾句笂鏁翠釜娴佺▼鎶借薄鎴愪负涓€涓湁闄愮姸鎬佹満鏉ヨ繘琛屽疄鐜帮紝鐘舵€佽縼绉诲涓嬪浘  
![棰勮█鏈哄唴閮ㄧ姸鎬佹祦杞琞(../images/oracle-fsm.png)
<center>棰勮█鏈哄唴閮ㄧ姸鎬佹祦杞�</center>  

#### 鍙栨暟鎹ā鍧�
`棰勮█鏈烘湇鍔鍙互鏀寔鍥涚鍙栨暟鎹ā寮忋€傞€氳繃瑙f瀽鎸囧畾鐨勬煡璇㈤渶姹傦紝瀹炰緥鍖栫浉搴旂殑鎺ュ彛瀹炵幇绫诲彇鏁版嵁
- 鏀寔閫氳繃http鎺ュ彛鍙杧ml,json,html绫诲瀷鏁版嵁
- 鏀寔閫氳繃mysql鏁版嵁婧愬彇鏁版嵁
- 鏀寔璺ㄩ摼锛堥暱瀹夐摼锛夎皟鐢ㄩ摼涓婂悎绾︽煡璇㈡暟鎹�
- 鏀寔vrf鏁版嵁鍙�/楠�   
![鍙栨暟鎹ā鍧梋(../images/oracle-fetch-data.png)
<center>鍙栨暟鎹ā鍧�</center>   

#### 棰勮█鏈烘帴鍙fā鍧�
`棰勮█鏈烘湇鍔鐨勬帴鍙fā鍧楁彁渚涗袱绉嶇被鍨嬬殑鎺ュ彛銆備袱绫绘帴鍙e垎鍒兘鏈塦token`楠岃瘉锛坱oken涓烘湇鍔¢厤缃腑鐨勫搴旈」锛�
- 瀹夎銆佸崌绾ч瑷€鏈哄悎绾︽帴鍙token閰嶇疆椤圭洰涓簊mart_oracle.yml鏂囦欢涓環ttpServer瀵硅薄鐨刟dmin_token`
- 杈呭姪寮€鍙戞帴鍙o紝鐢ㄤ簬鏌ョ湅閫氳繃棰勮█鏈哄彲浠ュ彇鐨勬暟鎹牱寮忥紙涓嶅€熷姪鏅鸿兘鍚堢害锛屾柟渚垮紑鍙戞祴璇曪級`token閰嶇疆椤圭洰涓簊mart_oracle.yml鏂囦欢涓環ttpServer瀵硅薄鐨刟ssist_token`    
![api鎺ュ彛妯″潡](../images/oracle-apis.png)
<center>api鎺ュ彛妯″潡</center>

#### 棰勮█鏈哄憡璀︽ā鍧�
`棰勮█鏈烘湇鍔鍦ㄥ皢鏁版嵁閫氳繃璋冪敤`棰勮█鏈哄悎绾鍥炶皟閾剧殑鏃跺€欏彲鑳戒細鍙戠敓閿欒锛屾垜浠緷鎹甪ibonacci閲嶈瘯鐨勭瓥鐣ヨ繘琛岄噸璇�,褰撻噸璇曟鏁拌秴杩囩敤鎴烽厤缃殑鏈€澶у€�(閰嶇疆椤逛负fibonacci_limits)鏃跺€欙紝浼氭牴鎹敤鎴烽厤缃紙閰嶇疆椤逛负alarmer锛夌殑鎶ヨ鏂瑰紡杩涜鎶ヨ   
![鍛婅妯″潡](../images/oracle-alarmer.png)
<center>鍛婅妯″潡</center>   

## 浠g爜鏁翠綋鐩綍缁撴瀯   
```bash
.
鈹溾攢鈹€ alarms # 鍛婅妯″潡鐨勪唬鐮佸寘
鈹溾攢鈹€ config # 璇诲彇閰嶇疆妯″潡鐨勪唬鐮佸寘
鈹溾攢鈹€ config_files # 閰嶇疆鏂囦欢
鈹�   鈹溾攢鈹€ crypto-config # 浣跨敤chainmaker-cryptogen 宸ュ叿鐢熸垚鐨勮瘉涔︽枃浠�
鈹�   鈹溾攢鈹€ sdk-config # 閾緎dk閰嶇疆
鈹�   鈹�   鈹溾攢鈹€ sdk_config_chain1.yml # 閾�1鐨剆dk
鈹�   鈹�   鈹斺攢鈹€ sdk_config_chain2.yml # 閾�2鐨剆dk
鈹�   鈹斺攢鈹€ smart_oracle.yml # 棰勮█鏈烘湇鍔$殑閰嶇疆鏂囦欢
鈹溾攢鈹€ contract_process # 棰勮█鏈烘湇鍔℃牳蹇冧笟鍔¢€昏緫鐨勪唬鐮佸寘
鈹溾攢鈹€ custom.cnf # 鍙互鍙傝€冭繖涓厤缃潵閰嶇疆docker涓殑mysql鏁版嵁搴擄紙5.7 鍙婁互鍚庯級
鈹溾攢鈹€ databases # 杩炴帴鏁版嵁搴撴ā鍧楃殑浠g爜鍖�
鈹溾攢鈹€ docker-compose.yml # docker-compose 妯℃澘鏂囦欢
鈹溾攢鈹€ Dockerfile # docker 闀滃儚鏂囦欢
鈹溾攢鈹€ fetch_data # 鍙栨暟鎹ā鍧椾唬鐮佸寘
鈹溾攢鈹€ http_server_process # 棰勮█鏈篽ttp鏈嶅姟鎺ュ彛浠g爜鍖�
鈹溾攢鈹€ install-upgrade-oracle-contract.sh #棰勮█鏈哄悎绾﹀畨瑁呫€佸崌绾ц剼鏈�
鈹溾攢鈹€ logger # 鏃ュ織妯″潡浠g爜鍖�
鈹溾攢鈹€ main.go # 鏈嶅姟鍏ュ彛
鈹溾攢鈹€ Makefile 
鈹溾攢鈹€ models # 棰勮█鏈烘暟鎹ā鍨嬪寘
鈹溾攢鈹€ mysql-data-volumes # 榛樿鐨刣ocker-compose鍚姩mysql鏁版嵁搴撶殑鏁版嵁鎸傝浇鐐�
鈹�   鈹斺攢鈹€ data
鈹溾攢鈹€ readme.md
鈹溾攢鈹€ scripts # mysql寤哄簱鑴氭湰
鈹�   鈹斺攢鈹€ init.sql
鈹溾攢鈹€ standard_oracle_contract # 绯荤粺鑷甫鐨勫悎绾︽枃浠跺強浠g爜
鈹�   鈹溾攢鈹€ oracle_contract_file
鈹�   鈹�   鈹斺攢鈹€ oracle_contract_v1 # 缂栬瘧杩囩殑棰勮█鏈哄悎绾﹀彲鎵ц鏂囦欢
鈹�   鈹溾攢鈹€ oracle_contract_src.zip # 棰勮█鏈哄悎绾︽簮浠g爜
鈹�   鈹斺攢鈹€ use_demo_src.zip # 鐢ㄦ埛浣跨敤棰勮█鏈哄悎绾︾殑绀轰緥婧愪唬鐮�
鈹�   鈹斺攢鈹€ use_demo.7z # 缂栬瘧鍘嬬缉鍚庣殑绀轰緥鏅鸿兘鍚堢害鏂囦欢
鈹斺攢鈹€ ut_cover.sh # 鍗曞厓娴嬭瘯鑴氭湰

```