]> Git Repo - VerusCoin.git/blobdiff - src/komodo_kv.h
test
[VerusCoin.git] / src / komodo_kv.h
index f4f31272432ad9b3e96cbd130f15028ae4aa78a2..1ed6e1347879f04d20207cff8feed754196a746f 100644 (file)
 #ifndef H_KOMODOKV_H
 #define H_KOMODOKV_H
 
+int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize)
+{
+    if ( refvalue == 0 && value == 0 )
+        return(0);
+    else if ( refvalue == 0 || value == 0 )
+        return(-1);
+    else if ( refvaluesize != valuesize )
+        return(-1);
+    else return(memcmp(refvalue,value,valuesize));
+}
+
+int32_t komodo_kvnumdays(uint32_t flags)
+{
+    int32_t numdays;
+    if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 )
+        numdays = 365;
+    return(numdays);
+}
+
+int32_t komodo_kvduration(uint32_t flags)
+{
+    return(komodo_kvnumdays(flags) * KOMODO_KVDURATION);
+}
+
+uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen)
+{
+    int32_t numdays,k; uint64_t fee;
+    if ( (k= keylen) > 32 )
+        k = 32;
+    numdays = komodo_kvnumdays(flags);
+    if ( (fee= (numdays*(opretlen * opretlen / k))) < 100000 )
+        fee = 100000;
+    return(fee);
+}
+
 int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen)
 {
     struct komodo_kv *ptr; int32_t duration,retval = -1;
@@ -26,7 +61,7 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp
     HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
     if ( ptr != 0 )
     {
-        duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION;
+        duration = komodo_kvduration(ptr->flags);
         //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize);
         if ( current_height > (ptr->height + duration) )
         {
@@ -41,6 +76,12 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp
         {
             *heightp = ptr->height;
             *flagsp = ptr->flags;
+            int32_t i; for (i=0; i<32; i++)
+            {
+                //printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]);
+                ((uint8_t *)pubkeyp)[i] = ((uint8_t *)&ptr->pubkey)[31-i];
+            }
+            //printf(" ptr->pubkey\n");
             memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp));
             if ( (retval= ptr->valuesize) != 0 )
                 memcpy(value,ptr->value,retval);
@@ -50,43 +91,10 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp
     return(retval);
 }
 
-int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize)
-{
-    if ( refvalue == 0 && value == 0 )
-        return(0);
-    else if ( refvalue == 0 || value == 0 )
-        return(-1);
-    else if ( refvaluesize != valuesize )
-        return(-1);
-    else return(memcmp(refvalue,value,valuesize));
-}
-
-int32_t komodo_kvnumdays(uint32_t flags)
-{
-    int32_t numdays;
-    if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 )
-        numdays = 365;
-    return(numdays);
-}
-
-int32_t komodo_kvduration(uint32_t flags)
-{
-    return(komodo_kvnumdays(flags) * KOMODO_KVDURATION);
-}
-
-uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen)
-{
-    int32_t numdays; uint64_t fee;
-    numdays = komodo_kvnumdays(flags);
-    if ( (fee= (numdays*(opretlen * opretlen / keylen))) < 100000 )
-        fee = 100000;
-    return(fee);
-}
-
 void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
 {
     static uint256 zeroes;
-    uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee;
+    uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,keyvalue[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee;
     iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen);
     iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize);
     iguana_rwnum(0,&opretbuf[5],sizeof(height),&height);
@@ -112,13 +120,14 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
                 for (i=0; i<32; i++)
                     ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i];
             }
-            if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 )
+            memcpy(keyvalue,key,keylen);
+            if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&keyvalue[keylen],key,keylen)) >= 0 )
             {
                 if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
                 {
-                    if ( memcmp(&pubkey,&refpubkey,sizeof(pubkey)) != 0 || komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 )
+                    if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 )
                     {
-                        printf("komodo_kvsigverify error [%d]\n",coresize-13);
+                        //printf("komodo_kvsigverify error [%d]\n",coresize-13);
                         return;
                     }
                 }
@@ -127,15 +136,13 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
             HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
             if ( ptr != 0 )
             {
-                if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
+                //if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 )
                 {
                     tstr = (char *)"transfer:";
                     transferpubstr = (char *)&valueptr[strlen(tstr)];
                     if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 )
                     {
-                        memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey));
-                        ptr->height = height;
-                        printf("transfer.(%s) to [%s]\n",key,transferpubstr);
+                        printf("transfer.(%s) to [%s]? ishex.%d\n",key,transferpubstr,is_hexstr(transferpubstr,0));
                         for (i=0; i<32; i++)
                             ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]);
                     }
@@ -159,12 +166,19 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
                     ptr->value = (uint8_t *)calloc(1,valuesize);
                     memcpy(ptr->value,valueptr,valuesize);
                 }
-                ptr->height = height;
-                ptr->flags = flags;
             }
+            /*for (i=0; i<32; i++)
+                printf("%02x",((uint8_t *)&ptr->pubkey)[i]);
+            printf(" <- ");
+            for (i=0; i<32; i++)
+                printf("%02x",((uint8_t *)&pubkey)[i]);
+            printf(" new pubkey\n");*/
+            memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey));
+            ptr->height = height;
+            ptr->flags = flags | 1;
             portable_mutex_unlock(&KOMODO_KV_mutex);
-        } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]);
-    } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize);
+        } //else printf("size mismatch %d vs %d\n",opretlen,coresize);
+    } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]);
 }
 
 #endif
This page took 0.040917 seconds and 4 git commands to generate.