Browse Source

Stats

etomic
jl777 8 years ago
parent
commit
21814135c1
  1. 16
      basilisk/basilisk_swap.c
  2. 327
      iguana/exchanges/stats.c
  3. 1
      iguana/m_stats
  4. BIN
      iguana/stats

16
basilisk/basilisk_swap.c

@ -2404,7 +2404,7 @@ cJSON *swapjson(struct supernet_info *myinfo,struct basilisk_swap *swap)
void basilisk_psockinit(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t amlp) void basilisk_psockinit(struct supernet_info *myinfo,struct basilisk_swap *swap,int32_t amlp)
{ {
char keystr[64],databuf[1024],*retstr,*retstr2,*datastr,*pushaddr=0,*subaddr=0; cJSON *retjson,*addrjson; uint8_t data[512]; int32_t datalen,timeout,pushsock = -1,subsock = -1; char keystr[64],databuf[1024],pubkeystr[128],*retstr,*retstr2,*datastr,*pushaddr=0,*subaddr=0; cJSON *retjson,*addrjson; uint8_t data[512]; int32_t datalen,timeout,pushsock = -1,subsock = -1;
if ( swap->connected == 1 ) if ( swap->connected == 1 )
return; return;
if ( swap->pushsock < 0 && swap->subsock < 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) if ( swap->pushsock < 0 && swap->subsock < 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 )
@ -2466,7 +2466,8 @@ void basilisk_psockinit(struct supernet_info *myinfo,struct basilisk_swap *swap,
if ( nn_connect(subsock,subaddr) >= 0 ) if ( nn_connect(subsock,subaddr) >= 0 )
{ {
swap->connected = 1; swap->connected = 1;
sprintf((char *)data,"{\"push\":\"%s\",\"sub\":\"%s\",\"trade\":[\"%s\", %.8f, \"%s\", %.8f]}",pushaddr,subaddr,swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount)); init_hexbytes_noT(pubkeystr,myinfo->persistent_pubkey33,33);
sprintf((char *)data,"{\"push\":\"%s\",\"sub\":\"%s\",\"trade\":[\"%s\", %.8f, \"%s\", %.8f],\"pub\":\"%s\"}",pushaddr,subaddr,swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount),pubkeystr);
datalen = (int32_t)strlen((char *)data) + 1; datalen = (int32_t)strlen((char *)data) + 1;
printf("datalen.%d (%s)\n",datalen,(char *)data); printf("datalen.%d (%s)\n",datalen,(char *)data);
init_hexbytes_noT(databuf,data,datalen); init_hexbytes_noT(databuf,data,datalen);
@ -3690,12 +3691,13 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,int64_t *KMDtotals,int64_t
if ( bits256_nonz(txids[BASILISK_BOBPAYMENT]) != 0 ) if ( bits256_nonz(txids[BASILISK_BOBPAYMENT]) != 0 )
{ {
// alicespend // alicespend
//for (j=0; j<32; j++) for (j=0; j<32; j++)
// rev.bytes[j] = privAm.bytes[31 - j]; rev.bytes[j] = privAm.bytes[31 - j];
//revcalc_rmd160_sha256(secretAm,rev);//privAm); revcalc_rmd160_sha256(secretAm,rev);//privAm);
//vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev));
redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256);
len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen);
printf("alicespend len.%d redeemlen.%d\n",len,redeemlen);
if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend("alicespend",myinfo,bobcoin,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND])) != 0 ) if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend("alicespend",myinfo,bobcoin,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND])) != 0 )
printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]);
} }
@ -3979,7 +3981,7 @@ char *basilisk_swaplist(struct supernet_info *myinfo)
if ( (item= basilisk_remember(myinfo,KMDtotals,BTCtotals,requestid,quoteid)) != 0 ) if ( (item= basilisk_remember(myinfo,KMDtotals,BTCtotals,requestid,quoteid)) != 0 )
{ {
jaddi(array,item); jaddi(array,item);
if ( 0 && (status= jstr(item,"status")) != 0 && strcmp(status,"pending") == 0 ) if ( 1 && (status= jstr(item,"status")) != 0 && strcmp(status,"pending") == 0 )
break; break;
} }
} }

327
iguana/exchanges/stats.c

@ -0,0 +1,327 @@
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
//
// main.c
// stats
//
// Copyright © 2017 SuperNET. All rights reserved.
//
#include <stdio.h>
#include <stdint.h>
#include "OS_portable.h"
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define IGUANA_URL "http://127.0.0.1:7778"
#define STATS_DESTDIR "/var/www/html"
#define STATS_DEST "/var/www/html/DEXstats.json"
char CURRENCIES[][8] = { "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", // major currencies
"CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK", // end of currencies
};
char ASSETCHAINS_SYMBOL[16] = { "KV" };
struct komodo_state
{
bits256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
int32_t SAVEDHEIGHT,CURRENT_HEIGHT,NOTARIZED_HEIGHT;
uint32_t SAVEDTIMESTAMP;
uint64_t deposited,issued,withdrawn,approved,redeemed,shorted;
struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS;
struct komodo_event **Komodo_events; int32_t Komodo_numevents;
uint32_t RTbufs[64][3]; uint64_t RTmask;
};
struct komodo_state KOMODO_STATE;
void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value)
{
static bits256 zeroes;
uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,keyvalue[10000];
iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen);
iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize);
iguana_rwnum(0,&opretbuf[5],sizeof(height),&height);
iguana_rwnum(0,&opretbuf[9],sizeof(flags),&flags);
key = &opretbuf[13];
if ( keylen+13 > opretlen )
{
printf("komodo_kvupdate: keylen.%d + 13 > opretlen.%d\n",keylen,opretlen);
return;
}
valueptr = &key[keylen];
coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1);
if ( opretlen == coresize || opretlen == coresize+sizeof(bits256) || opretlen == coresize+2*sizeof(bits256) )
{
memset(&pubkey,0,sizeof(pubkey));
memset(&sig,0,sizeof(sig));
if ( (haspubkey= (opretlen >= coresize+sizeof(bits256))) != 0 )
{
for (i=0; i<32; i++)
((uint8_t *)&pubkey)[i] = opretbuf[coresize+i];
}
if ( (hassig= (opretlen == coresize+sizeof(bits256)*2)) != 0 )
{
for (i=0; i<32; i++)
((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(bits256)+i];
}
/*if ( (refvaluesize= komodo_kvsearch((bits256 *)&refpubkey,height,&flags,&kvheight,&keyvalue[keylen],key,keylen)) >= 0 )
{
if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 )
{
if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 )
{
//printf("komodo_kvsigverify error [%d]\n",coresize-13);
return;
}
}
}*/
for (i=0; i<keylen; i++)
putchar((char)key[i]);
printf(" -> ");
for (i=0; i<valuesize; i++)
putchar((char)valueptr[i]);
printf(" [%d]\n",valuesize);
}
}
void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t height,bits256 txid,uint64_t value,uint16_t vout,uint8_t *opretbuf,uint16_t opretlen)
{
if ( sp != 0 )
{
if ( opretbuf[0] == 'K' && opretlen != 40 )
{
komodo_kvupdate(opretbuf,opretlen,value);
}
}
}
void komodo_setkmdheight(struct komodo_state *sp,int32_t kmdheight,uint32_t timestamp)
{
if ( sp != 0 )
{
if ( kmdheight > sp->SAVEDHEIGHT )
{
sp->SAVEDHEIGHT = kmdheight;
sp->SAVEDTIMESTAMP = timestamp;
}
if ( kmdheight > sp->CURRENT_HEIGHT )
sp->CURRENT_HEIGHT = kmdheight;
}
}
void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight,uint32_t timestamp)
{
uint32_t buf[2];
if ( kmdheight > 0 )
{
buf[0] = (uint32_t)kmdheight;
buf[1] = timestamp;
//komodo_eventadd(sp,height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)buf,sizeof(buf));
if ( sp != 0 )
komodo_setkmdheight(sp,kmdheight,timestamp);
}
else
{
kmdheight = -kmdheight;
//komodo_eventadd(sp,height,symbol,KOMODO_EVENT_REWIND,(uint8_t *)&height,sizeof(height));
//if ( sp != 0 )
// komodo_event_rewind(sp,symbol,height);
}
}
int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
{
static int32_t errs;
int32_t func,ht,notarized_height,num,matched=0; bits256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
if ( (func= fgetc(fp)) != EOF )
{
if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
matched = 1;
else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
errs++;
//printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
if ( func == 'P' )
{
if ( (num= fgetc(fp)) <= 64 )
{
if ( fread(pubkeys,33,num,fp) != num )
errs++;
else
{
//printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
//if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
// komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
}
} else printf("illegal num.%d\n",num);
}
else if ( func == 'N' )
{
if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
errs++;
if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
errs++;
if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
errs++;
//if ( matched != 0 ) global independent states -> inside *sp
//komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
}
else if ( func == 'U' ) // deprecated
{
uint8_t n,nid; bits256 hash; uint64_t mask;
n = fgetc(fp);
nid = fgetc(fp);
//printf("U %d %d\n",n,nid);
if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
errs++;
if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
errs++;
//if ( matched != 0 )
// komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
}
else if ( func == 'K' )
{
int32_t kheight;
if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
errs++;
//if ( matched != 0 ) global independent states -> inside *sp
//printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
}
else if ( func == 'T' )
{
int32_t kheight,ktimestamp;
if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
errs++;
if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
errs++;
//if ( matched != 0 ) global independent states -> inside *sp
//printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
}
else if ( func == 'R' )
{
uint16_t olen,v; uint64_t ovalue; bits256 txid; uint8_t opret[16384];
if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
errs++;
if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
errs++;
if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
errs++;
if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
errs++;
if ( olen < sizeof(opret) )
{
if ( fread(opret,1,olen,fp) != olen )
errs++;
if ( 0 && matched != 0 )
{
int32_t i; for (i=0; i<olen; i++)
printf("%02x",opret[i]);
printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/SATOSHIDEN);
}
komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
} else
{
int32_t i;
for (i=0; i<olen; i++)
fgetc(fp);
//printf("illegal olen.%u\n",olen);
}
}
else if ( func == 'D' )
{
printf("unexpected function D[%d]\n",ht);
}
else if ( func == 'V' )
{
int32_t numpvals; uint32_t pvals[128];
numpvals = fgetc(fp);
if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
{
//if ( matched != 0 ) global shared state -> global PVALS
//printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
//komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
//printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
} else printf("error loading pvals[%d]\n",numpvals);
}
else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
return(func);
} else return(-1);
}
void stats_stateupdate(char *destdir,char *statefname,int32_t maxseconds)
{
static long lastpos;
char fname[512],symbol[64],base[64],dest[64]; int32_t n; FILE *fp; uint32_t starttime; struct komodo_state *sp;
starttime = (uint32_t)time(NULL);
strcpy(base,"KV");
strcpy(symbol,"KV");
strcpy(dest,"KMD");
sp = &KOMODO_STATE;
n = 0;
if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
{
fseek(fp,0,SEEK_END);
if ( ftell(fp) > lastpos )
{
fseek(fp,lastpos,SEEK_SET);
while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 1000 )
{
if ( n == 999 )
{
if ( time(NULL) < starttime+maxseconds )
n = 0;
else break;
}
n++;
}
lastpos = ftell(fp);
}
fclose(fp);
}
}
char *stats_update(char *destdir,char *statefname)
{
cJSON *retjson = cJSON_CreateArray();
stats_stateupdate(destdir,statefname,10);
return(jprint(retjson,1));
}
int main(int argc, const char * argv[])
{
FILE *fp; char *filestr,*statefname;
if ( argc < 2 )
statefname = "/root/.komodo/KV/komodostate";
else statefname = (char *)argv[1];
printf("DEX stats running\n");
while ( 1 )
{
if ( (filestr= stats_update(STATS_DEST,statefname)) != 0 )
{
printf("%u: %s\n",(uint32_t)time(NULL),filestr);
if ( (fp= fopen(STATS_DEST,"wb")) != 0 )
{
fwrite(filestr,1,strlen(filestr)+1,fp);
fclose(fp);
}
free(filestr);
}
sleep(60);
}
return 0;
}

1
iguana/m_stats

@ -0,0 +1 @@
gcc -o stats -I../crypto777 exchanges/stats.c ../crypto777/cJSON.c ../agents/libcrypto777.a -lcurl -lpthread -lm

BIN
iguana/stats

Binary file not shown.
Loading…
Cancel
Save