Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
ce9e476d6f
  1. 3
      crypto777/OS_portable.h
  2. 6
      crypto777/hmac_sha512.c
  3. 27
      crypto777/iguana_utils.c
  4. 277
      crypto777/scrypt.c
  5. 2
      iguana/SuperNET_category.c
  6. 1008
      iguana/chart.html
  7. 1
      iguana/exchanges777.h
  8. 3
      iguana/iguana777.c
  9. 14
      iguana/iguana777.h
  10. 2
      iguana/iguana_accept.c
  11. 18
      iguana/iguana_blocks.c
  12. 2
      iguana/iguana_bundles.c
  13. 58
      iguana/iguana_chains.c
  14. 2
      iguana/iguana_exchanges.c
  15. 4
      iguana/iguana_init.c
  16. 6
      iguana/iguana_instantdex.c
  17. 16
      iguana/iguana_msg.c
  18. 9
      iguana/iguana_payments.c
  19. 18
      iguana/iguana_ramchain.c
  20. 9
      iguana/iguana_sign.c
  21. 348
      iguana/iguana_stake.c
  22. 4
      iguana/iguana_tx.c
  23. 2
      iguana/iguana_volatiles.c
  24. 6
      iguana/iguana_wallet.c
  25. 2
      iguana/js/api.js
  26. 59
      iguana/js/blockexplorer.js
  27. 13
      iguana/main.c
  28. 2
      iguana/mingw32
  29. 577
      iguana/mini-gmp.c
  30. 148
      iguana/mini-gmp.h
  31. 22
      iguana/swaps/iguana_BTCswap.c
  32. 2
      iguana/tests/decoderawtransaction
  33. 1
      includes/curve25519.h

3
crypto777/OS_portable.h

@ -364,11 +364,14 @@ char *bits256_lstr(char hexstr[65],bits256 x);
bits256 bits256_add(bits256 a,bits256 b);
int32_t bits256_cmp(bits256 a,bits256 b);
bits256 bits256_lshift(bits256 x);
bits256 bits256_rshift(bits256 x);
bits256 bits256_from_compact(uint32_t c);
uint32_t bits256_to_compact(bits256 x);
bits256 bits256_conv(char *hexstr);
int32_t btc_priv2pub(uint8_t pubkey[33],uint8_t privkey[32]);
void calc_shares(unsigned char *shares,unsigned char *secret,int32_t size,int32_t width,int32_t M,int32_t N,unsigned char *sharenrs);
int32_t OS_portable_rmdir(char *dirname,int32_t diralso);
void calc_hmac_sha256(uint8_t *mac,int32_t maclen,uint8_t *key,int32_t key_size,uint8_t *message,int32_t len);
extern char *Iguana_validcommands[];
extern bits256 GENESIS_PUBKEY,GENESIS_PRIVKEY;

6
crypto777/hmac_sha512.c

@ -519,6 +519,12 @@ char *hmac_sha384_str(char *dest,char *key,int32_t key_size,char *message)
return(dest);
}
void calc_hmac_sha256(uint8_t *mac,int32_t maclen,uint8_t *key,int32_t key_size,uint8_t *message,int32_t len)
{
unsigned long size = maclen;
hmac_memory(&sha256_desc,(void *)key,key_size,(void *)message,len,mac,&size);
}
char *hmac_sha256_str(char *dest,char *key,int32_t key_size,char *message)
{
unsigned char mac[1024]; unsigned long size = sizeof(mac);

27
crypto777/iguana_utils.c

@ -112,6 +112,18 @@ bits256 bits256_lshift(bits256 x)
return(x);
}
bits256 bits256_rshift(bits256 x)
{
int32_t i; uint64_t carry,prevcarry = 0;
for (i=3; i>=0; i--)
{
carry = (1 & x.ulongs[i]) << 63;
x.ulongs[i] = prevcarry | (x.ulongs[i] >> 1);
prevcarry = carry;
}
return(x);
}
bits256 bits256_from_compact(uint32_t c)
{
uint32_t nbytes,nbits,i; bits256 x;
@ -124,6 +136,21 @@ bits256 bits256_from_compact(uint32_t c)
return(x);
}
uint32_t bits256_to_compact(bits256 x)
{
int32_t i; uint32_t nbits;
for (i=31; i>2; i--)
if ( x.bytes[i] != 0 )
break;
if ( (x.bytes[i] & 0x80) != 0 )
i++;
nbits = x.bytes[i] << 16;
nbits |= x.bytes[i-1] << 8;
nbits |= x.bytes[i-2];
nbits |= ((i+1) << 24);
return(nbits);
}
int32_t bitweight(uint64_t x)
{
int i,wt = 0;

277
crypto777/scrypt.c

@ -0,0 +1,277 @@
/*-
* Copyright 2009 Colin Percival, 2011 ArtForz, 2011 pooler, 2013 Balthazar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file was originally written by Colin Percival as part of the Tarsnap
* online backup system.
*/
#include "../includes/curve25519.h"
#define SCRYPT_BUFFER_SIZE (131072 + 63)
/*
static inline uint32_t be32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
}
static inline void be32enc(void *pp,uint32_t x)
{
uint8_t *p = (uint8_t *)pp;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
void HMAC_SHA256_Init(HMAC_SHA256_CTX *ctx,const void * _K,size_t Klen)
{
size_t i; uint8_t pad[64],khash[32]; const uint8_t * K = (const uint8_t *)_K;
// If Klen > 64, the key is really SHA256(K).
if ( Klen > 64 )
{
SHA256_Init(&ctx->ictx);
SHA256_Update(&ctx->ictx, K, Klen);
SHA256_Final(khash, &ctx->ictx);
K = khash;
Klen = 32;
}
// Inner SHA256 operation is SHA256(K xor [block of 0x36] || data).
SHA256_Init(&ctx->ictx);
memset(pad, 0x36, 64);
for (i = 0; i < Klen; i++)
pad[i] ^= K[i];
SHA256_Update(&ctx->ictx, pad, 64);
// Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash).
SHA256_Init(&ctx->octx);
memset(pad, 0x5c, 64);
for (i = 0; i < Klen; i++)
pad[i] ^= K[i];
SHA256_Update(&ctx->octx, pad, 64);
// Clean the stack.
memset(khash,0,32);
}
// Add bytes to the HMAC-SHA256 operation.
void HMAC_SHA256_Update(HMAC_SHA256_CTX *ctx,const void *in,size_t len)
{
SHA256_Update(&ctx->ictx,in,len);
}
// Finish an HMAC-SHA256 operation.
void HMAC_SHA256_Final(uint8_t digest[32],HMAC_SHA256_CTX *ctx)
{
uint8_t ihash[32];
SHA256_Final(ihash,&ctx->ictx);
SHA256_Update(&ctx->octx,ihash,32);
SHA256_Final(digest,&ctx->octx);
memset(ihash,0,32);
}
// PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf. The value dkLen must be at most 32 * (2^32 - 1)
void PBKDF2_SHA256(const uint8_t *passwd,size_t passwdlen,const uint8_t *salt,size_t saltlen,uint64_t c,uint8_t *buf,size_t dkLen)
{
HMAC_SHA256_CTX PShctx, hctx;
size_t i,clen; uint8_t ivec[4],U[32],T[32]; uint64_t j; int32_t k;
// Compute HMAC state after processing P and S.
HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
HMAC_SHA256_Update(&PShctx, salt, saltlen);
// Iterate through the blocks.
for (i=0; i*32<dkLen; i++)
{
// Generate INT(i + 1).
be32enc(ivec,(uint32_t)(i + 1));
// Compute U_1 = PRF(P, S || INT(i)).
memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
HMAC_SHA256_Update(&hctx, ivec, 4);
HMAC_SHA256_Final(U, &hctx);
// T_i = U_1 ...
memcpy(T,U,32);
for (j=2; j<=c; j++)
{
// Compute U_j.
HMAC_SHA256_Init(&hctx, passwd, passwdlen);
HMAC_SHA256_Update(&hctx, U, 32);
HMAC_SHA256_Final(U, &hctx);
// ... xor U_j ...
for (k=0; k<32; k++)
T[k] ^= U[k];
}
// Copy as many bytes as necessary into buf
clen = dkLen - i * 32;
if (clen > 32)
clen = 32;
memcpy(&buf[i * 32],T,clen);
}
}*/
// Generic scrypt_core implementation
static inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16])
{
int32_t i; uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
x00 = (B[0] ^= Bx[0]);
x01 = (B[1] ^= Bx[1]);
x02 = (B[2] ^= Bx[2]);
x03 = (B[3] ^= Bx[3]);
x04 = (B[4] ^= Bx[4]);
x05 = (B[5] ^= Bx[5]);
x06 = (B[6] ^= Bx[6]);
x07 = (B[7] ^= Bx[7]);
x08 = (B[8] ^= Bx[8]);
x09 = (B[9] ^= Bx[9]);
x10 = (B[10] ^= Bx[10]);
x11 = (B[11] ^= Bx[11]);
x12 = (B[12] ^= Bx[12]);
x13 = (B[13] ^= Bx[13]);
x14 = (B[14] ^= Bx[14]);
x15 = (B[15] ^= Bx[15]);
for (i = 0; i < 8; i += 2) {
#define R(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
/* Operate on columns. */
x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7);
x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7);
x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9);
x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9);
x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13);
x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13);
x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18);
x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18);
/* Operate on rows. */
x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7);
x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7);
x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9);
x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9);
x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13);
x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13);
x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18);
x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18);
#undef R
}
B[0] += x00;
B[1] += x01;
B[2] += x02;
B[3] += x03;
B[4] += x04;
B[5] += x05;
B[6] += x06;
B[7] += x07;
B[8] += x08;
B[9] += x09;
B[10] += x10;
B[11] += x11;
B[12] += x12;
B[13] += x13;
B[14] += x14;
B[15] += x15;
}
static inline void scrypt_core(uint32_t *X,uint32_t *V)
{
uint32_t i,j,k;
for (i=0; i<1024; i++)
{
memcpy(&V[i * 32],X,128);
xor_salsa8(&X[0],&X[16]);
xor_salsa8(&X[16],&X[0]);
}
for (i=0; i<1024; i++)
{
j = 32 * (X[16] & 1023);
for (k = 0; k < 32; k++)
X[k] ^= V[j + k];
xor_salsa8(&X[0],&X[16]);
xor_salsa8(&X[16],&X[0]);
}
}
/* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
r = 1, p = 1, N = 1024
*/
bits256 scrypt_nosalt(const void *input,size_t inputlen,void *scratchpad)
{
uint32_t *V; uint32_t X[32]; bits256 result;
memset(result.bytes,0,sizeof(result));
V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
calc_hmac_sha256((void *)X,128,(void *)input,(int32_t)inputlen,(void *)input,(int32_t)inputlen);
//PBKDF2_SHA256((const uint8_t *)input,inputlen,(const uint8_t *)input,inputlen,1,(uint8_t *)X,128);
scrypt_core(X,V);
calc_hmac_sha256((void *)result.bytes,sizeof(result),(void *)input,(int32_t)inputlen,(void *)X,128);
//PBKDF2_SHA256((const uint8_t *)input,inputlen,(uint8_t *)X,128,1,(uint8_t*)&result,32);
return result;
}
bits256 scrypt(const void *data,size_t datalen,const void *salt,size_t saltlen,void *scratchpad)
{
uint32_t *V; uint32_t X[32]; bits256 result;
memset(result.bytes,0,sizeof(result));
V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
calc_hmac_sha256((void *)X,128,(void *)data,(int32_t)datalen,(void *)salt,(int32_t)saltlen);
//PBKDF2_SHA256((const uint8_t *)data,datalen,(const uint8_t *)salt,saltlen,1,(uint8_t *)X,128);
scrypt_core(X,V);
calc_hmac_sha256((void *)result.bytes,sizeof(result),(void *)data,(int32_t)datalen,(void *)X,128);
//PBKDF2_SHA256((const uint8_t *)data,datalen,(uint8_t *)X,128,1,(uint8_t *)&result,32);
return result;
}
bits256 scrypt_hash(const void *input,size_t inputlen)
{
uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
return scrypt_nosalt(input,inputlen,scratchpad);
}
bits256 scrypt_salted_hash(const void *input,size_t inputlen,const void *salt,size_t saltlen)
{
uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
return scrypt(input,inputlen,salt,saltlen,scratchpad);
}
bits256 scrypt_salted_multiround_hash(const void *input,size_t inputlen,const void *salt,size_t saltlen,const uint32_t nRounds)
{
uint32_t i; bits256 resultHash = scrypt_salted_hash(input,inputlen,salt,saltlen);
bits256 transitionalHash = resultHash;
for(i=1; i<nRounds; i++)
{
resultHash = scrypt_salted_hash(input,inputlen,(const void *)&transitionalHash,32);
transitionalHash = resultHash;
}
return resultHash;
}
bits256 scrypt_blockhash(const void *input)
{
uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
return scrypt_nosalt(input,80,scratchpad);
}

