From dfee18080ab998d7fd4c93db4a2fce5b8df41786 Mon Sep 17 00:00:00 2001
From: jl777 <jameslee777@yahoo.com>
Date: Wed, 2 May 2018 16:49:45 +0300
Subject: [PATCH 1/4] Maximize best matches

---
 iguana/dPoW.h              | 2 +-
 iguana/dpow/dpow_fsm.c     | 2 +-
 iguana/dpow/dpow_network.c | 9 +++++----
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/iguana/dPoW.h b/iguana/dPoW.h
index f9d474787..6b99cf27d 100755
--- a/iguana/dPoW.h
+++ b/iguana/dPoW.h
@@ -114,7 +114,7 @@ struct dpow_block
     struct dpow_entry notaries[DPOW_MAXRELAYS];
     uint32_t MoMdepth,state,starttime,timestamp,waiting,sigcrcs[2],txidcrcs[2],utxocrcs[2],lastepoch,paxwdcrc,lastnanosend;
     int32_t rawratifiedlens[2],height,numnotaries,numerrors,completed,minsigs,duration,numratified,isratify,require0,scores[DPOW_MAXRELAYS];
-    int8_t myind,bestk,ratifybestk,pendingbestk,pendingratifybestk;
+    int8_t myind,bestk,ratifybestk,pendingbestk,pendingratifybestk,bestmatches;
     cJSON *ratified;
     uint8_t ratified_pubkeys[DPOW_MAXRELAYS][33],ratifysigs[2][DPOW_MAXSIGLEN],ratifysiglens[2];
     char handles[DPOW_MAXRELAYS][32];
diff --git a/iguana/dpow/dpow_fsm.c b/iguana/dpow/dpow_fsm.c
index 778415025..b5742af38 100755
--- a/iguana/dpow/dpow_fsm.c
+++ b/iguana/dpow/dpow_fsm.c
@@ -358,7 +358,7 @@ void dpow_statemachinestart(void *ptr)
             }
             free_json(ratified);
         }
-        bp->bestk = -1;
+        bp->pendingbestk = bp->bestk = -1;
         dp->blocks[checkpoint.blockhash.height] = bp;
         dp->currentbp = bp;
         bp->beacon = rand256(0);
diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c
index 2da514cf0..ea000bcc1 100755
--- a/iguana/dpow/dpow_network.c
+++ b/iguana/dpow/dpow_network.c
@@ -1502,11 +1502,12 @@ void dpow_bestconsensus(struct dpow_block *bp)
             besti = i;
         }
     }
-    if ( besti >= 0 && bestks[besti] >= 0 && masks[besti] != 0 && (recvmask & masks[besti]) == masks[besti] )
+    if ( best > bp->bestmatches && besti >= 0 && bestks[besti] >= 0 && masks[besti] != 0 && (recvmask & masks[besti]) == masks[besti] )
     {
+        bp->bestmatches = best;
         bp->notaries[bp->myind].bestmask = bp->bestmask = masks[besti];
         bp->notaries[bp->myind].bestk = bp->bestk = bestks[besti];
-        //printf("set best.%d to (%d %llx) recv.%llx\n",best,bp->bestk,(long long)bp->bestmask,(long long)recvmask);
+        printf("set best.%d to (%d %llx) recv.%llx\n",best,bp->bestk,(long long)bp->bestmask,(long long)recvmask);
     }
     bp->recvmask |= recvmask;
     if ( bp->bestmask == 0 )//|| (time(NULL) / 180) != bp->lastepoch )
@@ -1904,9 +1905,9 @@ void dpow_notarize_update(struct supernet_info *myinfo,struct dpow_info *dp,stru
             }
             if ( 0 && bp->myind <= 1 )
                 printf("recv.%llx best.(%d %llx) m.%d p.%d:%d b.%d\n",(long long)bp->recvmask,bp->bestk,(long long)bp->bestmask,matches,paxmatches,paxbestmatches,bestmatches);
