Merge pull request #55 from jl777/acspeed

Acspeed
This commit is contained in:
jl777
2016-11-21 07:16:12 -03:00
committed by GitHub
21 changed files with 667 additions and 583 deletions

View File

@@ -1,6 +1,45 @@
#!/bin/bash
source pubkey.txt
echo $pubkey
curl --url "http://127.0.0.1:7776" --data "{\"timeout\":60000,\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"REVS\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"USD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"EUR\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"JPY\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"GBP\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"AUD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CAD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CHF\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NZD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CNY\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RUB\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MXN\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BRL\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"INR\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HKD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"TRY\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ZAR\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PLN\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NOK\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SEK\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"DKK\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CZK\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HUF\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ILS\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"KRW\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MYR\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PHP\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RON\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SGD\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"THB\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BGN\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"IDR\",\"pubkey\":\"$pubkey\"}"
curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HRK\",\"pubkey\":\"$pubkey\"}"
./komodod -pubkey=$pubkey -ac_name=REVS -ac_supply=1300000 -addnode=78.47.196.146 $1 &
./komodod -pubkey=$pubkey -ac_name=USD -addnode=78.47.196.146 $1 &
./komodod -pubkey=$pubkey -ac_name=EUR -addnode=78.47.196.146 $1 &
./komodod -pubkey=$pubkey -ac_name=JPY -addnode=78.47.196.146 $1 &

View File

@@ -66,7 +66,7 @@ public:
#include "cJSON.c"
#include "komodo_notary.h"
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
{
}

View File

@@ -33,9 +33,7 @@
static bool fDaemon;
extern char ASSETCHAINS_SYMBOL[16];
void komodo_gateway_iteration(char *symbol);
void komodo_iteration(char *symbol);
int32_t komodo_is_issuer();
void komodo_passport_iteration();
void WaitForShutdown(boost::thread_group* threadGroup)
{
@@ -44,10 +42,7 @@ void WaitForShutdown(boost::thread_group* threadGroup)
while (!fShutdown)
{
MilliSleep(10000);
if ( komodo_is_issuer() != 0 )
komodo_gateway_iteration(ASSETCHAINS_SYMBOL);
else komodo_iteration((char *)"EUR");
komodo_passport_iteration();
fShutdown = ShutdownRequested();
}
if (threadGroup)
@@ -64,6 +59,7 @@ void WaitForShutdown(boost::thread_group* threadGroup)
extern int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,ASSETCHAIN_INIT;
extern std::string NOTARY_PUBKEY;
int32_t komodo_is_issuer();
void komodo_passport_iteration();
bool AppInit(int argc, char* argv[])
{
@@ -106,10 +102,11 @@ bool AppInit(int argc, char* argv[])
fprintf(stderr,"call komodo_args NOTARY_PUBKEY.(%s)\n",NOTARY_PUBKEY.c_str());
while ( ASSETCHAIN_INIT == 0 )
{
if ( komodo_is_issuer() != 0 )
komodo_gateway_iteration(ASSETCHAINS_SYMBOL);
//if ( komodo_is_issuer() != 0 )
// komodo_passport_iteration();
sleep(1);
}
printf("initialized %s\n",ASSETCHAINS_SYMBOL);
if (!boost::filesystem::is_directory(GetDataDir(false)))
{
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
@@ -144,7 +141,6 @@ bool AppInit(int argc, char* argv[])
fDaemon = GetBoolArg("-daemon", false);
if (fDaemon)
{
extern char ASSETCHAINS_SYMBOL[16];
fprintf(stdout, "Komodo %s server starting\n",ASSETCHAINS_SYMBOL);
// Daemonize

View File

@@ -31,3 +31,4 @@ echo thb; fiat/thb $1 $2 $3 $4
echo try; fiat/try $1 $2 $3 $4
echo usd; fiat/usd $1 $2 $3 $4
echo zar; fiat/zar $1 $2 $3 $4
echo revs; fiat/revs $1 $2 $3 $4

2
src/fiat/revs Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
./komodo-cli -ac_name=REVS $1 $2 $3 $4

View File

@@ -17,7 +17,6 @@
#define H_KOMODO_H
// Todo:
// 0. optimize assetchains
// 1. error check fiat redeem amounts
// 2. net balance limiter
// 3. verify: reorgs
@@ -41,21 +40,16 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
#include "komodo_globals.h"
#include "komodo_utils.h"
void komodo_setkmdheight(int32_t kmdheight)
{
if ( kmdheight > KMDHEIGHT )
KMDHEIGHT = kmdheight;
}
#include "cJSON.c"
#include "komodo_bitcoind.h"
#include "komodo_interest.h"
#include "komodo_pax.h"
#include "komodo_notary.h"
int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
#include "komodo_gateway.h"
#include "komodo_events.h"
void komodo_currentheight_set(int32_t height)
{
char symbol[16],dest[16]; struct komodo_state *sp;
@@ -91,8 +85,8 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
errs++;
else
{
printf("updated %d pubkeys at ht.%d\n",num,ht);
if ( matched != 0 )
printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
}
} else printf("illegal num.%d\n",num);
@@ -105,11 +99,11 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
errs++;
if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
errs++;
printf("load NOTARIZED %d %s\n",notarized_height,notarized_hash.ToString().c_str());
if ( matched != 0 )
komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
//printf("%s load[%s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,notarized_height,notarized_hash.ToString().c_str());
//if ( matched != 0 ) global independent states -> inside *sp
komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
}
else if ( func == 'U' )
else if ( func == 'U' ) // deprecated
{
uint8_t n,nid; uint256 hash; uint64_t mask;
n = fgetc(fp);
@@ -127,8 +121,20 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
int32_t kheight;
if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
errs++;
if ( matched != 0 )
komodo_eventadd_kmdheight(sp,symbol,ht,kheight);
//if ( matched != 0 ) global independent states -> inside *sp
//printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
}
else if ( func == 'T' )
{
int32_t kheight,ktimestamp;
if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
errs++;
if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
errs++;
//if ( matched != 0 ) global independent states -> inside *sp
//printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
}
else if ( func == 'R' )
{
@@ -145,8 +151,11 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
{
if ( fread(opret,1,olen,fp) != olen )
errs++;
if ( matched != 0 )
komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
//if ( matched != 0 ) global shared state -> global PAX
int32_t i; for (i=0; i<olen; i++)
printf("%02x",opret[i]);
printf(" %s load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,symbol,opret[0],olen,(double)ovalue/COIN);
komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
} else printf("illegal olen.%u\n",olen);
}
else if ( func == 'D' )
@@ -159,8 +168,9 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
numpvals = fgetc(fp);
if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
{
if ( matched != 0 )
komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
//if ( matched != 0 ) global shared state -> global PVALS
//printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
//printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
} else printf("error loading pvals[%d]\n",numpvals);
}
@@ -169,7 +179,7 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
} else return(-1);
}
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
{
static FILE *fp; static int32_t errs;
struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
@@ -177,25 +187,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
return;
if ( fp == 0 )
{
#ifdef WIN32
sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
//sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids");
#else
sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
//sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids");
#endif
/*memset(Minerids,0xfe,sizeof(Minerids));
if ( (Minerfp= fopen(fname2,"rb+")) == 0 )
{
if ( (Minerfp= fopen(fname2,"wb")) != 0 )
{
fwrite(Minerids,1,sizeof(Minerids),Minerfp);
fclose(Minerfp);
}
Minerfp = fopen(fname2,"rb+");
}
if ( Minerfp != 0 && fread(Minerids,1,sizeof(Minerids),Minerfp) != sizeof(Minerids) )
printf("read error Minerids\n");*/
komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate");
if ( (fp= fopen(fname,"rb+")) != 0 )
{
while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
@@ -212,14 +204,27 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
{
//printf("fpos.%ld ",ftell(fp));
if ( KMDheight > 0 )
if ( KMDheight != 0 )
{
fputc('K',fp);
if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
errs++;
if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
errs++;
komodo_eventadd_kmdheight(sp,symbol,height,KMDheight);
if ( KMDtimestamp != 0 )
{
fputc('T',fp);
if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
errs++;
if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
errs++;
if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
errs++;
}
else
{
fputc('K',fp);
if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
errs++;
if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
errs++;
}
komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
}
else if ( opretbuf != 0 && opretlen > 0 )
{
@@ -306,7 +311,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
}
}
int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp,uint64_t value)
int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp,uint64_t value,int32_t notarized,uint64_t signedmask)
{
static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16];
if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
@@ -361,14 +366,16 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr
len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
{
printf("ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
printf("%s ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
sp->NOTARIZED_HEIGHT = *notarizedheightp;
sp->NOTARIZED_HASH = kmdtxid;
sp->NOTARIZED_DESTTXID = desttxid;
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
} else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
if ( opretlen > len && scriptbuf[len] == 'A' )
komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len,j);
} else printf("notarized.%d %llx reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
}
else if ( i == 0 && j == 1 && opretlen == 149 )
komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
@@ -376,7 +383,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr
{
int32_t k; for (k=0; k<scriptlen; k++)
printf("%02x",scriptbuf[k]);
printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
{
for (k=0; k<32; k++)
@@ -389,7 +396,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr
}
}
if ( *isratificationp == 0 )
komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
}
}
return(notaryid);
@@ -407,7 +414,7 @@ int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
// if all outputs to notary -> notary utxo
// if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
// OP_RETURN: 'D' -> deposit, 'W' -> withdraw
void komodo_currentheight_set(int32_t height);
int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
@@ -428,30 +435,32 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
{
static int32_t hwmheight;
uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp;
uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,btctxid,txhash;
int32_t i,j,k,numnotaries,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
memset(&zero,0,sizeof(zero));
komodo_init(pindex->nHeight);
if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
return;
numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
calc_rmd160_sha256(rmd160,pubkeys[0],33);
if ( pindex->nHeight > hwmheight )
hwmheight = pindex->nHeight;
else
{
printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight);
if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
komodo_event_rewind(sp,symbol,pindex->nHeight);
// komodo_stateupdate();
printf("hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",hwmheight,pindex->nHeight,(uint32_t)pindex->nTime,hwmheight-pindex->nHeight);
komodo_event_rewind(sp,symbol,pindex->nHeight);
komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0);
}
komodo_currentheight_set(chainActive.Tip()->nHeight);
if ( komodo_is_issuer() != 0 )
/*if ( komodo_is_issuer() != 0 )
{
while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
{
fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
sleep(30);
}
}
KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
}*/
KOMODO_INITDONE = (uint32_t)time(NULL);
if ( pindex != 0 )
{
height = pindex->nHeight;
@@ -461,7 +470,23 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
txhash = block.vtx[i].GetHash();
numvouts = block.vtx[i].vout.size();
notaryid = -1;
voutmask = specialtx = notarizedheight = isratification = 0;
voutmask = specialtx = notarizedheight = isratification = notarized = 0;
signedmask = 0;
numvins = block.vtx[i].vin.size();
for (j=0; j<numvins; j++)
{
if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
{
if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
signedmask |= (1LL << k);
}
}
numvalid = bitweight(signedmask);
if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1)) )
{
//printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
notarized = 1;
}
for (j=0; j<numvouts; j++)
{
len = block.vtx[i].vout[j].scriptPubKey.size();
@@ -472,7 +497,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
#else
memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
#endif
notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue,notarized,signedmask);
if ( 0 && i > 0 )
{
for (k=0; k<len; k++)
@@ -481,29 +506,9 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
}
}
}
if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) )
{
//komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
}
signedmask = 0;
numvins = block.vtx[i].vin.size();
for (j=0; j<numvins; j++)
{
if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
{
if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
signedmask |= (1LL << k);
}
/*if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
signedmask |= (1LL << k);
else if ( signedmask != 0 )
printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n);*/
}
if ( signedmask != 0 )
printf("ht.%d signedmask.%llx numvins.%d numvouts.%d\n",height,(long long)signedmask,numvins,numvouts);
if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
{
printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
printf("%s NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",ASSETCHAINS_SYMBOL,(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
{
@@ -531,12 +536,14 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
{
memset(&txhash,0,sizeof(txhash));
komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
printf("RATIFIED! >>>>>>>>>> new notaries.%d newheight.%d from height.%d\n",numvalid,(((height+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,height);
} else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
}
}
}
if ( pindex->nHeight == hwmheight )
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
} else printf("komodo_connectblock: unexpected null pindex\n");
KOMODO_INITDONE = (uint32_t)time(NULL);
}

View File

@@ -329,7 +329,7 @@ char *curl_post(CURL **cHandlep,char *url,char *userpass,char *postfields,char *
return(chunk.memory);
}
char *komodo_issuemethod(char *method,char *params,uint16_t port)
char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port)
{
//static void *cHandle;
char url[512],*retstr=0,*retstr2=0,postdata[8192];
@@ -340,7 +340,7 @@ char *komodo_issuemethod(char *method,char *params,uint16_t port)
sprintf(url,(char *)"http://127.0.0.1:%u",port);
sprintf(postdata,"{\"method\":\"%s\",\"params\":%s}",method,params);
//printf("postdata.(%s) USERPASS.(%s)\n",postdata,KMDUSERPASS);
retstr2 = bitcoind_RPC(&retstr,(char *)"debug",url,KMDUSERPASS,method,params);
retstr2 = bitcoind_RPC(&retstr,(char *)"debug",url,userpass,method,params);
//retstr = curl_post(&cHandle,url,USERPASS,postdata,0,0,0,0);
}
return(retstr2);
@@ -539,7 +539,7 @@ int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 has
}
} else fprintf(stderr,"unexpected error notary_hash %s ht.%d at ht.%d\n",notarized_hash.ToString().c_str(),notarized_height,notary->nHeight);
} else if ( notarized_height > 0 && notarized_height != 73880 )
fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height);
fprintf(stderr,"[%s] couldnt find notarized.(%s %d) ht.%d\n",ASSETCHAINS_SYMBOL,notarized_hash.ToString().c_str(),notarized_height,chainActive.Tip()->nHeight);
return(0);
}
@@ -577,3 +577,14 @@ uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256
}
return(0);
}
int32_t komodo_isrealtime(int32_t *kmdheightp)
{
struct komodo_state *sp;
if ( (sp= komodo_stateptrget((char *)"KMD")) != 0 )
*kmdheightp = sp->CURRENT_HEIGHT;
else *kmdheightp = 0;
if ( chainActive.Tip()->nHeight == (int32_t)komodo_longestchain() )
return(1);
else return(0);
}