2
iguana/SuperNET_category.c

@ -207,7 +207,7 @@ char *bitcoin_hexmsg(struct supernet_info *myinfo,struct category_info *cat,void
char *method="",*agent="",*retstr = 0; int32_t i,j; cJSON *json,*valsobj; struct iguana_info *coin=0; struct iguana_peer *addr;
if ( (json= cJSON_Parse(ptr)) != 0 )
{
printf("bitcoinprocess.(%s)\n",jprint(json,0));
//printf("bitcoinprocess.(%s)\n",jprint(json,0));
agent = jstr(json,"agent");
method = jstr(json,"method");
valsobj = jobj(json,"vals");

1008
iguana/chart.html

File diff suppressed because it is too large

1
iguana/exchanges777.h

@ -200,5 +200,6 @@ struct bitcoin_swapinfo *instantdex_statemachinefind(struct supernet_info *myinf
char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_t *txidp,struct exchange_info *exchange,struct instantdex_accept *ap,cJSON *json);
void tradebot_timeslices(struct exchange_info *exchange);
struct instantdex_stateinfo *BTC_initFSM(int32_t *n);
struct bitcoin_statetx *instantdex_feetx(struct supernet_info *myinfo,struct instantdex_accept *A,struct bitcoin_swapinfo *swap,struct iguana_info *coin);
#endif

3
iguana/iguana777.c

@ -492,7 +492,10 @@ int32_t iguana_utxogen(struct iguana_info *coin,int32_t helperid,int32_t convert
else hdrsi = coin->origbalanceswritten;
for (i=0; i<max; i++)
if ( (bp= coin->bundles[i]) != 0 && bp != coin->current )
{
iguana_volatilespurge(coin,&bp->ramchain);
iguana_volatilesalloc(coin,&bp->ramchain,i < hdrsi);
}
for (; hdrsi<max; hdrsi++)
{
if ( (bp= coin->bundles[hdrsi]) != 0 )

14
iguana/iguana777.h

@ -218,6 +218,7 @@ struct iguana_chain
char userhome[512],serverport[128],userpass[1024];
char use_addmultisig,do_opreturn;
int32_t estblocktime;
bits256 PoWtarget,PoStargets[16]; int32_t numPoStargets,PoSheights[16];
};
struct iguana_msgaddress { uint32_t nTime; uint64_t nServices; uint8_t ip[16]; uint16_t port; } __attribute__((packed));
@ -572,8 +573,8 @@ int32_t iguana_rwmem(int32_t rwflag,uint8_t *serialized,int32_t len,void *endian
int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp);
int32_t iguana_rwvarint32(int32_t rwflag,uint8_t *serialized,uint32_t *int32p);
int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp);
int32_t iguana_rwblock(int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg);
int32_t iguana_serialize_block(bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block);
int32_t iguana_rwblock(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg);
int32_t iguana_serialize_block(struct iguana_chain *chain,bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block);
void iguana_blockconv(struct iguana_block *dest,struct iguana_msgblock *msg,bits256 hash2,int32_t height);
//void iguana_freetx(struct iguana_msgtx *tx,int32_t n);
int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struct OS_memspace *rawmem,struct OS_memspace *txmem,struct OS_memspace *hashmem,struct iguana_msghdr *H,uint8_t *data,int32_t datalen);
@ -620,6 +621,8 @@ void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bi
bits256 iguana_blockhash(struct iguana_info *coin,int32_t height);
#define iguana_blockfind(str,coin,hash2) iguana_blockhashset(str,coin,-1,hash2,0)
struct iguana_block *iguana_blockhashset(char *debugstr,struct iguana_info *coin,int32_t height,bits256 hash2,int32_t createflag);
struct iguana_block *iguana_prevblock(struct iguana_info *coin,struct iguana_block *block,int32_t PoSflag);
uint32_t iguana_targetbits(struct iguana_info *coin,struct iguana_block *hwmchain,struct iguana_block *prev,struct iguana_block *prev2,int32_t PoSflag);
uint32_t iguana_syncs(struct iguana_info *coin);
void iguana_gotdata(struct iguana_info *coin,struct iguana_peer *addr,int32_t height);
@ -801,7 +804,7 @@ int32_t SuperNET_sendmsg(struct supernet_info *myinfo,struct iguana_info *coin,s
int32_t category_peer(struct supernet_info *myinfo,struct iguana_peer *addr,bits256 category,bits256 subhash);
int32_t bitcoin_wif2priv(uint8_t *addrtypep,bits256 *privkeyp,char *wifstr);
int32_t bitcoin_priv2wif(char *wifstr,bits256 privkey,uint8_t addrtype);
bits256 iguana_chaingenesis(bits256 genesishash,char *genesisblock,char *hashalgostr,int32_t version,uint32_t timestamp,uint32_t bits,uint32_t nonce,bits256 merkle_root);
bits256 iguana_chaingenesis(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),bits256 genesishash,char *genesisblock,char *hashalgostr,int32_t version,uint32_t timestamp,uint32_t bits,uint32_t nonce,bits256 merkle_root);
int32_t iguana_send_ConnectTo(struct iguana_info *coin,struct iguana_peer *addr);
cJSON *iguana_txjson(struct iguana_info *coin,struct iguana_txid *tx,int32_t height,struct vin_info *V);
char *iguana_txscan(struct iguana_info *coin,cJSON *json,uint8_t *data,int32_t recvlen,bits256 txid);
@ -984,8 +987,11 @@ struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana
void instantdex_FSMinit();
void iguana_unspentslock(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *vins);
char *iguana_calcrawtx(struct supernet_info *myinfo,struct iguana_info *coin,cJSON **vinsp,cJSON *txobj,int64_t satoshis,char *changeaddr,int64_t txfee,cJSON *addresses,int32_t minconf);
char *iguana_signrawtx(struct supernet_info *myinfo,struct iguana_info *coin,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtx);
char *iguana_signrawtx(struct supernet_info *myinfo,struct iguana_info *coin,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtx,cJSON *privkey);
char *iguana_pollrawtx(queue_t *Q,cJSON **vinsp,uint32_t rawtxtag,double expiration);
bits256 scrypt_blockhash(const void *input);
bits256 iguana_calcblockhash(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),uint8_t *serialized,int32_t len);
uint32_t iguana_targetbits(struct iguana_info *coin,struct iguana_block *hwmchain,struct iguana_block *prev,struct iguana_block *prev2,int32_t PoSflag);
extern int32_t HDRnet,netBLOCKS;

2
iguana/iguana_accept.c

