From 80a73e4e4f4fdcb79c84aae31e8bfe1b9f6f786f Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Tue, 3 Nov 2020 02:24:28 +0700
Subject: [PATCH] [!] Inter->bridge [+] Bridges infrastructure

---
 CMakeLists.txt                                | 10 +-
 modules/CMakeLists.txt                        |  6 +-
 modules/{inter => bridge}/btc/CMakeLists.txt  |  8 +-
 modules/bridge/btc/dap_chain_bridge_btc.c     | 40 ++++++++
 .../btc/include/dap_chain_bridge_btc.h}       |  3 +
 modules/bridge/btc/share/in.satoshi.tpl       |  9 ++
 .../btc/share/out.satoshi.tpl                 |  0
 .../btc/share/profiles/bitcoin-test.conf      |  0
 .../btc/share/profiles/bitcoin.conf           |  0
 .../btc/share/profiles/emercoin-test.conf     |  0
 .../btc/share/profiles/emercoin.conf          |  0
 .../btc/share/profiles/lightcoin-test.conf    |  0
 .../btc/share/profiles/lightcoin.conf         |  0
 modules/chain/dap_chain.c                     | 11 +++
 modules/common/dap_chain_inter.c              | 27 -----
 modules/inter/btc/dap_chain_inter_btc.c       |  5 -
 modules/inter/btc/share/in.satoshi.tpl        |  0
 modules/net/dap_chain_bridge.c                | 98 +++++++++++++++++++
 modules/net/dap_chain_net.c                   |  6 ++
 .../include/dap_chain_bridge.h}               | 11 +++
 20 files changed, 190 insertions(+), 44 deletions(-)
 rename modules/{inter => bridge}/btc/CMakeLists.txt (50%)
 create mode 100644 modules/bridge/btc/dap_chain_bridge_btc.c
 rename modules/{inter/btc/include/dap_chain_inter_btc.h => bridge/btc/include/dap_chain_bridge_btc.h} (93%)
 create mode 100644 modules/bridge/btc/share/in.satoshi.tpl
 rename modules/{inter => bridge}/btc/share/out.satoshi.tpl (100%)
 rename modules/{inter => bridge}/btc/share/profiles/bitcoin-test.conf (100%)
 rename modules/{inter => bridge}/btc/share/profiles/bitcoin.conf (100%)
 rename modules/{inter => bridge}/btc/share/profiles/emercoin-test.conf (100%)
 rename modules/{inter => bridge}/btc/share/profiles/emercoin.conf (100%)
 rename modules/{inter => bridge}/btc/share/profiles/lightcoin-test.conf (100%)
 rename modules/{inter => bridge}/btc/share/profiles/lightcoin.conf (100%)
 delete mode 100644 modules/common/dap_chain_inter.c
 delete mode 100644 modules/inter/btc/dap_chain_inter_btc.c
 delete mode 100644 modules/inter/btc/share/in.satoshi.tpl
 create mode 100644 modules/net/dap_chain_bridge.c
 rename modules/{common/include/dap_chain_inter.h => net/include/dap_chain_bridge.h} (67%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a4d1569b57..6fcfcaeadd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,7 @@ project(cellframe-sdk C)
 cmake_minimum_required(VERSION 2.8)
 
 set(CMAKE_C_STANDARD 11)
-set(CELLFRAME_SDK_NATIVE_VERSION "2.7-0")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.7-1")
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 
 set(DAPSDK_MODULES "")
@@ -123,10 +123,10 @@ if (CELLFRAME_MODULES MATCHES "srv-stake")
     set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_net_srv_stake )
 endif()
 
-# Enable interchain exchange with BTC-like networks
-if (CELLFRAME_MODULES MATCHES "inter-btc")
-    message("[+] Module 'inter-btc'")
-    set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_inter_btc )
+# Enable bridge with BTC-like networks
+if (CELLFRAME_MODULES MATCHES "bridge-btc")
+    message("[+] Module 'bridge-btc'")
+    set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_bridge_btc )
 endif()
 
 # Enable service for dynamic modules
diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt
index 2f02311724..2b3c7d0eac 100644
--- a/modules/CMakeLists.txt
+++ b/modules/CMakeLists.txt
@@ -87,9 +87,9 @@ if (CELLFRAME_MODULES MATCHES "srv-stake")
     add_subdirectory(service/stake)
 endif()
 
-# Interchaine communications with BTC-like networks
-if (CELLFRAME_MODULES MATCHES "inter-btc")
-    add_subdirectory(inter/btc)
+# Bridge communications with BTC-like networks
+if (CELLFRAME_MODULES MATCHES "bridge-btc")
+    add_subdirectory(bridge/btc)
 endif()
 
 # Support for dynamic modules
