Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
391ab7131a
  1. 175
      iguana/iguana_instantdex.c
  2. 2
      iguana/iguana_msg.c
  3. 8
      iguana/peggy_update.c

175
iguana/iguana_instantdex.c

@ -15,6 +15,39 @@
#include "exchanges777.h" #include "exchanges777.h"
#define INSTANTDEX_HOPS 3
#define INSTANTDEX_DURATION 60
#define INSTANTDEX_PROPOSE 1
#define INSTANTDEX_ACCEPT 2
#define INSTANTDEX_CONFIRM 3
cJSON *InstantDEX_argjson(char *reference,char *message,bits256 basetxid,bits256 reltxid,int32_t iter,int32_t val,int32_t val2)
{
cJSON *argjson = cJSON_CreateObject();
if ( reference != 0 )
jaddstr(argjson,"refstr",reference);
if ( message != 0 && message[0] != 0 )
jaddstr(argjson,"message",message);
jaddbits256(argjson,"basetxid",basetxid);
jaddbits256(argjson,"reltxid",reltxid);
if ( iter != 3 )
{
if ( val == 0 )
val = INSTANTDEX_DURATION;
jaddnum(argjson,"duration",val);
jaddnum(argjson,"flags",val2);
}
else
{
if ( val > 0 )
jaddnum(argjson,"baseheight",val);
if ( val2 > 0 )
jaddnum(argjson,"relheight",val2);
}
return(argjson);
}
int32_t instantdex_rwdata(int32_t rwflag,uint64_t cmdbits,uint8_t *data,int32_t datalen) int32_t instantdex_rwdata(int32_t rwflag,uint64_t cmdbits,uint8_t *data,int32_t datalen)
{ {
// need to inplace serialize/deserialize here // need to inplace serialize/deserialize here
@ -168,9 +201,11 @@ double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *s
return(0); return(0);
} }
char *instantdex_request(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) char *instantdex_request(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen)
{ {
char *base,*rel,*request; double volume,aveprice,totalvol; int32_t num,depth; struct exchange_quote sortbuf[1000]; char *base,*rel,*request,*refstr,*nextcmdstr,*message;; double volume,price,aveprice,totalvol;
cJSON *newjson; int32_t duration,flags,nextcmd; int32_t num,depth;
struct exchange_quote sortbuf[1000]; bits256 basetxid,reltxid;
if ( argjson != 0 ) if ( argjson != 0 )
{ {
num = 0; num = 0;
@ -178,44 +213,61 @@ char *instantdex_request(struct supernet_info *myinfo,struct instantdex_msghdr *
request = jstr(argjson,"request"); request = jstr(argjson,"request");
base = jstr(argjson,"base"); base = jstr(argjson,"base");
rel = jstr(argjson,"rel"); rel = jstr(argjson,"rel");
refstr = jstr(argjson,"refstr");
volume = jdouble(argjson,"volume"); volume = jdouble(argjson,"volume");
aveprice = instantdex_aveprice(myinfo,sortbuf,(int32_t)(sizeof(sortbuf)/sizeof(*sortbuf)),&totalvol,base,rel,volume,argjson); duration = juint(argjson,"duration");
printf("aveprice %.8f vol %f\n",aveprice,volume); flags = juint(argjson,"flags");
return(clonestr("{\"result\":\"request calculated aveprice\"}")); nextcmd = 0;
} else return(clonestr("{\"error\":\"request needs argjson\"}")); nextcmdstr = message = "";
} if ( strcmp(cmdstr,"request") == 0 )
{
char *instantdex_proposal(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) aveprice = instantdex_aveprice(myinfo,sortbuf,(int32_t)(sizeof(sortbuf)/sizeof(*sortbuf)),&totalvol,base,rel,volume,argjson);
{ printf("aveprice %.8f vol %f\n",aveprice,totalvol);
if ( argjson != 0 ) OS_randombytes(basetxid.bytes,sizeof(basetxid));
{ OS_randombytes(reltxid.bytes,sizeof(reltxid));
return(clonestr("{\"result\":\"proposal ignored\"}")); nextcmd = INSTANTDEX_PROPOSE;
return(clonestr("{\"result\":\"proposal accepted\"}")); nextcmdstr = "proposal";
} else return(clonestr("{\"error\":\"response needs argjson\"}")); message = "hello";
} price = aveprice;
volume = totalvol;
char *instantdex_accepted(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) }
{ else
if ( argjson != 0 ) {
{ basetxid = jbits256(argjson,"basetxid");
return(clonestr("{\"result\":\"proposal was accepted, confirmation sent\"}")); reltxid = jbits256(argjson,"reltxid");
} else return(clonestr("{\"error\":\"response needs argjson\"}")); if ( strcmp(cmdstr,"proposal") == 0 )
} {
nextcmd = INSTANTDEX_ACCEPT;
char *instantdex_confirmed(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) nextcmdstr = "accept";
{ message = "world";
if ( argjson != 0 ) }
{ else if ( strcmp(cmdstr,"accept") == 0 )
return(clonestr("{\"result\":\"proposal was confirmed\"}")); {
} else return(clonestr("{\"error\":\"response needs argjson\"}")); nextcmd = INSTANTDEX_CONFIRM;
nextcmdstr = "confirm";
message = "confirmed";
}
else if ( strcmp(cmdstr,"confirm") == 0 )
{
return(clonestr("{\"error\":\"trade confirmed\"}"));
}
}
if ( (newjson= InstantDEX_argjson(refstr,message,basetxid,reltxid,nextcmd,duration,flags)) != 0 )
{
jaddnum(newjson,"price",price);
jaddnum(newjson,"volume",volume);
return(instantdex_sendcmd(myinfo,newjson,nextcmdstr,myinfo->ipaddr,INSTANTDEX_HOPS));
}
}
return(clonestr("{\"error\":\"request needs argjson\"}"));
} }
char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen)
{ {
static struct { char *cmdstr; char *(*func)(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen); uint64_t cmdbits; } cmds[] = static struct { char *cmdstr; char *(*func)(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen); uint64_t cmdbits; } cmds[] =
{ {
{ "request", instantdex_request }, { "proposal", instantdex_proposal }, { "request", instantdex_request }, { "proposal", instantdex_request },
{ "accepted", instantdex_accepted }, { "confirmed", instantdex_confirmed }, { "accepted", instantdex_request }, { "confirmed", instantdex_request },
}; };
char *retstr = 0; int32_t i; uint64_t cmdbits; char *retstr = 0; int32_t i; uint64_t cmdbits;
if ( cmds[0].cmdbits == 0 ) if ( cmds[0].cmdbits == 0 )
@ -226,15 +278,13 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
cmdbits = stringbits(msg->cmd); cmdbits = stringbits(msg->cmd);
for (i=0; i<sizeof(cmds)/sizeof(*cmds); i++) for (i=0; i<sizeof(cmds)/sizeof(*cmds); i++)
{ {
printf("%llx vs ",(long long)cmds[i].cmdbits);
if ( cmds[i].cmdbits == cmdbits ) if ( cmds[i].cmdbits == cmdbits )
{ {
printf("parsed.(%s)\n",cmds[i].cmdstr); printf("parsed.(%s)\n",cmds[i].cmdstr);
retstr = (*cmds[i].func)(myinfo,msg,argjson,remoteaddr,signerbits,data,datalen); retstr = (*cmds[i].func)(myinfo,cmds[i].cmdstr,msg,argjson,remoteaddr,signerbits,data,datalen);
break; break;
} }
} }
printf("%llx (%s)\n",(long long)cmdbits,msg->cmd);
return(retstr); return(retstr);
} }
@ -289,11 +339,11 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
THREE_STRINGS_AND_DOUBLE(InstantDEX,request,reference,base,rel,volume) THREE_STRINGS_AND_DOUBLE(InstantDEX,request,reference,base,rel,volume)
{ {
int32_t hops = 3; cJSON *argjson; int32_t hops = INSTANTDEX_HOPS; cJSON *argjson;
if ( remoteaddr == 0 ) if ( remoteaddr == 0 )
{ {
argjson = cJSON_CreateObject(); argjson = cJSON_CreateObject();
jaddstr(argjson,"ref",reference); jaddstr(argjson,"refstr",reference);
jaddstr(argjson,"base",base); jaddstr(argjson,"base",base);
jaddstr(argjson,"rel",rel); jaddstr(argjson,"rel",rel);
jaddnum(argjson,"volume",volume); jaddnum(argjson,"volume",volume);
@ -301,62 +351,33 @@ THREE_STRINGS_AND_DOUBLE(InstantDEX,request,reference,base,rel,volume)
} else return(clonestr("{\"error\":\"InstantDEX API request only local usage!\"}")); } else return(clonestr("{\"error\":\"InstantDEX API request only local usage!\"}"));
} }
cJSON *InstantDEX_argjson(char *reference,char *message,bits256 basetxid,bits256 reltxid,int32_t iter,int32_t val,int32_t val2)
{
cJSON *argjson = cJSON_CreateObject();
jaddstr(argjson,"ref",reference);
if ( message != 0 && message[0] != 0 )
jaddstr(argjson,"message",message);
jaddbits256(argjson,"basetxid",basetxid);
jaddbits256(argjson,"reltxid",reltxid);
if ( iter != 3 )
{
jaddnum(argjson,"duration",val);
jaddnum(argjson,"flags",val2);
}
else
{
if ( val > 0 )
jaddnum(argjson,"baseheight",val);
if ( val2 > 0 )
jaddnum(argjson,"relheight",val2);
}
return(argjson);
}
TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,proposal,reference,message,basetxid,reltxid,duration,flags) TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,proposal,reference,message,basetxid,reltxid,duration,flags)
{ {
int32_t hops = 3; cJSON *argjson; char *retstr; int32_t hops = INSTANTDEX_HOPS; cJSON *argjson;
if ( remoteaddr == 0 ) if ( remoteaddr == 0 )
{ {
argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,1,duration,flags); argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,INSTANTDEX_PROPOSE,duration,flags);
retstr = instantdex_sendcmd(myinfo,argjson,"proposal",myinfo->ipaddr,hops); return(instantdex_sendcmd(myinfo,argjson,"proposal",myinfo->ipaddr,hops));
free_json(argjson);
return(retstr);
} else return(clonestr("{\"error\":\"InstantDEX API proposal only local usage!\"}")); } else return(clonestr("{\"error\":\"InstantDEX API proposal only local usage!\"}"));
} }
TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,accept,reference,message,basetxid,reltxid,duration,flags) TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,accept,reference,message,basetxid,reltxid,duration,flags)
{ {
int32_t hops = 3; cJSON *argjson; char *retstr; int32_t hops = INSTANTDEX_HOPS; cJSON *argjson;
if ( remoteaddr == 0 ) if ( remoteaddr == 0 )
{ {
argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,2,duration,flags); argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,INSTANTDEX_ACCEPT,duration,flags);
retstr = instantdex_sendcmd(myinfo,argjson,"accept",myinfo->ipaddr,hops); return(instantdex_sendcmd(myinfo,argjson,"accept",myinfo->ipaddr,hops));
free_json(argjson);
return(retstr);
} else return(clonestr("{\"error\":\"InstantDEX API accept only local usage!\"}")); } else return(clonestr("{\"error\":\"InstantDEX API accept only local usage!\"}"));
} }
TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,confirm,reference,message,basetxid,reltxid,baseheight,relheight) TWOSTRINGS_AND_TWOHASHES_AND_TWOINTS(InstantDEX,confirm,reference,message,basetxid,reltxid,baseheight,relheight)
{ {
int32_t hops = 3; cJSON *argjson; char *retstr; int32_t hops = INSTANTDEX_HOPS; cJSON *argjson;
if ( remoteaddr == 0 ) if ( remoteaddr == 0 )
{ {
argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,3,baseheight,relheight); argjson = InstantDEX_argjson(reference,message,basetxid,reltxid,INSTANTDEX_CONFIRM,baseheight,relheight);
retstr = instantdex_sendcmd(myinfo,argjson,"confirm",myinfo->ipaddr,hops); return(instantdex_sendcmd(myinfo,argjson,"confirm",myinfo->ipaddr,hops));
free_json(argjson);
return(retstr);
} else return(clonestr("{\"error\":\"InstantDEX API confirm only local usage!\"}")); } else return(clonestr("{\"error\":\"InstantDEX API confirm only local usage!\"}"));
} }