View File

@@ -16,45 +16,7 @@
#ifndef H_KOMODOEVENTS_H
#define H_KOMODOEVENTS_H
#ifdef WIN32
#define PACKED
#else
#define PACKED __attribute__((packed))
#endif
#define KOMODO_EVENT_RATIFY 'P'
#define KOMODO_EVENT_NOTARIZED 'N'
#define KOMODO_EVENT_UTXO 'U'
#define KOMODO_EVENT_KMDHEIGHT 'K'
#define KOMODO_EVENT_REWIND 'B'
//#define KOMODO_EVENT_DELETE 'D'
#define KOMODO_EVENT_PRICEFEED 'V'
#define KOMODO_EVENT_OPRETURN 'R'
#define KOMODO_OPRETURN_DEPOSIT 'D'
#define KOMODO_OPRETURN_ISSUED 'I' // assetchain
#define KOMODO_OPRETURN_WITHDRAW 'W' // assetchain
#define KOMODO_OPRETURN_REDEEMED 'X'
struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; };
struct komodo_event_pubkeys { uint8_t num; uint8_t pubkeys[64][33]; };
struct komodo_event_utxo { uint256 txid; uint64_t voutmask; uint8_t numvouts; };
struct komodo_event_opreturn { uint256 txid; uint64_t value; uint16_t vout,oplen; uint8_t opret[]; };
struct komodo_event_pricefeed { uint8_t num; uint32_t prices[35]; };
struct komodo_event
{
struct komodo_event *related;
uint16_t len;
int32_t height;
uint8_t type,reorged;
char symbol[16];
uint8_t space[];
} PACKED;
struct komodo_event **Komodo_events; int32_t Komodo_numevents;
struct komodo_event *komodo_eventadd(int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen)
struct komodo_event *komodo_eventadd(struct komodo_state *sp,int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen)
{
struct komodo_event *ep; uint16_t len = (uint16_t)(sizeof(*ep) + datalen);
ep = (struct komodo_event *)calloc(1,len);
@@ -64,8 +26,8 @@ struct komodo_event *komodo_eventadd(int32_t height,char *symbol,uint8_t type,ui
strcpy(ep->symbol,symbol);
if ( datalen != 0 )
memcpy(ep->space,data,datalen);
Komodo_events = (struct komodo_event **)realloc(Komodo_events,(1 + Komodo_numevents) * sizeof(*Komodo_events));
Komodo_events[Komodo_numevents++] = ep;
sp->Komodo_events = (struct komodo_event **)realloc(sp->Komodo_events,(1 + sp->Komodo_numevents) * sizeof(*sp->Komodo_events));
sp->Komodo_events[sp->Komodo_numevents++] = ep;
return(ep);
}
@@ -77,7 +39,7 @@ void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t heig
N.desttxid = notarized_desttxid;
N.notarizedheight = notarizedheight;
strcpy(N.dest,dest);
komodo_eventadd(height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N));
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N));
if ( sp != 0 )
komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid);
}
@@ -89,41 +51,22 @@ void komodo_eventadd_pubkeys(struct komodo_state *sp,char *symbol,int32_t height
memset(&P,0,sizeof(P));
P.num = num;
memcpy(P.pubkeys,pubkeys,33 * num);
komodo_eventadd(height,symbol,KOMODO_EVENT_RATIFY,(uint8_t *)&P,(int32_t)(sizeof(P.num) + 33 * num));
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_RATIFY,(uint8_t *)&P,(int32_t)(sizeof(P.num) + 33 * num));
if ( sp != 0 )
komodo_notarysinit(height,pubkeys,num);
}
/*void komodo_eventadd_utxo(struct komodo_state *sp,char *symbol,int32_t height,uint8_t notaryid,uint256 txid,uint64_t voutmask,uint8_t numvouts)
{
struct komodo_event_utxo U;
memset(&U,0,sizeof(U));
U.txid = txid;
U.voutmask = voutmask;
U.numvouts = numvouts;
komodo_eventadd(height,symbol,KOMODO_EVENT_UTXO,(uint8_t *)&U,sizeof(U));
if ( sp != 0 )
komodo_nutxoadd(height,notaryid,txid,voutmask,numvouts);
}*/
void komodo_eventadd_pricefeed(struct komodo_state *sp,char *symbol,int32_t height,uint32_t *prices,uint8_t num)
{
struct komodo_event_pricefeed F;
memset(&F,0,sizeof(F));
F.num = num;
memcpy(F.prices,prices,sizeof(*F.prices) * num);
komodo_eventadd(height,symbol,KOMODO_EVENT_PRICEFEED,(uint8_t *)&F,(int32_t)(sizeof(F.num) + sizeof(*F.prices) * num));
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_PRICEFEED,(uint8_t *)&F,(int32_t)(sizeof(F.num) + sizeof(*F.prices) * num));
if ( sp != 0 )
komodo_pvals(height,prices,num);
}
void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight)
{
komodo_eventadd(height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)&kmdheight,sizeof(kmdheight));
if ( sp != 0 )
komodo_setkmdheight(kmdheight);
}
void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t height,uint256 txid,uint64_t value,uint16_t vout,uint8_t *buf,uint16_t opretlen)
{
struct komodo_event_opreturn O; uint8_t opret[10000];
@@ -134,21 +77,26 @@ void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t heigh
memcpy(opret,&O,sizeof(O));
memcpy(&opret[sizeof(O)],buf,opretlen);
O.oplen = (int32_t)(opretlen + sizeof(O));
komodo_eventadd(height,symbol,KOMODO_EVENT_OPRETURN,opret,O.oplen);
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_OPRETURN,opret,O.oplen);
if ( sp != 0 )
komodo_opreturn(height,value,buf,opretlen,txid,vout);
}
void komodo_event_undo(struct komodo_event *ep)
void komodo_event_undo(struct komodo_state *sp,struct komodo_event *ep)
{
switch ( ep->type )
{
case KOMODO_EVENT_RATIFY:
case KOMODO_EVENT_NOTARIZED:
case KOMODO_EVENT_UTXO:
case KOMODO_EVENT_RATIFY: printf("rewind of ratify, needs to be coded.%d\n",ep->height); break;
case KOMODO_EVENT_NOTARIZED: printf("unexpected rewind of notarization.%d\n",ep->height); break;
case KOMODO_EVENT_KMDHEIGHT:
if ( ep->height <= sp->SAVEDHEIGHT )
sp->SAVEDHEIGHT = ep->height;
break;
case KOMODO_EVENT_PRICEFEED:
// backtrack prices;
break;
case KOMODO_EVENT_OPRETURN:
// backtrack opreturns
break;
}
}
@@ -156,23 +104,54 @@ void komodo_event_undo(struct komodo_event *ep)
void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height)
{
struct komodo_event *ep;
komodo_eventadd(height,symbol,KOMODO_EVENT_REWIND,(uint8_t *)&height,sizeof(height));
if ( sp != 0 )
{
while ( Komodo_numevents > 0 )
while ( sp->Komodo_events != 0 && sp->Komodo_numevents > 0 )
{
if ( (ep= Komodo_events[Komodo_numevents-1]) != 0 )
if ( (ep= sp->Komodo_events[sp->Komodo_numevents-1]) != 0 )
{
if ( ep->height < height )
break;
printf("undo event.%c ht.%d for rewind.%d\n",ep->type,ep->height,height);
komodo_event_undo(ep);
Komodo_numevents--;
//printf("[%s] undo %s event.%c ht.%d for rewind.%d\n",ASSETCHAINS_SYMBOL,symbol,ep->type,ep->height,height);
komodo_event_undo(sp,ep);
sp->Komodo_numevents--;
}
}
}
}
void komodo_setkmdheight(struct komodo_state *sp,int32_t kmdheight,uint32_t timestamp)
{
if ( kmdheight > sp->SAVEDHEIGHT )
{
sp->SAVEDHEIGHT = kmdheight;
sp->SAVEDTIMESTAMP = timestamp;
}
if ( kmdheight > sp->CURRENT_HEIGHT )
sp->CURRENT_HEIGHT = kmdheight;
}
void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight,uint32_t timestamp)
{
uint32_t buf[2];
if ( kmdheight > 0 )
{
buf[0] = (uint32_t)kmdheight;
buf[1] = timestamp;
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)buf,sizeof(buf));
if ( sp != 0 )
komodo_setkmdheight(sp,kmdheight,timestamp);
}
else
{
kmdheight = -kmdheight;
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_REWIND,(uint8_t *)&height,sizeof(height));
if ( sp != 0 )
komodo_event_rewind(sp,symbol,height);
}
}
/*void komodo_eventadd_deposit(int32_t actionflag,char *symbol,int32_t height,uint64_t komodoshis,char *fiat,uint64_t fiatoshis,uint8_t rmd160[20],bits256 kmdtxid,uint16_t kmdvout,uint64_t price)
{
uint8_t opret[512]; uint16_t opretlen;

View File

@@ -15,21 +15,6 @@
// paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
uint64_t komodo_paxtotal()
{
struct pax_transaction *pax,*tmp; uint64_t total = 0;
HASH_ITER(hh,PAX,pax,tmp)
{
if ( pax->marked == 0 )
{
if ( komodo_is_issuer() != 0 )
total += pax->fiatoshis;
else total += pax->komodoshis;
}
}
return(total);
}
struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
{
struct pax_transaction *pax;
@@ -66,9 +51,10 @@ struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *sp
return(pax);
}
void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight) // assetchain context
void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight) // assetchain context
{
struct pax_transaction *pax; int32_t addflag = 0;
struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16];
sp = komodo_stateptr(str,dest);
pthread_mutex_lock(&komodo_mutex);
HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
if ( pax == 0 )
@@ -89,13 +75,18 @@ void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char
if ( coinaddr != 0 )
{
strcpy(pax->coinaddr,coinaddr);
pax->komodoshis = value;
pax->shortflag = shortflag;
strcpy(pax->symbol,symbol);
pax->fiatoshis = fiatoshis;
memcpy(pax->rmd160,rmd160,20);
pax->height = height;
pax->otherheight = otherheight;
if ( value != 0 )
pax->komodoshis = value;
if ( symbol != 0 )
strcpy(pax->symbol,symbol);
if ( fiatoshis != 0 )
pax->fiatoshis = fiatoshis;
if ( rmd160 != 0 )
memcpy(pax->rmd160,rmd160,20);
if ( height != 0 )
pax->height = height;
if ( otherheight != 0 )
pax->otherheight = otherheight;
if ( pax->marked == 0 )
{
if ( addflag != 0 )
@@ -110,18 +101,16 @@ void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char
}
}
int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
{
int32_t i,n=0,j,len;
if ( opretbuf[opretlen-5] == '-' )
*shortflagp = 1;
else *shortflagp = 0;
int32_t i,n=0,j,len,incr,height,otherheight; uint8_t rmd160[20]; uint64_t fiatoshis; char symbol[16];
for (i=0; i<4; i++)
base[i] = opretbuf[opretlen-4+i];
if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
{
opretbuf++, opretlen--;
for (n=len=0; n<opretlen/34; n++)
incr = 34 + (iskomodo * (sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
for (n=len=0; n<opretlen/incr; n++)
{
for (j=0; j<32; j++)
{
@@ -133,37 +122,82 @@ int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uin
//printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
if ( iskomodo != 0 )
{
uint64_t fiatoshis; int32_t height,otherheight,shortflag; char symbol[16];
len += iguana_rwnum(0,&opretbuf[len],sizeof(fiatoshis),&fiatoshis);
len += iguana_rwnum(0,&opretbuf[len],sizeof(height),&height);
len += iguana_rwnum(0,&opretbuf[len],sizeof(otherheight),&otherheight);
if ( opretbuf[len] == '-' )
shortflag = 1, len++;
else shortflag = 0;
memcpy(rmd160,&opretbuf[len],20);
for (i=0; opretbuf[len+i]!=0&&i<3; i++)
symbol[i] = opretbuf[len+i];
symbol[i] = 0;
printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d short.%d\n",symbol,dstr(fiatoshis),height,otherheight,shortflag);
if ( values != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 )
{
values[i] = fiatoshis;
kmdheights[i] = height;
otherheights[i] = otherheight;
memcpy(&rmd160s[n * 20],rmd160,20);
baseids[i] = komodo_baseid(symbol);
}
printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d\n",symbol,dstr(fiatoshis),height,otherheight);
}
}
}
return(n);
}
int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *base,int32_t tokomodo)
uint64_t komodo_paxtotal()
{
struct pax_transaction *pax,*tmp; char symbol[16]; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
struct pax_transaction *pax,*tmp; int32_t ht; uint64_t total = 0;
if ( komodo_isrealtime(&ht) == 0 )
return(0);
HASH_ITER(hh,PAX,pax,tmp)
{
//printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
if ( pax->marked == 0 )
{
if ( komodo_is_issuer() != 0 )
total += pax->fiatoshis;
else total += pax->komodoshis;
}
}
//printf("paxtotal %.8f\n",dstr(total));
return(total);
}
int32_t komodo_withdraws_pending(char *opretbuf)
{
struct pax_transaction *pax,*tmp; int32_t ht,len=0; uint64_t total = 0;
if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
return(0);
HASH_ITER(hh,PAX,pax,tmp)
{
if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 )
{
// add 'A' opreturn entry
}
}
return(len);
}
int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
{
struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t mask;
sp = komodo_stateptr(symbol,dest);
strcpy(symbol,base);
PENDING_KOMODO_TX = 0;
if ( tokomodo == 0 )
{
opcode = 'I';
if ( komodo_isrealtime(&ht) == 0 )
return(0);
}
else opcode = 'X';
HASH_ITER(hh,PAX,pax,tmp)
{
if ( pax->marked != 0 )
//printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 || (strcmp(symbol,"KMD") == 0 && pax->approved == 0) )
continue;
if ( ASSETCHAINS_SYMBOL[0] != 0 )
printf("pax.%p marked.%d %.8f -> %.8f\n",pax,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
//if ( ASSETCHAINS_SYMBOL[0] != 0 )
printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
txNew->vout.resize(numvouts+1);
txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
txNew->vout[numvouts].scriptPubKey.resize(25);
@@ -189,9 +223,7 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,cha
len += iguana_rwnum(1,&data[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
len += iguana_rwnum(1,&data[len],sizeof(pax->height),&pax->height);
len += iguana_rwnum(1,&data[len],sizeof(pax->otherheight),&pax->otherheight);
if ( pax->shortflag != 0 )
data[len++] = '-';
for (i=0; pax->symbol[i]!=0&&i<3; i++)
for (i=0; pax->symbol[i]!=0&&i<3; i++) // must be 3 letter currency
data[len++] = pax->symbol[i];
data[len++] = 0;
PENDING_KOMODO_TX += pax->komodoshis;
@@ -202,8 +234,6 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,cha
}
if ( numvouts > 1 )
{
if ( shortflag != 0 )
data[len++] = '-';
if ( tokomodo != 0 )
strcpy(symbol,(char *)"KMD");
for (i=0; symbol[i]!=0; i++)
@@ -223,7 +253,12 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,cha
int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
{
int32_t i,j,n,num,opretlen,offset=1,errs=0,matched=0; uint256 hash,txids[64]; uint8_t shortflag; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space;
int32_t i,j,n,num,opretlen,offset=1,errs=0,matched=0,kmdheights[64],otherheights[64]; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; int8_t baseids[64]; uint8_t *script,opcode,rmd160s[64*20]; int64_t values[64]; struct pax_transaction *pax,space;
memset(baseids,0xff,sizeof(baseids));
memset(values,0,sizeof(values));
memset(rmd160s,0,sizeof(rmd160s));
memset(kmdheights,0,sizeof(kmdheights));
memset(otherheights,0,sizeof(otherheights));
n = block.vtx[0].vout.size();
script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
if ( n <= 2 || script[0] != 0x6a )
@@ -235,7 +270,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
// printf("%02x",script[i]);
//printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
opcode = 'X';
strcpy(symbol,"KMD");
strcpy(symbol,(char *)"KMD");
}
else
{
@@ -244,7 +279,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
}
if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
{
if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen,opcode == 'X')) > 0 )
if ( (num= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
{
for (i=1; i<n-1; i++)
{
@@ -257,6 +292,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
else matched++;
if ( 0 && opcode == 'X' )
printf("errs.%d i.%d match %.8f == %.8f\n",errs,i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
}
else
{
@@ -265,7 +301,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
{
for (j=0; j<32; j++)
printf("%02x",((uint8_t *)&hash)[j]);
printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
// validate amount! via fiat chain
}
}
@@ -277,12 +313,11 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
matched++;
for (j=0; j<32; j++)
printf("%02x",((uint8_t *)&txids[i-1])[j]);
printf(" cant paxfind txid\n");
printf(" cant paxfind X txid\n");
// validate amount! via fiat chain
} else if ( opcode == 'I' )
matched++;
}
komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
}
if ( matched != num )
{
@@ -298,7 +333,13 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above
const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
{
uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; double diff; uint64_t seed; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fiatoshis,komodoshis,checktoshis,values[64]; struct pax_transaction *pax;
const char *typestr = "unknown";
memset(baseids,0xff,sizeof(baseids));
memset(values,0,sizeof(values));
memset(rmd160s,0,sizeof(rmd160s));
memset(kmdheights,0,sizeof(kmdheights));
memset(otherheights,0,sizeof(otherheights));
tokomodo = (komodo_is_issuer() == 0);
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
{
@@ -313,260 +354,157 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
memset(base,0,sizeof(base));
PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
if ( fiatoshis < 0 )
fiatoshis = -fiatoshis;
bitcoin_address(coinaddr,addrtype,rmd160,20);
checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
typestr = "deposit";
printf("kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
diff = ((double)value / checktoshis) - 1.;
if ( diff < 0. )
diff = -diff;
printf("%s kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
if ( kmdheight <= height )
{
if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG )
if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
{
if ( shortflag == 0 )
for (i=0; i<32; i++)
printf("%02x",((uint8_t *)&txid)[i]);
printf(" <- txid.v%u ",vout);
for (i=0; i<33; i++)
printf("%02x",pubkey33[i]);
printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
if ( value == checktoshis )//value >= checktoshis || (seed == 0 && diff < .01) )
{
for (i=0; i<32; i++)
printf("%02x",((uint8_t *)&txid)[i]);
printf(" <- txid.v%u ",vout);
for (i=0; i<33; i++)
printf("%02x",pubkey33[i]);
printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
if ( value >= checktoshis || (seed == 0 && diff < .01) )
if ( komodo_paxfind(&space,txid,vout) == 0 )
{
if ( komodo_paxfind(&space,txid,vout) == 0 )
{
komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight,height);
} else printf("duplicate deposit\n");
}
}
else // short
{
for (i=0; i<opretlen; i++)
printf("%02x",opretbuf[i]);
printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis));
if ( value <= checktoshis || (seed == 0 && diff < .01) )
{
}
komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height);
} else printf("duplicate deposit\n");
}
}
}
}
}
else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
else if ( opretbuf[0] == 'W' && opretlen >= 38 )
{
if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
memset(base,0,sizeof(base));
PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
bitcoin_address(coinaddr,addrtype,rmd160,20);
checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
typestr = "withdraw";
printf("%s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s)\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr);
if ( checktoshis == komodoshis )
{
if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
if ( (pax= komodo_paxfind(&space,txid,vout)) == 0 )
{
printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
}
else
{
printf(" %.8f -> %s withdraw already there\n",dstr(value),coinaddr);
komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height);
}
}
}
else if ( tokomodo != 0 && opretbuf[0] == 'A' )
{
if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
{
for (i=0; i<n; i++)
{
if ( (pax= komodo_paxfind(&space,txids[i],vouts[i])) == 0 )
{
bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],kmdheights[i],otherheights[i]);
} else pax->approved = kmdheights[i];
}
}
}
else if ( tokomodo == 0 && opretbuf[0] == 'I' )
{
if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
{
if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
{
for (i=0; i<n; i++)
{
//for (j=0; j<32; j++)
// printf("%02x",((uint8_t *)&txids[i])[j]);
bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 )
{
komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height,0);
}
else
{
//printf(" duplicate issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
}
komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0);
}
}
}
else if ( tokomodo != 0 && opretbuf[0] == 'X' )
{
// verify and update limits
}
}
return(typestr);
}
void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
void komodo_passport_iteration()
{
int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
typestr = "unknown";
memcpy(&utxid,&txid,sizeof(utxid));
if ( script[offset++] == 0x6a )
{
offset += komodo_scriptitemlen(&opretlen,&script[offset]);
if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
typestr = "notarized";
else if ( txi == 0 && vout == 1 && opretlen == 149 )
{
typestr = "pricefeed";
komodo_paxpricefeed(height,&script[offset],opretlen);
//printf("height.%d pricefeed len.%d\n",height,opretlen);
}
else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
}
else if ( numvouts >= KOMODO_MINRATIFY )
typestr = "ratify";
}
int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
{
char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *oldpub,*newpub,*json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
sprintf(params,"[\"%s\", 1]",txidstr);
if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
{
if ( (json= cJSON_Parse(retstr)) != 0 )
{
if ( (result= jobj(json,(char *)"result")) != 0 )
{
oldpub = jobj(result,(char *)"vpub_old");
newpub = jobj(result,(char *)"vpub_new");
retval = 0;
if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
{
isspecial = 0;
txid = jbits256(result,(char *)"txid");
for (vout=0; vout<n; vout++)
{
item = jitem(vouts,vout);
value = SATOSHIDEN * jdouble(item,(char *)"value");
if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
{
if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
{
len = (int32_t)strlen(hexstr) >> 1;
if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
isspecial = 1;
else if ( len <= sizeof(script) )
{
decode_hex(script,len,hexstr);
komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
}
}
}
}
}
} else printf("error getting txids.(%s) %p\n",retstr,result);
free_json(json);
}
free(retstr);
}
return(retval);
}
int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
{
char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
sprintf(params,"[%d]",height);
if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
{
if ( (result= cJSON_Parse(retstr)) != 0 )
{
if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
{
sprintf(params,"[\"%s\"]",txidstr);
if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
{
//printf("getblock.(%s)\n",retstr2);
if ( (json= cJSON_Parse(retstr2)) != 0 )
{
if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
{
for (i=0; i<n; i++)
if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
break;
if ( i == n )
retval = 0;
else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
} else printf("cant get result.%p or tx.%p\n",result,tx);
free_json(json);
} else printf("cant parse2.(%s)\n",retstr2);
free(retstr2);
} else printf("error getblock %s\n",params);
} else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
free_json(result);
} else printf("couldnt parse.(%s)\n",retstr);
free(retstr);
} else printf("error from getblockhash %d\n",height);
return(retval);
}
void komodo_gateway_iteration(char *symbol)
{
char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
if ( KMDHEIGHT <= 0 )
KMDHEIGHT = 1;
//KOMODO_REALTIME = 0;
if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
{
if ( (infoobj= cJSON_Parse(retstr)) != 0 )
{
if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
{
//printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight);
for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
{
if ( (KMDHEIGHT % 10) == 0 )
{
if ( (KMDHEIGHT % 100) == 0 )
fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
memset(&zero,0,sizeof(zero));
komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
}
if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
{
printf("error KMDHEIGHT %d\n",KMDHEIGHT);
break;
}
usleep(10000);
}
if ( KMDHEIGHT >= kmdheight )
KOMODO_REALTIME = (uint32_t)time(NULL);
}
free_json(infoobj);
}
free(retstr);
}
else
{
printf("error from %s\n",symbol);
sleep(30);
}
}
void komodo_iteration(char *symbol)
{
char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height,fiatheight; uint256 txid; uint8_t rmd160[20],addrtype;
static long lastpos[34]; static char userpass[33][1024];
FILE *fp; int32_t baseid,isrealtime,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3]; cJSON *infoobj,*result; uint64_t RTmask = 0;
refsp = komodo_stateptr(symbol,dest);
if ( ASSETCHAINS_SYMBOL[0] == 0 )
refid = 33;
else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
//printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
for (baseid=0; baseid<=32; baseid++)
{
sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol);
if ( (retstr= issue_curl(cmd)) != 0 )
sp = 0;
isrealtime = 0;
base = (char *)CURRENCIES[baseid];
if ( baseid+1 != refid )
{
if ( (array= cJSON_Parse(retstr)) != 0 )
komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
komodo_nameset(symbol,dest,base);
//port = komodo_port(base,10,&magic) + 1;
if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
{
if ( (n= cJSON_GetArraySize(array)) > 0 )
//printf("refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
fseek(fp,0,SEEK_END);
if ( ftell(fp) > lastpos[baseid] )
{
for (i=0; i<n; i++)
{
item = jitem(array,i);
coinaddr = jstr(item,(char *)"address");
value = jdouble(item,(char *)"KMD") * COIN;
base = jstr(item,(char *)"fiat");
shortflag = juint(item,(char *)"short");
vout = jint(item,(char *)"prev_vout");
height = jint(item,(char *)"kmdheight");
fiatheight = jint(item,(char *)"height");
txidstr = jstr(item,(char *)"prev_hash");
if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 )
{
fiatoshis = jdouble(item,base) * COIN;
decode_hex((uint8_t *)&txid,sizeof(txid),txidstr);
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height,fiatheight);
}
}
}
fseek(fp,lastpos[baseid],SEEK_SET);
while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
;
lastpos[baseid] = ftell(fp);
//printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
} //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
fclose(fp);
}
//printf("retstr.(%s)\n",retstr);
free(retstr);
komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
if ( (fp= fopen(fname,"rb")) != 0 )
{
if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
{
sp->CURRENT_HEIGHT = buf[0];
if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
{
isrealtime = 1;
RTmask |= (1LL << baseid);
memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
} //else fprintf(stderr,"%s not RT\n",base);
} //else fprintf(stderr,"%s size error RT\n",base);
fclose(fp);
} else fprintf(stderr,"%s open error RT\n",base);
}
else
{
komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
if ( (fp= fopen(fname,"wb")) != 0 )
{
buf[0] = (uint32_t)chainActive.Tip()->nHeight;
buf[1] = (uint32_t)komodo_longestchain();
if ( buf[0] != 0 && buf[0] == buf[1] )
{
buf[2] = (uint32_t)time(NULL);
RTmask |= (1LL << baseid) | 1;
memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
if ( refid != 0 )
memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
}
if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
fclose(fp);
} else fprintf(stderr,"%s create error RT\n",base);
}
if ( sp != 0 && isrealtime == 0 )
refsp->RTbufs[0][2] = 0;
}
refsp->RTmask = RTmask;
}

View File

@@ -13,43 +13,39 @@
* *
******************************************************************************/
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t kheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout);
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t kheight,uint32_t ktime,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout);
void komodo_init(int32_t height);
int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp);
char *komodo_issuemethod(char *method,char *params,uint16_t port);
char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port);
void komodo_init(int32_t height);
void komodo_assetchain_pubkeys(char *jsonstr);
int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33);
int32_t komodo_isrealtime(int32_t *kmdheightp);
uint64_t komodo_paxtotal();
int32_t komodo_longestchain();
pthread_mutex_t komodo_mutex;
//FILE *Minerfp;
//int8_t Minerids[KOMODO_MAXBLOCKS]; // 5 million blocks
#define KOMODO_ELECTION_GAP ((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100)
int32_t KMDHEIGHT = 43000;
#define KOMODO_ELECTION_GAP 2000 //((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100)
struct pax_transaction *PAX;
int32_t NUM_PRICES; uint32_t *PVALS;
struct knotaries_entry *Pubkeys;
//struct nutxo_entry *NUTXOS; int32_t Num_nutxos;
struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS;
struct komodo_state KOMODO_STATES[33];
struct komodo_state KOMODO_STATES[34];
int COINBASE_MATURITY = 100;
int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND;
int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES;
std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES;
uint8_t NOTARY_PUBKEY33[33];
int32_t ASSETCHAINS_SHORTFLAG;
char ASSETCHAINS_SYMBOL[16];
uint16_t ASSETCHAINS_PORT;
uint32_t ASSETCHAIN_INIT;
uint32_t ASSETCHAINS_MAGIC = 2387029918;
uint64_t ASSETCHAINS_SUPPLY = 10;
uint32_t KOMODO_INITDONE,KOMODO_REALTIME;
uint32_t KOMODO_INITDONE;
char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771;
uint64_t PENDING_KOMODO_TX;

