diff --git a/src/assetchains b/src/assetchains index 6341963fe..adb4b59d1 100755 --- a/src/assetchains +++ b/src/assetchains @@ -1,6 +1,20 @@ +#!/bin/bash +set -x source pubkey.txt echo $pubkey +./komodod -pubkey=$pubkey -ac_name=REVS -ac_supply=1300000 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=SUPERNET -ac_supply=816061 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=DEX -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=PANGEA -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=JUMBLR -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=BET -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=CRYPTO -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=HODL -ac_supply=9999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=SHARK -ac_supply=1401 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=BOTS -ac_supply=999999 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=MGW -ac_supply=999999 -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 & @@ -33,3 +47,50 @@ echo $pubkey ./komodod -pubkey=$pubkey -ac_name=BGN -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=IDR -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=HRK -addnode=78.47.196.146 $1 & + +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\":\"SUPERNET\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"DEX\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PANGEA\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"JUMBLR\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BET\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CRYPTO\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HODL\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SHARK\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BOTS\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MGW\",\"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\"}" + diff --git a/src/assetfunds b/src/assetfunds new file mode 100755 index 000000000..6aede587c --- /dev/null +++ b/src/assetfunds @@ -0,0 +1,34 @@ +#!/bin/bash +set -x +./komodo-cli paxdeposit $1 0.01 aud +./komodo-cli paxdeposit $1 0.01 bgn +./komodo-cli paxdeposit $1 0.01 cad +./komodo-cli paxdeposit $1 0.01 chf +./komodo-cli paxdeposit $1 0.01 cny +./komodo-cli paxdeposit $1 0.01 czk +./komodo-cli paxdeposit $1 0.01 dkk +./komodo-cli paxdeposit $1 0.01 eur +./komodo-cli paxdeposit $1 0.01 gbp +./komodo-cli paxdeposit $1 0.01 hkd +./komodo-cli paxdeposit $1 0.01 hrk +./komodo-cli paxdeposit $1 0.01 huf +./komodo-cli paxdeposit $1 0.01 idr +./komodo-cli paxdeposit $1 0.01 ils +./komodo-cli paxdeposit $1 0.01 inr +./komodo-cli paxdeposit $1 0.01 jpy +./komodo-cli paxdeposit $1 0.01 krw +./komodo-cli paxdeposit $1 0.01 mxn +./komodo-cli paxdeposit $1 0.01 myr +./komodo-cli paxdeposit $1 0.01 nok +./komodo-cli paxdeposit $1 0.01 nzd +./komodo-cli paxdeposit $1 0.01 php +./komodo-cli paxdeposit $1 0.01 pln +./komodo-cli paxdeposit $1 0.01 brl +./komodo-cli paxdeposit $1 0.01 ron +./komodo-cli paxdeposit $1 0.01 rub +./komodo-cli paxdeposit $1 0.01 sek +./komodo-cli paxdeposit $1 0.01 sgd +./komodo-cli paxdeposit $1 0.01 thb +./komodo-cli paxdeposit $1 0.01 try +./komodo-cli paxdeposit $1 0.01 usd +./komodo-cli paxdeposit $1 0.01 zar diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 53204a8c4..a03dca4ad 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -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) { } diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 1f7267701..590d91463 100644 --- a/src/bitcoind.cpp +++ b/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) @@ -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 diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 991cc1333..b0a0f28a0 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -95,6 +95,8 @@ public: assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")); vFixedSeeds.clear(); vSeeds.clear(); + vSeeds.push_back(CDNSSeedData("komodoplatform.com", "seeds.komodoplatform.com")); // @kolo + vSeeds.push_back(CDNSSeedData("komodo.mewhub.com", "seeds.komodo.mewhub.com")); // @kolo // TODO: set up bootstrapping for mainnet base58Prefixes[PUBKEY_ADDRESS] = std::vector(1,60); base58Prefixes[SCRIPT_ADDRESS] = std::vector(1,85); diff --git a/src/fiat-cli b/src/fiat-cli index 8dd0fc02b..78eadea07 100755 --- a/src/fiat-cli +++ b/src/fiat-cli @@ -1,4 +1,5 @@ #!/bin/bash +#set -x echo aud; fiat/aud $1 $2 $3 $4 echo bgn; fiat/bgn $1 $2 $3 $4 echo cad; fiat/cad $1 $2 $3 $4 @@ -31,3 +32,14 @@ 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 +echo supernet; fiat/supernet $1 $2 $3 $4 +echo dex; fiat/dex $1 $2 $3 $4 +echo pangea; fiat/pangea $1 $2 $3 $4 +echo jumblr; fiat/jumblr $1 $2 $3 $4 +echo bet; fiat/bet $1 $2 $3 $4 +echo crypto; fiat/crypto $1 $2 $3 $4 +echo hodl; fiat/hodl $1 $2 $3 $4 +echo shark; fiat/shark $1 $2 $3 $4 +echo bots; fiat/bots $1 $2 $3 $4 +echo mgw; fiat/mgw $1 $2 $3 $4 diff --git a/src/fiat/_usd b/src/fiat/_usd deleted file mode 100755 index bdba4ea52..000000000 --- a/src/fiat/_usd +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -./komodo-cli -ac_name=-USD $1 $2 $3 $4 diff --git a/src/fiat/bet b/src/fiat/bet new file mode 100755 index 000000000..73208cab5 --- /dev/null +++ b/src/fiat/bet @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=BET $1 $2 $3 $4 diff --git a/src/fiat/bots b/src/fiat/bots new file mode 100755 index 000000000..4fc22f5eb --- /dev/null +++ b/src/fiat/bots @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=BOTS $1 $2 $3 $4 diff --git a/src/fiat/crypto b/src/fiat/crypto new file mode 100755 index 000000000..cb35025fa --- /dev/null +++ b/src/fiat/crypto @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=CRYPTO $1 $2 $3 $4 diff --git a/src/fiat/dex b/src/fiat/dex new file mode 100755 index 000000000..b1ad72e09 --- /dev/null +++ b/src/fiat/dex @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=DEX $1 $2 $3 $4 diff --git a/src/fiat/hodl b/src/fiat/hodl new file mode 100755 index 000000000..192326ada --- /dev/null +++ b/src/fiat/hodl @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=HODL $1 $2 $3 $4 diff --git a/src/fiat/jumblr b/src/fiat/jumblr new file mode 100755 index 000000000..24d332184 --- /dev/null +++ b/src/fiat/jumblr @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=JUMBLR $1 $2 $3 $4 diff --git a/src/fiat/mgw b/src/fiat/mgw new file mode 100755 index 000000000..d498ed85a --- /dev/null +++ b/src/fiat/mgw @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=MGW $1 $2 $3 $4 diff --git a/src/fiat/pangea b/src/fiat/pangea new file mode 100755 index 000000000..de19a2d6d --- /dev/null +++ b/src/fiat/pangea @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=PANGEA $1 $2 $3 $4 diff --git a/src/fiat/revs b/src/fiat/revs new file mode 100755 index 000000000..5c8782ae5 --- /dev/null +++ b/src/fiat/revs @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=REVS $1 $2 $3 $4 diff --git a/src/fiat/shark b/src/fiat/shark new file mode 100755 index 000000000..4c25f6a7e --- /dev/null +++ b/src/fiat/shark @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=SHARK $1 $2 $3 $4 diff --git a/src/fiat/supernet b/src/fiat/supernet new file mode 100755 index 000000000..8afb833ca --- /dev/null +++ b/src/fiat/supernet @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=SUPERNET $1 $2 $3 $4 diff --git a/src/init.cpp b/src/init.cpp index cfd4f56dc..f464df44d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1256,7 +1256,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // first suggest a reindex if (!fReset) { bool fRet = uiInterface.ThreadSafeMessageBox( - strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?"), + strLoadError + ".\n\n" + _("error in HDD data, might just need to update to latest, if that doesnt work, then you need to resync"), "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT); if (fRet) { fReindex = true; diff --git a/src/komodo.h b/src/komodo.h index ed05c1a46..2d978d443 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -17,15 +17,15 @@ #define H_KOMODO_H // Todo: -// 0. optimize assetchains -// 1. error check fiat redeem amounts -// 2. net balance limiter -// 3. verify: reorgs +// verify: reorgs // non komodod (non-hardfork) todo: // a. automate notarization fee payouts // b. automated distribution of test REVS snapshot +//#define KOMODO_ASSETCHAINS_WAITNOTARIZE +#define KOMODO_PAXMAX (10000 * COIN) + #include #include #include @@ -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; @@ -91,8 +86,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 +100,12 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char errs++; if ( fread(¬arized_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); + if ( 0 && sp != 0 ) + printf("%s load[%s.%d] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,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,12 +123,24 @@ 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' ) { - uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000]; + uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384]; if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) errs++; if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) @@ -145,9 +153,20 @@ 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); - } else printf("illegal olen.%u\n",olen); + if ( 0 && matched != 0 ) + { + int32_t i; for (i=0; i global PAX + } else + { + int32_t i; + for (i=0; i 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); } - else printf("illegal func.(%d %c)\n",func,func); + else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func); return(func); } 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 +197,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 +214,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 ) { @@ -290,25 +305,28 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar else if ( height != 0 ) { //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs); - fputc('N',fp); - if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) - errs++; - if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) - errs++; - if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) - errs++; - if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) - errs++; - komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); - //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); + if ( sp != 0 ) + { + fputc('N',fp); + if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) + errs++; + if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) + errs++; + if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) + errs++; + if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) + errs++; + komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); + } } fflush(fp); } } -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]; + static uint256 zero; static FILE *signedfp; + 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 ) return(-1); if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) @@ -354,21 +372,45 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr else if ( opretlen == 0x4d ) { opretlen = scriptbuf[len++]; - opretlen = (opretlen << 8) + scriptbuf[len++]; + opretlen += (scriptbuf[len++] << 8); } if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) { len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); - len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); + len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(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]); 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); + len += 4; + if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) + printf("%s ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s) lens.(%d %d)\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],opretlen,len); + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + { + if ( signedfp == 0 ) + { + char fname[512]; + komodo_statefname(fname,(char *)"",(char *)"signedmasks"); + if ( (signedfp= fopen(fname,"rb+")) == 0 ) + signedfp = fopen(fname,"wb"); + else fseek(signedfp,0,SEEK_END); + } + if ( signedfp != 0 ) + { + fwrite(&height,1,sizeof(height),signedfp); + fwrite(&signedmask,1,sizeof(signedmask),signedfp); + fflush(signedfp); + } + if ( opretlen > len && scriptbuf[len] == 'A' ) + { + printf("Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len); + 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 +418,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr { int32_t k; for (k=0; k= 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,25 +431,25 @@ 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); } -int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) +/*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) { if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) return(1); else return(0); -} +}*/ // Special tx have vout[0] -> CRYPTO777 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify // 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 +470,27 @@ 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); + KOMODO_INITDONE = (uint32_t)time(NULL); + if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) + { + fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL); + 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("%s hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",ASSETCHAINS_SYMBOL,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 ) - { - 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); if ( pindex != 0 ) { height = pindex->nHeight; @@ -461,7 +500,32 @@ 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 = (height < 91400) ? 1 : 0; + numvins = block.vtx[i].vin.size(); + for (j=0; j 0 ) + { + if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) + signedmask |= (1LL << k); + else if ( 0 && numvins >= 17 ) + { + int32_t k; + for (k=0; k= KOMODO_MINRATIFY) || numvalid > (numnotaries/4)) ) + { + 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 0 ) { for (k=0; k= 0 && notaryid < 64 && voutmask != 0 ) + printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d notarized.%d special.%d isratification.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts,notarized,specialtx,isratification); + 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 0 ) + if ( isratification != 0 ) { - if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) - signedmask |= (1LL << k); + 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 ( (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("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 ) { numvalid = 0; @@ -528,17 +576,19 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) } } } - if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) ) + if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) ) { 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); } } } - } else printf("komodo_connectblock: unexpected null pindex\n"); - KOMODO_INITDONE = (uint32_t)time(NULL); + 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 fprintf(stderr,"komodo_connectblock: unexpected null pindex\n"); + //KOMODO_INITDONE = (uint32_t)time(NULL); } diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 8e915a170..d2464889b 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -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,12 +340,70 @@ 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); } +uint256 komodo_getblockhash(int32_t height) +{ + uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32]; + memset(&hash,0,sizeof(hash)); + sprintf(params,"[%d]",height); + if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,7771)) != 0 ) + { + if ( (result= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (hexstr= jstr(result,(char *)"result")) != 0 ) + { + if ( is_hexstr(hexstr,0) == 64 ) + { + decode_hex(revbuf,32,hexstr); + for (i=0; i<32; i++) + ((uint8_t *)&hash)[i] = revbuf[31-i]; + } + } + free_json(result); + } + printf("KMD hash.%d (%s) %x\n",height,jsonstr,*(uint32_t *)&hash); + free(jsonstr); + } + return(hash); +} + +uint256 _komodo_getblockhash(int32_t height); + +uint64_t komodo_seed(int32_t height) +{ + uint64_t seed = 0; + if ( 0 ) // problem during init time, seeds are needed for loading blockindex, so null seeds... + { + uint256 hash,zero; CBlockIndex *pindex; + memset(&hash,0,sizeof(hash)); + memset(&zero,0,sizeof(zero)); + if ( height > 10 ) + height -= 10; + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + hash = _komodo_getblockhash(height); + if ( memcmp(&hash,&zero,sizeof(hash)) == 0 ) + hash = komodo_getblockhash(height); + int32_t i; + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" seed.%d\n",height); + seed = arith_uint256(hash.GetHex()).GetLow64(); + } + else + { + seed = (height << 13) ^ (height << 2); + seed <<= 21; + seed |= (height & 0xffffffff); + seed ^= (seed << 17) ^ (seed << 1); + } + return(seed); +} + uint32_t komodo_txtime(uint256 hash) { CTransaction tx; @@ -362,17 +420,6 @@ uint32_t komodo_txtime(uint256 hash) return(0); } -uint64_t komodo_seed(int32_t height) -{ - uint256 hash; uint64_t seed = 0; CBlockIndex *pindex = chainActive[height]; - if ( pindex != 0 ) - { - hash = pindex->GetBlockHash(); - seed = arith_uint256(hash.GetHex()).GetLow64(); - } - return(seed); -} - void komodo_disconnect(CBlockIndex *pindex,CBlock& block) { char symbol[16],dest[16]; struct komodo_state *sp; @@ -422,7 +469,7 @@ int32_t komodo_block2height(CBlock *block) } //printf(" <- coinbase.%d ht.%d\n",(int32_t)block->vtx[0].vin[0].scriptSig.size(),height); } - komodo_init(height); + //komodo_init(height); return(height); } @@ -434,7 +481,7 @@ void komodo_block2pubkey33(uint8_t *pubkey33,CBlock& block) #else uint8_t *ptr = (uint8_t *)&block.vtx[0].vout[0].scriptPubKey[0]; #endif - komodo_init(0); + //komodo_init(0); n = block.vtx[0].vout[0].scriptPubKey.size(); if ( n == 35 ) memcpy(pubkey33,ptr+1,33); @@ -461,7 +508,7 @@ int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex) void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) { CBlock block; - komodo_init(height); + //komodo_init(height); memset(pubkey33,0,33); if ( pindex != 0 ) { @@ -497,21 +544,41 @@ int8_t komodo_minerid(int32_t height) return(i); } } - //printf("minderid not notary ht.%d\n",height); return(-1); } +int32_t komodo_minerids(uint8_t *minerids,int32_t height) +{ + int32_t i,n=0; + for (i=0; i<1000; i++,n++) + { + if ( height-i <= 0 ) + break; + minerids[i] = komodo_minerid(height - i); + } + return(n); +} + int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]) { - int32_t i,notaryid,minerid; + int32_t i,notaryid,minerid,limit; komodo_chosennotary(¬aryid,height,pubkey33); if ( height >= 34000 && notaryid >= 0 ) { - for (i=1; i<64; i++) + if ( height < 79693 ) + limit = 64; + else if ( height < 82000 ) + limit = 8; + else limit = 66; + for (i=1; inHeight,¬arized_hash,¬arized_desttxid); + int32_t notarized_height; uint256 notarized_hash,notarized_desttxid; CBlockIndex *notary; CBlockIndex *pindex; + if ( (pindex= chainActive.Tip()) == 0 ) + return(-1); + notarized_height = komodo_notarizeddata(pindex->nHeight,¬arized_hash,¬arized_desttxid); *notarized_heightp = notarized_height; - if ( notarized_height >= 0 && notarized_height <= chainActive.Tip()->nHeight && (notary= mapBlockIndex[notarized_hash]) != 0 ) + if ( notarized_height >= 0 && notarized_height <= pindex->nHeight && (notary= mapBlockIndex[notarized_hash]) != 0 ) { - //printf("nHeight.%d -> (%d %s)\n",chainActive.Tip()->nHeight,notarized_height,notarized_hash.ToString().c_str()); + //printf("nHeight.%d -> (%d %s)\n",pindex->Tip()->nHeight,notarized_height,notarized_hash.ToString().c_str()); if ( notary->nHeight == notarized_height ) // if notarized_hash not in chain, reorg { if ( nHeight < notarized_height ) @@ -539,7 +608,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,pindex->nHeight); return(0); } @@ -577,3 +646,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; CBlockIndex *pindex; + if ( (sp= komodo_stateptrget((char *)"KMD")) != 0 ) + *kmdheightp = sp->CURRENT_HEIGHT; + else *kmdheightp = 0; + if ( (pindex= chainActive.Tip()) != 0 && pindex->nHeight == (int32_t)komodo_longestchain() ) + return(1); + else return(0); +} diff --git a/src/komodo_events.h b/src/komodo_events.h index 017fcb145..0d77b0148 100644 --- a/src/komodo_events.h +++ b/src/komodo_events.h @@ -16,56 +16,23 @@ #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 *komodo_eventadd(struct komodo_state *sp,int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen) { - 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 *ep; uint16_t len = (uint16_t)(sizeof(*ep) + datalen); - ep = (struct komodo_event *)calloc(1,len); - ep->len = len; - ep->height = height; - ep->type = type; - 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; + struct komodo_event *ep=0; uint16_t len = (uint16_t)(sizeof(*ep) + datalen); + if ( sp != 0 ) + { + portable_mutex_lock(&komodo_mutex); + ep = (struct komodo_event *)calloc(1,len); + ep->len = len; + ep->height = height; + ep->type = type; + strcpy(ep->symbol,symbol); + if ( datalen != 0 ) + memcpy(ep->space,data,datalen); + 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; + portable_mutex_unlock(&komodo_mutex); + } return(ep); } @@ -77,7 +44,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); } @@ -85,48 +52,29 @@ void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t heig void komodo_eventadd_pubkeys(struct komodo_state *sp,char *symbol,int32_t height,uint8_t num,uint8_t pubkeys[64][33]) { struct komodo_event_pubkeys P; - printf("eventadd pubkeys ht.%d\n",height); + //printf("eventadd pubkeys ht.%d\n",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]; + struct komodo_event_opreturn O; uint8_t opret[16384]; memset(&O,0,sizeof(O)); O.txid = txid; O.value = value; @@ -134,21 +82,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); + komodo_opreturn(height,value,buf,opretlen,txid,vout,symbol); } -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 +109,57 @@ 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 ( sp != 0 ) + { + 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; diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d72c4b1c0..0fb8a74d7 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -15,155 +15,361 @@ // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse -uint64_t komodo_paxtotal() +int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base) { - struct pax_transaction *pax,*tmp; uint64_t total = 0; - HASH_ITER(hh,PAX,pax,tmp) + int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed; + *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0; + if ( (baseid= komodo_baseid(base)) >= 0 ) { - if ( pax->marked == 0 ) + if ( (sp= komodo_stateptrget(base)) != 0 ) { - if ( komodo_is_issuer() != 0 ) - total += pax->fiatoshis; - else total += pax->komodoshis; - } - } - return(total); + *deposited = sp->deposited; + *issued = sp->issued; + *withdrawn = sp->withdrawn; + *approved = sp->approved; + *redeemed = sp->redeemed; + netliability = (sp->deposited - sp->withdrawn) - sp->shorted; + maxallowed = komodo_maxallowed(baseid); + if ( netliability < maxallowed ) + *available = (maxallowed - netliability); + //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed)); + return(0); + } else printf("pax_fiatstatus cant get basesp.%s\n",base); + } else printf("pax_fiatstatus illegal base.%s\n",base); + return(-1); } -struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout) +void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type) { - struct pax_transaction *pax; + memcpy(buf,&txid,32); + memcpy(&buf[32],&vout,2); + buf[34] = type; +} + +struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type) +{ + struct pax_transaction *pax; uint8_t buf[35]; pthread_mutex_lock(&komodo_mutex); - HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); - if ( pax != 0 ) - memcpy(space,pax,sizeof(*pax)); + pax_keyset(buf,txid,vout,type); + HASH_FIND(hh,PAX,buf,sizeof(buf),pax); pthread_mutex_unlock(&komodo_mutex); return(pax); } -struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark) +struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout) { - struct pax_transaction *pax; + struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'D', 'I', 'W', 'A', 'X' }; + for (i=0; itxid = txid; pax->vout = vout; - HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); + pax->type = type; + memcpy(pax->buf,buf,sizeof(pax->buf)); + HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark); } if ( pax != 0 ) - { pax->marked = mark; - //int32_t i; for (i=0; i<32; i++) - // printf("%02x",((uint8_t *)&txid)[i]); - //printf(" paxmark.ht %d vout%d\n",mark,vout); - memcpy(space,pax,sizeof(*pax)); - } pthread_mutex_unlock(&komodo_mutex); 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,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context { - struct pax_transaction *pax; int32_t addflag = 0; + struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s; + sp = komodo_stateptr(str,dest); pthread_mutex_lock(&komodo_mutex); - HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); + pax_keyset(buf,txid,vout,type); + HASH_FIND(hh,PAX,buf,sizeof(buf),pax); if ( pax == 0 ) { pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); pax->txid = txid; pax->vout = vout; - HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); + pax->type = type; + memcpy(pax->buf,buf,sizeof(pax->buf)); + HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); addflag = 1; - if ( ASSETCHAINS_SYMBOL[0] == 0 ) + if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) { int32_t i; for (i=0; i<32; i++) printf("%02x",((uint8_t *)&txid)[i]); - printf(" v.%d [%s] kht.%d ht.%d create pax.%p\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax); + printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source); } } pthread_mutex_unlock(&komodo_mutex); 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 ( pax->marked == 0 ) - { - if ( addflag != 0 ) - printf("[%s] addflag.%d ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,symbol,dstr(fiatoshis),coinaddr,height,otherheight,dstr(komodo_paxtotal())); - } - //else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height,otherheight); + if ( value != 0 ) + pax->komodoshis = value; + if ( symbol != 0 ) + strcpy(pax->symbol,symbol); + if ( source != 0 ) + strcpy(pax->source,source); + 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; } else { pax->marked = height; - printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight); + //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight); } } -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_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax) { - int32_t i,n=0,j,len; - if ( opretbuf[opretlen-5] == '-' ) - *shortflagp = 1; - else *shortflagp = 0; + int32_t i,len = 0; + if ( rwflag == 1 ) + { + for (i=0; i<32; i++) + opretbuf[len++] = ((uint8_t *)&pax->txid)[i]; + opretbuf[len++] = pax->vout & 0xff; + opretbuf[len++] = (pax->vout >> 8) & 0xff; + } + else + { + for (i=0; i<32; i++) + ((uint8_t *)&pax->txid)[i] = opretbuf[len++]; + //for (i=0; i<32; i++) + // printf("%02x",((uint8_t *)&pax->txid)[31-i]); + pax->vout = opretbuf[len++]; + pax->vout += ((uint32_t)opretbuf[len++] << 8); + //printf(" txid v.%d\n",pax->vout); + } + len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis); + len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis); + len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height); + len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight); + if ( rwflag != 0 ) + { + memcpy(&opretbuf[len],pax->rmd160,20), len += 20; + for (i=0; i<4; i++) + opretbuf[len++] = pax->source[i]; + } + else + { + memcpy(pax->rmd160,&opretbuf[len],20), len += 20; + for (i=0; i<4; i++) + pax->source[i] = opretbuf[len++]; + } + return(len); +} + +int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo) +{ + struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16]; + incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4)); 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 + //for (i=0; i>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d short.%d\n",symbol,dstr(fiatoshis),height,otherheight,shortflag); + memset(&p,0,sizeof(p)); + len += komodo_rwapproval(0,&opretbuf[len],&p); + if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 ) + { + txids[n] = p.txid; + vouts[n] = p.vout; + values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis; + srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis; + kmdheights[n] = p.height; + otherheights[n] = p.otherheight; + memcpy(&rmd160s[n * 20],p.rmd160,20); + baseids[n] = komodo_baseid(p.source); + if ( 0 ) + { + char coinaddr[64]; + bitcoin_address(coinaddr,60,&rmd160s[n * 20],20); + printf(">>>>>>> %s: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",type=='A'?"approvedA":"issuedX",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.fiatoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n])); + } + } } + else + { + for (j=0; j<32; j++) + { + ((uint8_t *)&txids[n])[j] = opretbuf[len++]; + //printf("%02x",((uint8_t *)&txids[n])[j]); + } + vouts[n] = opretbuf[len++]; + vouts[n] = (opretbuf[len++] << 8) | vouts[n]; + baseids[n] = komodo_baseid(base); + if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 ) + { + values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis; + srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis; + kmdheights[n] = pax->height; + otherheights[n] = pax->otherheight; + memcpy(&rmd160s[n * 20],pax->rmd160,20); + } + } + //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); } } 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,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp; + if ( komodo_isrealtime(&ht) == 0 ) + return(0); + else + { + HASH_ITER(hh,PAX,pax,tmp) + { + if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' ) + str = pax->symbol; + else str = pax->source; + basesp = komodo_stateptrget(str); + if ( basesp != 0 && pax->didstats == 0 && pax->type == 'I' ) + { + if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 ) + { + if ( pax2->fiatoshis != 0 ) + { + pax->komodoshis = pax2->komodoshis; + pax->fiatoshis = pax2->fiatoshis; + basesp->issued += pax->fiatoshis; + pax->didstats = 1; + if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight); + pax2->marked = pax->height; + pax->marked = pax->height; + } + } + } + } + } + komodo_stateptr(symbol,dest); + 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 ( strcmp(symbol,pax->symbol) == 0 ) + { + if ( pax->marked == 0 ) + { + if ( komodo_is_issuer() != 0 ) + total += pax->fiatoshis; + else if ( pax->approved != 0 ) + { + if ( pax->validated != 0 ) + total += pax->komodoshis; + else + { + seed = 0; + checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis); + //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",pax->height,pax->symbol,(double)pax->fiatoshis/COIN,(double)pax->komodoshis/COIN,(double)checktoshis/COIN); + //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight); + if ( seed != 0 ) + { + if ( checktoshis >= pax->komodoshis ) + { + total += pax->komodoshis; + pax->validated = pax->komodoshis; + } else pax->marked = pax->height; + } + } + } + } + } + } + //printf("paxtotal %.8f\n",dstr(total)); + return(total); +} + +int32_t komodo_pending_withdraws(char *opretstr) +{ + struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; 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) + { + //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved); + if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 ) + { + // add 'A' opreturn entry + if ( len == 0 ) + opretbuf[len++] = 'A'; + len += komodo_rwapproval(1,&opretbuf[len],pax); + //printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax); + } + } + if ( len > 0 ) + init_hexbytes_noT(opretstr,opretbuf,len); + else opretstr[0] = 0; + fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal())); + 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[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask; + sp = komodo_stateptr(symbol,dest); strcpy(symbol,base); PENDING_KOMODO_TX = 0; if ( tokomodo == 0 ) + { opcode = 'I'; - else opcode = 'X'; + if ( komodo_isrealtime(&ht) == 0 ) + return(0); + } else opcode = 'X'; 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 ( strcmp(symbol,"KMD") == 0 && pax->approved == 0 ) + continue; + //else if ( strcmp(symbol,"KMD") != 0 ) + { +#ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE + struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD"); + if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height ) // assumes same chain as notarize + pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT; +#endif + } + if ( pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis ) + { + printf("miner: skip %s %.8f when avail %.8f\n",symbol,dstr(pax->fiatoshis),dstr(available)); + continue; + } if ( pax->marked != 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 ( strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 ) + { + //printf("pax->symbol.%s != %s or null pax->validated %.8f\n",pax->symbol,symbol,dstr(pax->validated)); + continue; + } + if ( 0 && 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); @@ -174,36 +380,25 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,cha memcpy(script,pax->rmd160,20), script += 20; *script++ = 0x88; *script++ = 0xac; - for (i=0; i<32; i++) - { - //printf("%02x",((uint8_t *)&pax->txid)[i]); - data[len++] = ((uint8_t *)&pax->txid)[i]; - } - data[len++] = pax->vout & 0xff; - data[len++] = (pax->vout >> 8) & 0xff; if ( tokomodo == 0 ) + { + for (i=0; i<32; i++) + data[len++] = ((uint8_t *)&pax->txid)[i]; + data[len++] = pax->vout & 0xff; + data[len++] = (pax->vout >> 8) & 0xff; PENDING_KOMODO_TX += pax->fiatoshis; + } else { - //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.10000000,"fiat":"EUR","kmdheight":57930,"height":153,"KMD":0.78329000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5","rmd160":"306c507eea639e7220b3069ed9f49f3bc97eaca1"}] - 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++) - data[len++] = pax->symbol[i]; - data[len++] = 0; + len += komodo_rwapproval(1,&data[len],pax); PENDING_KOMODO_TX += pax->komodoshis; - printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX)); + //printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX)); } if ( numvouts++ >= 64 ) break; } if ( numvouts > 1 ) { - if ( shortflag != 0 ) - data[len++] = '-'; if ( tokomodo != 0 ) strcpy(symbol,(char *)"KMD"); for (i=0; symbol[i]!=0; i++) @@ -223,7 +418,13 @@ 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]; uint64_t available,deposited,issued,withdrawn,approved,redeemed; int64_t values[64],srcvalues[64]; struct pax_transaction *pax; + memset(baseids,0xff,sizeof(baseids)); + memset(values,0,sizeof(values)); + memset(srcvalues,0,sizeof(srcvalues)); + 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 +436,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,51 +445,51 @@ 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,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 ) { for (i=1; ifiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) ) + pax->type = opcode; + if ( opcode == 'I' && pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis ) { - if ( pax->marked != 0 ) + printf("checkdeposit: skip %s %.8f when avail %.8f\n",pax->symbol,dstr(pax->fiatoshis),dstr(available)); + continue; + } + if ( ((opcode == 'I' && (pax->fiatoshis == 0 || pax->fiatoshis == block.vtx[0].vout[i].nValue)) || (opcode == 'X' && (pax->komodoshis == 0 || pax->komodoshis == block.vtx[0].vout[i].nValue))) ) + { + if ( pax->marked != 0 && height >= 80820 ) + { + printf("%c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax); errs++; - 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)); + } else matched++; } else { - hash = block.GetHash(); - if ( opcode == 'X' ) - { - for (j=0; j<32; j++) - printf("%02x",((uint8_t *)&hash)[j]); - printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i); - // validate amount! via fiat chain - } + for (j=0; j<32; j++) + printf("%02x",((uint8_t *)&txids[i-1])[j]); + printf(" cant paxfind %c txid\n",opcode); + printf("%c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax); } } else { - if ( opcode == 'X' ) - { - matched++; - for (j=0; j<32; j++) - printf("%02x",((uint8_t *)&txids[i-1])[j]); - printf(" cant paxfind txid\n"); - // validate amount! via fiat chain - } else if ( opcode == 'I' ) - matched++; + hash = block.GetHash(); + for (j=0; j<32; j++) + printf("%02x",((uint8_t *)&hash)[j]); + printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i); } - komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height); } if ( matched != num ) { + printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d\n",symbol,height,opcode,matched,num); // can easily happen depending on order of loading - if ( height > 60000 ) - printf("WARNING: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num); + if ( height > 100000 ) //&& opcode == 'X' ) + { + printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num); + return(-1); + } } } //printf("opretlen.%d num.%d\n",opretlen,num); @@ -296,277 +497,373 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above return(0); } -const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout) +int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed) { - 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"; - tokomodo = (komodo_is_issuer() == 0); - if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + int32_t ratio; + if ( seed == 0 && checkvalue != 0 ) { - for (i=0; i= 63 && ratio <= 65 ) + return(0); + else + { + if ( kmdheight >= 86150 ) + printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio); + return(-1); + } } + else if ( checkvalue != 0 ) + { + ratio = ((value << 10) / checkvalue); + if ( ratio >= 1023 && ratio <= 1025 ) + return(0); + } + return(value != checkvalue); +} + +const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) +{ + uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; + const char *typestr = "unknown"; + memset(baseids,0xff,sizeof(baseids)); + memset(values,0,sizeof(values)); + memset(srcvalues,0,sizeof(srcvalues)); + memset(rmd160s,0,sizeof(rmd160s)); + memset(kmdheights,0,sizeof(kmdheights)); + memset(otherheights,0,sizeof(otherheights)); + tokomodo = (komodo_is_issuer() == 0); if ( opretbuf[0] == 'D' ) { + tokomodo = 0; if ( opretlen == 38 ) // any KMD tx { 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; if ( kmdheight <= height ) { - if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG ) + didstats = 0; + if ( strcmp(base,ASSETCHAINS_SYMBOL) == 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) ) - { - 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 0 && shortflag == ASSETCHAINS_SHORTFLAG ) - { - for (i=0; i= 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; voutdeposited += fiatoshis; + didstats = 1; + if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value)); + } else printf("cant get stateptr.(%s)\n",base); + komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0); + } + if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 ) + { + pax->height = kmdheight; + pax->validated = value; + pax->komodoshis = value; + pax->fiatoshis = fiatoshis; + if ( didstats == 0 && pax->didstats == 0 ) + { + if ( (basesp= komodo_stateptrget(base)) != 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) ) + basesp->deposited += fiatoshis; + didstats = 1; + if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p deposited %s += %.8f/%.8f kmdheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),kmdheight,pax->height,dstr(value),dstr(pax->komodoshis)); + } + } + if ( didstats != 0 ) + pax->didstats = 1; + if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 ) + { + pax2->fiatoshis = pax->fiatoshis; + pax2->komodoshis = pax->komodoshis; + pax->marked = pax2->marked = pax->height; + pax->otherheight = height; + if ( pax2->didstats == 0 ) + { + if ( (basesp= komodo_stateptrget(base)) != 0 ) { - decode_hex(script,len,hexstr); - komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len); + basesp->issued += pax2->fiatoshis; + pax2->didstats = 1; + if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight); } } } } } - } 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= kmdheight ) - KOMODO_REALTIME = (uint32_t)time(NULL); + else if ( kmdheight > 91800 ) + printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD\n",base,dstr(fiatoshis),kmdheight,dstr(value)); } - free_json(infoobj); } - free(retstr); } - else + else if ( opretbuf[0] == 'I' ) { - 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; - if ( ASSETCHAINS_SYMBOL[0] == 0 ) - { - sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol); - if ( (retstr= issue_curl(cmd)) != 0 ) + tokomodo = 0; + if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) { - if ( (array= cJSON_Parse(retstr)) != 0 ) + if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 ) { - if ( (n= cJSON_GetArraySize(array)) > 0 ) + for (i=0; i 0 && height > 0 ) + printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]); + continue; + } + bitcoin_address(coinaddr,60,&rmd160s[i*20],20); + komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0); + komodo_paxmark(height,txids[i],vouts[i],'I',height); + if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 ) + { + pax->type = opretbuf[0]; + strcpy(pax->source,(char *)&opretbuf[opretlen-4]); + if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 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); + // realtime path? + pax->fiatoshis = pax2->fiatoshis; + pax->komodoshis = pax2->komodoshis; + pax->marked = pax2->marked = pax2->height; + if ( pax->didstats == 0 ) + { + if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) + { + basesp->issued += pax->fiatoshis; + pax->didstats = 1; + pax->height = pax2->height; + pax->otherheight = height; + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight); + } + } } + komodo_paxmark(pax->height,txids[i],vouts[i],'D',height); + } + } + } else printf("opreturn none issued?\n"); + } + if ( strcmp(source,ASSETCHAINS_SYMBOL) == 0 ) + printf("source.%s opreturn[I] matches %s\n",source,(char *)&opretbuf[opretlen-4]); + } + else if ( opretbuf[0] == 'W' && opretlen >= 38 ) + { + tokomodo = 1; + 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); + didstats = 0; + if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 ) + { + if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) ) + { + if ( (basesp= komodo_stateptrget(base)) != 0 ) + { + basesp->withdrawn += value; + didstats = 1; + if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p withdrawn %s += %.8f\n",basesp,base,dstr(value)); + } + //printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height); + } + komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0); + if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 ) + { + if ( didstats != 0 ) + pax->didstats = 1; + pax->type = opretbuf[0]; + pax->validated = komodoshis; + } + } + } + else if ( tokomodo != 0 && opretbuf[0] == 'A' ) + { + tokomodo = 1; + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + { + for (i=0; i 0 ) + { + for (i=0; i KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)srcvalues[i]/COIN,(double)values[i]/COIN,(double)checktoshis/COIN); + for (j=0; j<32; j++) + printf("%02x",((uint8_t *)&txids[i])[j]); + printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);*/ + if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 ) + { + bitcoin_address(coinaddr,60,&rmd160s[i*20],20); + komodo_gateway_deposit(coinaddr,values[i],CURRENCIES[baseids[i]],srcvalues[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]); + komodo_paxmark(height,txids[i],vouts[i],'W',height); + komodo_paxmark(height,txids[i],vouts[i],'A',height); + if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) + { + basesp->approved += srcvalues[i]; + didstats = 1; + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); + } + //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i])); + } + else if ( pax->didstats == 0 && srcvalues[i] != 0 ) + { + if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) + { + basesp->approved += srcvalues[i]; + didstats = 1; + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); + } + } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]); + if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 ) + { + pax->type = opretbuf[0]; + pax->approved = kmdheights[i]; + if ( didstats != 0 ) + pax->didstats = 1; + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax); + } + } + } + //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal())); + } + else if ( opretbuf[0] == 'X' ) + { + tokomodo = 1; + if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) + { + for (i=0; itype = opretbuf[0]; + if ( baseids[i] >= 0 && srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) + { + basesp->redeemed += srcvalues[i]; + pax->didstats = 1; + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + printf("########### %p redeemed %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); } } } - //printf("retstr.(%s)\n",retstr); - free(retstr); - } + } //else printf("komodo_issued_opreturn returned %d\n",n); } + return(typestr); } + +void komodo_passport_iteration() +{ + 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; + while ( KOMODO_INITDONE == 0 ) + { + fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n"); + sleep(3); + } + 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=32; baseid>=0; baseid--) + { + sp = 0; + isrealtime = 0; + base = (char *)CURRENCIES[baseid]; + if ( baseid+1 != refid ) + { + komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate"); + komodo_nameset(symbol,dest,base); + if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 ) + { + fseek(fp,0,SEEK_END); + if ( ftell(fp) > lastpos[baseid] ) + { + if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) + printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base); + fseek(fp,lastpos[baseid],SEEK_SET); + while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) + ; + lastpos[baseid] = ftell(fp); + if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) + printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]); + } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); + fclose(fp); + } + 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; + } + komodo_paxtotal(); + refsp->RTmask = RTmask; + KOMODO_PASSPORT_INITDONE = 1; +} + diff --git a/src/komodo_globals.h b/src/komodo_globals.h index f5eba4929..7c8b7126d 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -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,KOMODO_REWIND,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE; 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; diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 954c029a5..cc80582ca 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -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 >> 1) || (wt > 7 && (signedmask & 3) != 0) ) + if ( wt > (numnotaries >> 1) || (wt > 7 && (signedmask & 1) != 0) ) return(1); else return(0); } @@ -130,7 +99,7 @@ void komodo_notarysinit(int32_t origheight,uint8_t pubkeys[64][33],int32_t num) height /= KOMODO_ELECTION_GAP; height = ((height + 1) * KOMODO_ELECTION_GAP); htind = (height / KOMODO_ELECTION_GAP); - printf("htind.%d activation %d from %d vs %d | hwmheight.%d\n",htind,height,origheight,(((origheight+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,hwmheight); + printf("htind.%d activation %d from %d vs %d | hwmheight.%d %s\n",htind,height,origheight,(((origheight+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,hwmheight,ASSETCHAINS_SYMBOL); } else htind = 0; pthread_mutex_lock(&komodo_mutex); for (k=0; kpubkey,pubkeys[k],33); kp->notaryid = k; HASH_ADD_KEYPTR(hh,N.Notaries,kp->pubkey,33,kp); - //if ( height > 0 ) + if ( 0 && height > 10000 ) { for (i=0; i<33; i++) printf("%02x",pubkeys[k][i]); @@ -166,6 +135,7 @@ int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) { // -1 if not notary, 0 if notary, 1 if special notary struct knotary_entry *kp; int32_t numnotaries=0,htind,modval = -1; + komodo_init(0); *notaryidp = -1; if ( height < 0 || height >= KOMODO_MAXBLOCKS ) { @@ -199,16 +169,18 @@ 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++]; + portable_mutex_lock(&komodo_mutex); + 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; sp->NOTARIZED_HASH = np->notarized_hash = notarized_hash; sp->NOTARIZED_DESTTXID = np->notarized_desttxid = notarized_desttxid; + portable_mutex_unlock(&komodo_mutex); } -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 +200,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; iNUM_NPOINTS > 0 ) { - if ( NPOINTS[i].nHeight >= nHeight ) - break; - np = &NPOINTS[i]; + for (i=0; iNUM_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 +246,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 +274,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); diff --git a/src/komodo_pax.h b/src/komodo_pax.h index d63ffa0f6..1f7e23a03 100644 --- a/src/komodo_pax.h +++ b/src/komodo_pax.h @@ -94,6 +94,17 @@ uint64_t peggy_smooth_coeffs[sizeof(Peggy_inds)/sizeof(*Peggy_inds)] = // numpri 1, 1, 1, 1, 1, 1, 0, 0, // isum 100000000000 }; +uint64_t komodo_maxallowed(int32_t baseid) +{ + uint64_t mult,val = COIN * (uint64_t)10000; + if ( baseid < 0 || baseid >= 32 ) + return(0); + if ( baseid < 10 ) + val *= 4; + mult = MINDENOMS[baseid] / MIND; + return(mult * val); +} + uint64_t komodo_paxvol(uint64_t volume,uint64_t price) { if ( volume < 10000000000 ) @@ -285,20 +296,84 @@ void komodo_pvals(int32_t height,uint32_t *pvals,uint8_t numpvals) KMDBTC = ((double)kmdbtc / (1000000000. * 1000.)); BTCUSD = ((double)btcusd / (1000000000. / 1000.)); CNYUSD = ((double)cnyusd / 1000000000.); + portable_mutex_lock(&komodo_mutex); PVALS = (uint32_t *)realloc(PVALS,(NUM_PRICES+1) * sizeof(*PVALS) * 36); PVALS[36 * NUM_PRICES] = height; memcpy(&PVALS[36 * NUM_PRICES + 1],pvals,sizeof(*pvals) * 35); NUM_PRICES++; + portable_mutex_unlock(&komodo_mutex); if ( 0 ) printf("OP_RETURN.%d KMD %.8f BTC %.6f CNY %.6f NUM_PRICES.%d (%llu %llu %llu)\n",height,KMDBTC,BTCUSD,CNYUSD,NUM_PRICES,(long long)kmdbtc,(long long)btcusd,(long long)cnyusd); } } } -uint64_t komodo_paxcalc(uint32_t *pvals,int32_t baseid,int32_t relid,uint64_t basevolume) +uint64_t komodo_paxcorrelation(uint64_t *votes,int32_t numvotes,uint64_t seed) { - uint32_t pvalb,pvalr,kmdbtc,btcusd; uint64_t usdvol,baseusd,usdkmd,baserel,ranked[32]; - if ( basevolume > 10000*COIN ) + int32_t i,j,k,ind,zeroes,wt,nonz; int64_t delta; uint64_t lastprice,tolerance,den,densum,sum=0; + for (sum=i=zeroes=nonz=0; i> 2) ) + return(0); + sum /= nonz; + lastprice = sum; + for (i=0; i (numvotes >> 1) ) + break; + } + } + } + } + if ( wt > (numvotes >> 1) ) + { + ind = i; + for (densum=sum=j=0; j KOMODO_PAXMAX ) { printf("paxcalc overflow %.8f\n",dstr(basevolume)); return(0); @@ -307,16 +382,20 @@ uint64_t komodo_paxcalc(uint32_t *pvals,int32_t baseid,int32_t relid,uint64_t ba { if ( relid == MAX_CURRENCIES ) { - kmdbtc = pvals[MAX_CURRENCIES]; - btcusd = pvals[MAX_CURRENCIES + 1]; + if ( kmdbtc == 0 ) + kmdbtc = pvals[MAX_CURRENCIES]; + if ( btcusd == 0 ) + btcusd = pvals[MAX_CURRENCIES + 1]; + if ( kmdbtc < 25000000 ) + kmdbtc = 25000000; if ( pvals[USD] != 0 && kmdbtc != 0 && btcusd != 0 ) { - baseusd = ((uint64_t)pvalb * 1000000000) / pvals[USD]; - usdvol = komodo_paxvol(basevolume,baseusd) / MINDENOMS[baseid]; - usdkmd = ((uint64_t)btcusd * 1000000000) / kmdbtc; - //printf("base -> USD %llu, BTC %llu KMDUSD %llu\n",(long long)baseusd,(long long)btcusd,(long long)kmdusd); + baseusd = (((uint64_t)pvalb * 1000000000) / pvals[USD]); + usdvol = komodo_paxvol(basevolume,baseusd); + usdkmd = ((uint64_t)kmdbtc * 1000000000) / btcusd; + //printf("kmdbtc.%llu btcusd.%llu base -> USD %llu, usdkmd %llu usdvol %llu -> %llu\n",(long long)kmdbtc,(long long)btcusd,(long long)baseusd,(long long)usdkmd,(long long)usdvol,(long long)(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd) / (MINDENOMS[baseid]/100))); //printf("usdkmd.%llu basevolume.%llu baseusd.%llu paxvol.%llu usdvol.%llu -> %.8f\n",(long long)usdkmd,(long long)basevolume,(long long)baseusd,(long long)komodo_paxvol(basevolume,baseusd),(long long)usdvol,dstr(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd))); - return(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd)); + return(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd) / (MINDENOMS[baseid]/100)); } //else printf("zero val in KMD conv %llu %llu %llu\n",(long long)pvals[USD],(long long)kmdbtc,(long long)btcusd); } else if ( baseid == relid ) @@ -343,16 +422,27 @@ uint64_t komodo_paxcalc(uint32_t *pvals,int32_t baseid,int32_t relid,uint64_t ba return(0); } -uint64_t _komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolume) +uint64_t _komodo_paxprice(uint64_t *kmdbtcp,uint64_t *btcusdp,int32_t height,char *base,char *rel,uint64_t basevolume,uint64_t kmdbtc,uint64_t btcusd) { int32_t baseid=-1,relid=-1,i; uint32_t *ptr; + if ( height > 10 ) + height -= 10; if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 ) { for (i=NUM_PRICES-1; i>=0; i--) { ptr = &PVALS[36 * i]; if ( *ptr < height ) - return(komodo_paxcalc(&ptr[1],baseid,relid,basevolume)); + { + if ( kmdbtcp != 0 && btcusdp != 0 ) + { + *kmdbtcp = ptr[MAX_CURRENCIES + 1] / 539; + *btcusdp = ptr[MAX_CURRENCIES + 2] / 539; + } + if ( kmdbtc != 0 && btcusd != 0 ) + return(komodo_paxcalc(&ptr[1],baseid,relid,basevolume,kmdbtc,btcusd)); + else return(0); + } } } else printf("paxprice invalid base.%s %d, rel.%s %d\n",base,baseid,rel,relid); return(0); @@ -360,75 +450,39 @@ uint64_t _komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolum uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume) { - int32_t i,j,k,ind,zeroes,numvotes,wt,nonz; int64_t delta; uint64_t lastprice,seed,tolerance,den,densum,sum=0,votes[539]; - if ( basevolume > 10000*COIN ) + int32_t i,j,k,ind,zeroes,numvotes,wt,nonz; int64_t delta; uint64_t lastprice,tolerance,den,densum,sum=0,votes[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],btcusds[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],kmdbtcs[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],kmdbtc,btcusd; + *seedp = komodo_seed(height); + if ( basevolume > KOMODO_PAXMAX ) { printf("komodo_paxprice overflow %.8f\n",dstr(basevolume)); return(0); } + if ( strcmp(base,"KMD") == 0 || strcmp(base,"kmd") == 0 ) + { + printf("kmd cannot be base currency\n"); + return(0); + } numvotes = (int32_t)(sizeof(Peggy_inds)/sizeof(*Peggy_inds)); memset(votes,0,sizeof(votes)); - for (sum=i=zeroes=nonz=0; i> 1) ) return(0); - sum /= nonz; - lastprice = sum; - for (i=0; i (numvotes >> 1) ) - break; - } - } - } - } - if ( wt > (numvotes >> 1) ) - { - ind = i; - for (densum=sum=j=0; j= max ) return(num); @@ -454,22 +508,26 @@ 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,×tamp,&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); } uint64_t PAX_fiatdest(uint64_t *seedp,int32_t tokomodo,char *destaddr,uint8_t pubkey33[33],char *coinaddr,int32_t height,char *origbase,int64_t fiatoshis) { uint8_t shortflag = 0; char base[4]; int32_t i,baseid; uint8_t addrtype,rmd160[20]; int64_t komodoshis = 0; + *seedp = 0; if ( (baseid= komodo_baseid(origbase)) < 0 || baseid == MAX_CURRENCIES ) + { + printf("PAX_fiatdest illegal base.(%s)\n",origbase); return(0); + } for (i=0; i<3; i++) base[i] = toupper(origbase[i]); base[i] = 0; 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 seed.%llx\n",height,base,(double)fiatoshis/COIN,(double)komodoshis/COIN,(long long)*seedp); if ( bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr) == 20 ) { PAX_pubkey(1,pubkey33,&addrtype,rmd160,base,&shortflag,tokomodo != 0 ? &komodoshis : &fiatoshis); diff --git a/src/komodo_structs.h b/src/komodo_structs.h index dd653fc37..b62a76d3c 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -16,21 +16,52 @@ #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_MINRATIFY ((height < 90000) ? 7 : 11) #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[]; +}; + struct pax_transaction { UT_hash_handle hh; uint256 txid; - uint64_t komodoshis,fiatoshis; - int32_t marked,height,otherheight; + uint64_t komodoshis,fiatoshis,validated; + int32_t marked,height,otherheight,approved,didstats; uint16_t vout; - char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag; + char symbol[16],source[16],coinaddr[64]; uint8_t rmd160[20],type,buf[35]; }; -//struct nutxo_entry { UT_hash_handle hh; uint256 txhash; uint64_t voutmask; int32_t notaryid,height; }; struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; struct knotaries_entry { int32_t height,numnotaries; struct knotary_entry *Notaries; }; struct notarized_checkpoint { uint256 notarized_hash,notarized_desttxid; int32_t nHeight,notarized_height; }; @@ -38,6 +69,10 @@ 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; + uint64_t deposited,issued,withdrawn,approved,redeemed,shorted; + 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; }; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 78d38141d..15562669d 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1261,43 +1261,83 @@ 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]; FILE *fp; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i; - r = (uint32_t)time(NULL); - r2 = OS_milliseconds(); - memcpy(buf,&r,sizeof(r)); - memcpy(&buf[sizeof(r)],&r2,sizeof(r2)); - memcpy(&buf[sizeof(r)+sizeof(r2)],symbol,strlen(symbol)); - crc = calc_crc32(0,(uint8_t *)buf,(int32_t)(sizeof(r)+sizeof(r2)+strlen(symbol))); - OS_randombytes(buf2,sizeof(buf2)); - for (i=0; i userpass.(%s)\n",fname,KMDUSERPASS); } 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 +1398,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 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"); @@ -1394,8 +1471,7 @@ void komodo_args() int32_t komodo_baseid(char *origbase); 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()); @@ -1413,32 +1489,59 @@ void komodo_args() } else { + char fname[512],username[512],password[4096]; FILE *fp; ASSETCHAINS_PORT = 8777; - //fprintf(stderr,"IS_KOMODO_NOTARY %d %s\n",IS_KOMODO_NOTARY,NOTARY_PUBKEY.c_str()); + strcpy(fname,GetDataDir().string().c_str()); +#ifdef WIN32 + while ( fname[strlen(fname)-1] != '\\' ) + fname[strlen(fname)-1] = 0; + strcat(fname,".komodo/komodo.conf"); +#else + while ( fname[strlen(fname)-1] != '/' ) + fname[strlen(fname)-1] = 0; + strcat(fname,".komodo/komodo.conf"); +#endif + if ( (fp= fopen(fname,"rb")) != 0 ) + { + komodo_userpass(username,password,fp); + sprintf(KMDUSERPASS,"%s:%s",username,password); + fclose(fp); + //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); + } else printf("couldnt open.(%s)\n",fname); } //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)); +} + + + diff --git a/src/main.cpp b/src/main.cpp index 41d21c7b8..fe832ac0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } } @@ -676,14 +676,18 @@ bool IsStandardTx(const CTransaction& tx, string& reason) txnouttype whichType; BOOST_FOREACH(const CTxOut& txout, tx.vout) { - if (!::IsStandard(txout.scriptPubKey, whichType)) { + if (!::IsStandard(txout.scriptPubKey, whichType)) + { reason = "scriptpubkey"; - fprintf(stderr,"vout.%d nDataout.%d\n",v,nDataOut); + fprintf(stderr,">>>>>>>>>>>>>>> vout.%d nDataout.%d\n",v,nDataOut); return false; } if (whichType == TX_NULL_DATA) + { nDataOut++; + //fprintf(stderr,"is OP_RETURN\n"); + } else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) { reason = "bare-multisig"; return false; @@ -1179,7 +1183,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); @@ -2551,6 +2555,7 @@ static int64_t nTimePostConnect = 0; * corresponding to pindexNew, to bypass loading it again from disk. */ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *pblock) { + assert(pindexNew->pprev == chainActive.Tip()); mempool.check(pcoinsTip); // Read block from disk. @@ -2689,6 +2694,7 @@ static void PruneBlockIndexCandidates() { * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork. */ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { + extern int32_t KOMODO_REWIND; AssertLockHeld(cs_main); bool fInvalidFound = false; const CBlockIndex *pindexOldTip = chainActive.Tip(); @@ -2699,7 +2705,21 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo if (!DisconnectTip(state)) return false; } - + if ( KOMODO_REWIND != 0 && chainActive.Tip()->nHeight > KOMODO_REWIND ) + { + static int32_t didinit; + if ( didinit++ == 0 ) + { + while (chainActive.Tip()->nHeight > KOMODO_REWIND ) + { + fprintf(stderr,"rewind ht.%d\n",chainActive.Tip()->nHeight); + if ( !DisconnectTip(state) ) + return false; + } + pindexOldTip = chainActive.Tip(); + pindexFork = chainActive.FindFork(pindexMostWork); + } + } // Build list of new blocks to connect. std::vector vpindexToConnect; bool fContinue = true; diff --git a/src/miner.cpp b/src/miner.cpp index 90fd460f6..1c7b477b0 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -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 58 +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,28 +110,32 @@ 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 pblocktemplate(new CBlockTemplate()); if(!pblocktemplate.get()) return NULL; CBlock *pblock = &pblocktemplate->block; // pointer for convenience - if ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight >= 100 ) + if ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight > ASSETCHAINS_MINHEIGHT ) { - 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); + 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 ) + { + 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); break; + } sleep(10); } KOMODO_ON_DEMAND = 0; @@ -374,9 +378,9 @@ 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 ( (nHeight % 60) == 0 || komodo_gateway_deposits(&txNew,(char *)"KMD",1) == 0 ) { - if ( (opretlen= komodo_pax_opreturn(opret,sizeof(opret))) > 0 ) + if ( (opretlen= komodo_pax_opreturn(opret,sizeof(opret))) > 0 ) // have pricefeed { txNew.vout.resize(2); txNew.vout[1].scriptPubKey.resize(opretlen); @@ -390,8 +394,9 @@ 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); + if ( txNew.vout.size() > 1 ) + fprintf(stderr,"%s txNew numvouts.%d\n",ASSETCHAINS_SYMBOL,(int32_t)txNew.vout.size()); } pblock->vtx[0] = txNew; pblocktemplate->vTxFees[0] = -nFees; @@ -445,6 +450,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& // // Internal miner // +int8_t komodo_minerid(int32_t height); CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) { @@ -467,6 +473,12 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) script[34] = OP_CHECKSIG; //scriptPubKey = CScript() << ToByteVector(pubkey) << OP_CHECKSIG; } + if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) + { + for (i=0; i<65; i++) + fprintf(stderr,"%d ",komodo_minerid(chainActive.Tip()->nHeight-i)); + fprintf(stderr," minerids.special %d from ht.%d\n",komodo_is_special(chainActive.Tip()->nHeight+1,NOTARY_PUBKEY33),chainActive.Tip()->nHeight); + } return CreateNewBlock(scriptPubKey); } @@ -532,7 +544,7 @@ void static BitcoinMiner(CWallet *pwallet) else solver = "default"; assert(solver == "tromp" || solver == "default"); LogPrint("pow", "Using Equihash solver \"%s\" with n = %u, k = %u\n", solver, n, k); - fprintf(stderr,"Mining with %s\n",solver.c_str()); + //fprintf(stderr,"Mining with %s\n",solver.c_str()); std::mutex m_cs; bool cancelSolver = false; boost::signals2::connection c = uiInterface.NotifyBlockTip.connect( @@ -548,8 +560,8 @@ void static BitcoinMiner(CWallet *pwallet) { if (chainparams.MiningRequiresPeers()) { - if ( ASSETCHAINS_SEED != 0 && chainActive.Tip()->nHeight < 100 ) - break; + //if ( ASSETCHAINS_SEED != 0 && chainActive.Tip()->nHeight < 100 ) + // break; // Busy-wait for the network to come online so we don't waste time mining // on an obsolete chain. In regtest mode we expect to fly solo. //fprintf(stderr,"Wait for peers...\n"); @@ -567,6 +579,11 @@ void static BitcoinMiner(CWallet *pwallet) } while (true); //fprintf(stderr,"%s Found peers\n",ASSETCHAINS_SYMBOL); } + /*while ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight < ASSETCHAINS_MINHEIGHT ) + { + fprintf(stderr,"%s waiting for block 100, ht.%d\n",ASSETCHAINS_SYMBOL,chainActive.Tip()->nHeight); + sleep(3); + }*/ // // Create new block // @@ -577,7 +594,7 @@ void static BitcoinMiner(CWallet *pwallet) Mining_height = pindexPrev->nHeight+1; Mining_start = (uint32_t)time(NULL); } - if ( ASSETCHAINS_SYMBOL[0] != 0 ) + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) fprintf(stderr,"%s create new block ht.%d\n",ASSETCHAINS_SYMBOL,Mining_height); unique_ptr pblocktemplate(CreateNewBlockWithKey(reservekey)); @@ -598,11 +615,16 @@ void static BitcoinMiner(CWallet *pwallet) if ( ASSETCHAINS_SYMBOL[0] == 0 && komodo_is_special(pindexPrev->nHeight+1,NOTARY_PUBKEY33) > 0 ) { hashTarget = arith_uint256().SetCompact(KOMODO_MINDIFF_NBITS); - //fprintf(stderr,"I am the chosen one for %s ht.%d\n",ASSETCHAINS_SYMBOL,pindexPrev->nHeight+1); + fprintf(stderr,"I am the chosen one for %s ht.%d\n",ASSETCHAINS_SYMBOL,pindexPrev->nHeight+1); } else Mining_start = 0; while (true) { - //fprintf(stderr,"%s start mining loop\n",ASSETCHAINS_SYMBOL); + if ( ASSETCHAINS_SYMBOL[0] != 0 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) + { + //fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); + sleep(10); + break; + } // Hash state KOMODO_CHOSEN_ONE = 0; crypto_generichash_blake2b_state state; @@ -629,16 +651,19 @@ 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))); - sleep(Mining_start+ROUNDROBIN_DELAY-time(NULL)); - KOMODO_CHOSEN_ONE = 1; + //printf("Round robin diff sleep %d\n",(int32_t)(Mining_start+ROUNDROBIN_DELAY-time(NULL))); + int32_t nseconds = Mining_start+ROUNDROBIN_DELAY-time(NULL); + if ( nseconds > 0 ) + sleep(nseconds); + MilliSleep((rand() % 700) + 1); } + KOMODO_CHOSEN_ONE = 1; // Found a solution SetThreadPriority(THREAD_PRIORITY_NORMAL); LogPrintf("KomodoMiner:\n"); @@ -649,10 +674,6 @@ void static BitcoinMiner(CWallet *pwallet) cancelSolver = false; } KOMODO_CHOSEN_ONE = 0; - int32_t i; uint256 hash = pblock->GetHash(); - for (i=0; i<32; i++) - fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); - fprintf(stderr," <- %s Block found %d\n",ASSETCHAINS_SYMBOL,Mining_height); SetThreadPriority(THREAD_PRIORITY_LOWEST); // In regression test mode, stop mining after a block is found. if (chainparams.MineBlocksOnDemand()) { @@ -706,6 +727,11 @@ void static BitcoinMiner(CWallet *pwallet) bool found = EhOptimisedSolve(n, k, curr_state, validBlock, cancelled); ehSolverRuns.increment(); if (found) { + int32_t i; uint256 hash = pblock->GetHash(); + for (i=0; i<32; i++) + fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); + fprintf(stderr," <- %s Block found %d\n",ASSETCHAINS_SYMBOL,Mining_height); + sleep(60); // avoid mining forks break; } } catch (EhSolverCancelledException&) { @@ -720,25 +746,28 @@ void static BitcoinMiner(CWallet *pwallet) // Regtest mode doesn't require peers if (vNodes.empty() && chainparams.MiningRequiresPeers()) { - if ( ASSETCHAINS_SYMBOL[0] == 0 || Mining_height >= 100 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 || Mining_height > ASSETCHAINS_MINHEIGHT ) + { + //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 diff --git a/src/pow.cpp b/src/pow.cpp index 8d97786e0..ac2544cb2 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -108,11 +108,12 @@ int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]); int32_t komodo_currentheight(); extern int32_t KOMODO_CHOSEN_ONE; -//extern int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks +#define KOMODO_ELECTION_GAP 2000 bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params& params) { - bool fNegative,fOverflow; int32_t i,nonz=0,special,special2,notaryid,flag = 0; + extern int32_t KOMODO_REWIND; + bool fNegative,fOverflow; int32_t i,nonz=0,special,special2,notaryid=-1,flag = 0; arith_uint256 bnTarget; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); @@ -128,41 +129,32 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in } if ( nonz == 0 ) return(true); // will come back via different path with pubkey set - //if ( height > 60000 ) + special2 = komodo_is_special(height,pubkey33); + if ( notaryid >= 0 && ((height >= 64000 && height <= 90065) || (height % KOMODO_ELECTION_GAP) > 64) ) { - if ( notaryid >= 0 ) + //if ( special2 == -2 ) + // printf("height.%d special2.%d special.%d\n",height,special2,special); + if ( (height >= 64000 && height <= 90065) || (height % KOMODO_ELECTION_GAP) == 0 || (height < 80000 && (special != 0 || special2 > 0)) || (height >= 80000 && special2 > 0) ) { - special2 = komodo_is_special(height,pubkey33); - if ( special2 == -2 ) - printf("height.%d special2.%d special.%d\n",height,special2,special); - if ( special2 == -2 || (height < 70000 && (special != 0 || special2 > 0)) || - (height >= 70000 && special2 > 0) ) - { - bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); - flag = 1; - } - } - } - /*else - { - if ( special > 0 ) // special notary id == (height % numnotaries) - { - if (UintToArith256(hash) <= bnTarget) // accept normal diff - return true; bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); flag = 1; } - }*/ + } } if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) return error("CheckProofOfWork(): nBits below minimum work"); // Check proof of work matches claimed amount if ( UintToArith256(hash) > bnTarget ) { - //for (i=0; i<33; i++) - // printf("%02x",pubkey33[i]); - //printf(" special.%d notaryid.%d ht.%d mod.%d error\n",special,notaryid,height,(height % 35)); - return error("CheckProofOfWork(): hash doesn't match nBits"); + int32_t i; + for (i=31; i>=0; i--) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" hash vs "); + for (i=31; i>=0; i--) + printf("%02x",((uint8_t *)&bnTarget)[i]); + printf(" ht.%d REWIND.%d special.%d notaryid.%d ht.%d mod.%d error\n",height,KOMODO_REWIND,special,notaryid,height,(height % 35)); + if ( height <= KOMODO_REWIND ) + return error("CheckProofOfWork(): hash doesn't match nBits"); } return true; } diff --git a/src/qt/bitcoinstrings.cpp b/src/qt/bitcoinstrings.cpp index a3c9ec968..756542c4d 100644 --- a/src/qt/bitcoinstrings.cpp +++ b/src/qt/bitcoinstrings.cpp @@ -199,7 +199,7 @@ QT_TRANSLATE_NOOP("bitcoin-core", "Corrupted block database detected"), QT_TRANSLATE_NOOP("bitcoin-core", "Could not parse -rpcbind value %s as network address"), QT_TRANSLATE_NOOP("bitcoin-core", "Debugging/Testing options:"), QT_TRANSLATE_NOOP("bitcoin-core", "Do not load the wallet and disable wallet RPC calls"), -QT_TRANSLATE_NOOP("bitcoin-core", "Do you want to rebuild the block database now?"), +QT_TRANSLATE_NOOP("bitcoin-core", "error with HDD data, maybe just need to update to latest version"), QT_TRANSLATE_NOOP("bitcoin-core", "Done loading"), QT_TRANSLATE_NOOP("bitcoin-core", "Error initializing block database"), QT_TRANSLATE_NOOP("bitcoin-core", "Error initializing wallet database environment %s!"), diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 007600a91..696d9fc39 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -272,6 +272,22 @@ Value getblockhash(const Array& params, bool fHelp) return pblockindex->GetBlockHash().GetHex(); } +uint256 _komodo_getblockhash(int32_t nHeight) +{ + uint256 hash; + LOCK(cs_main); + if ( nHeight >= 0 && nHeight <= chainActive.Height() ) + { + CBlockIndex* pblockindex = chainActive[nHeight]; + hash = pblockindex->GetBlockHash(); + int32_t i; + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" blockhash.%d\n",nHeight); + } else memset(&hash,0,sizeof(hash)); + return(hash); +} + Value getblock(const Array& params, bool fHelp) { if (fHelp || params.size() < 1 || params.size() > 2) @@ -387,6 +403,58 @@ int32_t komodo_paxprices(int32_t *heights,uint64_t *prices,int32_t max,char *bas int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); uint32_t komodo_interest_args(int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n); +int32_t komodo_minerids(uint8_t *minerids,int32_t height); + +Value minerids(const Array& params, bool fHelp) +{ + Object ret; Array a; uint8_t minerids[1000],pubkeys[64][33]; int32_t i,j,n,numnotaries,tally[65]; + if ( fHelp || params.size() != 1 ) + throw runtime_error("minerids needs height\n"); + LOCK(cs_main); + int32_t height = atoi(params[0].get_str().c_str()); + if ( height <= 0 ) + height = chainActive.Tip()->nHeight; + if ( (n= komodo_minerids(minerids,height)) > 0 ) + { + memset(tally,0,sizeof(tally)); + numnotaries = komodo_notaries(pubkeys,height); + if ( numnotaries > 0 ) + { + for (i=0; i= numnotaries ) + tally[64]++; + else tally[minerids[i]]++; + } + for (i=0; i<64; i++) + { + Object item; std::string hex,kmdaddress; char *hexstr,kmdaddr[64],*ptr; int32_t m; + hex.resize(66); + hexstr = (char *)hex.data(); + for (j=0; j<33; j++) + sprintf(&hexstr[j*2],"%02x",pubkeys[i][j]); + item.push_back(Pair("notaryid", i)); + + bitcoin_address(kmdaddr,60,pubkeys[i],33); + m = (int32_t)strlen(kmdaddr); + kmdaddress.resize(m); + ptr = (char *)kmdaddress.data(); + memcpy(ptr,kmdaddr,m); + item.push_back(Pair("KMDaddress", kmdaddress)); + + item.push_back(Pair("pubkey", hex)); + item.push_back(Pair("blocks", tally[i])); + a.push_back(item); + } + Object item; + item.push_back(Pair("pubkey", (char *)"external miners")); + item.push_back(Pair("blocks", tally[64])); + a.push_back(item); + } + ret.push_back(Pair("mined", a)); + } else ret.push_back(Pair("error", (char *)"couldnt extract minerids")); + return ret; +} Value notaries(const Array& params, bool fHelp) { @@ -398,9 +466,9 @@ Value notaries(const Array& params, bool fHelp) if ( height < 0 ) height = 0; //fprintf(stderr,"notaries as of height.%d\n",height); - if ( height > chainActive.Height()+20000 ) - throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); - else + //if ( height > chainActive.Height()+20000 ) + // throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); + //else { if ( (n= komodo_notaries(pubkeys,height)) > 0 ) { @@ -436,6 +504,41 @@ Value notaries(const Array& params, bool fHelp) return ret; } +int32_t komodo_pending_withdraws(char *opretstr); +int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base); +extern char CURRENCIES[][8]; + +Value paxpending(const Array& params, bool fHelp) +{ + Object ret; Array a; char opretbuf[10000*2]; int32_t opretlen,baseid; uint64_t available,deposited,issued,withdrawn,approved,redeemed; + if ( fHelp || params.size() != 0 ) + throw runtime_error("paxpending needs no args\n"); + LOCK(cs_main); + if ( (opretlen= komodo_pending_withdraws(opretbuf)) > 0 ) + ret.push_back(Pair("withdraws", opretbuf)); + else ret.push_back(Pair("withdraws", (char *)"")); + for (baseid=0; baseid<32; baseid++) + { + Object item,obj; + if ( pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,CURRENCIES[baseid]) == 0 ) + { + if ( deposited != 0 || issued != 0 || withdrawn != 0 || approved != 0 || redeemed != 0 ) + { + item.push_back(Pair("available", ValueFromAmount(available))); + item.push_back(Pair("deposited", ValueFromAmount(deposited))); + item.push_back(Pair("issued", ValueFromAmount(issued))); + item.push_back(Pair("withdrawn", ValueFromAmount(withdrawn))); + item.push_back(Pair("approved", ValueFromAmount(approved))); + item.push_back(Pair("redeemed", ValueFromAmount(redeemed))); + obj.push_back(Pair(CURRENCIES[baseid],item)); + a.push_back(obj); + } + } + } + ret.push_back(Pair("fiatstatus", a)); + return ret; +} + Value paxprice(const Array& params, bool fHelp) { if ( fHelp || params.size() < 3 || params.size() > 4 ) @@ -590,29 +693,6 @@ Value gettxout(const Array& params, bool fHelp) return ret; } -int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n) -{ - int32_t i,m; uint8_t *ptr; - LOCK(cs_main); - CCoins coins; - if ( 1 ) - { - LOCK(mempool.cs); - CCoinsViewMemPool view(pcoinsTip,mempool); - if ( view.GetCoins(txid,coins) == 0 ) - return(-1); - mempool.pruneSpent(txid, coins); // TODO: this should be done by the CCoinsViewMemPool - } else if ( pcoinsTip->GetCoins(txid,coins) == 0 ) - return(-1); - if ( n < 0 || (unsigned int)n >= coins.vout.size() || coins.vout[n].IsNull() ) - return(-1); - ptr = (uint8_t *)coins.vout[n].scriptPubKey.data(); - m = coins.vout[n].scriptPubKey.size(); - for (i=0; i 2) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 30142ee82..7375bbdcd 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -108,7 +108,9 @@ static const CRPCConvertParam vRPCConvertParams[] = { "z_importkey", 1 }, { "paxprice", 4 }, { "paxprices", 3 }, + { "paxpending", 0 }, { "notaries", 1 }, + { "minerids", 1 }, }; class CRPCConvertTable diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 9cc9d4d12..a635782a8 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -40,12 +40,14 @@ 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); +int32_t komodo_whoami(char *pubkeystr,int32_t height); 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 +100,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)); @@ -116,6 +121,12 @@ Value getinfo(const Array& params, bool fHelp) #endif obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); obj.push_back(Pair("errors", GetWarnings("statusbar"))); + { + char pubkeystr[65]; int32_t notaryid; + notaryid = komodo_whoami(pubkeystr,longestchain); + obj.push_back(Pair("notaryid", notaryid)); + obj.push_back(Pair("pubkey", pubkeystr)); + } return obj; } diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index e3d264a57..7e05c6efd 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -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 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; diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 4b5cdae35..313cfea2c 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -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))); } @@ -253,6 +253,56 @@ Value getrawtransaction(const Array& params, bool fHelp) return result; } +int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n) +{ + int32_t i,m; uint8_t *ptr; + LOCK(cs_main); + /*CCoins coins; + for (iter=0; iter<2; iter++) + { + if ( iter == 0 ) + { + LOCK(mempool.cs); + CCoinsViewMemPool view(pcoinsTip,mempool); + if ( view.GetCoins(txid,coins) == 0 ) + { + //fprintf(stderr,"cant get view\n"); + continue; + } + mempool.pruneSpent(txid, coins); // TODO: this should be done by the CCoinsViewMemPool + } + else if ( pcoinsTip->GetCoins(txid,coins) == 0 ) + { + //fprintf(stderr,"cant get pcoinsTip->GetCoins\n"); + continue; + } + if ( n < 0 || (unsigned int)n >= coins.vout.size() || coins.vout[n].IsNull() ) + { + fprintf(stderr,"iter.%d n.%d vs voutsize.%d\n",iter,n,(int32_t)coins.vout.size()); + continue; + } + ptr = (uint8_t *)coins.vout[n].scriptPubKey.data(); + m = coins.vout[n].scriptPubKey.size(); + for (i=0; i nMaxDatacarrierBytes) + { + fprintf(stderr,"size.%d > nMaxDatacarrier.%d\n",(int32_t)vch1.size(),(int32_t)nMaxDatacarrierBytes); break; + } } else if (opcode1 != opcode2 || vch1 != vch2) { @@ -202,7 +205,6 @@ bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType) if (m < 1 || m > n) return false; } - return whichType != TX_NONSTANDARD; } diff --git a/src/util.cpp b/src/util.cpp index a586a7516..4dfe13919 100644 --- a/src/util.cpp +++ b/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 diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index b103f0ce7..766e2afda 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -83,7 +83,7 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn) TryCreateDirectory(pathLogDir); boost::filesystem::path pathErrorFile = pathIn / "db.log"; LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s pathIn.(%s)\n", pathLogDir.string(), pathErrorFile.string(),pathIn.string()); - fprintf(stderr,"strPath.(%s)\n",strPath.c_str()); + //fprintf(stderr,"strPath.(%s)\n",strPath.c_str()); unsigned int nEnvFlags = 0; if (GetBoolArg("-privdb", true)) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1f2cb706b..b98e221a3 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -481,17 +481,19 @@ 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); +int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base); Value paxdeposit(const Array& params, bool fHelp) { - uint64_t seed,komodoshis = 0; int32_t height; char destaddr[64]; uint8_t i,pubkey37[33]; + uint64_t available,deposited,issued,withdrawn,approved,redeemed,seed,komodoshis = 0; int32_t height; char destaddr[64]; uint8_t i,pubkey37[33]; bool fSubtractFeeFromAmount = false; if ( komodo_is_issuer() != 0 ) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "paxdeposit only from KMD"); 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()) @@ -500,14 +502,16 @@ Value paxdeposit(const Array& params, bool fHelp) std::string base = params[2].get_str(); std::string dest; height = chainActive.Tip()->nHeight; + if ( pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,(char *)base.c_str()) != 0 || available < fiatoshis ) + throw runtime_error("paxdeposit not enough available inventory"); komodoshis = PAX_fiatdest(&seed,0,destaddr,pubkey37,(char *)params[0].get_str().c_str(),height,(char *)base.c_str(),fiatoshis); dest.append(destaddr); CBitcoinAddress destaddress(CRYPTO777_KMDADDR); if (!destaddress.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid dest Bitcoin address"); for (i=0; i<33; i++) - printf("%02x",pubkey37[i]); - printf(" ht.%d srcaddr.(%s) %s fiatoshis.%lld -> dest.(%s) komodoshis.%llu seed.%llx\n",height,(char *)params[0].get_str().c_str(),(char *)base.c_str(),(long long)fiatoshis,destaddr,(long long)komodoshis,(long long)seed); + fprintf(stderr,"%02x",pubkey37[i]); + fprintf(stderr," ht.%d srcaddr.(%s) %s fiatoshis.%lld -> dest.(%s) komodoshis.%llu seed.%llx\n",height,(char *)params[0].get_str().c_str(),(char *)base.c_str(),(long long)fiatoshis,destaddr,(long long)komodoshis,(long long)seed); EnsureWalletIsUnlocked(); CWalletTx wtx; uint8_t opretbuf[64]; int32_t opretlen; uint64_t fee = komodoshis / 1000; @@ -521,22 +525,20 @@ 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); if (!EnsureWalletIsAvailable(fHelp)) return 0; if (fHelp || params.size() != 2) - throw runtime_error("paxwithdraw \"address\" fiatamount"); - if ( KOMODO_REALTIME == 0 ) + throw runtime_error("paxwithdraw address fiatamount"); + 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); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 0ef2bb723..9d9712166 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -748,6 +748,7 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, void CWallet::DecrementNoteWitnesses() { + extern int32_t KOMODO_REWIND; { LOCK(cs_wallet); for (std::pair& wtxItem : mapWallet) { @@ -770,12 +771,13 @@ void CWallet::DecrementNoteWitnesses() } } // TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302) - assert(nWitnessCacheSize > 0); if ( nWitnessCacheSize <= 0 ) { extern char ASSETCHAINS_SYMBOL[16]; fprintf(stderr,"%s nWitnessCacheSize.%d\n",ASSETCHAINS_SYMBOL,(int32_t)nWitnessCacheSize); } + if ( KOMODO_REWIND == 0 ) + assert(nWitnessCacheSize > 0); if (fFileBacked) { CWalletDB walletdb(strWalletFile); WriteWitnessCache(walletdb);