From 3a23e1c603e5701022a88de4fd460c6b8a644db4 Mon Sep 17 00:00:00 2001
From: Dmitriy Gerasimov <dm@cifercom.com>
Date: Fri, 12 Jan 2018 03:26:19 +0700
Subject: [PATCH] [+] Block's header [+] Block's section format [+] Block's
 section roots header

---
 CMakeLists.txt            | 25 ++++++++++++++++++-------
 dap_chain_block.c         |  5 +++++
 dap_chain_block.h         | 15 +++++++++++++--
 dap_chain_block_roots.c   |  4 ++++
 dap_chain_block_roots.h   | 21 +++++++++++++++++++++
 dap_chain_block_section.c |  3 +++
 dap_chain_block_section.h | 12 ++++++++----
 dap_chain_common.c        |  0
 dap_chain_common.h        | 11 +++++++++++
 9 files changed, 83 insertions(+), 13 deletions(-)
 create mode 100644 dap_chain_block_roots.c
 create mode 100644 dap_chain_block_roots.h
 create mode 100644 dap_chain_common.c
 create mode 100644 dap_chain_common.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9942583..a5a0302 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,14 +1,25 @@
 cmake_minimum_required(VERSION 2.8)
 project (libdap-chain)
   
-set(DAP_CHAIN_SRCS dap_chain_block.c dap_chain_block_section.c  dap_chain.c	dap_chain_coin.c  dap_chain_srv.c  dap_chain_tx.c)
+set(DAP_CHAIN_SRCS 
+	dap_chain_common.c
+	dap_chain_block.c 
+	dap_chain_block_section.c 
+	dap_chain_block_roots.c
+	dap_chain.c
+	dap_chain_coin.c  
+	dap_chain_srv.c  
+	dap_chain_tx.c)
+
 set(DAP_CHAIN_HEADERS
-    dap_chain_block.h
-    dap_chain_block_section.h
-    dap_chain.h
-    dap_chain_coin.h
-    dap_chain_srv.h
-    dap_chain_tx.h
+	dap_chain_block.h
+	dap_chain_block_section.h
+	dap_chain_block_roots.h
+	dap_chain.h
+	dap_chain_common.h
+	dap_chain_coin.h
+	dap_chain_srv.h
+	dap_chain_tx.h
     )
 
 include_directories("${dap_core_INCLUDE_DIRS}")
diff --git a/dap_chain_block.c b/dap_chain_block.c
index dc73e82..9c38007 100644
--- a/dap_chain_block.c
+++ b/dap_chain_block.c
@@ -18,8 +18,13 @@
     along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "dap_common.h"
+#include "dap_chain_block_roots.h"
+
 #include "dap_chain_block.h"
 
+#define LOG_TAG "dap_chain_block"
+
 /**
  * @brief dap_chain_block_init
  * @return
diff --git a/dap_chain_block.h b/dap_chain_block.h
index 2eb5e79..f370e84 100644
--- a/dap_chain_block.h
+++ b/dap_chain_block.h
@@ -24,16 +24,27 @@
 #include <stddef.h>
 #include "dap_common.h"
 #include "dap_math_ops.h"
+#include "dap_chain_common.h"
 #include "dap_chain_block_section.h"
 
+#define DAP_CHAIN_BLOCK_SIGNATURE 0xDA05BF8E
+
+
 /**
  * @brief The dap_chain_block struct
  */
 typedef struct  dap_chain_block{
     struct {
-        dap_uint128_t id; /// @param Block ID uniqie identificator of the block
-        size_t size;
+        uint32_t signature; /// Magic number, always equels to DAP_CHAIN_BLOCK_SIGNATURE
+        int32_t version; /// block version
+        dap_chain_hash_t prev_block_hash;
+        uint64_t timestamp; /// Timestamp
+        uint64_t bits; /// difficulty
+        uint64_t nonce; /// Nonce value to allow header variation for mining
+        dap_uint128_t id; /// @param id Block ID uniqie identificator of the block
+        size_t section_size; /// @param secion_size Size of section[] array
     } header;
+    dap_chain_hash_t tree_root_;
     dap_chain_block_section_t section[];
 } DAP_ALIGN_PACKED dap_chain_block_t;
 