View File

@@ -52,46 +52,15 @@ const char *Notaries_genesis[][2] =
{ "titomane_SH", "035f49d7a308dd9a209e894321f010d21b7793461b0c89d6d9231a3fe5f68d9960" },
};
/*void komodo_nutxoadd(int32_t height,int32_t notaryid,uint256 txhash,uint64_t voutmask,int32_t numvouts)
{
struct nutxo_entry *np;
if ( numvouts > 1 && notaryid < 64 )
{
pthread_mutex_lock(&komodo_mutex);
np = (struct nutxo_entry *)calloc(1,sizeof(*np));
np->height = height;
np->txhash = txhash;
np->voutmask = voutmask;
np->notaryid = notaryid;
HASH_ADD_KEYPTR(hh,NUTXOS,&np->txhash,sizeof(np->txhash),np);
//printf("Add NUTXO[%d] <- %s notaryid.%d t%u %s %llx\n",Num_nutxos,Notaries[notaryid][0],notaryid,komodo_txtime(txhash),txhash.ToString().c_str(),(long long)voutmask);
Num_nutxos++;
pthread_mutex_unlock(&komodo_mutex);
}
}
int32_t komodo_nutxofind(int32_t height,uint256 txhash,int32_t vout)
{
struct nutxo_entry *np;
pthread_mutex_lock(&komodo_mutex);
HASH_FIND(hh,NUTXOS,&txhash,sizeof(txhash),np);
pthread_mutex_unlock(&komodo_mutex);
if ( np != 0 && ((1LL << vout) & np->voutmask) != 0 )
return(np->notaryid);
return(-1);
}*/
int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask)
{
int32_t htind,numnotaries,i,wt = 0;
if ( ASSETCHAINS_SYMBOL[0] != 0 )
return(2);
htind = height / KOMODO_ELECTION_GAP;
numnotaries = Pubkeys[htind].numnotaries;
for (i=0; i<numnotaries; i++)
if ( ((1LL << i) & signedmask) != 0 )
wt++;
if ( wt > (numnotaries >> 1) || (wt > 7 && (signedmask & 3) != 0) )
if ( wt > (numnotaries >> 1) || (wt > 7 && (signedmask & 1) != 0) )
return(1);
else return(0);
}
@@ -139,7 +108,7 @@ void komodo_notarysinit(int32_t origheight,uint8_t pubkeys[64][33],int32_t num)
memcpy(kp->pubkey,pubkeys[k],33);
kp->notaryid = k;
HASH_ADD_KEYPTR(hh,N.Notaries,kp->pubkey,33,kp);
//if ( height > 0 )
if ( height > 10000 )
{
for (i=0; i<33; i++)
printf("%02x",pubkeys[k][i]);
@@ -199,8 +168,8 @@ void komodo_notarized_update(struct komodo_state *sp,int32_t nHeight,int32_t not
printf("komodo_notarized_update REJECT notarized_height %d > %d nHeight\n",notarized_height,nHeight);
return;
}
NPOINTS = (struct notarized_checkpoint *)realloc(NPOINTS,(NUM_NPOINTS+1) * sizeof(*NPOINTS));
np = &NPOINTS[NUM_NPOINTS++];
sp->NPOINTS = (struct notarized_checkpoint *)realloc(sp->NPOINTS,(sp->NUM_NPOINTS+1) * sizeof(*sp->NPOINTS));
np = &sp->NPOINTS[sp->NUM_NPOINTS++];
memset(np,0,sizeof(*np));
np->nHeight = nHeight;
sp->NOTARIZED_HEIGHT = np->notarized_height = notarized_height;
@@ -208,7 +177,7 @@ void komodo_notarized_update(struct komodo_state *sp,int32_t nHeight,int32_t not
sp->NOTARIZED_DESTTXID = np->notarized_desttxid = notarized_desttxid;
}
struct komodo_state *komodo_stateptr(char *symbol,char *dest);
//struct komodo_state *komodo_stateptr(char *symbol,char *dest);
int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp)
{
char symbol[16],dest[16]; struct komodo_state *sp;
@@ -228,21 +197,24 @@ int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp)
int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp)
{
struct notarized_checkpoint *np = 0; int32_t i;
if ( NUM_NPOINTS > 0 )
struct notarized_checkpoint *np = 0; int32_t i; char symbol[16],dest[16]; struct komodo_state *sp;
if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
{
for (i=0; i<NUM_NPOINTS; i++)
if ( sp->NUM_NPOINTS > 0 )
{
if ( NPOINTS[i].nHeight >= nHeight )
break;
np = &NPOINTS[i];
for (i=0; i<sp->NUM_NPOINTS; i++)
{
if ( sp->NPOINTS[i].nHeight >= nHeight )
break;
np = &sp->NPOINTS[i];
}
}
if ( np != 0 )
{
*notarized_hashp = np->notarized_hash;
*notarized_desttxidp = np->notarized_desttxid;
return(np->notarized_height);
}
}
if ( np != 0 )
{
*notarized_hashp = np->notarized_hash;
*notarized_desttxidp = np->notarized_desttxid;
return(np->notarized_height);
}
memset(notarized_hashp,0,sizeof(*notarized_hashp));
return(0);
@@ -271,7 +243,7 @@ void komodo_init(int32_t height)
// Minerids[i] = -2;
didinit = 1;
}
komodo_stateupdate(0,0,0,0,zero,0,0,0,0,0,0,0,0,0);
komodo_stateupdate(0,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
}
void komodo_assetchain_pubkeys(char *jsonstr)
@@ -299,6 +271,7 @@ void komodo_assetchain_pubkeys(char *jsonstr)
{
komodo_init(-1);
komodo_notarysinit(0,pubkeys,n);
KOMODO_EXTERNAL_NOTARIES = 1;
//printf("initialize pubkeys[%d]\n",n);
} else fprintf(stderr,"komodo_assetchain_pubkeys i.%d vs n.%d\n",i,n);
} else fprintf(stderr,"assetchain pubkeys n.%d\n",n);