-            if ( bestmatches >= bp->minsigs && paxbestmatches >= bp->minsigs )
+            if ( bestmatches >= bp->minsigs && paxbestmatches >= bp->minsigs && bp->bestk >= 0 && bp->bestmask != 0 )
             {
-                if ( bp->pendingbestk != bp->bestk || bp->pendingbestmask != bp->bestmask )
+                if ( bp->pendingbestk < 0 )//bp->pendingbestk != bp->bestk || bp->pendingbestmask != bp->bestmask )
                 {
                     printf("new PENDING BESTK (%d %llx) state.%d\n",bp->bestk,(long long)bp->bestmask,bp->state);
                     bp->pendingbestk = bp->bestk;

From fb3e358d22689ec5bc06dfede4ff28d1f6de2056 Mon Sep 17 00:00:00 2001
From: jl777 <jameslee777@yahoo.com>
Date: Wed, 2 May 2018 17:18:47 +0300
Subject: [PATCH 2/4] dpow_recvdata

---
 iguana/dPoW.h              |  3 +++
 iguana/dpow/dpow_network.c | 21 ++++++++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/iguana/dPoW.h b/iguana/dPoW.h
index 6b99cf27d..52a6b43e2 100755
--- a/iguana/dPoW.h
+++ b/iguana/dPoW.h
@@ -105,12 +105,15 @@ struct dpow_checkpoint
     bits256 miner; uint32_t blocktime,timestamp;
 };
 
+struct dpow_recvdata { uint64_t recvmask,bestmask; int8_t bestk; };
+
 struct dpow_block
 {
     bits256 hashmsg,desttxid,srctxid,beacon,commit,MoM;
     struct iguana_info *srccoin,*destcoin; char *opret_symbol;
     uint64_t destsigsmasks[DPOW_MAXRELAYS],srcsigsmasks[DPOW_MAXRELAYS];
     uint64_t recvmask,bestmask,ratifybestmask,ratifyrecvmask,pendingbestmask,pendingratifybestmask,ratifysigmasks[2];
+    struct dpow_recvdata recv[64];
     struct dpow_entry notaries[DPOW_MAXRELAYS];
     uint32_t MoMdepth,state,starttime,timestamp,waiting,sigcrcs[2],txidcrcs[2],utxocrcs[2],lastepoch,paxwdcrc,lastnanosend;
     int32_t rawratifiedlens[2],height,numnotaries,numerrors,completed,minsigs,duration,numratified,isratify,require0,scores[DPOW_MAXRELAYS];
diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c
index ea000bcc1..d16469762 100755
--- a/iguana/dpow/dpow_network.c
+++ b/iguana/dpow/dpow_network.c
@@ -1507,13 +1507,14 @@ void dpow_bestconsensus(struct dpow_block *bp)
         bp->bestmatches = best;
         bp->notaries[bp->myind].bestmask = bp->bestmask = masks[besti];
         bp->notaries[bp->myind].bestk = bp->bestk = bestks[besti];
-        printf("set best.%d to (%d %llx) recv.%llx\n",best,bp->bestk,(long long)bp->bestmask,(long long)recvmask);
+        if ( bp->myind == 0 )
+            printf("%s.%d set best.%d to (%d %llx) recv.%llx\n",dp->symbol,bp->height,best,bp->bestk,(long long)bp->bestmask,(long long)recvmask);
     }
     bp->recvmask |= recvmask;
     if ( bp->bestmask == 0 )//|| (time(NULL) / 180) != bp->lastepoch )
     {
         bp->bestmask = dpow_notarybestk(bp->recvmask,bp,&bp->bestk);
-        if ( 0 && (time(NULL) / 180) != bp->lastepoch )
+        if ( 0 && (time(NULL) / 180) != bp->lastepoch ) // diverges too fast
         {
             bp->lastepoch = (uint32_t)(time(NULL) / 180);
             printf("epoch %u\n",bp->lastepoch % bp->numnotaries);
@@ -1955,7 +1956,21 @@ void dpow_nanoutxoget(struct supernet_info *myinfo,struct dpow_info *dp,struct d
     {
         dpow_notarize_update(myinfo,dp,bp,senderind,(int8_t)np->bestk,np->bestmask,np->recvmask,np->srcutxo,np->srcvout,np->destutxo,np->destvout,np->siglens,np->sigs,np->paxwdcrc);
         if ( bp->myind == 0 )
-            printf("%s.%d lag.[%d] RECV.%d %llx (%d %llx) %llx/%llx\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid);
+        {
+            int32_t i,matches = 0;
+            if ( np->bestk >= 0 )
+            {
+                bp->recv[senderind].recvmask = np->recvmask;
+                bp->recv[senderind].bestk = np->bestk;
+                bp->recv[senderind].bestmask = np->bestmask;
+                for (i=0; i<bp->numnotaries; i++)
+                {
+                    if ( bp->recv[i].recvmask == np->recvmask && bp->recv[i].bestmask == np->bestmask && bp->recv[i].bestk == np->bestk )
+                        matches++;
+                }
+            }
+            printf("%s.%d lag.[%2d] RECV.%d %llx (%2d %llx) %llx/%llx\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid,matches);
+        }
     }
     //dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask);
 }

From 81f74ecf2fcb81b90997b667ad1f5546c1ebd1b2 Mon Sep 17 00:00:00 2001
From: jl777 <jameslee777@yahoo.com>
Date: Wed, 2 May 2018 17:21:14 +0300
Subject: [PATCH 3/4] Fix

---
 iguana/dpow/dpow_network.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c
index d16469762..a5ad5eb48 100755
--- a/iguana/dpow/dpow_network.c
+++ b/iguana/dpow/dpow_network.c
@@ -1462,7 +1462,7 @@ void dpow_nanomsginit(struct supernet_info *myinfo,char *ipaddr)
     dpow_addnotary(myinfo,0,ipaddr);
 }
 
-void dpow_bestconsensus(struct dpow_block *bp)
+void dpow_bestconsensus(struct dpow_info *dp,struct dpow_block *bp)
 {
     int8_t bestks[64]; int32_t counts[64],i,j,numcrcs=0,numdiff,besti,best,bestmatches = 0,matches = 0; uint64_t masks[64],matchesmask,recvmask; uint32_t crcval=0; char srcaddr[64],destaddr[64];
     memset(masks,0,sizeof(masks));
@@ -1590,7 +1590,7 @@ void dpow_nanoutxoset(struct supernet_info *myinfo,struct dpow_info *dp,struct d
     }
     else
     {
-        dpow_bestconsensus(bp);
+        dpow_bestconsensus(dp,bp);
         np->srcutxo = bp->notaries[bp->myind].src.prev_hash;
         np->srcvout = bp->notaries[bp->myind].src.prev_vout;
         np->destutxo = bp->notaries[bp->myind].dest.prev_hash;
@@ -1861,7 +1861,7 @@ void dpow_notarize_update(struct supernet_info *myinfo,struct dpow_info *dp,stru
             bp->recvmask |= (1LL << senderind) | (1LL << bp->myind);
             bp->bestmask = dpow_maskmin(bp->recvmask,bp,&bp->bestk);
         }
-        dpow_bestconsensus(bp);
+        dpow_bestconsensus(dp,bp);
         if ( bp->bestk >= 0 )
             bp->notaries[bp->myind].bestk = bp->bestk;
         if ( bp->bestmask != 0 )
@@ -1969,7 +1969,7 @@ void dpow_nanoutxoget(struct supernet_info *myinfo,struct dpow_info *dp,struct d
                         matches++;
                 }
             }
-            printf("%s.%d lag.[%2d] RECV.%d %llx (%2d %llx) %llx/%llx\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid,matches);
+            printf("%s.%d lag.[%2d] RECV.%d %llx (%2d %llx) %llx/%llx matches.%d\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid,matches);
         }
     }
     //dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask);
@@ -2009,7 +2009,7 @@ void dpow_send(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_blo
         else src_or_dest = 1;
         extralen = dpow_paxpending(extras,sizeof(extras),&paxwdcrc,bp->MoM,bp->MoMdepth,src_or_dest,bp);
         bp->paxwdcrc = bp->notaries[bp->myind].paxwdcrc = np->notarize.paxwdcrc = paxwdcrc;
-        //dpow_bestconsensus(bp);
+        //dpow_bestconsensus(dp,bp);
         dpow_nanoutxoset(myinfo,dp,&np->notarize,bp,0);
     }
     else

From 338d8a451b7dd0e41eb79d3f774287d6bd1369a2 Mon Sep 17 00:00:00 2001
From: jl777 <jameslee777@yahoo.com>
Date: Wed, 2 May 2018 17:47:57 +0300
Subject: [PATCH 4/4] Test

---
 iguana/dpow/dpow_network.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c
index a5ad5eb48..5d9e22d74 100755
--- a/iguana/dpow/dpow_network.c
+++ b/iguana/dpow/dpow_network.c
@@ -1954,22 +1954,26 @@ void dpow_nanoutxoget(struct supernet_info *myinfo,struct dpow_info *dp,struct d
     }
     else
     {
+        int32_t i,bestmatches=0,matches = 0;
         dpow_notarize_update(myinfo,dp,bp,senderind,(int8_t)np->bestk,np->bestmask,np->recvmask,np->srcutxo,np->srcvout,np->destutxo,np->destvout,np->siglens,np->sigs,np->paxwdcrc);
-        if ( bp->myind == 0 )
+        if ( np->bestk >= 0 )
         {
-            int32_t i,matches = 0;
-            if ( np->bestk >= 0 )
+            bp->recv[senderind].recvmask = np->recvmask;
+            bp->recv[senderind].bestk = np->bestk;
+            bp->recv[senderind].bestmask = np->bestmask;
+            for (i=0; i<bp->numnotaries; i++)
             {
-                bp->recv[senderind].recvmask = np->recvmask;
-                bp->recv[senderind].bestk = np->bestk;
-                bp->recv[senderind].bestmask = np->bestmask;
-                for (i=0; i<bp->numnotaries; i++)
+                if ( bp->recv[i].recvmask == np->recvmask && bp->recv[i].bestmask == np->bestmask && bp->recv[i].bestk == np->bestk )
                 {
-                    if ( bp->recv[i].recvmask == np->recvmask && bp->recv[i].bestmask == np->bestmask && bp->recv[i].bestk == np->bestk )
-                        matches++;
+                    matches++;
+                    if ( ((1LL << i) & np->bestmask) != 0 )
+                        bestmatches++;
                 }
             }
-            printf("%s.%d lag.[%2d] RECV.%d %llx (%2d %llx) %llx/%llx matches.%d\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid,matches);
+        }
+        if ( bp->myind == 0 )
+        {
+            printf("%s.%d lag.[%2d] RECV.%d %llx (%2d %llx) %llx/%llx matches.%d best.%d\n",dp->symbol,bp->height,(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid,matches,bestmatches);
         }
     }
     //dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask);