1515
1616#ifndef H_KOMODO_H
1717#define H_KOMODO_H
18+ #include "komodo_defs.h"
1819
1920#ifdef _WIN32
2021#define printf (...)
@@ -56,14 +57,14 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
5657
5758void komodo_currentheight_set (int32_t height )
5859{
59- char symbol [16 ],dest [16 ]; struct komodo_state * sp ;
60+ char symbol [KOMODO_ASSETCHAIN_MAXLEN ],dest [KOMODO_ASSETCHAIN_MAXLEN ]; struct komodo_state * sp ;
6061 if ( (sp = komodo_stateptr (symbol ,dest )) != 0 )
6162 sp -> CURRENT_HEIGHT = height ;
6263}
6364
6465int32_t komodo_currentheight ()
6566{
66- char symbol [16 ],dest [16 ]; struct komodo_state * sp ;
67+ char symbol [KOMODO_ASSETCHAIN_MAXLEN ],dest [KOMODO_ASSETCHAIN_MAXLEN ]; struct komodo_state * sp ;
6768 if ( (sp = komodo_stateptr (symbol ,dest )) != 0 )
6869 return (sp -> CURRENT_HEIGHT );
6970 else return (0 );
@@ -192,11 +193,143 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
192193 return (func );
193194 } else return (-1 );
194195}
195-
196+
197+ int32_t memread (void * dest ,int32_t size ,uint8_t * filedata ,long * fposp ,long datalen )
198+ {
199+ if ( * fposp + size <= datalen )
200+ {
201+ memcpy (dest ,& filedata [* fposp ],size );
202+ (* fposp ) += size ;
203+ return (size );
204+ }
205+ return (-1 );
206+ }
207+
208+ int32_t komodo_parsestatefiledata (struct komodo_state * sp ,uint8_t * filedata ,long * fposp ,long datalen ,char * symbol ,char * dest )
209+ {
210+ static int32_t errs ;
211+ int32_t func = -1 ,ht ,notarized_height ,num ,matched = 0 ; uint256 notarized_hash ,notarized_desttxid ; uint8_t pubkeys [64 ][33 ]; long fpos = * fposp ;
212+ if ( fpos < datalen )
213+ {
214+ func = filedata [fpos ++ ];
215+ if ( ASSETCHAINS_SYMBOL [0 ] == 0 && strcmp (symbol ,"KMD" ) == 0 )
216+ matched = 1 ;
217+ else matched = (strcmp (symbol ,ASSETCHAINS_SYMBOL ) == 0 );
218+ if ( memread (& ht ,sizeof (ht ),filedata ,& fpos ,datalen ) != sizeof (ht ) )
219+ errs ++ ;
220+ if ( func == 'P' )
221+ {
222+ if ( (num = filedata [fpos ++ ]) <= 64 )
223+ {
224+ if ( memread (pubkeys ,33 * num ,filedata ,& fpos ,datalen ) != 33 * num )
225+ errs ++ ;
226+ else
227+ {
228+ //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
229+ if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0 ) || (strcmp (symbol ,"KMD" ) == 0 && KOMODO_EXTERNAL_NOTARIES == 0 ) )
230+ komodo_eventadd_pubkeys (sp ,symbol ,ht ,num ,pubkeys );
231+ }
232+ } else printf ("illegal num.%d\n" ,num );
233+ }
234+ else if ( func == 'N' )
235+ {
236+ if ( memread (& notarized_height ,sizeof (notarized_height ),filedata ,& fpos ,datalen ) != sizeof (notarized_height ) )
237+ errs ++ ;
238+ if ( memread (& notarized_hash ,sizeof (notarized_hash ),filedata ,& fpos ,datalen ) != sizeof (notarized_hash ) )
239+ errs ++ ;
240+ if ( memread (& notarized_desttxid ,sizeof (notarized_desttxid ),filedata ,& fpos ,datalen ) != sizeof (notarized_desttxid ) )
241+ errs ++ ;
242+ if ( 0 && ASSETCHAINS_SYMBOL [0 ] != 0 && sp != 0 )
243+ printf ("%s load[%s.%d -> %s] NOTARIZED %d %s\n" ,ASSETCHAINS_SYMBOL ,symbol ,sp -> NUM_NPOINTS ,dest ,notarized_height ,notarized_hash .ToString ().c_str ());
244+ //if ( matched != 0 ) global independent states -> inside *sp
245+ komodo_eventadd_notarized (sp ,symbol ,ht ,dest ,notarized_hash ,notarized_desttxid ,notarized_height );
246+ }
247+ else if ( func == 'U' ) // deprecated
248+ {
249+ uint8_t n ,nid ; uint256 hash ; uint64_t mask ;
250+ n = filedata [fpos ++ ];
251+ nid = filedata [fpos ++ ];
252+ //printf("U %d %d\n",n,nid);
253+ if ( memread (& mask ,sizeof (mask ),filedata ,& fpos ,datalen ) != sizeof (mask ) )
254+ errs ++ ;
255+ if ( memread (& hash ,sizeof (hash ),filedata ,& fpos ,datalen ) != sizeof (hash ) )
256+ errs ++ ;
257+ }
258+ else if ( func == 'K' )
259+ {
260+ int32_t kheight ;
261+ if ( memread (& kheight ,sizeof (kheight ),filedata ,& fpos ,datalen ) != sizeof (kheight ) )
262+ errs ++ ;
263+ komodo_eventadd_kmdheight (sp ,symbol ,ht ,kheight ,0 );
264+ }
265+ else if ( func == 'T' )
266+ {
267+ int32_t kheight ,ktimestamp ;
268+ if ( memread (& kheight ,sizeof (kheight ),filedata ,& fpos ,datalen ) != sizeof (kheight ) )
269+ errs ++ ;
270+ if ( memread (& ktimestamp ,sizeof (ktimestamp ),filedata ,& fpos ,datalen ) != sizeof (ktimestamp ) )
271+ errs ++ ;
272+ //if ( matched != 0 ) global independent states -> inside *sp
273+ //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
274+ komodo_eventadd_kmdheight (sp ,symbol ,ht ,kheight ,ktimestamp );
275+ }
276+ else if ( func == 'R' )
277+ {
278+ uint16_t olen ,v ; uint64_t ovalue ; uint256 txid ; uint8_t opret [16384 ];
279+ if ( memread (& txid ,sizeof (txid ),filedata ,& fpos ,datalen ) != sizeof (txid ) )
280+ errs ++ ;
281+ if ( memread (& v ,sizeof (v ),filedata ,& fpos ,datalen ) != sizeof (v ) )
282+ errs ++ ;
283+ if ( memread (& ovalue ,sizeof (ovalue ),filedata ,& fpos ,datalen ) != sizeof (ovalue ) )
284+ errs ++ ;
285+ if ( memread (& olen ,sizeof (olen ),filedata ,& fpos ,datalen ) != sizeof (olen ) )
286+ errs ++ ;
287+ if ( olen < sizeof (opret ) )
288+ {
289+ if ( memread (opret ,olen ,filedata ,& fpos ,datalen ) != olen )
290+ errs ++ ;
291+ if ( 0 && ASSETCHAINS_SYMBOL [0 ] != 0 && matched != 0 )
292+ {
293+ int32_t i ; for (i = 0 ; i < olen ; i ++ )
294+ printf ("%02x" ,opret [i ]);
295+ printf (" %s.%d load[%s] opret[%c] len.%d %.8f\n" ,ASSETCHAINS_SYMBOL ,ht ,symbol ,opret [0 ],olen ,(double )ovalue /COIN );
296+ }
297+ komodo_eventadd_opreturn (sp ,symbol ,ht ,txid ,ovalue ,v ,opret ,olen ); // global shared state -> global PAX
298+ } else
299+ {
300+ int32_t i ;
301+ for (i = 0 ; i < olen ; i ++ )
302+ filedata [fpos ++ ];
303+ //printf("illegal olen.%u\n",olen);
304+ }
305+ }
306+ else if ( func == 'D' )
307+ {
308+ printf ("unexpected function D[%d]\n" ,ht );
309+ }
310+ else if ( func == 'V' )
311+ {
312+ int32_t numpvals ; uint32_t pvals [128 ];
313+ numpvals = filedata [fpos ++ ];
314+ if ( numpvals * sizeof (uint32_t ) <= sizeof (pvals ) && memread (pvals ,(int32_t )(sizeof (uint32_t )* numpvals ),filedata ,& fpos ,datalen ) == numpvals * sizeof (uint32_t ) )
315+ {
316+ //if ( matched != 0 ) global shared state -> global PVALS
317+ //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
318+ komodo_eventadd_pricefeed (sp ,symbol ,ht ,pvals ,numpvals );
319+ //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
320+ } else printf ("error loading pvals[%d]\n" ,numpvals );
321+ }
322+ else printf ("[%s] %s illegal func.(%d %c)\n" ,ASSETCHAINS_SYMBOL ,symbol ,func ,func );
323+ * fposp = fpos ;
324+ return (func );
325+ }
326+ return (-1 );
327+ }
328+
196329void komodo_stateupdate (int32_t height ,uint8_t notarypubs [][33 ],uint8_t numnotaries ,uint8_t notaryid ,uint256 txhash ,uint64_t voutmask ,uint8_t numvouts ,uint32_t * pvals ,uint8_t numpvals ,int32_t KMDheight ,uint32_t KMDtimestamp ,uint64_t opretvalue ,uint8_t * opretbuf ,uint16_t opretlen ,uint16_t vout )
197330{
198331 static FILE * fp ; static int32_t errs ,didinit ;
199- struct komodo_state * sp ; char fname [512 ],symbol [16 ],dest [16 ]; int32_t ht ,func ; uint8_t num ,pubkeys [64 ][33 ];
332+ struct komodo_state * sp ; char fname [512 ],symbol [KOMODO_ASSETCHAIN_MAXLEN ],dest [KOMODO_ASSETCHAIN_MAXLEN ]; int32_t retval , ht ,func ; uint8_t num ,pubkeys [64 ][33 ];
200333 if ( didinit == 0 )
201334 {
202335 portable_mutex_init (& KOMODO_KV_mutex );
@@ -214,10 +347,15 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
214347 komodo_statefname (fname ,ASSETCHAINS_SYMBOL ,(char * )"komodostate" );
215348 if ( (fp = fopen (fname ,"rb+" )) != 0 )
216349 {
217- while ( komodo_parsestatefile (sp ,fp ,symbol ,dest ) >= 0 )
218- ;
350+ if ( (retval = komodo_faststateinit (sp ,fname ,symbol ,dest )) > 0 )
351+ fseek (fp ,0 ,SEEK_END );
352+ else
353+ {
354+ fprintf (stderr ,"komodo_faststateinit retval.%d\n" ,retval );
355+ while ( komodo_parsestatefile (sp ,fp ,symbol ,dest ) >= 0 )
356+ ;
357+ }
219358 } else fp = fopen (fname ,"wb+" );
220- printf ("fname.(%s) fpos.%ld\n" ,fname ,ftell (fp ));
221359 KOMODO_INITDONE = (uint32_t )time (NULL );
222360 }
223361 if ( height <= 0 )
@@ -338,7 +476,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
338476int32_t komodo_voutupdate (int32_t * isratificationp ,int32_t notaryid ,uint8_t * scriptbuf ,int32_t scriptlen ,int32_t height ,uint256 txhash ,int32_t i ,int32_t j ,uint64_t * voutmaskp ,int32_t * specialtxp ,int32_t * notarizedheightp ,uint64_t value ,int32_t notarized ,uint64_t signedmask )
339477{
340478 static uint256 zero ; static FILE * signedfp ;
341- int32_t opretlen ,nid ,k ,len = 0 ; uint256 kmdtxid ,desttxid ; uint8_t crypto777 [33 ]; struct komodo_state * sp ; char symbol [16 ],dest [16 ];
479+ int32_t opretlen ,nid ,k ,len = 0 ; uint256 kmdtxid ,desttxid ; uint8_t crypto777 [33 ]; struct komodo_state * sp ; char symbol [KOMODO_ASSETCHAIN_MAXLEN ],dest [KOMODO_ASSETCHAIN_MAXLEN ];
342480 if ( (sp = komodo_stateptr (symbol ,dest )) == 0 )
343481 return (-1 );
344482 if ( scriptlen == 35 && scriptbuf [0 ] == 33 && scriptbuf [34 ] == 0xac )
@@ -407,7 +545,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr
407545 komodo_stateupdate (height ,0 ,0 ,0 ,zero ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 );
408546 len += 4 ;
409547 if ( ASSETCHAINS_SYMBOL [0 ] != 0 )
410- printf ("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s) lens.(%d %d)\n" ,ASSETCHAINS_SYMBOL ,height ,* notarizedheightp ,ASSETCHAINS_SYMBOL [0 ]== 0 ?"KMD" :ASSETCHAINS_SYMBOL ,kmdtxid .ToString ().c_str (),ASSETCHAINS_SYMBOL [0 ]== 0 ?"BTC" :"KMD" ,desttxid .ToString ().c_str (),( char * ) & scriptbuf [ len ] ,opretlen ,len );
548+ printf ("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d)\n" ,ASSETCHAINS_SYMBOL ,height ,* notarizedheightp ,ASSETCHAINS_SYMBOL [0 ]== 0 ?"KMD" :ASSETCHAINS_SYMBOL ,kmdtxid .ToString ().c_str (),ASSETCHAINS_SYMBOL [0 ]== 0 ?"BTC" :"KMD" ,desttxid .ToString ().c_str (),opretlen ,len );
411549 if ( ASSETCHAINS_SYMBOL [0 ] == 0 )
412550 {
413551 if ( signedfp == 0 )
@@ -496,7 +634,7 @@ int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys
496634void komodo_connectblock (CBlockIndex * pindex ,CBlock & block )
497635{
498636 static int32_t hwmheight ;
499- uint64_t signedmask ,voutmask ; char symbol [16 ],dest [16 ]; struct komodo_state * sp ;
637+ uint64_t signedmask ,voutmask ; char symbol [KOMODO_ASSETCHAIN_MAXLEN ],dest [KOMODO_ASSETCHAIN_MAXLEN ]; struct komodo_state * sp ;
500638 uint8_t scriptbuf [4096 ],pubkeys [64 ][33 ],rmd160 [20 ],scriptPubKey [35 ]; uint256 kmdtxid ,zero ,btctxid ,txhash ;
501639 int32_t i ,j ,k ,numnotaries ,notarized ,scriptlen ,isratification ,nid ,numvalid ,specialtx ,notarizedheight ,notaryid ,len ,numvouts ,numvins ,height ,txn_count ;
502640 memset (& zero ,0 ,sizeof (zero ));
0 commit comments