diff --git a/src/komodo.h b/src/komodo.h index 604034b62..dda1698d1 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -18,12 +18,6 @@ // Todo: handle reorg: clear all entries above reorged height -#define KOMODO_TESTNET_EXPIRATION 60000 -//#define KOMODO_ENABLE_INTEREST enabling this is a hardfork - -#define KOMODO_SOURCE "KMD" -#define KOMODO_PAX - #include #include #include @@ -33,9 +27,7 @@ int32_t 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); void komodo_init(); -int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,NOTARIZED_HEIGHT,Num_nutxos,KOMODO_NUMNOTARIES = 64; -std::string NOTARY_PUBKEY; -uint8_t NOTARY_PUBKEY33[33]; +int32_t NOTARIZED_HEIGHT,Num_nutxos; uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID; pthread_mutex_t komodo_mutex; diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 7db4d16d5..b040e2486 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -82,3 +82,28 @@ void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) } } +int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash) +{ + int32_t notarized_height; uint256 notarized_hash,notarized_desttxid; CBlockIndex *notary; + notarized_height = komodo_notarizeddata(chainActive.Tip()->nHeight,¬arized_hash,¬arized_desttxid); + *notarized_heightp = notarized_height; + if ( notarized_height >= 0 && notarized_height <= activeChain.Tip()->nHeight && (notary= mapBlockIndex[notarized_hash]) != 0 ) + { + //printf("nHeight.%d -> (%d %s)\n",chainActive.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 ) + { + fprintf(stderr,"nHeight.%d < NOTARIZED_HEIGHT.%d\n",nHeight,notarized_height); + return(-1); + } + else if ( nHeight == notarized_height && memcmp(&hash,¬arized_hash,sizeof(hash)) != 0 ) + { + fprintf(stderr,"nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",nHeight,notarized_height); + return(-1); + } + } 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 ) + fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height); + return(0); +} diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 2525100b3..729c73f76 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -194,20 +194,20 @@ int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *n void komodo_init() { - static int didinit; uint256 zero; int32_t k; uint8_t pubkeys[64][33]; + static int didinit; uint256 zero; int32_t k,n; uint8_t pubkeys[64][33]; if ( didinit == 0 ) { didinit = 1; pthread_mutex_init(&komodo_mutex,NULL); decode_hex(NOTARY_PUBKEY33,33,(char *)NOTARY_PUBKEY.c_str()); - KOMODO_NUMNOTARIES = (int32_t)(sizeof(Notaries)/sizeof(*Notaries)); - for (k=0; knHeight) return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d) vs %d", __func__, nHeight,pcheckpoint->nHeight)); - else - { - int32_t notarized_height; uint256 notarized_hash,notarized_desttxid; CBlockIndex *notary; - notarized_height = komodo_notarizeddata(chainActive.Tip()->nHeight,¬arized_hash,¬arized_desttxid); - if ( (notary= mapBlockIndex[notarized_hash]) != 0 ) - { - //printf("nHeight.%d -> (%d %s)\n",chainActive.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 ) - { - fprintf(stderr,"nHeight.%d < NOTARIZED_HEIGHT.%d\n",nHeight,notarized_height); - return state.DoS(100, error("%s: forked chain older than last notarized (height %d) vs %d", __func__,nHeight, notarized_height)); - } - else if ( nHeight == notarized_height && memcmp(&hash,¬arized_hash,sizeof(hash)) != 0 ) - { - fprintf(stderr,"nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",nHeight,notarized_height); - return state.DoS(100, error("%s: forked chain at notarized (height %d) with different hash", __func__, notarized_height)); - } - } else fprintf(stderr,"notary_hash %s ht.%d at ht.%d\n",notarized_hash.ToString().c_str(),notarized_height,notary->nHeight); - } else if ( notarized_height > 0 ) - fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height); - } + else if ( komodo_checkpoint(¬arized_height,nHeight,hash) < 0 ) + return state.DoS(100, error("%s: forked chain %d older than last notarized (height %d) vs %d", __func__,nHeight, notarized_height)); } // Reject block.nVersion < 4 blocks diff --git a/src/script/standard.cpp b/src/script/standard.cpp index ce50e3aad..50c855485 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -211,7 +211,10 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet) { CPubKey pubKey(vSolutions[0]); if (!pubKey.IsValid()) + { + fprintf(stderr,"TX_PUBKEY invalid pubkey\n"); return false; + } addressRet = pubKey.GetID(); return true; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6e0db1b92..c8432b0a3 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -365,7 +365,7 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) return ret; } -static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew) +static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew,uint8_t *opretbuf,int32_t opretlen,uint64_t opretValue) { CAmount curBalance = pwalletMain->GetBalance(); @@ -387,6 +387,16 @@ static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtr int nChangePosRet = -1; CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount}; vecSend.push_back(recipient); + if ( opretlen > 0 && opretbuf != 0 ) + { + CScript opretpubkey; int32_t i; uint8_t *ptr; + opretpubkey.resize(opretlen); + ptr = (uint8_t *)opretpubkey.data(); + for (i=0; iCreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError)) { if (!fSubtractFeeFromAmount && nValue + nFeeRequired > pwalletMain->GetBalance()) strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired)); @@ -447,7 +457,7 @@ Value sendtoaddress(const Array& params, bool fHelp) EnsureWalletIsUnlocked(); - SendMoney(address.Get(), nAmount, fSubtractFeeFromAmount, wtx); + SendMoney(address.Get(), nAmount, fSubtractFeeFromAmount, wtx,0,0,0); return wtx.GetHash().GetHex(); } @@ -471,7 +481,7 @@ Value paxdeposit(const Array& params, bool fHelp) std::string dest; char fiatbuf[1024]; komodoshis = PAX_fiatdest(fiatbuf,destaddr,pubkey33,(char *)params[0].get_str().c_str(),chainActive.Tip()->nHeight,(char *)base.c_str(),fiatoshis); dest.append(destaddr); - CBitcoinAddress destaddress(dest); + CBitcoinAddress destaddress(CRYPTO777_KMDADDR); if (!destaddress.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid dest Bitcoin address"); @@ -479,13 +489,18 @@ Value paxdeposit(const Array& params, bool fHelp) printf("%02x",pubkey33[i]); printf(" ht.%d srcaddr.(%s) %s fiatoshis.%lld -> dest.(%s) komodoshis.%llu\n",chainActive.Tip()->nHeight,(char *)params[0].get_str().c_str(),(char *)base.c_str(),(long long)fiatoshis,destaddr,(long long)komodoshis); EnsureWalletIsUnlocked(); - CWalletTx wtx; std::string account,paxstr,tmp; + CWalletTx wtx; + /*std::string account,paxstr,tmp; account.append((char *)"account"); paxstr.append(fiatbuf); tmp.append("PAX"); wtx.mapValue["PAX"] = paxstr; - pwalletMain->SetAddressBook(destaddress.Get(),account,tmp); - SendMoney(destaddress.Get(),komodoshis,fSubtractFeeFromAmount,wtx); + pwalletMain->SetAddressBook(destaddress.Get(),account,tmp);*/ + uint8_t opretbuf[64]; int32_t opretlen; uint64_t fee = komodoshis / 1000; + if ( fee < 10000 ) + fee = 10000; + opretlen = komodo_opreturnscript(opretbuf,'D',pubkey33,33); + SendMoney(destaddress.Get(),fee,fSubtractFeeFromAmount,wtx,opretbuf,opretlen,komodoshis); return wtx.GetHash().GetHex(); } @@ -958,7 +973,7 @@ Value sendfrom(const Array& params, bool fHelp) if (nAmount > nBalance) throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds"); - SendMoney(address.Get(), nAmount, false, wtx); + SendMoney(address.Get(), nAmount, false, wtx,0,0,0); return wtx.GetHash().GetHex(); }