View File

@@ -454,7 +454,7 @@ void komodo_paxpricefeed(int32_t height,uint8_t *pricefeed,int32_t opretlen)
double KMDBTC,BTCUSD,CNYUSD; uint32_t numpvals,timestamp,pvals[128]; uint256 zero;
numpvals = dpow_readprices(pricefeed,&timestamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals);
memset(&zero,0,sizeof(zero));
komodo_stateupdate(height,0,0,0,zero,0,0,pvals,numpvals,0,0,0,0,0);
komodo_stateupdate(height,0,0,0,zero,0,0,pvals,numpvals,0,0,0,0,0,0);
//printf("komodo_paxpricefeed vout OP_RETURN.%d prices numpvals.%d opretlen.%d\n",height,numpvals,opretlen);
}
@@ -469,7 +469,7 @@ uint64_t PAX_fiatdest(uint64_t *seedp,int32_t tokomodo,char *destaddr,uint8_t pu
if ( fiatoshis < 0 )
shortflag = 1, fiatoshis = -fiatoshis;
komodoshis = komodo_paxprice(seedp,height,base,(char *)"KMD",(uint64_t)fiatoshis);
//printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f\n",height,base,(double)fiatoshis/COIN,(double)komodoshis/COIN);
printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f\n",height,base,(double)fiatoshis/COIN,(double)komodoshis/COIN);
if ( bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr) == 20 )
{
PAX_pubkey(1,pubkey33,&addrtype,rmd160,base,&shortflag,tokomodo != 0 ? &komodoshis : &fiatoshis);

View File

@@ -16,16 +16,48 @@
#include "uthash.h"
#include "utlist.h"
#ifdef WIN32
#define PACKED
#else
#define PACKED __attribute__((packed))
#endif
#define GENESIS_NBITS 0x1f00ffff
#define KOMODO_MINRATIFY 7
#define KOMODO_MAXBLOCKS 5000000
#define KOMODO_EVENT_RATIFY 'P'
#define KOMODO_EVENT_NOTARIZED 'N'
#define KOMODO_EVENT_KMDHEIGHT 'K'
#define KOMODO_EVENT_REWIND 'B'
#define KOMODO_EVENT_PRICEFEED 'V'
#define KOMODO_EVENT_OPRETURN 'R'
#define KOMODO_OPRETURN_DEPOSIT 'D'
#define KOMODO_OPRETURN_ISSUED 'I' // assetchain
#define KOMODO_OPRETURN_WITHDRAW 'W' // assetchain
#define KOMODO_OPRETURN_REDEEMED 'X'
struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; };
struct komodo_event_pubkeys { uint8_t num; uint8_t pubkeys[64][33]; };
struct komodo_event_opreturn { uint256 txid; uint64_t value; uint16_t vout,oplen; uint8_t opret[]; };
struct komodo_event_pricefeed { uint8_t num; uint32_t prices[35]; };
struct komodo_event
{
struct komodo_event *related;
uint16_t len;
int32_t height;
uint8_t type,reorged;
char symbol[16];
uint8_t space[];
} PACKED;
struct pax_transaction
{
UT_hash_handle hh;
uint256 txid;
uint64_t komodoshis,fiatoshis;
int32_t marked,height,otherheight;
int32_t marked,height,otherheight,approved;
uint16_t vout;
char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
};
@@ -38,6 +70,9 @@ struct notarized_checkpoint { uint256 notarized_hash,notarized_desttxid; int32_t
struct komodo_state
{
uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
int32_t CURRENT_HEIGHT,NOTARIZED_HEIGHT,rewinding;
// gateway state
int32_t SAVEDHEIGHT,CURRENT_HEIGHT,NOTARIZED_HEIGHT;
uint32_t SAVEDTIMESTAMP;
struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS;
struct komodo_event **Komodo_events; int32_t Komodo_numevents;
uint32_t RTbufs[64][3]; uint64_t RTmask;
};

View File

@@ -1261,6 +1261,43 @@ void komodo_userpass(char *username,char *password,FILE *fp)
free(rpcpassword);
}
void komodo_statefname(char *fname,char *symbol,char *str)
{
int32_t n,len;
sprintf(fname,"%s",GetDataDir(false).string().c_str());
if ( (n= (int32_t)strlen(ASSETCHAINS_SYMBOL)) != 0 )
{
len = (int32_t)strlen(fname);
if ( strcmp(ASSETCHAINS_SYMBOL,&fname[len - n]) == 0 )
fname[len - n] = 0;
else
{
printf("unexpected fname.(%s) vs %s [%s] n.%d len.%d (%s)\n",fname,symbol,ASSETCHAINS_SYMBOL,n,len,&fname[len - n]);
return;
}
}
else
{
#ifdef WIN32
strcat(fname,"\\");
#else
strcat(fname,"/");
#endif
}
if ( symbol != 0 && symbol[0] != 0 && strcmp("KMD",symbol) != 0 )
{
strcat(fname,symbol);
//printf("statefname.(%s) -> (%s)\n",symbol,fname);
#ifdef WIN32
strcat(fname,"\\");
#else
strcat(fname,"/");
#endif
}
strcat(fname,str);
//printf("test.(%s) -> [%s] statename.(%s) %s\n",test,ASSETCHAINS_SYMBOL,symbol,fname);
}
void komodo_configfile(char *symbol,uint16_t port)
{
static char myusername[512],mypassword[8192];
@@ -1318,16 +1355,36 @@ void komodo_configfile(char *symbol,uint16_t port)
} else printf("couldnt open.(%s)\n",fname);
}
int32_t komodo_userpass(char *userpass,char *symbol)
{
FILE *fp; char fname[512],username[512],password[512],confname[16];
userpass[0] = 0;
if ( strcmp("KMD",symbol) == 0 )
sprintf(confname,"komodo.conf");
else sprintf(confname,"%s.conf",symbol);
komodo_statefname(fname,symbol,confname);
if ( (fp= fopen(fname,"rb")) != 0 )
{
komodo_userpass(username,password,fp);
sprintf(userpass,"%s:%s",username,password);
fclose(fp);
return((int32_t)strlen(userpass));
}
return(-1);
}
uint32_t komodo_assetmagic(char *symbol,uint64_t supply)
{
uint8_t buf[512]; int32_t len = 0;
if ( strcmp(symbol,"KMD") == 0 )
return(0x8de4eef9);
len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply);
strcpy((char *)&buf[len],symbol);
len += strlen(symbol);
return(calc_crc32(0,buf,len));
}
int32_t komodo_shortflag(char *symbol)
/*int32_t komodo_shortflag(char *symbol)
{
int32_t i,shortflag = 0;
if ( symbol[0] == '-' )
@@ -1338,26 +1395,32 @@ int32_t komodo_shortflag(char *symbol)
symbol[i] = 0;
}
return(shortflag);
}*/
uint16_t komodo_assetport(uint32_t magic)
{
if ( magic == 0x8de4eef9 )
return(7770);
else return(8000 + (magic % 7777));
}
uint16_t komodo_assetport(uint32_t magic,int32_t shortflag)
{
return(8000 + shortflag*7777 + (magic % 7777));
}
uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp,int32_t *shortflagp)
uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp)
{
if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 )
{
*magicp = 0x8de4eef9;
return(7770);
}
*magicp = komodo_assetmagic(symbol,supply);
*shortflagp = komodo_shortflag(symbol);
return(komodo_assetport(*magicp,*shortflagp));
return(komodo_assetport(*magicp));
}
void komodo_ports(uint16_t ports[MAX_CURRENCIES])
{
int32_t i,shortflag; uint32_t magic;
int32_t i; uint32_t magic;
for (i=0; i<MAX_CURRENCIES; i++)
{
ports[i] = komodo_port(CURRENCIES[i],10,&magic,&shortflag);
ports[i] = komodo_port(CURRENCIES[i],10,&magic);
printf("%u ",ports[i]);
}
printf("ports\n");
@@ -1381,8 +1444,7 @@ void komodo_args()
if ( strlen(addn.c_str()) > 0 )
ASSETCHAINS_SEED = 1;
strncpy(ASSETCHAINS_SYMBOL,name.c_str(),sizeof(ASSETCHAINS_SYMBOL)-1);
ASSETCHAINS_PORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC,&ASSETCHAINS_SHORTFLAG);
//fprintf(stderr,"after args: %c%s port.%u magic.%08x supply.%u\n",ASSETCHAINS_SHORTFLAG!=0?'-':'+',ASSETCHAINS_SYMBOL,ASSETCHAINS_PORT,ASSETCHAINS_MAGIC,(int32_t)ASSETCHAINS_SUPPLY);
ASSETCHAINS_PORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC);
while ( (dirname= (char *)GetDataDir(false).string().c_str()) == 0 || dirname[0] == 0 )
{
fprintf(stderr,"waiting for datadir\n");
@@ -1395,7 +1457,7 @@ void komodo_args()
extern int COINBASE_MATURITY;
komodo_configfile(ASSETCHAINS_SYMBOL,ASSETCHAINS_PORT + 1);
//if ( komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 )
COINBASE_MATURITY = 1;
COINBASE_MATURITY = 1;
}
ASSETCHAINS_NOTARIES = GetArg("-ac_notaries","");
komodo_assetchain_pubkeys((char *)ASSETCHAINS_NOTARIES.c_str());
@@ -1419,26 +1481,36 @@ void komodo_args()
//fprintf(stderr,"%s chain params initialized\n",ASSETCHAINS_SYMBOL);
}
struct komodo_state *komodo_stateptr(char *symbol,char *dest)
void komodo_nameset(char *symbol,char *dest,char *source)
{
int32_t baseid; struct komodo_state *sp;
if ( ASSETCHAINS_SYMBOL[0] == 0 )
if ( source[0] == 0 )
{
strcpy(symbol,"KMD");
strcpy(dest,"BTC");
sp = &KOMODO_STATES[0];
strcpy(symbol,(char *)"KMD");
strcpy(dest,(char *)"BTC");
}
else
{
strcpy(symbol,ASSETCHAINS_SYMBOL);
strcpy(dest,"KMD");
if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 )
sp = &KOMODO_STATES[baseid];
else
{
fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL);
return(0);
}
strcpy(symbol,source);
strcpy(dest,(char *)"KMD");
}
return(sp);
}
struct komodo_state *komodo_stateptrget(char *base)
{
int32_t baseid;
if ( base == 0 || base[0] == 0 || strcmp(base,(char *)"KMD") == 0 )
return(&KOMODO_STATES[33]);
else if ( (baseid= komodo_baseid(base)) >= 0 )
return(&KOMODO_STATES[baseid+1]);
else return(&KOMODO_STATES[0]);
}
struct komodo_state *komodo_stateptr(char *symbol,char *dest)
{
int32_t baseid;
komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL);
return(komodo_stateptrget(symbol));
}

