]> Git Repo - VerusCoin.git/blobdiff - src/komodo_gateway.h
test
[VerusCoin.git] / src / komodo_gateway.h
index 035d766a0d3f9314c97120361ad26b8e3c1f7f65..e1546083766cb4809cbd0d6333c972f7ed3ca6a8 100644 (file)
 
 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
 
-int32_t pax_fiatstatus(uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
+int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
 {
-    int32_t baseid; struct komodo_state *sp;
+    int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed;
+    *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0;
     if ( (baseid= komodo_baseid(base)) >= 0 )
     {
         if ( (sp= komodo_stateptrget(base)) != 0 )
@@ -27,6 +28,10 @@ int32_t pax_fiatstatus(uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,
             *withdrawn = sp->withdrawn;
             *approved = sp->approved;
             *redeemed = sp->redeemed;
+            netliability = (sp->deposited - sp->withdrawn) - sp->shorted;
+            maxallowed = komodo_maxallowed(baseid);
+            if ( netliability < maxallowed )
+                *available = (maxallowed - netliability);
             //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
             return(0);
         } else printf("pax_fiatstatus cant get basesp.%s\n",base);
@@ -34,26 +39,46 @@ int32_t pax_fiatstatus(uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,
     return(-1);
 }
 
-struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout)
+void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
 {
-    struct pax_transaction *pax;
+    memcpy(buf,&txid,32);
+    memcpy(&buf[32],&vout,2);
+    buf[34] = type;
+}
+
+struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
+{
+    struct pax_transaction *pax; uint8_t buf[35];
     pthread_mutex_lock(&komodo_mutex);
-    HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
+    pax_keyset(buf,txid,vout,type);
+    HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
     pthread_mutex_unlock(&komodo_mutex);
     return(pax);
 }
 
-struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,int32_t mark)
+struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
 {
-    struct pax_transaction *pax;
+    struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'D', 'I', 'W', 'A', 'X' };
+    for (i=0; i<sizeof(types)/sizeof(*types); i++)
+        if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
+            return(pax);
+    return(0);
+}
+
+struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
+{
+    struct pax_transaction *pax; uint8_t buf[35];
     pthread_mutex_lock(&komodo_mutex);
-    HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
+    pax_keyset(buf,txid,vout,type);
+    HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
     if ( pax == 0 )
     {
         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
         pax->txid = txid;
         pax->vout = vout;
-        HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
+        pax->type = type;
+        memcpy(pax->buf,buf,sizeof(pax->buf));
+        HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
     }
     if ( pax != 0 )
@@ -67,18 +92,21 @@ struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout
     return(pax);
 }
 