diff --git a/modules/inter/btc/CMakeLists.txt b/modules/bridge/btc/CMakeLists.txt
similarity index 50%
rename from modules/inter/btc/CMakeLists.txt
rename to modules/bridge/btc/CMakeLists.txt
index ea8db63993..0da1a6cad9 100644
--- a/modules/inter/btc/CMakeLists.txt
+++ b/modules/bridge/btc/CMakeLists.txt
@@ -1,10 +1,10 @@
-project (dap_chain_inter_btc)
+project (dap_chain_bridge_btc)
   
-file(GLOB DAP_CHAIN_INTER_BTC_SRCS *.c)
+file(GLOB DAP_CHAIN_BRIDGE_BTC_SRCS *.c)
 
-file(GLOB DAP_CHAIN_INTER_BTC_HEADERS include/*.h)
+file(GLOB DAP_CHAIN_BRIDGE_BTC_HEADERS include/*.h)
 
-add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_INTER_BTC_SRCS} ${DAP_CHAIN_INTER_BTC_HEADERS})
+add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_BRIDGE_BTC_SRCS} ${DAP_CHAIN_BRIDGE_BTC_HEADERS})
 
 target_include_directories(dap_chain_crypto INTERFACE .)
 target_include_directories(${PROJECT_NAME} PUBLIC include)
diff --git a/modules/bridge/btc/dap_chain_bridge_btc.c b/modules/bridge/btc/dap_chain_bridge_btc.c
new file mode 100644
index 0000000000..fd0703e26f
--- /dev/null
+++ b/modules/bridge/btc/dap_chain_bridge_btc.c
@@ -0,0 +1,40 @@
+#include "dap_common.h"
+#include "dap_config.h"
+#include "dap_chain_net.h"
+#include "dap_chain_bridge.h"
+#include "dap_chain_bridge_btc.h"
+
+#define LOG_TAG "dap_chain_bridge_btc"
+
+typedef struct dap_chain_bridge_btc_pvt{
+    dap_chain_net_t * net;
+    const char * profile;
+    const char * wallet;
+    const char * network;
+    double  stake_min, stake_max;
+} dap_chain_bridge_btc_pvt_t;
+
+static int s_bridge_callback_init(const char * a_bridge_name, dap_chain_net_t * a_net, dap_config_t * a_cfg);
+
+int dap_chain_bridge_btc_init()
+{
+    dap_chain_bridge_register("bridge-btc", s_bridge_callback_init);
+    return 0;
+}
+
+void dap_chain_bridge_btc_deinit()
+{
+
+}
+
+static int s_bridge_callback_init(const char * a_bridge_name, dap_chain_net_t * a_net, dap_config_t * a_cfg)
+{
+    dap_chain_bridge_btc_pvt_t * l_btc_pvt = DAP_NEW_Z(dap_chain_bridge_btc_pvt_t);
+    l_btc_pvt->net = a_net;
+    l_btc_pvt->profile = dap_config_get_item_str_default(a_cfg , a_bridge_name , "profile","emercoin" );
+    l_btc_pvt->wallet = dap_config_get_item_str(a_cfg , a_bridge_name , "wallet");
+    l_btc_pvt->network = dap_config_get_item_str_default(a_cfg , a_bridge_name , "network", "testnet" );
+    l_btc_pvt->stake_min = dap_config_get_item_double_default(a_cfg , a_bridge_name , "stake_min", -1.0);
+    l_btc_pvt->stake_max = dap_config_get_item_double_default(a_cfg , a_bridge_name , "stake_max", -1.0);
+    return 0;
+}
diff --git a/modules/inter/btc/include/dap_chain_inter_btc.h b/modules/bridge/btc/include/dap_chain_bridge_btc.h
similarity index 93%
rename from modules/inter/btc/include/dap_chain_inter_btc.h
rename to modules/bridge/btc/include/dap_chain_bridge_btc.h
index d5d9fec30e..d560ddb8d0 100644
--- a/modules/inter/btc/include/dap_chain_inter_btc.h
+++ b/modules/bridge/btc/include/dap_chain_bridge_btc.h
@@ -23,3 +23,6 @@
     along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 #pragma once
+
+int dap_chain_bridge_btc_init();
+void dap_chain_bridge_btc_deinit();
diff --git a/modules/bridge/btc/share/in.satoshi.tpl b/modules/bridge/btc/share/in.satoshi.tpl
new file mode 100644
index 0000000000..c4d3b90276
--- /dev/null
+++ b/modules/bridge/btc/share/in.satoshi.tpl
@@ -0,0 +1,9 @@
+{{for: i from 0 to $arbitr-count }}
+OP_IF
+  <arbitr-{i}.public_key>
+  OP_CHECKSIGVERIFY
+  <1>
+OP_ELSE
+  <2>
+OP_ENDIF
+{{endfor}}
diff --git a/modules/inter/btc/share/out.satoshi.tpl b/modules/bridge/btc/share/out.satoshi.tpl
similarity index 100%
rename from modules/inter/btc/share/out.satoshi.tpl
rename to modules/bridge/btc/share/out.satoshi.tpl
diff --git a/modules/inter/btc/share/profiles/bitcoin-test.conf b/modules/bridge/btc/share/profiles/bitcoin-test.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/bitcoin-test.conf
rename to modules/bridge/btc/share/profiles/bitcoin-test.conf
diff --git a/modules/inter/btc/share/profiles/bitcoin.conf b/modules/bridge/btc/share/profiles/bitcoin.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/bitcoin.conf
rename to modules/bridge/btc/share/profiles/bitcoin.conf
diff --git a/modules/inter/btc/share/profiles/emercoin-test.conf b/modules/bridge/btc/share/profiles/emercoin-test.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/emercoin-test.conf
rename to modules/bridge/btc/share/profiles/emercoin-test.conf
diff --git a/modules/inter/btc/share/profiles/emercoin.conf b/modules/bridge/btc/share/profiles/emercoin.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/emercoin.conf
rename to modules/bridge/btc/share/profiles/emercoin.conf
diff --git a/modules/inter/btc/share/profiles/lightcoin-test.conf b/modules/bridge/btc/share/profiles/lightcoin-test.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/lightcoin-test.conf
rename to modules/bridge/btc/share/profiles/lightcoin-test.conf
diff --git a/modules/inter/btc/share/profiles/lightcoin.conf b/modules/bridge/btc/share/profiles/lightcoin.conf
similarity index 100%
rename from modules/inter/btc/share/profiles/lightcoin.conf
rename to modules/bridge/btc/share/profiles/lightcoin.conf
diff --git a/modules/chain/dap_chain.c b/modules/chain/dap_chain.c
index db90df595e..b6bff2711e 100644
--- a/modules/chain/dap_chain.c
+++ b/modules/chain/dap_chain.c
@@ -36,6 +36,7 @@
 #include "dap_cert.h"
 #include "dap_chain_cs.h"
 #include "dap_chain_vf.h"
+#include "dap_chain_bridge.h"
 #include <uthash.h>
 #include <pthread.h>
 
@@ -80,6 +81,8 @@ int dap_chain_init(void)
     dap_chain_cs_init();
 
     dap_chain_vf_init();
+
+    dap_chain_bridge_init();
     //dap_chain_show_hash_blocks_file(g_gold_hash_blocks_file);
     //dap_chain_show_hash_blocks_file(g_silver_hash_blocks_file);
     return 0;
@@ -96,6 +99,14 @@ void dap_chain_deinit(void)
           dap_chain_delete(s_chain_items->chain);
         }
     pthread_rwlock_unlock(&s_chain_items_rwlock);
+
+
+    dap_chain_cs_deinit();
+
+    dap_chain_vf_deinit();
+
+    dap_chain_bridge_deinit();
+
 }
 
 /**
diff --git a/modules/common/dap_chain_inter.c b/modules/common/dap_chain_inter.c
deleted file mode 100644
index 5143d668f9..0000000000
--- a/modules/common/dap_chain_inter.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Authors:
- * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * CellFrame       https://cellframe.net
- * Sources         https://gitlab.demlabs.net/cellframe
- * Copyright  (c) 2020
- * All rights reserved.
-
- This file is part of CellFrame SDK the open source project
-
-    CellFrame SDK is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    CellFrame SDK is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-#include "dap_chain_inter.h"
-
-#define LOG_TAG "dap_chain_inter"
diff --git a/modules/inter/btc/dap_chain_inter_btc.c b/modules/inter/btc/dap_chain_inter_btc.c
deleted file mode 100644
index 0b0aae6555..0000000000
--- a/modules/inter/btc/dap_chain_inter_btc.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "dap_common.h"
-#include "dap_chain_inter.h"
-#include "dap_chain_inter_btc.h"
-
-#define LOG_TAG "dap_chain_inter_btc"
diff --git a/modules/inter/btc/share/in.satoshi.tpl b/modules/inter/btc/share/in.satoshi.tpl
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/modules/net/dap_chain_bridge.c b/modules/net/dap_chain_bridge.c
new file mode 100644
index 0000000000..854b1394fa
--- /dev/null
+++ b/modules/net/dap_chain_bridge.c
@@ -0,0 +1,98 @@
+/*
+ * Authors:
+ * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
+ * DeM Labs Inc.   https://demlabs.net
+ * CellFrame       https://cellframe.net
+ * Sources         https://gitlab.demlabs.net/cellframe
+ * Copyright  (c) 2020
+ * All rights reserved.
+
+ This file is part of CellFrame SDK the open source project
+
+    CellFrame SDK is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    CellFrame SDK is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <assert.h>
+#include "uthash.h"
+#include "dap_common.h"
+#include "dap_chain_bridge.h"
+
+#define LOG_TAG "dap_chain_bridge"
+typedef struct bridge_item{
+    char name[64];
+    dap_chain_bridge_callback_init_t callback_init;
+    UT_hash_handle hh;
+} bridge_item_t;
+
+static bridge_item_t * s_items = NULL;
+
+/**
+ * @brief dap_chain_bridge_init
+ * @return
+ */
+int dap_chain_bridge_init()
+{
+    return 0;
+}
+
+/**
+ * @brief dap_chain_bridge_deinit
+ */
+void dap_chain_bridge_deinit()
+{
+
+}
+
+/**
+ * @brief dap_chain_bridge_register
+ * @param a_bridge_name
+ * @param a_callback_init
+ * @return
+ */
+int dap_chain_bridge_register(const char * a_bridge_name,  dap_chain_bridge_callback_init_t a_callback_init )
+{
+    bridge_item_t * l_item = NULL;
+    HASH_FIND_STR(s_items,a_bridge_name, l_item);
+    if (l_item)
+        return -1;
+
+    l_item = DAP_NEW_Z(bridge_item_t);
+    strncpy( l_item->name,a_bridge_name,sizeof (l_item->name)-1);
+    l_item->callback_init = a_callback_init;
+    HASH_ADD_STR(s_items,name,l_item);
+
+    return 0;
+}
+
+/**
+ * @brief dap_chain_bridge_add
+ * @param a_bridge_name
+ * @param a_net
+ * @param a_net_config
+ * @return
+ */
+int dap_chain_bridge_add(const char * a_bridge_name, dap_chain_net_t * a_net, dap_config_t * a_net_config )
+{
+    bridge_item_t * l_item = NULL;
+    HASH_FIND_STR(s_items, a_bridge_name, l_item);
+    if (!l_item){
+        log_it(L_ERROR,"Can't find \"%s\" bridge", a_bridge_name);
+        return -1;
+    }
+    if (l_item->callback_init){
+        log_it(L_ERROR,"Init callback for bridge name \"%s\" is NULL", a_bridge_name);
+        return -2;
+    }
+    l_item->callback_init(a_bridge_name,a_net, a_net_config);
+    return 0;
+}
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 902d9619e9..9d4f07f31b 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -71,6 +71,7 @@
 #include "dap_chain_node_cli.h"
 #include "dap_chain_node_cli_cmd.h"
 #include "dap_chain_ledger.h"
+#include "dap_chain_bridge.h"
 #include "dap_chain_cs_none.h"
 
 #include "dap_chain_global_db.h"
@@ -1754,6 +1755,11 @@ int s_net_load(const char * a_net_name, uint16_t a_acl_idx)
         PVT(l_net)->load_mode = false;
         PVT(l_net)->flags |= F_DAP_CHAIN_NET_GO_SYNC;
 
+        bool l_bridge_btc_enabled = dap_config_get_item_bool_default(l_cfg , "bridge-btc" , "enabled",false );
+        if ( l_bridge_btc_enabled){
+            dap_chain_bridge_add ( "bridge-btc",l_net,  l_cfg);
+        }
+
         // Start the proc thread
         s_net_check_thread_start(l_net);
         log_it(L_NOTICE, "Сhain network \"%s\" initialized",l_net_item->name);
diff --git a/modules/common/include/dap_chain_inter.h b/modules/net/include/dap_chain_bridge.h
similarity index 67%
rename from modules/common/include/dap_chain_inter.h
rename to modules/net/include/dap_chain_bridge.h
index d023c26b51..f85d136cba 100644
--- a/modules/common/include/dap_chain_inter.h
+++ b/modules/net/include/dap_chain_bridge.h
@@ -20,3 +20,14 @@
     along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 #pragma once
+
+#include "dap_config.h"
+#include "dap_chain_net.h"
+
+int dap_chain_bridge_init();
+void dap_chain_bridge_deinit();
+
+typedef int (*dap_chain_bridge_callback_init_t)(const char *,dap_chain_net_t * , dap_config_t *);
+
+int dap_chain_bridge_register(const char * a_bridge_name, dap_chain_bridge_callback_init_t a_callback_init);
+int dap_chain_bridge_add(const char * a_bridge_name, dap_chain_net_t * a_net,dap_config_t * a_net_config );
-- 
GitLab