View File

@@ -538,7 +538,7 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (chain.Contains(pindex))
if (pindex != 0 && chain.Contains(pindex))
return pindex;
}
}
@@ -1179,7 +1179,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
view.GetBestBlock();
nValueIn = view.GetValueIn(chainActive.Tip()->nHeight,&interest,tx,chainActive.Tip()->nTime);
if ( interest != 0 )
if ( 0 && interest != 0 )
fprintf(stderr,"add interest %.8f\n",(double)interest/COIN);
// we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
view.SetBackend(dummy);

View File

@@ -99,8 +99,8 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams,
}
#define ASSETCHAINS_MINHEIGHT 100
#define ROUNDROBIN_DELAY 45
extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,ASSETCHAINS_SHORTFLAG,KOMODO_REALTIME;
#define ROUNDROBIN_DELAY 61
extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE;
extern char ASSETCHAINS_SYMBOL[16];
extern std::string NOTARY_PUBKEY;
extern uint8_t NOTARY_PUBKEY33[33];
@@ -110,11 +110,12 @@ int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]);
int32_t komodo_pax_opreturn(uint8_t *opret,int32_t maxsize);
uint64_t komodo_paxtotal();
int32_t komodo_is_issuer();
int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol,int32_t tokomodo);
int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *symbol,int32_t tokomodo);
int32_t komodo_isrealtime(int32_t *kmdheightp);
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
{
uint64_t deposits; const CChainParams& chainparams = Params();
uint64_t deposits; int32_t isrealtime,kmdheight; const CChainParams& chainparams = Params();
// Create new block
unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
if(!pblocktemplate.get())
@@ -122,13 +123,14 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
if ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight >= 100 )
{
fprintf(stderr,"start CreateNewBlock %s initdone.%d deposit %.8f mempool.%d RT.%u KOMODO_ON_DEMAND.%d\n",ASSETCHAINS_SYMBOL,KOMODO_INITDONE,(double)komodo_paxtotal()/COIN,(int32_t)mempool.GetTotalTxSize(),KOMODO_REALTIME,KOMODO_ON_DEMAND);
while ( KOMODO_ON_DEMAND == 0 )//mempool.GetTotalTxSize() <= 0 )
isrealtime = komodo_isrealtime(&kmdheight);
fprintf(stderr,"start CreateNewBlock %s initdone.%d deposit %.8f mempool.%d RT.%u KOMODO_ON_DEMAND.%d\n",ASSETCHAINS_SYMBOL,KOMODO_INITDONE,(double)komodo_paxtotal()/COIN,(int32_t)mempool.GetTotalTxSize(),isrealtime,KOMODO_ON_DEMAND);
while ( KOMODO_ON_DEMAND == 0 )
{
deposits = komodo_paxtotal();
if ( KOMODO_INITDONE == 0 || KOMODO_REALTIME == 0 )
if ( KOMODO_INITDONE == 0 || (isrealtime= komodo_isrealtime(&kmdheight)) == 0 )
{
//fprintf(stderr,"INITDONE.%d RT.%d deposits %.8f\n",KOMODO_INITDONE,KOMODO_REALTIME,(double)deposits/COIN);
//fprintf(stderr,"INITDONE.%d RT.%d deposits %.8f ht.%d\n",KOMODO_INITDONE,isrealtime,(double)deposits/COIN,kmdheight);
}
else if ( deposits != 0 )
break;
@@ -374,7 +376,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
if ( ASSETCHAINS_SYMBOL[0] == 0 )
{
int32_t i,opretlen; uint8_t opret[256],*ptr;
if ( komodo_gateway_deposits(&txNew,0,(char *)"EUR",1) == 0 )
if ( komodo_gateway_deposits(&txNew,(char *)"KMD",1) == 0 )
{
if ( (opretlen= komodo_pax_opreturn(opret,sizeof(opret))) > 0 )
{
@@ -390,8 +392,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
}
else if ( komodo_is_issuer() != 0 )
{
komodo_gateway_deposits(&txNew,0,ASSETCHAINS_SYMBOL,0);
fprintf(stderr,"txNew numvouts.%d\n",(int32_t)txNew.vout.size());
komodo_gateway_deposits(&txNew,ASSETCHAINS_SYMBOL,0);
fprintf(stderr,"%s txNew numvouts.%d\n",ASSETCHAINS_SYMBOL,(int32_t)txNew.vout.size());
}
pblock->vtx[0] = txNew;
pblocktemplate->vTxFees[0] = -nFees;
@@ -602,7 +604,8 @@ void static BitcoinMiner(CWallet *pwallet)
} else Mining_start = 0;
while (true)
{
//fprintf(stderr,"%s start mining loop\n",ASSETCHAINS_SYMBOL);
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"%s start mining loop\n",ASSETCHAINS_SYMBOL);
// Hash state
KOMODO_CHOSEN_ONE = 0;
crypto_generichash_blake2b_state state;
@@ -629,13 +632,13 @@ void static BitcoinMiner(CWallet *pwallet)
solutionTargetChecks.increment();
if ( UintToArith256(pblock->GetHash()) > hashTarget )
{
//if ( ASSETCHAINS_SYMBOL[0] != 0 )
// printf("missed target\n");
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"missed target\n");
return false;
}
if ( ASSETCHAINS_SYMBOL[0] == 0 && Mining_start != 0 && time(NULL) < Mining_start+ROUNDROBIN_DELAY )
{
printf("Round robin diff sleep %d\n",(int32_t)(Mining_start+ROUNDROBIN_DELAY-time(NULL)));
//printf("Round robin diff sleep %d\n",(int32_t)(Mining_start+ROUNDROBIN_DELAY-time(NULL)));
sleep(Mining_start+ROUNDROBIN_DELAY-time(NULL));
KOMODO_CHOSEN_ONE = 1;
}
@@ -721,24 +724,27 @@ void static BitcoinMiner(CWallet *pwallet)
if (vNodes.empty() && chainparams.MiningRequiresPeers())
{
if ( ASSETCHAINS_SYMBOL[0] == 0 || Mining_height >= 100 )
{
//fprintf(stderr,"no nodes, break\n");
break;
}
}
if ((UintToArith256(pblock->nNonce) & 0xffff) == 0xffff)
{
if ( ASSETCHAINS_SYMBOL[0] != 0 )
printf("0xffff, break\n");
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"0xffff, break\n");
break;
}
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
{
//if ( ASSETCHAINS_SYMBOL[0] != 0 )
// printf("timeout, break\n");
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"timeout, break\n");
break;
}
if ( pindexPrev != chainActive.Tip() )
{
//if ( ASSETCHAINS_SYMBOL[0] != 0 )
// printf("Tip advanced, break\n");
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"Tip advanced, break\n");
break;
}
// Update nNonce and nTime

