Browse Source

Merge pull request #398 from jl777/spvdex

Spvdex
etomic
jl777 7 years ago
committed by GitHub
parent
commit
2ed5fb4efe
  1. BIN
      OSlibs/js/libnanomsg.a
  2. BIN
      OSlibs/js/libnanomsg.so
  3. 6
      crypto777/crypto777.sources
  4. 73
      crypto777/nanosrc/aio/usock_posix.c
  5. 11
      crypto777/nanosrc/aio/usock_posix.c_dev
  6. 4
      crypto777/nanosrc/aio/usock_posix.h
  7. 2
      crypto777/nanosrc/aio/usock_win.c
  8. 43
      crypto777/nanosrc/aio/worker_posix.c
  9. 83
      crypto777/nanosrc/core/global.c
  10. 8
      crypto777/nanosrc/core/pipe.c
  11. 8
      crypto777/nanosrc/core/sock.c
  12. 8
      crypto777/nanosrc/devices/tcpmuxd.c
  13. 1
      crypto777/nanosrc/nn.h
  14. 14
      crypto777/nanosrc/nn_config.h
  15. 6
      crypto777/nanosrc/tests/testutil.h
  16. 4
      crypto777/nanosrc/transports/ipc/bipc.c
  17. 5
      crypto777/nanosrc/transports/ipc/cipc.c
  18. 4
      crypto777/nanosrc/transports/ipc/ipc.c
  19. 4
      crypto777/nanosrc/transports/tcpmux/btcpmux.c
  20. 4
      crypto777/nanosrc/transports/utils/tcpmux.c
  21. 2
      crypto777/nanosrc/transports/ws/sws.h
  22. 4
      crypto777/nanosrc/transports/ws/ws_handshake.c
  23. 4
      crypto777/nanosrc/utils/efd_eventfd.c
  24. 12
      crypto777/nanosrc/utils/efd_pipe.c
  25. 2
      crypto777/nanosrc/utils/efd_socketpair.c
  26. 5
      crypto777/nanosrc/utils/efd_win.c
  27. 4
      crypto777/nanosrc/utils/err.c
  28. 13
      crypto777/nanosrc/utils/err.h
  29. 4
      crypto777/nanosrc/utils/thread_posix.c
  30. 2
      iguana/exchanges/LP_bitcoin.c
  31. 12
      iguana/exchanges/LP_coins.c
  32. 16
      iguana/exchanges/LP_commands.c
  33. 38
      iguana/exchanges/LP_include.h
  34. 177
      iguana/exchanges/LP_nativeDEX.c
  35. 18
      iguana/exchanges/LP_network.c
  36. 159
      iguana/exchanges/LP_ordermatch.c
  37. 52
      iguana/exchanges/LP_peers.c
  38. 2
      iguana/exchanges/LP_portfolio.c
  39. 9
      iguana/exchanges/LP_prices.c
  40. 86
      iguana/exchanges/LP_remember.c
  41. 6
      iguana/exchanges/LP_rpc.c
  42. 43
      iguana/exchanges/LP_signatures.c
  43. 65
      iguana/exchanges/LP_socket.c
  44. 89
      iguana/exchanges/LP_statemachine.c
  45. 181
      iguana/exchanges/LP_stats.c
  46. 17
      iguana/exchanges/LP_swap.c
  47. 10
      iguana/exchanges/LP_transaction.c
  48. 218
      iguana/exchanges/LP_utxo.c
  49. 46
      iguana/exchanges/LP_utxos.c
  50. 8
      iguana/exchanges/Makefile
  51. 5
      iguana/exchanges/coins.json
  52. 2
      iguana/exchanges/install
  53. 4
      iguana/exchanges/m_js
  54. 18
      iguana/exchanges/mm.c
  55. 3
      iguana/exchanges/parselog
  56. 4
      iguana/exchanges/stats.c
  57. 3
      iguana/exchanges/statsdisp

BIN
OSlibs/js/libnanomsg.a

Binary file not shown.

BIN
OSlibs/js/libnanomsg.so

Binary file not shown.

6
crypto777/crypto777.sources

@ -36,11 +36,11 @@ TRANSPORTS_TCPMUX = $(NANOSRC)/transports/tcpmux/atcpmux.c $(NANOSRC)/transports
NANOMSG_PROTOCOLS = $(PROTOCOLS_UTILS) $(PROTOCOLS_PUBSUB) $(PROTOCOLS_PAIR) $(PROTOCOLS_REQREP) $(PROTOCOLS_BUS) $(PROTOCOLS_PIPELINE) $(PROTOCOLS_SURVEY)
NANOMSG_TRANSPORTS = $(TRANSPORTS_UTILS) $(TRANSPORTS_TCP) $(TRANSPORTS_IPC) $(TRANSPORTS_INPROC)
NANOMSG_TRANSPORTS = $(TRANSPORTS_UTILS) $(TRANSPORTS_TCP) $(TRANSPORTS_IPC) $(TRANSPORTS_INPROC) $(TRANSPORTS_WS)
NANOMSG = $(NANOMSG_CORE) $(NANOMSG_AIO) $(NANOMSG_UTILS) $(NANOMSG_DEVICES) $(NANOMSG_TRANSPORTS) $(NANOMSG_PROTOCOLS)
JPEG_SRCS := ../crypto777/jpeg/jaricom.c ../crypto777/jpeg/jcapimin.c ../crypto777/jpeg/jcapistd.c ../crypto777/jpeg/jcarith.c ../crypto777/jpeg/jccoefct.c ../crypto777/jpeg/jccolor.c ../crypto777/jpeg/jcdctmgr.c ../crypto777/jpeg/jchuff.c ../crypto777/jpeg/jcinit.c ../crypto777/jpeg/jcmainct.c ../crypto777/jpeg/jcmarker.c ../crypto777/jpeg/jcmaster.c ../crypto777/jpeg/jcomapi.c ../crypto777/jpeg/jcparam.c ../crypto777/jpeg/jcprepct.c ../crypto777/jpeg/jcsample.c ../crypto777/jpeg/jctrans.c ../crypto777/jpeg/jdapimin.c ../crypto777/jpeg/jdapistd.c ../crypto777/jpeg/jdarith.c ../crypto777/jpeg/jdatadst.c ../crypto777/jpeg/jdatasrc.c ../crypto777/jpeg/jdcoefct.c ../crypto777/jpeg/jdcolor.c ../crypto777/jpeg/jddctmgr.c ../crypto777/jpeg/jdhuff.c ../crypto777/jpeg/jdinput.c ../crypto777/jpeg/jdmainct.c ../crypto777/jpeg/jdmarker.c ../crypto777/jpeg/jdmaster.c ../crypto777/jpeg/jdmerge.c ../crypto777/jpeg/jdpostct.c ../crypto777/jpeg/jdsample.c ../crypto777/jpeg/jdtrans.c ../crypto777/jpeg/jerror.c ../crypto777/jpeg/jfdctflt.c ../crypto777/jpeg/jfdctfst.c ../crypto777/jpeg/jfdctint.c ../crypto777/jpeg/jidctflt.c ../crypto777/jpeg/jidctfst.c ../crypto777/jpeg/jidctint.c ../crypto777/jpeg/jquant1.c ../crypto777/jpeg/jquant2.c ../crypto777/jpeg/jutils.c ../crypto777/jpeg/misc/jmemnobs.c ../crypto777/jpeg/jmemmgr.c
JPEG_SRCS = ../crypto777/jpeg/jaricom.c ../crypto777/jpeg/jcapimin.c ../crypto777/jpeg/jcapistd.c ../crypto777/jpeg/jcarith.c ../crypto777/jpeg/jccoefct.c ../crypto777/jpeg/jccolor.c ../crypto777/jpeg/jcdctmgr.c ../crypto777/jpeg/jchuff.c ../crypto777/jpeg/jcinit.c ../crypto777/jpeg/jcmainct.c ../crypto777/jpeg/jcmarker.c ../crypto777/jpeg/jcmaster.c ../crypto777/jpeg/jcomapi.c ../crypto777/jpeg/jcparam.c ../crypto777/jpeg/jcprepct.c ../crypto777/jpeg/jcsample.c ../crypto777/jpeg/jctrans.c ../crypto777/jpeg/jdapimin.c ../crypto777/jpeg/jdapistd.c ../crypto777/jpeg/jdarith.c ../crypto777/jpeg/jdatadst.c ../crypto777/jpeg/jdatasrc.c ../crypto777/jpeg/jdcoefct.c ../crypto777/jpeg/jdcolor.c ../crypto777/jpeg/jddctmgr.c ../crypto777/jpeg/jdhuff.c ../crypto777/jpeg/jdinput.c ../crypto777/jpeg/jdmainct.c ../crypto777/jpeg/jdmarker.c ../crypto777/jpeg/jdmaster.c ../crypto777/jpeg/jdmerge.c ../crypto777/jpeg/jdpostct.c ../crypto777/jpeg/jdsample.c ../crypto777/jpeg/jdtrans.c ../crypto777/jpeg/jerror.c ../crypto777/jpeg/jfdctflt.c ../crypto777/jpeg/jfdctfst.c ../crypto777/jpeg/jfdctint.c ../crypto777/jpeg/jidctflt.c ../crypto777/jpeg/jidctfst.c ../crypto777/jpeg/jidctint.c ../crypto777/jpeg/jquant1.c ../crypto777/jpeg/jquant2.c ../crypto777/jpeg/jutils.c ../crypto777/jpeg/misc/jmemnobs.c ../crypto777/jpeg/jmemmgr.c
CRYPTO777_SRCS := ../crypto777/tweetnacl.c ../crypto777/scrypt.c ../crypto777/bitcoind_RPC.c ../crypto777/cJSON.c ../crypto777/iguana_utils.c ../crypto777/OS_nonportable.c ../crypto777/curve25519-donna.c ../crypto777/inet.c ../crypto777/OS_portable.c ../crypto777/curve25519.c ../crypto777/OS_time.c ../crypto777/hmac_sha512.c ../crypto777/ramcoder.c ../crypto777/SaM.c ../crypto777/iguana_OS.c ../crypto777/iguana_serdes.c $(JPEG_SRCS) $(NANOMSG)
CRYPTO777_SRCS = ../crypto777/tweetnacl.c ../crypto777/scrypt.c ../crypto777/bitcoind_RPC.c ../crypto777/cJSON.c ../crypto777/iguana_utils.c ../crypto777/OS_nonportable.c ../crypto777/curve25519-donna.c ../crypto777/inet.c ../crypto777/OS_portable.c ../crypto777/curve25519.c ../crypto777/OS_time.c ../crypto777/hmac_sha512.c ../crypto777/ramcoder.c ../crypto777/SaM.c ../crypto777/iguana_OS.c ../crypto777/iguana_serdes.c $(JPEG_SRCS) $(NANOMSG)

