Browse Source

test

metaverse
jl777 8 years ago
parent
commit
ab91876752
  1. 9
      src/bitcoind.cpp
  2. 60
      src/komodo.h
  3. 105
      src/komodo_events.h
  4. 86
      src/komodo_gateway.h
  5. 9
      src/komodo_globals.h
  6. 2
      src/komodo_notary.h
  7. 36
      src/komodo_structs.h
  8. 85
      src/komodo_utils.h
  9. 13
      src/miner.cpp
  10. 8
      src/util.cpp
  11. 4
      src/wallet/rpcwallet.cpp

9
src/bitcoind.cpp

@ -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)

60
src/komodo.h

@ -41,21 +41,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;
@ -177,25 +172,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);
if ( (fp= fopen(fname,"rb+")) != 0 )
{
while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
@ -212,7 +189,7 @@ 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) )
@ -368,6 +345,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr
sp->NOTARIZED_HASH = kmdtxid;
sp->NOTARIZED_DESTTXID = desttxid;
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
// extract X opreturns here
} 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]);
}
else if ( i == 0 && j == 1 && opretlen == 149 )
@ -407,7 +385,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,9 +406,12 @@ 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;
uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
int32_t i,j,k,numnotaries,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
memset(zero.bytes,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 )
@ -438,20 +419,19 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
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();
komodo_event_rewind(sp,symbol,pindex->nHeight);
komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,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);
}*/
sp->KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
if ( pindex != 0 )
{
height = pindex->nHeight;
@ -481,10 +461,6 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
}
}
}
if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 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++)
@ -494,10 +470,6 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
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);
@ -537,6 +509,8 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
}
}
}
if ( pindex->nHeight == hwmheight && (hwmheight % 100) == 0 )
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,0,0,0,0);
} else printf("komodo_connectblock: unexpected null pindex\n");
KOMODO_INITDONE = (uint32_t)time(NULL);
}

105
src/komodo_events.h

@ -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(*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,7 +51,7 @@ 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);
}
@ -112,18 +74,11 @@ void komodo_eventadd_pricefeed(struct komodo_state *sp,char *symbol,int32_t heig
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 +89,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 +116,46 @@ 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--;
komodo_event_undo(sp,ep);
sp->Komodo_numevents--;
}
}
}
}
void komodo_setkmdheight(struct komodo_state *sp,int32_t kmdheight)
{
if ( kmdheight > sp->SAVEDHEIGHT )
sp->SAVEDHEIGHT = kmdheight;
}
void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight)
{
if ( kmdheight > 0 )
{
komodo_eventadd(sp,height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)&kmdheight,sizeof(kmdheight));
if ( sp != 0 )
komodo_setkmdheight(sp,kmdheight);
}
else
{
kmdheight = -kmdheight;
komodo_eventadd(so,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;

86
src/komodo_gateway.h

@ -110,12 +110,9 @@ 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,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;
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
@ -143,7 +140,7 @@ int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uin
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);
printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d\n",symbol,dstr(fiatoshis),height,otherheight);
}
}
}
@ -223,7 +220,7 @@ 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; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space;
n = block.vtx[0].vout.size();
script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
if ( n <= 2 || script[0] != 0x6a )
@ -244,7 +241,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,&script[offset],opretlen,opcode == 'X')) > 0 )
{
for (i=1; i<n-1; i++)
{
@ -324,33 +321,20 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
diff = -diff;
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 || (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,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight,height);
} else printf("duplicate deposit\n");
}
}
}
@ -360,7 +344,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
{
if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
{
if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 )
{
for (i=0; i<n; i++)
{
@ -385,6 +369,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
return(typestr);
}
#ifdef pollmethod
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)
{
int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
@ -519,7 +504,7 @@ void komodo_gateway_iteration(char *symbol)
usleep(10000);
}
if ( KMDHEIGHT >= kmdheight )
KOMODO_REALTIME = (uint32_t)time(NULL);
sp->KOMODO_REALTIME = (uint32_t)time(NULL);
}
free_json(infoobj);
}
@ -570,3 +555,38 @@ void komodo_iteration(char *symbol)
}
}
}
#else
void komodo_gateway_iteration()
{
static long lastpos[33];
FILE *fp; int32_t baseid,refid; struct komodo_state *sp; char fname[512],*base,dest[16];
if ( ASSETCHAINS_SYMBOL[0] == 0 )
refid = 32;
else refid = komodo_baseid(ASSETCHAINS_SYMBOL);
if ( refid >= 0 )
{
for (baseid=0; baseid<=32; baseid++)
{
if ( baseid != refid ) // use direct data for yourself
{
komodo_statefname(fname,"");
komodo_nameset(symbol,dest,source);
if ( (fp= fopen(fname,"rb")) != 0 )
{
fseek(fp,lastpos,SEEK_SET);
if ( ftell(fp) > lastpos[baseid] )
{
while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
;
lastpos[baseid] = ftell(fp);
printf("%s lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
}
fclose(fp);
}
}
}
}
}
#endif

9
src/komodo_globals.h

@ -20,19 +20,15 @@ char *komodo_issuemethod(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,char *target);
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;
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];
@ -43,13 +39,12 @@ int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,
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;

