1 /******************************************************************************
2 * Copyright © 2014-2017 The SuperNET Developers. *
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. *
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 *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
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)
21 struct komodo_kv *ptr; int32_t duration,retval = -1;
24 memset(pubkeyp,0,sizeof(*pubkeyp));
25 portable_mutex_lock(&KOMODO_KV_mutex);
26 HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
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) )
33 HASH_DELETE(hh,KOMODO_KV,ptr);
34 if ( ptr->value != 0 )
42 *heightp = ptr->height;
44 memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp));
45 if ( (retval= ptr->valuesize) != 0 )
46 memcpy(value,ptr->value,retval);
49 portable_mutex_unlock(&KOMODO_KV_mutex);
53 int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize)
55 if ( refvalue == 0 && value == 0 )
57 else if ( refvalue == 0 || value == 0 )
59 else if ( refvaluesize != valuesize )
61 else return(memcmp(refvalue,value,valuesize));
64 int32_t komodo_kvnumdays(uint32_t flags)
67 if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 )
72 int32_t komodo_kvduration(uint32_t flags)
74 return(komodo_kvnumdays(flags) * KOMODO_KVDURATION);
77 uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen)
79 int32_t numdays; uint64_t fee;
80 numdays = komodo_kvnumdays(flags);
81 if ( (fee= (numdays*(opretlen * opretlen / keylen))) < 100000 )
86 void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
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);
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]);
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) )
103 memset(&pubkey,0,sizeof(pubkey));
104 memset(&sig,0,sizeof(sig));
105 if ( (haspubkey= (opretlen >= coresize+sizeof(uint256))) != 0 )
108 ((uint8_t *)&pubkey)[i] = opretbuf[coresize+i];
110 if ( (hassig= (opretlen == coresize+sizeof(uint256)*2)) != 0 )
113 ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i];
115 if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 )
117 if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
119 if ( komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 )
121 printf("komodo_kvsigverify error [%d]\n",coresize-13);
126 portable_mutex_lock(&KOMODO_KV_mutex);
127 HASH_FIND(hh,KOMODO_KV,key,keylen,ptr);
130 if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
132 tstr = (char *)"transfer:";
133 transferpubstr = (char *)&valueptr[strlen(tstr)];
134 if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 )
136 printf("transfer.(%s) to [%s]\n",key,transferpubstr);
138 ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]);
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);
149 HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr);
151 if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 )
153 if ( ptr->value != 0 )
154 free(ptr->value), ptr->value = 0;
155 if ( (ptr->valuesize= valuesize) != 0 )
157 ptr->value = (uint8_t *)calloc(1,valuesize);
158 memcpy(ptr->value,valueptr,valuesize);
161 memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey));
162 ptr->height = height;
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);