From 4f0a43ddec26933153cec11ad2fed8e570e5652a Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Fri, 28 Aug 2020 20:54:50 +0700
Subject: [PATCH] [*] Fixes

---
 modules/chain/include/dap_chain.h                 | 8 ++++----
 modules/consensus/dag-poa/dap_chain_cs_dag_poa.c  | 5 +++--
 modules/type/dag/dap_chain_cs_dag.c               | 8 +++++---
 modules/type/dag/include/dap_chain_cs_dag_event.h | 2 +-
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h
index 77ade0bd80..86f4782d85 100644
--- a/modules/chain/include/dap_chain.h
+++ b/modules/chain/include/dap_chain.h
@@ -51,10 +51,10 @@ typedef struct dap_chain_atom_iter{
 } dap_chain_atom_iter_t;
 
 typedef enum dap_chain_atom_verify_res{
-    ATOM_ACCEPT,
-    ATOM_PASS,
-    ATOM_REJECT,
-    ATOM_MOVE_TO_THRESHOLD
+    ATOM_ACCEPT=0,
+    ATOM_PASS=1,
+    ATOM_REJECT=2,
+    ATOM_MOVE_TO_THRESHOLD=3
 } dap_chain_atom_verify_res_t;
 
 typedef dap_chain_t* (*dap_chain_callback_new_t)(void);
diff --git a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c
index a2599f189e..60b04baf49 100644
--- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c
+++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c
@@ -349,8 +349,9 @@ static int s_callback_event_verify(dap_chain_cs_dag_t * a_dag, dap_chain_cs_dag_
 {
     dap_chain_cs_dag_poa_pvt_t * l_poa_pvt = PVT ( DAP_CHAIN_CS_DAG_POA( a_dag ) );
     size_t l_offset_from_beginning = dap_chain_cs_dag_event_calc_size_excl_signs(a_dag_event,a_dag_event_size);
-    if( l_offset_from_beginning > a_dag_event_size){
-        log_it(L_WARNING,"Incorrect size with event %p", a_dag_event);
+    if( l_offset_from_beginning >= a_dag_event_size){
+        log_it(L_WARNING,"Incorrect size with event %p: caled size excl signs %zd is bigger or equal then event size %zd",
+               a_dag_event, l_offset_from_beginning, a_dag_event_size);
         return -7; // Incorrest size
     }
     if ( a_dag_event->header.signs_count >= l_poa_pvt->auth_certs_count_verify ){
diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c
index 60e7935995..b037fae22e 100644
--- a/modules/type/dag/dap_chain_cs_dag.c
+++ b/modules/type/dag/dap_chain_cs_dag.c
@@ -287,7 +287,7 @@ static int s_dap_chain_add_atom_to_events_table(dap_chain_cs_dag_t * a_dag, dap_
     if(res == 0){
         char l_buf_hash[128];
         dap_chain_hash_fast_to_str(&a_event_item->hash,l_buf_hash,sizeof(l_buf_hash)-1);
-        log_it(L_DEBUG,"Dag event %s checked, add it to ledger", a_event_item->hash);
+        log_it(L_DEBUG,"Dag event %s checked, add it to ledger", l_buf_hash);
         res = s_dap_chain_add_atom_to_ledger(a_dag, a_ledger, a_event_item);
 
         HASH_ADD(hh, PVT(a_dag)->events,hash,sizeof (a_event_item->hash), a_event_item);
@@ -354,7 +354,7 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_cha
     // verify hashes and consensus
     if(ret == ATOM_ACCEPT){
         ret = s_chain_callback_atom_verify (a_chain, a_atom, a_atom_size);
-        log_it(L_DEBUG, "Accepted atom %p", a_atom);
+        log_it(L_DEBUG, "Verified atom %p: code %d", a_atom, ret);
     }
 
     if( ret == ATOM_MOVE_TO_THRESHOLD){
@@ -587,8 +587,10 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_verify(dap_chain_t * a_
     dap_chain_cs_dag_event_t * l_event = (dap_chain_cs_dag_event_t *) a_atom;
     dap_chain_atom_verify_res_t res = ATOM_ACCEPT;
 
-    if(sizeof (l_event->header)<= a_atom_size)
+    if(sizeof (l_event->header) >= a_atom_size){
+        log_it(L_WARNING,"Size of atom is %zd that is equel or less then header %zd",a_atom_size,sizeof (l_event->header));
         return  ATOM_REJECT;
+    }
     // genesis or seed mode
     if (l_event->header.hash_count == 0){
       if(s_seed_mode && !PVT(l_dag)->events)
diff --git a/modules/type/dag/include/dap_chain_cs_dag_event.h b/modules/type/dag/include/dap_chain_cs_dag_event.h
index 14e2d78506..5f3661be72 100644
--- a/modules/type/dag/include/dap_chain_cs_dag_event.h
+++ b/modules/type/dag/include/dap_chain_cs_dag_event.h
@@ -103,7 +103,7 @@ static inline size_t dap_chain_cs_dag_event_calc_size(dap_chain_cs_dag_event_t *
  */
 static inline ssize_t dap_chain_cs_dag_event_calc_size_excl_signs(dap_chain_cs_dag_event_t * a_event,size_t a_event_size)
 {
-    if (a_event_size> sizeof (a_event->header))
+    if (a_event_size< sizeof (a_event->header))
         return -1;
     size_t l_hashes_size = a_event->header.hash_count*sizeof(dap_chain_hash_fast_t);
     dap_chain_datum_t * l_datum = (dap_chain_datum_t*) (a_event->hashes_n_datum_n_signs + l_hashes_size);
-- 
GitLab