73
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_message("got socket s.%d\n",s);
//PNACL_msg("got socket s.%d\n",s);
nn_usock_init_from_fd (self, s);
/* Start the state machine. */
@ -286,11 +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);
rc = setsockopt (self->s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
errno_assert (rc == 0);
PNACL_msg("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);
//printf("usock.%d -> bind rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno));
PNACL_msg("usock.%d -> bind rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno));
if (nn_slow (rc != 0))
return -errno;
@ -306,7 +309,7 @@ int nn_usock_listen (struct nn_usock *self, int backlog)
/* Start listening for incoming connections. */
rc = listen (self->s, backlog);
//printf("usock.%d -> listen rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno));
//PNACL_msg("usock.%d -> listen rc.%d errno.%d %s\n",self->s,rc,errno,nn_strerror(errno));
if (nn_slow (rc != 0))
return -errno;
@ -333,7 +336,7 @@ void nn_usock_accept (struct nn_usock *self, struct nn_usock *listener)
#else
s = accept (listener->s, NULL, NULL);
#endif
//printf("usock.%d -> accept errno.%d s.%d %s\n",self->s,errno,s,nn_strerror(errno));
//PNACL_msg("usock.%d -> accept errno.%d s.%d %s\n",self->s,errno,s,nn_strerror(errno));
/* Immediate success. */
if (nn_fast (s >= 0)) {
@ -366,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_message("listen errno.%d\n",errno);
PNACL_msg("listen errno.%d\n",errno);
listener->errnum = errno;
listener->state = NN_USOCK_STATE_ACCEPTING_ERROR;
nn_fsm_raise (&listener->fsm,
@ -393,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);
//printf("usock.%d <- connect (%llx) rc.%d errno.%d %s\n",self->s,*(long long *)addr,rc,errno,nn_strerror(errno));
//PNACL_msg("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) )
{
@ -404,7 +407,7 @@ void nn_usock_connect (struct nn_usock *self, const struct sockaddr *addr,
if ( nn_slow(errno != EINPROGRESS) )
{
self->errnum = errno;
PNACL_message("error.%d not EINPROGRESS\n",errno);
PNACL_msg("error.%d not EINPROGRESS\n",errno);
nn_fsm_action (&self->fsm, NN_USOCK_ACTION_ERROR);
return;
}
@ -433,9 +436,9 @@ 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_message("{%d} ",(int)iov [i].iov_len);
//PNACL_msg("{%d} ",(int)iov [i].iov_len);
}
//PNACL_message("iov[%d]\n",out);
//PNACL_msg("iov[%d]\n",out);
self->out.hdr.msg_iovlen = out;
/* Try to send the data immediately. */
@ -472,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_message("rc.%d vs ECONNRESET\n",rc,ECONNRESET);
//PNACL_msg("rc.%d vs ECONNRESET\n",rc,ECONNRESET);
nn_fsm_action (&self->fsm, NN_USOCK_ACTION_ERROR);
return;
}
//int i;
//for (i=0; i<16&&i<nbytes; i++)
// PNACL_message("%02x ",((uint8_t *)buf)[i]);
//PNACL_message("nn_usock_recv nbytes.%d\n",(int)nbytes);
// PNACL_msg("%02x ",((uint8_t *)buf)[i]);
//PNACL_msg("nn_usock_recv nbytes.%d\n",(int)nbytes);
/* Success. */
if (nn_fast (nbytes == len)) {
//PNACL_message("raise NN_USOCK_RECEIVED\n");
//PNACL_msg("raise NN_USOCK_RECEIVED\n");
nn_fsm_raise (&self->fsm, &self->event_received, NN_USOCK_RECEIVED);
return;
}
@ -1021,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_message("ERROR: iov->iov_len == NN_MSG\n");
PNACL_msg("ERROR: iov->iov_len == NN_MSG\n");
return(-1);
}
if ( nn_slow(!iov->iov_base && iov->iov_len) )
{
errno = EFAULT;
PNACL_message("ERROR: !iov->iov_base && iov->iov_len\n");
PNACL_msg("ERROR: !iov->iov_base && iov->iov_len\n");
return(-1);
}
if ( maxlen > 0 && nn_slow(size + iov->iov_len > maxlen) )
{
errno = EINVAL;
PNACL_message("ERROR: sz.%d + iov->iov_len.%d < maxlen.%d\n",(int32_t)size,(int32_t)iov->iov_len,maxlen);
PNACL_msg("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 )
@ -1069,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_message(">>>>>>>>> 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);
//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);
if ( nbytes != offset + veclen )
{
//PNACL_message("nbytes.%d != offset.%d veclen.%d errno.%d usock.%d\n",(int32_t)nbytes,(int32_t)offset,veclen,errno,usock);
//PNACL_msg("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;
@ -1080,19 +1083,19 @@ ssize_t mysendmsg(int32_t usock,struct msghdr *hdr,int32_t flags)
else
{
err = -errno;
PNACL_message("mysendmsg: unexpected nn_getiovec_size error %d\n",err);
PNACL_msg("mysendmsg: unexpected nn_getiovec_size error %d\n",err);
}
if ( buf != _buf )
free(buf);
if ( err != 0 )
{
PNACL_message("nn_usock_send_raw errno.%d err.%d\n",errno,err);
PNACL_msg("nn_usock_send_raw errno.%d err.%d\n",errno,err);
return(-errno);
}
}
else
{
PNACL_message("nn_usock_send_raw errno.%d invalid iovec size\n",errno);
PNACL_msg("nn_usock_send_raw errno.%d invalid iovec size\n",errno);
return(-errno);
}
return(nbytes);
@ -1104,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_message("error getting veclen/clen n.%d vs %d from usock.%d\n",n,(int32_t)sizeof(lens),usock);
PNACL_msg("error getting veclen/clen n.%d vs %d from usock.%d\n",n,(int32_t)sizeof(lens),usock);
return(0);
} else PNACL_message("GOT %d bytes from usock.%d\n",n,usock);
} else PNACL_msg("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_message("veclen.%d clen.%d waiting in usock.%d\n",veclen,clen,usock);
PNACL_msg("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_message("myrecvmsg: unexpected cbytes.%d vs clen.%d\n",cbytes,clen);
PNACL_msg("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_message("myrecvmsg: partial nbytes.%d vs veclen.%d\n",(int32_t)nbytes,len);
//PNACL_msg("myrecvmsg: partial nbytes.%d vs veclen.%d\n",(int32_t)nbytes,len);
}
//PNACL_message("GOT nbytes.%d of len.%d from usock.%d\n",(int32_t)nbytes,len,usock);
//PNACL_msg("GOT nbytes.%d of len.%d from usock.%d\n",(int32_t)nbytes,len,usock);
if ( 0 && nbytes > 0 )
{
PNACL_message("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]);
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]);
}
return(nbytes);
}
@ -1144,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);
//printf("sendmsg nbytes.%d\n",(int32_t)nbytes);
//PNACL_msg("sendmsg nbytes.%d\n",(int32_t)nbytes);
#endif
#endif
/* Handle errors. */
@ -1199,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_message("CMSG set self->in.pfd (%d)\n",retval);
PNACL_msg("CMSG set self->in.pfd (%d)\n",retval);
*self->in.pfd = retval;
self->in.pfd = NULL;
}
else
{
PNACL_message("CMSG nn_closefd(%d)\n",retval);
PNACL_msg("CMSG nn_closefd(%d)\n",retval);
nn_closefd(retval);
}
break;
@ -1286,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);
//printf("got nbytes.%d from recvmsg errno.%d %s\n",(int32_t)nbytes,errno,nn_strerror(errno));
//PNACL_msg("got nbytes.%d from recvmsg errno.%d %s\n",(int32_t)nbytes,errno,nn_strerror(errno));
#else
nbytes = recvmsg (self->s, &hdr, 0);
#endif
@ -1298,7 +1301,7 @@ static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len)
nbytes = 0;
else
{
printf("recvraw errno.%d %s\n",errno,nn_strerror(errno));
PNACL_msg("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
@ -1310,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_message("nbytes.%d length.%d *len %d\n",(int)nbytes,(int)length,(int)*len);
//PNACL_msg("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 )

11
crypto777/nanosrc/aio/usock_posix.c_dev

@ -31,11 +31,11 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef __PNACL
//#ifndef __PNACL
#include <sys/uio.h>
#else
#include <glibc-compat/sys/uio.h>
#endif
//#else
//#include <glibc-compat/sys/uio.h>
//#endif
#define NN_USOCK_STATE_IDLE 1
#define NN_USOCK_STATE_STARTING 2
@ -287,7 +287,8 @@ int nn_usock_bind (struct nn_usock *self, const struct sockaddr *addr,
/* Allow re-using the address. */
opt = 1;
rc = setsockopt (self->s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
errno_assert (rc == 0);
// ignore SO_REUSEADDR failures
//errno_assert (rc == 0);
rc = bind (self->s, addr, (socklen_t) addrlen);
if (nn_slow (rc != 0))

4
crypto777/nanosrc/aio/usock_posix.h

@ -25,11 +25,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#ifndef __PNACL
#include <sys/uio.h>
#else
#include <glibc-compat/sys/uio.h>
#endif
struct nn_usock {

2
crypto777/nanosrc/aio/usock_win.c

@ -126,7 +126,7 @@ int nn_usock_start (struct nn_usock *self, int domain, int type, int protocol)
#if defined HANDLE_FLAG_INHERIT
BOOL brc;
#endif
printf("nn_usock_start protocol %d\n",protocol);
/* NamedPipes aren't sockets. They don't need all the socket
initialisation stuff. */
if (domain != AF_UNIX) {

43
crypto777/nanosrc/aio/worker_posix.c

@ -102,29 +102,28 @@ void nn_worker_task_term (struct nn_worker_task *self)
int nn_worker_init(struct nn_worker *self)
{
int32_t rc;
PNACL_message("nn_worker_init %p\n",self);
//sleep(1);
//PNACL_msg("nn_worker_init %p\n",self);
rc = nn_efd_init(&self->efd);
PNACL_message("efd init: rc.%d\n",rc);
//PNACL_msg("efd init: rc.%d\n",rc);
if ( rc < 0 )
return rc;
PNACL_message("nn_mutex_init\n");
//PNACL_msg("nn_mutex_init\n");
nn_mutex_init(&self->sync);
PNACL_message("nn_queue_init\n");
//PNACL_msg("nn_queue_init\n");
nn_queue_init(&self->tasks);
PNACL_message("nn_queue_item_init\n");
//PNACL_msg("nn_queue_item_init\n");
nn_queue_item_init(&self->stop);
PNACL_message("nn_poller_init\n");
//PNACL_msg("nn_poller_init\n");
nn_poller_init(&self->poller);
PNACL_message("nn_poller_add\n");
//PNACL_msg("nn_poller_add\n");
nn_poller_add(&self->poller,nn_efd_getfd(&self->efd),&self->efd_hndl);
PNACL_message("nn_poller_set_in\n");
//PNACL_msg("nn_poller_set_in\n");
nn_poller_set_in(&self->poller, &self->efd_hndl);
PNACL_message("nn_timerset_init\n");
//PNACL_msg("nn_timerset_init\n");
nn_timerset_init(&self->timerset);
PNACL_message("nn_thread_init\n");
//PNACL_msg("nn_thread_init\n");
nn_thread_init(&self->thread,nn_worker_routine, self);
PNACL_message("finished nn_worker_init\n");
//PNACL_msg("finished nn_worker_init\n");
return 0;
}
@ -174,7 +173,7 @@ static void nn_worker_routine (void *arg)
struct nn_worker_task *task;
struct nn_worker_fd *fd;
struct nn_worker_timer *timer;
PNACL_message("nn_worker_routine started\n");
PNACL_msg("nn_worker_routine started\n");
self = (struct nn_worker*) arg;
while ( 1 ) // Infinite loop. It will be interrupted only when the object is shut down.
{
@ -186,7 +185,7 @@ static void nn_worker_routine (void *arg)
rc = nn_timerset_event(&self->timerset, &thndl);
if ( rc == -EAGAIN )
break;
PNACL_message("nn_worker process expired user\n");
PNACL_msg("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);
@ -198,7 +197,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_message("nn_worker process all events from the poller\n");
PNACL_msg("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);
@ -213,14 +212,14 @@ static void nn_worker_routine (void *arg)
item = nn_queue_pop(&tasks); // Next worker task
if ( nn_slow(!item) )
break;
PNACL_message("nn_worker next worker task\n");
PNACL_msg("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_message("nn_worker user defined task\n");
PNACL_msg("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);
@ -229,15 +228,15 @@ static void nn_worker_routine (void *arg)
nn_queue_term (&tasks);
continue;
}
PNACL_message("nn_worker true i/o, invoke handler\n");
PNACL_msg("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_message("nn_worker true i/o, fd.%p\n",fd);
PNACL_msg("nn_worker true i/o, fd.%p\n",fd);
nn_ctx_enter(fd->owner->ctx);
PNACL_message("nn_worker true i/o, after nn_ctx_enter\n");
PNACL_msg("nn_worker true i/o, after nn_ctx_enter\n");
nn_fsm_feed(fd->owner,fd->src,pevent,fd);
PNACL_message("nn_worker true i/o, after nn_fsm_feed leave.%p\n",fd->owner->ctx);
PNACL_msg("nn_worker true i/o, after nn_fsm_feed leave.%p\n",fd->owner->ctx);
nn_ctx_leave(fd->owner->ctx);
PNACL_message("nn_worker true i/o, after nn_ctx_leave\n");
PNACL_msg("nn_worker true i/o, after nn_ctx_leave\n");
}
}
}

83
crypto777/nanosrc/core/global.c

@ -201,9 +201,9 @@ static void nn_global_add_transport (struct nn_transport *transport)
static void nn_global_add_socktype (struct nn_socktype *socktype)
{
PNACL_message("add socktype %p -> %p\n",socktype,&SELF.socktypes);
PNACL_msg("add socktype %p -> %p\n",socktype,&SELF.socktypes);
nn_list_insert (&SELF.socktypes, &socktype->item,nn_list_end (&SELF.socktypes));
PNACL_message("added socktype %p -> %p\n",socktype,&SELF.socktypes);
PNACL_msg("added socktype %p -> %p\n",socktype,&SELF.socktypes);
}
void nn_global_init (void)
{
@ -218,7 +218,7 @@ void nn_global_init (void)
nn_assert (LOBYTE (data.wVersion) == 2 &&
HIBYTE (data.wVersion) == 2);
#endif
PNACL_message("nn_global_init\n");
PNACL_msg("nn_global_init\n");
nn_alloc_init(); // Initialise the memory allocation subsystem
nn_random_seed(); // Seed the pseudo-random number generator
// Allocate the global table of SP sockets.
@ -227,7 +227,7 @@ void nn_global_init (void)
for (i=0; i<NN_MAX_SOCKETS; i++)
SELF.socks[i] = NULL;
SELF.nsocks = SELF.flags = 0;
PNACL_message("do getenv\n");
PNACL_msg("do getenv\n");
envvar = getenv("NN_PRINT_ERRORS"); // Print connection and accepting errors to the stderr
SELF.print_errors = envvar && *envvar; // any non-empty string is true
envvar = getenv("NN_PRINT_STATISTICS"); // Print socket statistics to stderr
@ -236,26 +236,26 @@ PNACL_message("do getenv\n");
alloc_assert (SELF.unused);
for (i=0; i<NN_MAX_SOCKETS; i++)
SELF.unused [i] = NN_MAX_SOCKETS - i - 1;
PNACL_message("list init\n");
PNACL_msg("list init\n");
// Initialise other parts of the global state.
nn_list_init(&SELF.transports);
nn_list_init(&SELF.socktypes);
//sleep(1);
PNACL_message("transports init\n");
PNACL_msg("transports init\n");
// Plug in individual transports.
//nn_global_add_transport(nn_ipc);
nn_global_add_transport(nn_ipc);
nn_global_add_transport(nn_tcp);
//nn_global_add_transport(nn_inproc);
//nn_global_add_transport(nn_ws);
//nn_global_add_transport(nn_tcpmux);
nn_global_add_transport(nn_inproc);
nn_global_add_transport(nn_ws);
//nn_global_add_transport(nn_tcpmux);
//sleep(1);
PNACL_message("socktypes init\n");
PNACL_msg("socktypes init\n");
// Plug in individual socktypes
nn_global_add_socktype(nn_pair_socktype);
//sleep(1);
PNACL_message("nn_xpair_socktype init\n");
PNACL_msg("nn_xpair_socktype init\n");
nn_global_add_socktype(nn_xpair_socktype);
PNACL_message("did nn_xpair_socktype init\n");
PNACL_msg("did nn_xpair_socktype init\n");
nn_global_add_socktype(nn_rep_socktype);
nn_global_add_socktype(nn_req_socktype);
nn_global_add_socktype(nn_xrep_socktype);
@ -275,19 +275,19 @@ PNACL_message("socktypes init\n");
nn_global_add_socktype(nn_bus_socktype);
nn_global_add_socktype(nn_xbus_socktype);
//sleep(1);
PNACL_message("do pool init\n");
PNACL_msg("do pool init\n");
nn_pool_init(&SELF.pool); // Start the worker threads
//sleep(1);
PNACL_message("do FSM init\n");
PNACL_msg("do FSM init\n");
nn_fsm_init_root(&SELF.fsm,nn_global_handler,nn_global_shutdown,&SELF.ctx); // Start FSM
SELF.state = NN_GLOBAL_STATE_IDLE;
PNACL_message("ctx init\n");
PNACL_msg("ctx init\n");
nn_ctx_init(&SELF.ctx, nn_global_getpool(),NULL);
PNACL_message("timer init\n");
PNACL_msg("timer init\n");
nn_timer_init(&SELF.stat_timer,NN_GLOBAL_SRC_STAT_TIMER,&SELF.fsm);
PNACL_message("do FSM start\n");
PNACL_msg("do FSM start\n");
nn_fsm_start(&SELF.fsm);
PNACL_message("special sockets init\n");
PNACL_msg("special sockets init\n");
// Initializing special sockets.
addr = getenv("NN_STATISTICS_SOCKET");
if ( addr != 0 )
@ -503,7 +503,7 @@ int32_t nn_global_create_socket(int32_t domain,int32_t protocol)
int nn_socket(int domain,int protocol)
{
int rc;
PNACL_message("nn_socket flags.%d\n",SELF.flags);
PNACL_msg("nn_socket flags.%d\n",SELF.flags);
nn_glock_lock();
if (nn_slow (SELF.flags & NN_CTX_FLAG_ZOMBIE)) // If nn_term() was already called, return ETERM
{
@ -511,7 +511,7 @@ int nn_socket(int domain,int protocol)
errno = ETERM;
return -1;
}
//PNACL_message("nn_socket flags.%d\n",SELF.flags);
//PNACL_msg("nn_socket flags.%d\n",SELF.flags);
nn_global_init(); // Make sure that global state is initialised
rc = nn_global_create_socket (domain, protocol);
if ( rc < 0 )
@ -522,7 +522,7 @@ int nn_socket(int domain,int protocol)
return -1;
}
nn_glock_unlock();
//PNACL_message("did nn_global_init\n");
//PNACL_msg("did nn_global_init\n");
return rc;
}
@ -652,6 +652,7 @@ int32_t nn_send(int32_t s,const void *buf,size_t len,int32_t flags)
hdr.msg_iovlen = 1;
hdr.msg_control = NULL;
hdr.msg_controllen = 0;
printf("nn_send calling nn_sendmsg\n");
return nn_sendmsg(s,&hdr,flags);
}
@ -672,15 +673,17 @@ int32_t nn_recv(int32_t s,void *buf,size_t len,int32_t flags)
int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
{
int32_t rc,i,nnmsg; size_t sz; struct nn_iovec *iov; struct nn_msg msg; void *chunk;
//PNACL_message("nn_sendmsg.(%d) \n",s);
printf("nn_sendmsg.(%d) \n",s);
NN_BASIC_CHECKS;
if ( nn_slow(!msghdr) )
{
printf("nn_sendmsg.(%d) einval\n",s);
errno = EINVAL;
return -1;
}
if ( nn_slow(msghdr->msg_iovlen < 0) )
{
printf("nn_sendmsg.(%d) emsgsize\n",s);
errno = EMSGSIZE;
return -1;
}
@ -689,6 +692,7 @@ int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
chunk = *(void **)msghdr->msg_iov[0].iov_base;
if ( nn_slow(chunk == NULL) )
{
printf("nn_sendmsg.(%d) efault\n",s);
errno = EFAULT;
return -1;
}
@ -731,6 +735,7 @@ int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
}
nn_assert(msghdr->msg_control == 0); // cant support msgs until sendmsg()/recvmsg() native to pnacl
rc = nn_sock_send(SELF.socks[s],&msg,flags); // Send it further down the stack
printf("rc from nn_socksend.%d\n",rc);
if ( nn_slow(rc < 0) )
{
// If we are dealing with user-supplied buffer, detach it from the message object
@ -772,13 +777,13 @@ int32_t nn_recvmsg(int32_t s,struct nn_msghdr *msghdr,int32_t flags)
chunk = nn_chunkref_getchunk(&msg.body);
*(void **)(msghdr->msg_iov[0].iov_base) = chunk;
sz = nn_chunk_size(chunk);
//PNACL_message("got message -> iov_base.%p sz.%d\n",msghdr->msg_iov[0].iov_base,(int32_t)sz);
//PNACL_msg("got message -> iov_base.%p sz.%d\n",msghdr->msg_iov[0].iov_base,(int32_t)sz);
}
else // Copy the message content into the supplied gather array
{
data = nn_chunkref_data(&msg.body);
sz = nn_chunkref_size(&msg.body);
//PNACL_message("got message -> data.%p sz.%d\n",data,(int32_t)sz);
//PNACL_msg("got message -> data.%p sz.%d\n",data,(int32_t)sz);
for (i=0; i!=msghdr->msg_iovlen; i++)
{
iov = &msghdr->msg_iov[i];
@ -809,15 +814,17 @@ int32_t nn_recvmsg(int32_t s,struct nn_msghdr *msghdr,int32_t flags)
int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
{
int32_t rc,i,nnmsg; size_t sz,spsz; struct nn_iovec *iov; struct nn_msg msg; void *chunk; struct nn_cmsghdr *cmsg;
//PNACL_message("nn_sendmsg.(%d) \n",s);
printf("nn_sendmsg.(%d) \n",s);
NN_BASIC_CHECKS;
if ( nn_slow(!msghdr) )
{
printf("nn_sendmsg.EINVAL\n");
errno = EINVAL;
return -1;
}
if ( nn_slow(msghdr->msg_iovlen < 0) )
{
printf("nn_sendmsg.EMSGSIZE\n");
errno = EMSGSIZE;
return -1;
}
@ -827,6 +834,7 @@ int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
if ( nn_slow(chunk == NULL) )
{
errno = EFAULT;
printf("nn_sendmsg.EFAULT\n");
return -1;
}
sz = nn_chunk_size(chunk);
@ -905,6 +913,7 @@ int32_t nn_sendmsg(int32_t s,const struct nn_msghdr *msghdr,int32_t flags)
/* Send it further down the stack. */
rc = nn_sock_send (SELF.socks [s], &msg, flags);
printf("nn_sock_send rc.%d\n",rc);
if (nn_slow (rc < 0)) {
/* If we are dealing with user-supplied buffer, detach it from
@ -928,7 +937,7 @@ int32_t nn_recvmsg(int32_t s,struct nn_msghdr *msghdr,int32_t flags)
{
struct nn_msg msg; uint8_t *data; struct nn_iovec *iov; void *chunk,*ctrl; struct nn_cmsghdr *chdr;
int32_t i,rc; size_t sz,hdrssz,ctrlsz,spsz,sptotalsz;
//PNACL_message("nn_recvmsg.(%d) \n",s);
//PNACL_msg("nn_recvmsg.(%d) \n",s);
NN_BASIC_CHECKS;
if ( nn_slow(!msghdr) )
{
@ -940,7 +949,7 @@ int32_t nn_recvmsg(int32_t s,struct nn_msghdr *msghdr,int32_t flags)
errno = EMSGSIZE;
return -1;
}
//PNACL_message("get a message from sock.%d\n",s);
//PNACL_msg("get a message from sock.%d\n",s);
rc = nn_sock_recv(SELF.socks[s],&msg,flags); // Get a message
if ( nn_slow(rc < 0) )
{
@ -952,13 +961,13 @@ int32_t nn_recvmsg(int32_t s,struct nn_msghdr *msghdr,int32_t flags)
chunk = nn_chunkref_getchunk(&msg.body);
*(void **)(msghdr->msg_iov[0].iov_base) = chunk;
sz = nn_chunk_size(chunk);
//PNACL_message("got message -> iov_base.%p sz.%d\n",msghdr->msg_iov[0].iov_base,(int32_t)sz);
//PNACL_msg("got message -> iov_base.%p sz.%d\n",msghdr->msg_iov[0].iov_base,(int32_t)sz);
}
else // Copy the message content into the supplied gather array
{
data = nn_chunkref_data(&msg.body);
sz = nn_chunkref_size (&msg.body);
//PNACL_message("got message -> data.%p sz.%d\n",data,(int32_t)sz);
//PNACL_msg("got message -> data.%p sz.%d\n",data,(int32_t)sz);
for (i=0; i!=msghdr->msg_iovlen; i++)
{
iov = &msghdr->msg_iov[i];
@ -1150,7 +1159,7 @@ static void nn_global_submit_errors (int i, struct nn_sock *s,
len = snprintf (curbuf, buf_left,
" nanomsg: Endpoint %d [%s] error: %s\n",
ep->eid, nn_ep_getaddr (ep), nn_strerror (ep->last_errno));
PNACL_message("%s\n",curbuf);
PNACL_msg("%s\n",curbuf);
#endif
if (buf_left < len)
break;
@ -1245,11 +1254,11 @@ static int nn_global_create_ep (int s, const char *addr, int bind)
protosz = delim - addr;
addr += protosz + 3;
#ifdef NN_USE_MYMSG
if ( strncmp("inproc",proto,strlen("inproc")) != 0 && strncmp("ipc",proto,strlen("ipc")) != 0 && strncmp("tcp",proto,strlen("tcp")) != 0 )
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_message("only ipc, inproc and tcp transport is supported\n");
printf("only ipc, inproc and tcp transport is supported\n");
fprintf(stderr,"only ipc, inproc and tcp transport is supported\n");
PNACL_msg("only ipc, inproc, ws and tcp transport is supported\n");
printf("only ipc, inproc, ws and tcp transport is supported\n");
fprintf(stderr,"only ipc, inproc, ws and tcp transport is supported\n");
exit(-1);
return -EPROTONOSUPPORT;
}
@ -1324,12 +1333,12 @@ static void nn_global_handler (struct nn_fsm *myself,int src, int type, NN_UNUSE
nn_timer_start (&global->stat_timer, 10000); // Start statistics collection timer
return;
default:
PNACL_message("bad action %d type %d\n",src,type);
PNACL_msg("bad action %d type %d\n",src,type);
nn_fsm_bad_action(global->state, src, type);
}
default:
PNACL_message("bad source %d\n",src);
PNACL_msg("bad source %d\n",src);
nn_fsm_bad_source(global->state, src, type);
}

8
crypto777/nanosrc/core/pipe.c

@ -58,7 +58,7 @@ void nn_pipebase_init(struct nn_pipebase *self,const struct nn_pipebase_vfptr *v
memcpy(&self->options,&epbase->ep->options,sizeof(struct nn_ep_options));
nn_fsm_event_init(&self->in);
nn_fsm_event_init(&self->out);
PNACL_message("pipebase_init vfptr.%p recv.%p rcvprio.%d\n",vfptr,vfptr->recv,self->options.rcvprio);
PNACL_msg("pipebase_init vfptr.%p recv.%p rcvprio.%d\n",vfptr,vfptr->recv,self->options.rcvprio);
}
void nn_pipebase_term (struct nn_pipebase *self)
@ -78,7 +78,7 @@ int nn_pipebase_start (struct nn_pipebase *self)
self->instate = NN_PIPEBASE_INSTATE_ASYNC;
self->outstate = NN_PIPEBASE_OUTSTATE_IDLE;
rc = nn_sock_add(self->sock,(struct nn_pipe *)self);
PNACL_message("nn_pipebase_start self.%p rc.%d\n",self,rc);
PNACL_msg("nn_pipebase_start self.%p rc.%d\n",self,rc);
if ( nn_slow(rc < 0) )
{
self->state = NN_PIPEBASE_STATE_FAILED;
@ -98,7 +98,7 @@ void nn_pipebase_stop(struct nn_pipebase *self)
void nn_pipebase_received(struct nn_pipebase *self)
{
PNACL_message("nn_pipebase_received\n");
PNACL_msg("nn_pipebase_received\n");
if ( nn_fast(self->instate == NN_PIPEBASE_INSTATE_RECEIVING) )
{
self->instate = NN_PIPEBASE_INSTATE_RECEIVED;
@ -196,7 +196,7 @@ int nn_pipe_recv(struct nn_pipe *self,struct nn_msg *msg)
pipebase = (struct nn_pipebase*) self;
nn_assert (pipebase->instate == NN_PIPEBASE_INSTATE_IDLE);
pipebase->instate = NN_PIPEBASE_INSTATE_RECEIVING;
PNACL_message("call pipebase recv\n");
PNACL_msg("call pipebase recv\n");
rc = pipebase->vfptr->recv (pipebase,msg);
errnum_assert (rc >= 0, -rc);
if ( nn_fast(pipebase->instate == NN_PIPEBASE_INSTATE_RECEIVED) )

8
crypto777/nanosrc/core/sock.c

@ -489,11 +489,11 @@ int nn_sock_add_ep(struct nn_sock *self,struct nn_transport *transport,int32_t b
return rc;
}
nn_ep_start(ep);
PNACL_message("ep sock.(%s) started %s://(%s) bind.%d\n",self->socket_name,transport->name,addr,bind);
PNACL_msg("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_message("self->sock.(%s) %p already has (%s)\n",self->socket_name,self->sockbase->sock,addr);
} else PNACL_msg("self->sock.(%s) %p already has (%s)\n",self->socket_name,self->sockbase->sock,addr);
return(ep->eid);
}
@ -973,12 +973,12 @@ void nn_sock_report_error(struct nn_sock *self,struct nn_ep *ep,int32_t errnum,c
if ( ep != 0 )
{
fprintf(stderr,"nanomsg: socket.%s[%s]: Error: %s\n",self->socket_name,nn_ep_getaddr(ep),nn_strerror(errnum));
PNACL_message("nanomsg: socket.%s[%s]: [%s:%d] Error: %s\n",self->socket_name,nn_ep_getaddr(ep),fname,linenum,nn_strerror(errnum));
PNACL_msg("nanomsg: socket.%s[%s]: [%s:%d] Error: %s\n",self->socket_name,nn_ep_getaddr(ep),fname,linenum,nn_strerror(errnum));
}
else
{
fprintf(stderr,"nanomsg: socket.%s: Error: %s\n",self->socket_name, nn_strerror(errnum));
PNACL_message("nanomsg: socket.%s: [%s:%d] Error: %s\n",self->socket_name,fname,linenum,nn_strerror(errnum));
PNACL_msg("nanomsg: socket.%s: [%s:%d] Error: %s\n",self->socket_name,fname,linenum,nn_strerror(errnum));
}
}

8
crypto777/nanosrc/devices/tcpmuxd.c

@ -53,12 +53,7 @@ int nn_tcpmuxd (int port)
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#ifndef __PNACL
#include <sys/un.h>
#else
#include <glibc-compat/sys/un.h>
#include <glibc-compat/sys/uio.h>
#endif
#include <stddef.h>
#include <ctype.h>
#include <poll.h>
@ -101,7 +96,8 @@ int nn_tcpmuxd (int port)
opt = 1;
rc = setsockopt (tcp_listener, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof (opt));
if (rc != 0) { return -1; }
// ignore SO_REUSEADDR failures
//if (rc != 0) { return -1; }
memset (&tcp_addr, 0, sizeof (tcp_addr));
tcp_addr.sin_family = AF_INET;
tcp_addr.sin_port = htons (port);

1
crypto777/nanosrc/nn.h

@ -30,6 +30,7 @@ extern "C" {
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include "nn_config.h"
/* Handle DSO symbol visibility */

14
crypto777/nanosrc/nn_config.h

@ -32,8 +32,8 @@
// need one of following 3, listed in order of precedence, used by efd*
//#define NN_HAVE_EVENTFD 1
//#define NN_HAVE_PIPE 1
#define NN_HAVE_SOCKETPAIR 1
#define NN_HAVE_PIPE 1
//#define NN_HAVE_SOCKETPAIR 1
// need one of following 3, listed in order of precedence, used by poller*
#define NN_USE_POLL 1
@ -46,21 +46,13 @@
#define NN_HAVE_MSG_CONTROL 0
//#define STANDALONE 1
#define PNACL_msg(...)
#ifdef __PNACL
//#define FD_CLOEXEC 1
void PNACL_message(const char* format, ...);
#include <glibc-compat/sys/uio.h>
#include <glibc-compat/sys/un.h>
#else
#if !defined(WIN32)
//#define NN_ENABLE_EXTRA 1
#define PNACL_message printf
#include <sys/uio.h>
#include <sys/un.h>
#endif
#endif
/* Size of the buffer used for batch-reads of inbound data. To keep the
performance optimal make sure that this value is larger than network MTU. */

6
crypto777/nanosrc/tests/testutil.h

@ -26,9 +26,9 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef __PNACL
#define printf PostMessage
#endif
//#ifdef __PNACL
//#define printf PostMessage
//#endif
#include "../utils/attr.h"
#include "../utils/err.h"

4
crypto777/nanosrc/transports/ipc/bipc.c

@ -39,12 +39,8 @@
#include "../../utils/win.h"
#else
#include <unistd.h>
#ifndef __PNACL
#include <sys/uio.h>
#include <sys/un.h>
#else
#include <glibc-compat/sys/un.h>
#endif
#include <fcntl.h>
#endif

5
crypto777/nanosrc/transports/ipc/cipc.c

@ -39,13 +39,8 @@
#include "../../utils/win.h"
#else
#include <unistd.h>
#ifndef __PNACL
#include <sys/uio.h>
#include <sys/un.h>
#else
#include <glibc-compat/sys/uio.h>
#include <glibc-compat/sys/un.h>
#endif
#endif
#define NN_CIPC_STATE_IDLE 1

4
crypto777/nanosrc/transports/ipc/ipc.c

@ -37,11 +37,7 @@
#include "../../utils/win.h"
#else
#include <sys/socket.h>
#ifndef __PNACL
#include <sys/uio.h>
#else
#include <glibc-compat/sys/un.h>
#endif
#include <unistd.h>
#endif

4
crypto777/nanosrc/transports/tcpmux/btcpmux.c

@ -44,12 +44,8 @@
#include "../../utils/win.h"
#else
#include <unistd.h>
#ifndef __PNACL
#include <sys/uio.h>
#include <sys/un.h>
#else
#include <glibc-compat/sys/un.h>
#endif
#include <arpa/inet.h>
#endif

4
crypto777/nanosrc/transports/utils/tcpmux.c

@ -28,11 +28,7 @@
#else
#include <unistd.h>
#include <sys/socket.h>
#ifndef __PNACL
#include <sys/un.h>
#else
#include <glibc-compat/sys/un.h>
#endif
#endif
#include "tcpmux.h"

2
crypto777/nanosrc/transports/ws/sws.h

@ -152,7 +152,7 @@ struct nn_sws {
uint8_t inmsg_control [NN_SWS_PAYLOAD_MAX_LENGTH];
/* Reason this connection is closing to send as closing handshake. */
char fail_msg [NN_SWS_PAYLOAD_MAX_LENGTH];
uint8_t fail_msg [NN_SWS_PAYLOAD_MAX_LENGTH];
size_t fail_msg_len;
/* State of the outbound state machine. */

4
crypto777/nanosrc/transports/ws/ws_handshake.c

@ -1213,7 +1213,7 @@ static void nn_ws_handshake_client_request (struct nn_ws_handshake *self)
nn_random_generate (rand_key, sizeof (rand_key));
rc = nn_base64_encode (rand_key, sizeof (rand_key),
rc = _nn_base64_encode (rand_key, sizeof (rand_key),
encoded_key, sizeof (encoded_key));
encoded_key_len = strlen (encoded_key);
@ -1355,7 +1355,7 @@ static int nn_ws_handshake_hash_key (const char *key, size_t key_len,
for (i = 0; i < strlen (NN_WS_HANDSHAKE_MAGIC_GUID); i++)
nn_sha1_hashbyte (&hash, NN_WS_HANDSHAKE_MAGIC_GUID [i]);
rc = nn_base64_encode (nn_sha1_result (&hash),
rc = _nn_base64_encode (nn_sha1_result (&hash),
sizeof (hash.state), hashed, hashed_len);
return rc;

4
crypto777/nanosrc/utils/efd_eventfd.c

@ -32,9 +32,9 @@ int nn_efd_init(struct nn_efd *self)
{
int rc;
int flags;
//PostMessage("inside efd_init eventfd\n");
PNACL_msg("inside efd_init eventfd\n");
self->efd = eventfd(0, EFD_CLOEXEC);
//PostMessage("self->efd: %d\n",self->efd);
PNACL_msg("self->efd: %d\n",self->efd);
if (self->efd == -1 && (errno == EMFILE || errno == ENFILE))
return -EMFILE;
errno_assert(self->efd != -1);

12
crypto777/nanosrc/utils/efd_pipe.c

@ -33,14 +33,14 @@
int nn_efd_init(struct nn_efd *self)
{
int rc,flags,p[2];
PNACL_message("inside efd_init: pipe\n");
PNACL_msg("inside efd_init: pipe\n");
//sleep(1);
#if defined NN_HAVE_PIPE2
rc = pipe2(p, O_NONBLOCK | O_CLOEXEC);
#else
rc = pipe(p);
#endif
PNACL_message("rc efd_init: %d\n",rc);
PNACL_msg("rc efd_init: %d\n",rc);
//sleep(1);
if (rc != 0 && (errno == EMFILE || errno == ENFILE))
return -EMFILE;
@ -50,20 +50,20 @@ int nn_efd_init(struct nn_efd *self)
#if !defined NN_HAVE_PIPE2 && defined FD_CLOEXEC
rc = fcntl (self->r, F_SETFD, FD_CLOEXEC);
PNACL_message("pipe efd_init: F_SETFDr rc %d\n",rc);
PNACL_msg("pipe efd_init: F_SETFDr rc %d\n",rc);
errno_assert(rc != -1);
rc = fcntl(self->w, F_SETFD, FD_CLOEXEC);
PNACL_message("pipe efd_init: F_SETFDw rc %d\n",rc);
PNACL_msg("pipe efd_init: F_SETFDw rc %d\n",rc);
errno_assert(rc != -1);
#endif
#if !defined NN_HAVE_PIPE2
flags = fcntl(self->r, F_GETFL, 0);
PNACL_message("pipe efd_init: flags %d\n",flags);
PNACL_msg("pipe efd_init: flags %d\n",flags);
if ( flags == -1 )
flags = 0;
rc = fcntl(self->r, F_SETFL, flags | O_NONBLOCK);
PNACL_message("pipe efd_init: rc %d flags.%d\n",rc,flags);
PNACL_msg("pipe efd_init: rc %d flags.%d\n",rc,flags);
errno_assert (rc != -1);
#endif

2
crypto777/nanosrc/utils/efd_socketpair.c

@ -35,7 +35,7 @@ int nn_efd_init (struct nn_efd *self)
int rc;
int flags;
int sp [2];
PNACL_msg("socketpair efdinit\n");
#if defined SOCK_CLOEXEC
rc = socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sp);
#else

5
crypto777/nanosrc/utils/efd_win.c

@ -84,8 +84,9 @@ int nn_efd_init (struct nn_efd *self)
reuseaddr = 1;
rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
(char*) &reuseaddr, sizeof (reuseaddr));
if (nn_slow (rc == SOCKET_ERROR))
goto wsafail;
// ignore SO_REUSEADDR failures
// if (nn_slow (rc == SOCKET_ERROR))
// goto wsafail;
/* Bind the listening socket to the local port. */
memset (&addr, 0, sizeof (addr));

4
crypto777/nanosrc/utils/err.c

@ -30,7 +30,7 @@
void nn_err_abort(void)
{
PNACL_message("nn_err_abort\n");
PNACL_msg("nn_err_abort\n");
while ( 1 )
{
printf("exit due to error\n");
@ -111,7 +111,7 @@ const char *nn_err_strerror(int32_t errnum)
#pragma warning (push)
#pragma warning (disable:4996)
#endif
PNACL_message("nanomsg: errno.%d (%s)\n",errnum,strerror(errnum));
PNACL_msg("nanomsg: errno.%d (%s)\n",errnum,strerror(errnum));
getchar();
return strerror (errnum);
#if defined _MSC_VER

13
crypto777/nanosrc/utils/err.h

@ -26,6 +26,7 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define PNACL_msg(...)
/* Include nn.h header to define nanomsg-specific error codes. */
#include "../nn.h"
@ -44,7 +45,7 @@
#define nn_assert(x) \
do {\
if (nn_slow (!(x))) {\
PNACL_message("Assertion failed: %s (%s:%d)\n", #x, \
PNACL_msg("Assertion failed: %s (%s:%d)\n", #x, \
__FILE__, __LINE__);\
fflush (stderr);\
nn_err_abort ();\
@ -54,7 +55,7 @@
#define nn_assert_state(obj, state_name) \
do {\
if (nn_slow ((obj)->state != state_name)) {\
PNACL_message( \
PNACL_msg( \
"Assertion failed: %d == %s (%s:%d)\n", \
(obj)->state, #state_name, \
__FILE__, __LINE__);\
@ -66,7 +67,7 @@
#define alloc_assert(x) \
do {\
if (nn_slow (!x)) {\
PNACL_message("Out of memory (%s:%d)\n",\
PNACL_msg("Out of memory (%s:%d)\n",\
__FILE__, __LINE__);\
nn_err_abort ();\
}\
@ -76,7 +77,7 @@
#define errno_assert(x) \
do {\
if (nn_slow (!(x))) {\
PNACL_message("%s [%d] (%s:%d)\n", nn_err_strerror (errno),\
PNACL_msg("%s [%d] (%s:%d)\n", nn_err_strerror (errno),\
(int) errno, __FILE__, __LINE__);\
nn_err_abort ();\
}\
@ -86,7 +87,7 @@
#define errnum_assert(cond, err) \
do {\
if (nn_slow (!(cond))) {\
PNACL_message("%s [%d] (%s:%d)\n", nn_err_strerror (err),(int) (err), __FILE__, __LINE__);\
PNACL_msg("%s [%d] (%s:%d)\n", nn_err_strerror (err),(int) (err), __FILE__, __LINE__);\
nn_err_abort ();\
}\
} while (0)
@ -118,7 +119,7 @@
/* Assertion-like macros for easier fsm debugging. */
#define nn_fsm_error(message, state, src, type) \
do {\
PNACL_message("%s: state=%d source=%d action=%d (%s:%d)\n", \
PNACL_msg("%s: state=%d source=%d action=%d (%s:%d)\n", \
message, state, src, type, __FILE__, __LINE__);\
nn_err_abort ();\
} while (0)

4
crypto777/nanosrc/utils/thread_posix.c

@ -30,7 +30,7 @@ static void *nn_thread_main_routine(void *arg)
{
struct nn_thread *self;
self = (struct nn_thread *)arg;
PNACL_message("nn_thread_main_routine arg.%p self->routine(%p) at %p\n",arg,self->arg,self->routine);
PNACL_msg("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;
}
@ -47,7 +47,7 @@ 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_message("nn_thread_init routine.%p arg.%p\n",routine,arg);
PNACL_msg("nn_thread_init routine.%p arg.%p\n",routine,arg);
self->routine = routine;
self->arg = arg;
rc = pthread_create(&self->handle,NULL,nn_thread_main_routine,(void *)self);

2
iguana/exchanges/LP_bitcoin.c

@ -3561,7 +3561,7 @@ cJSON *bitcoin_data2json(uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t
if ( n != len )
{
int32_t i;
for (i=0; i<=len; i++)
for (i=0; i<=len&&i<200; i++)
printf("%02x",serialized[i]);
printf(" data2json n.%d vs len.%d\n",n,len);
}

12
iguana/exchanges/LP_coins.c

@ -285,7 +285,6 @@ struct iguana_info *LP_coinsearch(char *symbol)
struct iguana_info *LP_coinadd(struct iguana_info *cdata)
{
struct iguana_info *coin = calloc(1,sizeof(*coin));
//printf("%s: (%s) (%s)\n",symbol,cdata.serverport,cdata.userpass);
*coin = *cdata;
portable_mutex_init(&coin->txmutex);
portable_mutex_init(&coin->addrmutex);
@ -297,6 +296,7 @@ struct iguana_info *LP_coinadd(struct iguana_info *cdata)
uint16_t LP_coininit(struct iguana_info *coin,char *symbol,char *name,char *assetname,int32_t isPoS,uint16_t port,uint8_t pubtype,uint8_t p2shtype,uint8_t wiftype,uint64_t txfee,double estimatedrate,int32_t longestchain,uint8_t wiftaddr,uint8_t taddr,uint16_t busport,char *confpath)
{
static void *ctx;
char *name2;
memset(coin,0,sizeof(*coin));
safecopy(coin->symbol,symbol,sizeof(coin->symbol));
@ -312,10 +312,12 @@ uint16_t LP_coininit(struct iguana_info *coin,char *symbol,char *name,char *asse
coin->wiftype = wiftype;
coin->inactive = (uint32_t)time(NULL);
coin->bussock = LP_coinbus(busport);
coin->ctx = bitcoin_ctx();
if ( ctx == 0 )
ctx = bitcoin_ctx();
coin->ctx = ctx;
if ( assetname != 0 && strcmp(name,assetname) == 0 )
{
printf("%s is assetchain\n",symbol);
//printf("%s is assetchain\n",symbol);
coin->isassetchain = 1;
}
if ( strcmp(symbol,"KMD") == 0 || (assetname != 0 && assetname[0] != 0) )
@ -326,7 +328,9 @@ uint16_t LP_coininit(struct iguana_info *coin,char *symbol,char *name,char *asse
coin->noimportprivkey_flag = 1;
printf("truncate importprivkey for %s\n",symbol);
}
#ifndef FROM_JS
port = LP_userpass(coin->userpass,symbol,assetname,name,name2,confpath,port);
#endif
sprintf(coin->serverport,"127.0.0.1:%u",port);
return(port);
}
@ -434,7 +438,7 @@ struct iguana_info *LP_coincreate(cJSON *item)
}
} else coin->inactive = 0;
}
if ( coin != 0 && coin->inactive != 0 )
if ( 0 && coin != 0 && coin->inactive != 0 )
printf("LPnode.%d %s inactive.%u %p vs %p\n",IAMLP,coin->symbol,coin->inactive,assetname,name);
return(0);
}

16
iguana/exchanges/LP_commands.c

@ -104,6 +104,9 @@ goal(coin=*, val=<autocalc>)\n\
myprice(base, rel)\n\
enable(coin)\n\
disable(coin)\n\
notarizations(coin)\n\
parselog()\n\
statsdisp()\n\
getrawtransaction(coin, txid)\n\
inventory(coin)\n\
bestfit(rel, relvolume)\n\
@ -214,6 +217,10 @@ stop()\n\
{
return(LP_portfolio());
}
else if ( strcmp(method,"parselog") == 0 )
return(LP_statslog_parse());
else if ( strcmp(method,"statsdisp") == 0 )
return(LP_statslog_disp(0));
else if ( strcmp(method,"secretaddresses") == 0 )
{
uint8_t taddr,pubtype;
@ -443,13 +450,13 @@ stop()\n\
}
// received response
if ( strcmp(method,"postprice") == 0 )
retstr = LP_postprice_recv(argjson);
return(LP_postprice_recv(argjson));
else if ( strcmp(method,"postutxos") == 0 )
retstr = LP_postutxos_recv(argjson);
return(LP_postutxos_recv(argjson));
else if ( strcmp(method,"uitem") == 0 )
retstr = LP_uitem_recv(argjson);
return(LP_uitem_recv(argjson));
else if ( strcmp(method,"notify") == 0 )
retstr = LP_notify_recv(argjson);
return(LP_notify_recv(argjson));
// end received response
// public access, even from http
@ -508,6 +515,7 @@ stop()\n\
LP_listunspent_issue(coin,coinaddr,1);
if ( strcmp(coinaddr,ptr->smartaddr) == 0 && bits256_nonz(G.LP_privkey) != 0 )
{
printf("network invoked\n");
LP_privkey_init(-1,ptr,G.LP_privkey,G.LP_mypub25519);
//LP_smartutxos_push(ptr);
}

38
iguana/exchanges/LP_include.h

@ -21,8 +21,24 @@
#ifndef LP_INCLUDE_H
#define LP_INCLUDE_H
#ifdef FROM_JS
#include <emscripten.h>
#define sleep(x) emscripten_sleep((x) * 1000)
void emscripten_usleep(int32_t x);
#define usleep(x) emscripten_usleep(x)
// ./autogen.sh
// emconfigure ./configure CFLAGS="-s PTHREAD_POOL_SIZE=8 -s USE_PTHREADS=1 -O2"
// Edit src/core/sock.c and add here #include <limits.h> for INT_MAX support
// emmake make
// cp .libs/libnanomsg.a ~/SuperNET/OSlibs/js
#endif
//#define LP_STRICTPEERS
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL)
#define LP_AUTOTRADE_TIMEOUT 10
#define ELECTRUM_TIMEOUT 15
#define LP_MEMPOOL_TIMEINCR 10
#define LP_COMMAND_SENDSOCK NN_PUSH
#define LP_COMMAND_RECVSOCK NN_PULL
@ -34,17 +50,14 @@
#define MAINLOOP_PERSEC 100
#define MAX_PSOCK_PORT 60000
#define MIN_PSOCK_PORT 10000
#define LP_MEMPOOL_TIMEINCR 10
#define LP_GETINFO_INCR 30
#define LP_ORDERBOOK_DURATION 120
#define LP_HTTP_TIMEOUT 2 // 1 is too small due to edge cases of time(NULL)
#define LP_MAXPEER_ERRORS 3
#define LP_MINPEER_GOOD 20
#define LP_PEERGOOD_ERRORDECAY 0.9
#define LP_SWAPSTEP_TIMEOUT 30
#define LP_AUTOTRADE_TIMEOUT 15
#define LP_MIN_TXFEE 10000
#define LP_MINVOL 20
#define LP_MINCLIENTVOL 50
@ -72,7 +85,7 @@
#define LP_RESERVETIME 60
#define LP_AVETXSIZE 256
#define LP_CACHEDURATION 60
#define BASILISK_DEFAULT_NUMCONFIRMS 3
#define BASILISK_DEFAULT_NUMCONFIRMS 1
#define BASILISK_DEFAULT_MAXCONFIRMS 6
#define DEX_SLEEP 3
#define BASILISK_KEYSIZE ((int32_t)(2*sizeof(bits256)+sizeof(uint32_t)*2))
@ -185,14 +198,15 @@ struct iguana_info
portable_mutex_t txmutex,addrmutex; struct LP_transaction *transactions; struct LP_address *addresses;
uint64_t txfee;
int32_t longestchain,firstrefht,firstscanht,lastscanht,bussock,height; uint16_t busport;
uint32_t lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,unspenttime,obooktime;
uint32_t lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime;
uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms;
char symbol[16],smartaddr[64],userpass[1024],serverport[128],lastunspent[64];
char symbol[16],smartaddr[64],userpass[1024],serverport[128];
// portfolio
double price_kmd,force,perc,goal,goalperc,relvolume,rate;
void *electrum; void *ctx;
uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB;
uint8_t pubkey33[33];
bits256 cachedtxid; uint8_t *cachedtxiddata; int32_t cachedtxidlen;
};
struct _LP_utxoinfo { bits256 txid; uint64_t value; int32_t vout,height; };
@ -231,7 +245,8 @@ struct LP_address
struct LP_address_utxo *utxos;
bits256 pubkey;
int64_t balance,total;
uint32_t timestamp,n;
uint32_t timestamp,n,unspenttime;
int32_t unspentheight;
char coinaddr[40];
uint8_t pubsecp[33],pad;
};
@ -250,7 +265,7 @@ struct LP_quoteinfo
{
struct basilisk_request R;
bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey;
uint64_t satoshis,txfee,destsatoshis,desttxfee;
uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid;
uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,feevout,pair;
char srccoin[16],coinaddr[64],destcoin[16],destaddr[64],gui[64];
};
@ -270,7 +285,7 @@ struct basilisk_swap
bits256 privkeys[INSTANTDEX_DECKSIZE];
struct basilisk_swapmessage *messages; int32_t nummessages,sentflag;
char Bdeposit[64],Bpayment[64];
uint64_t otherdeck[INSTANTDEX_DECKSIZE][2],deck[INSTANTDEX_DECKSIZE][2];
uint64_t aliceid,otherdeck[INSTANTDEX_DECKSIZE][2],deck[INSTANTDEX_DECKSIZE][2];
uint8_t persistent_pubkey33[33],persistent_other33[33],changermd160[20],pad[15],verifybuf[100000];
};
@ -287,6 +302,8 @@ struct LP_pubkeyinfo
int32_t LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item);
int32_t LP_pubkey_sigadd(cJSON *item,uint32_t timestamp,bits256 priv,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp);
int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson);
struct LP_address *LP_address(struct iguana_info *coin,char *coinaddr);
void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint64_t *valuep,uint8_t *data,int32_t datalen,int32_t vout);
void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx);
uint32_t basilisk_requestid(struct basilisk_request *rp);
@ -304,6 +321,7 @@ struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port);
uint64_t LP_value_extract(cJSON *obj,int32_t addinterest);
int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vout);
char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen);
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);
@ -332,7 +350,7 @@ int32_t LP_mempoolscan(char *symbol,bits256 searchtxid);
int32_t LP_txheight(struct iguana_info *coin,bits256 txid);
int32_t LP_numpeers();
uint64_t LP_KMDvalue(struct iguana_info *coin,uint64_t balance);
int32_t LP_address_utxoadd(struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight);
int32_t LP_address_utxoadd(char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight);
void LP_smartutxos_push(struct iguana_info *coin);
cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrumret);
cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout);

177
iguana/exchanges/LP_nativeDEX.c

@ -18,11 +18,14 @@
// LP_nativeDEX.c
// marketmaker
//
// ZEC LP_transaction_fromdata mismatched txid e2a3eebcf5bef6fe63296f53ce35d2f2c6e3b29c7c907da4171a580f50c93c74 vs 1ed5c15bbf991ff42c73b1492d1bb50e91d1d731c86cb3058405cff95cd9bd70
//0200000000018c343500000000001976a9141462c3dd3f936d595c9af55978003b27c250441f88ac000000000100000000000000009c5b3500000000005a6c707fc604699d0166b2a750c8a2ff21c8aed0b6e6fe160f651da8cf865c2aa079a7f71f7782604785e1f9a7300cd6c89ca50b5d91f6c0ae5f293ea5a72e62bb0f05da84510e6431c5d4be7a372221265fd98dbccee11f5ed064c5afaa19eff27fd12c30a7a5205f70c99f22b7090c00048c1c9767eea1c79e54ed60c08c138758ad73d43f2692fcb05f data2json n.44 vs len.
// process stats.log local file -> map of realtime activity!
// select oldest utxo first
// handles <-> pubkeys, deal with offline pubkeys, reputations, bonds etc.
//
// verify portfolio, pricearray, interest to KMD withdraw, reliable networking
// select oldest utxo first
// verify portfolio, pricearray, interest to KMD withdraw
// dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections
// bigendian architectures need to use little endian for sighash calcs
@ -47,7 +50,7 @@ char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.25
//uint32_t LP_deadman_switch;
uint16_t LP_fixed_pairport,LP_publicport;
uint32_t LP_lastnonce;
uint32_t LP_lastnonce,LP_counter;
int32_t LP_mybussock = -1;
int32_t LP_mypubsock = -1;
int32_t LP_mypullsock = -1;
@ -99,6 +102,7 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_
#include "LP_prices.c"
#include "LP_scan.c"
#include "LP_transaction.c"
#include "LP_stats.c"
#include "LP_remember.c"
#include "LP_swap.c"
#include "LP_peers.c"
@ -271,16 +275,21 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
while ( nonz < maxdepth && recvlen > 0 )
{
nonz++;
#ifndef FROM_JS
memset(&pfd,0,sizeof(pfd));
pfd.fd = sock;
pfd.events = NN_POLLIN;
if ( nn_poll(&pfd,1,1) != 1 )
break;
#endif
if ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 )
{
methodstr[0] = 0;
if ( 1 )
{
#ifdef FROM_JS
printf("%s RECV.(%s)\n",typestr,(char *)ptr);
#endif
cJSON *recvjson; //char *mstr,*cstr;
if ( (recvjson= cJSON_Parse((char *)ptr)) != 0 )
{
@ -293,6 +302,9 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
free_json(recvjson);
}
}
#ifdef FROM_JS
else printf("%s got recv.%d\n",typestr,recvlen);
#endif
double millis = OS_milliseconds();
if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,ptr,recvlen,sock)) != 0 )
free(retstr);
@ -391,12 +403,12 @@ void utxosQ_loop(void *myipaddr)
int32_t LP_utxos_sync(struct LP_peerinfo *peer)
{
int32_t i,j,n=0,m,v,posted=0; bits256 txid; cJSON *array,*item,*item2,*array2,*array3; uint64_t total,total2,metric; struct iguana_info *coin,*ctmp; struct LP_address *ap; char *retstr,*retstr2,*coinaddr;
int32_t i,j,n=0,m,v,posted=0; bits256 txid; cJSON *array,*item,*item2,*array2; uint64_t total,total2; struct iguana_info *coin,*ctmp; char *retstr,*retstr2;
if ( strcmp(peer->ipaddr,LP_myipaddr) == 0 )
return(0);
HASH_ITER(hh,LP_coins,coin,ctmp)
{
if ( IAMLP == 0 && coin->inactive != 0 )//|| (coin->electrum != 0 && coin->obooktime == 0) )
if ( IAMLP == 0 && coin->inactive != 0 )
continue;
if ( coin->smartaddr[0] == 0 )
continue;
@ -468,50 +480,13 @@ int32_t LP_utxos_sync(struct LP_peerinfo *peer)
}
free_json(array);
}
if ( 0 && (retstr= issue_LP_listunspent(peer->ipaddr,peer->port,coin->symbol,"")) != 0 )
{
if ( (array2= cJSON_Parse(retstr)) != 0 )
{
if ( (m= cJSON_GetArraySize(array2)) > 0 )
{
for (j=0; j<m; j++)
{
item = jitem(array2,j);
if ( (coinaddr= jfieldname(item)) != 0 )
{
metric = j64bits(item,coinaddr);
//printf("(%s) -> %.8f n.%d\n",coinaddr,dstr(metric>>16),(uint16_t)metric);
if ( (ap= LP_addressfind(coin,coinaddr)) == 0 || _LP_unspents_metric(ap->total,ap->n) != metric )
{
if ( ap == 0 || ap->n < (metric & 0xffff) )
{
if ( (retstr2= issue_LP_listunspent(peer->ipaddr,peer->port,coin->symbol,coinaddr)) != 0 )
{
if ( (array3= cJSON_Parse(retstr2)) != 0 )
{
LP_unspents_array(coin,coinaddr,array3);
//printf("pulled.(%s)\n",retstr2);
free_json(array3);
}
free(retstr2);
}
} //else printf("wait for %s to pull %d vs %d\n",peer->ipaddr,ap!=0?ap->n:-1,(uint16_t)metric);
}
}
}
}
free_json(array2);
}
//printf("processed.(%s)\n",retstr);
free(retstr);
}
}
return(posted);
}
void LP_coinsloop(void *_coins)
{
struct iguana_info *coin,*ctmp; bits256 zero; int32_t j,nonz; char *coins = _coins;
struct LP_address *ap=0,*atmp; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65]; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t oldht,j,nonz; char *coins = _coins;
while ( 1 )
{
nonz = 0;
@ -530,8 +505,40 @@ void LP_coinsloop(void *_coins)
memset(&zero,0,sizeof(zero));
if ( coin->inactive != 0 )
continue;
if ( coin->electrum != 0 )
if ( (ep= coin->electrum) != 0 )
{
if ( (backupep= ep->prev) == 0 )
backupep = ep;
HASH_ITER(hh,coin->addresses,ap,atmp)
{
DL_FOREACH_SAFE(ap->utxos,up,tmp)
{
if ( up->U.height > 0 && up->spendheight < 0 )
{
if ( up->SPV == 0 )
{
nonz++;
up->SPV = LP_merkleproof(coin,backupep,up->U.txid,up->U.height);
if ( 0 && up->SPV > 0 )
printf("%s %s: SPV.%d\n",coin->symbol,bits256_str(str,up->U.txid),up->SPV);
}
else if ( up->SPV == -1 )
{
nonz++;
printf("SPV failure for %s %s\n",coin->symbol,bits256_str(str,up->U.txid));
oldht = up->U.height;
LP_txheight_check(coin,ap->coinaddr,up);
if ( oldht != up->U.height )
up->SPV = LP_merkleproof(coin,backupep,up->U.txid,up->U.height);
if ( up->SPV <= 0 )
up->SPV = -2;
else printf("%s %s: corrected SPV.%d\n",coin->symbol,bits256_str(str,up->U.txid),up->SPV);
}
}
}
}
continue;
}
if ( coin->firstrefht == 0 )
continue;
else if ( coin->firstscanht == 0 )
@ -556,6 +563,7 @@ void LP_coinsloop(void *_coins)
coin->lastscanht = coin->firstscanht;
continue;
}
nonz++;
//if ( (coin->lastscanht % 1000) == 0 )
printf("[%s]: %s ref.%d scan.%d to %d, longest.%d\n",coins,coin->symbol,coin->firstrefht,coin->firstscanht,coin->lastscanht,coin->longestchain);
for (j=0; j<100; j++)
@ -569,8 +577,6 @@ void LP_coinsloop(void *_coins)
if ( coin->lastscanht == coin->longestchain+1 )
break;
}
nonz++;
continue;
}
if ( nonz == 0 )
usleep(1000);
@ -606,21 +612,24 @@ int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int
if ( strcmp(peer->ipaddr,myipaddr) != 0 )
{
nonz++;
#ifndef FROM_JS
LP_peersquery(mypeer,pubsock,peer->ipaddr,peer->port,myipaddr,myport);
#endif
peer->diduquery = 0;
LP_peer_pricesquery(peer);
//LP_utxos_sync(peer);
LP_utxos_sync(peer);
needpings++;
}
peer->lastpeers = now;
}
if ( peer->needping != 0 )
{
LP_utxos_sync(peer);
peer->diduquery = now;
nonz++;
#ifndef FROM_JS
if ( (retstr= issue_LP_notify(peer->ipaddr,peer->port,"127.0.0.1",0,numpeers,G.LP_sessionid,G.LP_myrmd160str,G.LP_mypub25519)) != 0 )
free(retstr);
#endif
peer->needping = 0;
needpings++;
}
@ -633,7 +642,7 @@ int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int
LP_smartutxos_push(coin);
coin->addr_listunspent_requested = 0;
}
if ( time(NULL) > coin->lastgetinfo+LP_GETINFO_INCR )
if ( coin->inactive == 0 && time(NULL) > coin->lastgetinfo+LP_GETINFO_INCR )
{
nonz++;
if ( (height= LP_getheight(coin)) > coin->longestchain )
@ -716,6 +725,8 @@ void LP_pubkeysloop(void *ctx)
sleep(10);
while ( 1 )
{
LP_counter += 100;
//printf("LP_pubkeysloop %d\n",LP_counter);
LP_notify_pubkeys(ctx,LP_mypubsock);
sleep(60);
}
@ -726,6 +737,8 @@ void LP_privkeysloop(void *ctx)
sleep(20);
while ( 1 )
{
LP_counter += 1000;
//printf("LP_privkeysloop %u\n",LP_counter);
LP_privkey_updates(ctx,LP_mypubsock,0);
sleep(60);
}
@ -737,6 +750,8 @@ void LP_swapsloop(void *ignore)
sleep(50);
while ( 1 )
{
LP_counter += 10000;
//printf("LP_swapsloop %u\n",LP_counter);
if ( (retstr= basilisk_swapentry(0,0)) != 0 )
free(retstr);
sleep(600);
@ -764,12 +779,13 @@ void LP_reserved_msgs(void *ignore)
{
portable_mutex_lock(&LP_reservedmutex);
num_Reserved_msgs--;
//printf("%d BROADCASTING RESERVED.(%s)\n",num_Reserved_msgs,Reserved_msgs[num_Reserved_msgs]);
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[num_Reserved_msgs]);
Reserved_msgs[num_Reserved_msgs] = 0;
portable_mutex_unlock(&LP_reservedmutex);
}
}
if ( ignore == 0 )
break;
usleep(3000);
}
}
@ -777,6 +793,7 @@ 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) )
{
@ -784,6 +801,10 @@ 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;
@ -854,7 +875,9 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
portable_mutex_init(&LP_butxomutex);
portable_mutex_init(&LP_reservedmutex);
portable_mutex_init(&LP_nanorecvsmutex);
myipaddr = clonestr("127.0.0.1");
#ifndef _WIN32
#ifndef FROM_JS
if ( system("curl -s4 checkip.amazonaws.com > myipaddr") == 0 )
{
char ipfname[64];
@ -868,7 +891,8 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
} else printf("error getting myipaddr\n");
} else printf("error issuing curl\n");
#else
myipaddr = clonestr("127.0.0.1");
IAMLP = 0;
#endif
#endif
if ( IAMLP != 0 )
{
@ -903,7 +927,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
printf("canbind.%d my command address is (%s) pullsock.%d pullport.%u\n",LP_canbind,pushaddr,LP_mypullsock,mypullport);
if ( (coinsjson= jobj(argjson,"coins")) == 0 )
{
if ( (coins_str= OS_filestr(&filesize,"coins.json")) != 0 )
if ( (coins_str= OS_filestr(&filesize,"coins.json")) != 0 || (coins_str= OS_filestr(&filesize,"exchanges/coins.json")) != 0 )
{
unstringify(coins_str);
printf("UNSTRINGIFIED.(%s)\n",coins_str);
@ -914,12 +938,13 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
}
if ( coinsjson == 0 )
{
printf("no coins object or coins file, must abort\n");
printf("no coins object or coins.json file, must abort\n");
exit(-1);
}
LP_initcoins(ctx,pubsock,coinsjson);
G.waiting = 1;
LP_passphrase_init(passphrase,jstr(argjson,"gui"));
#ifndef FROM_JS
if ( IAMLP != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)myipaddr) != 0 )
{
printf("error launching LP_psockloop for (%s)\n",myipaddr);
@ -985,10 +1010,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
printf("error launching LP_swapsloop for port.%u\n",myport);
exit(-1);
}
//if ( (retstr= basilisk_swapentry(0,0)) != 0 )
// free(retstr);
int32_t nonz;
printf("start mainloop\n");
while ( 1 )
{
nonz = 0;
@ -1005,7 +1027,48 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
else if ( IAMLP == 0 )
usleep(1000);
}
#endif
}
#ifdef FROM_JS
void emscripten_usleep(int32_t x)
{
}
void LP_fromjs_iter()
{
static void *ctx;
if ( G.initializing != 0 )
{
printf("LP_fromjs_iter during G.initializing, skip\n");
return;
}
if ( ctx == 0 )
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();
LP_utxosQ_process();
LP_nanomsg_recvs(ctx);
LP_mainloop_iter(ctx,LP_myipaddr,0,LP_mypubsock,LP_publicaddr,LP_RPCPORT);
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

18
iguana/exchanges/LP_network.c

@ -30,7 +30,7 @@ uint16_t Numpsocks,Psockport = MIN_PSOCK_PORT;
char *nanomsg_transportname(int32_t bindflag,char *str,char *ipaddr,uint16_t port)
{
sprintf(str,"tcp://%s:%u",bindflag == 0 ? ipaddr : "*",port); // ws is worse
sprintf(str,"ws://%s:%u",bindflag == 0 ? ipaddr : "*",port); // ws is worse
return(str);
}
@ -287,7 +287,21 @@ void LP_broadcast_finish(int32_t pubsock,char *base,char *rel,uint8_t *msg,cJSON
// add signature here
msg = (void *)jprint(argjson,0);
msglen = (int32_t)strlen((char *)msg) + 1;
#ifdef FROM_JS
int32_t sentbytes,sock,peerind,maxind;
if ( (maxind= LP_numpeers()) > 0 )
peerind = (rand() % maxind) + 1;
else peerind = 1;
sock = LP_peerindsock(&peerind);
if ( sock >= 0 )
{
if ( (sentbytes= nn_send(sock,msg,msglen,0)) != msglen )
printf("LP_send sent %d instead of %d\n",sentbytes,msglen);
else printf("sent %d bytes of %d to sock.%d\n",sentbytes,msglen,sock);
} else printf("couldnt get valid sock\n");
#else
LP_queuesend(crc32,-1,base,rel,msg,msglen);
#endif
} else LP_queuesend(crc32,pubsock,base,rel,msg,msglen);
free(msg);
}
@ -332,7 +346,7 @@ void LP_broadcast_message(int32_t pubsock,char *base,char *rel,bits256 destpub25
// printf("finished %u\n",crc32);
} // else printf("no valid method in (%s)\n",msgstr);
free_json(argjson);
} else printf("couldnt parse (%s)\n",msgstr);
} else printf("couldnt parse %p (%s)\n",msgstr,msgstr);
}
else
{

159
iguana/exchanges/LP_ordermatch.c

@ -77,7 +77,7 @@ int32_t LP_quote_checkmempool(struct LP_quoteinfo *qp,struct LP_utxoinfo *autxo,
double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,struct LP_quoteinfo *qp,int32_t iambob)
{
double qprice=0.; uint64_t txfee,desttxfee,srcvalue=0,srcvalue2=0,destvalue=0,destvalue2=0;
double qprice=0.; char str[65]; cJSON *txout; uint64_t txfee,desttxfee,srcvalue=0,srcvalue2=0,destvalue=0,destvalue2=0;
printf(">>>>>>> quote satoshis.(%.8f %.8f) %s %.8f -> %s %.8f\n",dstr(qp->satoshis),dstr(qp->destsatoshis),qp->srccoin,dstr(qp->satoshis),qp->destcoin,dstr(qp->destsatoshis));
if ( butxo != 0 )
{
@ -86,6 +86,20 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
printf("bob not eligible %s\n",jprint(LP_quotejson(qp),1));
return(-2);
}
if ( (txout= LP_gettxout(qp->srccoin,qp->coinaddr,qp->txid,qp->vout)) != 0 )
free_json(txout);
else
{
printf("%s %s payment %s/v%d is spent\n",qp->srccoin,qp->coinaddr,bits256_str(str,qp->txid),qp->vout);
return(-21);
}
if ( (txout= LP_gettxout(qp->srccoin,qp->coinaddr,qp->txid2,qp->vout2)) != 0 )
free_json(txout);
else
{
printf("%s %s deposit %s/v%d is spent\n",qp->srccoin,qp->coinaddr,bits256_str(str,qp->txid2),qp->vout2);
return(-22);
}
if ( bits256_cmp(butxo->deposit.txid,qp->txid2) != 0 || butxo->deposit.vout != qp->vout2 )
{
char str[65],str2[65]; printf("%s != %s v%d != %d\n",bits256_str(str,butxo->deposit.txid),bits256_str(str2,qp->txid2),butxo->deposit.vout,qp->vout2);
@ -97,10 +111,27 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
return(-7);
}
}
if ( autxo != 0 && LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 )
if ( autxo != 0 )
{
char str[65]; printf("alice not eligible (%.8f %.8f) %s/v%d\n",dstr(destvalue),dstr(destvalue2),bits256_str(str,qp->feetxid),qp->feevout);
return(-3);
if ( LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 )
{
char str[65]; printf("alice not eligible (%.8f %.8f) %s/v%d\n",dstr(destvalue),dstr(destvalue2),bits256_str(str,qp->feetxid),qp->feevout);
return(-3);
}
if ( (txout= LP_gettxout(qp->destcoin,qp->destaddr,qp->desttxid,qp->destvout)) != 0 )
free_json(txout);
else
{
printf("%s %s Apayment %s/v%d is spent\n",qp->destcoin,qp->destaddr,bits256_str(str,qp->desttxid),qp->destvout);
return(-23);
}
if ( (txout= LP_gettxout(qp->destcoin,qp->destaddr,qp->feetxid,qp->feevout)) != 0 )
free_json(txout);
else
{
printf("%s %s dexfee %s/v%d is spent\n",qp->destcoin,qp->destaddr,bits256_str(str,qp->feetxid),qp->feevout);
return(-24);
}
}
if ( LP_quote_checkmempool(qp,autxo,butxo) < 0 )
return(-4);
@ -200,7 +231,7 @@ int32_t LP_nanobind(void *ctx,char *pairstr)
int32_t LP_nearest_utxovalue(struct iguana_info *coin,char *coinaddr,struct LP_address_utxo **utxos,int32_t n,uint64_t targetval)
{
int32_t i,oldht,mini = -1; struct LP_address_utxo *up; struct electrum_info *backupep=0,*ep; char str[65]; int64_t dist; uint64_t mindist = (1LL << 60);
int32_t i,mini = -1; struct LP_address_utxo *up; struct electrum_info *backupep=0,*ep; int64_t dist; uint64_t mindist = (1LL << 60);
if ( (ep= coin->electrum) != 0 )
{
if ( (backupep= ep->prev) == 0 )
@ -215,25 +246,6 @@ int32_t LP_nearest_utxovalue(struct iguana_info *coin,char *coinaddr,struct LP_a
//printf("nearest i.%d target %.8f val %.8f dist %.8f mindist %.8f mini.%d spent.%d\n",i,dstr(targetval),dstr(up->U.value),dstr(dist),dstr(mindist),mini,up->spendheight);
if ( up->spendheight <= 0 )
{
if ( coin->electrum != 0 )
{
if ( up->SPV == 0 || up->SPV == -1 )
up->SPV = LP_merkleproof(coin,backupep,up->U.txid,up->U.height);
if ( up->SPV == 0 || up->SPV == -1 )
{
printf("SPV failure for %s %s\n",coin->symbol,bits256_str(str,up->U.txid));
if ( up->SPV == -1 )
{
oldht = up->U.height;
LP_txheight_check(coin,coinaddr,up);
if ( oldht != up->U.height )
up->SPV = LP_merkleproof(coin,backupep,up->U.txid,up->U.height);
if ( up->SPV < 0 )
up->SPV = -2;
}
continue;
} //else printf("%s %s: SPV.%d\n",coin->symbol,bits256_str(str,up->U.txid),up->SPV);
}
if ( (coin->electrum == 0 || up->SPV > 0) && dist >= 0 && dist < mindist )
{
//printf("(%.8f %.8f %.8f).%d ",dstr(up->U.value),dstr(dist),dstr(mindist),mini);
@ -260,7 +272,7 @@ struct LP_utxoinfo *LP_address_utxopair(int32_t iambob,struct LP_address_utxo **
struct LP_address *ap; uint64_t targetval,targetval2; int32_t m,mini; struct LP_address_utxo *up,*up2; struct LP_utxoinfo *utxo = 0;
if ( coin != 0 && (ap= LP_addressfind(coin,coinaddr)) != 0 )
{
if ( (m= LP_address_utxo_ptrs(iambob,utxos,max,ap,coinaddr)) > 1 )
if ( (m= LP_address_utxo_ptrs(coin,iambob,utxos,max,ap,coinaddr)) > 1 )
{
targetval = LP_basesatoshis(relvolume,price,txfee,desttxfee);
if ( 0 )
@ -359,8 +371,8 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,struct LP_utxoinfo *utxo,cJ
retjson = LP_quotejson(qp);
jaddstr(retjson,"method","connected");
jaddstr(retjson,"pair",pairstr);
jaddnum(retjson,"requestid",qp->R.requestid);
jaddnum(retjson,"quoteid",qp->R.quoteid);
//jaddnum(retjson,"requestid",qp->R.requestid);
//jaddnum(retjson,"quoteid",qp->R.quoteid);
// LP_addsig
char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,utxo->S.otherpubkey));
LP_reserved_msg(base,rel,utxo->S.otherpubkey,jprint(retjson,0));
@ -396,6 +408,7 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
return(clonestr("{\"error\":\"cant find alice utxopair\"}"));
}
price = 0.;
qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout);
LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout;
return(clonestr("{\"result\":\"success\"}"));
@ -462,30 +475,9 @@ char *LP_connectedalice(cJSON *argjson) // alice
butxo = &B;
memset(butxo,0,sizeof(*butxo));
LP_abutxo_set(0,butxo,&Q);
/*if ( (butxo= LP_utxopairfind(1,Q.txid,Q.vout,Q.txid2,Q.vout2)) == 0 )
{
value = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid,Q.vout);
value2 = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid2,Q.vout2);
if ( value == 0 || value2 == 0 )
{
printf("zero value %.8f or value2 %.8f\n",dstr(value),dstr(value2));
return(clonestr("{\"error\":\"spent txid or txid2 for bob?\"}"));
}
if ( (butxo= LP_utxoadd(1,Q.srccoin,Q.txid,Q.vout,value,Q.txid2,Q.vout2,value2,Q.coinaddr,Q.srchash,LP_gui,0)) == 0 )
{
printf("cant find or create butxo\n");
return(clonestr("{\"error\":\"cant find or create butxo\"}"));
}
if ( value < Q.satoshis )
{
printf("butxo value %.8f less satoshis %.8f\n",dstr(value),dstr(Q.satoshis));
return(clonestr("{\"error\":\"butxo value less than satoshis\"}"));
}
}*/
if ( (qprice= LP_quote_validate(autxo,butxo,&Q,0)) <= SMALLVAL )
{
LP_availableset(autxo);
//G.LP_pendingswaps--;
printf("quote validate error %.0f\n",qprice);
return(clonestr("{\"error\":\"quote validation error\"}"));
}
@ -493,16 +485,12 @@ char *LP_connectedalice(cJSON *argjson) // alice
{
printf("this node has no price for %s/%s (%.8f %.8f)\n",Q.destcoin,Q.srccoin,bid,ask);
LP_availableset(autxo);
//G.LP_pendingswaps--;
return(clonestr("{\"error\":\"no price set\"}"));
}
printf("%s/%s bid %.8f ask %.8f values %.8f %.8f\n",Q.srccoin,Q.destcoin,bid,ask,dstr(butxo->payment.value),dstr(butxo->deposit.value));
price = bid;
if ( (coin= LP_coinfind(Q.destcoin)) == 0 )
{
//G.LP_pendingswaps--;
return(clonestr("{\"error\":\"cant get alicecoin\"}"));
}
Q.privkey = LP_privkey(Q.destaddr,coin->taddr);
if ( bits256_nonz(Q.privkey) != 0 )//&& Q.quotetime >= Q.timestamp-3 )
{
@ -522,16 +510,15 @@ char *LP_connectedalice(cJSON *argjson) // alice
printf("alice pairstr.(%s) pairsock.%d\n",pairstr,pairsock);
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)swap) == 0 )
{
retjson = LP_quotejson(&Q);
jaddstr(retjson,"result","success");
jadd(retjson,"trade",LP_quotejson(&Q));
jaddnum(retjson,"requestid",Q.R.requestid);
jaddnum(retjson,"quoteid",Q.R.quoteid);
//jaddnum(retjson,"requestid",Q.R.requestid);
//jaddnum(retjson,"quoteid",Q.R.quoteid);
} else jaddstr(retjson,"error","couldnt aliceloop");
} else printf("connect error %s\n",nn_strerror(nn_errno()));
printf("connected result.(%s)\n",jprint(retjson,0));
if ( jobj(retjson,"error") != 0 )
LP_availableset(autxo);
else G.LP_pendingswaps++;
return(jprint(retjson,1));
}
else
@ -570,7 +557,7 @@ int32_t LP_listunspent_both(char *symbol,char *coinaddr,int32_t fullflag)
value = LP_value_extract(item,0);
height = LP_txheight(coin,txid);
//char str[65]; printf("LP_listunspent_both: %s/v%d ht.%d %.8f\n",bits256_str(str,txid),v,height,dstr(value));
LP_address_utxoadd(coin,coinaddr,txid,v,value,height,-1);
LP_address_utxoadd("LP_listunspent_both",coin,coinaddr,txid,v,value,height,-1);
}
}
}
@ -605,7 +592,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
LP_quoteparse(&Q,argjson);
LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-2*Q.txfee,Q.destcoin,Q.destsatoshis-2*Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector);
LP_tradecommand_log(argjson);
printf("LP_tradecommand: check received method %s\n",method);
printf("LP_tradecommand: check received method %s aliceid.%llx\n",method,(long long)Q.aliceid);
retval = 1;
if ( strcmp(method,"reserved") == 0 )
{
@ -645,6 +632,11 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
printf("{\"error\":\"GAME can only be alice coin\"}\n");
return(retval);
}
if ( strcmp(Q.coinaddr,coin->smartaddr) != 0 )
{
printf("bob is patching Q.coinaddr %s mismatch != %s\n",Q.coinaddr,coin->smartaddr);
strcpy(Q.coinaddr,coin->smartaddr);
}
price = ask;
autxo = &A;
butxo = &B;
@ -688,7 +680,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
butxo = LP_utxopairfind(1,Q.txid,Q.vout,Q.txid2,Q.vout2);
if ( butxo == 0 || bits256_cmp(Q.txid,butxo->payment.txid) != 0 || bits256_cmp(Q.txid2,butxo->deposit.txid) != 0 )
{
printf("null butxo.%p case\n",butxo);
printf("%s %s null butxo.%p case\n",Q.srccoin,Q.coinaddr,butxo);
value = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid,Q.vout);
value2 = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid2,Q.vout2);
butxo = LP_utxoadd(1,Q.srccoin,Q.txid,Q.vout,value,Q.txid2,Q.vout2,value2,Q.coinaddr,Q.srchash,LP_gui,0);
@ -771,7 +763,7 @@ struct LP_utxoinfo *LP_ordermatch_iter(struct LP_address_utxo **utxos,int32_t ma
struct LP_utxoinfo *LP_buyutxo(double *ordermatchpricep,int64_t *bestsatoshisp,int64_t *bestdestsatoshisp,struct LP_utxoinfo *autxo,char *base,double maxprice,int32_t duration,uint64_t txfee,uint64_t desttxfee,char *gui,bits256 *avoids,int32_t numavoids)
{
bits256 pubkey; char *obookstr,coinaddr[64]; cJSON *orderbook,*asks,*item; int32_t maxiters,i,j,numasks,max = 10000; struct LP_address_utxo **utxos; double price; struct LP_pubkeyinfo *pubp; uint64_t asatoshis; struct iguana_info *basecoin; struct LP_utxoinfo *bestutxo = 0;
bits256 pubkey; char *obookstr,coinaddr[64]; cJSON *orderbook,*asks,*item; int32_t maxiters,i,j,numasks,max; struct LP_address_utxo **utxos; double price; struct LP_pubkeyinfo *pubp; uint64_t asatoshis; struct iguana_info *basecoin; struct LP_utxoinfo *bestutxo = 0;
maxiters = 100;
*ordermatchpricep = 0.;
*bestsatoshisp = *bestdestsatoshisp = 0;
@ -780,6 +772,9 @@ struct LP_utxoinfo *LP_buyutxo(double *ordermatchpricep,int64_t *bestsatoshisp,i
duration = LP_ORDERBOOK_DURATION;
if ( maxprice <= 0. || LP_priceinfofind(base) == 0 || basecoin == 0 )
return(0);
if ( basecoin->electrum == 0 )
max = 1000;
else max = 20;
utxos = calloc(max,sizeof(*utxos));
LP_txfees(&txfee,&desttxfee,base,autxo->coin);
printf("LP_buyutxo maxprice %.8f relvol %.8f %s/%s %.8f %.8f\n",maxprice,dstr(autxo->S.satoshis),base,autxo->coin,dstr(txfee),dstr(desttxfee));
@ -806,20 +801,30 @@ struct LP_utxoinfo *LP_buyutxo(double *ordermatchpricep,int64_t *bestsatoshisp,i
if ( bits256_cmp(pubkey,G.LP_mypub25519) != 0 && (pubp= LP_pubkeyadd(pubkey)) != 0 )
{
bitcoin_address(coinaddr,basecoin->taddr,basecoin->pubtype,pubp->rmd160,sizeof(pubp->rmd160));
LP_listunspent_query(base,coinaddr);
LP_listunspent_both(base,coinaddr,1);
asatoshis = autxo->S.satoshis;
for (j=0; j<maxiters; j++)
/*if ( basecoin->electrum != 0 )
{
if ( (bestutxo= LP_ordermatch_iter(utxos,max,ordermatchpricep,bestsatoshisp,bestdestsatoshisp,basecoin,coinaddr,asatoshis,price,txfee,desttxfee,pubp->pubkey,gui)) != 0 )
price *= 1.01;
*ordermatchpricep = price;
*bestsatoshisp = LP_basesatoshis(dstr(asatoshis),price,txfee,desttxfee);
*bestdestsatoshisp = asatoshis;
}
else*/
{
LP_listunspent_query(base,coinaddr);
LP_listunspent_both(base,coinaddr,1);
for (j=0; j<maxiters; j++)
{
printf("j.%d/%d ordermatch %.8f best satoshis %.8f destsatoshis %.8f txfees (%.8f %.8f)\n",j,maxiters,price,dstr(*bestsatoshisp),dstr(*bestdestsatoshisp),dstr(txfee),dstr(desttxfee));
break;
if ( (bestutxo= LP_ordermatch_iter(utxos,max,ordermatchpricep,bestsatoshisp,bestdestsatoshisp,basecoin,coinaddr,asatoshis,price,txfee,desttxfee,pubp->pubkey,gui)) != 0 )
{
printf("j.%d/%d ordermatch %.8f best satoshis %.8f destsatoshis %.8f txfees (%.8f %.8f)\n",j,maxiters,price,dstr(*bestsatoshisp),dstr(*bestdestsatoshisp),dstr(txfee),dstr(desttxfee));
break;
}
asatoshis = (asatoshis / 64) * 63;
}
asatoshis = (asatoshis / 64) * 63;
if ( j < maxiters )
break;
}
if ( j < maxiters )
break;
} else printf("self trading or blacklisted peer\n");
}
else
@ -844,7 +849,11 @@ struct LP_utxoinfo *LP_buyutxo(double *ordermatchpricep,int64_t *bestsatoshisp,i
char *LP_autobuy(void *ctx,char *myipaddr,int32_t mypubsock,char *base,char *rel,double maxprice,double relvolume,int32_t timeout,int32_t duration,char *gui,uint32_t nonce)
{
uint64_t desttxfee,txfee; uint32_t lastnonce; int32_t i,maxiters,numpubs = 0; int64_t bestsatoshis=0,destsatoshis,bestdestsatoshis=0; struct LP_utxoinfo *autxo,*bestutxo = 0; double qprice,ordermatchprice=0.; struct LP_quoteinfo Q; bits256 pubkeys[100];
uint64_t desttxfee,txfee; uint32_t lastnonce; int32_t i,maxiters,numpubs = 0; int64_t bestsatoshis=0,destsatoshis,bestdestsatoshis=0; struct iguana_info *basecoin,*relcoin; struct LP_utxoinfo *autxo,*bestutxo = 0; double qprice,ordermatchprice=0.; struct LP_quoteinfo Q; bits256 pubkeys[100];
basecoin = LP_coinfind(base);
relcoin = LP_coinfind(rel);
if ( basecoin == 0 || basecoin->inactive != 0 || relcoin == 0 || relcoin->inactive != 0 )
return(clonestr("{\"error\":\"base or rel not found or inactive\"}"));
if ( LP_aliceonly(base) > 0 )
return(clonestr("{\"error\":\"GAME can only be alice coin\"}"));
printf("LP_autobuy %s/%s price %.8f vol %.8f nonce %u\n",base,rel,maxprice,relvolume,nonce);
@ -858,6 +867,16 @@ char *LP_autobuy(void *ctx,char *myipaddr,int32_t mypubsock,char *base,char *rel
duration = LP_ORDERBOOK_DURATION;
if ( timeout <= 0 )
timeout = LP_AUTOTRADE_TIMEOUT;
if ( basecoin->electrum != 0 && relcoin->electrum != 0 )
{
if ( timeout < 3*LP_AUTOTRADE_TIMEOUT )
timeout = 3*LP_AUTOTRADE_TIMEOUT;
}
else if ( basecoin->electrum != 0 || relcoin->electrum != 0 )
{
if ( timeout < 2*LP_AUTOTRADE_TIMEOUT )
timeout = 2*LP_AUTOTRADE_TIMEOUT;
}
if ( time(NULL) < Alice_expiration )
return(clonestr("{\"error\":\"only one pending request at a time\"}"));
else

52
iguana/exchanges/LP_peers.c

@ -57,7 +57,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;
printf("addpeer (%s:%u)\n",ipaddr,port);
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 )
return(0);
@ -83,6 +83,10 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
else peer->sessionid = sessionid;
peer->pushsock = peer->subsock = pushsock = subsock = -1;
strcpy(peer->ipaddr,ipaddr);
//peer->profitmargin = profitmargin;
peer->ipbits = ipbits;
peer->port = port;
peer->ip_port = ((uint64_t)port << 32) | ipbits;
if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 )
{
nanomsg_transportname(0,pushaddr,peer->ipaddr,pushport);
@ -114,36 +118,36 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
printf("error connecting to push.(%s)\n",pushaddr);
}
} else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock);
//peer->profitmargin = profitmargin;
peer->ipbits = ipbits;
peer->port = port;
peer->ip_port = ((uint64_t)port << 32) | ipbits;
portable_mutex_lock(&LP_peermutex);
HASH_ADD(hh,LP_peerinfos,ip_port,sizeof(peer->ip_port),peer);
if ( mypeer != 0 )
{
mypeer->numpeers++;
printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d %d)\n",ipaddr,mypeer->numpeers,mypubsock,numpeers,numutxos);
} else peer->numpeers = 1; // will become mypeer
portable_mutex_unlock(&LP_peermutex);
if ( IAMLP != 0 && mypubsock >= 0 )
if ( peer->pushsock >= 0 && peer->subsock >= 0 )
{
struct iguana_info *coin,*ctmp; bits256 zero; char busaddr[64];
memset(zero.bytes,0,sizeof(zero));
//LP_send(mypubsock,msg,(int32_t)strlen(msg)+1,1);
LP_reserved_msg("","",zero,jprint(LP_peerjson(peer),1));
if ( 0 )
printf("add peer %s\n",peer->ipaddr);
portable_mutex_lock(&LP_peermutex);
HASH_ADD(hh,LP_peerinfos,ip_port,sizeof(peer->ip_port),peer);
if ( mypeer != 0 )
{
HASH_ITER(hh,LP_coins,coin,ctmp)
mypeer->numpeers++;
printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d %d)\n",ipaddr,mypeer->numpeers,mypubsock,numpeers,numutxos);
} else peer->numpeers = 1; // will become mypeer
portable_mutex_unlock(&LP_peermutex);
if ( IAMLP != 0 && mypubsock >= 0 )
{
struct iguana_info *coin,*ctmp; bits256 zero; char busaddr[64];
memset(zero.bytes,0,sizeof(zero));
//LP_send(mypubsock,msg,(int32_t)strlen(msg)+1,1);
LP_reserved_msg("","",zero,jprint(LP_peerjson(peer),1));
if ( 0 )
{
if ( coin->bussock >= 0 )
HASH_ITER(hh,LP_coins,coin,ctmp)
{
nanomsg_transportname(0,busaddr,peer->ipaddr,coin->busport);
nn_connect(coin->bussock,busaddr);
if ( coin->bussock >= 0 )
{
nanomsg_transportname(0,busaddr,peer->ipaddr,coin->busport);
nn_connect(coin->bussock,busaddr);
}
}
}
}
}
} else printf("%s invalid pushsock.%d or subsock.%d\n",peer->ipaddr,peer->pushsock,peer->subsock);
}
} else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr);
return(peer);

