]> Git Repo - VerusCoin.git/blob - src/komodo_kv.h
test
[VerusCoin.git] / src / komodo_kv.h
1 /******************************************************************************
2  * Copyright © 2014-2017 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #ifndef H_KOMODOKV_H
17 #define H_KOMODOKV_H
18
19 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)
20 {
21     struct komodo_kv *ptr; int32_t duration,retval = -1;
22     *heightp = -1;
23     *flagsp = 0;
24     memset(pubkeyp,0,sizeof(*pubkeyp));
25     portable_mutex_lock(&KOMODO_KV_mutex);
26     HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
27     if ( ptr != 0 )
28     {
29         duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION;
30         //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);
31         if ( current_height > (ptr->height + duration) )
32         {
33             HASH_DELETE(hh,KOMODO_KV,ptr);
34             if ( ptr->value != 0 )
35                 free(ptr->value);
36             if ( ptr->key != 0 )
37                 free(ptr->key);
38             free(ptr);
39         }
40         else
41         {
42             *heightp = ptr->height;
43             *flagsp = ptr->flags;
44             memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp));
45             if ( (retval= ptr->valuesize) != 0 )
46                 memcpy(value,ptr->value,retval);
47         }
48     }
49     portable_mutex_unlock(&KOMODO_KV_mutex);
50     return(retval);
51 }
52
53 int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize)
54 {
55     if ( refvalue == 0 && value == 0 )
56         return(0);
57     else if ( refvalue == 0 || value == 0 )
58         return(-1);
59     else if ( refvaluesize != valuesize )
60         return(-1);
61     else return(memcmp(refvalue,value,valuesize));
62 }
63
64 int32_t komodo_kvnumdays(uint32_t flags)
65 {
66     int32_t numdays;
67     if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 )
68         numdays = 365;
69     return(numdays);
70 }
71
72 int32_t komodo_kvduration(uint32_t flags)
73 {
74     return(komodo_kvnumdays(flags) * KOMODO_KVDURATION);
75 }
76
77 uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen)
78 {
79     int32_t numdays; uint64_t fee;
80     numdays = komodo_kvnumdays(flags);
81     if ( (fee= (numdays*(opretlen * opretlen / keylen))) < 100000 )
82         fee = 100000;
83     return(fee);
84 }
85
86 void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
87 {
88     static uint256 zeroes;
89     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;
90     iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen);
91     iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize);
92     iguana_rwnum(0,&opretbuf[5],sizeof(height),&height);
93     iguana_rwnum(0,&opretbuf[9],sizeof(flags),&flags);
94     key = &opretbuf[13];
95     valueptr = &key[keylen];
96     fee = komodo_kvfee(flags,opretlen,keylen);
97     //printf("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]);
98     if ( value >= fee )
99     {
100         coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1);
101         if ( opretlen == coresize || opretlen == coresize+sizeof(uint256) || opretlen == coresize+2*sizeof(uint256) )
102         {
103             memset(&pubkey,0,sizeof(pubkey));
104             memset(&sig,0,sizeof(sig));
105             if ( (haspubkey= (opretlen >= coresize+sizeof(uint256))) != 0 )
106             {
107                 for (i=0; i<32; i++)
108                     ((uint8_t *)&pubkey)[i] = opretbuf[coresize+i];
109             }
110             if ( (hassig= (opretlen == coresize+sizeof(uint256)*2)) != 0 )
111             {
112                 for (i=0; i<32; i++)
113                     ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i];
114             }
115             if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 )
116             {
117                 if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
118                 {
119                     if ( komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 )
120                     {
121                         printf("komodo_kvsigverify error [%d]\n",coresize-13);
122                         return;
123                     }
124                 }
125             }
126             portable_mutex_lock(&KOMODO_KV_mutex);
127             HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
128             if ( ptr != 0 )
129             {
130                 if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
131                 {
132                     tstr = (char *)"transfer:";
133                     transferpubstr = (char *)&valueptr[strlen(tstr)];
134                     if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 )
135                     {
136                         printf("transfer.(%s) to [%s]\n",key,transferpubstr);
137                         for (i=0; i<32; i++)
138                             ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]);
139                     }
140                 }
141             }
142             else if ( ptr == 0 )
143             {
144                 ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr));
145                 ptr->key = (uint8_t *)calloc(1,keylen);
146                 ptr->keylen = keylen;
147                 memcpy(ptr->key,key,keylen);
148                 newflag = 1;
149                 HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr);
150             }
151             if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 )
152             {
153                 if ( ptr->value != 0 )
154                     free(ptr->value), ptr->value = 0;
155                 if ( (ptr->valuesize= valuesize) != 0 )
156                 {
157                     ptr->value = (uint8_t *)calloc(1,valuesize);
158                     memcpy(ptr->value,valueptr,valuesize);
159                 }
160             }
161             memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey));
162             ptr->height = height;
163             ptr->flags = flags;
164             portable_mutex_unlock(&KOMODO_KV_mutex);
165         } else printf("size mismatch %d vs %d\n",opretlen,coresize);
166     } 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]);
167     } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize);
168 }
169
170 #endif
This page took 0.04258 seconds and 4 git commands to generate.