2
src/komodo_notary.h

@ -208,7 +208,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;

36
src/komodo_structs.h

@ -16,10 +16,42 @@
#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;
@ -38,6 +70,6 @@ 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,rewinding;
struct komodo_event **Komodo_events; int32_t Komodo_numevents;
};

85
src/komodo_utils.h

@ -1261,6 +1261,36 @@ void komodo_userpass(char *username,char *password,FILE *fp)
free(rpcpassword);
}
void komodo_statefname(char *fname,char *symbol)
{
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;
if ( symbol[0] != 0 )
{
strcpy(&fname[len - n],symbol);
#ifdef WIN32
strcat(fname,"\\");
#else
strcat(fname,"//");
#endif
}
}
else
{
printf("unexpected fname.(%s) vs %s\n",fname,ASSETCHAINS_SYMBOL);
return;
}
}
strcat(fname,(char *)"komodostate");
printf("statename.(%s) %s\n",symbol,fname);
}
void komodo_configfile(char *symbol,uint16_t port)
{
static char myusername[512],mypassword[8192];
@ -1327,7 +1357,7 @@ uint32_t komodo_assetmagic(char *symbol,uint64_t supply)
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,17 +1368,16 @@ int32_t komodo_shortflag(char *symbol)
symbol[i] = 0;
}
return(shortflag);
}
}*/
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)
{
*magicp = komodo_assetmagic(symbol,supply);
*shortflagp = komodo_shortflag(symbol);
return(komodo_assetport(*magicp,*shortflagp));
}
@ -1381,8 +1410,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 +1423,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,21 +1447,30 @@ 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];
}
else
{
strcpy(symbol,ASSETCHAINS_SYMBOL);
strcpy(symbol,source);
strcpy(dest,"KMD");
}
}
struct komodo_state *komodo_stateptr(char *symbol,char *dest)
{
int32_t baseid; struct komodo_state *sp;
komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL);
if ( ASSETCHAINS_SYMBOL[0] == 0 )
sp = &KOMODO_STATES[0];
else
{
if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 )
sp = &KOMODO_STATES[baseid];
sp = &KOMODO_STATES[baseid+1];
else
{
fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL);
@ -1442,3 +1479,25 @@ struct komodo_state *komodo_stateptr(char *symbol,char *dest)
}
return(sp);
}
struct komodo_state *komodo_stateptrget(char *base)
{
int32_t baseid;
if ( base == 0 || base[0] == 0 || strcmp(base,"KMD") == 0 )
return(KOMODO_STATES[0]);
else if ( (baseid= komodo_baseid(base)) >= 0 )
return(&KOMODO_STATES[baseid+1]);
else return(0);
}
int32_t komodo_isrealtime(int32_t *kmdheightp,char *target)
{
char symbol[16],dest[16]; struct komodo_state *sp;
*kmdheightp = 0;
if ( (sp= komodo_stateptrget(target)) != 0 )
{
*kmdheightp = sp->CURRENT_HEIGHT;
return(sp->KOMODO_REALTIME);
}
return(0);
}

13
src/miner.cpp

@ -100,7 +100,7 @@ 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;
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];
@ -114,7 +114,7 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,cha
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 +122,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,ASSETCHAINS_SYMBOL);
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 || komodo_isrealtime(&kmdheight,ASSETCHAINS_SYMBOL) == 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\n",KOMODO_INITDONE,isrealtime,(double)deposits/COIN);
}
else if ( deposits != 0 )
break;

8
src/util.cpp

@ -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

4
src/wallet/rpcwallet.cpp

@ -521,7 +521,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 +528,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,"KMD") == 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);

Loading…
Cancel
Save