2
iguana/exchanges/LP_portfolio.c

@ -93,6 +93,7 @@ char *LP_portfolio()
continue;
if ( iter == 0 )
{
//printf("from portfolio\n");
LP_privkey_init(-1,coin,G.LP_privkey,G.LP_mypub25519);
coin->balanceA = LP_balance(&coin->valuesumA,0,coin->symbol,coin->smartaddr);
coin->balanceB = LP_balance(&coin->valuesumB,1,coin->symbol,coin->smartaddr);
@ -634,6 +635,7 @@ void prices_loop(void *ignore)
}
free(retstr);
}
sleep(60);
}
}

9
iguana/exchanges/LP_prices.c

@ -344,6 +344,9 @@ void LP_peer_pricesquery(struct LP_peerinfo *peer)
peer->needping = (uint32_t)time(NULL);
if ( (retstr= issue_LP_getprices(peer->ipaddr,peer->port)) != 0 )
{
#ifdef FROM_JS
printf("%s\n",retstr);
#endif
if ( (array= cJSON_Parse(retstr)) != 0 )
{
if ( is_cJSON_Array(array) && (n= cJSON_GetArraySize(array)) > 0 )
@ -682,7 +685,7 @@ void LP_pubkeys_query()
reqjson = cJSON_CreateObject();
jaddstr(reqjson,"method","wantnotify");
jaddbits256(reqjson,"pub",pubp->pubkey);
//printf("%s\n",jprint(reqjson,0));
printf("LP_pubkeys_query %s\n",jprint(reqjson,0));
LP_reserved_msg("","",zero,jprint(reqjson,1));
}
}
@ -776,7 +779,7 @@ char *LP_orderbook(char *base,char *rel,int32_t duration)
for (i=n=0; i<numbids; i++)
{
jaddi(array,LP_orderbookjson(rel,bids[i]));
if ( suppress_prefetch == 0 && n < 10 && bids[i]->numutxos == 0 )
if ( suppress_prefetch == 0 && n < 7 && bids[i]->numutxos == 0 )
{
//printf("bid ping %s %s\n",rel,bids[i]->coinaddr);
LP_address(relcoin,bids[i]->coinaddr);
@ -796,7 +799,7 @@ char *LP_orderbook(char *base,char *rel,int32_t duration)
for (i=n=0; i<numasks; i++)
{
jaddi(array,LP_orderbookjson(base,asks[i]));
if ( suppress_prefetch == 0 && n < 10 && asks[i]->numutxos == 0 )
if ( suppress_prefetch == 0 && n < 7 && asks[i]->numutxos == 0 )
{
//printf("ask ping %s %s\n",base,asks[i]->coinaddr);
LP_address(basecoin,asks[i]->coinaddr);

86
iguana/exchanges/LP_remember.c

@ -93,7 +93,7 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx
sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid), OS_compatible_path(fname);
if ( (fp= fopen(fname,"wb")) != 0 )
{
fprintf(fp,"{\"src\":\"%s\",\"srcamount\":%.8f,\"dest\":\"%s\",\"destamount\":%.8f,\"requestid\":%u,\"quoteid\":%u,\"iambob\":%d,\"state\":%u,\"otherstate\":%u,\"expiration\":%u,\"dlocktime\":%u,\"plocktime\":%u,\"Atxfee\":%llu,\"Btxfee\":%llu",swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount),swap->I.req.requestid,swap->I.req.quoteid,swap->I.iambob,swap->I.statebits,swap->I.otherstatebits,swap->I.expiration,swap->bobdeposit.I.locktime,swap->bobpayment.I.locktime,(long long)swap->I.Atxfee,(long long)swap->I.Btxfee);
fprintf(fp,"{\"aliceid\":\"%llu\",\"src\":\"%s\",\"srcamount\":%.8f,\"dest\":\"%s\",\"destamount\":%.8f,\"requestid\":%u,\"quoteid\":%u,\"iambob\":%d,\"state\":%u,\"otherstate\":%u,\"expiration\":%u,\"dlocktime\":%u,\"plocktime\":%u,\"Atxfee\":%llu,\"Btxfee\":%llu",(long long)swap->aliceid,swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount),swap->I.req.requestid,swap->I.req.quoteid,swap->I.iambob,swap->I.statebits,swap->I.otherstatebits,swap->I.expiration,swap->bobdeposit.I.locktime,swap->bobpayment.I.locktime,(long long)swap->I.Atxfee,(long long)swap->I.Btxfee);
if ( memcmp(zeroes,swap->I.secretAm,20) != 0 )
{
init_hexbytes_noT(secretAmstr,swap->I.secretAm,20);
@ -463,7 +463,8 @@ void LP_totals_update(int32_t iambob,char *alicecoin,char *bobcoin,int64_t *KMDt
struct LP_swap_remember
{
bits256 pubA0,pubB0,pubB1,privAm,privBn,paymentspent,Apaymentspent,depositspent,myprivs[2],txids[sizeof(txnames)/sizeof(*txnames)];
uint64_t Atxfee,Btxfee,srcamount,destamount; int64_t values[sizeof(txnames)/sizeof(*txnames)];
uint64_t Atxfee,Btxfee,srcamount,destamount,aliceid;
int64_t values[sizeof(txnames)/sizeof(*txnames)];
uint32_t requestid,quoteid,plocktime,dlocktime,expiration,state,otherstate;
int32_t iambob,finishedflag,origfinishedflag,Predeemlen,Dredeemlen,sentflags[sizeof(txnames)/sizeof(*txnames)];
uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],Predeemscript[1024],Dredeemscript[1024],pubkey33[33],other33[33];
@ -483,6 +484,7 @@ cJSON *LP_swap_json(struct LP_swap_remember *rswap)
jaddstr(item,"alice",rswap->dest);
jaddnum(item,"destamount",dstr(rswap->destamount));
jaddnum(item,"alicetxfee",dstr(rswap->Atxfee));
jadd64bits(item,"aliceid",rswap->aliceid);
array = cJSON_CreateArray();
for (i=0; i<sizeof(txnames)/sizeof(*txnames); i++)
{
@ -521,6 +523,7 @@ int32_t LP_rswap_init(struct LP_swap_remember *rswap,uint32_t requestid,uint32_t
if ( (item= cJSON_Parse(fstr)) != 0 )
{
rswap->iambob = jint(item,"iambob");
rswap->aliceid = j64bits(item,"aliceid");
if ( (secretstr= jstr(item,"secretAm")) != 0 && strlen(secretstr) == 40 )
decode_hex(rswap->secretAm,20,secretstr);
if ( (secretstr= jstr(item,"secretAm256")) != 0 && strlen(secretstr) == 64 )
@ -928,9 +931,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
{
if ( rswap.sentflags[BASILISK_BOBPAYMENT] != 0 && bits256_nonz(rswap.paymentspent) == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobpaymentaddr,rswap.txids[BASILISK_BOBPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.paymentspent = deadtxid;
flag = 0;
if ( bob->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobpaymentaddr,rswap.txids[BASILISK_BOBPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.paymentspent = deadtxid;
}
if ( flag == 0 )
{
if ( bits256_nonz(rswap.txids[BASILISK_BOBPAYMENT]) != 0 )
@ -964,9 +971,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
{
if ( time(NULL) > rswap.expiration )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobdepositaddr,rswap.txids[BASILISK_BOBDEPOSIT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.depositspent = deadtxid;
flag = 0;
if ( bob->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobdepositaddr,rswap.txids[BASILISK_BOBDEPOSIT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.depositspent = deadtxid;
}
if ( flag == 0 )
{
if ( rswap.Dredeemlen != 0 )
@ -989,9 +1000,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
}
if ( rswap.sentflags[BASILISK_ALICEPAYMENT] != 0 && bits256_nonz(rswap.Apaymentspent) == 0 && rswap.sentflags[BASILISK_ALICECLAIM] == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.alicecoin,rswap.alicepaymentaddr,rswap.txids[BASILISK_ALICEPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.Apaymentspent = deadtxid;
flag = 0;
if ( alice->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.alicecoin,rswap.alicepaymentaddr,rswap.txids[BASILISK_ALICEPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.Apaymentspent = deadtxid;
}
if ( flag == 0 )
{
rswap.privBn = basilisk_swap_privBn_extract(&rswap.txids[BASILISK_BOBREFUND],rswap.bobcoin,rswap.txids[BASILISK_BOBDEPOSIT],rswap.privBn);
@ -1011,9 +1026,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
//printf("try to bobspend aspend.%s have privAm.%d\n",bits256_str(str,rswap.txids[BASILISK_ALICESPEND]),bits256_nonz(rswap.privAm));
if ( bits256_nonz(rswap.txids[BASILISK_ALICESPEND]) != 0 || bits256_nonz(rswap.privAm) != 0 )
{
if ( (txoutobj= LP_gettxout(rswap.alicecoin,rswap.alicepaymentaddr,rswap.txids[BASILISK_ALICEPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.Apaymentspent = deadtxid;
flag = 0;
if ( alice->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.alicecoin,rswap.alicepaymentaddr,rswap.txids[BASILISK_ALICEPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.Apaymentspent = deadtxid;
}
if ( flag == 0 )
{
if ( bits256_nonz(rswap.privAm) == 0 )
@ -1032,9 +1051,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
}
if ( rswap.sentflags[BASILISK_BOBRECLAIM] == 0 && rswap.sentflags[BASILISK_BOBPAYMENT] != 0 && bits256_nonz(rswap.txids[BASILISK_BOBPAYMENT]) != 0 && bits256_nonz(rswap.paymentspent) == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobpaymentaddr,rswap.txids[BASILISK_BOBPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.paymentspent = deadtxid;
flag = 0;
if ( bob->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobpaymentaddr,rswap.txids[BASILISK_BOBPAYMENT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.paymentspent = deadtxid;
}
if ( flag == 0 && time(NULL) > rswap.expiration )
{
// bobreclaim
@ -1057,9 +1080,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
}
if ( rswap.sentflags[BASILISK_BOBREFUND] == 0 && rswap.sentflags[BASILISK_BOBDEPOSIT] != 0 && bits256_nonz(rswap.txids[BASILISK_BOBDEPOSIT]) != 0 && bits256_nonz(rswap.depositspent) == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobdepositaddr,rswap.txids[BASILISK_BOBDEPOSIT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.depositspent = deadtxid;
flag = 0;
if ( bob->electrum == 0 )
{
if ( (txoutobj= LP_gettxout(rswap.bobcoin,rswap.bobdepositaddr,rswap.txids[BASILISK_BOBDEPOSIT],0)) != 0 )
free_json(txoutobj), flag = 0;
else flag = -1, rswap.depositspent = deadtxid;
}
if ( flag == 0 && (bits256_nonz(rswap.Apaymentspent) != 0 || time(NULL) > rswap.expiration) )
{
printf("do the refund! paymentspent.%s now.%u vs expiration.%u\n",bits256_str(str,rswap.paymentspent),(uint32_t)time(NULL),rswap.expiration);
@ -1110,6 +1137,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
jaddstr(item,"method","tradestatus");
itemstr = jprint(item,0);
fprintf(fp,"%s\n",itemstr);
LP_tradecommand_log(item);
LP_reserved_msg(rswap.src,rswap.dest,zero,clonestr(itemstr));
LP_broadcast_message(LP_mypubsock,rswap.src,rswap.dest,zero,itemstr);
fclose(fp);
@ -1228,24 +1256,6 @@ char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid)
return(retstr);
}
void LP_tradecommand_log(cJSON *argjson)
{
static FILE *logfp; char *jsonstr;
if ( logfp == 0 )
{
if ( (logfp= fopen("stats.log","rb+")) != 0 )
fseek(logfp,0,SEEK_END);
else logfp = fopen("stats.log","wb");
}
if ( logfp != 0 )
{
jsonstr = jprint(argjson,0);
fprintf(logfp,"%s\n",jsonstr);
free(jsonstr);
fflush(logfp);
}
}
extern struct LP_quoteinfo LP_Alicequery;
extern uint32_t Alice_expiration;

6
iguana/exchanges/LP_rpc.c

@ -290,7 +290,7 @@ cJSON *LP_gettx(char *symbol,bits256 txid)
{
if ( (retjson= electrum_transaction(symbol,coin->electrum,&retjson,txid)) != 0 )
return(retjson);
else printf("failed blockchain.transaction.get %s %s\n",coin->symbol,buf);
else printf("failed blockchain.transaction.get %s %s\n",coin->symbol,bits256_str(str,txid));
return(cJSON_Parse("{\"error\":\"no transaction bytes\"}"));
}
}
@ -345,7 +345,7 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout)
{
if ( tx->outpoints[vout].spendheight > 0 )
return(0);
return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value));
//return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value));
}
if ( coinaddr[0] == 0 )
{
@ -362,7 +362,7 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout)
{
if ( up->spendheight > 0 )
return(0);
return(LP_gettxout_json(txid,vout,up->U.height,coinaddr,up->U.value));
//return(LP_gettxout_json(txid,vout,up->U.height,coinaddr,up->U.value));
}
if ( (array= electrum_address_listunspent(coin->symbol,0,&array,coinaddr,1)) != 0 )
{

43
iguana/exchanges/LP_signatures.c

@ -43,6 +43,7 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp)
{
double price; cJSON *retjson = cJSON_CreateObject();
jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui);
jadd64bits(retjson,"aliceid",qp->aliceid);
jaddstr(retjson,"base",qp->srccoin);
jaddstr(retjson,"rel",qp->destcoin);
if ( qp->coinaddr[0] != 0 )
@ -92,16 +93,22 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp)
jaddnum(retjson,"price",price);
}
}
if ( qp->R.requestid != 0 )
jaddnum(retjson,"requestid",qp->R.requestid);
if ( qp->R.quoteid != 0 )
jaddnum(retjson,"quoteid",qp->R.quoteid);
return(retjson);
}
int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
{
uint32_t rid,qid;
safecopy(qp->gui,LP_gui,sizeof(qp->gui));
safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin));
safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr));
safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin));
safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr));
qp->aliceid = j64bits(argjson,"aliceid");
qp->timestamp = juint(argjson,"timestamp");
qp->quotetime = juint(argjson,"quotetime");
qp->txid = jbits256(argjson,"txid");
@ -118,6 +125,20 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
qp->destsatoshis = j64bits(argjson,"destsatoshis");
qp->txfee = j64bits(argjson,"txfee");
qp->desttxfee = j64bits(argjson,"desttxfee");
qp->R.requestid = juint(argjson,"requestid");
qp->R.quoteid = juint(argjson,"quoteid");
if ( qp->R.requestid == 0 )
{
rid= basilisk_requestid(&qp->R);
//printf("requestid.%u -> %u\n",qp->R.requestid,rid);
qp->R.requestid = rid;
}
if ( qp->R.quoteid == 0 )
{
qid= basilisk_quoteid(&qp->R);
//printf("quoteid.%u -> %u\n",qp->R.quoteid,qid);
qp->R.quoteid = qid;
}
return(0);
}
@ -263,7 +284,7 @@ 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));
printf("LP_utxos_sigcheck failure, probably from %s with older version\n",bits256_str(str,pubkey));
retval = -1;
} else retval = 0;
}
@ -329,7 +350,15 @@ struct LP_utxos_qitem { struct queueitem DL; cJSON *argjson; };
char *LP_postutxos_recv(cJSON *argjson)
{
struct LP_utxos_qitem *uitem; bits256 utxoshash; cJSON *obj;
struct LP_utxos_qitem *uitem; struct iguana_info *coin; char *coinaddr,*symbol; bits256 utxoshash; cJSON *obj;
if ( (coinaddr= jstr(argjson,"coinaddr")) != 0 && (symbol= jstr(argjson,"coin")) != 0 && (coin= LP_coinfind(symbol)) != 0 )
{
if ( strcmp(coinaddr,coin->smartaddr) == 0 )
{
//printf("ignore my utxo from external source %s %s\n",symbol,coinaddr);
return(clonestr("{\"result\":\"success\"}"));
}
}
if ( (obj= jobj(argjson,"utxos")) != 0 )
{
utxoshash = LP_utxoshash_calc(obj);
@ -583,16 +612,18 @@ void LP_smartutxos_push(struct iguana_info *coin)
char *LP_uitem_recv(cJSON *argjson)
{
bits256 txid; int32_t vout,height; uint64_t value; char *coinaddr,*symbol;
bits256 txid; int32_t vout,height; uint64_t value; struct iguana_info *coin; char *coinaddr,*symbol;
txid = jbits256(argjson,"txid");
vout = jint(argjson,"vout");
height = jint(argjson,"ht");
value = j64bits(argjson,"value");
coinaddr = jstr(argjson,"coinaddr");
if ( (symbol= jstr(argjson,"coin")) != 0 && coinaddr != 0 )
if ( (symbol= jstr(argjson,"coin")) != 0 && coinaddr != 0 && (coin= LP_coinfind(symbol)) != 0 )
{
//char str[65]; printf("uitem %s %s %s/v%d %.8f ht.%d\n",coin,coinaddr,bits256_str(str,txid),vout,dstr(value),height);
LP_address_utxoadd(LP_coinfind(symbol),coinaddr,txid,vout,value,height,-1);
//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);
//else printf("ignore external uitem %s %s\n",symbol,coin->smartaddr);
}
return(clonestr("{\"result\":\"success\"}"));
}

65
iguana/exchanges/LP_socket.c

@ -26,8 +26,6 @@
#include <WinSock2.h>
#endif
#define ELECTRUM_TIMEOUT 10
int32_t LP_socket(int32_t bindflag,char *hostname,uint16_t port)
{
int32_t opt,sock,result; char ipaddr[64],checkipaddr[64]; struct timeval timeout;
@ -315,7 +313,7 @@ int32_t electrum_process_array(struct iguana_info *coin,struct electrum_info *ep
else
{
//printf("external unspent has no gettxout\n");
flag += LP_address_utxoadd(coin,coinaddr,txid,v,value,0,1);
flag += LP_address_utxoadd("electrum process",coin,coinaddr,txid,v,value,0,1);
}
}
else
@ -349,10 +347,10 @@ int32_t electrum_process_array(struct iguana_info *coin,struct electrum_info *ep
tx->outpoints[v].value = value;
}
}
if ( value != 0 || tx->height > 0 )
if ( tx->height > 0 )
{
//printf("from electrum_process_array\n");
flag += LP_address_utxoadd(coin,coinaddr,txid,v,value,tx->height,-1);
flag += LP_address_utxoadd("electrum process2",coin,coinaddr,txid,v,value,tx->height,-1);
}
//printf("v.%d numvouts.%d %.8f (%s)\n",v,tx->numvouts,dstr(tx->outpoints[jint(item,"tx_pos")].value),jprint(item,0));
} //else printf("cant find tx\n");
@ -373,6 +371,7 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch
{
*retjsonp = 0;
sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params);
//printf("%s %s",symbol,stratumreq);
memset(ep->buf,0,ep->bufsize);
sitem = (struct stritem *)queueitem(stratumreq);
sitem->expiration = timeout;
@ -484,8 +483,7 @@ cJSON *electrum_address_gethistory(char *symbol,struct electrum_info *ep,cJSON *
if ( tx->height > 0 && tx->height != height )
printf("update %s height.%d <- %d\n",bits256_str(str,txid),tx->height,height);
tx->height = height;
//printf("from history\n");
LP_address_utxoadd(coin,addr,txid,0,0,height,-1);
LP_address_utxoadd("electrum history",coin,addr,txid,0,0,height,-1);
}
}
}
@ -516,17 +514,34 @@ cJSON *electrum_address_getmempool(char *symbol,struct electrum_info *ep,cJSON *
cJSON *electrum_address_listunspent(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *addr,int32_t electrumflag)
{
cJSON *retjson=0; struct iguana_info *coin = LP_coinfind(symbol);
cJSON *retjson=0; struct LP_address *ap; struct iguana_info *coin; int32_t height,usecache=1;
if ( (coin= LP_coinfind(symbol)) == 0 )
return(0);
if ( ep == 0 || ep->heightp == 0 )
height = coin->longestchain;
else height = *(ep->heightp);
if ( (ap= LP_address(coin,addr)) != 0 )
{
if ( ap->unspenttime == 0 )
usecache = 0;
else if ( ap->unspentheight < height )
usecache = 0;
else if ( G.LP_pendingswaps != 0 && time(NULL) > ap->unspenttime+20 )
usecache = 0;
}
//printf("electrum.%s/%s listunspent last.(%s lag %d)\n",ep->symbol,coin->symbol,coin->lastunspent,(int32_t)(time(NULL) - coin->unspenttime));
if ( strcmp(coin->lastunspent,addr) != 0 || time(NULL) > coin->unspenttime+30 )
if ( usecache == 0 )
{
if ( (retjson= electrum_strarg(symbol,ep,retjsonp,"blockchain.address.listunspent",addr,ELECTRUM_TIMEOUT)) != 0 )
{
//printf("LISTUNSPENT.(%s)\n",jprint(retjson,0));
printf("%s.%d u.%u/%d t.%ld %s LISTUNSPENT.(%d)\n",coin->symbol,height,ap->unspenttime,ap->unspentheight,time(NULL),addr,(int32_t)strlen(jprint(retjson,0)));
if ( electrum_process_array(coin,ep,addr,retjson,electrumflag) != 0 )
LP_postutxos(coin->symbol,addr);
safecopy(coin->lastunspent,addr,sizeof(coin->lastunspent));
coin->unspenttime = (uint32_t)time(NULL);
if ( ap != 0 )
{
ap->unspenttime = (uint32_t)time(NULL);
ap->unspentheight = height;
}
}
} else retjson = LP_address_utxos(coin,addr,1);
return(retjson);
@ -541,9 +556,13 @@ cJSON *electrum_addpeer(char *symbol,struct electrum_info *ep,cJSON **retjsonp,c
cJSON *electrum_sendrawtransaction(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *rawtx) { return(electrum_strarg(symbol,ep,retjsonp,"blockchain.transaction.broadcast",rawtx,ELECTRUM_TIMEOUT)); }
cJSON *electrum_estimatefee(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t numblocks) { return(electrum_intarg(symbol,ep,retjsonp,"blockchain.estimatefee",numblocks,ELECTRUM_TIMEOUT)); }
cJSON *electrum_getheader(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t n) { return(electrum_intarg(symbol,ep,retjsonp,"blockchain.block.get_header",n,ELECTRUM_TIMEOUT)); }
cJSON *electrum_getchunk(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t n) { return(electrum_intarg(symbol,ep,retjsonp,"blockchain.block.get_chunk",n,ELECTRUM_TIMEOUT)); }
cJSON *electrum_getheader(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t n)
{
return(electrum_intarg(symbol,ep,retjsonp,"blockchain.block.get_header",n,ELECTRUM_TIMEOUT));
}
cJSON *LP_transaction_fromdata(struct iguana_info *coin,bits256 txid,uint8_t *serialized,int32_t len)
{
uint8_t *extraspace; cJSON *txobj; char str[65],str2[65]; struct iguana_msgtx msgtx; bits256 checktxid;
@ -554,7 +573,7 @@ cJSON *LP_transaction_fromdata(struct iguana_info *coin,bits256 txid,uint8_t *se
free(extraspace);
if ( bits256_cmp(txid,checktxid) != 0 )
{
printf("LP_transaction_fromdata mismatched txid %s vs %s\n",bits256_str(str,txid),bits256_str(str2,checktxid));
printf("%s LP_transaction_fromdata mismatched txid %s vs %s\n",coin->symbol,bits256_str(str,txid),bits256_str(str2,checktxid));
free_json(txobj);
txobj = 0;
}
@ -576,6 +595,14 @@ cJSON *electrum_transaction(char *symbol,struct electrum_info *ep,cJSON **retjso
return(txobj);
}
}
if ( bits256_cmp(txid,coin->cachedtxid) == 0 )
{
if ( (txobj= LP_transaction_fromdata(coin,txid,coin->cachedtxiddata,coin->cachedtxidlen)) != 0 )
{
*retjsonp = txobj;
return(txobj);
}
}
hexjson = electrum_hasharg(symbol,ep,&hexjson,"blockchain.transaction.get",txid,ELECTRUM_TIMEOUT);
hexstr = jprint(hexjson,0);
if ( strlen(hexstr) > 60000 )
@ -594,7 +621,12 @@ cJSON *electrum_transaction(char *symbol,struct electrum_info *ep,cJSON **retjso
{
len = (int32_t)strlen(hexstr+1) >> 1;
serialized = malloc(len);
if ( coin->cachedtxiddata != 0 )
free(coin->cachedtxiddata);
coin->cachedtxiddata = malloc(len);
coin->cachedtxidlen = len;
decode_hex(serialized,len,hexstr+1);
memcpy(coin->cachedtxiddata,serialized,len);
free(hexstr);
//printf("DATA.(%s) from (%s)\n",hexstr+1,jprint(hexjson,0));
txobj = LP_transaction_fromdata(coin,txid,serialized,len);
@ -618,7 +650,7 @@ cJSON *electrum_transaction(char *symbol,struct electrum_info *ep,cJSON **retjso
free_json(hexjson);
//printf("return from electrum_transaction\n");
return(*retjsonp);
} else printf("non-hex tx.(%s)\n",jprint(hexjson,0));
} else printf("%s %s non-hex tx.(%s)\n",coin->symbol,bits256_str(str,txid),jprint(hexjson,0));
free(hexstr);
free_json(hexjson);
}
@ -775,7 +807,8 @@ int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len)
{
if ( (height= jint(resultjson,"block_height")) > 0 && ep->heightp != 0 && ep->heighttimep != 0 )
{
*(ep->heightp) = height;
if ( height > *(ep->heightp) )
*(ep->heightp) = height;
*(ep->heighttimep) = (uint32_t)time(NULL);
if ( (coin= LP_coinfind(ep->symbol)) != 0 )
coin->updaterate = (uint32_t)time(NULL);

89
iguana/exchanges/LP_statemachine.c

@ -1433,6 +1433,27 @@ int32_t bitcoin_coinptrs(bits256 pubkey,struct iguana_info **bobcoinp,struct igu
}
return(-1);
}*/
/*if ( (butxo= LP_utxopairfind(1,Q.txid,Q.vout,Q.txid2,Q.vout2)) == 0 )
{
value = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid,Q.vout);
value2 = LP_txvalue(Q.coinaddr,Q.srccoin,Q.txid2,Q.vout2);
if ( value == 0 || value2 == 0 )
{
printf("zero value %.8f or value2 %.8f\n",dstr(value),dstr(value2));
return(clonestr("{\"error\":\"spent txid or txid2 for bob?\"}"));
}
if ( (butxo= LP_utxoadd(1,Q.srccoin,Q.txid,Q.vout,value,Q.txid2,Q.vout2,value2,Q.coinaddr,Q.srchash,LP_gui,0)) == 0 )
{
printf("cant find or create butxo\n");
return(clonestr("{\"error\":\"cant find or create butxo\"}"));
}
if ( value < Q.satoshis )
{
printf("butxo value %.8f less satoshis %.8f\n",dstr(value),dstr(Q.satoshis));
return(clonestr("{\"error\":\"butxo value less than satoshis\"}"));
}
}*/
/*if ( addflag != 0 && LP_utxofind(1,Q.txid,Q.vout) == 0 )
{
LP_utxoadd(1,-1,Q.srccoin,Q.txid,Q.vout,Q.value,Q.txid2,Q.vout2,Q.value2,"",Q.srcaddr,Q.srchash,0.);
@ -1831,6 +1852,36 @@ void LP_utxo_clientpublish(struct LP_utxoinfo *utxo)
}
}
/*char *LP_spentcheck(cJSON *argjson)
{
bits256 txid,checktxid; int32_t vout,checkvout; struct LP_utxoinfo *utxo; int32_t iambob,retval = 0;
txid = jbits256(argjson,"txid");
vout = jint(argjson,"vout");
for (iambob=0; iambob<=1; iambob++)
{
if ( (utxo= LP_utxofind(iambob,txid,vout)) != 0 && utxo->T.spentflag == 0 )
{
if ( jobj(argjson,"check") == 0 )
checktxid = txid, checkvout = vout;
else
{
checktxid = jbits256(argjson,"checktxid");
checkvout = jint(argjson,"checkvout");
}
if ( LP_txvalue(0,utxo->coin,checktxid,checkvout) == 0 )
{
//if ( LP_mypeer != 0 && LP_mypeer->numutxos > 0 )
// LP_mypeer->numutxos--;
utxo->T.spentflag = (uint32_t)time(NULL);
retval++;
printf("indeed txid was spent\n");
}
}
}
if ( retval > 0 )
return(clonestr("{\"result\":\"marked as spent\"}"));
return(clonestr("{\"error\":\"cant find txid to check spent status\"}"));
}*/
void LP_utxo_spentcheck(int32_t pubsock,struct LP_utxoinfo *utxo)
{
struct _LP_utxoinfo u; struct iguana_info *coin; char str[65]; uint32_t now = (uint32_t)time(NULL);
@ -1928,7 +1979,43 @@ if ( aliceutxo->S.swap == 0 )
LP_availableset(aliceutxo);
return(jprint(bestitem,0));
}
if ( 0 && (retstr= issue_LP_listunspent(peer->ipaddr,peer->port,coin->symbol,"")) != 0 )
{
if ( (array2= cJSON_Parse(retstr)) != 0 )
{
if ( (m= cJSON_GetArraySize(array2)) > 0 )
{
for (j=0; j<m; j++)
{
item = jitem(array2,j);
if ( (coinaddr= jfieldname(item)) != 0 )
{
metric = j64bits(item,coinaddr);
//printf("(%s) -> %.8f n.%d\n",coinaddr,dstr(metric>>16),(uint16_t)metric);
if ( (ap= LP_addressfind(coin,coinaddr)) == 0 || _LP_unspents_metric(ap->total,ap->n) != metric )
{
if ( ap == 0 || ap->n < (metric & 0xffff) )
{
if ( (retstr2= issue_LP_listunspent(peer->ipaddr,peer->port,coin->symbol,coinaddr)) != 0 )
{
if ( (array3= cJSON_Parse(retstr2)) != 0 )
{
LP_unspents_array(coin,coinaddr,array3);
//printf("pulled.(%s)\n",retstr2);
free_json(array3);
}
free(retstr2);
}
} //else printf("wait for %s to pull %d vs %d\n",peer->ipaddr,ap!=0?ap->n:-1,(uint16_t)metric);
}
}
}
}
free_json(array2);
}
//printf("processed.(%s)\n",retstr);
free(retstr);
}
/*if ( time(NULL) > coin->lastmonitor+60 )
{
//portable_mutex_lock(&coin->addrmutex);

181
iguana/exchanges/LP_stats.c

@ -0,0 +1,181 @@
/******************************************************************************
* 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. *
* *
******************************************************************************/
//
// LP_stats.c
// marketmaker
//
#define LP_STATSLOG_FNAME "stats.log"
void LP_tradecommand_log(cJSON *argjson)
{
static FILE *logfp; char *jsonstr;
if ( logfp == 0 )
{
if ( (logfp= fopen(LP_STATSLOG_FNAME,"rb+")) != 0 )
fseek(logfp,0,SEEK_END);
else logfp = fopen(LP_STATSLOG_FNAME,"wb");
}
if ( logfp != 0 )
{
jsonstr = jprint(argjson,0);
fprintf(logfp,"%s\n",jsonstr);
free(jsonstr);
fflush(logfp);
}
}
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];
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));
}
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;
memset(&Q,0,sizeof(Q));
if ( LP_quoteparse(&Q,lineobj) < 0 )
{
printf("quoteparse_error.(%s)\n",jprint(lineobj,0));
LP_parse_errors++;
return(-1);
}
else
{
base = jstr(lineobj,"base");
rel = jstr(lineobj,"rel");
gui = jstr(lineobj,"gui");
satoshis = j64bits(lineobj,"satoshis");
if ( base == 0 || rel == 0 || satoshis == 0 )
{
printf("quoteparse_error.(%s)\n",jprint(lineobj,0));
LP_parse_errors++;
return(-1);
}
txfee = j64bits(lineobj,"txfee");
timestamp = juint(lineobj,"timestamp");
destsatoshis = j64bits(lineobj,"destsatoshis");
desttxid = jbits256(lineobj,"desttxid");
destvout = jint(lineobj,"destvout");
feetxid = jbits256(lineobj,"feetxid");
feevout = jint(lineobj,"feevout");
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<sizeof(Ridqids)/sizeof(*Ridqids); i++)
{
if ( Ridqids[i] == aliceid )
{
duplicate = 1;
LP_duplicates++;
break;
}
}
if ( duplicate == 0 )
{
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++;
}
}
return(duplicate == 0);
}
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_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));
}
char *LP_statslog_disp(int32_t n)
{
cJSON *retjson;
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jaddnum(retjson,"newlines",n);
jaddnum(retjson,"request",LP_requests);
jaddnum(retjson,"reserved",LP_reserveds);
jaddnum(retjson,"connect",LP_connects);
jaddnum(retjson,"connected",LP_connecteds);
jaddnum(retjson,"duplicates",LP_duplicates);
jaddnum(retjson,"parse_errors",LP_parse_errors);
jaddnum(retjson,"uniques",LP_numridqids);
jaddnum(retjson,"tradestatus",LP_tradestatuses);
jaddnum(retjson,"unknown",LP_unknowns);
return(jprint(retjson,1));
}
char *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(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));
}

17
iguana/exchanges/LP_swap.c

@ -746,6 +746,7 @@ int32_t LP_swapwait(uint32_t requestid,uint32_t quoteid,int32_t duration,int32_t
void LP_bobloop(void *_swap)
{
uint8_t *data; int32_t maxlen,m,n; uint32_t expiration; struct basilisk_swap *swap = _swap;
G.LP_pendingswaps++;
fprintf(stderr,"start swap iambob\n");
maxlen = 1024*1024 + sizeof(*swap);
data = malloc(maxlen);
@ -785,7 +786,7 @@ void LP_bobloop(void *_swap)
while ( (n= LP_numconfirms(swap->alicecoin.symbol,swap->alicepayment.I.destaddr,swap->alicepayment.I.signedtxid,0,1)) < m ) // sync with alice
{
char str[65];printf("%d waiting for alicepayment %s to be confirmed.%d %s %s\n",n,swap->alicepayment.I.destaddr,m,swap->alicecoin.symbol,bits256_str(str,swap->alicepayment.I.signedtxid));
sleep(3);
sleep(10);
}
if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 )
printf("error sending bobpayment\n");
@ -804,11 +805,13 @@ void LP_bobloop(void *_swap)
basilisk_swap_finished(swap);
//free(swap);
} else printf("swap timed out\n");
G.LP_pendingswaps--;
}
void LP_aliceloop(void *_swap)
{
uint8_t *data; int32_t maxlen,n,m; uint32_t expiration; struct basilisk_swap *swap = _swap;
G.LP_pendingswaps++;
maxlen = 1024*1024 + sizeof(*swap);
data = malloc(maxlen);
expiration = (uint32_t)time(NULL) + LP_SWAPSTEP_TIMEOUT;
@ -850,7 +853,7 @@ void LP_aliceloop(void *_swap)
while ( (n= LP_numconfirms(swap->bobcoin.symbol,swap->bobpayment.I.destaddr,swap->bobpayment.I.signedtxid,0,1)) < swap->I.bobconfirms )
{
char str[65];printf("%d waiting for bobpayment %s to be confirmed.%d %s %s\n",n,swap->bobpayment.I.destaddr,swap->I.bobconfirms,swap->bobcoin.symbol,bits256_str(str,swap->bobpayment.I.signedtxid));
sleep(LP_SWAPSTEP_TIMEOUT);
sleep(10);
}
/*if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x20000,data,maxlen,&swap->alicespend,0x40000,0) == 0 )
printf("error sending alicespend\n");
@ -1120,9 +1123,16 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256
swap->bobpayment.utxotxid = qp->txid, swap->bobpayment.utxovout = qp->vout;
swap->bobdeposit.utxotxid = qp->txid2, swap->bobdeposit.utxovout = qp->vout2;
swap->alicepayment.utxotxid = qp->desttxid, swap->alicepayment.utxovout = qp->destvout;
LP_mark_spent(swap->bobcoin.symbol,qp->txid,qp->vout);
LP_mark_spent(swap->bobcoin.symbol,qp->txid2,qp->vout2);
LP_mark_spent(swap->alicecoin.symbol,qp->desttxid,qp->destvout);
if ( swap->I.iambob != 0 )
swap->otherfee.utxotxid = qp->feetxid, swap->otherfee.utxovout = qp->feevout;
else swap->myfee.utxotxid = qp->feetxid, swap->myfee.utxovout = qp->feevout;
else
{
swap->myfee.utxotxid = qp->feetxid, swap->myfee.utxovout = qp->feevout;
LP_mark_spent(swap->alicecoin.symbol,qp->feetxid,qp->feevout);
}
char str[65],str2[65],str3[65]; printf("IAMBOB.%d %s %s %s\n",swap->I.iambob,bits256_str(str,qp->txid),bits256_str(str2,qp->txid2),bits256_str(str3,qp->feetxid));
return(swap);
}
@ -1131,6 +1141,7 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256
{
struct basilisk_swap *swap; bits256 pubkey25519; uint8_t pubkey33[33];
swap = calloc(1,sizeof(*swap));
swap->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout);
swap->I.req.quoteid = rp->quoteid;
swap->ctx = bitcoin_ctx();
vcalc_sha256(0,swap->I.orderhash.bytes,(uint8_t *)rp,sizeof(*rp));

10
iguana/exchanges/LP_transaction.c

@ -604,6 +604,7 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t wiftaddr,uint8_
char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privkey,bits256 *privkey2p,uint8_t *redeemscript,int32_t redeemlen,uint8_t *userdata,int32_t userdatalen,bits256 utxotxid,int32_t utxovout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr,int32_t suppress_pubkeys)
{
char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*obj,*vouts,*item,*privkeys; int32_t completed,spendlen,n,ignore_cltverr=1; struct vin_info V[2]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value=0,change = 0; struct iguana_msgtx msgtx; struct iguana_info *coin;
LP_mark_spent(symbol,utxotxid,utxovout);
if ( txfee > 0 && txfee < 10000 )
txfee = 10000;
*destamountp = 0;
@ -916,7 +917,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf
*txobjp = 0;
if ( sizeof(utxos)/sizeof(*utxos) != max )
{
printf("LP_createrawtransaction: internal error %ld != max.%d\n",sizeof(utxos)/sizeof(*utxos),max);
printf("LP_createrawtransaction: internal error %d != max.%d\n",(int32_t)(sizeof(utxos)/sizeof(*utxos)),max);
return(0);
}
if ( coin == 0 || outputs == 0 || (numvouts= cJSON_GetArraySize(outputs)) <= 0 )
@ -955,7 +956,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf
return(0);
}
memset(utxos,0,sizeof(utxos));
if ( (numutxos= LP_address_utxo_ptrs(0,utxos,max,ap,coin->smartaddr)) <= 0 )
if ( (numutxos= LP_address_utxo_ptrs(coin,0,utxos,max,ap,coin->smartaddr)) <= 0 )
{
printf("LP_createrawtransaction: address_utxo_ptrs %d, error\n",numutxos);
return(0);
@ -1176,7 +1177,7 @@ int32_t basilisk_alicescript(uint8_t *redeemscript,int32_t *redeemlenp,uint8_t *
return(n);
}
char *basilisk_swap_Aspend(char *name,char *symbol,uint64_t Atxfee,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privAm,bits256 privBn,bits256 utxotxid,int32_t vout,uint8_t pubkey33[33],uint32_t expiration,int64_t *destamountp,char *vinaddr)
char *basilisk_swap_Aspend(char *name,char *symbol,uint64_t Atxfee,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privAm,bits256 privBn,bits256 utxotxid,int32_t utxovout,uint8_t pubkey33[33],uint32_t expiration,int64_t *destamountp,char *vinaddr)
{
char msigaddr[64],*signedtx = 0; int32_t spendlen,redeemlen; uint8_t tmp33[33],redeemscript[512],spendscript[128]; bits256 pubAm,pubBn,signedtxid; uint64_t txfee;
if ( bits256_nonz(privAm) != 0 && bits256_nonz(privBn) != 0 )
@ -1200,7 +1201,8 @@ char *basilisk_swap_Aspend(char *name,char *symbol,uint64_t Atxfee,uint8_t wifta
txfee = LP_MIN_TXFEE;
}
//txfee = LP_txfee(symbol);
signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,wiftaddr,taddr,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0,vinaddr,1);
signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,wiftaddr,taddr,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,utxovout,0,pubkey33,1,expiration,destamountp,0,0,vinaddr,1);
LP_mark_spent(symbol,utxotxid,utxovout);
}
return(signedtx);
}

218
iguana/exchanges/LP_utxo.c

@ -18,19 +18,6 @@
// marketmaker
//
// listunspent: valid for local node, mostly valid for electrum
// full node + electrum for external listunspent, gettxout to validate
// pruned node + electrum for external listunspent, gettxout to validate
// full node, network for external listunspent, gettxout to validate
// pruned node, network for external listunspent, gettxout to validate
// electrum only, network for gettxout
// handle spurious errors
// handle invalid data
//REJECT KMD ccee27b53b52ca61bbc9fdc7de5feb0a12c14d4d92639414d372f002cc3d092f/v0 value.468169379 vs 468169380 ({"bestblock":"080400d4216c02d100004fd2f4f3505ddb507b643785e02703d3412feba39fb1","confirmations":2356,"value":4.68169380,"scriptPubKey":{"asm":"0224e31f93eff0cc30eaf0b2389fbc591085c0e122c4d11862c1729d090106c842 OP_CHECKSIG","hex":"210224e31f93eff0cc30eaf0b2389fbc591085c0e122c4d11862c1729d090106c842ac","reqSigs":1,"type":"pubkey","addresses":["RFssbc211PJdVy1bvcvAG5X2N4ovPAoy5o"]},"version":1,"coinbase":true})
uint64_t LP_value_extract(cJSON *obj,int32_t addinterest)
{
double val = 0.; uint64_t value = 0; int32_t electrumflag;
@ -151,9 +138,9 @@ struct LP_utxoinfo *LP_allocated(bits256 txid,int32_t vout)
return(0);
}
int32_t LP_address_utxo_ptrs(int32_t iambob,struct LP_address_utxo **utxos,int32_t max,struct LP_address *ap,char *coinaddr)
int32_t LP_address_utxo_ptrs(struct iguana_info *coin,int32_t iambob,struct LP_address_utxo **utxos,int32_t max,struct LP_address *ap,char *coinaddr)
{
struct LP_address_utxo *up,*tmp; int32_t n = 0;
struct LP_address_utxo *up,*tmp; struct LP_transaction *tx; cJSON *txout; int32_t n = 0; char str[65];
//printf("LP_address_utxo_ptrs for (%s).(%s)\n",ap->coinaddr,coinaddr);
if ( strcmp(ap->coinaddr,coinaddr) != 0 )
printf("UNEXPECTED coinaddr mismatch (%s) != (%s)\n",ap->coinaddr,coinaddr);
@ -163,6 +150,40 @@ int32_t LP_address_utxo_ptrs(int32_t iambob,struct LP_address_utxo **utxos,int32
//char str[65]; printf("LP_address_utxo_ptrs %s n.%d %.8f %s v%d spendheight.%d allocated.%p\n",ap->coinaddr,n,dstr(up->U.value),bits256_str(str,up->U.txid),up->U.vout,up->spendheight,LP_allocated(up->U.txid,up->U.vout));
if ( up->spendheight <= 0 )
{
if ( coin->electrum == 0 )
{
if ( (txout= LP_gettxout(coin->symbol,coinaddr,up->U.txid,up->U.vout)) != 0 )
{
if ( LP_value_extract(txout,0) == 0 )
{
printf("LP_address_utxo_ptrs skip zero value %s/v%d\n",bits256_str(str,up->U.txid),up->U.vout);
free_json(txout);
up->spendheight = 1;
if ( (tx= LP_transactionfind(coin,up->U.txid)) != 0 && up->U.vout < tx->numvouts )
tx->outpoints[up->U.vout].spendheight = 1;
continue;
}
free_json(txout);
}
else
{
printf("LP_address_utxo_ptrs skips %s %s payment %s/v%d is spent\n",coin->symbol,coinaddr,bits256_str(str,up->U.txid),up->U.vout);
up->spendheight = 1;
if ( (tx= LP_transactionfind(coin,up->U.txid)) != 0 && up->U.vout < tx->numvouts )
tx->outpoints[up->U.vout].spendheight = 1;
continue;
}
}
else
{
if ( up->SPV <= 0 || up->U.height == 0 )
{
printf("LP_address_utxo_ptrs skips %s/v%u due to SPV.%d ht.%d\n",bits256_str(str,up->U.txid),up->U.vout,up->SPV,up->U.height);
if ( (tx= LP_transactionfind(coin,up->U.txid)) != 0 && up->U.vout < tx->numvouts )
tx->outpoints[up->U.vout].spendheight = 1;
continue;
}
}
if ( LP_allocated(up->U.txid,up->U.vout) == 0 )
{
utxos[n++] = up;
@ -170,6 +191,11 @@ int32_t LP_address_utxo_ptrs(int32_t iambob,struct LP_address_utxo **utxos,int32
break;
}
}
else
{
if ( (tx= LP_transactionfind(coin,up->U.txid)) != 0 && up->U.vout < tx->numvouts )
tx->outpoints[up->U.vout].spendheight = 1;
}
}
portable_mutex_unlock(&LP_utxomutex);
//printf("return n.%d\n",n);
@ -191,7 +217,24 @@ struct LP_address_utxo *LP_address_utxofind(struct iguana_info *coin,char *coina
return(0);
}
int32_t LP_address_utxoadd(struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight)
void LP_mark_spent(char *symbol,bits256 txid,int32_t vout)
{
struct iguana_info *coin; struct LP_transaction *tx; struct LP_address_utxo *up;
if ( (coin= LP_coinfind(symbol)) != 0 )
{
if ( (tx= LP_transactionfind(coin,txid)) != 0 )
{
if ( vout < tx->numvouts )
{
tx->outpoints[vout].spendheight = 1;
if ( (up= LP_address_utxofind(coin,tx->outpoints[vout].coinaddr,txid,vout)) != 0 )
up->spendheight = 1;
}
}
}
}
int32_t LP_address_utxoadd(char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight)
{
struct LP_address *ap; cJSON *txobj; struct LP_address_utxo *up,*tmp; int32_t flag,retval = 0; char str[65];
if ( coin == 0 )
@ -208,21 +251,17 @@ int32_t LP_address_utxoadd(struct iguana_info *coin,char *coinaddr,bits256 txid,
if ( vout == up->U.vout && bits256_cmp(up->U.txid,txid) == 0 )
{
flag = 1;
if ( height > 0 && up->U.height != height && up->SPV <= 0 )
{
if ( up->U.height > 0 )
printf("%s SPV.%d update %s/v%d up->U.height %d <- %d\n",coin->symbol,up->SPV,bits256_str(str,up->U.txid),up->U.vout,up->U.height,height);
if ( height > 0 && up->U.height != height )
up->U.height = height, flag |= 2;
}
if ( spendheight > 0 && up->spendheight != spendheight )
up->spendheight = spendheight, flag |= 4;
if ( up->U.value == 0 && up->U.value != value )
if ( value != 0 && up->U.value == 0 && up->U.value != value )
up->U.value = value, flag |= 8;
//printf("found >>>>>>>>>> %s %s %s/v%d ht.%d %.8f\n",coin->symbol,coinaddr,bits256_str(str,txid),vout,height,dstr(value));
break;
}
}
if ( flag == 0 )
if ( flag == 0 && value != 0 )
{
if ( coin->electrum == 0 )
{
@ -240,10 +279,10 @@ int32_t LP_address_utxoadd(struct iguana_info *coin,char *coinaddr,bits256 txid,
up->spendheight = spendheight;
portable_mutex_lock(&coin->addrmutex);
DL_APPEND(ap->utxos,up);
portable_mutex_unlock(&coin->addrmutex);
portable_mutex_unlock(&coin->addrmutex);
retval = 1;
if ( 0 && height > 0 && strcmp("REVS",coin->symbol) == 0 )
printf("ADD UTXO >> %s %s %s/v%d ht.%d %.8f\n",coin->symbol,coinaddr,bits256_str(str,txid),vout,height,dstr(value));
if ( value == 0 )
printf("%s ADD UTXO >> %s %s %s/v%d ht.%d %.8f\n",debug,coin->symbol,coinaddr,bits256_str(str,txid),vout,height,dstr(value));
}
} // else printf("cant get ap %s %s\n",coin->symbol,coinaddr);
//printf("done %s add addr.%s ht.%d\n",coin->symbol,coinaddr,height);
@ -257,7 +296,8 @@ cJSON *LP_address_item(struct iguana_info *coin,struct LP_address_utxo *up,int32
{
jaddbits256(item,"txid",up->U.txid);
jaddnum(item,"vout",up->U.vout);
jaddnum(item,"confirmations",LP_getheight(coin) - up->U.height + 1);
if ( up->U.height > 0 )
jaddnum(item,"confirmations",LP_getheight(coin) - up->U.height + 1);
jaddnum(item,"amount",dstr(up->U.value));
jaddstr(item,"scriptPubKey","");
}
@ -267,6 +307,8 @@ cJSON *LP_address_item(struct iguana_info *coin,struct LP_address_utxo *up,int32
jaddnum(item,"tx_pos",up->U.vout);
jaddnum(item,"height",up->U.height);
jadd64bits(item,"value",up->U.value);
if ( up->U.value == 0 )
printf("ERROR LP_address_item illegal.(%s)\n",jprint(item,0));
}
return(item);
}
@ -346,7 +388,11 @@ int32_t LP_merkleproof(struct iguana_info *coin,struct electrum_info *ep,bits256
} else printf("couldnt get header for ht.%d\n",height);
}
if ( SPV < 0 )
printf("MERKLE DIDNT VERIFY.(%s)\n",jprint(merkobj,0));
{
printf("MERKLE DIDNT VERIFY.%s %s ht.%d (%s)\n",coin->symbol,bits256_str(str,txid),height,jprint(merkobj,0));
if ( jobj(merkobj,"error") != 0 )
SPV = 0; // try again later
}
free_json(merkobj);
}
return(SPV);
@ -354,15 +400,10 @@ int32_t LP_merkleproof(struct iguana_info *coin,struct electrum_info *ep,bits256
cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrumret)
{
cJSON *array,*item; int32_t n; uint64_t total; struct LP_address *ap=0,*atmp; struct LP_address_utxo *up,*tmp; cJSON *txobj; struct electrum_info *ep,*backupep=0;
cJSON *array,*item; int32_t n; uint64_t total; struct LP_address *ap=0,*atmp; struct LP_address_utxo *up,*tmp; cJSON *txobj;
array = cJSON_CreateArray();
if ( coinaddr != 0 && coinaddr[0] != 0 )
{
if ( (ep= coin->electrum) != 0 )
{
if ( (backupep= ep->prev) == 0 )
backupep = ep;
}
//portable_mutex_lock(&coin->addrmutex);
if ( (ap= _LP_addressfind(coin,coinaddr)) != 0 )
{
@ -377,9 +418,9 @@ cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrum
up->spendheight = 1;
else free_json(txobj);
}
if ( up->spendheight <= 0 )
if ( up->spendheight <= 0 && up->U.value != 0 )
{
if ( up->SPV >= 0 )
if ( coin->electrum == 0 || up->SPV > 0 )
{
jaddi(array,LP_address_item(coin,up,electrumret));
n++;
@ -465,7 +506,7 @@ int32_t LP_unspents_array(struct iguana_info *coin,char *coinaddr,cJSON *array)
if ( errs == 0 )
{
//printf("from LP_unspents_array\n");
LP_address_utxoadd(coin,coinaddr,txid,v,val,height,-1);
LP_address_utxoadd("LP_unspents_array",coin,coinaddr,txid,v,val,height,-1);
count++;
}
}
@ -566,32 +607,6 @@ struct LP_transaction *LP_transactionadd(struct iguana_info *coin,bits256 txid,i
return(tx);
}
uint64_t LP_txinterestvalue(uint64_t *interestp,char *destaddr,struct iguana_info *coin,bits256 txid,int32_t vout)
{
uint64_t interest,value = 0; cJSON *txobj;
*interestp = 0;
destaddr[0] = 0;
if ( (txobj= LP_gettxout(coin->symbol,destaddr,txid,vout)) != 0 )
{
if ( (value= LP_value_extract(txobj,0)) == 0 )
{
char str[65]; printf("%s LP_txvalue.%s strange utxo.(%s) vout.%d\n",coin->symbol,bits256_str(str,txid),jprint(txobj,0),vout);
}
else if ( strcmp(coin->symbol,"KMD") == 0 )
{
if ( (interest= jdouble(txobj,"interest")) != 0. )
{
//printf("add interest of %.8f to %.8f\n",interest,dstr(value));
*interestp = SATOSHIDEN * interest;
}
}
LP_destaddr(destaddr,txobj);
//char str[65]; printf("dest.(%s) %.8f <- %s.(%s) txobj.(%s)\n",destaddr,dstr(value),coin->symbol,bits256_str(str,txid),jprint(txobj,0));
free_json(txobj);
} //else { char str[65]; printf("null gettxout return %s/v%d\n",bits256_str(str,txid),vout); }
return(value);
}
cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJSON *txobj)
{
struct LP_transaction *tx; int32_t i,height,numvouts,numvins,spentvout; cJSON *vins,*vouts,*vout,*vin; bits256 spenttxid; char str[65];
@ -614,8 +629,8 @@ cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJS
tx->outpoints[i].value = LP_value_extract(vout,0);
tx->outpoints[i].interest = SATOSHIDEN * jdouble(vout,"interest");
LP_destaddr(tx->outpoints[i].coinaddr,vout);
//printf("from transaction init\n");
//LP_address_utxoadd(coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1);
//printf("from transaction init %s %s %s/v%d <- %.8f\n",coin->symbol,tx->outpoints[i].coinaddr,bits256_str(str,txid),i,dstr(tx->outpoints[i].value));
LP_address_utxoadd("LP_transactioninit iter0",coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1);
}
//printf("numvouts.%d\n",numvouts);
}
@ -637,7 +652,7 @@ cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJS
tx->outpoints[spentvout].spendtxid = txid;
tx->outpoints[spentvout].spendvini = i;
tx->outpoints[spentvout].spendheight = height > 0 ? height : 1;
LP_address_utxoadd(coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1);
LP_address_utxoadd("LP_transactioninit iter1",coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1);
if ( 0 && strcmp(coin->symbol,"REVS") == 0 )
printf("spend %s %s/v%d at ht.%d\n",coin->symbol,bits256_str(str,tx->txid),spentvout,height);
}
@ -714,6 +729,35 @@ int32_t LP_numconfirms(char *symbol,char *coinaddr,bits256 txid,int32_t vout,int
return(numconfirms);
}
uint64_t LP_txinterestvalue(uint64_t *interestp,char *destaddr,struct iguana_info *coin,bits256 txid,int32_t vout)
{
uint64_t interest,value = 0; cJSON *txobj;
*interestp = 0;
destaddr[0] = 0;
if ( (txobj= LP_gettxout(coin->symbol,destaddr,txid,vout)) != 0 )
{
if ( (value= LP_value_extract(txobj,0)) == 0 )
{
char str[65]; printf("%s LP_txvalue.%s strange utxo.(%s) vout.%d\n",coin->symbol,bits256_str(str,txid),jprint(txobj,0),vout);
}
else if ( strcmp(coin->symbol,"KMD") == 0 )
{
if ( coin->electrum == 0 )
{
if ((interest= jdouble(txobj,"interest")) != 0. )
{
//printf("add interest of %.8f to %.8f\n",interest,dstr(value));
*interestp = SATOSHIDEN * interest;
}
} else *interestp = LP_komodo_interest(txid,value);
}
LP_destaddr(destaddr,txobj);
//char str[65]; printf("dest.(%s) %.8f <- %s.(%s) txobj.(%s)\n",destaddr,dstr(value),coin->symbol,bits256_str(str,txid),jprint(txobj,0));
free_json(txobj);
} //else { char str[65]; printf("null gettxout return %s/v%d\n",bits256_str(str,txid),vout); }
return(value);
}
int64_t basilisk_txvalue(char *symbol,bits256 txid,int32_t vout)
{
char destaddr[64]; uint64_t value,interest = 0; struct iguana_info *coin;
@ -726,7 +770,7 @@ int64_t basilisk_txvalue(char *symbol,bits256 txid,int32_t vout)
uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
{
struct LP_transaction *tx; cJSON *txobj=0; uint64_t value; struct iguana_info *coin; char str[65],_coinaddr[65];
struct LP_transaction *tx; cJSON *txobj=0; struct iguana_info *coin;
if ( bits256_nonz(txid) == 0 )
return(0);
if ( (coin= LP_coinfind(symbol)) == 0 || coin->inactive != 0 )
@ -745,7 +789,7 @@ uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
{
if ( vout < tx->numvouts )
{
if ( bits256_nonz(tx->outpoints[vout].spendtxid) != 0 )
/*if ( bits256_nonz(tx->outpoints[vout].spendtxid) != 0 )
{
//printf("LP_txvalue %s/v%d is spent at %s\n",bits256_str(str,txid),vout,bits256_str(str2,tx->outpoints[vout].spendtxid));
return(0);
@ -760,11 +804,13 @@ uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
//printf("(%s) return value %.8f + interest %.8f\n",coinaddr,dstr(tx->outpoints[vout].value),dstr(tx->outpoints[vout].interest));
}
return(tx->outpoints[vout].value + 0*tx->outpoints[vout].interest);
}
}*/
return(tx->outpoints[vout].value);
} else printf("LP_txvalue vout.%d >= tx->numvouts.%d\n",vout,tx->numvouts);
}
else
else if ( coin->electrum == 0 )
{
uint64_t value; char _coinaddr[64];
if ( (txobj= LP_gettxout(coin->symbol,coinaddr,txid,vout)) != 0 )
{
value = LP_value_extract(txobj,0);//SATOSHIDEN * (jdouble(txobj,"value") + jdouble(txobj,"interest"));
@ -778,7 +824,7 @@ uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
return(value);
}
}
printf("pruned node? LP_txvalue couldnt find %s tx %s/v%d (%s)\n",coin->symbol,bits256_str(str,txid),vout,txobj!=0?jprint(txobj,0):"");
//printf("pruned node? LP_txvalue couldnt find %s tx %s/v%d (%s)\n",coin->symbol,bits256_str(str,txid),vout,txobj!=0?jprint(txobj,0):"");
if ( txobj != 0 )
free_json(txobj);
}
@ -787,8 +833,7 @@ uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
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)
{
//struct LP_utxoinfo *utxo; struct LP_address_utxo *up;
uint64_t val,val2=0,txfee,threshold=0; int32_t bypass = 0; char destaddr[64],destaddr2[64],str[65]; struct iguana_info *coin = LP_coinfind(symbol);
uint64_t val,val2=0,txfee,threshold=0; int32_t bypass = 0; char destaddr[64],destaddr2[64]; struct LP_transaction *tx; struct LP_address_utxo *up; struct iguana_info *coin = LP_coinfind(symbol);
if ( bits256_nonz(txid) == 0 || bits256_nonz(txid2) == 0 )
{
printf("null txid not eligible\n");
@ -800,16 +845,6 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
if ( bypass != 0 )
val = satoshis;
else val = LP_txvalue(destaddr,symbol,txid,vout);
/*if ( (up= LP_address_utxofind(coin,destaddr,txid,vout)) != 0 && up->spendheight > 0 )
{
//printf("%s/v%d spent %d\n",bits256_str(str,txid),vout,up->spendheight);
return(-2);
}
if ( (up= LP_address_utxofind(coin,destaddr,txid2,vout2)) != 0 && up->spendheight > 0 )
{
//printf("%s/v%d spent %d\n",bits256_str(str,txid2),vout2,up->spendheight);
return(-3);
}*/
txfee = LP_txfeecalc(LP_coinfind(symbol),0,0);
if ( val >= satoshis && val > (1+LP_MINSIZE_TXFEEMULT)*txfee )
{
@ -827,18 +862,31 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
{
*valp = val;
*val2p = val2;
if ( destaddr[0] == 0 )
strcpy(destaddr,destaddr2);
if ( coin != 0 )
{
if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts && tx->outpoints[vout].spendheight > 0 )
return(0);
if ( (tx= LP_transactionfind(coin,txid2)) != 0 && vout2 < tx->numvouts && tx->outpoints[vout2].spendheight > 0 )
return(0);
if ( (up= LP_address_utxofind(coin,destaddr,txid,vout)) != 0 && up->spendheight > 0 )
return(0);
if ( (up= LP_address_utxofind(coin,destaddr,txid2,vout2)) != 0 && up->spendheight > 0 )
return(0);
}
return(1);
}
} // else printf("no val2\n");
}
char str2[65];
/*char str2[65];
if ( val != 0 && val2 != 0 )
printf("spent.%d %s txid or value %.8f < %.8f or val2 %.8f < %.8f, %s/v%d %s/v%d or < 10x txfee %.8f\n",iambob,symbol,dstr(val),dstr(satoshis),dstr(val2),dstr(threshold),bits256_str(str,txid),vout,bits256_str(str2,txid2),vout2,dstr(txfee));
if ( val == 0 )
LP_address_utxoadd(coin,destaddr,txid,vout,satoshis,-1,1);
if ( val2 == 0 )
LP_address_utxoadd(coin,destaddr,txid2,vout2,threshold,-1,1);
/*for (iter=0; iter<2; iter++)
for (iter=0; iter<2; iter++)
{
if ( (utxo= LP_utxofind(iter,txid,vout)) != 0 )
{

46
iguana/exchanges/LP_utxos.c

@ -265,51 +265,15 @@ void LP_spentnotify(struct LP_utxoinfo *utxo,int32_t selector)
utxo->T.spentflag = (uint32_t)time(NULL);
}
/*char *LP_spentcheck(cJSON *argjson)
{
bits256 txid,checktxid; int32_t vout,checkvout; struct LP_utxoinfo *utxo; int32_t iambob,retval = 0;
txid = jbits256(argjson,"txid");
vout = jint(argjson,"vout");
for (iambob=0; iambob<=1; iambob++)
{
if ( (utxo= LP_utxofind(iambob,txid,vout)) != 0 && utxo->T.spentflag == 0 )
{
if ( jobj(argjson,"check") == 0 )
checktxid = txid, checkvout = vout;
else
{
checktxid = jbits256(argjson,"checktxid");
checkvout = jint(argjson,"checkvout");
}
if ( LP_txvalue(0,utxo->coin,checktxid,checkvout) == 0 )
{
//if ( LP_mypeer != 0 && LP_mypeer->numutxos > 0 )
// LP_mypeer->numutxos--;
utxo->T.spentflag = (uint32_t)time(NULL);
retval++;
printf("indeed txid was spent\n");
}
}
}
if ( retval > 0 )
return(clonestr("{\"result\":\"marked as spent\"}"));
return(clonestr("{\"error\":\"cant find txid to check spent status\"}"));
}*/
struct LP_utxoinfo *LP_utxoadd(int32_t iambob,char *symbol,bits256 txid,int32_t vout,int64_t value,bits256 txid2,int32_t vout2,int64_t value2,char *coinaddr,bits256 pubkey,char *gui,uint32_t sessionid)
{
uint64_t val,val2=0,tmpsatoshis,txfee; int32_t spendvini,numconfirms,selector; bits256 spendtxid; struct iguana_info *coin; struct _LP_utxoinfo u; struct LP_utxoinfo *utxo = 0;
if ( symbol == 0 || symbol[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(txid2) == 0 || vout < 0 || vout2 < 0 || value <= 0 || value2 <= 0 )//|| sessionid == 0 )
{
char str[65],str2[65]; printf("REJECT %s iambob.%d %s utxoadd.(%.8f %.8f) %s %s\n",coinaddr,iambob,symbol,dstr(value),dstr(value2),bits256_str(str,txid),bits256_str(str2,txid2));
char str[65],str2[65]; printf("REJECT %s iambob.%d %s utxoadd.(%.8f %.8f) %s/v%d %s/v%d\n",coinaddr,iambob,symbol,dstr(value),dstr(value2),bits256_str(str,txid),vout,bits256_str(str2,txid2),vout2);
printf("session.%u addutxo %d %d %d %d %d %d %d %d\n",sessionid,symbol == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(txid2) == 0,vout < 0,vout2 < 0,value <= 0,value2 <= 0);
return(0);
}
/*if ( iambob != 0 )
{
printf("deprecated bob utxos\n");
return(0);
}*/
if ( (coin= LP_coinfind(symbol)) == 0 || (IAMLP == 0 && coin->inactive != 0) )
{
//printf("LP_utxoadd reject inactive %s\n",symbol);
@ -453,10 +417,7 @@ cJSON *LP_inventory(char *symbol)
myipaddr = LP_mypeer->ipaddr;
else myipaddr = "127.0.0.1";
if ( (coin= LP_coinfind(symbol)) != 0 )
{
coin->unspenttime = (uint32_t)time(NULL) - 777;
LP_listunspent_both(symbol,coin->smartaddr,0);
}
HASH_ITER(hh,G.LP_utxoinfos[iambob],utxo,tmp)
{
char str[65];
@ -555,12 +516,12 @@ int32_t LP_privkey_init(int32_t mypubsock,struct iguana_info *coin,bits256 mypri
}
satoshis = LP_txvalue(destaddr,coin->symbol,txid,vout);
if ( satoshis != 0 && satoshis != value )
printf("unexpected privkey_init value mismatch %.8f vs %.8f (%s) %.8f %.8f\n",dstr(satoshis),dstr(value),jprint(item,0),jdouble(item,"amount"),jdouble(item,"interest"));
printf("%s %s unexpected privkey_init value mismatch %.8f vs %.8f (%s) %.8f %.8f\n",coin->symbol,coin->smartaddr,dstr(satoshis),dstr(value),jprint(item,0),jdouble(item,"amount"),jdouble(item,"interest"));
if ( LP_inventory_prevent(iambob,coin->symbol,txid,vout) == 0 && height > 0 )
{
//printf("%s\n",jprint(item,0));
values[i] = satoshis;
flag += LP_address_utxoadd(coin,destaddr,txid,vout,satoshis,height,-1);
//flag += LP_address_utxoadd(coin,destaddr,txid,vout,satoshis,height,-1);
} else used++;
}
//printf("array.%d\n",n);
@ -792,6 +753,7 @@ void LP_privkey_updates(void *ctx,int32_t pubsock,char *passphrase)
//printf("i.%d of %d\n",i,LP_numcoins);
else if ( IAMLP == 0 || coin->inactive == 0 )
{
//printf("from updates %s\n",coin->symbol);
if ( LP_privkey_init(pubsock,coin,G.LP_privkey,G.LP_mypub25519) == 0 && (rand() % 10) == 0 )
LP_postutxos(coin->symbol,coin->smartaddr);
}

8
iguana/exchanges/Makefile

@ -0,0 +1,8 @@
#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
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

5
iguana/exchanges/coins.json

File diff suppressed because one or more lines are too long

2
iguana/exchanges/install

@ -1,5 +1,5 @@
#!/bin/bash
cp trust trusted setconfirms balance listunspent electrum snapshot_balance snapshot_loop secretaddresses dividends snapshot goals goal portfolio autoprice deletemessages getmessages debug buy sell bestfit orderbook client run_osx client_osx run coins disable enable myprice myprices getcoins getpeers getpeersIP getprices help inv setprice status ../dexscripts
cp notarizations getrawtransaction parselog statsdisp m_js trust trusted setconfirms balance listunspent electrum snapshot_balance snapshot_loop secretaddresses dividends snapshot goals goal portfolio autoprice deletemessages getmessages debug buy sell bestfit orderbook client run_osx client_osx run coins disable enable myprice myprices getcoins getpeers getpeersIP getprices help inv setprice status ../dexscripts
cp coins.json ..
cd ../dexscripts
#cp ../exchanges/passphrase ../exchanges/userpass .

4
iguana/exchanges/m_js

@ -0,0 +1,4 @@
#!/bin/sh
cd ..
make -f exchanges/Makefile

18
iguana/exchanges/mm.c

@ -19,13 +19,17 @@
// Copyright © 2017 SuperNET. All rights reserved.
//
void PNACL_message(char *arg,...)
{
}
#define FROM_MARKETMAKER
#include <stdio.h>
#include <stdint.h>
#ifndef NATIVE_WINDOWS
#include "OS_portable.h"
#include "OS_portable.h"
#else
#include "../../crypto777/OS_portable.h"
#include "../../crypto777/OS_portable.h"
#endif // !_WIN_32
@ -35,7 +39,7 @@ char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,char *r
void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]);
//defined(__APPLE__) ||
#if defined(WIN32) || defined(USE_STATIC_NANOMSG)
#ifdef FROM_JS // defined(WIN32) || defined(USE_STATIC_NANOMSG)
#include "../../crypto777/nanosrc/nn.h"
#include "../../crypto777/nanosrc/bus.h"
#include "../../crypto777/nanosrc/pubsub.h"
@ -878,6 +882,13 @@ int main(int argc, const char * argv[])
sprintf(dirname,"%s",GLOBAL_DBDIR), OS_ensure_directory(dirname);
sprintf(dirname,"%s/SWAPS",GLOBAL_DBDIR), OS_ensure_directory(dirname);
sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname);
#ifdef FROM_JS
argc = 2;
retjson = cJSON_Parse("{\"client\":1,\"passphrase\":\"test\"}");
printf("calling LP_main(%s)\n",jprint(retjson,0));
LP_main(retjson);
emscripten_set_main_loop(LP_fromjs_iter,1,0);
#else
if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 )
{
if ( (passphrase= jstr(retjson,"passphrase")) == 0 )
@ -949,5 +960,6 @@ int main(int argc, const char * argv[])
}
free_json(retjson);
}
#endif
return 0;
}

3
iguana/exchanges/parselog

@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"parselog\"}"

4
iguana/exchanges/stats.c

@ -328,11 +328,7 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
j = i = 0;
filetype[0] = 0;
//printf("url.(%s) method.(%s)\n",&url[i],urlmethod);
#ifdef __PNACL__
snprintf(furl,sizeof(furl),"%s/%s",GLOBAL_DBDIR,url+1);
#else
snprintf(furl,sizeof(furl),"%s",url+1);
#endif
if ( strcmp(&url[i],"/") == 0 && strcmp(urlmethod,"GET") == 0 )
{
*jsonflagp = 1;

3
iguana/exchanges/statsdisp

@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"statsdisp\"}"
Loading…
Cancel
Save