diff --git a/crypto777/nanosrc/aio/usock_posix.c b/crypto777/nanosrc/aio/usock_posix.c index 3989d3a22..2f85e559c 100755 --- a/crypto777/nanosrc/aio/usock_posix.c +++ b/crypto777/nanosrc/aio/usock_posix.c @@ -167,7 +167,7 @@ int nn_usock_start (struct nn_usock *self, int domain, int type, int protocol) s = socket (domain, type, protocol); if (nn_slow (s < 0)) return -errno; - //PNACL_msg("got socket s.%d\n",s); + //printf("got socket s.%d\n",s); nn_usock_init_from_fd (self, s); /* Start the state machine. */ @@ -286,14 +286,14 @@ int nn_usock_bind (struct nn_usock *self, const struct sockaddr *addr, /* Allow re-using the address. */ opt = 1; - PNACL_msg("call setsockopt %d SOL_SOCKET.%d SO_REUSEADDR.%d in nn_usock_bind\n",self->s,SOL_SOCKET,SO_REUSEADDR); + printf("call setsockopt %d SOL_SOCKET.%d SO_REUSEADDR.%d in nn_usock_bind\n",self->s,SOL_SOCKET,SO_REUSEADDR); rc = setsockopt (self->s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)); - PNACL_msg("called setsockopt in nn_usock_bind returns %d\n",rc); + printf("called setsockopt in nn_usock_bind returns %d\n",rc); // ignore SO_REUSEADDR failures //errno_assert (rc == 0); rc = bind (self->s, addr, (socklen_t) addrlen); - PNACL_msg("usock.%d -> bind rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno)); + printf("usock.%d -> bind rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno)); if (nn_slow (rc != 0)) return -errno; @@ -309,7 +309,7 @@ int nn_usock_listen (struct nn_usock *self, int backlog) /* Start listening for incoming connections. */ rc = listen (self->s, backlog); - //PNACL_msg("usock.%d -> listen rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno)); + printf("usock.%d -> listen rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno)); if (nn_slow (rc != 0)) return -errno; @@ -336,7 +336,7 @@ void nn_usock_accept (struct nn_usock *self, struct nn_usock *listener) #else s = accept (listener->s, NULL, NULL); #endif - //PNACL_msg("usock.%d -> accept errno.%d s.%d %s\n",self->s,errno,s,nn_strerror(errno)); + printf("usock.%d -> accept errno.%d s.%d %s\n",self->s,errno,s,nn_strerror(errno)); /* Immediate success. */ if (nn_fast (s >= 0)) { @@ -369,7 +369,7 @@ void nn_usock_accept (struct nn_usock *self, struct nn_usock *listener) and allow processing other events in the meantime */ if (nn_slow (errno != EAGAIN && errno != EWOULDBLOCK && errno != ECONNABORTED && errno != listener->errnum)) { - PNACL_msg("listen errno.%d\n",errno); + printf("listen errno.%d\n",errno); listener->errnum = errno; listener->state = NN_USOCK_STATE_ACCEPTING_ERROR; nn_fsm_raise (&listener->fsm, @@ -396,7 +396,7 @@ void nn_usock_connect (struct nn_usock *self, const struct sockaddr *addr, /* Do the connect itself. */ rc = connect(self->s,addr,(socklen_t)addrlen); - //PNACL_msg("usock.%d <- connect (%llx) rc.%d errno.%d %s\n",self->s,*(long long *)addr,rc,errno,nn_strerror(errno)); + printf("usock.%d <- connect (%llx) rc.%d errno.%d %s\n",self->s,*(long long *)addr,rc,errno,nn_strerror(errno)); /* Immediate success. */ if ( nn_fast(rc == 0) ) { @@ -407,7 +407,7 @@ void nn_usock_connect (struct nn_usock *self, const struct sockaddr *addr, if ( nn_slow(errno != EINPROGRESS) ) { self->errnum = errno; - PNACL_msg("error.%d not EINPROGRESS\n",errno); + printf("error.%d not EINPROGRESS\n",errno); nn_fsm_action (&self->fsm, NN_USOCK_ACTION_ERROR); return; } @@ -436,13 +436,13 @@ void nn_usock_send (struct nn_usock *self, const struct nn_iovec *iov, self->out.iov [out].iov_base = iov [i].iov_base; self->out.iov [out].iov_len = iov [i].iov_len; out++; - //PNACL_msg("{%d} ",(int)iov [i].iov_len); + printf("{%d} ",(int)iov [i].iov_len); } - //PNACL_msg("iov[%d]\n",out); self->out.hdr.msg_iovlen = out; /* Try to send the data immediately. */ rc = nn_usock_send_raw (self, &self->out.hdr); + printf("iov[%d] nn_usock_send_raw -> rc.%d\n",out,rc); /* Success. */ if (nn_fast (rc == 0)) { @@ -475,17 +475,17 @@ void nn_usock_recv (struct nn_usock *self, void *buf, size_t len, int *fd) rc = nn_usock_recv_raw (self, buf, &nbytes); if (nn_slow (rc < 0)) { errnum_assert (rc == -ECONNRESET, -rc); - //PNACL_msg("rc.%d vs ECONNRESET\n",rc,ECONNRESET); + //printf("rc.%d vs ECONNRESET\n",rc,ECONNRESET); nn_fsm_action (&self->fsm, NN_USOCK_ACTION_ERROR); return; } //int i; //for (i=0; i<16&&ifsm, &self->event_received, NN_USOCK_RECEIVED); return; } @@ -1024,19 +1024,19 @@ int32_t nn_getiovec_size(uint8_t *buf,int32_t maxlen,struct msghdr *hdr) if ( nn_slow(iov->iov_len == NN_MSG) ) { errno = EINVAL; - PNACL_msg("ERROR: iov->iov_len == NN_MSG\n"); + printf("ERROR: iov->iov_len == NN_MSG\n"); return(-1); } if ( nn_slow(!iov->iov_base && iov->iov_len) ) { errno = EFAULT; - PNACL_msg("ERROR: !iov->iov_base && iov->iov_len\n"); + printf("ERROR: !iov->iov_base && iov->iov_len\n"); return(-1); } if ( maxlen > 0 && nn_slow(size + iov->iov_len > maxlen) ) { errno = EINVAL; - PNACL_msg("ERROR: sz.%d + iov->iov_len.%d < maxlen.%d\n",(int32_t)size,(int32_t)iov->iov_len,maxlen); + printf("ERROR: sz.%d + iov->iov_len.%d < maxlen.%d\n",(int32_t)size,(int32_t)iov->iov_len,maxlen); return(-1); } if ( iov->iov_len > 0 ) @@ -1057,7 +1057,7 @@ ssize_t mysendmsg(int32_t usock,struct msghdr *hdr,int32_t flags) clen = hdr->msg_controllen; if ( hdr->msg_control == 0 ) clen = 0; - nn_assert(clen == 0); // no supporty control messagies + nn_assert(clen == 0); // no support control messagies if ( veclen > sizeof(_buf) ) // - clen - 5) ) buf = malloc(veclen);// + clen + 5); else buf = _buf; @@ -1072,10 +1072,10 @@ ssize_t mysendmsg(int32_t usock,struct msghdr *hdr,int32_t flags) if ( nn_getiovec_size(&buf[offset],veclen,hdr) == veclen ) { nbytes = send(usock,buf,offset + veclen,0); - //PNACL_msg(">>>>>>>>> send.[%d %d %d %d] (n.%d v.%d c.%d)-> usock.%d nbytes.%d\n",buf[offset],buf[offset+1],buf[offset+2],buf[offset+3],(int32_t)offset+veclen,veclen,clen,usock,(int32_t)nbytes); + printf(">>>>>>>>> send.[%d %d %d %d] (n.%d v.%d c.%d)-> usock.%d nbytes.%d\n",buf[offset],buf[offset+1],buf[offset+2],buf[offset+3],(int32_t)offset+veclen,veclen,clen,usock,(int32_t)nbytes); if ( nbytes != offset + veclen ) { - //PNACL_msg("nbytes.%d != offset.%d veclen.%d errno.%d usock.%d\n",(int32_t)nbytes,(int32_t)offset,veclen,errno,usock); + printf("nbytes.%d != offset.%d veclen.%d errno.%d usock.%d\n",(int32_t)nbytes,(int32_t)offset,veclen,errno,usock); } if ( nbytes >= offset ) nbytes -= offset; @@ -1083,19 +1083,19 @@ ssize_t mysendmsg(int32_t usock,struct msghdr *hdr,int32_t flags) else { err = -errno; - PNACL_msg("mysendmsg: unexpected nn_getiovec_size error %d\n",err); + printf("mysendmsg: unexpected nn_getiovec_size error %d\n",err); } if ( buf != _buf ) free(buf); if ( err != 0 ) { - PNACL_msg("nn_usock_send_raw errno.%d err.%d\n",errno,err); + printf("nn_usock_send_raw errno.%d err.%d\n",errno,err); return(-errno); } } else { - PNACL_msg("nn_usock_send_raw errno.%d invalid iovec size\n",errno); + printf("nn_usock_send_raw errno.%d invalid iovec size\n",errno); return(-errno); } return(nbytes); @@ -1107,32 +1107,32 @@ ssize_t myrecvmsg(int32_t usock,struct msghdr *hdr,int32_t flags,int32_t len) iov = hdr->msg_iov; /*if ( (n= (int32_t)recv(usock,lens,sizeof(lens),0)) != sizeof(lens) ) { - PNACL_msg("error getting veclen/clen n.%d vs %d from usock.%d\n",n,(int32_t)sizeof(lens),usock); + printf("error getting veclen/clen n.%d vs %d from usock.%d\n",n,(int32_t)sizeof(lens),usock); return(0); - } else PNACL_msg("GOT %d bytes from usock.%d\n",n,usock); + } else printf("GOT %d bytes from usock.%d\n",n,usock); offset = 0; veclen = lens[offset++]; veclen |= ((int32_t)lens[offset++] << 8); veclen |= ((int32_t)lens[offset++] << 16); clen = lens[offset++]; clen |= ((int32_t)lens[offset++] << 8); - PNACL_msg("veclen.%d clen.%d waiting in usock.%d\n",veclen,clen,usock); + printf("veclen.%d clen.%d waiting in usock.%d\n",veclen,clen,usock); if ( clen > 0 ) { if ( (cbytes= (int32_t)recv(usock,hdr->msg_control,clen,0)) != clen ) { - PNACL_msg("myrecvmsg: unexpected cbytes.%d vs clen.%d\n",cbytes,clen); + printf("myrecvmsg: unexpected cbytes.%d vs clen.%d\n",cbytes,clen); } } else cbytes = 0;*/ hdr->msg_controllen = 0; if ( (nbytes= (int32_t)recv(usock,iov->iov_base,len,0)) != len ) { - //PNACL_msg("myrecvmsg: partial nbytes.%d vs veclen.%d\n",(int32_t)nbytes,len); + //printf("myrecvmsg: partial nbytes.%d vs veclen.%d\n",(int32_t)nbytes,len); } - //PNACL_msg("GOT nbytes.%d of len.%d from usock.%d\n",(int32_t)nbytes,len,usock); + //printf("GOT nbytes.%d of len.%d from usock.%d\n",(int32_t)nbytes,len,usock); if ( 0 && nbytes > 0 ) { - PNACL_msg("got nbytes.%d from usock.%d [%d %d %d %d]\n",(int32_t)nbytes,usock,((uint8_t *)iov->iov_base)[0],((uint8_t *)iov->iov_base)[1],((uint8_t *)iov->iov_base)[2],((uint8_t *)iov->iov_base)[3]); + printf("got nbytes.%d from usock.%d [%d %d %d %d]\n",(int32_t)nbytes,usock,((uint8_t *)iov->iov_base)[0],((uint8_t *)iov->iov_base)[1],((uint8_t *)iov->iov_base)[2],((uint8_t *)iov->iov_base)[3]); } return(nbytes); } @@ -1147,7 +1147,7 @@ static int nn_usock_send_raw (struct nn_usock *self, struct msghdr *hdr) nbytes = sendmsg(self->s,hdr,MSG_NOSIGNAL); #else nbytes = sendmsg(self->s,hdr,0); - //PNACL_msg("sendmsg nbytes.%d\n",(int32_t)nbytes); + printf("nn_usock_send_raw nbytes.%d\n",(int32_t)nbytes); #endif #endif /* Handle errors. */ @@ -1202,13 +1202,13 @@ int32_t nn_process_cmsg(struct nn_usock *self,struct msghdr *hdr) memcpy(&retval,(int32_t *)CMSG_DATA(cmsg),sizeof(int32_t)); if ( self->in.pfd ) { - PNACL_msg("CMSG set self->in.pfd (%d)\n",retval); + printf("CMSG set self->in.pfd (%d)\n",retval); *self->in.pfd = retval; self->in.pfd = NULL; } else { - PNACL_msg("CMSG nn_closefd(%d)\n",retval); + printf("CMSG nn_closefd(%d)\n",retval); nn_closefd(retval); } break; @@ -1260,7 +1260,7 @@ static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len) if (!length) return 0; } -#ifdef NN_USE_MYMSG +#if NN_USE_MYMSG usebuf = (length >= NN_USOCK_BATCH_SIZE); #else usebuf = (length >= NN_USOCK_BATCH_SIZE); @@ -1289,7 +1289,7 @@ static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len) #if NN_USE_MYMSG nbytes = myrecvmsg(self->s,&hdr,0,(int32_t)iov.iov_len); - //PNACL_msg("got nbytes.%d from recvmsg errno.%d %s\n",(int32_t)nbytes,errno,nn_strerror(errno)); + printf("got nbytes.%d from recvmsg errno.%d %s\n",(int32_t)nbytes,errno,nn_strerror(errno)); #else nbytes = recvmsg (self->s, &hdr, 0); #endif @@ -1301,7 +1301,7 @@ static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len) nbytes = 0; else { - PNACL_msg("recvraw errno.%d %s\n",errno,nn_strerror(errno)); + printf("recvraw errno.%d %s\n",errno,nn_strerror(errno)); // If the peer closes the connection, return ECONNRESET errno_assert(errno == ECONNRESET || errno == ENOTCONN || errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH #if NN_USE_MYMSG @@ -1313,7 +1313,7 @@ static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len) } } else if ( hdr.msg_controllen > 0 ) nn_process_cmsg(self,&hdr); - //PNACL_msg("nbytes.%d length.%d *len %d\n",(int)nbytes,(int)length,(int)*len); + printf("nbytes.%d length.%d *len %d\n",(int)nbytes,(int)length,(int)*len); // If the data were received directly into the place we can return straight away if ( usebuf != 0 ) diff --git a/crypto777/nanosrc/aio/usock_posix.c_dev b/crypto777/nanosrc/aio/usock_posix.c_dev index e5b8a1c35..244b0753c 100755 --- a/crypto777/nanosrc/aio/usock_posix.c_dev +++ b/crypto777/nanosrc/aio/usock_posix.c_dev @@ -1000,7 +1000,7 @@ static int32_t nn_usock_send_raw(struct nn_usock *self,struct msghdr *hdr) #else nbytes = sendmsg(self->s,hdr,0); #endif - PostMessage("nn_usock_send_raw nbytes.%d for sock.%d\n",(int32_t)nbytes,self->s); + printf("nn_usock_send_raw nbytes.%d for sock.%d\n",(int32_t)nbytes,self->s); if ( nn_slow(nbytes < 0) ) // Handle errors { if ( nn_fast(errno == EAGAIN || errno == EWOULDBLOCK) ) diff --git a/crypto777/nanosrc/aio/worker_posix.c b/crypto777/nanosrc/aio/worker_posix.c index 8d3ed2a46..550aa31f4 100755 --- a/crypto777/nanosrc/aio/worker_posix.c +++ b/crypto777/nanosrc/aio/worker_posix.c @@ -173,9 +173,11 @@ static void nn_worker_routine (void *arg) struct nn_worker_task *task; struct nn_worker_fd *fd; struct nn_worker_timer *timer; - PNACL_msg("nn_worker_routine started\n"); + printf("nn_worker_routine started\n"); self = (struct nn_worker*) arg; +#ifndef FROM_JS while ( 1 ) // Infinite loop. It will be interrupted only when the object is shut down. +#endif { // Wait for new events and/or timeouts. rc = nn_poller_wait(&self->poller,nn_timerset_timeout (&self->timerset)); @@ -185,7 +187,7 @@ static void nn_worker_routine (void *arg) rc = nn_timerset_event(&self->timerset, &thndl); if ( rc == -EAGAIN ) break; - PNACL_msg("nn_worker process expired user\n"); + printf("nn_worker process expired user\n"); errnum_assert(rc == 0, -rc); timer = nn_cont(thndl, struct nn_worker_timer, hndl); nn_ctx_enter(timer->owner->ctx); @@ -197,7 +199,7 @@ static void nn_worker_routine (void *arg) rc = nn_poller_event(&self->poller,&pevent,&phndl); // Get next poller event, such as IN or OUT if ( nn_slow(rc == -EAGAIN) ) break; - PNACL_msg("nn_worker process all events from the poller\n"); + printf("nn_worker process all events from the poller\n"); if ( phndl == &self->efd_hndl ) // If there are any new incoming worker tasks, process them { nn_assert (pevent == NN_POLLER_IN); @@ -212,31 +214,36 @@ static void nn_worker_routine (void *arg) item = nn_queue_pop(&tasks); // Next worker task if ( nn_slow(!item) ) break; - PNACL_msg("nn_worker next worker task\n"); + printf("nn_worker next worker task\n"); if ( nn_slow(item == &self->stop) ) // If the worker thread is asked to stop, do so { nn_queue_term(&tasks); return; } // It's a user-defined task. Notify the user that it has arrived in the worker thread - PNACL_msg("nn_worker user defined task\n"); + printf("nn_worker user defined task\n"); task = nn_cont(item,struct nn_worker_task,item); nn_ctx_enter(task->owner->ctx); nn_fsm_feed(task->owner,task->src,NN_WORKER_TASK_EXECUTE,task); nn_ctx_leave (task->owner->ctx); } nn_queue_term (&tasks); +#ifdef FROM_JS + printf("done worker ITER\n"); + return; +#else continue; +#endif } - PNACL_msg("nn_worker true i/o, invoke handler\n"); + printf("nn_worker true i/o, invoke handler\n"); fd = nn_cont(phndl,struct nn_worker_fd,hndl); // It's a true I/O event. Invoke the handler - PNACL_msg("nn_worker true i/o, fd.%p\n",fd); + printf("nn_worker true i/o, fd.%p\n",fd); nn_ctx_enter(fd->owner->ctx); - PNACL_msg("nn_worker true i/o, after nn_ctx_enter\n"); + printf("nn_worker true i/o, after nn_ctx_enter\n"); nn_fsm_feed(fd->owner,fd->src,pevent,fd); - PNACL_msg("nn_worker true i/o, after nn_fsm_feed leave.%p\n",fd->owner->ctx); + printf("nn_worker true i/o, after nn_fsm_feed leave.%p\n",fd->owner->ctx); nn_ctx_leave(fd->owner->ctx); - PNACL_msg("nn_worker true i/o, after nn_ctx_leave\n"); + printf("nn_worker true i/o, after nn_ctx_leave\n"); } } } diff --git a/crypto777/nanosrc/core/global.c b/crypto777/nanosrc/core/global.c index 3c6f07011..83b8e63d0 100755 --- a/crypto777/nanosrc/core/global.c +++ b/crypto777/nanosrc/core/global.c @@ -668,7 +668,7 @@ int32_t nn_recv(int32_t s,void *buf,size_t len,int32_t flags) return nn_recvmsg(s,&hdr,flags); } -#ifdef NN_USE_MYMSG +#if NN_USE_MYMSG int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags) { @@ -1253,7 +1253,7 @@ static int nn_global_create_ep (int s, const char *addr, int bind) return -EINVAL; protosz = delim - addr; addr += protosz + 3; -#ifdef NN_USE_MYMSG +#if NN_USE_MYMSG if ( strncmp("inproc",proto,strlen("inproc")) != 0 && strncmp("ipc",proto,strlen("ipc")) != 0 && strncmp("tcp",proto,strlen("tcp")) && strncmp("ws",proto,strlen("ws")) != 0 ) { PNACL_msg("only ipc, inproc, ws and tcp transport is supported\n"); @@ -1276,7 +1276,10 @@ static int nn_global_create_ep (int s, const char *addr, int bind) tp = NULL; } if ( !tp ) // The protocol specified doesn't match any known protocol + { + printf("unknown protocol\n"); return -EPROTONOSUPPORT; + } rc = nn_sock_add_ep (SELF.socks [s], tp, bind, addr); // Ask the socket to create the endpoint return rc; } diff --git a/crypto777/nanosrc/core/pipe.c b/crypto777/nanosrc/core/pipe.c index a025d1f00..1b1cd1dc6 100755 --- a/crypto777/nanosrc/core/pipe.c +++ b/crypto777/nanosrc/core/pipe.c @@ -176,6 +176,7 @@ int nn_pipe_send(struct nn_pipe *self,struct nn_msg *msg) int rc; struct nn_pipebase *pipebase; pipebase = (struct nn_pipebase *)self; + printf("pipesend\n"); nn_assert (pipebase->outstate == NN_PIPEBASE_OUTSTATE_IDLE); pipebase->outstate = NN_PIPEBASE_OUTSTATE_SENDING; rc = pipebase->vfptr->send(pipebase,msg); diff --git a/crypto777/nanosrc/core/sock.c b/crypto777/nanosrc/core/sock.c index 83e8afce7..ee95034a8 100755 --- a/crypto777/nanosrc/core/sock.c +++ b/crypto777/nanosrc/core/sock.c @@ -477,6 +477,7 @@ struct nn_ep *nn_find_ep(struct nn_sock *self,int32_t eid,const char *addr,struc int nn_sock_add_ep(struct nn_sock *self,struct nn_transport *transport,int32_t bind,const char *addr) { int rc,eid; struct nn_ep *ep; + printf("nn_sock_add_ep\n"); nn_ctx_enter (&self->ctx); if ( (ep= nn_find_ep(self,0,addr,transport,bind)) == NULL ) // The endpoint doesn't exist { @@ -484,16 +485,17 @@ int nn_sock_add_ep(struct nn_sock *self,struct nn_transport *transport,int32_t b rc = nn_ep_init(ep,NN_SOCK_SRC_EP,self,self->eid,transport,bind,addr); if ( nn_slow(rc < 0) ) { + printf("nn_sock_add_ep nn_ep_init rc.%d\n",rc); nn_free(ep); nn_ctx_leave(&self->ctx); return rc; } nn_ep_start(ep); - PNACL_msg("ep sock.(%s) started %s://(%s) bind.%d\n",self->socket_name,transport->name,addr,bind); + printf("ep sock.(%s) started %s://(%s) bind.%d\n",self->socket_name,transport->name,addr,bind); eid = self->eid++; // Increase the endpoint ID for the next endpoint nn_list_insert(&self->eps,&ep->item,nn_list_end(&self->eps)); // Add to the list of active endpoints nn_ctx_leave (&self->ctx); - } else PNACL_msg("self->sock.(%s) %p already has (%s)\n",self->socket_name,self->sockbase->sock,addr); + } else printf("self->sock.(%s) %p already has (%s)\n",self->socket_name,self->sockbase->sock,addr); return(ep->eid); } @@ -548,6 +550,7 @@ int nn_sock_send(struct nn_sock *self, struct nn_msg *msg, int flags) /* Try to send the message in a non-blocking way. */ rc = self->sockbase->vfptr->send (self->sockbase, msg); +printf("sockbase send rc.%d\n",rc); if (nn_fast (rc == 0)) { nn_ctx_leave (&self->ctx); return 0; @@ -560,8 +563,7 @@ int nn_sock_send(struct nn_sock *self, struct nn_msg *msg, int flags) return rc; } - /* If the message cannot be sent at the moment and the send call - is non-blocking, return immediately. */ + // If the message cannot be sent at the moment and the send call is non-blocking, return immediately. if (nn_fast (flags & NN_DONTWAIT)) { nn_ctx_leave (&self->ctx); return -EAGAIN; @@ -673,6 +675,7 @@ int nn_sock_add(struct nn_sock *self, struct nn_pipe *pipe) { int rc; rc = self->sockbase->vfptr->add(self->sockbase,pipe); + printf("nn_sock_add rc.%d\n",rc); if (nn_slow (rc >= 0)) { nn_sock_stat_increment (self, NN_STAT_CURRENT_CONNECTIONS, 1); } diff --git a/crypto777/nanosrc/protocols/pipeline/xpush.c b/crypto777/nanosrc/protocols/pipeline/xpush.c index 2d938bd1f..10c5cf480 100755 --- a/crypto777/nanosrc/protocols/pipeline/xpush.c +++ b/crypto777/nanosrc/protocols/pipeline/xpush.c @@ -115,7 +115,7 @@ static int nn_xpush_add (struct nn_sockbase *self, struct nn_pipe *pipe) alloc_assert (data); nn_pipe_setdata (pipe, data); nn_lb_add (&xpush->lb, &data->lb, pipe, sndprio); - + printf("nn_xpush_add\n"); return 0; } @@ -160,6 +160,7 @@ static int nn_xpush_events (struct nn_sockbase *self) static int nn_xpush_send (struct nn_sockbase *self, struct nn_msg *msg) { + printf("nn_xpush_send\n"); return nn_lb_send (&nn_cont (self, struct nn_xpush, sockbase)->lb, msg, NULL); } diff --git a/crypto777/nanosrc/protocols/reqrep/xreq.c b/crypto777/nanosrc/protocols/reqrep/xreq.c index 7b96ba744..7ab93d201 100755 --- a/crypto777/nanosrc/protocols/reqrep/xreq.c +++ b/crypto777/nanosrc/protocols/reqrep/xreq.c @@ -101,7 +101,7 @@ int nn_xreq_add(struct nn_sockbase *self, struct nn_pipe *pipe) nn_pipe_setdata(pipe,data); nn_lb_add(&xreq->lb,&data->lb,pipe,sndprio); nn_fq_add(&xreq->fq,&data->fq,pipe,rcvprio); - + printf("nn_xreq_add\n"); return 0; } diff --git a/crypto777/nanosrc/protocols/utils/lb.c b/crypto777/nanosrc/protocols/utils/lb.c index f0e2e88e3..cbff61f2f 100755 --- a/crypto777/nanosrc/protocols/utils/lb.c +++ b/crypto777/nanosrc/protocols/utils/lb.c @@ -37,8 +37,7 @@ void nn_lb_term (struct nn_lb *self) nn_priolist_term (&self->priolist); } -void nn_lb_add (struct nn_lb *self, struct nn_lb_data *data, - struct nn_pipe *pipe, int priority) +void nn_lb_add (struct nn_lb *self, struct nn_lb_data *data,struct nn_pipe *pipe, int priority) { nn_priolist_add (&self->priolist, &data->priodata, pipe, priority); } @@ -68,14 +67,16 @@ int nn_lb_send (struct nn_lb *self, struct nn_msg *msg, struct nn_pipe **to) { int rc; struct nn_pipe *pipe; - + printf("nn_lb_send\n"); /* Pipe is NULL only when there are no avialable pipes. */ pipe = nn_priolist_getpipe (&self->priolist); + printf("nn_lb_send pipe.%p\n",pipe); if (nn_slow (!pipe)) return -EAGAIN; /* Send the messsage. */ rc = nn_pipe_send (pipe, msg); + printf("nn_pipe_send rc.%d\n",rc); errnum_assert (rc >= 0, -rc); /* Move to the next pipe. */ diff --git a/crypto777/nanosrc/protocols/utils/priolist.c b/crypto777/nanosrc/protocols/utils/priolist.c index a72b5d535..5b6cb5401 100755 --- a/crypto777/nanosrc/protocols/utils/priolist.c +++ b/crypto777/nanosrc/protocols/utils/priolist.c @@ -54,7 +54,7 @@ void nn_priolist_add(struct nn_priolist *self,struct nn_priolist_data *data, str { data->pipe = pipe; data->priority = priority; - //printf("nn_priolist_add.%p data.%p priority.%d\n",self,data,priority); + printf("nn_priolist_add.%p data.%p priority.%d\n",self,data,priority); nn_list_item_init (&data->item); } @@ -139,7 +139,7 @@ struct nn_pipe *nn_priolist_getpipe(struct nn_priolist *self) //printf("nn_priolist_getpipe.%p -1 current it is\n",self); return NULL; } - //printf("nn_priolist_getpipe.%p current.%d slot.%p\n",self,self->current,self->slots[self->current - 1].current->pipe); + printf("nn_priolist_getpipe.%p current.%d slot.%p\n",self,self->current,self->slots[self->current - 1].current->pipe); return self->slots[self->current - 1].current->pipe; } diff --git a/crypto777/nanosrc/protocols/utils/priolist.h b/crypto777/nanosrc/protocols/utils/priolist.h index 9d4a9cad7..358faae79 100755 --- a/crypto777/nanosrc/protocols/utils/priolist.h +++ b/crypto777/nanosrc/protocols/utils/priolist.h @@ -86,8 +86,7 @@ void nn_priolist_activate (struct nn_priolist *self, struct nn_priolist_data *da 0 otherwise. */ int nn_priolist_is_active (struct nn_priolist *self); -/* Get the pointer to the current pipe. If there's no pipe in the list, - NULL is returned. */ +// Get the pointer to the current pipe. If there's no pipe in the list, NULL is returned. struct nn_pipe *nn_priolist_getpipe (struct nn_priolist *self); /* Moves to the next pipe in the list. If 'release' is set to 1, the current diff --git a/crypto777/nanosrc/transports/utils/tcpmux.c b/crypto777/nanosrc/transports/utils/tcpmux.c index b6cd7c24b..9f2cc3a60 100755 --- a/crypto777/nanosrc/transports/utils/tcpmux.c +++ b/crypto777/nanosrc/transports/utils/tcpmux.c @@ -83,7 +83,7 @@ int tcpmux_accept (int s) memset (&hdr, 0, sizeof (hdr)); hdr.msg_iov = &iov; hdr.msg_iovlen = 1; -#ifndef NN_USE_MYMSG +#if !NN_USE_MYMSG unsigned char buf [256]; hdr.msg_control = buf; hdr.msg_controllen = sizeof (buf); diff --git a/crypto777/nanosrc/transports/ws/cws.c b/crypto777/nanosrc/transports/ws/cws.c index 57e66c91a..905e90405 100755 --- a/crypto777/nanosrc/transports/ws/cws.c +++ b/crypto777/nanosrc/transports/ws/cws.c @@ -329,7 +329,7 @@ static void nn_cws_handler (struct nn_fsm *self, int src, int type, struct nn_cws *cws; cws = nn_cont (self, struct nn_cws, fsm); - + printf("cws_handler src.%d type.%d state.%d\n",src,type,cws->state); switch (cws->state) { /******************************************************************************/ @@ -409,6 +409,7 @@ static void nn_cws_handler (struct nn_fsm *self, int src, int type, case NN_CWS_SRC_USOCK: switch (type) { case NN_USOCK_CONNECTED: + printf("cws connected\n"); nn_sws_start (&cws->sws, &cws->usock, NN_WS_CLIENT, nn_chunkref_data (&cws->resource), nn_chunkref_data (&cws->remote_host)); @@ -421,6 +422,7 @@ static void nn_cws_handler (struct nn_fsm *self, int src, int type, nn_epbase_clear_error (&cws->epbase); return; case NN_USOCK_ERROR: + printf("cws NN_USOCK_ERROR\n"); nn_epbase_set_error (&cws->epbase,nn_usock_geterrno (&cws->usock),__FILE__,__LINE__); nn_usock_stop(&cws->usock); cws->state = NN_CWS_STATE_STOPPING_USOCK; diff --git a/crypto777/nanosrc/transports/ws/sws.c b/crypto777/nanosrc/transports/ws/sws.c index 45f5430cc..627c5590f 100755 --- a/crypto777/nanosrc/transports/ws/sws.c +++ b/crypto777/nanosrc/transports/ws/sws.c @@ -370,7 +370,7 @@ static int nn_sws_send (struct nn_pipebase *self, struct nn_msg *msg) struct nn_cmsghdr *cmsg; struct nn_msghdr msghdr; uint8_t rand_mask [NN_SWS_FRAME_SIZE_MASK]; - +printf("nn_sws_send\n"); sws = nn_cont (self, struct nn_sws, pipebase); nn_assert_state (sws, NN_SWS_STATE_ACTIVE); @@ -884,7 +884,7 @@ static void nn_sws_handler (struct nn_fsm *self, int src, int type, int rc; sws = nn_cont (self, struct nn_sws, fsm); - + printf("sws src.%d type.%d state.%d\n",src,type,sws->state); switch (sws->state) { /******************************************************************************/ diff --git a/crypto777/nanosrc/transports/ws/ws.c b/crypto777/nanosrc/transports/ws/ws.c index 0e91e9474..3a9288284 100755 --- a/crypto777/nanosrc/transports/ws/ws.c +++ b/crypto777/nanosrc/transports/ws/ws.c @@ -156,7 +156,7 @@ int nn_ws_send (int s, const void *msg, size_t len, uint8_t msg_type, int flags) struct nn_msghdr hdr; struct nn_cmsghdr *cmsg; size_t cmsgsz; - +printf("nn_ws_send\n"); iov.iov_base = (void*) msg; iov.iov_len = len; diff --git a/crypto777/nanosrc/utils/thread_posix.c b/crypto777/nanosrc/utils/thread_posix.c index 32c8428e4..df005eee3 100755 --- a/crypto777/nanosrc/utils/thread_posix.c +++ b/crypto777/nanosrc/utils/thread_posix.c @@ -26,11 +26,11 @@ #include #include -static void *nn_thread_main_routine(void *arg) +void *nn_thread_main_routine(void *arg) { struct nn_thread *self; self = (struct nn_thread *)arg; - PNACL_msg("nn_thread_main_routine arg.%p self->routine(%p) at %p\n",arg,self->arg,self->routine); + printf("nn_thread_main_routine arg.%p self->routine(%p) at %p\n",arg,self->arg,self->routine); self->routine(self->arg); // Run the thread routine return NULL; } @@ -42,16 +42,22 @@ void nn_thread_term(struct nn_thread *self) errnum_assert(rc == 0,rc); } +void *Nanomsg_threadarg; + #ifdef __PNACL void nn_thread_init(struct nn_thread *self,nn_thread_routine *routine,void *arg) { int32_t rc; // No signals should be processed by this thread. The library doesn't use signals and thus all the signals should be delivered to application threads, not to worker threads. - PNACL_msg("nn_thread_init routine.%p arg.%p\n",routine,arg); + printf("nn_thread_init routine.%p arg.%p\n",routine,arg); self->routine = routine; self->arg = arg; +#ifdef FROM_JS + Nanomsg_threadarg = self; +#else rc = pthread_create(&self->handle,NULL,nn_thread_main_routine,(void *)self); errnum_assert (rc == 0, rc); +#endif } #else diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 3ab402856..fb8e1f69e 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -2031,7 +2031,7 @@ int32_t bitcoin_addr2rmd160(uint8_t taddr,uint8_t *addrtypep,uint8_t rmd160[20], } for (i=0; i [timestamp, avebid, aveask, highbid, lowask]\n\ +pricearray(base, rel, starttime=0, endtime=-1, timescale=60) -> [timestamp, avebid, aveask, highbid, lowask]\n\ setprice(base, rel, price)\n\ autoprice(base, rel, minprice, margin, refbase, refrel, factor, offset)*\n\ goal(coin=*, val=)\n\ @@ -106,7 +106,7 @@ enable(coin)\n\ disable(coin)\n\ notarizations(coin)\n\ parselog()\n\ -statsdisp()\n\ +statsdisp(starttime=0, endtime=0)\n\ getrawtransaction(coin, txid)\n\ inventory(coin)\n\ bestfit(rel, relvolume)\n\ @@ -218,9 +218,15 @@ stop()\n\ return(LP_portfolio()); } else if ( strcmp(method,"parselog") == 0 ) - return(LP_statslog_parse()); + { + int32_t n = LP_statslog_parse(); + return(LP_statslog_disp(n,2000000000,2000000000)); + } else if ( strcmp(method,"statsdisp") == 0 ) - return(LP_statslog_disp(0)); + { + int32_t n = LP_statslog_parse(); + return(LP_statslog_disp(n,juint(argjson,"starttime"),juint(argjson,"endtime"))); + } else if ( strcmp(method,"secretaddresses") == 0 ) { uint8_t taddr,pubtype; @@ -253,7 +259,7 @@ stop()\n\ } else if ( strcmp(method,"pricearray") == 0 ) { - return(jprint(LP_pricearray(base,rel,juint(argjson,"firsttime"),juint(argjson,"lasttime"),jint(argjson,"timescale")),1)); + return(jprint(LP_pricearray(base,rel,juint(argjson,"starttime"),juint(argjson,"endtime"),jint(argjson,"timescale")),1)); } else if ( strcmp(method,"myprice") == 0 ) { diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 01872157f..965062623 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -290,7 +290,7 @@ struct basilisk_swap }; -#define LP_MAXPRICEINFOS 256 +#define LP_MAXPRICEINFOS 1024 struct LP_pubkeyinfo { UT_hash_handle hh; @@ -325,7 +325,7 @@ int64_t LP_komodo_interest(bits256 txid,int64_t value); void LP_availableset(struct LP_utxoinfo *utxo); int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2); int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock); -uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired); +uint16_t LP_psock_get(char *connectaddr,char *connectaddr2,char *publicaddr,int32_t ispaired); //void LP_utxo_clientpublish(struct LP_utxoinfo *utxo); int32_t LP_coinbus(uint16_t coin_busport); int32_t LP_nanomsg_recvs(void *ctx); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index aceba1ffd..f92770639 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -492,15 +492,18 @@ void LP_coinsloop(void *_coins) nonz = 0; HASH_ITER(hh,LP_coins,coin,ctmp) // firstrefht,firstscanht,lastscanht { - if ( coins[0] != 0 ) + if ( coins != 0 ) { - if ( strcmp(coins,coin->symbol) != 0 ) - continue; - } - else // avoid hardcode special case LP_coinsloop - { - if ( strcmp("BTC",coin->symbol) == 0 || strcmp("KMD",coin->symbol) == 0 ) - continue; + if ( coins[0] != 0 ) + { + if ( strcmp(coins,coin->symbol) != 0 ) + continue; + } + else // avoid hardcode special case LP_coinsloop + { + if ( strcmp("BTC",coin->symbol) == 0 || strcmp("KMD",coin->symbol) == 0 ) + continue; + } } memset(&zero,0,sizeof(zero)); if ( coin->inactive != 0 ) @@ -578,6 +581,8 @@ void LP_coinsloop(void *_coins) break; } } + if ( coins == 0 ) + return; if ( nonz == 0 ) usleep(1000); } @@ -793,7 +798,6 @@ void LP_reserved_msgs(void *ignore) int32_t LP_reserved_msg(char *base,char *rel,bits256 pubkey,char *msg) { int32_t n = 0; -#ifndef FROM_JS portable_mutex_lock(&LP_reservedmutex); if ( num_Reserved_msgs < sizeof(Reserved_msgs)/sizeof(*Reserved_msgs) ) { @@ -801,10 +805,6 @@ int32_t LP_reserved_msg(char *base,char *rel,bits256 pubkey,char *msg) n = num_Reserved_msgs; } else LP_broadcast_message(LP_mypubsock,base,rel,pubkey,msg); portable_mutex_unlock(&LP_reservedmutex); -#else - printf("reserved_msg.(%s)\n",msg); - LP_broadcast_message(LP_mypubsock,base,rel,pubkey,msg); -#endif if ( num_Reserved_msgs > max_Reserved_msgs ) { max_Reserved_msgs = num_Reserved_msgs; @@ -815,7 +815,7 @@ int32_t LP_reserved_msg(char *base,char *rel,bits256 pubkey,char *msg) void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybusport,char *passphrase,int32_t amclient,char *userhome,cJSON *argjson) { - char *myipaddr=0; long filesize,n; int32_t timeout,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128],bindaddr[128],*coins_str=0; cJSON *coinsjson=0; void *ctx = bitcoin_ctx(); + char *myipaddr=0; long filesize,n; int32_t valid,timeout,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],bindaddr2[128],subaddr[128],bindaddr[128],*coins_str=0; cJSON *coinsjson=0; void *ctx = bitcoin_ctx(); LP_showwif = juint(argjson,"wif"); if ( passphrase == 0 || passphrase[0] == 0 ) { @@ -899,9 +899,16 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu pubsock = -1; nanomsg_transportname(0,subaddr,myipaddr,mypubport); nanomsg_transportname(1,bindaddr,myipaddr,mypubport); + nanomsg_transportname2(1,bindaddr2,myipaddr,mypubport); + valid = 0; if ( (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 ) { + valid = 0; if ( nn_bind(pubsock,bindaddr) >= 0 ) + valid++; + if ( nn_bind(pubsock,bindaddr2) >= 0 ) + valid++; + if ( valid > 0 ) { timeout = 1; nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); @@ -913,7 +920,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu nn_close(pubsock), pubsock = -1; } } else printf("error getting pubsock %d\n",pubsock); - printf(">>>>>>>>> myipaddr.%s (%s) pullsock.%d\n",myipaddr,subaddr,pubsock); + printf(">>>>>>>>> myipaddr.(%s %s) (%s) pullsock.%d valid.%d\n",bindaddr,bindaddr2,subaddr,pubsock,valid); LP_mypubsock = pubsock; } printf("got %s, initpeers\n",myipaddr); @@ -1031,14 +1038,30 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu } #ifdef FROM_JS +extern void *Nanomsg_threadarg; +void *nn_thread_main_routine(void *arg); void emscripten_usleep(int32_t x) { } +char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout) +{ + static uint32_t counter; char fname[512],*retstr; long fsize; + if ( strncmp("http://",url,strlen("http://")) != 0 ) + return(clonestr("{\"error\":\"only http allowed\"}")); + sprintf(fname,"bitcoind_RPC/req.%u",counter); + counter++; + //printf("issue.(%s)\n",url); + emscripten_wget(url,fname); + retstr = OS_filestr(&fsize,fname); + //printf("bitcoind_RPC(%s) -> fname.(%s) %s\n",url,fname,retstr); + return(retstr); +} + void LP_fromjs_iter() { - static void *ctx; + static void *ctx; char *retstr; if ( G.initializing != 0 ) { printf("LP_fromjs_iter during G.initializing, skip\n"); @@ -1048,27 +1071,28 @@ void LP_fromjs_iter() ctx = bitcoin_ctx(); if ( 0 && (LP_counter % 100) == 0 ) printf("LP_fromjs_iter got called LP_counter.%d userpass.(%s) ctx.%p\n",LP_counter,G.USERPASS,ctx); - LP_pubkeys_query(); + //if ( Nanomsg_threadarg != 0 ) + // nn_thread_main_routine(Nanomsg_threadarg); + //LP_pubkeys_query(); LP_utxosQ_process(); LP_nanomsg_recvs(ctx); LP_mainloop_iter(ctx,LP_myipaddr,0,LP_mypubsock,LP_publicaddr,LP_RPCPORT); + queue_loop(0); + if ( (LP_counter % 10) == 0 ) + { + LP_coinsloop(0); + if ( (LP_counter % 100) == 0 ) + { + LP_notify_pubkeys(ctx,LP_mypubsock); + LP_privkey_updates(ctx,LP_mypubsock,0); + prices_loop(0); + if ( (retstr= basilisk_swapentry(0,0)) != 0 ) + free(retstr); + } + } LP_counter++; } -char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout) -{ - static uint32_t counter; char fname[512],*retstr; long fsize; - if ( strncmp("http://",url,strlen("http://")) != 0 ) - return(clonestr("{\"error\":\"only http allowed\"}")); - sprintf(fname,"bitcoind_RPC/req.%u",counter); - counter++; - //printf("issue.(%s)\n",url); - emscripten_wget(url,fname); - retstr = OS_filestr(&fsize,fname); - //printf("bitcoind_RPC(%s) -> fname.(%s) %s\n",url,fname,retstr); - return(retstr); -} - #endif diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index eeb23603f..723da5980 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -30,7 +30,13 @@ uint16_t Numpsocks,Psockport = MIN_PSOCK_PORT; char *nanomsg_transportname(int32_t bindflag,char *str,char *ipaddr,uint16_t port) { - sprintf(str,"ws://%s:%u",bindflag == 0 ? ipaddr : "*",port); // ws is worse + sprintf(str,"ws://%s:%u",bindflag == 0 ? ipaddr : "*",port); + return(str); +} + +char *nanomsg_transportname2(int32_t bindflag,char *str,char *ipaddr,uint16_t port) +{ + sprintf(str,"tcp://%s:%u",bindflag == 0 ? ipaddr : "*",port+10); return(str); } @@ -139,7 +145,7 @@ int32_t LP_peerindsock(int32_t *peerindp) return(-1); } -void queue_loop(void *ignore) +void queue_loop(void *arg) { struct LP_queue *ptr,*tmp; int32_t sentbytes,nonz,flag,duplicate,n=0; while ( 1 ) @@ -195,6 +201,8 @@ void queue_loop(void *ignore) ptr = 0; } } + if ( arg == 0 ) + break; //if ( n != 0 ) // printf("LP_Q.[%d]\n",n); if ( nonz == 0 ) @@ -652,7 +660,7 @@ char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired) return(retstr); } -uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired) +uint16_t LP_psock_get(char *connectaddr,char *connectaddr2,char *publicaddr,int32_t ispaired) { uint16_t publicport = 0; char *retstr,*addr; cJSON *retjson; struct LP_peerinfo *peer,*tmp; HASH_ITER(hh,LP_peerinfos,peer,tmp) @@ -667,6 +675,8 @@ uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired) safecopy(publicaddr,addr,128); if ( (addr= jstr(retjson,"connectaddr")) != 0 ) safecopy(connectaddr,addr,128); + if ( (addr= jstr(retjson,"connectaddr2")) != 0 ) + safecopy(connectaddr2,addr,128); if ( publicaddr[0] != 0 && connectaddr[0] != 0 ) publicport = juint(retjson,"publicport"); free_json(retjson); @@ -682,8 +692,9 @@ uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired) int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char *myipaddr,uint16_t mypullport,int32_t ispaired) { - int32_t nntype,pullsock,timeout; char bindaddr[128],connectaddr[128]; + int32_t nntype,pullsock,timeout; char bindaddr[128],bindaddr2[128],connectaddr[128],connectaddr2[128]; *mypullportp = mypullport; + connectaddr2[0] = 0; if ( ispaired == 0 ) { if ( LP_canbind != 0 ) @@ -694,6 +705,7 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char { nanomsg_transportname(0,publicaddr,myipaddr,mypullport); nanomsg_transportname(1,bindaddr,myipaddr,mypullport); + nanomsg_transportname2(1,bindaddr2,myipaddr,mypullport); } else { @@ -705,7 +717,7 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char } while ( *mypullportp == 0 ) { - if ( (*mypullportp= LP_psock_get(connectaddr,publicaddr,ispaired)) != 0 ) + if ( (*mypullportp= LP_psock_get(connectaddr,connectaddr2,publicaddr,ispaired)) != 0 ) break; sleep(10); printf("try to get publicaddr again\n"); @@ -721,7 +733,13 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char { printf("bind to %s error for %s: %s\n",connectaddr,publicaddr,nn_strerror(nn_errno())); exit(-1); - } else printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock); + } + else + { + if ( connectaddr2[0] != 0 && nn_connect(pullsock,connectaddr2) > 0 ) + printf("%s ",connectaddr2); + printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock); + } } else { @@ -730,6 +748,8 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char printf("bind to %s error for %s: %s\n",bindaddr,publicaddr,nn_strerror(nn_errno())); exit(-1); } + if ( nn_bind(pullsock,bindaddr2) >= 0 ) + printf("bound to %s\n",bindaddr2); } timeout = 1; nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); diff --git a/iguana/exchanges/LP_peers.c b/iguana/exchanges/LP_peers.c index 064086a03..87b03d2ad 100644 --- a/iguana/exchanges/LP_peers.c +++ b/iguana/exchanges/LP_peers.c @@ -56,7 +56,7 @@ char *LP_peers() struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,int32_t numpeers,int32_t numutxos,uint32_t sessionid) { - uint32_t ipbits; int32_t pushsock,subsock,timeout; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; + uint32_t ipbits; int32_t valid,pushsock,subsock,timeout; char checkip[64],subaddr2[64],pushaddr[64],pushaddr2[64],subaddr[64]; struct LP_peerinfo *peer = 0; printf("addpeer (%s:%u) pushport.%u subport.%u\n",ipaddr,port,pushport,subport); #ifdef LP_STRICTPEERS if ( strncmp("5.9.253",ipaddr,strlen("5.9.253")) != 0 ) @@ -90,13 +90,19 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { nanomsg_transportname(0,pushaddr,peer->ipaddr,pushport); + nanomsg_transportname(0,pushaddr2,peer->ipaddr,pushport); + valid = 0; if ( nn_connect(pushsock,pushaddr) >= 0 ) + valid++; + if ( nn_connect(pushsock,pushaddr2) >= 0 ) + valid++; + if ( valid > 0 ) { timeout = 1; nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); //maxsize = 2 * 1024 * 1024; //nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDBUF,&maxsize,sizeof(maxsize)); - printf("connected to push.(%s) %d\n",pushaddr,pushsock); + printf("connected to push.(%s %s) pushsock.%d valid.%d\n",pushaddr,pushaddr2,pushsock,valid); peer->connected = (uint32_t)time(NULL); peer->pushsock = pushsock; if ( (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) @@ -105,17 +111,30 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); nanomsg_transportname(0,subaddr,peer->ipaddr,subport); + nanomsg_transportname2(0,subaddr2,peer->ipaddr,subport); + valid = 0; if ( nn_connect(subsock,subaddr) >= 0 ) + valid++; + if ( nn_connect(subsock,subaddr2) >= 0 ) + valid++; + if ( valid > 0 ) { peer->subsock = subsock; - printf("connected to sub.(%s) %d\n",subaddr,peer->subsock); - } else nn_close(subsock); + printf("connected to sub.(%s %s) subsock.%d valid.%d\n",subaddr,subaddr2,peer->subsock,valid); + } + else + { + printf("error connecting to subsock.%d (%s %s)\n",subsock,subaddr,subaddr2); + nn_close(subsock); + subsock = -1; + } } } else { nn_close(pushsock); - printf("error connecting to push.(%s)\n",pushaddr); + pushsock = -1; + printf("error connecting to push.(%s %s)\n",pushaddr,pushaddr2); } } else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock); if ( peer->pushsock >= 0 && peer->subsock >= 0 ) diff --git a/iguana/exchanges/LP_portfolio.c b/iguana/exchanges/LP_portfolio.c index f3492daf6..dce7e3f99 100644 --- a/iguana/exchanges/LP_portfolio.c +++ b/iguana/exchanges/LP_portfolio.c @@ -595,13 +595,15 @@ int32_t LP_portfolio_order(struct LP_portfoliotrade *trades,int32_t max,cJSON *a return(n); } -void prices_loop(void *ignore) +void prices_loop(void *arg) { char *retstr; cJSON *retjson,*array; char *buycoin,*sellcoin; struct iguana_info *buy,*sell; uint32_t requestid,quoteid; int32_t i,n,m; struct LP_portfoliotrade trades[256]; struct LP_priceinfo *btcpp; void *ctx = bitcoin_ctx(); while ( 1 ) { if ( (btcpp= LP_priceinfofind("BTC")) == 0 ) { + if ( arg == 0 ) + return; sleep(60); continue; } @@ -635,6 +637,8 @@ void prices_loop(void *ignore) } free(retstr); } + if ( arg == 0 ) + return; sleep(60); } } diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 4b4113f42..68125fbfa 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -47,6 +47,13 @@ struct LP_cacheinfo uint32_t timestamp; } *LP_cacheinfos; +char *LP_priceinfostr(int32_t ind) +{ + if ( ind < 0 || ind >= LP_MAXPRICEINFOS ) + return("UNKNOWN"); + else return(LP_priceinfos[ind].symbol); +} + int32_t LP_pricevalid(double price) { if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN ) @@ -70,6 +77,14 @@ struct LP_priceinfo *LP_priceinfofind(char *symbol) return(0); } +int32_t LP_priceinfoind(char *symbol) +{ + struct LP_priceinfo *pp; + if ( (pp= LP_priceinfofind(symbol)) != 0 ) + return(pp->ind); + else return(-1); +} + struct LP_priceinfo *LP_priceinfoptr(int32_t *indp,char *base,char *rel) { struct LP_priceinfo *basepp,*relpp; @@ -685,7 +700,7 @@ void LP_pubkeys_query() reqjson = cJSON_CreateObject(); jaddstr(reqjson,"method","wantnotify"); jaddbits256(reqjson,"pub",pubp->pubkey); - printf("LP_pubkeys_query %s\n",jprint(reqjson,0)); + //printf("LP_pubkeys_query %s\n",jprint(reqjson,0)); LP_reserved_msg("","",zero,jprint(reqjson,1)); } } diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index a53c1ef6e..efbfa6438 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -271,10 +271,11 @@ bits256 LP_utxos_sighash(uint32_t timestamp,uint8_t *pubsecp,bits256 pubkey,bits int32_t LP_utxos_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits256 pubkey,bits256 utxoshash) { - static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; char str[65]; + static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; char str[65]; struct LP_pubkeyinfo *pubp; if ( ctx == 0 ) ctx = bitcoin_ctx(); - if ( sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 ) + pubp = LP_pubkeyfind(pubkey); + if ( (pubp == 0 || pubp->numerrors < LP_MAXPUBKEY_ERRORS) && sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 ) { decode_hex(sig,65,sigstr); decode_hex(pubsecp,33,pubsecpstr); @@ -284,7 +285,11 @@ int32_t LP_utxos_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits2 { static uint32_t counter; if ( counter++ < 100 ) - printf("LP_utxos_sigcheck failure, probably from %s with older version\n",bits256_str(str,pubkey)); + { + if ( pubp != 0 ) + pubp->numerrors++; + printf("LP_utxos_sigcheck failure.%d, probably from %s with older version\n",pubp!=0?pubp->numerrors:-1,bits256_str(str,pubkey)); + } retval = -1; } else retval = 0; } @@ -350,7 +355,11 @@ struct LP_utxos_qitem { struct queueitem DL; cJSON *argjson; }; char *LP_postutxos_recv(cJSON *argjson) { - struct LP_utxos_qitem *uitem; struct iguana_info *coin; char *coinaddr,*symbol; bits256 utxoshash; cJSON *obj; + struct LP_utxos_qitem *uitem; struct iguana_info *coin; char *coinaddr,*symbol; bits256 utxoshash,pubkey; cJSON *obj; struct LP_pubkeyinfo *pubp; + pubkey = jbits256(argjson,"pubkey"); + pubp = LP_pubkeyfind(pubkey); + if ( pubp->numerrors > LP_MAXPUBKEY_ERRORS ) + return(clonestr("{\"error\":\"blacklisted\"}")); if ( (coinaddr= jstr(argjson,"coinaddr")) != 0 && (symbol= jstr(argjson,"coin")) != 0 && (coin= LP_coinfind(symbol)) != 0 ) { if ( strcmp(coinaddr,coin->smartaddr) == 0 ) @@ -363,7 +372,7 @@ char *LP_postutxos_recv(cJSON *argjson) { utxoshash = LP_utxoshash_calc(obj); //char str[65]; printf("got utxoshash %s\n",bits256_str(str,utxoshash)); - if ( LP_utxos_sigcheck(juint(argjson,"timestamp"),jstr(argjson,"sig"),jstr(argjson,"pubsecp"),jbits256(argjson,"pubkey"),utxoshash) == 0 ) + if ( LP_utxos_sigcheck(juint(argjson,"timestamp"),jstr(argjson,"sig"),jstr(argjson,"pubsecp"),pubkey,utxoshash) == 0 ) { uitem = calloc(1,sizeof(*uitem)); uitem->argjson = jduplicate(argjson); @@ -399,10 +408,11 @@ int32_t LP_utxosQ_process() int32_t LP_price_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits256 pubkey,char *base,char *rel,uint64_t price64) { - static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; + static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; struct LP_pubkeyinfo *pubp; if ( ctx == 0 ) ctx = bitcoin_ctx(); - if ( sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 ) + pubp = LP_pubkeyfind(pubkey); + if ( (pubp == 0 || pubp->numerrors < LP_MAXPUBKEY_ERRORS) && sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 ) { decode_hex(sig,65,sigstr); decode_hex(pubsecp,33,pubsecpstr); @@ -410,6 +420,8 @@ int32_t LP_price_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits2 retval = bitcoin_recoververify(ctx,"price",sig,sighash,pub33,0); if ( memcmp(pub33,pubsecp,33) != 0 || retval != 0 ) { + if ( pubp != 0 ) + pubp->numerrors++; printf("LP_price_sigcheck failure\n"); retval = -1; } @@ -520,7 +532,7 @@ int32_t LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item) char str[65]; printf(" -> rmd160.(%s) for %s (%s) sig.%s\n",hexstr,bits256_str(str,pubp->pubkey),pubsecpstr,sigstr); retval = 0; pubp->timestamp = (uint32_t)time(NULL); - } //else printf("sig %s error pub33.%s\n",sigstr,pubsecpstr); + } else pubp->numerrors++; } } else @@ -622,7 +634,7 @@ char *LP_uitem_recv(cJSON *argjson) { //char str[65]; printf("uitem %s %s %s/v%d %.8f ht.%d\n",symbol,coinaddr,bits256_str(str,txid),vout,dstr(value),height); if ( strcmp(coin->smartaddr,coinaddr) != 0 ) - LP_address_utxoadd("LP_uitem,recv",coin,coinaddr,txid,vout,value,height,-1); + LP_address_utxoadd("LP_uitem_recv",coin,coinaddr,txid,vout,value,height,-1); //else printf("ignore external uitem %s %s\n",symbol,coin->smartaddr); } return(clonestr("{\"result\":\"success\"}")); diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index b84c79843..61396e280 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -2305,7 +2305,7 @@ struct LP_utxoinfo *LP_bestutxo(double *ordermatchpricep,int64_t *bestsatoshisp, { numrestraints++; butxo->T.bestflag = 0; - pubp->numerrors = 0; + //pubp->numerrors = 0; } } } diff --git a/iguana/exchanges/LP_stats.c b/iguana/exchanges/LP_stats.c index f5b244822..89919ef3f 100644 --- a/iguana/exchanges/LP_stats.c +++ b/iguana/exchanges/LP_stats.c @@ -20,6 +20,22 @@ #define LP_STATSLOG_FNAME "stats.log" +struct LP_swapstats +{ + UT_hash_handle hh; + struct LP_quoteinfo Q; + bits256 bobdeposit,alicepayment,bobpayment,paymentspent,Apaymentspent,depositspent; + double qprice; + uint64_t aliceid; + uint32_t ind,methodind,finished,expired; + char alicegui[32],bobgui[32]; +} *LP_swapstats; +int32_t LP_statslog_parsequote(char *method,cJSON *lineobj); + +char *LP_stats_methods[] = { "unknown", "request", "reserved", "connect", "connected", "tradestatus" }; + +static uint32_t LP_requests,LP_reserveds,LP_connects,LP_connecteds,LP_tradestatuses,LP_parse_errors,LP_unknowns,LP_duplicates,LP_aliceids; + void LP_tradecommand_log(cJSON *argjson) { static FILE *logfp; char *jsonstr; @@ -38,18 +54,206 @@ void LP_tradecommand_log(cJSON *argjson) } } -uint32_t LP_requests,LP_reserveds,LP_connects,LP_connecteds,LP_tradestatuses,LP_parse_errors,LP_unknowns,LP_duplicates,LP_numridqids; -uint64_t Ridqids[128]; +void LP_statslog_parseline(cJSON *lineobj) +{ + char *method; cJSON *obj; + if ( (method= jstr(lineobj,"method")) != 0 ) + { + if ( strcmp(method,"request") == 0 ) + LP_requests++; + else if ( strcmp(method,"reserved") == 0 ) + LP_reserveds++; + else if ( strcmp(method,"connect") == 0 ) + { + if ( (obj= jobj(lineobj,"trade")) == 0 ) + obj = lineobj; + LP_statslog_parsequote(method,obj); + LP_connects++; + } + else if ( strcmp(method,"connected") == 0 ) + { + LP_statslog_parsequote(method,lineobj); + LP_connecteds++; + } + else if ( strcmp(method,"tradestatus") == 0 ) + { + LP_statslog_parsequote(method,lineobj); + LP_tradestatuses++; + } + else + { + LP_unknowns++; + printf("parseline unknown method.(%s) (%s)\n",method,jprint(lineobj,0)); + } + } else printf("parseline no method.(%s)\n",jprint(lineobj,0)); +} + +int32_t LP_statslog_parse() +{ + static long lastpos; FILE *fp; char line[8192]; cJSON *lineobj; int32_t n = 0; + if ( (fp= fopen(LP_STATSLOG_FNAME,"rb")) != 0 ) + { + if ( lastpos > 0 ) + { + fseek(fp,0,SEEK_END); + if ( ftell(fp) > lastpos ) + fseek(fp,lastpos,SEEK_SET); + else + { + fclose(fp); + return(0); + } + } + while ( fgets(line,sizeof(line),fp) > 0 ) + { + lastpos = ftell(fp); + if ( (lineobj= cJSON_Parse(line)) != 0 ) + { + n++; + LP_statslog_parseline(lineobj); + //printf("%s\n",jprint(lineobj,0)); + free_json(lineobj); + } + } + fclose(fp); + } + return(n); +} + +struct LP_swapstats *LP_swapstats_find(uint64_t aliceid) +{ + struct LP_swapstats *sp; + HASH_FIND(hh,LP_swapstats,&aliceid,sizeof(aliceid),sp); + return(sp); +} + +struct LP_swapstats *LP_swapstats_add(uint64_t aliceid) +{ + struct LP_swapstats *sp; + if ( (sp= LP_swapstats_find(aliceid)) == 0 ) + { + sp = calloc(1,sizeof(*sp)); + sp->aliceid = aliceid; + HASH_ADD(hh,LP_swapstats,aliceid,sizeof(aliceid),sp); + } + return(LP_swapstats_find(aliceid)); +} uint64_t LP_aliceid_calc(bits256 desttxid,int32_t destvout,bits256 feetxid,int32_t feevout) { return((((uint64_t)desttxid.uints[0] << 48) | ((uint64_t)destvout << 32) | ((uint64_t)feetxid.uints[0] << 16) | (uint32_t)feevout)); } +void LP_swapstats_line(int32_t *numtrades,uint64_t *basevols,uint64_t *relvols,char *line,struct LP_swapstats *sp) +{ + char tstr[64]; int32_t baseind,relind; + if ( (baseind= LP_priceinfoind(sp->Q.srccoin)) >= 0 ) + basevols[baseind] += sp->Q.satoshis, numtrades[baseind]++; + if ( (relind= LP_priceinfoind(sp->Q.destcoin)) >= 0 ) + relvols[relind] += sp->Q.destsatoshis, numtrades[relind]++; + sprintf(line,"%s (%s).(%s) %-4d %9s %22llu: (%.8f %5s) -> (%.8f %5s) %.8f finished.%u expired.%u",utc_str(tstr,sp->Q.timestamp),sp->alicegui,sp->bobgui,sp->ind,LP_stats_methods[sp->methodind],(long long)sp->aliceid,dstr(sp->Q.satoshis),sp->Q.srccoin,dstr(sp->Q.destsatoshis),sp->Q.destcoin,sp->qprice,sp->finished,sp->expired); +} + +bits256 LP_swapstats_txid(cJSON *argjson,char *name,bits256 oldtxid) +{ + bits256 txid,deadtxid; + decode_hex(deadtxid.bytes,32,"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + txid = jbits256(argjson,name); + if ( bits256_nonz(txid) != 0 ) + { + if ( bits256_cmp(deadtxid,txid) == 0 ) + { + if ( bits256_nonz(oldtxid) == 0 ) + return(deadtxid); + else return(oldtxid); + } else return(txid); + } else return(oldtxid); +} + +int32_t LP_swapstats_update(struct LP_swapstats *sp,struct LP_quoteinfo *qp,cJSON *lineobj) +{ + char *statusstr,*base,*rel,gui[64]; uint32_t requestid,quoteid; uint64_t satoshis,destsatoshis; + safecopy(gui,sp->Q.gui,sizeof(gui)); + if ( strcmp(LP_stats_methods[sp->methodind],"tradestatus") == 0 ) + { + base = jstr(lineobj,"bob"); + rel = jstr(lineobj,"alice"); + requestid = juint(lineobj,"requestid"); + quoteid = juint(lineobj,"quoteid"); + satoshis = jdouble(lineobj,"srcamount") * SATOSHIDEN; + destsatoshis = jdouble(lineobj,"destamount") * SATOSHIDEN; + if ( base != 0 && strcmp(base,sp->Q.srccoin) == 0 && rel != 0 && strcmp(rel,sp->Q.destcoin) == 0 && requestid == sp->Q.R.requestid && quoteid == sp->Q.R.quoteid && ((satoshis+2*sp->Q.txfee)|1) == (sp->Q.satoshis|1) && ((destsatoshis+2*sp->Q.desttxfee)|1) == (sp->Q.destsatoshis|1) ) + { + sp->bobdeposit = LP_swapstats_txid(lineobj,"bobdeposit",sp->bobdeposit); + sp->alicepayment = LP_swapstats_txid(lineobj,"alicepayment",sp->alicepayment); + sp->bobpayment = LP_swapstats_txid(lineobj,"bobpayment",sp->bobpayment); + sp->paymentspent = LP_swapstats_txid(lineobj,"paymentspent",sp->paymentspent); + sp->Apaymentspent = LP_swapstats_txid(lineobj,"Apaymentspent",sp->Apaymentspent); + sp->depositspent = LP_swapstats_txid(lineobj,"depositspent",sp->depositspent); + if ( (statusstr= jstr(lineobj,"status")) != 0 && strcmp(statusstr,"finished") == 0 ) + sp->finished = juint(lineobj,"timestamp"); + if ( sp->finished == 0 && time(NULL) > sp->Q.timestamp+INSTANTDEX_LOCKTIME*2 ) + sp->expired = (uint32_t)time(NULL); + return(0); + } + else + { + if ( requestid == sp->Q.R.requestid && quoteid == sp->Q.R.quoteid ) + printf("mismatched tradestatus aliceid.%016llx b%s/%s r%s/%s r%u/%u q%u/%u %.8f/%.8f -> %.8f/%.8f\n",(long long)sp->aliceid,base,sp->Q.srccoin,rel,sp->Q.destcoin,requestid,sp->Q.R.requestid,quoteid,sp->Q.R.quoteid,dstr(satoshis+2*sp->Q.txfee),dstr(sp->Q.satoshis),dstr(destsatoshis+2*sp->Q.desttxfee),dstr(sp->Q.destsatoshis)); + return(-1); + } + + } else sp->Q = *qp; + if ( sp->Q.gui[0] == 0 || strcmp(sp->Q.gui,"nogui") == 0 ) + strcpy(sp->Q.gui,gui); + return(0); +} + int32_t LP_statslog_parsequote(char *method,cJSON *lineobj) { - double qprice; uint32_t timestamp; int32_t i,destvout,feevout,duplicate=0; char *gui,*base,*rel,tstr[128]; uint64_t txfee,satoshis,destsatoshis; bits256 desttxid,feetxid; struct LP_quoteinfo Q; uint64_t aliceid; + static uint32_t unexpected; + struct LP_swapstats *sp,*tmp; double qprice; uint32_t requestid,quoteid,timestamp; int32_t i,flag,numtrades[LP_MAXPRICEINFOS],methodind,destvout,feevout,duplicate=0; char *gui,*base,*rel; uint64_t aliceid,txfee,satoshis,destsatoshis; bits256 desttxid,feetxid; struct LP_quoteinfo Q; uint64_t basevols[LP_MAXPRICEINFOS],relvols[LP_MAXPRICEINFOS]; + memset(numtrades,0,sizeof(numtrades)); + memset(basevols,0,sizeof(basevols)); + memset(relvols,0,sizeof(relvols)); memset(&Q,0,sizeof(Q)); + for (i=methodind=0; imethodind = methodind; + if ( LP_swapstats_update(sp,&Q,lineobj) == 0 ) + flag = 1; + } + if ( flag == 0 ) + { + HASH_ITER(hh,LP_swapstats,sp,tmp) + { + if ( sp->Q.R.requestid == requestid && sp->Q.R.quoteid == quoteid ) + { + sp->methodind = methodind; + if ( LP_swapstats_update(sp,&Q,lineobj) == 0 ) + flag = 1; + else printf("error after delayed match\n"); + break; + } + } + } + if ( flag == 0 ) + printf("unexpected.%d tradestatus.(%s)\n",unexpected++,jprint(lineobj,0)); + return(0); + } if ( LP_quoteparse(&Q,lineobj) < 0 ) { printf("quoteparse_error.(%s)\n",jprint(lineobj,0)); @@ -58,9 +262,11 @@ int32_t LP_statslog_parsequote(char *method,cJSON *lineobj) } else { + gui = jstr(lineobj,"gui"); + if ( gui == 0 || gui[0] == 0 ) + gui = "nogui"; base = jstr(lineobj,"base"); rel = jstr(lineobj,"rel"); - gui = jstr(lineobj,"gui"); satoshis = j64bits(lineobj,"satoshis"); if ( base == 0 || rel == 0 || satoshis == 0 ) { @@ -78,104 +284,110 @@ int32_t LP_statslog_parsequote(char *method,cJSON *lineobj) qprice = ((double)destsatoshis / (satoshis - txfee)); //printf("%s/v%d %s/v%d\n",bits256_str(str,desttxid),destvout,bits256_str(str2,feetxid),feevout); aliceid = LP_aliceid_calc(desttxid,destvout,feetxid,feevout); - for (i=0; i sp->methodind ) { - duplicate = 1; - LP_duplicates++; - break; + sp->methodind = methodind; + LP_swapstats_update(sp,&Q,lineobj); } + duplicate = 1; + LP_duplicates++; } - if ( duplicate == 0 ) + else { - Ridqids[LP_numridqids % (sizeof(Ridqids)/sizeof(*Ridqids))] = aliceid; - printf("%s %8s %-4d %9s swap.%016llx: (%.8f %5s) -> (%.8f %5s) qprice %.8f\n",utc_str(tstr,timestamp),gui!=0?gui:"",LP_numridqids,method,(long long)aliceid,dstr(satoshis),base,dstr(destsatoshis),rel,qprice); - LP_numridqids++; + if ( (sp= LP_swapstats_add(aliceid)) != 0 ) + { + sp->Q = Q; + sp->qprice = qprice; + sp->methodind = methodind; + sp->ind = LP_aliceids++; + strcpy(sp->bobgui,"nogui"); + strcpy(sp->alicegui,"nogui"); + //LP_swapstats_line(numtrades,basevols,relvols,line,sp); + //printf("%s\n",line); + } else printf("unexpected LP_swapstats_add failure\n"); + } + if ( sp != 0 ) + { + if ( strcmp(gui,"nogui") != 0 ) + { + if ( jint(lineobj,"iambob") != 0 ) + strcpy(sp->bobgui,gui); + else strcpy(sp->alicegui,gui); + } } } return(duplicate == 0); } -void LP_statslog_parseline(cJSON *lineobj) +char *LP_statslog_disp(int32_t n,uint32_t starttime,uint32_t endtime) { - char *method; cJSON *obj; - if ( (method= jstr(lineobj,"method")) != 0 ) + cJSON *retjson,*array,*item; struct LP_swapstats *sp,*tmp; int32_t i,dispflag,numtrades[LP_MAXPRICEINFOS]; char line[1024]; uint64_t basevols[LP_MAXPRICEINFOS],relvols[LP_MAXPRICEINFOS]; + if ( starttime > endtime ) + starttime = endtime; + memset(basevols,0,sizeof(basevols)); + memset(relvols,0,sizeof(relvols)); + memset(numtrades,0,sizeof(numtrades)); + retjson = cJSON_CreateObject(); + jaddstr(retjson,"result","success"); + jaddnum(retjson,"newlines",n); + array = cJSON_CreateArray(); + HASH_ITER(hh,LP_swapstats,sp,tmp) { - if ( strcmp(method,"request") == 0 ) - LP_requests++; - else if ( strcmp(method,"reserved") == 0 ) - LP_reserveds++; - else if ( strcmp(method,"connect") == 0 ) - { - if ( (obj= jobj(lineobj,"trade")) == 0 ) - obj = lineobj; - LP_statslog_parsequote(method,obj); - LP_connects++; - } - else if ( strcmp(method,"connected") == 0 ) + if ( sp->finished == 0 && time(NULL) > sp->Q.timestamp+INSTANTDEX_LOCKTIME*2 ) + sp->expired = (uint32_t)time(NULL); + dispflag = 0; + if ( starttime == 0 && endtime == 0 ) + dispflag = 1; + else if ( starttime > time(NULL) && endtime == starttime && sp->finished == 0 && sp->expired == 0 ) + dispflag = 1; + else if ( sp->Q.timestamp >= starttime && sp->Q.timestamp <= endtime ) + dispflag = 1; + if ( dispflag != 0 ) { - LP_statslog_parsequote(method,lineobj); - LP_connecteds++; + LP_swapstats_line(numtrades,basevols,relvols,line,sp); + item = cJSON_CreateObject(); + jadd64bits(item,"aliceid",sp->aliceid); + jaddbits256(item,"src",sp->Q.srchash); + jaddstr(item,"base",sp->Q.srccoin); + jaddnum(item,"basevol",dstr(sp->Q.satoshis)); + jaddbits256(item,"dest",sp->Q.desthash); + jaddstr(item,"rel",sp->Q.destcoin); + jaddnum(item,"relvol",dstr(sp->Q.destsatoshis)); + jaddnum(item,"price",sp->qprice); + jaddnum(item,"requestid",sp->Q.R.requestid); + jaddnum(item,"quoteid",sp->Q.R.quoteid); + jaddstr(item,"line",line); + jaddi(array,item); } - else if ( strcmp(method,"tradestatus") == 0 ) - LP_tradestatuses++; - else + } + jadd(retjson,"swaps",array); + array = cJSON_CreateArray(); + for (i=0; i 0 ) - { - fseek(fp,0,SEEK_END); - if ( ftell(fp) > lastpos ) - fseek(fp,lastpos,SEEK_SET); - else - { - fclose(fp); - return(clonestr("{\"result\":\"success\",\"newlines\":0}")); - } - } - while ( fgets(line,sizeof(line),fp) > 0 ) - { - lastpos = ftell(fp); - if ( (lineobj= cJSON_Parse(line)) != 0 ) - { - n++; - LP_statslog_parseline(lineobj); - //printf("%s\n",jprint(lineobj,0)); - free_json(lineobj); - } - } - fclose(fp); - } - return(LP_statslog_disp(n)); -} - diff --git a/iguana/exchanges/Makefile b/iguana/exchanges/Makefile index 0048287e2..7d315ff5f 100644 --- a/iguana/exchanges/Makefile +++ b/iguana/exchanges/Makefile @@ -1,8 +1,10 @@ #cd .. #emcc -s ASSERTIONS=1 -s ASYNCIFY=1 -s TOTAL_MEMORY=134217728 --preload-file coins.json -DFROM_JS -O2 -I../includes -I../crypto777 -s PTHREAD_POOL_SIZE=8 -s USE_PTHREADS=1 -o /var/www/html/index.html exchanges/mm.c ../crypto777/*.c ../crypto777/jpeg/*.c ../crypto777/jpeg/unix/*.c mini-gmp.c secp256k1/src/secp256k1.c -lm ../OSlibs/js/libnanomsg.a -lc -lpthread +#emcc -s ASSERTIONS=1 -s ASYNCIFY=1 -s TOTAL_MEMORY=134217728 --preload-file coins.json -D__PNACL -DFROM_JS -O2 -I../includes -I../crypto777 -s PTHREAD_POOL_SIZE=8 -s USE_PTHREADS=1 -o /var/www/html/index.html exchanges/mm.c $(CRYPTO777_SRCS) mini-gmp.c secp256k1/src/secp256k1.c -lm -lc + include ../crypto777/crypto777.sources all: - emcc -s ASSERTIONS=1 -s ASYNCIFY=1 -s TOTAL_MEMORY=134217728 --preload-file coins.json -D__PNACL -DFROM_JS -O2 -I../includes -I../crypto777 -s PTHREAD_POOL_SIZE=8 -s USE_PTHREADS=1 -o /var/www/html/index.html exchanges/mm.c $(CRYPTO777_SRCS) mini-gmp.c secp256k1/src/secp256k1.c -lm + emcc -s ASSERTIONS=1 -s ASYNCIFY=1 -s TOTAL_MEMORY=134217728 --preload-file coins.json -D__PNACL -DFROM_JS -O2 -I../includes -I../crypto777 -o /var/www/html/index.html exchanges/mm.c $(CRYPTO777_SRCS) mini-gmp.c secp256k1/src/secp256k1.c -lm -lc diff --git a/iguana/exchanges/pendings b/iguana/exchanges/pendings new file mode 100755 index 000000000..a2aa46c83 --- /dev/null +++ b/iguana/exchanges/pendings @@ -0,0 +1,3 @@ +#!/bin/bash +source userpass +curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"statsdisp\",\"starttime\":2000000000,\"endtime\":2000000000}" diff --git a/iguana/exchanges/statsdisp b/iguana/exchanges/statsdisp index d322235a2..768c4e8b2 100755 --- a/iguana/exchanges/statsdisp +++ b/iguana/exchanges/statsdisp @@ -1,3 +1,3 @@ #!/bin/bash source userpass -curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"statsdisp\"}" +curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"statsdisp\",\"starttime\":0,\"endtime\":0}"