-void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
+void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
 {
-    struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp,*basesp; char str[16],dest[16],*s;
+    struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
     sp = komodo_stateptr(str,dest);
     pthread_mutex_lock(&komodo_mutex);
-    HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
+    pax_keyset(buf,txid,vout,type);
+    HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
     if ( pax == 0 )
     {
         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
         pax->txid = txid;
         pax->vout = vout;
-        HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
+        pax->type = type;
+        memcpy(pax->buf,buf,sizeof(pax->buf));
+        HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
         addflag = 1;
         if ( ASSETCHAINS_SYMBOL[0] == 0 )
         {
@@ -105,45 +133,14 @@ void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t
             pax->height = height;
         if ( otherheight != 0 )
             pax->otherheight = otherheight;
-        if ( pax->didstats == 0 && fiatoshis != 0 )
-        {
-            if ( (pax->approved= approved) != 0 )
-            {
-                if ( (basesp= komodo_stateptrget(symbol)) != 0 )
-                {
-                    basesp->approved += fiatoshis;
-                    printf("########### %p approved %s += %.8f\n",basesp,symbol,dstr(fiatoshis));
-                }
-            }
-            else
-            {
-                if ( ASSETCHAINS_SYMBOL[0] == 0 )
-                {
-                    if ( (basesp= komodo_stateptrget(source)) != 0 )
-                    {
-                        basesp->withdrawn += fiatoshis;
-                        printf("########### %p withdrawn %s += %.8f\n",basesp,source,dstr(fiatoshis));
-                    }
-                }
-                else
-                {
-                    if ( (basesp= komodo_stateptrget(symbol)) != 0 )
-                    {
-                        basesp->deposited += fiatoshis;
-                        printf("########### %p deposited %s += %.8f\n",basesp,symbol,dstr(fiatoshis));
-                    }
-                }
-            }
-            pax->didstats = 1;
-        }
         if ( pax->marked == 0 )
         {
             if ( addflag != 0 )
             {
                 if ( (pax->approved= approved) != 0 )
                     s = (char *)"APPROVED";
-                else s = (char *)((ASSETCHAINS_SYMBOL[0] == 0) ? "WITHDRAW" : "DEPOSIT");
-                printf("[%s] addflag.%d ADD %s/%s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,source,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
+                else s = (char *)((strcmp(pax->symbol,"KMD") == 0) ? "WITHDRAW" : "DEPOSIT");
+                printf("[%s] addflag.%d ADD %s/%s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,source,dstr(strcmp(pax->symbol,"KMD")==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
             }
         }
         //else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height,otherheight);
@@ -196,14 +193,15 @@ int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transactio
 
 int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
 {
-    struct pax_transaction p; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16];
+    struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16];
     incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
     for (i=0; i<4; i++)
         base[i] = opretbuf[opretlen-4+i];
     //for (i=0; i<opretlen; i++)
     //    printf("%02x",opretbuf[i]);
+    //printf(" opretlen.%d (%s)\n",opretlen,base);
     //printf(" opretlen.%d vs %d incr.%d\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4),incr);
-    if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
+    //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
     {
         type = opretbuf[0];
         opretbuf++, opretlen--;
@@ -217,8 +215,8 @@ int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t
                 {
                     txids[n] = p.txid;
                     vouts[n] = p.vout;
-                    values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis;
-                    srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.fiatoshis : p.komodoshis;
+                    values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
+                    srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
                     kmdheights[n] = p.height;
                     otherheights[n] = p.otherheight;
                     memcpy(&rmd160s[n * 20],p.rmd160,20);
@@ -239,6 +237,15 @@ int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t
                 }
                 vouts[n] = opretbuf[len++];
                 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
+                baseids[n] = komodo_baseid(base);
+                if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
+                {
+                    values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
+                    srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
+                    kmdheights[n] = pax->height;
+                    otherheights[n] = pax->otherheight;
+                    memcpy(&rmd160s[n * 20],pax->rmd160,20);
+                }
             }
             //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
         }
@@ -248,9 +255,40 @@ int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t
 
 uint64_t komodo_paxtotal()
 {
-    struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; int32_t ht; int64_t checktoshis; uint64_t seed,total = 0;
+    struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp;
     if ( komodo_isrealtime(&ht) == 0 )
         return(0);
+    else
+    {
+        HASH_ITER(hh,PAX,pax,tmp)
+        {
+            if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
+                str = pax->symbol;
+            else str = pax->source;
+            basesp = komodo_stateptrget(str);
+            if ( basesp != 0 && pax->didstats == 0 && pax->type == 'I' )
+            {
+                if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
+                {
+                    if ( pax2->fiatoshis != 0 )
+                    {
+                        pax->komodoshis = pax2->komodoshis;
+                        pax->fiatoshis = pax2->fiatoshis;
+                        basesp->issued += pax->fiatoshis;
+                        pax->didstats = 1;
+                        printf("Iset %s dstats %.8f += %.8f\n",str,dstr(basesp->issued),dstr(pax->fiatoshis));
+                        pax2->marked = pax->height;
+                    }
+                }
+            }
+            /*if ( strcmp(str,"HRK") == 0 || strcmp("HRK",pax->symbol) == 0 || strcmp("HRK",pax->source) == 0 )
+            {
+                for (i=0; i<32; i++)
+                    printf("%02x",((uint8_t *)&pax->txid)[i]);
+                printf(" stats.%d type.%c (%s) k.%d %.8f h.%d %.8f I.%.8f X.%.8f\n",pax->didstats,pax->type,str,pax->height,dstr(pax->komodoshis),pax->otherheight,dstr(pax->fiatoshis),dstr(basesp->issued),dstr(basesp->redeemed));
+            }*/
+        }
+    }
     komodo_stateptr(symbol,dest);
     HASH_ITER(hh,PAX,pax,tmp)
     {
@@ -323,16 +361,23 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t to
         opcode = 'I';
         if ( komodo_isrealtime(&ht) == 0 )
             return(0);
-    }
-    else opcode = 'X';
+    } else opcode = 'X';
     HASH_ITER(hh,PAX,pax,tmp)
     {
         //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
-        if ( strcmp(symbol,"KMD") == 0 && (pax->approved == 0 || pax->validated == 0) )
+        if ( strcmp(symbol,"KMD") == 0 && pax->approved == 0 )
             continue;
-        if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 )
+        else if ( strcmp(symbol,"KMD") != 0 )
+        {
+#ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
+            struct komodo_state *kmdsp = komodo_stateptrget("KMD");
+            if ( kmdsp != 0 && kmdsp->notarized_height >= pax->height ) // assumes same chain as notarize
+                pax->validated = kmdsp->notarized_height;
+#endif
+        }
+        if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 )
             continue;
-        if ( ASSETCHAINS_SYMBOL[0] != 0 )
+        if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
             printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
         txNew->vout.resize(numvouts+1);
         txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
@@ -413,42 +458,45 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
         {
             for (i=1; i<n-1; i++)
             {
-                if ( (pax= komodo_paxfind(txids[i-1],vouts[i-1])) != 0 )
+                if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 )
                 {
-                    if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
+                    pax->type = opcode;
+                    if ( ((opcode == 'I' && (pax->fiatoshis == 0 || pax->fiatoshis == block.vtx[0].vout[i].nValue)) || (opcode == 'X' && (pax->komodoshis == 0 || pax->komodoshis == block.vtx[0].vout[i].nValue))) )
                     {
                         if ( pax->marked != 0 && height >= 80820 )
                             errs++;
                         else matched++;
-                        if ( opcode == 'X' )
-                            printf("errs.%d i.%d match %.8f == %.8f pax.%p\n",errs,i,dstr(pax != 0 ? pax->komodoshis:-1),dstr(block.vtx[0].vout[i].nValue),pax);
+                        //if ( opcode == 'X' )
+                        printf("%c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax);
                     }
                     else
                     {
-                        if ( opcode == 'X' )
+                        //if ( opcode == 'X' )
                         {
                             for (j=0; j<32; j++)
                                 printf("%02x",((uint8_t *)&txids[i-1])[j]);
-                            printf(" cant paxfind X txid\n");
+                            printf(" cant paxfind %c txid\n",opcode);
+                            printf("%c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax);
                         }
                     }
                 }
                 else
                 {
-                    if  ( opcode == 'X' )
+                    //if  ( opcode == 'X' )
                     {
                         hash = block.GetHash();
                         for (j=0; j<32; j++)
                             printf("%02x",((uint8_t *)&hash)[j]);
                         printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
-                    } else if ( opcode == 'I' )
-                        matched++;
+                    } //else if ( opcode == 'I' )
+                      //  matched++;
                 }
             }
             if ( matched != num )
             {
+                printf("WOULD REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
                 // can easily happen depending on order of loading
-                if ( height > 100000 && opcode == 'X' )
+                if ( height > 100000 ) //&& opcode == 'X' )
                 {
                     printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
                     return(-1);
@@ -465,8 +513,8 @@ int32_t komodo_paxcmp(uint64_t value,uint64_t checkvalue,uint64_t seed)
     int32_t ratio;
     if ( seed == 0 )
     {
-        ratio = ((value << 10) / checkvalue);
-        if ( ratio >= 1023 && ratio <= 1025 )
+        ratio = ((value << 6) / checkvalue);
+        if ( ratio >= 63 && ratio <= 65 )
             return(0);
         else
         {
@@ -487,7 +535,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
     memset(rmd160s,0,sizeof(rmd160s));
     memset(kmdheights,0,sizeof(kmdheights));
     memset(otherheights,0,sizeof(otherheights));
-    tokomodo = (komodo_is_issuer() == 0);
+    tokomodo = (komodo_baseid(source) >= 0 && komodo_baseid(source) != 32); //(komodo_is_issuer() == 0);
     if ( opretbuf[0] == 'D' )
     {
         if ( opretlen == 38 ) // any KMD tx
@@ -508,9 +556,9 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                     printf("%02x",pubkey33[i]);
                 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
                 didstats = 0;
-                if ( komodo_paxcmp(value,checktoshis,seed) == 0 )//value >= checktoshis-(checktoshis >> 8) )
+                if ( komodo_paxcmp(value,checktoshis,seed) == 0 )
                 {
-                    if ( (pax= komodo_paxfind(txid,vout)) == 0 )
+                    if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
                     {
                         if ( (basesp= komodo_stateptrget(base)) != 0 )
                         {
@@ -518,22 +566,22 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                             didstats = 1;
                             printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
                         }
-                        if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
-                        {
-                            komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height,(char *)"KMD",0);
-                        }
+                        komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
                     }
-                    else if ( pax->didstats == 0 )
+                    if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
                     {
-                        if ( (basesp= komodo_stateptrget(base)) != 0 )
+                        if ( didstats == 0 && pax->didstats == 0 )
                         {
-                            basesp->deposited += fiatoshis;
-                            didstats = 1;
-                            printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
+                            if ( (basesp= komodo_stateptrget(base)) != 0 )
+                            {
+                                basesp->deposited += fiatoshis;
+                                didstats = 1;
+                                printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
+                            }
                         }
+                        if ( didstats != 0 )
+                            pax->didstats = 1;
                     }
-                    if ( didstats != 0 && (pax= komodo_paxfind(txid,vout)) != 0 )
-                        pax->didstats = 1;
                 }
             }
         }
@@ -548,9 +596,9 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
         typestr = "withdraw";
         printf("%s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s)\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr);
         didstats = 0;
-        if ( komodo_paxcmp(komodoshis,checktoshis,seed) == 0 ) //checktoshis <= komodoshis+(komodoshis >> 10) )
+        if ( komodo_paxcmp(komodoshis,checktoshis,seed) == 0 )
         {
-            if ( (pax= komodo_paxfind(txid,vout)) == 0 || pax->didstats == 0 )
+            if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
             {
                 if ( (basesp= komodo_stateptrget(base)) != 0 )
                 {
@@ -560,9 +608,13 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                 }
                 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
             }
-            komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height,source,0);
-            if ( didstats != 0 && (pax= komodo_paxfind(txid,vout)) != 0 )
-                pax->didstats = 1;
+            komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
+            if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
+            {
+                if ( didstats != 0 )
+                    pax->didstats = 1;
+                pax->type = opretbuf[0];
+            }
         }
     }
     else if ( tokomodo != 0 && opretbuf[0] == 'A' )
@@ -586,11 +638,13 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                 for (j=0; j<32; j++)
                     printf("%02x",((uint8_t *)&txids[i])[j]);
                 printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);
-                if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 )
+                if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
                 {
                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
-                    komodo_gateway_deposit(coinaddr,values[i],CURRENCIES[baseids[i]],srcvalues[i],&rmd160s[i*20],txids[i],vouts[i],kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
-                    if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
+                    komodo_gateway_deposit(coinaddr,values[i],CURRENCIES[baseids[i]],srcvalues[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
+                    komodo_paxmark(height,txids[i],vouts[i],'W',height);
+                    komodo_paxmark(height,txids[i],vouts[i],'A',height);
+                    if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
                     {
                         basesp->approved += srcvalues[i];
                         didstats = 1;
@@ -598,7 +652,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                     }
                     printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
                 }
-                else if ( pax->didstats == 0 )
+                else if ( pax->didstats == 0 && srcvalues[i] != 0 )
                 {
                     if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
                     {
@@ -608,8 +662,9 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                     }
                 }
                 else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
-                if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
+                if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
                 {
+                    pax->type = opretbuf[0];
                     pax->approved = kmdheights[i];
                     if ( didstats != 0 )
                         pax->didstats = 1;
@@ -619,7 +674,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
         }
         printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
     }
-    else if ( tokomodo == 0 && opretbuf[0] == 'I' )
+    else if ( opretbuf[0] == 'I' )
     {
         if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
         {
@@ -628,54 +683,45 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
                 for (i=0; i<n; i++)
                 {
                     if ( baseids[i] < 0 )
-                        continue;
-                    didstats = 0;
-                    bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
-                    if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 || pax->didstats == 0 )
                     {
-                        if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
-                        {
-                            basesp->issued += srcvalues[i];
-                            printf("########### %p issued %s += %.8f\n",basesp,base,dstr(srcvalues[i]));
-                        }
-                        didstats = 1;
+                        printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
+                        continue;
                     }
-                    if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 )
+                    bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
+                    komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
+                    komodo_paxmark(height,txids[i],vouts[i],'D',height);
+                    komodo_paxmark(height,txids[i],vouts[i],'I',height);
+                    if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
                     {
-                        komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,CURRENCIES[baseids[i]],0);
+                        pax->type = opretbuf[0];
+                        strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
                     }
-                    if ( didstats != 0 && (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
-                        pax->didstats = 1;
                 }
-            } else printf("opreturn none issued?\n");
-        }
+            } //else printf("opreturn none issued?\n");
+        } //else printf("opreturn matches KMD? %s\n",(char *)&opretbuf[opretlen-4]);
     }
     else if ( opretbuf[0] == 'X' )
     {
-        printf("got X opreturn height.%d\n",height);
         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
         {
             for (i=0; i<n; i++)
             {
                 if ( baseids[i] < 0 )
                     continue;
-                didstats = 0;
                 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
-                printf("i.%d of %d: %.8f -> %s\n",i,n,dstr(values[i]),coinaddr);
-                if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 || pax->didstats == 0 )
+                komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
+                komodo_paxmark(height,txids[i],vouts[i],'W',height);
+                komodo_paxmark(height,txids[i],vouts[i],'A',height);
+                komodo_paxmark(height,txids[i],vouts[i],'X',height);
+                if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
                 {
-                    if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
+                    pax->type = opretbuf[0];
+                    if ( baseids[i] >= 0 && srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
                     {
                         basesp->redeemed += srcvalues[i];
-                        didstats = 1;
-                        printf("########### %p redeemed %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i]));
+                        pax->didstats = 1;
                     }
-                    didstats = 1;
                 }
-                if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 )
-                    komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,(char *)"KMD",0);
-                if ( didstats != 0 && (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
-                    pax->didstats = 1;
             }
         } else printf("komodo_issued_opreturn returned %d\n",n);
     }
@@ -696,29 +742,28 @@ void komodo_passport_iteration()
         refid = 33;
     else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
     //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
-    for (baseid=0; baseid<=32; baseid++)
+    for (baseid=32; baseid>=0; baseid--)
     {
         sp = 0;
         isrealtime = 0;
         base = (char *)CURRENCIES[baseid];
-if ( strcmp("EUR",base) != 0 && baseid < 32 )
-    continue;
         if ( baseid+1 != refid )
         {
             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
             komodo_nameset(symbol,dest,base);
-            //port = komodo_port(base,10,&magic) + 1;
             if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
             {
                 fseek(fp,0,SEEK_END);
                 if ( ftell(fp) > lastpos[baseid] )
                 {
-                    //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
+                    if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
+                        printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
                     fseek(fp,lastpos[baseid],SEEK_SET);
                     while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
                         ;
                     lastpos[baseid] = ftell(fp);
-                    //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
+                    if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
+                        printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
                 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
                 fclose(fp);
             }
@@ -761,6 +806,7 @@ if ( strcmp("EUR",base) != 0 && baseid < 32 )
         if ( sp != 0 && isrealtime == 0 )
             refsp->RTbufs[0][2] = 0;
     }
+    komodo_paxtotal();
     refsp->RTmask = RTmask;
     KOMODO_PASSPORT_INITDONE = 1;
 }
This page took 0.04415 seconds and 4 git commands to generate.