diff --git a/dap_chain_block_roots.c b/dap_chain_block_roots.c
new file mode 100644
index 0000000..1b2d6b0
--- /dev/null
+++ b/dap_chain_block_roots.c
@@ -0,0 +1,4 @@
+#include "dap_common.h"
+#include "dap_chain_block_roots.h"
+
+#define LOG_TAG "dap_chain_block_roots"
diff --git a/dap_chain_block_roots.h b/dap_chain_block_roots.h
new file mode 100644
index 0000000..c8928f5
--- /dev/null
+++ b/dap_chain_block_roots.h
@@ -0,0 +1,21 @@
+#ifndef _DAP_CHAIN_BLOCK_ROOTS_H_
+#define _DAP_CHAIN_BLOCK_ROOTS_H_
+
+#include "dap_common.h"
+#include "dap_chain_common.h"
+
+/**
+  * @struct dap_chain_block_roots
+  * @brief Hash tree roots for block
+  */
+typedef struct dap_chain_block_roots{
+    dap_chain_hash_t main;
+    dap_chain_hash_t txs;
+    dap_chain_hash_t txs_pending;
+    dap_chain_hash_t txs_requests;
+    dap_chain_hash_t contract_code;
+    dap_chain_hash_t contract_data;
+} DAP_ALIGN_PACKED dap_chain_block_roots_t;
+
+#endif
+
diff --git a/dap_chain_block_section.c b/dap_chain_block_section.c
index e69de29..64604b2 100644
--- a/dap_chain_block_section.c
+++ b/dap_chain_block_section.c
@@ -0,0 +1,3 @@
+#include "dap_common.h"
+
+#define LOG_TAG "dap_chain_block_secion"
diff --git a/dap_chain_block_section.h b/dap_chain_block_section.h
index f4328cf..df7fca5 100644
--- a/dap_chain_block_section.h
+++ b/dap_chain_block_section.h
@@ -7,6 +7,10 @@
 #include <stdint.h>
 #include "dap_common.h"
 #include "dap_math_ops.h"
+#include "dap_chain_common.h"
+
+/// First section that must be in any block, with hash tree roots
+#define DAP_CHAIN_BLOCK_SECTION_ROOTS 0xffff
 
 /// End section, means all the rest of the block is empty
 #define DAP_CHAIN_BLOCK_SECTION_END 0x0000
@@ -14,11 +18,11 @@
 /// Transaction section
 #define DAP_CHAIN_BLOCK_SECTION_TX 0x0100
 
-/// Smart contract: code section
-#define DAP_CHAIN_BLOCK_SECTION_TX 0x0200
+/// Smart contract: EVM code section
+#define DAP_CHAIN_BLOCK_SECTION_EVM_CODE 0x0200
 
-/// Smart contract: data section
-#define DAP_CHAIN_BLOCK_SECTION_TX 0x0201
+/// Smart contract: EVM data section
+#define DAP_CHAIN_BLOCK_SECTION_EVM_DATA 0x0201
 
 /// Public key
 #define DAP_CHAIN_BLOCK_SECTION_PKEY 0x0300
diff --git a/dap_chain_common.c b/dap_chain_common.c
new file mode 100644
index 0000000..e69de29
diff --git a/dap_chain_common.h b/dap_chain_common.h
new file mode 100644
index 0000000..5fa776a
--- /dev/null
+++ b/dap_chain_common.h
@@ -0,0 +1,11 @@
+#ifndef _DAP_CHAIN_COMMON_H_
+#define _DAP_CHAIN_COMMON_H_
+#include <stdint.h>
+
+#define DAP_CHAIN_HASH_SIZE 32
+
+typedef union dap_chain_hash{
+    uint8_t data[DAP_CHAIN_HASH_SIZE];
+} dap_chain_hash_t;
+
+#endif
-- 
GitLab