View File

@@ -40,12 +40,13 @@ using namespace std;
* Or alternatively, create a specific query method for the information.
**/
uint64_t komodo_interestsum();
int32_t komodo_longestchain();
int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp);
Value getinfo(const Array& params, bool fHelp)
{
uint256 notarized_hash,notarized_desttxid;
int32_t notarized_height;
int32_t notarized_height,longestchain;
if (fHelp || params.size() != 0)
throw runtime_error(
"getinfo\n"
@@ -98,6 +99,9 @@ Value getinfo(const Array& params, bool fHelp)
}
#endif
obj.push_back(Pair("blocks", (int)chainActive.Height()));
if ( (longestchain= komodo_longestchain()) != 0 && chainActive.Height() > longestchain )
longestchain = chainActive.Height();
obj.push_back(Pair("longestchain", longestchain));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
if ( chainActive.Tip() != 0 )
obj.push_back(Pair("tiptime", (int)chainActive.Tip()->nTime));

View File

@@ -165,6 +165,36 @@ Value getpeerinfo(const Array& params, bool fHelp)
return ret;
}
int32_t komodo_longestchain()
{
int32_t ht,n=0,num=0,maxheight=0,height = 0;
LOCK(cs_main);
vector<CNodeStats> vstats;
CopyNodeStats(vstats);
BOOST_FOREACH(const CNodeStats& stats, vstats)
{
CNodeStateStats statestats;
bool fStateStats = GetNodeStateStats(stats.nodeid,statestats);
ht = 0;
if ( stats.nStartingHeight > ht )
ht = stats.nStartingHeight;
if ( statestats.nSyncHeight > ht )
ht = statestats.nSyncHeight;
if ( statestats.nCommonHeight > ht )
ht = statestats.nCommonHeight;
if ( maxheight == 0 || ht > maxheight*1.01 )
maxheight = ht, num = 1;
else if ( ht > maxheight*0.99 )
num++;
n++;
if ( ht > height )
height = ht;
}
if ( num > (n >> 1) )
return(height);
else return(0);
}
Value addnode(const Array& params, bool fHelp)
{
string strCommand;

View File

@@ -132,7 +132,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
{
extern char ASSETCHAINS_SYMBOL[16];
interest = komodo_interest(pindex->nHeight,txout.nValue,tx.nLockTime,tipindex->nTime);
if ( strcmp("REVS",ASSETCHAINS_SYMBOL) == 0 )
if ( 0 && strcmp("REVS",ASSETCHAINS_SYMBOL) == 0 )
fprintf(stderr,"TxtoJSON interest %llu %.8f (%d %llu %u %u)\n",(long long)interest,(double)interest/COIN,(int32_t)pindex->nHeight,(long long)txout.nValue,(uint32_t)tx.nLockTime,(int32_t)tipindex->nTime);
out.push_back(Pair("interest", ValueFromAmount(interest)));
}

View File

@@ -401,18 +401,14 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
}
extern char ASSETCHAINS_SYMBOL[16];
extern int32_t ASSETCHAINS_SHORTFLAG;
boost::filesystem::path GetDefaultDataDir()
{
namespace fs = boost::filesystem;
char symbol[16];
if ( ASSETCHAINS_SYMBOL[0] != 0 )
{
if ( ASSETCHAINS_SHORTFLAG != 0 )
sprintf(symbol,"-%s",ASSETCHAINS_SYMBOL);
else strcpy(symbol,ASSETCHAINS_SYMBOL);
} else symbol[0] = 0;
strcpy(symbol,ASSETCHAINS_SYMBOL);
else symbol[0] = 0;
// Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
// Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
// Mac: ~/Library/Application Support/Zcash

View File

@@ -481,6 +481,7 @@ int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_
extern char ASSETCHAINS_SYMBOL[16];
int32_t komodo_is_issuer();
int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp);
int32_t komodo_isrealtime(int32_t *kmdheightp);
Value paxdeposit(const Array& params, bool fHelp)
{
@@ -491,7 +492,7 @@ Value paxdeposit(const Array& params, bool fHelp)
if (!EnsureWalletIsAvailable(fHelp))
return Value::null;
if (fHelp || params.size() != 3)
throw runtime_error("paxdeposit \"address\" [-]fiatoshis \"base\"\nnegative fiatoshis means a short position, long position capped at 100% gain");
throw runtime_error("paxdeposit \"address\" fiatoshis base");
LOCK2(cs_main, pwalletMain->cs_wallet);
CBitcoinAddress address(params[0].get_str());
if (!address.IsValid())
@@ -521,7 +522,6 @@ Value paxdeposit(const Array& params, bool fHelp)
Value paxwithdraw(const Array& params, bool fHelp)
{
extern int32_t KMDHEIGHT,KOMODO_REALTIME;
CWalletTx wtx; std::string dest; int32_t kmdheight; uint64_t seed,komodoshis = 0; char destaddr[64]; uint8_t i,pubkey37[37]; bool fSubtractFeeFromAmount = false;
if ( ASSETCHAINS_SYMBOL[0] == 0 )
return(0);
@@ -529,14 +529,13 @@ Value paxwithdraw(const Array& params, bool fHelp)
return 0;
if (fHelp || params.size() != 2)
throw runtime_error("paxwithdraw \"address\" fiatamount");
if ( KOMODO_REALTIME == 0 )
if ( komodo_isrealtime(&kmdheight) == 0 )
return(0);
LOCK2(cs_main, pwalletMain->cs_wallet);
CBitcoinAddress address(params[0].get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
int64_t fiatoshis = atof(params[1].get_str().c_str()) * COIN;
kmdheight = KMDHEIGHT;
komodoshis = PAX_fiatdest(&seed,1,destaddr,pubkey37,(char *)params[0].get_str().c_str(),kmdheight,ASSETCHAINS_SYMBOL,fiatoshis);
dest.append(destaddr);
CBitcoinAddress destaddress(CRYPTO777_KMDADDR);