2
iguana/iguana_msg.c

@ -848,7 +848,7 @@ int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struc
myfree(txids,sizeof(*txids) * (x+1)); myfree(txids,sizeof(*txids) * (x+1));
if ( blockhashes != 0 ) if ( blockhashes != 0 )
myfree(blockhashes,sizeof(*blockhashes) * (x+1)); myfree(blockhashes,sizeof(*blockhashes) * (x+1));
printf("intvectors.%c recvlen.%d\n",intvectors,recvlen); //printf("intvectors.%c recvlen.%d\n",intvectors,recvlen);
} }
if ( len != recvlen && len != recvlen-1 && len != recvlen-2 ) if ( len != recvlen && len != recvlen-1 && len != recvlen-2 )
{ {

8
iguana/peggy_update.c

@ -676,7 +676,7 @@ void PAX_bidask(struct exchange_info *exchange,uint32_t *timestamps,double *bids
bids[contractnum] = bidasks[0].price; bids[contractnum] = bidasks[0].price;
asks[contractnum] = bidasks[1].price; asks[contractnum] = bidasks[1].price;
timestamps[contractnum] = bidasks[0].timestamp; timestamps[contractnum] = bidasks[0].timestamp;
//printf("(%d %.6f) ",contractnum,_pairaved(bids[contractnum],asks[contractnum])); printf("(%d %.6f) ",contractnum,_pairaved(bids[contractnum],asks[contractnum]));
} }
struct exchange_info *PAX_bidasks(char *exchangestr,uint32_t *timestamps,double *bids,double *asks) struct exchange_info *PAX_bidasks(char *exchangestr,uint32_t *timestamps,double *bids,double *asks)
@ -693,7 +693,7 @@ struct exchange_info *PAX_bidasks(char *exchangestr,uint32_t *timestamps,double
} }
} }
} else printf("cant find (%s) exchange\n",exchangestr); } else printf("cant find (%s) exchange\n",exchangestr);
//printf("%s\n",exchangestr); printf("%s\n",exchangestr);
return(exchange); return(exchange);
} }
@ -701,13 +701,15 @@ int32_t PAX_idle(struct peggy_info *PEGS,int32_t peggyflag,int32_t idlegap)
{ {
static double lastupdate,lastdayupdate; static int32_t didinit; static portable_mutex_t mutex; static double lastupdate,lastdayupdate; static int32_t didinit; static portable_mutex_t mutex;
struct exchange_info *exchange; struct exchange_quote bidasks[2]; double btcdbtc,btcusd; struct exchange_info *exchange; struct exchange_quote bidasks[2]; double btcdbtc,btcusd;
int32_t i,datenum,contractnum; struct PAX_data *dp = &PEGS->tmp; int32_t i,datenum,contractnum,seconds; struct tai t; struct PAX_data *dp = &PEGS->tmp;
*dp = PEGS->data; *dp = PEGS->data;
if ( didinit == 0 ) if ( didinit == 0 )
{ {
portable_mutex_init(&mutex); portable_mutex_init(&mutex);
//prices777_init(BUNDLE.jsonstr,peggyflag); //prices777_init(BUNDLE.jsonstr,peggyflag);
didinit = 1; didinit = 1;
datenum = OS_conv_unixtime(&t,&seconds,(uint32_t)time(NULL));
expand_datenum(PEGS->data.edate,datenum);
if ( peggyflag != 0 ) if ( peggyflag != 0 )
{ {
//int32_t opreturns_init(uint32_t blocknum,uint32_t blocktimestamp,char *path); //int32_t opreturns_init(uint32_t blocknum,uint32_t blocktimestamp,char *path);

Loading…
Cancel
Save