@ -298,7 +298,7 @@ int32_t iguana_peerhdrrequest(struct iguana_info *coin,uint8_t *serialized,int32
if ( (block= bp->blocks[i]) != 0 )
{
iguana_blockunconv(&msgB,block,1);
len += iguana_rwblock(1,&checkhash2,&serialized[sizeof(struct iguana_msghdr) + len],&msgB);
len += iguana_rwblock(coin->chain->hashalgo,1,&checkhash2,&serialized[sizeof(struct iguana_msghdr) + len],&msgB);
flag++;
if ( bits256_cmp(checkhash2,block->RO.hash2) != 0 )
{

18
iguana/iguana_blocks.c

@ -40,6 +40,18 @@ bits256 iguana_merkle(struct iguana_info *coin,bits256 *tree,int32_t txn_count)
#define iguana_blockfind(str,coin,hash2) iguana_blockhashset(str,coin,-1,hash2,0)
struct iguana_block *iguana_prevblock(struct iguana_info *coin,struct iguana_block *block,int32_t PoSflag)
{
int32_t hdrsi,bundlei,height; struct iguana_bundle *bp;
if ( (height= block->height - 1) < 0 )
return(0);
hdrsi = (height / coin->chain->bundlesize);
bundlei = (height % coin->chain->bundlesize);
if ( hdrsi < coin->bundlescount && (bp= coin->bundles[hdrsi]) != 0 )
return(bp->blocks[bundlei]);
else return(0);
}
void _iguana_blocklink(struct iguana_info *coin,struct iguana_block *prev,struct iguana_block *block)
{
char str[65],str2[65]; struct iguana_block *next;
@ -174,7 +186,7 @@ int32_t iguana_blockvalidate(struct iguana_info *coin,int32_t *validp,struct igu
{
bits256 hash2; uint8_t serialized[sizeof(struct iguana_msgblock) + 4096];
*validp = 0;
iguana_serialize_block(&hash2,serialized,block);
iguana_serialize_block(coin->chain,&hash2,serialized,block);
*validp = (memcmp(hash2.bytes,block->RO.hash2.bytes,sizeof(hash2)) == 0);
block->valid = *validp;
char str[65]; char str2[65];
@ -285,12 +297,12 @@ double PoW_from_compact(uint32_t nBits,uint8_t unitval) // NOT consensus safe, b
printf("illegal nBits.%x\n",nBits);
return(0.);
}
if ( (n= ((8* (unitval-3)) - nbits)) != 0 ) // 0x1d00ffff is genesis nBits so we map that to 1.
if ( (n= ((8 * (unitval-3)) - nbits)) != 0 ) // 0x1d00ffff is genesis nBits so we map that to 1.
{
//printf("nbits.%d -> n.%d\n",nbits,n);
if ( n < 64 )
PoW /= (1LL << n);
else // very rare case efficiency not issue
else // rare case efficiency not issue
{
for (i=0; i<n; i++)
PoW /= 2.;

2
iguana/iguana_bundles.c

@ -463,7 +463,7 @@ void iguana_bundlepurgefiles(struct iguana_info *coin,struct iguana_bundle *bp)
{
if ( (fp= fopen(fname,"rb")) != 0 )
{
printf("purge.(%s)\n",fname);
//printf("purge.(%s)\n",fname);
fclose(fp);
if ( OS_removefile(fname,0) > 0 )
coin->peers.numfiles--, m++;

58
iguana/iguana_chains.c

@ -105,10 +105,12 @@ int32_t blockhash_sha256(uint8_t *blockhashp,uint8_t *serialized,int32_t len)
int32_t blockhash_scrypt(uint8_t *blockhashp,uint8_t *serialized,int32_t len)
{
bits256 hash;
vcalc_sha256(0,hash.bytes,serialized,len);
printf("need to implement scrypt hash here\n");
exit(-1);
if ( len == 80 )
*(bits256 *)blockhashp = scrypt_blockhash(serialized);
else memcpy(blockhashp,0,sizeof(*blockhashp));
int32_t i; for (i=0; i<32; i++)
printf("%02x",blockhashp[i]);
printf(" scrypt\n");
return(sizeof(bits256));
}
@ -122,27 +124,37 @@ blockhashfunc iguana_hashalgo(char *hashalgostr)
return(0);
}
bits256 iguana_chaingenesis(bits256 genesishash,char *genesisblock,char *hashalgostr,int32_t version,uint32_t timestamp,uint32_t nBits,uint32_t nonce,bits256 merkle_root)
bits256 iguana_calcblockhash(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),uint8_t *serialized,int32_t len)
{
struct iguana_msgblock msg; int32_t len,blockhashlen; bits256 hash2;
char blockhashstr[256],str3[65]; uint8_t serialized[1024],blockhash[256];
int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len);
bits256 tmp,hash2; int32_t i;
if ( (*hashalgo)(tmp.bytes,serialized,len) != sizeof(bits256) )
memset(tmp.bytes,0,sizeof(hash2));
else if ( hashalgo == blockhash_sha256 )
{
for (i=0; i<32; i++)
hash2.bytes[i] = tmp.bytes[31 - i];
} else return(tmp);
return(hash2);
}
bits256 iguana_chaingenesis(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),bits256 genesishash,char *genesisblock,char *hashalgostr,int32_t version,uint32_t timestamp,uint32_t nBits,uint32_t nonce,bits256 merkle_root)
{
struct iguana_msgblock msg; int32_t len; bits256 hash2; char blockhashstr[256],str3[65]; uint8_t serialized[1024];
memset(&msg,0,sizeof(msg));
msg.H.version = version;
msg.H.merkle_root = merkle_root;
msg.H.timestamp = timestamp;
msg.H.bits = nBits;
msg.H.nonce = nonce;
len = iguana_rwblock(1,&hash2,serialized,&msg);
if ( hashalgostr != 0 && strcmp(hashalgostr,"sha256") != 0 )
hashalgo = iguana_hashalgo(hashalgostr);
else hashalgo = blockhash_sha256;
len = iguana_rwblock(hashalgo,1,&hash2,serialized,&msg);
blockhashstr[0] = 0;
if ( hashalgostr != 0 && strcmp(hashalgostr,"sha256") != 0 && (hashalgo= iguana_hashalgo(hashalgostr)) != 0 )
{
if ( (blockhashlen= (*hashalgo)(blockhash,serialized,len)) > 0 )
init_hexbytes_noT(blockhashstr,blockhash,blockhashlen);
} else init_hexbytes_noT(blockhashstr,hash2.bytes,sizeof(hash2));
init_hexbytes_noT(blockhashstr,hash2.bytes,sizeof(hash2));
char str[65],str2[65];
if ( bits256_cmp(genesishash,hash2) != 0 )
printf("WARNING: genesishash.(%s) mismatch vs calc.(%s)\n",bits256_str(str,genesishash),bits256_str(str2,genesishash));
printf("WARNING: genesishash.(%s) mismatch vs calc.(%s)\n",bits256_str(str,genesishash),bits256_str(str2,hash2));
init_hexbytes_noT(genesisblock,serialized,len);
printf("v.%d t.%u %s nBits.%08x nonce.%u merkle.(%s) genesis.(%s) hash2.(%s) blockhash.(%s) size.%d\n",version,timestamp,utc_str(str3,timestamp),nBits,nonce,bits256_str(str2,merkle_root),genesisblock,bits256_str(str,hash2),blockhashstr,(int32_t)strlen(genesisblock)/2);
return(hash2);
@ -316,7 +328,7 @@ void iguana_chainparms(struct iguana_chain *chain,cJSON *argjson)
((uint8_t *)&nBits)[3] = tmp[0];
}
else nBits = 0x1e00ffff;
hash = iguana_chaingenesis(hash,genesisblock,jstr(genesis,"hashalgo"),juint(genesis,"version"),juint(genesis,"timestamp"),nBits,juint(genesis,"nonce"),jbits256(genesis,"merkle_root"));
hash = iguana_chaingenesis(chain->hashalgo,hash,genesisblock,jstr(genesis,"hashalgo"),juint(genesis,"version"),juint(genesis,"timestamp"),nBits,juint(genesis,"nonce"),jbits256(genesis,"merkle_root"));
//chain->genesis_hash = clonestr(bits256_str(str,hash));
chain->genesis_hex = clonestr(genesisblock);
}
@ -351,9 +363,21 @@ void iguana_chainparms(struct iguana_chain *chain,cJSON *argjson)
void iguana_chaininit(struct iguana_chain *chain,int32_t hasheaders,cJSON *argjson)
{
int32_t i;
if ( strcmp(chain->symbol,"BTCD") == 0 )
{
chain->numPoStargets = 1;
chain->PoSheights[0] = 0;
memset(&chain->PoStargets[0],0xff,sizeof(bits256));
for (i=0; i<20; i++)
chain->PoStargets[0] = bits256_rshift(chain->PoStargets[0]);
chain->PoWtarget = bits256_from_compact(0x1e0fffff);
}
chain->hasheaders = hasheaders;
chain->minoutput = 10000;
chain->hashalgo = blockhash_sha256;
if ( strcmp(chain->symbol,"LTC") == 0 || strcmp(chain->symbol,"VPN") == 0 )
chain->hashalgo = blockhash_scrypt;
else chain->hashalgo = blockhash_sha256;
if ( strcmp(chain->symbol,"BTC") == 0 )
{
chain->unitval = 0x1d;

2
iguana/iguana_exchanges.c

@ -590,6 +590,8 @@ void iguana_statemachineupdate(struct supernet_info *myinfo,struct exchange_info
portable_mutex_lock(&exchange->mutex);
DL_FOREACH_SAFE(exchange->statemachines,swap,tmp)
{
if ( swap->myfee == 0 )
swap->myfee = instantdex_feetx(myinfo,&swap->mine,swap,iguana_coinfind("BTC"));
//printf("FSM.(%llu / %llu) (%s/%s) state.(%s)\n",(long long)swap->mine.orderid,(long long)swap->other.orderid,swap->mine.offer.base,swap->mine.offer.rel,swap->state->name);
}
portable_mutex_unlock(&exchange->mutex);

4
iguana/iguana_init.c

@ -85,8 +85,8 @@ bits256 iguana_genesis(struct iguana_info *coin,struct iguana_chain *chain)
return(hash2);
}
decode_hex(buf,(int32_t)strlen(chain->genesis_hex)/2,(char *)chain->genesis_hex);
hash2 = bits256_doublesha256(0,buf,sizeof(struct iguana_msgblockhdr));
iguana_rwblock(0,&hash2,buf,&msg);
hash2 = iguana_calcblockhash(coin->chain->hashalgo,buf,sizeof(struct iguana_msgblockhdr));
iguana_rwblock(coin->chain->hashalgo,0,&hash2,buf,&msg);
if ( memcmp(hash2.bytes,chain->genesis_hashdata,sizeof(hash2)) != 0 )
{
bits256_str(str,hash2);

6
iguana/iguana_instantdex.c

@ -1176,7 +1176,7 @@ struct bitcoin_swapinfo *bitcoin_swapinit(struct supernet_info *myinfo,struct ex
char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_t *txidp,struct exchange_info *exchange,struct instantdex_accept *ap,cJSON *argjson)
{
struct instantdex_accept *otherap,*tmp; struct iguana_info *coin; struct bitcoin_swapinfo *swap; cJSON *newjson; int32_t isbob = 0;
struct instantdex_accept *otherap,*tmp; struct bitcoin_swapinfo *swap; cJSON *newjson; int32_t isbob = 0;
*addedp = 0;
if ( exchange == 0 )
{
@ -1222,8 +1222,8 @@ char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_
{
printf("STATEMACHINEQ.(%llx / %llx)\n",(long long)swap->mine.orderid,(long long)swap->other.orderid);
//queue_enqueue("acceptableQ",&exchange->acceptableQ,&swap->DL,0);
if ( isbob != 0 && (coin= iguana_coinfind("BTC")) != 0 )
swap->myfee = instantdex_feetx(myinfo,&swap->mine,swap,coin);
//if ( isbob != 0 && (coin= iguana_coinfind("BTC")) != 0 )
// swap->myfee = instantdex_feetx(myinfo,&swap->mine,swap,coin);
instantdex_statemachineadd(exchange,swap);
*addedp = 1;
if ( (newjson= instantdex_parseargjson(myinfo,exchange,swap,argjson,1)) == 0 )

16
iguana/iguana_msg.c

@ -78,16 +78,16 @@ int32_t iguana_rwversion(int32_t rwflag,uint8_t *serialized,struct iguana_msgver
// 06000000996da490f6151ad9d05d9defc99bda58441d2b833c0da69d11e764d7c70a00003378a650b506a66b41097a0b513f2fee899788711bc6643ff976ce6dbb0b620c5f800854ffff0f1e0004de0301010000005e800854010000000000000000000000000000000000000000000000000000000000000000ffffffff03510102ffffffff0100008a5d784563011976a9145166e6e52de58dfacb18670c0030aedcf295233988ac000000000000
int32_t iguana_rwblock(int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg)
int32_t iguana_rwblock(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg)
{
int32_t len = 0; char blockhash[65]; uint64_t x;
int32_t len = 0; uint64_t x;
len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.version),&msg->H.version);
len += iguana_rwbignum(rwflag,&serialized[len],sizeof(msg->H.prev_block),msg->H.prev_block.bytes);
len += iguana_rwbignum(rwflag,&serialized[len],sizeof(msg->H.merkle_root),msg->H.merkle_root.bytes);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.timestamp),&msg->H.timestamp);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.bits),&msg->H.bits);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.nonce),&msg->H.nonce);
*hash2p = bits256_doublesha256(blockhash,serialized,len);
*hash2p = iguana_calcblockhash(hashalgo,serialized,len);
//char str[65]; printf("len.%d: block version.%d timestamp.%u bits.%x nonce.%u prev.(%s) %llx blockhash.(%s) %llx\n",len,msg->H.version,msg->H.timestamp,msg->H.bits,msg->H.nonce,bits256_str(str,msg->H.prev_block),(long long)msg->H.merkle_root.txid,blockhash,(long long)hash2p->txid);
if ( rwflag != 0 )
x = msg->txn_count;
@ -104,7 +104,7 @@ int32_t iguana_rwblock(int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct
return(len);
}
int32_t iguana_serialize_block(bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block)
int32_t iguana_serialize_block(struct iguana_chain *chain,bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block)
{
struct iguana_msgblock msg;
memset(&msg,0,sizeof(msg));
@ -115,7 +115,7 @@ int32_t iguana_serialize_block(bits256 *hash2p,uint8_t serialized[sizeof(struct
msg.H.bits = block->RO.bits;
msg.H.nonce = block->RO.nonce;
msg.txn_count = block->RO.txn_count;
return(iguana_rwblock(1,hash2p,serialized,&msg));
return(iguana_rwblock(chain->hashalgo,1,hash2p,serialized,&msg));
}
int32_t iguana_rwblockhash(int32_t rwflag,uint8_t *serialized,uint32_t *nVersionp,uint32_t *varintp,bits256 *hashes,bits256 *stophash)
@ -423,7 +423,7 @@ char *iguana_txscan(struct iguana_info *coin,cJSON *json,uint8_t *data,int32_t r
int32_t i,n,len; char *txbytes,vpnstr[64];
memset(&msg,0,sizeof(msg));
vpnstr[0] = 0;
len = iguana_rwblock(0,&hash2,data,&msg);
len = iguana_rwblock(coin->chain->hashalgo,0,&hash2,data,&msg);
iguana_blockconv(&block,&msg,hash2,-1);
for (i=0; i<msg.txn_count; i++)
{
@ -448,7 +448,7 @@ int32_t iguana_gentxarray(struct iguana_info *coin,struct OS_memspace *mem,struc
{
struct iguana_msgtx *tx; bits256 hash2; struct iguana_msgblock msg; int32_t i,n,len,numvouts,numvins;
memset(&msg,0,sizeof(msg));
len = iguana_rwblock(0,&hash2,data,&msg);
len = iguana_rwblock(coin->chain->hashalgo,0,&hash2,data,&msg);
iguana_blockconv(&txdata->block,&msg,hash2,-1);
tx = iguana_memalloc(mem,msg.txn_count*sizeof(*tx),1);
for (i=numvins=numvouts=0; i<msg.txn_count; i++)
@ -676,7 +676,7 @@ int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struc
blocks = mycalloc('i',1,sizeof(*blocks) * n);
for (i=0; i<n; i++)
{
len += iguana_rwblock(0,&hash2,&data[len],&msg);
len += iguana_rwblock(coin->chain->hashalgo,0,&hash2,&data[len],&msg);
iguana_blockconv(&blocks[i],&msg,hash2,-1);
}
iguana_gotheadersM(coin,addr,blocks,n);

9
iguana/iguana_payments.c

@ -274,13 +274,14 @@ cJSON *iguana_inputsjson(struct supernet_info *myinfo,struct iguana_info *coin,i
return(vins);
}
char *iguana_signrawtx(struct supernet_info *myinfo,struct iguana_info *coin,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtx)
char *iguana_signrawtx(struct supernet_info *myinfo,struct iguana_info *coin,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtx,cJSON *privkeys)
{
cJSON *privkeys; struct vin_info *V; char *signedtx = 0; struct iguana_msgtx msgtx; int32_t numinputs;
struct vin_info *V; char *signedtx = 0; struct iguana_msgtx msgtx; int32_t numinputs;
*completedp = 0;
if ( (numinputs= cJSON_GetArraySize(vins)) > 0 && (privkeys= iguana_privkeysjson(myinfo,coin,vins)) != 0 )
if ( (numinputs= cJSON_GetArraySize(vins)) > 0 && (privkeys != 0 || (privkeys= iguana_privkeysjson(myinfo,coin,vins)) != 0) )
{
memset(&msgtx,0,sizeof(msgtx));
//printf("SIGN.(%s) priv.(%s)\n",jprint(vins,0),jprint(privkeys,0));
if ( (V= calloc(numinputs,sizeof(*V))) != 0 )
{
if ( iguana_signrawtransaction(myinfo,coin,&msgtx,&signedtx,signedtxidp,V,numinputs,rawtx,vins,privkeys) > 0 )
@ -516,7 +517,7 @@ char *sendtoaddress(struct supernet_info *myinfo,struct iguana_info *coin,char *
init_hexbytes_noT(spendscriptstr,spendscript,spendlen);
if ( (rawtx= iguana_rawtxissue(myinfo,rand(),coin->symbol,&vins,locktime,satoshis,coin->changeaddr,txfee,addresses,minconf,spendscriptstr,15000)) != 0 )
{
if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx)) != 0 )
if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx,0)) != 0 )
{
iguana_unspentslock(myinfo,coin,vins);
retjson = cJSON_CreateObject();

18
iguana/iguana_ramchain.c

@ -2298,7 +2298,7 @@ struct iguana_ramchain *iguana_bundleload(struct iguana_info *coin,struct iguana
{
static const bits256 zero;
struct iguana_blockRO *B; struct iguana_txid *T; int32_t i,firsti = 1; char fname[512];
struct iguana_block *block; struct iguana_ramchain *mapchain;
struct iguana_block *block,*prev,*prev2; struct iguana_ramchain *mapchain;
memset(ramchain,0,sizeof(*ramchain));
if ( (mapchain= iguana_ramchain_map(coin,fname,bp,bp->n,ramchain,0,0,bp->hashes[0],zero,0,0,extraflag,1)) != 0 )
{
@ -2307,12 +2307,16 @@ struct iguana_ramchain *iguana_bundleload(struct iguana_info *coin,struct iguana
//ramcoder_test(mapchain->H.data,mapchain->H.data->allocsize);
B = RAMCHAIN_PTR(ramchain->H.data,Boffset);
T = RAMCHAIN_PTR(ramchain->H.data,Toffset);
//B = (void *)(long)((long)mapchain->H.data + mapchain->H.data->Boffset);
//T = (void *)(long)((long)mapchain->H.data + mapchain->H.data->Toffset);
prev = prev2 = 0;
for (i=0; i<bp->n; i++)
{
if ( (block= bp->blocks[i]) != 0 || (block= iguana_blockhashset("bundleload",coin,bp->bundleheight+i,bp->hashes[i],1)) != 0 )
{
if ( bits256_to_compact(bits256_from_compact(block->RO.bits)) != block->RO.bits )
{
char str[65];
printf("nbits calc error %x -> %s -> %x\n",block->RO.bits,bits256_str(str,bits256_from_compact(block->RO.bits)),bits256_to_compact(bits256_from_compact(block->RO.bits)));
}
block->queued = 1;
block->txvalid = 1;
block->height = bp->bundleheight + i;
@ -2322,13 +2326,21 @@ struct iguana_ramchain *iguana_bundleload(struct iguana_info *coin,struct iguana
block->RO = B[i];
//printf("%x ",(int32_t)B[i].hash2.ulongs[3]);
iguana_hash2set(coin,"bundleload",bp,i,block->RO.hash2);
bp->blocks[i] = block;
//if ( bits256_nonz(bp->hashes[i]) == 0 )
// bp->hashes[i] = B[i].hash2;
if ( (prev= block->hh.prev) != 0 )
prev2 = prev->hh.prev;
if ( strcmp(coin->symbol,"BTCD") == 0 && bp->bundleheight > 20000 && prev != 0 && iguana_targetbits(coin,block,prev,prev2,1) != block->RO.bits )
{
printf("nbits target error %x != %x ht.%d\n",iguana_targetbits(coin,block,prev,prev2,1),block->RO.bits,block->height);
} //else printf(">>>>>>>>>> matched nbits %x ht.%d\n",block->RO.bits,block->height);
if ( bp->bundleheight+i == coin->blocks.hwmchain.height+1 )
{
//printf("try extend.%d\n",bp->bundleheight+i);
//_iguana_chainlink(coin,block); wrong context
}
prev2 = prev, prev = block;
}
}
//printf("mapped bundle.%d\n",bp->bundleheight);

9
iguana/iguana_sign.c

@ -743,7 +743,7 @@ void iguana_ensure_privkey(struct supernet_info *myinfo,struct iguana_info *coin
uint8_t pubkey33[33]; struct iguana_waccount *wacct; struct iguana_waddress *waddr,addr; char coinaddr[128];
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey);
bitcoin_address(coinaddr,coin->chain->pubtype,pubkey33,33);
//printf("privkey for (%s)\n",coinaddr);
printf("privkey for (%s)\n",coinaddr);
if ( myinfo->expiration != 0 && ((waddr= iguana_waddresssearch(myinfo,coin,&wacct,coinaddr)) == 0 || bits256_nonz(waddr->privkey) == 0) )
{
if ( waddr == 0 )
@ -758,10 +758,10 @@ void iguana_ensure_privkey(struct supernet_info *myinfo,struct iguana_info *coin
waddr->privkey = privkey;
if ( bitcoin_priv2wif(waddr->wifstr,waddr->privkey,coin->chain->wiftype) > 0 )
{
if ( waddr->wiftype != coin->chain->wiftype )
if ( 0 && waddr->wiftype != coin->chain->wiftype )
printf("ensurepriv warning: mismatched wiftype %02x != %02x\n",waddr->wiftype,coin->chain->wiftype);
if ( waddr->addrtype != coin->chain->pubtype )
printf("ensurepriv warning: mismatched wiftype %02x != %02x\n",waddr->addrtype,coin->chain->pubtype);
if ( 0 && waddr->addrtype != coin->chain->pubtype )
printf("ensurepriv warning: mismatched addrtype %02x != %02x\n",waddr->addrtype,coin->chain->pubtype);
}
}
}
@ -1066,6 +1066,7 @@ int32_t iguana_signrawtransaction(struct supernet_info *myinfo,struct iguana_inf
item = jitem(privkeys,i);
privkeystr = jstr(item,0);
privkey = iguana_str2priv(myinfo,coin,privkeystr);
V->signers[i].privkey = privkey;
if ( bits256_nonz(privkey) != 0 )
iguana_ensure_privkey(myinfo,coin,privkey);
}

348
iguana/iguana_stake.c

@ -1,25 +1,101 @@
#ifdef reference
// modify time.1462237906 modifier.baed58b98a00e41d
#include "iguana777.h"
#define CENT (SATOSHIDEN / 100)
#define COIN_YEAR_REWARD ((int64_t)5 * CENT) // 5% per year
#define NCOINBASEMATURITY 100
#define STAKE_TIMESTAMP_MASK 15
#define NTARGETSPACING 60 // BitcoinDark - 1 minute
#define NTARGETTIMESPAN (60 * NTARGETSPACING) // BitcoinDark - every 1 hour
#define NINTERVAL_MSPACING (((NTARGETTIMESPAN / NTARGETSPACING) - 1) * NTARGETSPACING)
#define NINTERVAL_PSPACING (((NTARGETTIMESPAN / NTARGETSPACING) + 1) * NTARGETSPACING)
static const int64_t COIN_YEAR_REWARD = 5 * CENT; // 5% per year
int nCoinbaseMaturity = 100;
static const int STAKE_TIMESTAMP_MASK = 15;
#define NSTAKESPLITAGE (1 * 24 * NTARGETTIMESPAN)
#define NSTAKE_MINAGE (8 * NTARGETTIMESPAN) // BitcoinDark - 8 hours
#define NSTAKEMAXAGE ((int64_t)-1)
#define NMAXSTAKESEARCHINTERVAL 60
#define NSTAKECOMBINETHRESHOLD (1000 * COIN)
// MODIFIER_INTERVAL_RATIO:
// ratio of group interval length between the last group and the first group
static const int MODIFIER_INTERVAL_RATIO = 3;
#define MODIFIER_INTERVAL_RATIO 3
#define NMODIFIERINTERVAL (10 * NTARGETSPACING) // BitcoinDark - time to elapse before new modifier is
CBigNum bnProofOfStakeLimit(~uint256(0) >> 20);
CBigNum bnProofOfStakeLimitV2(~uint256(0) >> 48);
// miner's coin stake reward based on coin age spent (coin-days)
int64_t iguana_POSreward(int64_t nCoinAge, int64_t nFees)
{
int64_t nSubsidy = (nCoinAge * COIN_YEAR_REWARD * 33) / (365 * 33 + 8);
return(nSubsidy + nFees);
}
unsigned int nTargetSpacing = 1 * 60; // BitcoinDark - 1 minute
static const int64_t nTargetTimespan = 60 * 60; // BitcoinDark - every 1 hour
unsigned int nStakeMinAge = 8 * 60 * 60; // BitcoinDark - 8 hours
unsigned int nStakeMaxAge = -1;
unsigned int nModifierInterval = 10 * 60; // BitcoinDark - time to elapse before new modifier is
// maximum nBits value could possible be required nTime after
uint32_t iguana_maxbits(bits256 targetval,uint32_t nBits,int64_t nTime)
{
bits256 bitsval;
bitsval = bits256_from_compact(nBits);
bitsval = bits256_lshift(bitsval);
while ( nTime > 0 && bits256_cmp(bitsval,targetval) < 0 )
{
bitsval = bits256_rshift(bitsval); // Maximum 200% adjustment per day...
nTime -= 24 * 60 * 60;
}
if ( bits256_cmp(bitsval,targetval) > 0 )
bitsval = targetval;
return(bits256_to_compact(bitsval));
}
unsigned int nStakeSplitAge = 1 * 24 * 60 * 60;
int64_t nStakeCombineThreshold = 1000 * COIN;
bits256 iguana_targetval(struct iguana_info *coin,int32_t height,int32_t PoSflag)
{
int32_t i; bits256 targetval;
if ( PoSflag == 0 )
return(coin->chain->PoWtarget);
else
{
targetval = coin->chain->PoStargets[0];
for (i=0; i<coin->chain->numPoStargets; i++)
{
if ( height < coin->chain->PoSheights[i] )
break;
targetval = coin->chain->PoStargets[i];
}
}
return(targetval);
}
// minimum amount of stake that could possibly be required nTime after
// minimum proof-of-stake required was nBase
uint32_t iguana_minstake(struct iguana_info *coin,int32_t height,uint32_t nBits,int64_t nTime,uint32_t nBlockTime)
{
return(iguana_maxbits(iguana_targetval(coin,height,1),nBits,nTime));
}
uint32_t iguana_targetbits(struct iguana_info *coin,struct iguana_block *hwmchain,struct iguana_block *prev,struct iguana_block *prev2,int32_t PoSflag)
{
bits256 mpz_muldivcmp(bits256 oldval,int32_t mulval,int32_t divval,bits256 cmpval);
bits256 targetval; int32_t gap;
if ( hwmchain->height <= 2 )
return(hwmchain->RO.bits);
targetval = iguana_targetval(coin,hwmchain->height,PoSflag);
if ( prev != 0 )
{
if ( prev2 != 0 )
{
//if ( prev->RO.timestamp != 0 && prev2->RO.timestamp != 0 ) skip check for compatiblity
{
if ( (gap= prev->RO.timestamp - prev2->RO.timestamp) < 0 )
gap = NTARGETSPACING;
// ppcoin: target change every block, retarget with exponential moving toward target spacing
targetval = mpz_muldivcmp(bits256_from_compact(prev->RO.bits),NINTERVAL_MSPACING + (gap << 1),NINTERVAL_PSPACING,targetval);
}
}
}
return(bits256_to_compact(targetval));
}
#ifdef reference
CBigNum bnProofOfStakeLimit(~uint256(0) >> 20);
CBigNum bnProofOfStakeLimitV2(~uint256(0) >> 48);
enum
{
@ -29,26 +105,24 @@ enum
};
uint64_t nStakeModifier; // hash modifier for proof-of-stake
unsigned int nStakeModifierChecksum; // checksum of index; in-memeory only
uint32_t nStakeModifierChecksum; // checksum of index; in-memory only
uint256 CBlockIndex::GetBlockTrust() const
{
CBigNum bnTarget;
bnTarget.SetCompact(nBits);
if (bnTarget <= 0)
return 0;
return ((CBigNum(1)<<256) / (bnTarget+1)).getuint256();
}
unsigned int GetStakeEntropyBit() const
uint32_t GetStakeEntropyBit() const
{
return ((nFlags & BLOCK_STAKE_ENTROPY) >> 1);
}
bool SetStakeEntropyBit(unsigned int nEntropyBit)
bool SetStakeEntropyBit(uint32_t nEntropyBit)
{
if (nEntropyBit > 1)
return false;
@ -68,80 +142,6 @@ void SetStakeModifier(uint64_t nModifier, bool fGeneratedStakeModifier)
nFlags |= BLOCK_STAKE_MODIFIER;
}
// miner's coin stake reward based on coin age spent (coin-days)
int64_t GetProofOfStakeReward(int64_t nCoinAge, int64_t nFees)
{
int64_t nSubsidy = nCoinAge * COIN_YEAR_REWARD * 33 / (365 * 33 + 8);
return(nSubsidy + nFees);
}
//
// maximum nBits value could possible be required nTime after
//
unsigned int ComputeMaxBits(CBigNum bnTargetLimit, unsigned int nBase, int64_t nTime)
{
CBigNum bnResult;
bnResult.SetCompact(nBase);
bnResult *= 2;
while (nTime > 0 && bnResult < bnTargetLimit)
{
// Maximum 200% adjustment per day...
bnResult *= 2;
nTime -= 24 * 60 * 60;
}
if (bnResult > bnTargetLimit)
bnResult = bnTargetLimit;
return bnResult.GetCompact();
}
//
// minimum amount of stake that could possibly be required nTime after
// minimum proof-of-stake required was nBase
//
unsigned int ComputeMinStake(unsigned int nBase, int64_t nTime, unsigned int nBlockTime)
{
return ComputeMaxBits(bnProofOfStakeLimit, nBase, nTime);
}
static CBigNum GetProofOfStakeLimit(int nHeight)
{
if(IsPoSV2(nHeight))
return bnProofOfStakeLimitV2;
else
return bnProofOfStakeLimit;
}
unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfStake)
{
CBigNum bnTargetLimit = fProofOfStake ? GetProofOfStakeLimit(pindexLast->nHeight) : bnProofOfWorkLimit;
if (pindexLast == NULL)
return bnTargetLimit.GetCompact(); // genesis block
const CBlockIndex* pindexPrev = GetLastBlockIndex(pindexLast, fProofOfStake);
if (pindexPrev->pprev == NULL)
return bnTargetLimit.GetCompact(); // first block
const CBlockIndex* pindexPrevPrev = GetLastBlockIndex(pindexPrev->pprev, fProofOfStake);
if (pindexPrevPrev->pprev == NULL)
return bnTargetLimit.GetCompact(); // second block
int64_t nActualSpacing = pindexPrev->GetBlockTime() - pindexPrevPrev->GetBlockTime();
if (nActualSpacing < 0)
nActualSpacing = nTargetSpacing;
// ppcoin: target change every block
// ppcoin: retarget with exponential moving toward target spacing
CBigNum bnNew;
bnNew.SetCompact(pindexPrev->nBits);
int64_t nInterval = nTargetTimespan / nTargetSpacing;
bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
bnNew /= ((nInterval + 1) * nTargetSpacing);
if (bnNew <= 0 || bnNew > bnTargetLimit)
bnNew = bnTargetLimit;
return bnNew.GetCompact();
}
// ppcoin: total coin age spent in transaction, in the unit of coin-days.
// Only those coins meeting minimum age requirement counts. As those
// transactions not in main chain are not currently indexed so we
@ -168,7 +168,7 @@ bool CTransaction::GetCoinAge(CTxDB& txdb, uint64_t& nCoinAge) const
CBlock block;
if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
return false; // unable to read block of previous transaction
if (block.GetBlockTime() + nStakeMinAge > nTime)
if (block.GetBlockTime() + NSTAKE_MINAGE > nTime)
continue; // only count coins meeting min age requirement
int64_t nValueIn = txPrev.vout[txin.prevout.n].nValue;
@ -206,13 +206,12 @@ bool CBlock::GetCoinAge(uint64_t& nCoinAge) const
}
// Get time weight
int64_t GetWeight(int64_t nIntervalBeginning, int64_t nIntervalEnd)
int64_t GetWeight(int64_t nIntervalBeginning,int64_t nIntervalEnd)
{
// Kernel hash weight starts from 0 at the min age
// this change increases active coins participating the hash and helps
// to secure the network when proof-of-stake difficulty is low
return min(nIntervalEnd - nIntervalBeginning - nStakeMinAge, (int64_t)nStakeMaxAge);
return min(nIntervalEnd - nIntervalBeginning - NSTAKE_MINAGE,NSTAKEMAXAGE);
}
// Get the last stake modifier and its generation time from a given block
@ -230,50 +229,44 @@ static bool GetLastStakeModifier(const CBlockIndex* pindex, uint64_t& nStakeModi
}
// Get selection interval section (in seconds)
static int64_t GetStakeModifierSelectionIntervalSection(int nSection)
static int64_t GetStakeModifierSelectionIntervalSection(int32_t nSection)
{
assert (nSection >= 0 && nSection < 64);
return (nModifierInterval * 63 / (63 + ((63 - nSection) * (MODIFIER_INTERVAL_RATIO - 1))));
return (NMODIFIERINTERVAL * 63 / (63 + ((63 - nSection) * (MODIFIER_INTERVAL_RATIO - 1))));
}
// Get stake modifier selection interval (in seconds)
static int64_t GetStakeModifierSelectionInterval()
{
int64_t nSelectionInterval = 0;
for (int nSection=0; nSection<64; nSection++)
for (int32_t nSection=0; nSection<64; nSection++)
nSelectionInterval += GetStakeModifierSelectionIntervalSection(nSection);
return nSelectionInterval;
}
// select a block from the candidate blocks in vSortedByTimestamp, excluding
// already selected blocks in vSelectedBlocks, and with timestamp up to
// nSelectionIntervalStop.
static bool SelectBlockFromCandidates(vector<pair<int64_t, uint256> >& vSortedByTimestamp, map<uint256, const CBlockIndex*>& mapSelectedBlocks,
int64_t nSelectionIntervalStop, uint64_t nStakeModifierPrev, const CBlockIndex** pindexSelected)
// already selected blocks in vSelectedBlocks, and with timestamp up to nSelectionIntervalStop.
static bool SelectBlockFromCandidates(vector<pair<int64_t, uint256> >& vSortedByTimestamp, map<uint256, const CBlockIndex*>& mapSelectedBlocks,int64_t nSelectionIntervalStop, uint64_t nStakeModifierPrev, const CBlockIndex **pindexSelected)
{
bool fSelected = false;
uint256 hashBest = 0;
bool fSelected = false; uint256 hashBest = 0;
*pindexSelected = (const CBlockIndex*) 0;
BOOST_FOREACH(const PAIRTYPE(int64_t, uint256)& item, vSortedByTimestamp)
BOOST_FOREACH(const PAIRTYPE(int64_t, uint256)&item, vSortedByTimestamp)
{
if (!mapBlockIndex.count(item.second))
return error("SelectBlockFromCandidates: failed to find block index for candidate block %s", item.second.ToString().c_str());
const CBlockIndex* pindex = mapBlockIndex[item.second];
if (fSelected && pindex->GetBlockTime() > nSelectionIntervalStop)
const CBlockIndex *pindex = mapBlockIndex[item.second];
if ( fSelected && pindex->GetBlockTime() > nSelectionIntervalStop )
break;
if (mapSelectedBlocks.count(pindex->GetBlockHash()) > 0)
continue;
// compute the selection hash by hashing its proof-hash and the
// previous proof-of-stake modifier
// compute the selection hash by hashing its proof-hash and the previous proof-of-stake modifier
CDataStream ss(SER_GETHASH, 0);
ss << pindex->hashProof << nStakeModifierPrev;
uint256 hashSelection = Hash(ss.begin(), ss.end());
// the selection hash is divided by 2**32 so that proof-of-stake block
// is always favored over proof-of-work block. this is to preserve
// the energy efficiency property
if (pindex->IsProofOfStake())
// the selection hash is divided by 2**32 so that proof-of-stake block is always favored over proof-of-work block. this is to preserve the energy efficiency property
if ( pindex->IsProofOfStake() )
hashSelection >>= 32;
if (fSelected && hashSelection < hashBest)
if ( fSelected && hashSelection < hashBest )
{
hashBest = hashSelection;
*pindexSelected = (const CBlockIndex*) pindex;
@ -321,21 +314,21 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeMod
{
printf("ComputeNextStakeModifier: prev modifier=0x%016"PRIx64" time=%s\n", nStakeModifier, DateTimeStrFormat(nModifierTime).c_str());
}
if (nModifierTime / nModifierInterval >= pindexPrev->GetBlockTime() / nModifierInterval)
if (nModifierTime / NMODIFIERINTERVAL >= pindexPrev->GetBlockTime() / NMODIFIERINTERVAL)
return true;
// Sort candidate blocks by timestamp
vector<pair<int64_t, uint256> > vSortedByTimestamp;
vSortedByTimestamp.reserve(64 * nModifierInterval / nTargetSpacing);
vSortedByTimestamp.reserve(64 * (NMODIFIERINTERVAL / NTARGETSPACING));
int64_t nSelectionInterval = GetStakeModifierSelectionInterval();
int64_t nSelectionIntervalStart = (pindexPrev->GetBlockTime() / nModifierInterval) * nModifierInterval - nSelectionInterval;
int64_t nSelectionIntervalStart = (pindexPrev->GetBlockTime() / NMODIFIERINTERVAL) * NMODIFIERINTERVAL - nSelectionInterval;
const CBlockIndex* pindex = pindexPrev;
while (pindex && pindex->GetBlockTime() >= nSelectionIntervalStart)
{
vSortedByTimestamp.push_back(make_pair(pindex->GetBlockTime(), pindex->GetBlockHash()));
pindex = pindex->pprev;
}
int nHeightFirstCandidate = pindex ? (pindex->nHeight + 1) : 0;
int32_t nHeightFirstCandidate = pindex ? (pindex->nHeight + 1) : 0;
reverse(vSortedByTimestamp.begin(), vSortedByTimestamp.end());
sort(vSortedByTimestamp.begin(), vSortedByTimestamp.end());
@ -343,7 +336,7 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeMod
uint64_t nStakeModifierNew = 0;
int64_t nSelectionIntervalStop = nSelectionIntervalStart;
map<uint256, const CBlockIndex*> mapSelectedBlocks;
for (int nRound=0; nRound<min(64, (int)vSortedByTimestamp.size()); nRound++)
for (int32_t nRound=0; nRound<min(64, (int)vSortedByTimestamp.size()); nRound++)
{
// add an interval section to the current selection round
nSelectionIntervalStop += GetStakeModifierSelectionIntervalSection(nRound);
@ -407,11 +400,9 @@ static bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifi
{
if (!pindex->pnext)
{ // reached best block; may happen if node is behind on block chain
if (fPrintProofOfStake || (pindex->GetBlockTime() + nStakeMinAge - nStakeModifierSelectionInterval > GetAdjustedTime()))
return error("GetKernelStakeModifier() : reached best block %s at height %d from block %s",
pindex->GetBlockHash().ToString().c_str(), pindex->nHeight, hashBlockFrom.ToString().c_str());
else
return false;
if (fPrintProofOfStake || (pindex->GetBlockTime() + NSTAKE_MINAGE - nStakeModifierSelectionInterval > GetAdjustedTime()))
return error("GetKernelStakeModifier() : reached best block %s at height %d from block %s",pindex->GetBlockHash().ToString().c_str(), pindex->nHeight, hashBlockFrom.ToString().c_str());
else return false;
}
pindex = pindex->pnext;
if (pindex->GeneratedStakeModifier())
@ -445,13 +436,13 @@ static bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifi
// quantities so as to generate blocks faster, degrading the system back into
// a proof-of-work situation.
//
bool CheckStakeKernelHashV1(unsigned int nBits, const CBlock& blockFrom, unsigned int nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout, unsigned int nTimeTx, uint256& hashProofOfStake, uint256& targetProofOfStake, bool fPrintProofOfStake)
bool CheckStakeKernelHashV1(uint32_t nBits, const CBlock& blockFrom,uint32_t nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout,uint32_t nTimeTx, uint256& hashProofOfStake, uint256& targetProofOfStake, bool fPrintProofOfStake)
{
if (nTimeTx < txPrev.nTime) // Transaction timestamp violation
return error("CheckStakeKernelHash() : nTime violation");
unsigned int nTimeBlockFrom = blockFrom.GetBlockTime();
if (nTimeBlockFrom + nStakeMinAge > nTimeTx) // Min age requirement
uint32_t nTimeBlockFrom = blockFrom.GetBlockTime();
if (nTimeBlockFrom + NSTAKE_MINAGE > nTimeTx) // Min age requirement
return error("CheckStakeKernelHash() : min age violation");
CBigNum bnTargetPerCoinDay;
@ -466,7 +457,7 @@ bool CheckStakeKernelHashV1(unsigned int nBits, const CBlock& blockFrom, unsigne
// Calculate hash
CDataStream ss(SER_GETHASH, 0);
uint64_t nStakeModifier = 0;
int nStakeModifierHeight = 0;
int32_t nStakeModifierHeight = 0;
int64_t nStakeModifierTime = 0;
if ( !GetKernelStakeModifier(hashBlockFrom, nStakeModifier, nStakeModifierHeight, nStakeModifierTime, fPrintProofOfStake) )
@ -504,7 +495,7 @@ bool CheckStakeKernelHashV1(unsigned int nBits, const CBlock& blockFrom, unsigne
return true;
}
bool CheckStakeKernelHash(CBlockIndex* pindexPrev, unsigned int nBits, const CBlock& blockFrom, unsigned int nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout, unsigned int nTimeTx, uint256& hashProofOfStake, uint256& targetProofOfStake, bool fPrintProofOfStake)
bool CheckStakeKernelHash(CBlockIndex* pindexPrev, uint32_t nBits, const CBlock& blockFrom, uint32_t nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout, uint32_t nTimeTx, uint256& hashProofOfStake, uint256& targetProofOfStake, bool fPrintProofOfStake)
{
if (IsPoSV2(pindexPrev->nHeight+1))
return CheckStakeKernelHashV2(pindexPrev, nBits, blockFrom.GetBlockTime(), txPrev, prevout, nTimeTx, hashProofOfStake, targetProofOfStake, fPrintProofOfStake);
@ -512,10 +503,9 @@ bool CheckStakeKernelHash(CBlockIndex* pindexPrev, unsigned int nBits, const CBl
return CheckStakeKernelHashV1(nBits, blockFrom, nTxPrevOffset, txPrev, prevout, nTimeTx, hashProofOfStake, targetProofOfStake, fPrintProofOfStake);
}
bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64_t nSearchInterval, int64_t nFees, CTransaction& txNew, CKey& key)
bool CWallet::CreateCoinStake(const CKeyStore& keystore, uint32_t nBits, int64_t nSearchInterval, int64_t nFees, CTransaction& txNew, CKey& key)
{
CBlockIndex* pindexPrev = pindexBest;
CBigNum bnTargetPerCoinDay;
CBigNum bnTargetPerCoinDay; CBlockIndex *pindexPrev = pindexBest;
bnTargetPerCoinDay.SetCompact(nBits);
txNew.vin.clear();
@ -528,12 +518,10 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
// Choose coins to use
int64_t nBalance = GetBalance();
if (nBalance <= nReserveBalance)
return false;
vector<const CWalletTx*> vwtxPrev;
set<pair<const CWalletTx*,unsigned int> > setCoins;
int64_t nValueIn = 0;
@ -564,15 +552,14 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
continue;
}
static int nMaxStakeSearchInterval = 60;
if (block.GetBlockTime() + nStakeMinAge > txNew.nTime - nMaxStakeSearchInterval)
if (block.GetBlockTime() + NSTAKE_MINAGE > txNew.nTime - NMAXSTAKESEARCHINTERVAL)
continue; // only count coins meeting min age requirement
bool fKernelFound = false;
for (unsigned int n=0; n<min(nSearchInterval,(int64_t)nMaxStakeSearchInterval) && !fKernelFound && !fShutdown && pindexPrev == pindexBest; n++)
for (uint32_t n=0; n<min(nSearchInterval,(int64_t)NMAXSTAKESEARCHINTERVAL) && !fKernelFound && !fShutdown && pindexPrev == pindexBest; n++)
{
// Search backward in time from the given txNew timestamp
// Search nSearchInterval seconds back up to nMaxStakeSearchInterval
// Search nSearchInterval seconds back up to NMAXSTAKESEARCHINTERVAL
uint256 hashProofOfStake = 0, targetProofOfStake = 0;
COutPoint prevoutStake = COutPoint(pcoin.first->GetHash(), pcoin.second);
if (CheckStakeKernelHash(pindexPrev, nBits, block, txindex.pos.nTxPos - txindex.pos.nBlockPos, *pcoin.first, prevoutStake, txNew.nTime - n, hashProofOfStake, targetProofOfStake))
@ -653,29 +640,26 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
{
// Attempt to add more inputs
// Only add coins of the same key/address as kernel
// Attempt to add more inputs: Only add coins of the same key/address as kernel
if (txNew.vout.size() == 2 && ((pcoin.first->vout[pcoin.second].scriptPubKey == scriptPubKeyKernel || pcoin.first->vout[pcoin.second].scriptPubKey == txNew.vout[1].scriptPubKey))
&& pcoin.first->GetHash() != txNew.vin[0].prevout.hash)
{
int64_t nTimeWeight = GetWeight((int64_t)pcoin.first->nTime, (int64_t)txNew.nTime);
// Stop adding more inputs if already too many inputs
if (txNew.vin.size() >= 100)
break;
// Stop adding more inputs if value is already pretty significant
if (nCredit >= nStakeCombineThreshold)
if (nCredit >= NSTAKECOMBINETHRESHOLD)
break;
// Stop adding inputs if reached reserve limit
if (nCredit + pcoin.first->vout[pcoin.second].nValue > nBalance - nReserveBalance)
break;
// Do not add additional significant input
if (pcoin.first->vout[pcoin.second].nValue >= nStakeCombineThreshold)
if (pcoin.first->vout[pcoin.second].nValue >= NSTAKECOMBINETHRESHOLD)
continue;
// Do not add input that is still too young
if (nTimeWeight < nStakeMinAge)
if (nTimeWeight < NSTAKE_MINAGE)
continue;
txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
nCredit += pcoin.first->vout[pcoin.second].nValue;
vwtxPrev.push_back(pcoin.first);
@ -689,7 +673,7 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
if (!txNew.GetCoinAge(txdb, nCoinAge))
return error("CreateCoinStake : failed to calculate coin age");
int64_t nReward = GetProofOfStakeReward(nCoinAge, nFees);
int64_t nReward = iguana_POSreward(nCoinAge, nFees);
if (nReward <= 0)
return false;
@ -701,12 +685,10 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
{
txNew.vout[1].nValue = (nCredit / 2 / CENT) * CENT;
txNew.vout[2].nValue = nCredit - txNew.vout[1].nValue;
}
else
txNew.vout[1].nValue = nCredit;
} else txNew.vout[1].nValue = nCredit;
// Sign
int nIn = 0;
int32_t nIn = 0;
BOOST_FOREACH(const CWalletTx* pcoin, vwtxPrev)
{
if (!SignSignature(*this, *pcoin, txNew, nIn++))
@ -714,7 +696,7 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
}
// Limit size
unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
uint32_t nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
return error("CreateCoinStake : exceeded coinstake size limit");
@ -723,7 +705,7 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
}
// Check kernel hash target and coinstake signature
bool CheckProofOfStake(CBlockIndex* pindexPrev, const CTransaction& tx, unsigned int nBits, uint256& hashProofOfStake, uint256& targetProofOfStake)
bool CheckProofOfStake(CBlockIndex* pindexPrev, const CTransaction& tx, uint32_t nBits, uint256& hashProofOfStake, uint256& targetProofOfStake)
{
if (!tx.IsCoinStake())
return error("CheckProofOfStake() : called on non-coinstake %s", tx.GetHash().ToString().c_str());
@ -761,7 +743,7 @@ bool CheckCoinStakeTimestamp(int64_t nTimeBlock, int64_t nTimeTx)
}
// Get stake modifier checksum
unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex)
uint32_t GetStakeModifierChecksum(const CBlockIndex* pindex)
{
//assert (pindex->pprev || pindex->GetBlockHash() == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet));
// Hash previous checksum with flags, hashProofOfStake and nStakeModifier
@ -775,7 +757,7 @@ unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex)
}
// Check stake modifier hard checkpoints
bool CheckStakeModifierCheckpoints(int nHeight, unsigned int nStakeModifierChecksum)
bool CheckStakeModifierCheckpoints(int32_t nHeight, uint32_t nStakeModifierChecksum)
{
MapModifierCheckpoints& checkpoints = (fTestNet ? mapStakeModifierCheckpointsTestNet : mapStakeModifierCheckpoints);
@ -783,47 +765,36 @@ bool CheckStakeModifierCheckpoints(int nHeight, unsigned int nStakeModifierCheck
return nStakeModifierChecksum == checkpoints[nHeight];
return true;
}
// novacoin: attempt to generate suitable proof-of-stake
bool CBlock::SignBlock(CWallet& wallet, int64_t nFees)
{
// if we are trying to sign
// something except proof-of-stake block template
// if we are trying to sign something except proof-of-stake block template
if (!vtx[0].vout[0].IsEmpty())
return false;
// if we are trying to sign
// a complete proof-of-stake block
// if we are trying to sign a complete proof-of-stake block
if (IsProofOfStake())
return true;
static int64_t nLastCoinStakeSearchTime = GetAdjustedTime(); // startup timestamp
CKey key;
CTransaction txCoinStake;
int64_t nSearchTime = txCoinStake.nTime; // search to current time
CKey key; CTransaction txCoinStake; int64_t nSearchTime = txCoinStake.nTime; // search to current time
if (nSearchTime > nLastCoinStakeSearchTime)
{
if (wallet.CreateCoinStake(wallet, nBits, nSearchTime-nLastCoinStakeSearchTime, nFees, txCoinStake, key))
{
if (txCoinStake.nTime >= max(pindexBest->GetPastTimeLimit()+1, PastDrift(pindexBest->GetBlockTime())))
{
// make sure coinstake would meet timestamp protocol
// as it would be the same as the block timestamp
vtx[0].nTime = nTime = txCoinStake.nTime;
nTime = max(pindexBest->GetPastTimeLimit()+1, GetMaxTransactionTime());
nTime = max(GetBlockTime(), PastDrift(pindexBest->GetBlockTime()));
// we have to make sure that we have no future timestamps in
// our transactions set
for (vector<CTransaction>::iterator it = vtx.begin(); it != vtx.end();)
if (it->nTime > nTime) { it = vtx.erase(it); } else { ++it; }
vtx.insert(vtx.begin() + 1, txCoinStake);
hashMerkleRoot = BuildMerkleTree();
// append a signature to our block
return key.Sign(GetHash(), vchBlockSig);
}
if (txCoinStake.nTime >= max(pindexBest->GetPastTimeLimit()+1, PastDrift(pindexBest->GetBlockTime())))
{
// make sure coinstake would meet timestamp protocol as it would be the same as the block timestamp
vtx[0].nTime = nTime = txCoinStake.nTime;
nTime = max(pindexBest->GetPastTimeLimit()+1, GetMaxTransactionTime());
nTime = max(GetBlockTime(), PastDrift(pindexBest->GetBlockTime()));
// we have to make sure that we have no future timestamps in our transactions set
for (vector<CTransaction>::iterator it = vtx.begin(); it != vtx.end();)
if (it->nTime > nTime) { it = vtx.erase(it); } else { ++it; }
vtx.insert(vtx.begin() + 1, txCoinStake);
hashMerkleRoot = BuildMerkleTree();
// append a signature to our block
return key.Sign(GetHash(), vchBlockSig);
}
}
nLastCoinStakeSearchInterval = nSearchTime - nLastCoinStakeSearchTime;
nLastCoinStakeSearchTime = nSearchTime;
@ -836,15 +807,11 @@ bool CBlock::CheckBlockSignature() const
{
if (IsProofOfWork())
return vchBlockSig.empty();
vector<valtype> vSolutions;
txnouttype whichType;
const CTxOut& txout = vtx[1].vout[1];
if (!Solver(txout.scriptPubKey, whichType, vSolutions))
return false;
if (whichType == TX_PUBKEY)
{
valtype& vchPubKey = vSolutions[0];
@ -855,7 +822,6 @@ bool CBlock::CheckBlockSignature() const
return false;
return key.Verify(GetHash(), vchBlockSig);
}
return false;
}

4
iguana/iguana_tx.c

@ -230,7 +230,7 @@ int32_t iguana_peerblockrequest(struct iguana_info *coin,uint8_t *blockspace,int
{
iguana_blockunconv(&msgB,block,0);
msgB.txn_count = block->RO.txn_count;
total = iguana_rwblock(1,&checkhash2,&blockspace[sizeof(struct iguana_msghdr) + 0],&msgB);
total = iguana_rwblock(coin->chain->hashalgo,1,&checkhash2,&blockspace[sizeof(struct iguana_msghdr) + 0],&msgB);
if ( bits256_cmp(checkhash2,block->RO.hash2) != 0 )
{
static int counter;
@ -368,7 +368,7 @@ cJSON *iguana_blockjson(struct iguana_info *coin,struct iguana_block *block,int3
msg.H.bits = block->RO.bits;
msg.H.nonce = block->RO.nonce;
msg.txn_count = 0;//block->RO.txn_count;
len = iguana_rwblock(1,&hash2,serialized,&msg);
len = iguana_rwblock(coin->chain->hashalgo,1,&hash2,serialized,&msg);
init_hexbytes_noT(hexstr,serialized,len);
jaddstr(json,"blockheader",hexstr);
if ( txidsflag != 0 )

2
iguana/iguana_volatiles.c

@ -89,6 +89,8 @@ struct iguana_utxo iguana_utxofind(struct iguana_info *coin,int16_t spent_hdrsi,
if ( (bp= coin->bundles[spent_hdrsi]) == 0 )
return(utxo);
ramchain = (bp == coin->current) ? &coin->RTramchain : &bp->ramchain;
if ( ramchain->H.data == 0 )
return(utxo);
val = ((uint64_t)spent_hdrsi << 32) | spent_unspentind;
if ( spent_unspentind > 0 && spent_unspentind < ramchain->H.data->numunspents )
{

6
iguana/iguana_wallet.c

@ -182,10 +182,10 @@ struct iguana_waddress *iguana_waddresssearch(struct supernet_info *myinfo,struc
{
if ( bitcoin_priv2wif(waddr->wifstr,waddr->privkey,coin->chain->wiftype) > 0 )
{
if ( waddr->wiftype != coin->chain->wiftype )
if ( 0 && waddr->wiftype != coin->chain->wiftype )
printf("waddresssearch warning: mismatched wiftype %02x != %02x\n",waddr->wiftype,coin->chain->wiftype);
if ( waddr->addrtype != coin->chain->pubtype )
printf("waddresssearch warning: mismatched wiftype %02x != %02x\n",waddr->addrtype,coin->chain->pubtype);
if ( 0 && waddr->addrtype != coin->chain->pubtype )
printf("waddresssearch warning: mismatched addrtype %02x != %02x\n",waddr->addrtype,coin->chain->pubtype);
}
}
(*wacctp) = wacct;

2
iguana/js/api.js

@ -153,7 +153,7 @@ var SPNAPI = (function(SPNAPI, $, undefined) {
SPNAPI.useGETRequest=function(request){
if(request.method && (request.method==='apikeypair' || request.method==='setuserid' || request.method==='encryptjson' || request.method==='decryptjson')){
return false;
return true;
}else{
return true;
}

59
iguana/js/blockexplorer.js

@ -36,64 +36,7 @@ document.getElementById('BlockExpCoin').innerHTML = html;
* to start RPC for particular coin
*/
var callBlockEXPRPC=function(coin){
var request="{\"agent\":\"SuperNET\",\"method\":\"bitcoinrpc\",\"setcoin\":\""+coin+"\"}";
SPNAPI.makeRequest(request, function(request,response){
response=JSON.parse(response);
if(response.result && response.result==='set bitcoin RPC coin'){
blockExp_input_table();
}
});
};
/*
*
* @param {type} height
* @returns {undefined}
* Function gets the blockhash when called and is stored in global variable
* (initially height is set to zero)
*
*/
var filterInt = function (value) {
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
return Number(value);
return "NaN";
};
var getBlockhash= function(height){
var height=($('#BlockExp_height').val());
/*
if (height === "NaN" || height ==='Infinity') {
height=0;
}*/
var request="{\"agent\":\"ramchain\",\"method\":\"getblockhash\",\"height\":\""+height+"\"}";
SPNAPI.makeRequest(request, function(request,response){
response=JSON.parse(response);
if(response.result){
BlockHash=response.result;
//Blockhashoutput
document.getElementById('block_output_table').innerHTML='<tr><td >'+'Blockhash is:</td><td width="300px"> '+BlockHash+'</td></tr>';
$('#BlockExp_blockhash').val(BlockHash);
}
});
};
/*
*
* @param {type} hash
* @returns {undefined}
* Function gets Block for a paritculat blockhash
* and store inside global varianle
*
*/
var callBlockEXPRds
var getBlock= function(hash){
var inputhash=$('#BlockExp_blockhash').val();
if(inputhash!==hash){

13
iguana/main.c

@ -1148,9 +1148,14 @@ void iguana_appletests(struct supernet_info *myinfo)
{
char *str;
//iguana_chaingenesis(1,1403138561,0x1e0fffff,8359109,bits256_conv("fd1751cc6963d88feca94c0d01da8883852647a37a0a67ce254d62dd8c9d5b2b")); // BTCD
//iguana_chaingenesis(1,1409839200,0x1e0fffff,64881664,bits256_conv("698a93a1cacd495a7a4fb3864ad8d06ed4421dedbc57f9aaad733ea53b1b5828")); // VPN
//char genesisblock[1024];
//iguana_chaingenesis(genesisblock,"sha256",1,1317972665,0x1e0ffff0,2084524493,bits256_conv("97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9")); // LTC
if ( 0 )
{
char genesisblock[1024];
iguana_chaingenesis(0,bits256_conv("00000ac7d764e7119da60d3c832b1d4458da9bc9ef9d5dd0d91a15f690a46d99"),genesisblock,"scrypt",1,1409839200,0x1e0fffff,64881664,bits256_conv("698a93a1cacd495a7a4fb3864ad8d06ed4421dedbc57f9aaad733ea53b1b5828")); // VPN
iguana_chaingenesis(0,bits256_conv("12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2"),genesisblock,"scrypt",1,1317972665,0x1e0ffff0,2084524493,bits256_conv("97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9")); // LTC
getchar();
}
if ( 1 )
{
//void ztest(); ztest();
@ -1159,7 +1164,7 @@ void iguana_appletests(struct supernet_info *myinfo)
//int32_t iguana_schnorr_test(void *ctx);
//iguana_schnorr_test(myinfo->ctx); getchar();
if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"RELAY\":0,\"VALIDATE\":0,\"prefetchlag\":-1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":64,\"newcoin\":\"BTCD\",\"active\":1,\"numhelpers\":4,\"poll\":100}"),0,myinfo->rpcport)) != 0 )
if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"RELAY\":1,\"VALIDATE\":1,\"prefetchlag\":-1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":64,\"newcoin\":\"BTCD\",\"active\":1,\"numhelpers\":4,\"poll\":100}"),0,myinfo->rpcport)) != 0 )
{
free(str);
if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"RELAY\":0,\"VALIDATE\":0,\"prefetchlag\":-1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":64,\"newcoin\":\"BTC\",\"active\":0,\"numhelpers\":4,\"poll\":100}"),0,myinfo->rpcport)) != 0 )

2
iguana/mingw32

@ -1,4 +1,4 @@
include ../mingw.path
LIBS := ../win/libsecp256k1.a ../win/libcrypto.a ../win/libssl.a /usr/share/mingw-w64/lib/libpthread.a ../agents/win32/libcrypto777.a ../win/libcurldll.a /usr/i586-mingw32msvc/lib/libws2_32.a /usr/i586-mingw32msvc/lib/libgdi32.a -I/usr/mingw32/include -I/usr/i386/include -I/usr/i386/include/curl -I/home/user/SuperNET/iguana -I/home/user/SuperNET/includes -I/home/user/SuperNET/crypto777
LIBS := /usr/share/mingw-w64/lib/libpthread.a ../agents/win32/libcrypto777.a /usr/i586-mingw32msvc/lib/libws2_32.a /usr/i586-mingw32msvc/lib/libgdi32.a -I/usr/mingw32/include -I/usr/i386/include -I/home/user/SuperNET/iguana -I/home/user/SuperNET/includes -I/home/user/SuperNET/crypto777
include mingw

577
iguana/mini-gmp.c

File diff suppressed because it is too large

148
iguana/mini-gmp.h

@ -55,18 +55,18 @@ void mp_get_memory_functions (void *(**) (size_t),
void *(**) (void *, size_t, size_t),
void (**) (void *, size_t));
typedef unsigned long mp_limb_t;
typedef long mp_size_t;
typedef unsigned long mp_bitcnt_t;
typedef uint64_t mp_limb_t;
typedef int64_t mp_size_t;
typedef uint64_t mp_bitcnt_t;
typedef mp_limb_t *mp_ptr;
typedef const mp_limb_t *mp_srcptr;
typedef struct
{
int _mp_alloc; /* Number of *limbs* allocated and pointed
int32_t _mp_alloc; /* Number of *limbs* allocated and pointed
to by the _mp_d field. */
int _mp_size; /* abs(_mp_size) is the number of limbs the
int32_t _mp_size; /* abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number. */
mp_limb_t *_mp_d; /* Pointer to the limbs. */
@ -77,14 +77,14 @@ typedef __mpz_struct mpz_t[1];
typedef __mpz_struct *mpz_ptr;
typedef const __mpz_struct *mpz_srcptr;
extern const int mp_bits_per_limb;
extern const int32_t mp_bits_per_limb;
void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
void mpn_zero (mp_ptr, mp_size_t);
int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
int mpn_zero_p (mp_srcptr, mp_size_t);
int32_t mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
int32_t mpn_zero_p (mp_srcptr, mp_size_t);
mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
@ -101,11 +101,11 @@ mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
int mpn_perfect_square_p (mp_srcptr, mp_size_t);
int32_t mpn_perfect_square_p (mp_srcptr, mp_size_t);
mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, uint32_t);
mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, uint32_t);
mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
@ -115,42 +115,42 @@ mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
size_t mpn_get_str (unsigned char *, int, mp_ptr, mp_size_t);
mp_size_t mpn_set_str (mp_ptr, const unsigned char *, size_t, int);
size_t mpn_get_str (uint8_t *, int32_t, mp_ptr, mp_size_t);
mp_size_t mpn_set_str (mp_ptr, const uint8_t *, size_t, int32_t);
void mpz_init (mpz_t);
void mpz_init2 (mpz_t, mp_bitcnt_t);
void mpz_clear (mpz_t);
#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int32_t) (z)->_mp_d[0])
#define mpz_even_p(z) (! mpz_odd_p (z))
int mpz_sgn (const mpz_t);
int mpz_cmp_si (const mpz_t, long);
int mpz_cmp_ui (const mpz_t, unsigned long);
int mpz_cmp (const mpz_t, const mpz_t);
int mpz_cmpabs_ui (const mpz_t, unsigned long);
int mpz_cmpabs (const mpz_t, const mpz_t);
int mpz_cmp_d (const mpz_t, double);
int mpz_cmpabs_d (const mpz_t, double);
int32_t mpz_sgn (const mpz_t);
int32_t mpz_cmp_si (const mpz_t, int64_t);
int32_t mpz_cmp_ui (const mpz_t, uint64_t);
int32_t mpz_cmp (const mpz_t, const mpz_t);
int32_t mpz_cmpabs_ui (const mpz_t, uint64_t);
int32_t mpz_cmpabs (const mpz_t, const mpz_t);
int32_t mpz_cmp_d (const mpz_t, double);
int32_t mpz_cmpabs_d (const mpz_t, double);
void mpz_abs (mpz_t, const mpz_t);
void mpz_neg (mpz_t, const mpz_t);
void mpz_swap (mpz_t, mpz_t);
void mpz_add_ui (mpz_t, const mpz_t, unsigned long);
void mpz_add_ui (mpz_t, const mpz_t, uint64_t);
void mpz_add (mpz_t, const mpz_t, const mpz_t);
void mpz_sub_ui (mpz_t, const mpz_t, unsigned long);
void mpz_ui_sub (mpz_t, unsigned long, const mpz_t);
void mpz_sub_ui (mpz_t, const mpz_t, uint64_t);
void mpz_ui_sub (mpz_t, uint64_t, const mpz_t);
void mpz_sub (mpz_t, const mpz_t, const mpz_t);
void mpz_mul_si (mpz_t, const mpz_t, long int);
void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
void mpz_mul_si (mpz_t, const mpz_t, int64_t);
void mpz_mul_ui (mpz_t, const mpz_t, uint64_t);
void mpz_mul (mpz_t, const mpz_t, const mpz_t);
void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
void mpz_addmul_ui (mpz_t, const mpz_t, uint64_t);
void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
void mpz_submul_ui (mpz_t, const mpz_t, uint64_t);
void mpz_submul (mpz_t, const mpz_t, const mpz_t);
void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
@ -174,53 +174,53 @@ void mpz_mod (mpz_t, const mpz_t, const mpz_t);
void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
int mpz_divisible_p (const mpz_t, const mpz_t);
int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
int32_t mpz_divisible_p (const mpz_t, const mpz_t);
int32_t mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
unsigned long mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
unsigned long mpz_cdiv_q_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_fdiv_q_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_tdiv_q_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_cdiv_r_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_fdiv_r_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_tdiv_r_ui (mpz_t, const mpz_t, unsigned long);
unsigned long mpz_cdiv_ui (const mpz_t, unsigned long);
unsigned long mpz_fdiv_ui (const mpz_t, unsigned long);
unsigned long mpz_tdiv_ui (const mpz_t, unsigned long);
uint64_t mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uint64_t);
uint64_t mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uint64_t);
uint64_t mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uint64_t);
uint64_t mpz_cdiv_q_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_fdiv_q_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_tdiv_q_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_cdiv_r_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_fdiv_r_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_tdiv_r_ui (mpz_t, const mpz_t, uint64_t);
uint64_t mpz_cdiv_ui (const mpz_t, uint64_t);
uint64_t mpz_fdiv_ui (const mpz_t, uint64_t);
uint64_t mpz_tdiv_ui (const mpz_t, uint64_t);
unsigned long mpz_mod_ui (mpz_t, const mpz_t, unsigned long);
uint64_t mpz_mod_ui (mpz_t, const mpz_t, uint64_t);
void mpz_divexact_ui (mpz_t, const mpz_t, unsigned long);
void mpz_divexact_ui (mpz_t, const mpz_t, uint64_t);
int mpz_divisible_ui_p (const mpz_t, unsigned long);
int32_t mpz_divisible_ui_p (const mpz_t, uint64_t);
unsigned long mpz_gcd_ui (mpz_t, const mpz_t, unsigned long);
uint64_t mpz_gcd_ui (mpz_t, const mpz_t, uint64_t);
void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
void mpz_lcm_ui (mpz_t, const mpz_t, unsigned long);
void mpz_lcm_ui (mpz_t, const mpz_t, uint64_t);
void mpz_lcm (mpz_t, const mpz_t, const mpz_t);
int mpz_invert (mpz_t, const mpz_t, const mpz_t);
int32_t mpz_invert (mpz_t, const mpz_t, const mpz_t);
void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
void mpz_sqrt (mpz_t, const mpz_t);
int mpz_perfect_square_p (const mpz_t);
int32_t mpz_perfect_square_p (const mpz_t);
void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
void mpz_pow_ui (mpz_t, const mpz_t, uint64_t);
void mpz_ui_pow_ui (mpz_t, uint64_t, uint64_t);
void mpz_powm (mpz_t, const mpz_t, const mpz_t, const mpz_t);
void mpz_powm_ui (mpz_t, const mpz_t, unsigned long, const mpz_t);
void mpz_powm_ui (mpz_t, const mpz_t, uint64_t, const mpz_t);
void mpz_rootrem (mpz_t, mpz_t, const mpz_t, unsigned long);
int mpz_root (mpz_t, const mpz_t, unsigned long);
void mpz_rootrem (mpz_t, mpz_t, const mpz_t, uint64_t);
int32_t mpz_root (mpz_t, const mpz_t, uint64_t);
void mpz_fac_ui (mpz_t, unsigned long);
void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
void mpz_fac_ui (mpz_t, uint64_t);
void mpz_bin_uiui (mpz_t, uint64_t, uint64_t);
int mpz_probab_prime_p (const mpz_t, int);
int32_t mpz_probab_prime_p (const mpz_t, int32_t);
int mpz_tstbit (const mpz_t, mp_bitcnt_t);
int32_t mpz_tstbit (const mpz_t, mp_bitcnt_t);
void mpz_setbit (mpz_t, mp_bitcnt_t);
void mpz_clrbit (mpz_t, mp_bitcnt_t);
void mpz_combit (mpz_t, mp_bitcnt_t);
@ -235,10 +235,10 @@ mp_bitcnt_t mpz_hamdist (const mpz_t, const mpz_t);
mp_bitcnt_t mpz_scan0 (const mpz_t, mp_bitcnt_t);
mp_bitcnt_t mpz_scan1 (const mpz_t, mp_bitcnt_t);
int mpz_fits_slong_p (const mpz_t);
int mpz_fits_ulong_p (const mpz_t);
long int mpz_get_si (const mpz_t);
unsigned long int mpz_get_ui (const mpz_t);
int32_t mpz_fits_slong_p (const mpz_t);
int32_t mpz_fits_ulong_p (const mpz_t);
int64_t mpz_get_si (const mpz_t);
uint64_t mpz_get_ui (const mpz_t);
double mpz_get_d (const mpz_t);
size_t mpz_size (const mpz_t);
mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
@ -252,20 +252,20 @@ mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
void mpz_set_si (mpz_t, signed long int);
void mpz_set_ui (mpz_t, unsigned long int);
void mpz_set_si (mpz_t, int64_t);
void mpz_set_ui (mpz_t, uint64_t);
void mpz_set (mpz_t, const mpz_t);
void mpz_set_d (mpz_t, double);
void mpz_init_set_si (mpz_t, signed long int);
void mpz_init_set_ui (mpz_t, unsigned long int);
void mpz_init_set_si (mpz_t, int64_t);
void mpz_init_set_ui (mpz_t, uint64_t);
void mpz_init_set (mpz_t, const mpz_t);
void mpz_init_set_d (mpz_t, double);
size_t mpz_sizeinbase (const mpz_t, int);
char *mpz_get_str (char *, int, const mpz_t);
int mpz_set_str (mpz_t, const char *, int);
int mpz_init_set_str (mpz_t, const char *, int);
size_t mpz_sizeinbase (const mpz_t, int32_t);
char *mpz_get_str (char *, int32_t, const mpz_t);
int32_t mpz_set_str (mpz_t, const char *, int32_t);
int32_t mpz_init_set_str (mpz_t, const char *, int32_t);
/* This long list taken from gmp.h. */
/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
@ -285,11 +285,11 @@ int mpz_init_set_str (mpz_t, const char *, int);
|| defined (_STDIO_H_INCLUDED) /* QNX4 */ \
|| defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
|| defined (__STDIO_LOADED) /* VMS */
size_t mpz_out_str (FILE *, int, const mpz_t);
size_t mpz_out_str (FILE *, int32_t, const mpz_t);
#endif
void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *);
void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t);
void mpz_import (mpz_t, size_t, int32_t, size_t, int32_t, size_t, const void *);
void *mpz_export (void *, size_t *, int32_t, size_t, int32_t, size_t, const mpz_t);
#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
#define GMP_NAIL_BITS 0
#define GMP_NUMB_BITS (GMP_LIMB_BITS - GMP_NAIL_BITS)

22
iguana/swaps/iguana_BTCswap.c

@ -109,16 +109,21 @@ void iguana_addinputs(struct iguana_info *coin,struct bitcoin_spend *spend,cJSON
struct bitcoin_statetx *instantdex_signtx(struct supernet_info *myinfo,struct iguana_info *coin,uint32_t locktime,char *scriptstr,int64_t satoshis,int64_t txfee,int32_t minconf,int32_t myside)
{
struct iguana_waddress *waddr; struct bitcoin_statetx *tx=0; uint8_t pubkey33[33]; char coinaddr[64]; char *rawtx,*signedtx,*retstr; bits256 signedtxid; uint32_t rawtxtag; int32_t flag,completed; cJSON *valsobj,*vins,*retjson,*argjson,*addresses = cJSON_CreateArray();
struct iguana_waddress *waddr; struct iguana_waccount *wacct; struct bitcoin_statetx *tx=0; uint8_t pubkey33[33]; char coinaddr[64],wifstr[64]; char *rawtx,*signedtx,*retstr; bits256 signedtxid; uint32_t rawtxtag; int32_t flag,completed; cJSON *valsobj,*vins,*retjson,*privkey,*argjson,*addresses;
if ( (waddr= iguana_getaccountaddress(myinfo,coin,0,0,coin->changeaddr,"change")) == 0 )
return(0);
privkey = cJSON_CreateArray();
addresses = cJSON_CreateArray();
if ( coin->changeaddr[0] == 0 )
{
if ( (waddr= iguana_getaccountaddress(myinfo,coin,0,0,coin->changeaddr,"change")) == 0 )
return(0);
bitcoin_address(coin->changeaddr,coin->chain->pubtype,waddr->rmd160,20);
}
bitcoin_pubkey33(myinfo->ctx,pubkey33,myinfo->persistent_priv);
bitcoin_address(coinaddr,coin->chain->pubtype,pubkey33,33);
printf("%s persistent.(%s) (%s) change.(%s) scriptstr.(%s)\n",coin->symbol,myinfo->myaddr.BTC,coinaddr,coin->changeaddr,scriptstr);
if ( (waddr= iguana_waddresssearch(myinfo,coin,&wacct,coinaddr)) != 0 )
{
bitcoin_priv2wif(wifstr,waddr->privkey,coin->chain->wiftype);
jaddistr(privkey,waddr->wifstr);
}
jaddistr(addresses,coinaddr);
valsobj = cJSON_CreateObject();
jaddstr(valsobj,"coin",coin->symbol);
@ -132,7 +137,7 @@ struct bitcoin_statetx *instantdex_signtx(struct supernet_info *myinfo,struct ig
jaddnum(argjson,"timeout",15000);
if ( (retstr= iguana_rawtx(myinfo,coin,argjson,0,coin->changeaddr,addresses,valsobj,scriptstr)) != 0 )
{
printf("feetx got.(%s)\n",retstr);
//printf("feetx got.(%s)\n",retstr);
flag = 0;
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
@ -148,9 +153,10 @@ struct bitcoin_statetx *instantdex_signtx(struct supernet_info *myinfo,struct ig
flag = 2;
}
}
if ( flag != 0 )
if ( flag != 0 && vins != 0 )
{
if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx)) != 0 )
//printf("vins.(%s)\n",jprint(vins,0));
if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx,privkey)) != 0 )
{
iguana_unspentslock(myinfo,coin,vins);
tx = calloc(1,sizeof(*tx) + strlen(rawtx) + 1);

2
iguana/tests/decoderawtransaction

@ -1 +1 @@
curl --url "http://127.0.0.1:7778" --data "{\"method\":\"decoderawtransaction\",\"params\":[\"0100000011533957010252b9534fe0186625e3075606f9b547e223830af04b3fb9dab2aaadc7cad5300100000000ffffffff0280969800000000001976a914b7128d2ee837cf03e30a2c0e3e0181f7b9669bb688ac700c023b000000001976a914b7128d2ee837cf03e30a2c0e3e0181f7b9669bb688ac00000000\"]}"
curl --url "http://127.0.0.1:7778" --data "{\"coin\":\"BTC\",\"method\":\"decoderawtransaction\",\"params\":[\"01000000016def80687bd97e9b932827a9fa8ce20d7e0c9032b3bc3a211a830e80ac0f5d560100000000ffffffff02bb280000000000002308d7019f135580dfb27576a914ca1e04745e8ca0c60d8c5881531d51bec470743f88acb5469800000000001976a9145513ce20df70c082324da1781c222a4d68dfe87988ac00000000\"]}"

1
includes/curve25519.h

@ -76,5 +76,6 @@ uint64_t acct777_sign(struct acct777_sig *sig,bits256 privkey,bits256 otherpubke
uint64_t acct777_validate(struct acct777_sig *sig,bits256 privkey,bits256 pubkey);
uint64_t acct777_signtx(struct acct777_sig *sig,bits256 privkey,uint32_t timestamp,uint8_t *data,int32_t datalen);
uint64_t acct777_swaptx(bits256 privkey,struct acct777_sig *sig,uint32_t timestamp,uint8_t *data,int32_t datalen);
void calc_hmac_sha256(uint8_t *mac,int32_t maclen,uint8_t *key,int32_t key_size,uint8_t *message,int32_t len);
#endif

Loading…
Cancel
Save