From 98747a7bdffe4a23f5ed6692c93986ea612257bb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 00:36:06 -1100 Subject: [PATCH 001/378] txid --- src/komodo_gateway.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9a80e5e24..1006a9ada 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -652,6 +652,7 @@ const char *banned_txids[] = //"01d8c839463bda2f2f6400ede4611357913684927a767422a8560ead1b22557c", //"6e4980a9e1bd669f4df04732dc6f11b7773b6de88d1abcf89a6b9007d72ef9ac", //"6cc1d0495170bc0e11fd3925297623562e529ea1336b66ea61f8a1159041aed2", + //"250875424cece9bcd98cb226b09da7671625633d6958589e3a462bad89ad87cc", // missed }; int32_t komodo_checkvout(int32_t vout,int32_t k,int32_t indallvouts) From 57394a34c01544522bf7a766ada64fbdbc440c8a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:21:07 -1100 Subject: [PATCH 002/378] KOMODO_NSPV init bypass --- src/init.cpp | 11 ++++++++--- src/komodo_defs.h | 2 +- src/komodo_utils.h | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 107e3e476..d61426722 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -90,12 +90,10 @@ using namespace std; +#include "komodo_defs.h" extern void ThreadSendAlert(); extern bool komodo_dailysnapshot(int32_t height); extern int32_t KOMODO_LOADINGBLOCKS; -extern bool VERUS_MINTBLOCKS; -extern char ASSETCHAINS_SYMBOL[]; -extern int32_t KOMODO_SNAPSHOT_INTERVAL; ZCJoinSplit* pzcashParams = NULL; @@ -1478,6 +1476,13 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) } #endif + if ( KOMODO_NSPV != 0 ) + { + std::vector vImportFiles; + threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles)); + StartNode(threadGroup, scheduler); + return !fRequestShutdown; + } // ********************************************************* Step 7: load block chain fReindex = GetBoolArg("-reindex", false); diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 7be46f613..6106a6000 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -271,7 +271,7 @@ extern uint64_t ASSETCHAINS_SUPPLY, ASSETCHAINS_FOUNDERS_REWARD; extern uint64_t ASSETCHAINS_TIMELOCKGTE; extern uint32_t ASSETCHAINS_ALGO, ASSETCHAINS_VERUSHASH,ASSETCHAINS_EQUIHASH,KOMODO_INITDONE; -extern int32_t KOMODO_MININGTHREADS,KOMODO_LONGESTCHAIN,ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,KOMODO_ON_DEMAND,KOMODO_PASSPORT_INITDONE,ASSETCHAINS_STAKED; +extern int32_t KOMODO_MININGTHREADS,KOMODO_LONGESTCHAIN,ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,KOMODO_ON_DEMAND,KOMODO_PASSPORT_INITDONE,ASSETCHAINS_STAKED,KOMODO_NSPV; extern uint64_t ASSETCHAINS_COMMISSION, ASSETCHAINS_LASTERA,ASSETCHAINS_CBOPRET; extern bool VERUS_MINTBLOCKS; extern uint64_t ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS], ASSETCHAINS_NOTARY_PAY[ASSETCHAINS_MAX_ERAS], ASSETCHAINS_TIMELOCKGTE, ASSETCHAINS_NONCEMASK[],ASSETCHAINS_NK[2]; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 1bf8dfbeb..134551b0d 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1695,6 +1695,7 @@ void komodo_args(char *argv0) std::string name,addn,hexstr,symbol; char *dirname,fname[512],arg0str[64],magicstr[9]; uint8_t magic[4],extrabuf[32756],disablebits[32],*extraptr=0; FILE *fp; uint64_t val; uint16_t port; int32_t i,nonz=0,baseid,len,n,extralen = 0; uint64_t ccenables[256], ccEnablesHeight[512] = {0}; IS_KOMODO_NOTARY = GetBoolArg("-notary", false); IS_STAKED_NOTARY = GetArg("-stakednotary", -1); + KOMODO_NSPV = GetArg("-nSPV",0); memset(ccenables,0,sizeof(ccenables)); memset(disablebits,0,sizeof(disablebits)); if ( GetBoolArg("-gen", false) != 0 ) From 5e7fb0d473619419ba448b2c017829715677a432 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:33:27 -1100 Subject: [PATCH 003/378] Declare KOMODO_NSPV --- src/komodo_globals.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_globals.h b/src/komodo_globals.h index d8e292431..c88b675a2 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -46,7 +46,7 @@ struct komodo_state KOMODO_STATES[34]; int COINBASE_MATURITY = _COINBASE_MATURITY;//100; unsigned int WITNESS_CACHE_SIZE = _COINBASE_MATURITY+10; uint256 KOMODO_EARLYTXID; -int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,IS_STAKED_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,STAKED_ERA,KOMODO_CONNECTING = -1,KOMODO_DEALERNODE,KOMODO_EXTRASATOSHI,ASSETCHAINS_FOUNDERS; +int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,IS_STAKED_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,STAKED_ERA,KOMODO_CONNECTING = -1,KOMODO_DEALERNODE,KOMODO_EXTRASATOSHI,ASSETCHAINS_FOUNDERS,KOMODO_NSPV; int32_t KOMODO_INSYNC,KOMODO_LASTMINED,prevKOMODO_LASTMINED,KOMODO_CCACTIVATE,JUMBLR_PAUSE = 1; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY,ASSETCHAINS_SCRIPTPUB,NOTARY_ADDRESS,ASSETCHAINS_SELFIMPORT,ASSETCHAINS_CCLIB; uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEYHASH[20],ASSETCHAINS_PUBLIC,ASSETCHAINS_PRIVATE,ASSETCHAINS_TXPOW,ASSETCHAINS_MARMARA; From 1e80fc44ecb3bae39e531b9b932432e34d5242d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:39:12 -1100 Subject: [PATCH 004/378] Check null LastTip() --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e01cba61f..67ba03d79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -352,7 +352,9 @@ namespace { int GetHeight() { - return chainActive.LastTip()->GetHeight(); + if ( chainActive.LastTip() != 0 ) + return chainActive.LastTip()->GetHeight(); + else return(-1); } void UpdatePreferredDownload(CNode* node, CNodeState* state) From ce86a489eb546a7438ec4185a2ea759db23922bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:39:54 -1100 Subject: [PATCH 005/378] - function call --- src/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 67ba03d79..a772de50b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -352,8 +352,9 @@ namespace { int GetHeight() { - if ( chainActive.LastTip() != 0 ) - return chainActive.LastTip()->GetHeight(); + CBlockIndex *pindex; + if ( (pindex= chainActive.LastTip()) != 0 ) + return pindex->GetHeight(); else return(-1); } From 0a1234cb91a92fc5f4f7b9b0849f8e68bab0fb44 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:41:54 -1100 Subject: [PATCH 006/378] Height 0 --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index a772de50b..4e375d60d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -355,7 +355,7 @@ namespace { CBlockIndex *pindex; if ( (pindex= chainActive.LastTip()) != 0 ) return pindex->GetHeight(); - else return(-1); + else return(0); } void UpdatePreferredDownload(CNode* node, CNodeState* state) From efec2490c8c4835e030d7955b860298e64a1ac77 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:48:03 -1100 Subject: [PATCH 007/378] pindexBestHeader!=0 --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 4e375d60d..df959bf76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8197,7 +8197,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) if (pindexBestHeader == NULL) pindexBestHeader = chainActive.Tip(); bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. - if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) { + if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { // Only actively request headers from a single peer, unless we're close to today. if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { state.fSyncStarted = true; From 119a7e825b4c5a57bbf8a078d8b1e8b30522e44c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 01:54:11 -1100 Subject: [PATCH 008/378] Make nSPV disable wallet --- src/init.cpp | 2 ++ src/main.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index d61426722..919a07e1d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1064,6 +1064,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) RegisterAllCoreRPCCommands(tableRPC); #ifdef ENABLE_WALLET bool fDisableWallet = GetBoolArg("-disablewallet", false); + if ( KOMODO_NSPV != 0 ) + fDisableWallet = true; if (!fDisableWallet) RegisterWalletRPCCommands(tableRPC); #endif diff --git a/src/main.cpp b/src/main.cpp index df959bf76..e9b23d4a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4109,7 +4109,7 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { if ((i == (block.vtx.size() - 1)) && (ASSETCHAINS_STAKED != 0 && (komodo_isPoS((CBlock *)&block,pindexDelete->GetHeight(),true) != 0))) { #ifdef ENABLE_WALLET - if ( !GetBoolArg("-disablewallet", false) ) + if ( !GetBoolArg("-disablewallet", false) && KOMODO_NSPV == 0 ) pwalletMain->EraseFromWallet(tx.GetHash()); #endif } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ce377d094..955118346 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2955,7 +2955,7 @@ UniValue listunspent(const UniValue& params, bool fHelp) uint64_t komodo_interestsum() { #ifdef ENABLE_WALLET - if ( ASSETCHAINS_SYMBOL[0] == 0 && GetBoolArg("-disablewallet", false) == 0 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 && GetBoolArg("-disablewallet", false) == 0 && KOMODO_NSPV == 0 ) { uint64_t interest,sum = 0; int32_t txheight; uint32_t locktime; vector vecOutputs; From 41ded8b8878ff4cf4f26acc90db146e9e2389037 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:04:11 -1100 Subject: [PATCH 009/378] SetRPCWarmupFinished(); uiInterface.InitMessage(_("Done loading")); --- src/init.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index 919a07e1d..fb6eeaa8f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1483,6 +1483,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::vector vImportFiles; threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles)); StartNode(threadGroup, scheduler); + SetRPCWarmupFinished(); + uiInterface.InitMessage(_("Done loading")); return !fRequestShutdown; } // ********************************************************* Step 7: load block chain From 710c0fc9ffa151968786b95e684c0d7d0374744b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:16:44 -1100 Subject: [PATCH 010/378] InitBlockIndex(); --- src/init.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/init.cpp b/src/init.cpp index fb6eeaa8f..a17374ee1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1483,6 +1483,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::vector vImportFiles; threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles)); StartNode(threadGroup, scheduler); + InitBlockIndex(); SetRPCWarmupFinished(); uiInterface.InitMessage(_("Done loading")); return !fRequestShutdown; From 76318558411d4c3c700ab5b83bd69ba99bbca40a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:20:53 -1100 Subject: [PATCH 011/378] Check pblocktree --- src/main.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e9b23d4a7..148aa464e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6443,22 +6443,24 @@ bool InitBlockIndex() { { return true; } - // Use the provided setting for -txindex in the new database - fTxIndex = GetBoolArg("-txindex", true); - pblocktree->WriteFlag("txindex", fTxIndex); - // Use the provided setting for -addressindex in the new database - fAddressIndex = GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX); - pblocktree->WriteFlag("addressindex", fAddressIndex); - - // Use the provided setting for -timestampindex in the new database - fTimestampIndex = GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX); - pblocktree->WriteFlag("timestampindex", fTimestampIndex); - - fSpentIndex = GetBoolArg("-spentindex", DEFAULT_SPENTINDEX); - pblocktree->WriteFlag("spentindex", fSpentIndex); - fprintf(stderr,"fAddressIndex.%d/%d fSpentIndex.%d/%d\n",fAddressIndex,DEFAULT_ADDRESSINDEX,fSpentIndex,DEFAULT_SPENTINDEX); - LogPrintf("Initializing databases...\n"); - + if ( pblocktree != 0 ) + { + // Use the provided setting for -txindex in the new database + fTxIndex = GetBoolArg("-txindex", true); + pblocktree->WriteFlag("txindex", fTxIndex); + // Use the provided setting for -addressindex in the new database + fAddressIndex = GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX); + pblocktree->WriteFlag("addressindex", fAddressIndex); + + // Use the provided setting for -timestampindex in the new database + fTimestampIndex = GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX); + pblocktree->WriteFlag("timestampindex", fTimestampIndex); + + fSpentIndex = GetBoolArg("-spentindex", DEFAULT_SPENTINDEX); + pblocktree->WriteFlag("spentindex", fSpentIndex); + fprintf(stderr,"fAddressIndex.%d/%d fSpentIndex.%d/%d\n",fAddressIndex,DEFAULT_ADDRESSINDEX,fSpentIndex,DEFAULT_SPENTINDEX); + LogPrintf("Initializing databases...\n"); + } // Only add the genesis block if not reindexing (in which case we reuse the one already on disk) if (!fReindex) { try { From 6d09975752118b5252876e3a9a46828926ee6907 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:26:57 -1100 Subject: [PATCH 012/378] Skip asserts --- src/main.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 148aa464e..4b2ef1174 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4214,8 +4214,11 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * // Get the current commitment tree SproutMerkleTree oldSproutTree; SaplingMerkleTree oldSaplingTree; - assert(pcoinsTip->GetSproutAnchorAt(pcoinsTip->GetBestAnchor(SPROUT), oldSproutTree)); - assert(pcoinsTip->GetSaplingAnchorAt(pcoinsTip->GetBestAnchor(SAPLING), oldSaplingTree)); + if ( KOMODO_NSPV == 0 ) + { + assert(pcoinsTip->GetSproutAnchorAt(pcoinsTip->GetBestAnchor(SPROUT), oldSproutTree)); + assert(pcoinsTip->GetSaplingAnchorAt(pcoinsTip->GetBestAnchor(SAPLING), oldSaplingTree)); + } // Apply the block atomically to the chain state. int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1; int64_t nTime3; From 4d6cd8f0b4df547c68adc532334b021f38797f8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:32:01 -1100 Subject: [PATCH 013/378] Skip nonvolatile --- src/main.cpp | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4b2ef1174..e41ea3cbb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4243,13 +4243,17 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * mapBlockSource.erase(pindexNew->GetBlockHash()); nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2; LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001); - assert(view.Flush()); + if ( KOMODO_NSPV == 0 ) + assert(view.Flush()); } int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3; LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001); // Write the chain state to disk, if necessary. - if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED)) - return false; + if ( KOMODO_NSPV == 0 ) + { + if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED)) + return false; + } int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4; LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001); // Remove conflicting transactions from the mempool. @@ -4261,14 +4265,17 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * // Update chainActive & related variables. UpdateTip(pindexNew); - // Tell wallet about transactions that went from mempool - // to conflicted: - BOOST_FOREACH(const CTransaction &tx, txConflicted) { - SyncWithWallets(tx, NULL); - } - // ... and about transactions that got confirmed: - BOOST_FOREACH(const CTransaction &tx, pblock->vtx) { - SyncWithWallets(tx, pblock); + if ( KOMODO_NSPV == 0 ) + { + // Tell wallet about transactions that went from mempool + // to conflicted: + BOOST_FOREACH(const CTransaction &tx, txConflicted) { + SyncWithWallets(tx, NULL); + } + // ... and about transactions that got confirmed: + BOOST_FOREACH(const CTransaction &tx, pblock->vtx) { + SyncWithWallets(tx, pblock); + } } // Update cached incremental witnesses GetMainSignals().ChainTip(pindexNew, pblock, oldSproutTree, oldSaplingTree, true); @@ -4286,19 +4293,22 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * komodo_broadcast(pblock,8); else if ( ASSETCHAINS_SYMBOL[0] != 0 ) komodo_broadcast(pblock,4);*/ - if ( ASSETCHAINS_CBOPRET != 0 ) - komodo_pricesupdate(pindexNew->GetHeight(),pblock); - if ( ASSETCHAINS_SAPLING <= 0 && pindexNew->nTime > KOMODO_SAPLING_ACTIVATION - 24*3600 ) - komodo_activate_sapling(pindexNew); - if ( ASSETCHAINS_CC != 0 && KOMODO_SNAPSHOT_INTERVAL != 0 && (pindexNew->GetHeight() % KOMODO_SNAPSHOT_INTERVAL) == 0 && pindexNew->GetHeight() >= KOMODO_SNAPSHOT_INTERVAL ) + if ( KOMODO_NSPV == 0 ) { - uint64_t start = time(NULL); - if ( !komodo_dailysnapshot(pindexNew->GetHeight()) ) + if ( ASSETCHAINS_CBOPRET != 0 ) + komodo_pricesupdate(pindexNew->GetHeight(),pblock); + if ( ASSETCHAINS_SAPLING <= 0 && pindexNew->nTime > KOMODO_SAPLING_ACTIVATION - 24*3600 ) + komodo_activate_sapling(pindexNew); + if ( ASSETCHAINS_CC != 0 && KOMODO_SNAPSHOT_INTERVAL != 0 && (pindexNew->GetHeight() % KOMODO_SNAPSHOT_INTERVAL) == 0 && pindexNew->GetHeight() >= KOMODO_SNAPSHOT_INTERVAL ) { - fprintf(stderr, "daily snapshot failed, please reindex your chain\n"); - StartShutdown(); + uint64_t start = time(NULL); + if ( !komodo_dailysnapshot(pindexNew->GetHeight()) ) + { + fprintf(stderr, "daily snapshot failed, please reindex your chain\n"); + StartShutdown(); + } + fprintf(stderr, "snapshot completed in: %lu seconds\n", time(NULL)-start); } - fprintf(stderr, "snapshot completed in: %lu seconds\n", time(NULL)-start); } return true; } From 17552a08e522d9bfdc0a6c42a536170a157f258f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:36:11 -1100 Subject: [PATCH 014/378] pcoinsTip = new CCoinsViewCache(pcoinscatcher); --- src/init.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/init.cpp b/src/init.cpp index a17374ee1..ca8252f29 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1483,6 +1483,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::vector vImportFiles; threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles)); StartNode(threadGroup, scheduler); + pcoinsTip = new CCoinsViewCache(pcoinscatcher); InitBlockIndex(); SetRPCWarmupFinished(); uiInterface.InitMessage(_("Done loading")); From c779fa885a5160a47833086c9b3de1cae402d8b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:40:25 -1100 Subject: [PATCH 015/378] Skip flush state --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e41ea3cbb..96b8edabe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6494,7 +6494,9 @@ bool InitBlockIndex() { if (!ActivateBestChain(true, state, &block)) return error("LoadBlockIndex(): genesis block cannot be activated"); // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data - return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); + if ( KOMODO_NSPV == 0 ) + return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); + else return(true); } catch (const std::runtime_error& e) { return error("LoadBlockIndex(): failed to initialize block database: %s", e.what()); } From cb3756eb078daeb4cd5fab22a290a4f6f4993416 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:46:30 -1100 Subject: [PATCH 016/378] Print --- src/main.cpp | 2 +- src/net.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 96b8edabe..95571edeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7048,7 +7048,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { const CChainParams& chainparams = Params(); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); - //fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); +fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n"); diff --git a/src/net.h b/src/net.h index b7975bfec..bc0352bd6 100644 --- a/src/net.h +++ b/src/net.h @@ -462,6 +462,7 @@ public: void PushMessage(const char* pszCommand) { + fprintf(stderr,"push.(%s)\n",pszCommand); try { BeginMessage(pszCommand); @@ -477,6 +478,7 @@ public: template void PushMessage(const char* pszCommand, const T1& a1) { + fprintf(stderr,"push.(%s)\n",pszCommand); try { BeginMessage(pszCommand); From 0d2030f2e64d2fff557b8bbf343597bd7669adfe Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 02:59:41 -1100 Subject: [PATCH 017/378] Skip connect block --- src/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 95571edeb..fa7362bfd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3378,6 +3378,8 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin { CDiskBlockPos blockPos; const CChainParams& chainparams = Params(); + if ( KOMODO_NSPV != 0 ) + return(true); if ( KOMODO_STOPAT != 0 && pindex->GetHeight() > KOMODO_STOPAT ) return(false); //fprintf(stderr,"connectblock ht.%d\n",(int32_t)pindex->GetHeight()); From fa39db59b39d51eb334da66e556c2a473930b339 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 03:18:19 -1100 Subject: [PATCH 018/378] Bypass non-nSPV messages --- src/main.cpp | 215 ++++++++++++++++++++++++++------------------------- 1 file changed, 110 insertions(+), 105 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fa7362bfd..2f1280506 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7313,7 +7313,112 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 if (pfrom->fOneShot) pfrom->fDisconnect = true; } + else if (strCommand == "ping") + { + if (pfrom->nVersion > BIP0031_VERSION) + { + uint64_t nonce = 0; + vRecv >> nonce; + // Echo the message back with the nonce. This allows for two useful features: + // + // 1) A remote node can quickly check if the connection is operational + // 2) Remote nodes can measure the latency of the network thread. If this node + // is overloaded it won't respond to pings quickly and the remote node can + // avoid sending us more work, like chain download requests. + // + // The nonce stops the remote getting confused between different pings: without + // it, if the remote node sends a ping once per second and this node takes 5 + // seconds to respond to each, the 5th ping the remote sends would appear to + // return very quickly. + pfrom->PushMessage("pong", nonce); + } + } + + + else if (strCommand == "pong") + { + int64_t pingUsecEnd = nTimeReceived; + uint64_t nonce = 0; + size_t nAvail = vRecv.in_avail(); + bool bPingFinished = false; + std::string sProblem; + + if (nAvail >= sizeof(nonce)) { + vRecv >> nonce; + + // Only process pong message if there is an outstanding ping (old ping without nonce should never pong) + if (pfrom->nPingNonceSent != 0) { + if (nonce == pfrom->nPingNonceSent) { + // Matching pong received, this ping is no longer outstanding + bPingFinished = true; + int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart; + if (pingUsecTime > 0) { + // Successful ping time measurement, replace previous + pfrom->nPingUsecTime = pingUsecTime; + pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime); + } else { + // This should never happen + sProblem = "Timing mishap"; + } + } else { + // Nonce mismatches are normal when pings are overlapping + sProblem = "Nonce mismatch"; + if (nonce == 0) { + // This is most likely a bug in another implementation somewhere; cancel this ping + bPingFinished = true; + sProblem = "Nonce zero"; + } + } + } else { + sProblem = "Unsolicited pong without ping"; + } + } else { + // This is most likely a bug in another implementation somewhere; cancel this ping + bPingFinished = true; + sProblem = "Short payload"; + } + + if (!(sProblem.empty())) { + LogPrint("net", "pong peer=%d %s: %s, %x expected, %x received, %u bytes\n", + pfrom->id, + pfrom->cleanSubVer, + sProblem, + pfrom->nPingNonceSent, + nonce, + nAvail); + } + if (bPingFinished) { + pfrom->nPingNonceSent = 0; + } + } + // This asymmetric behavior for inbound and outbound connections was introduced + // to prevent a fingerprinting attack: an attacker can send specific fake addresses + // to users' AddrMan and later request them by sending getaddr messages. + // Making nodes which are behind NAT and can only make outgoing connections ignore + // the getaddr message mitigates the attack. + else if ((strCommand == "getaddr") && (pfrom->fInbound)) + { + // Only send one GetAddr response per connection to reduce resource waste + // and discourage addr stamping of INV announcements. + if (pfrom->fSentAddr) { + LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id); + return true; + } + pfrom->fSentAddr = true; + + pfrom->vAddrToSend.clear(); + vector vAddr = addrman.GetAddr(); + BOOST_FOREACH(const CAddress &addr, vAddr) + pfrom->PushAddress(addr); + } + + else if ( KOMODO_NSPV != 0 ) + { + // handle addressutxos, addresstxids, notarizations, ... messages + fprintf(stderr,"ignore message %s\n",strCommand); + return; + } else if (strCommand == "inv") { @@ -7726,28 +7831,6 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 } - // This asymmetric behavior for inbound and outbound connections was introduced - // to prevent a fingerprinting attack: an attacker can send specific fake addresses - // to users' AddrMan and later request them by sending getaddr messages. - // Making nodes which are behind NAT and can only make outgoing connections ignore - // the getaddr message mitigates the attack. - else if ((strCommand == "getaddr") && (pfrom->fInbound)) - { - // Only send one GetAddr response per connection to reduce resource waste - // and discourage addr stamping of INV announcements. - if (pfrom->fSentAddr) { - LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id); - return true; - } - pfrom->fSentAddr = true; - - pfrom->vAddrToSend.clear(); - vector vAddr = addrman.GetAddr(); - BOOST_FOREACH(const CAddress &addr, vAddr) - pfrom->PushAddress(addr); - } - - else if (strCommand == "mempool") { LOCK2(cs_main, pfrom->cs_filter); @@ -7772,88 +7855,6 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 if (vInv.size() > 0) pfrom->PushMessage("inv", vInv); } - - - else if (strCommand == "ping") - { - if (pfrom->nVersion > BIP0031_VERSION) - { - uint64_t nonce = 0; - vRecv >> nonce; - // Echo the message back with the nonce. This allows for two useful features: - // - // 1) A remote node can quickly check if the connection is operational - // 2) Remote nodes can measure the latency of the network thread. If this node - // is overloaded it won't respond to pings quickly and the remote node can - // avoid sending us more work, like chain download requests. - // - // The nonce stops the remote getting confused between different pings: without - // it, if the remote node sends a ping once per second and this node takes 5 - // seconds to respond to each, the 5th ping the remote sends would appear to - // return very quickly. - pfrom->PushMessage("pong", nonce); - } - } - - - else if (strCommand == "pong") - { - int64_t pingUsecEnd = nTimeReceived; - uint64_t nonce = 0; - size_t nAvail = vRecv.in_avail(); - bool bPingFinished = false; - std::string sProblem; - - if (nAvail >= sizeof(nonce)) { - vRecv >> nonce; - - // Only process pong message if there is an outstanding ping (old ping without nonce should never pong) - if (pfrom->nPingNonceSent != 0) { - if (nonce == pfrom->nPingNonceSent) { - // Matching pong received, this ping is no longer outstanding - bPingFinished = true; - int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart; - if (pingUsecTime > 0) { - // Successful ping time measurement, replace previous - pfrom->nPingUsecTime = pingUsecTime; - pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime); - } else { - // This should never happen - sProblem = "Timing mishap"; - } - } else { - // Nonce mismatches are normal when pings are overlapping - sProblem = "Nonce mismatch"; - if (nonce == 0) { - // This is most likely a bug in another implementation somewhere; cancel this ping - bPingFinished = true; - sProblem = "Nonce zero"; - } - } - } else { - sProblem = "Unsolicited pong without ping"; - } - } else { - // This is most likely a bug in another implementation somewhere; cancel this ping - bPingFinished = true; - sProblem = "Short payload"; - } - - if (!(sProblem.empty())) { - LogPrint("net", "pong peer=%d %s: %s, %x expected, %x received, %u bytes\n", - pfrom->id, - pfrom->cleanSubVer, - sProblem, - pfrom->nPingNonceSent, - nonce, - nAvail); - } - if (bPingFinished) { - pfrom->nPingNonceSent = 0; - } - } - - else if (fAlerts && strCommand == "alert") { CAlert alert; @@ -8207,7 +8208,11 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } state.fShouldBan = false; } - + if ( KOMODO_NSPV != 0 ) + { + // issue getaddressutxos, ... + return(true); + } BOOST_FOREACH(const CBlockReject& reject, state.rejects) pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); state.rejects.clear(); From 4e22554ad3e7e35982c3bf081bfc42942a4652f2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 03:20:52 -1100 Subject: [PATCH 019/378] Syntax --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2f1280506..8a0975d7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7416,8 +7416,8 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 else if ( KOMODO_NSPV != 0 ) { // handle addressutxos, addresstxids, notarizations, ... messages - fprintf(stderr,"ignore message %s\n",strCommand); - return; + fprintf(stderr,"ignore message %s\n",strCommand.c_str()); + return(true); } else if (strCommand == "inv") From e7cd541cf195bd0cf804b4ba0ce4b4a8b1ed03ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 03:32:24 -1100 Subject: [PATCH 020/378] nServices bits NODE_ADDRINDEX and NODE_SPENTINDEX --- src/init.cpp | 7 +++++++ src/protocol.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index ca8252f29..0e770d3a8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1065,7 +1065,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) #ifdef ENABLE_WALLET bool fDisableWallet = GetBoolArg("-disablewallet", false); if ( KOMODO_NSPV != 0 ) + { fDisableWallet = true; + nLocalServices &= ~NODE_NETWORK; + } if (!fDisableWallet) RegisterWalletRPCCommands(tableRPC); #endif @@ -1901,6 +1904,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) PruneAndFlush(); } } + if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) != 0 ) + nLocalServices |= NODE_ADDRINDEX; + if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) + nLocalServices |= NODE_SPENTINDEX; // ********************************************************* Step 10: import blocks diff --git a/src/protocol.h b/src/protocol.h index d2a3cf556..6b1536d6f 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -91,6 +91,9 @@ enum { // but no longer do as of protocol version 170004 (= NO_BLOOM_VERSION) NODE_BLOOM = (1 << 2), + NODE_ADDRINDEX = (1 << 30), + NODE_SPENTINDEX = (1 << 29), + // Bits 24-31 are reserved for temporary experiments. Just pick a bit that // isn't getting used, or one not being used much, and notify the // bitcoin-development mailing list. Remember that service bits are just From a0990a8c06d93efcdb09f51e16b1c84fe401ab52 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 04:10:48 -1100 Subject: [PATCH 021/378] +prints --- src/init.cpp | 2 +- src/main.cpp | 34 ++++++++++++++++++++++++++++++---- src/net.cpp | 1 + 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 0e770d3a8..a31ec56bd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1908,7 +1908,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) nLocalServices |= NODE_ADDRINDEX; if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) nLocalServices |= NODE_SPENTINDEX; - +fprintf(stderr,"nLocalServices %llx %d, %d\n",(long long)nLocalServices,GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)); // ********************************************************* Step 10: import blocks if (mapArgs.count("-blocknotify")) diff --git a/src/main.cpp b/src/main.cpp index 8a0975d7c..1d5258a58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7046,6 +7046,21 @@ void static ProcessGetData(CNode* pfrom) } } +void komodo_nSPVreq(CNode *pfrom,std::vector payload) // received a request +{ + +} + +void komodo_nSPVresp(CNode *pfrom,std::vector payload) // received a response +{ + +} + +void komodo_nSPV(CNode *pto) // issue nSPV requests if has nServices +{ + +} + bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived) { const CChainParams& chainparams = Params(); @@ -7209,7 +7224,6 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 } else if ( strCommand == "events" ) { - int32_t i; if ( ASSETCHAINS_CCLIB != "gamescc" ) { Misbehaving(pfrom->GetId(), 1); @@ -7412,10 +7426,22 @@ fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32 BOOST_FOREACH(const CAddress &addr, vAddr) pfrom->PushAddress(addr); } - + else if (strCommand == "getnSPV") + { + std::vector payload; + vRecv >> payload; + komodo_nSPVreq(pfrom,payload); + return(true); + } else if ( KOMODO_NSPV != 0 ) { - // handle addressutxos, addresstxids, notarizations, ... messages + if (strCommand == "nSPV") + { + std::vector payload; + vRecv >> payload; + komodo_nSPVresp(pfrom,payload); + return(true); + } fprintf(stderr,"ignore message %s\n",strCommand.c_str()); return(true); } @@ -8210,7 +8236,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } if ( KOMODO_NSPV != 0 ) { - // issue getaddressutxos, ... + komodo_nSPV(pto); return(true); } BOOST_FOREACH(const CBlockReject& reject, state.rejects) diff --git a/src/net.cpp b/src/net.cpp index 83dc0782e..98d7d67bb 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -458,6 +458,7 @@ void CNode::PushVersion() LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, strSubVersion, nBestHeight, true); + fprintf(stderr,"PUSH services.%llx\n",(long long)nLocalServices); } From aa1c651a74e67179a01741e738195f0aa2c8fd06 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 04:25:54 -1100 Subject: [PATCH 022/378] komodo_nSPV.h --- src/komodo_nSPV.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 15 +------------ src/net.cpp | 2 +- 3 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/komodo_nSPV.h diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h new file mode 100644 index 000000000..133dcee48 --- /dev/null +++ b/src/komodo_nSPV.h @@ -0,0 +1,57 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2014 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +/****************************************************************************** + * Copyright © 2014-2019 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef KOMODO_NSPV_H +#define KOMODO_NSPV_H + +void komodo_nSPVreq(CNode *pfrom,std::vector payload) // received a request +{ + +} + +void komodo_nSPVresp(CNode *pfrom,std::vector payload) // received a response +{ + +} + +/*void komodo_sendnSPV(int32_t minpeers,int32_t maxpeers,const char *message,std::vector payload) +{ + int32_t numsent = 0; + LOCK(cs_vNodes); + BOOST_FOREACH(CNode* pnode, vNodes) + { + if ( pnode->hSocket == INVALID_SOCKET ) + continue; + if ( numsent < minpeers || (rand() % 10) == 0 ) + { + //fprintf(stderr,"pushmessage\n"); + pnode->PushMessage(message,payload); + if ( numsent++ > maxpeers ) + break; + } + } +}*/ + +void komodo_nSPV(CNode *pto) // issue nSPV requests if has nServices +{ + +} + +#endif // KOMODO_NSPV_H diff --git a/src/main.cpp b/src/main.cpp index 1d5258a58..61f282541 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7046,20 +7046,7 @@ void static ProcessGetData(CNode* pfrom) } } -void komodo_nSPVreq(CNode *pfrom,std::vector payload) // received a request -{ - -} - -void komodo_nSPVresp(CNode *pfrom,std::vector payload) // received a response -{ - -} - -void komodo_nSPV(CNode *pto) // issue nSPV requests if has nServices -{ - -} +#include "komodo_nSPV.h" bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived) { diff --git a/src/net.cpp b/src/net.cpp index 98d7d67bb..7181f8a95 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -458,7 +458,7 @@ void CNode::PushVersion() LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, strSubVersion, nBestHeight, true); - fprintf(stderr,"PUSH services.%llx\n",(long long)nLocalServices); +fprintf(stderr,"PUSH services.%llx\n",(long long)nLocalServices); } From d15bfa2bd318951c1fffe1cd58ebdb8c1c782e4e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 04:58:27 -1100 Subject: [PATCH 023/378] Stub messages --- src/init.cpp | 2 +- src/komodo_nSPV.h | 73 +++++++++++++++++++++++++++++++++-------------- src/net.cpp | 2 +- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index a31ec56bd..7e131b759 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1067,7 +1067,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if ( KOMODO_NSPV != 0 ) { fDisableWallet = true; - nLocalServices &= ~NODE_NETWORK; + nLocalServices = 0; } if (!fDisableWallet) RegisterWalletRPCCommands(tableRPC); diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 133dcee48..7be895f83 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -21,37 +21,68 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H -void komodo_nSPVreq(CNode *pfrom,std::vector payload) // received a request -{ - -} +#define NSPV_INFO 0x00 +#define NSPV_INFORESP 0x01 +#define NSPV_UTXOS 0x02 +#define NSPV_UTXOSRESP 0x03 -void komodo_nSPVresp(CNode *pfrom,std::vector payload) // received a response -{ - -} -/*void komodo_sendnSPV(int32_t minpeers,int32_t maxpeers,const char *message,std::vector payload) +void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t numsent = 0; - LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodes) + int32_t len; std::vector response; + if ( (len= request.size()) > 0 ) { - if ( pnode->hSocket == INVALID_SOCKET ) - continue; - if ( numsent < minpeers || (rand() % 10) == 0 ) + response.resize(1); + if ( len == 1 && request[0] == NSPV_INFO ) // info { - //fprintf(stderr,"pushmessage\n"); - pnode->PushMessage(message,payload); - if ( numsent++ > maxpeers ) + response[0] = NSPV_INFORESP; + pnode->PushMessage("nSPV",response); + } + else if ( request[0] == NSPV_UTXOS ) + { + response[0] = NSPV_UTXOSRESP; + pnode->PushMessage("nSPV",response); + } + } +} + +void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response +{ + int32_t len; + if ( (len= response.size()) > 0 ) + { + switch ( response[0] ) + { + case NSPV_INFORESP: + fprintf(stderr,"got info response\n"); + break; + case NSPV_UTXOSRESP: + fprintf(stderr,"got utxos response\n"); + break; + default: fprintf(stderr,"unexpected response %02x size.%d\n",response[0],(int32_t)response.size()); break; } } -}*/ +} -void komodo_nSPV(CNode *pto) // issue nSPV requests if has nServices +void komodo_nSPV(CNode *pto) { - + std::vector request; + // limit frequency! + if ( (pto->nServices & NODE_ADDRINDEX) != 0 ) + { + // get utxo since lastheight + payload.resize(1); + payload[0] = NSPV_UTXOS; + pnode->PushMessage("getnSPV",request); + } + else + { + // query current height, blockhash, notarization info + payload.resize(1); + payload[0] = NSPV_INFO; + pnode->PushMessage("getnSPV",request); + } } #endif // KOMODO_NSPV_H diff --git a/src/net.cpp b/src/net.cpp index 7181f8a95..1e5f11ee0 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -458,7 +458,7 @@ void CNode::PushVersion() LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, strSubVersion, nBestHeight, true); -fprintf(stderr,"PUSH services.%llx\n",(long long)nLocalServices); +//fprintf(stderr,"PUSH services.%llx\n",(long long)nLocalServices); } From 4478c3f3dfe724945ea9c5c39d1f12f53853bcb8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:00:32 -1100 Subject: [PATCH 024/378] Syntax --- src/komodo_nSPV.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 7be895f83..2fdd8a88f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -36,12 +36,12 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( len == 1 && request[0] == NSPV_INFO ) // info { response[0] = NSPV_INFORESP; - pnode->PushMessage("nSPV",response); + pfrom->PushMessage("nSPV",response); } else if ( request[0] == NSPV_UTXOS ) { response[0] = NSPV_UTXOSRESP; - pnode->PushMessage("nSPV",response); + pfrom->PushMessage("nSPV",response); } } } @@ -72,15 +72,15 @@ void komodo_nSPV(CNode *pto) if ( (pto->nServices & NODE_ADDRINDEX) != 0 ) { // get utxo since lastheight - payload.resize(1); - payload[0] = NSPV_UTXOS; + request.resize(1); + request[0] = NSPV_UTXOS; pnode->PushMessage("getnSPV",request); } else { // query current height, blockhash, notarization info - payload.resize(1); - payload[0] = NSPV_INFO; + request.resize(1); + request[0] = NSPV_INFO; pnode->PushMessage("getnSPV",request); } } From 4411270346715fc07cb82d30bf70bc5fe72c08ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:01:46 -1100 Subject: [PATCH 025/378] Pto --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 2fdd8a88f..6d2efcac2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -74,14 +74,14 @@ void komodo_nSPV(CNode *pto) // get utxo since lastheight request.resize(1); request[0] = NSPV_UTXOS; - pnode->PushMessage("getnSPV",request); + pto->PushMessage("getnSPV",request); } else { // query current height, blockhash, notarization info request.resize(1); request[0] = NSPV_INFO; - pnode->PushMessage("getnSPV",request); + pto->PushMessage("getnSPV",request); } } From 4b876b61188674924ee51ac1156b04069cbaf65a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:30:56 -1100 Subject: [PATCH 026/378] Throttle rate of nSPV requests --- src/komodo_nSPV.h | 53 +++++++++++++++++++++++++++++++++-------------- src/net.h | 1 + 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6d2efcac2..bb796b167 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -26,22 +26,31 @@ #define NSPV_UTXOS 0x02 #define NSPV_UTXOSRESP 0x03 +uint32_t NSPV_lastinfo,NSPV_lastutxos; void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t len; std::vector response; + int32_t len; std::vector response; uint32_t timestamp = time(NULL); if ( (len= request.size()) > 0 ) { response.resize(1); if ( len == 1 && request[0] == NSPV_INFO ) // info { - response[0] = NSPV_INFORESP; - pfrom->PushMessage("nSPV",response); + if ( timestamp > pfrom->lastinfo + KOMODO_BLOCKTIME/2 ) + { + response[0] = NSPV_INFORESP; + pfrom->lastinfo = timestamp; + pfrom->PushMessage("nSPV",response); + } } else if ( request[0] == NSPV_UTXOS ) { - response[0] = NSPV_UTXOSRESP; - pfrom->PushMessage("nSPV",response); + if ( timestamp > pfrom->lastutxos + KOMODO_BLOCKTIME/2 ) + { + response[0] = NSPV_UTXOSRESP; + pfrom->lastutxos = timestamp; + pfrom->PushMessage("nSPV",response); + } } } } @@ -67,21 +76,33 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV(CNode *pto) { - std::vector request; + std::vector request; uint32_t timestamp = time(NULL); // limit frequency! - if ( (pto->nServices & NODE_ADDRINDEX) != 0 ) + if ( timestamp > NSPV_lastutxos + KOMODO_BLOCKTIME/2 ) { - // get utxo since lastheight - request.resize(1); - request[0] = NSPV_UTXOS; - pto->PushMessage("getnSPV",request); + if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + KOMODO_BLOCKTIME ) + { + // get utxo since lastheight + if ( (rand() % 100) < 10 ) + { + request.resize(1); + request[0] = NSPV_UTXOS; + NSPV_lastutxos = pto->lastutxos = timestamp; + pto->PushMessage("getnSPV",request); + return; + } + } } - else + if ( timestamp > NSPV_lastinfo + KOMODO_BLOCKTIME/2 && timestamp > ptr->lastinfo + KOMODO_BLOCKTIME ) { - // query current height, blockhash, notarization info - request.resize(1); - request[0] = NSPV_INFO; - pto->PushMessage("getnSPV",request); + if ( (rand() % 100) < 10 ) + { + // query current height, blockhash, notarization info + request.resize(1); + request[0] = NSPV_INFO; + NSPV_lastinfo = pto->lastinfo = timestamp; + pto->PushMessage("getnSPV",request); + } } } diff --git a/src/net.h b/src/net.h index bc0352bd6..fbe756c85 100644 --- a/src/net.h +++ b/src/net.h @@ -204,6 +204,7 @@ public: bool fWhitelisted; double dPingTime; double dPingWait; + uint32_t lastutxos,lastinfo; std::string addrLocal; }; From 49a7008f95b7c6e13486dc926ec2f4f9745f8949 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:46:07 -1100 Subject: [PATCH 027/378] NSPV_lastinfo --- src/komodo_nSPV.h | 23 ++++++++++++++--------- src/net.cpp | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index bb796b167..6ce3d5094 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -26,6 +26,9 @@ #define NSPV_UTXOS 0x02 #define NSPV_UTXOSRESP 0x03 +uint32_t CNode_lastinfo(CNode *pnode,uint32_t lastutxos); +uint32_t CNode_lastutxos(CNode *pnode,uint32_t lastutxos); + uint32_t NSPV_lastinfo,NSPV_lastutxos; void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request @@ -36,19 +39,19 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req response.resize(1); if ( len == 1 && request[0] == NSPV_INFO ) // info { - if ( timestamp > pfrom->lastinfo + KOMODO_BLOCKTIME/2 ) + if ( timestamp > CNode_lastinfo(pfrom,0) + ASSETCHAINS_BLOCKTIME/2 ) { response[0] = NSPV_INFORESP; - pfrom->lastinfo = timestamp; + CNode_lastinfo(pfrom,timestamp); pfrom->PushMessage("nSPV",response); } } else if ( request[0] == NSPV_UTXOS ) { - if ( timestamp > pfrom->lastutxos + KOMODO_BLOCKTIME/2 ) + if ( timestamp > CNode_lastutxos(pfrom,0) + ASSETCHAINS_BLOCKTIME/2 ) { response[0] = NSPV_UTXOSRESP; - pfrom->lastutxos = timestamp; + CNode_lastutxos(pfrom,timestamp); pfrom->PushMessage("nSPV",response); } } @@ -78,29 +81,31 @@ void komodo_nSPV(CNode *pto) { std::vector request; uint32_t timestamp = time(NULL); // limit frequency! - if ( timestamp > NSPV_lastutxos + KOMODO_BLOCKTIME/2 ) + if ( timestamp > NSPV_lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { - if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + KOMODO_BLOCKTIME ) + if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > CNode_lastutxos(pto,0) + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight if ( (rand() % 100) < 10 ) { request.resize(1); request[0] = NSPV_UTXOS; - NSPV_lastutxos = pto->lastutxos = timestamp; + NSPV_lastutxos = timestamp; + CNode_lastutxos(pto,NSPV_lastutxos); pto->PushMessage("getnSPV",request); return; } } } - if ( timestamp > NSPV_lastinfo + KOMODO_BLOCKTIME/2 && timestamp > ptr->lastinfo + KOMODO_BLOCKTIME ) + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > CNode_lastinfo(pto,0) + ASSETCHAINS_BLOCKTIME ) { if ( (rand() % 100) < 10 ) { // query current height, blockhash, notarization info request.resize(1); request[0] = NSPV_INFO; - NSPV_lastinfo = pto->lastinfo = timestamp; + NSPV_lastinfo = timestamp; + CNode_lastinfo(pto,NSPV_lastinfo); pto->PushMessage("getnSPV",request); } } diff --git a/src/net.cpp b/src/net.cpp index 1e5f11ee0..f82e69e9d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -684,8 +684,21 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes) +uint32_t CNode_lastutxos(CNode *pnode,uint32_t lastutxos) +{ + uint32_t prev = pnode->lastutxos; + if ( lastutxos != 0 ) + pnode->lastutxos = lastutxos; + return(prev); +} - +uint32_t CNode_lastinfo(CNode *pnode,uint32_t lastinfo) +{ + uint32_t prev = pnode->lastinfo; + if ( lastinfo != 0 ) + pnode->lastinfo = lastinfo; + return(prev); +} // requires LOCK(cs_vSend) From e9a271444194db68576ee5ff050fcf1c8ea5caaa Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:49:14 -1100 Subject: [PATCH 028/378] uint32_t lastutxos,lastinfo; --- src/komodo_nSPV.h | 21 ++++++++------------- src/net.cpp | 19 ------------------- src/net.h | 2 +- 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6ce3d5094..680949b12 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -26,9 +26,6 @@ #define NSPV_UTXOS 0x02 #define NSPV_UTXOSRESP 0x03 -uint32_t CNode_lastinfo(CNode *pnode,uint32_t lastutxos); -uint32_t CNode_lastutxos(CNode *pnode,uint32_t lastutxos); - uint32_t NSPV_lastinfo,NSPV_lastutxos; void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request @@ -39,19 +36,19 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req response.resize(1); if ( len == 1 && request[0] == NSPV_INFO ) // info { - if ( timestamp > CNode_lastinfo(pfrom,0) + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > pfrom->lastinfo + ASSETCHAINS_BLOCKTIME/2 ) { response[0] = NSPV_INFORESP; - CNode_lastinfo(pfrom,timestamp); + pfrom->lastinfo = timestamp; pfrom->PushMessage("nSPV",response); } } else if ( request[0] == NSPV_UTXOS ) { - if ( timestamp > CNode_lastutxos(pfrom,0) + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { response[0] = NSPV_UTXOSRESP; - CNode_lastutxos(pfrom,timestamp); + pfrom->lastutxos = timestamp; pfrom->PushMessage("nSPV",response); } } @@ -83,29 +80,27 @@ void komodo_nSPV(CNode *pto) // limit frequency! if ( timestamp > NSPV_lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { - if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > CNode_lastutxos(pto,0) + ASSETCHAINS_BLOCKTIME ) + if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight if ( (rand() % 100) < 10 ) { request.resize(1); request[0] = NSPV_UTXOS; - NSPV_lastutxos = timestamp; - CNode_lastutxos(pto,NSPV_lastutxos); + NSPV_lastutxos = pto->lastutxos = timestamp; pto->PushMessage("getnSPV",request); return; } } } - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > CNode_lastinfo(pto,0) + ASSETCHAINS_BLOCKTIME ) + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > ptr->lastinfo + ASSETCHAINS_BLOCKTIME ) { if ( (rand() % 100) < 10 ) { // query current height, blockhash, notarization info request.resize(1); request[0] = NSPV_INFO; - NSPV_lastinfo = timestamp; - CNode_lastinfo(pto,NSPV_lastinfo); + NSPV_lastinfo = pto->lastinfo = timestamp; pto->PushMessage("getnSPV",request); } } diff --git a/src/net.cpp b/src/net.cpp index f82e69e9d..5806489a8 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -682,25 +682,6 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes) - - -uint32_t CNode_lastutxos(CNode *pnode,uint32_t lastutxos) -{ - uint32_t prev = pnode->lastutxos; - if ( lastutxos != 0 ) - pnode->lastutxos = lastutxos; - return(prev); -} - -uint32_t CNode_lastinfo(CNode *pnode,uint32_t lastinfo) -{ - uint32_t prev = pnode->lastinfo; - if ( lastinfo != 0 ) - pnode->lastinfo = lastinfo; - return(prev); -} - - // requires LOCK(cs_vSend) void SocketSendData(CNode *pnode) { diff --git a/src/net.h b/src/net.h index fbe756c85..c50d31f6a 100644 --- a/src/net.h +++ b/src/net.h @@ -204,7 +204,6 @@ public: bool fWhitelisted; double dPingTime; double dPingWait; - uint32_t lastutxos,lastinfo; std::string addrLocal; }; @@ -277,6 +276,7 @@ public: int64_t nLastRecv; int64_t nTimeConnected; int64_t nTimeOffset; + uint32_t lastutxos,lastinfo; CAddress addr; std::string addrName; CService addrLocal; From 52c78a5d796667cf9df8ee17e0c3ae54af16938b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:54:10 -1100 Subject: [PATCH 029/378] Pto --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 680949b12..bb5ddeefd 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -80,7 +80,7 @@ void komodo_nSPV(CNode *pto) // limit frequency! if ( timestamp > NSPV_lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { - if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + ASSETCHAINS_BLOCKTIME ) + if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight if ( (rand() % 100) < 10 ) @@ -93,7 +93,7 @@ void komodo_nSPV(CNode *pto) } } } - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > ptr->lastinfo + ASSETCHAINS_BLOCKTIME ) + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->lastinfo + ASSETCHAINS_BLOCKTIME ) { if ( (rand() % 100) < 10 ) { From e28d1005beee1b50d430ed03c0584f17c7d934cf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 06:43:05 -1100 Subject: [PATCH 030/378] Rest of nSPV function stubs --- src/komodo_nSPV.h | 142 +++++++++++++++++++++++++++++++++++++++++++--- src/net.h | 2 +- 2 files changed, 134 insertions(+), 10 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index bb5ddeefd..c43c7b991 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -25,8 +25,65 @@ #define NSPV_INFORESP 0x01 #define NSPV_UTXOS 0x02 #define NSPV_UTXOSRESP 0x03 +#define NSPV_NTZS 0x04 +#define NSPV_NTZSRESP 0x05 +#define NSPV_NTZPROOF 0x06 +#define NSPV_NTZPROOFRESP 0x07 +#define NSPV_TXPROOF 0x08 +#define NSPV_TXPROOFRESP 0x09 + +struct NSPV_ntz +{ + uint256 blockhash,txid,othertxid; + int32_t height,txidheight; +}; + +struct NSPV_info +{ + struct NSPV_ntz notarization; + uint256 blockhash; + int32_t height; +}; + +struct NSPV_utxo +{ + uint256 txid; + int64_t satoshis,extradata; + int32_t vout,height; +}; + +struct NSPV_ntzs +{ + struct NSPV_ntz before,after; +}; + +struct NSPV_ntzproofhdr +{ + std::vector headers; + int32_t beforeheight,afterheight; +}; + +struct NSPV_ntzproof +{ + struct NSPV_ntzproofhdr hdr; + std::vector beforentz,afterntz; +}; + +struct NSPV_MMRproof +{ + struct NSPV_ntzproofhdr hdr; + std::vector mmrproof; +}; + +struct NSPV_txproof +{ + uint256 txid; + std::vector tx,txproof; + int32_t height; +}; uint32_t NSPV_lastinfo,NSPV_lastutxos; +std::vector NSPV_utxos; void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { @@ -52,23 +109,59 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req pfrom->PushMessage("nSPV",response); } } - } + else if ( request[0] == NSPV_NTZS ) + { + if ( timestamp > pfrom->lastntzs ) + { + response[0] = NSPV_NTZSRESP; + pfrom->lastntzs = timestamp; + pfrom->PushMessage("nSPV",response); + } + } + else if ( request[0] == NSPV_NTZPROOF ) + { + if ( timestamp > pfrom->lastproof ) + { + response[0] = NSPV_NTZPROOFRESP; + pfrom->lastproof = timestamp; + pfrom->PushMessage("nSPV",response); + } + } + else if ( request[0] == NSPV_TXPROOF ) + { + if ( timestamp > pfrom->lastproof ) + { + response[0] = NSPV_TXPROOFRESP; + pfrom->lastproof = timestamp; + pfrom->PushMessage("nSPV",response); + } + } + } } void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { - int32_t len; + int32_t len; uint32_t timestamp = (uin32_t)time(NULL); if ( (len= response.size()) > 0 ) { switch ( response[0] ) { case NSPV_INFORESP: - fprintf(stderr,"got info response\n"); + fprintf(stderr,"got info response %u\n",timestamp); // update current height and ntrz status break; case NSPV_UTXOSRESP: - fprintf(stderr,"got utxos response\n"); - break; - default: fprintf(stderr,"unexpected response %02x size.%d\n",response[0],(int32_t)response.size()); + fprintf(stderr,"got utxos response %u\n",timestamp); // update utxos list + break; + case NSPV_NTZSRESP: + fprintf(stderr,"got ntzs response %u\n",timestamp); // update utxos[i] + break; + case NSPV_NTZPROOFRESP: + fprintf(stderr,"got ntzproof response %u\n",timestamp); // update utxos[i] + break; + case NSPV_TXPROOFRESP: + fprintf(stderr,"got txproof response %u\n",timestamp); // update utxos[i] + break; + default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); break; } } @@ -77,7 +170,39 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV(CNode *pto) { std::vector request; uint32_t timestamp = time(NULL); - // limit frequency! + if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) + { + for (i=0; i pto->lastntzs ) + { + request[0] = NSPV_NTZS; + pto->lastntzs = timestamp; + pto->PushMessage("getnSPV",request); + return; + } + else if ( timestamp > pto->lastproof ) + { + if ( NSPV_utxos[i].tx.size() == 0 ) + { + request[0] = NSPV_TXPROOF; + pto->lastproof = timestamp; + pto->PushMessage("getnSPV",request); + } + else + { + request[0] = NSPV_NTZPROOF; + pto->lastproof = timestamp; + pto->PushMessage("getnSPV",request); + } + return; + } + } + } + } if ( timestamp > NSPV_lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) @@ -89,13 +214,12 @@ void komodo_nSPV(CNode *pto) request[0] = NSPV_UTXOS; NSPV_lastutxos = pto->lastutxos = timestamp; pto->PushMessage("getnSPV",request); - return; } } } if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->lastinfo + ASSETCHAINS_BLOCKTIME ) { - if ( (rand() % 100) < 10 ) + if ( (rand() % 100) < 20 ) { // query current height, blockhash, notarization info request.resize(1); diff --git a/src/net.h b/src/net.h index c50d31f6a..3b9ad176f 100644 --- a/src/net.h +++ b/src/net.h @@ -276,7 +276,7 @@ public: int64_t nLastRecv; int64_t nTimeConnected; int64_t nTimeOffset; - uint32_t lastutxos,lastinfo; + uint32_t lastutxos,lastinfo,lastntzs,lastproof; CAddress addr; std::string addrName; CService addrLocal; From 550687d0c12054347b31bf22f9b843b9ffd4edac Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 06:54:35 -1100 Subject: [PATCH 031/378] NSPV_equiheader --- src/komodo_nSPV.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c43c7b991..c9399822a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -57,9 +57,21 @@ struct NSPV_ntzs struct NSPV_ntz before,after; }; +struct NSPV_equiheader +{ + int32_t nVersion; + uint256 hashPrevBlock; + uint256 hashMerkleRoot; + uint256 hashFinalSaplingRoot; + uint32_t nTime; + uint32_t nBits; + CPOSNonce nNonce; + uint8_t nSolution[1344]; +}; + struct NSPV_ntzproofhdr { - std::vector headers; + std::vector headers; int32_t beforeheight,afterheight; }; @@ -141,7 +153,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { - int32_t len; uint32_t timestamp = (uin32_t)time(NULL); + int32_t len; uint32_t timestamp = (uint32_t)time(NULL); if ( (len= response.size()) > 0 ) { switch ( response[0] ) @@ -169,7 +181,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV(CNode *pto) { - std::vector request; uint32_t timestamp = time(NULL); + std::vector request; int32_t i; uint32_t timestamp = time(NULL); if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) { for (i=0; i Date: Tue, 2 Jul 2019 06:58:29 -1100 Subject: [PATCH 032/378] Syntax --- src/komodo_nSPV.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c9399822a..1d2acb908 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -49,7 +49,8 @@ struct NSPV_utxo { uint256 txid; int64_t satoshis,extradata; - int32_t vout,height; + int32_t vout,height,before,after; + std::vector tx,txproof; }; struct NSPV_ntzs @@ -186,7 +187,7 @@ void komodo_nSPV(CNode *pto) { for (i=0; i pto->lastntzs ) @@ -204,7 +205,7 @@ void komodo_nSPV(CNode *pto) pto->lastproof = timestamp; pto->PushMessage("getnSPV",request); } - else + else // need space for the headers... { request[0] = NSPV_NTZPROOF; pto->lastproof = timestamp; From f016d63e5179ee8a97c6fd38870fc82328f7dcd5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 06:59:33 -1100 Subject: [PATCH 033/378] Syntax --- src/komodo_nSPV.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1d2acb908..e8bdb4a9e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -98,6 +98,7 @@ struct NSPV_txproof uint32_t NSPV_lastinfo,NSPV_lastutxos; std::vector NSPV_utxos; +// on fullnode: void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { int32_t len; std::vector response; uint32_t timestamp = time(NULL); @@ -152,6 +153,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } } +// on nSPV client void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); From 9edaca71f4ee6fd33b7fbbad693649b5d94fbcdc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 07:14:27 -1100 Subject: [PATCH 034/378] Remove rand throttle --- src/komodo_nSPV.h | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index e8bdb4a9e..4b46c830a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -162,8 +162,8 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: - fprintf(stderr,"got info response %u\n",timestamp); // update current height and ntrz status - break; + fprintf(stderr,"got info response %u\n",timestamp); // update current height and ntrz status + break; case NSPV_UTXOSRESP: fprintf(stderr,"got utxos response %u\n",timestamp); // update utxos list break; @@ -223,25 +223,19 @@ void komodo_nSPV(CNode *pto) if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight - if ( (rand() % 100) < 10 ) - { - request.resize(1); - request[0] = NSPV_UTXOS; - NSPV_lastutxos = pto->lastutxos = timestamp; - pto->PushMessage("getnSPV",request); - } + request.resize(1); + request[0] = NSPV_UTXOS; + NSPV_lastutxos = pto->lastutxos = timestamp; + pto->PushMessage("getnSPV",request); } } if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->lastinfo + ASSETCHAINS_BLOCKTIME ) { - if ( (rand() % 100) < 20 ) - { - // query current height, blockhash, notarization info - request.resize(1); - request[0] = NSPV_INFO; - NSPV_lastinfo = pto->lastinfo = timestamp; - pto->PushMessage("getnSPV",request); - } + // query current height, blockhash, notarization info + request.resize(1); + request[0] = NSPV_INFO; + NSPV_lastinfo = pto->lastinfo = timestamp; + pto->PushMessage("getnSPV",request); } } From a76d74fc648fe8b7ca5a6f5f52ad487d699be5b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 07:25:58 -1100 Subject: [PATCH 035/378] Revert rand skip --- src/komodo_nSPV.h | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4b46c830a..7fed4b206 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -223,19 +223,25 @@ void komodo_nSPV(CNode *pto) if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight - request.resize(1); - request[0] = NSPV_UTXOS; - NSPV_lastutxos = pto->lastutxos = timestamp; - pto->PushMessage("getnSPV",request); + if ( (rand() % 100) < 10 ) + { + request.resize(1); + request[0] = NSPV_UTXOS; + NSPV_lastutxos = pto->lastutxos = timestamp; + pto->PushMessage("getnSPV",request); + } } } if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->lastinfo + ASSETCHAINS_BLOCKTIME ) { - // query current height, blockhash, notarization info - request.resize(1); - request[0] = NSPV_INFO; - NSPV_lastinfo = pto->lastinfo = timestamp; - pto->PushMessage("getnSPV",request); + if ( (rand() % 100) < 10 ) + { + // query current height, blockhash, notarization info + request.resize(1); + request[0] = NSPV_INFO; + NSPV_lastinfo = pto->lastinfo = timestamp; + pto->PushMessage("getnSPV",request); + } } } From b876a9b92905cb51f488c2308ad23f139f034217 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 08:13:34 -1100 Subject: [PATCH 036/378] Cast uint32_t --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 7fed4b206..b8ed401bc 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -101,7 +101,7 @@ std::vector NSPV_utxos; // on fullnode: void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t len; std::vector response; uint32_t timestamp = time(NULL); + int32_t len; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); if ( (len= request.size()) > 0 ) { response.resize(1); @@ -184,7 +184,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV(CNode *pto) { - std::vector request; int32_t i; uint32_t timestamp = time(NULL); + std::vector request; int32_t i; uint32_t timestamp = (uint32_t)time(NULL); if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) { for (i=0; i Date: Tue, 2 Jul 2019 08:56:09 -1100 Subject: [PATCH 037/378] Create NODE_NSPV nServices flag --- src/komodo_nSPV.h | 2 ++ src/net.cpp | 2 +- src/protocol.cpp | 2 +- src/protocol.h | 5 +++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b8ed401bc..eec332102 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -185,6 +185,8 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV(CNode *pto) { std::vector request; int32_t i; uint32_t timestamp = (uint32_t)time(NULL); + if ( (pto->nServices & NODE_NSPV) == 0 ) + return; if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) { for (i=0; i mapLocalHost; static bool vfLimited[NET_MAX] = {}; diff --git a/src/protocol.cpp b/src/protocol.cpp index c7d95f809..36ed63934 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -102,7 +102,7 @@ CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn) void CAddress::Init() { - nServices = NODE_NETWORK; + nServices = NODE_NETWORK | NODE_NSPV; nTime = 100000000; } diff --git a/src/protocol.h b/src/protocol.h index 6b1536d6f..b7ad483ac 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -91,8 +91,9 @@ enum { // but no longer do as of protocol version 170004 (= NO_BLOOM_VERSION) NODE_BLOOM = (1 << 2), - NODE_ADDRINDEX = (1 << 30), - NODE_SPENTINDEX = (1 << 29), + NODE_NSPV = (1 << 30), + NODE_ADDRINDEX = (1 << 29), + NODE_SPENTINDEX = (1 << 28), // Bits 24-31 are reserved for temporary experiments. Just pick a bit that // isn't getting used, or one not being used much, and notify the From 0dc11fbb0282da54b46a4e71642356aecff85a2d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:16:11 -1100 Subject: [PATCH 038/378] Spent info rpc helper --- src/komodo_defs.h | 8 ++++++++ src/komodo_nSPV.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++- src/net.h | 2 +- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 6106a6000..49e247ae4 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -336,5 +336,13 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); +struct NSPV_spentinfo +{ + uint256 txid,spenttxid; + int32_t vout,height,spentvin,spentheight; +}; +void komodo_nSPV_spentinfoclear(); +struct nSPV_spentinfo komodo_nSPV_spentinfo(bits256 txid,int32_t vout); + #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index eec332102..5ea5b106c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -31,6 +31,8 @@ #define NSPV_NTZPROOFRESP 0x07 #define NSPV_TXPROOF 0x08 #define NSPV_TXPROOFRESP 0x09 +#define NSPV_SPENTINFO 0x0a +#define NSPV_SPENTINFORESP 0x0b struct NSPV_ntz { @@ -97,6 +99,7 @@ struct NSPV_txproof uint32_t NSPV_lastinfo,NSPV_lastutxos; std::vector NSPV_utxos; +std::vector NSPV_spends; // on fullnode: void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request @@ -120,6 +123,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response[0] = NSPV_UTXOSRESP; pfrom->lastutxos = timestamp; + // check mempool pfrom->PushMessage("nSPV",response); } } @@ -150,7 +154,17 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req pfrom->PushMessage("nSPV",response); } } - } + else if ( request[0] == NSPV_SPENTINFO ) + { + if ( timestamp > pfrom->lastspent ) + { + response[0] = NSPV_SPENTINFORESP; + // check mempool + pfrom->lastspent = timestamp; + pfrom->PushMessage("nSPV",response); + } + } + } } // on nSPV client @@ -176,12 +190,47 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_TXPROOFRESP: fprintf(stderr,"got txproof response %u\n",timestamp); // update utxos[i] break; + case NSPV_SPENTINFORESP: + fprintf(stderr,"got spentinfo response %u\n",timestamp); // update utxos[i] + break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); break; } } } +void komodo_nSPV_spentinfoclear() +{ + nSPV_spends.resize(0); +} + +struct nSPV_spentinfo komodo_nSPV_spentinfo(bits256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg +{ + std::vector request; struct nSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); + // lookup spentinfo + for (i=0; ihSocket == INVALID_SOCKET ) + continue; + if ( (pto->nServices & NODE_NSPV) != 0 && timestamp > pto->lastspent ) + { + request.resize(1); + request[0] = NSPV_SPENTINFO; + pto->lastspent = timestamp; + pto->PushMessage("getnSPV",request); + if ( ++numsent >= 3 ) + break; + } + } +} + void komodo_nSPV(CNode *pto) { std::vector request; int32_t i; uint32_t timestamp = (uint32_t)time(NULL); diff --git a/src/net.h b/src/net.h index 3b9ad176f..dce52aa8b 100644 --- a/src/net.h +++ b/src/net.h @@ -276,7 +276,7 @@ public: int64_t nLastRecv; int64_t nTimeConnected; int64_t nTimeOffset; - uint32_t lastutxos,lastinfo,lastntzs,lastproof; + uint32_t lastutxos,lastinfo,lastntzs,lastproof,lastspent; CAddress addr; std::string addrName; CService addrLocal; From e3986e77a01abbf5a2bb8742b17b684a4eed1ceb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:17:27 -1100 Subject: [PATCH 039/378] Uint256 --- src/komodo_defs.h | 2 +- src/komodo_nSPV.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 49e247ae4..8cdfe1c59 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -342,7 +342,7 @@ struct NSPV_spentinfo int32_t vout,height,spentvin,spentheight; }; void komodo_nSPV_spentinfoclear(); -struct nSPV_spentinfo komodo_nSPV_spentinfo(bits256 txid,int32_t vout); +struct nSPV_spentinfo komodo_nSPV_spentinfo(uint256 txid,int32_t vout); #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 5ea5b106c..e1964f887 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -204,7 +204,7 @@ void komodo_nSPV_spentinfoclear() nSPV_spends.resize(0); } -struct nSPV_spentinfo komodo_nSPV_spentinfo(bits256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg +struct nSPV_spentinfo komodo_nSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg { std::vector request; struct nSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); // lookup spentinfo From 347276f7765cca8c80ec1b242aa44955a7b3b02d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:24:44 -1100 Subject: [PATCH 040/378] Syntax --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index e1964f887..95ddcd4d6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -201,16 +201,16 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_nSPV_spentinfoclear() { - nSPV_spends.resize(0); + NSPV_spends.resize(0); } struct nSPV_spentinfo komodo_nSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg { std::vector request; struct nSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); // lookup spentinfo - for (i=0; i Date: Tue, 2 Jul 2019 09:26:39 -1100 Subject: [PATCH 041/378] nSPV --- src/komodo_defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 8cdfe1c59..8f32306d9 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -336,7 +336,7 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); -struct NSPV_spentinfo +struct nSPV_spentinfo { uint256 txid,spenttxid; int32_t vout,height,spentvin,spentheight; From c02de15ef2dcb255f62edefd7ed88a780c9dc6d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:37:20 -1100 Subject: [PATCH 042/378] NSPV --- src/komodo_defs.h | 6 +++--- src/komodo_nSPV.h | 6 +++--- src/main.cpp | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 8f32306d9..784502dc8 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -336,13 +336,13 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); -struct nSPV_spentinfo +struct NSPV_spentinfo { uint256 txid,spenttxid; int32_t vout,height,spentvin,spentheight; }; -void komodo_nSPV_spentinfoclear(); -struct nSPV_spentinfo komodo_nSPV_spentinfo(uint256 txid,int32_t vout); +void komodo_NSPV_spentinfoclear(); +struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout); #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 95ddcd4d6..4fde5b5e0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -199,14 +199,14 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r } } -void komodo_nSPV_spentinfoclear() +void komodo_NSPV_spentinfoclear() { NSPV_spends.resize(0); } -struct nSPV_spentinfo komodo_nSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg +struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg { - std::vector request; struct nSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); + std::vector request; struct NSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); // lookup spentinfo for (i=0; iSetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION)); + if ( KOMODO_NSPV != 0 && (pfrom->nServices & NODE_NSPV) == 0 ) + { + fprintf(stderr,"invalid nSPV peer.%d\n",pfrom->id); + pfrom->fDisconnect = true; + return false; + } // Mark this node as currently connected, so we update its timestamp later. if (pfrom->fNetworkNode) { LOCK(cs_main); From d984c6a7e0e3867777f7f8c3120c8e1e8cd6fca3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:42:14 -1100 Subject: [PATCH 043/378] pnode --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4fde5b5e0..59afa6ae0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -219,12 +219,12 @@ struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a { if ( pnode->hSocket == INVALID_SOCKET ) continue; - if ( (pto->nServices & NODE_NSPV) != 0 && timestamp > pto->lastspent ) + if ( (pnode->nServices & NODE_NSPV) != 0 && timestamp > pto->lastspent ) { request.resize(1); request[0] = NSPV_SPENTINFO; - pto->lastspent = timestamp; - pto->PushMessage("getnSPV",request); + pnode->lastspent = timestamp; + pnode->PushMessage("getnSPV",request); if ( ++numsent >= 3 ) break; } From ad424236e584154c5397d7855e8e786bd9f3e605 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 09:43:21 -1100 Subject: [PATCH 044/378] Again --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 59afa6ae0..6c46e049f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -219,7 +219,7 @@ struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a { if ( pnode->hSocket == INVALID_SOCKET ) continue; - if ( (pnode->nServices & NODE_NSPV) != 0 && timestamp > pto->lastspent ) + if ( (pnode->nServices & NODE_NSPV) != 0 && timestamp > pnode->lastspent ) { request.resize(1); request[0] = NSPV_SPENTINFO; From 6f5566940a63ede617a77abcb767875803af5413 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 01:32:33 -1100 Subject: [PATCH 045/378] Serdes for NSPV structs and initial functionality --- src/komodo_defs.h | 3 + src/komodo_nSPV.h | 570 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 493 insertions(+), 80 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 784502dc8..5ea260db6 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -336,6 +336,9 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); +bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); +std::vector Mypubkey(); + struct NSPV_spentinfo { uint256 txid,spenttxid; diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6c46e049f..e37a83746 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -18,6 +18,13 @@ * * ******************************************************************************/ +// todo: make sure no files are updated +// finalize structs, de/serialization +// rpc calls +// validate proofs + +// make sure to sanity check all vector lengths on receipt + #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H @@ -34,33 +41,15 @@ #define NSPV_SPENTINFO 0x0a #define NSPV_SPENTINFORESP 0x0b -struct NSPV_ntz +int32_t iguana_rwbuf(int32_t rwflag,uint8_t *serialized,uint16_t len,uint8_t *buf) { - uint256 blockhash,txid,othertxid; - int32_t height,txidheight; -}; + if ( rwflag != 0 ) + memcpy(serialized,buf,len); + else memcpy(buf,serialized,len); + return(len) +} -struct NSPV_info -{ - struct NSPV_ntz notarization; - uint256 blockhash; - int32_t height; -}; - -struct NSPV_utxo -{ - uint256 txid; - int64_t satoshis,extradata; - int32_t vout,height,before,after; - std::vector tx,txproof; -}; - -struct NSPV_ntzs -{ - struct NSPV_ntz before,after; -}; - -struct NSPV_equiheader +struct NSPV_equihdr { int32_t nVersion; uint256 hashPrevBlock; @@ -68,106 +57,516 @@ struct NSPV_equiheader uint256 hashFinalSaplingRoot; uint32_t nTime; uint32_t nBits; - CPOSNonce nNonce; + uint256 nNonce; uint8_t nSolution[1344]; }; -struct NSPV_ntzproofhdr +int32_t NSPV_rwequihdr(int32_t rwflag,uint8_t *serialized,struct NSPV_equihdr *ptr) { - std::vector headers; - int32_t beforeheight,afterheight; + int32_t len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nVersion),&ptr->nVersion); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->hashPrevBlock),(uint8_t *)&ptr->hashPrevBlock); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->hashMerkleRoot),(uint8_t *)&ptr->hashMerkleRoot); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->hashFinalSaplingRoot),(uint8_t *)&ptr->hashFinalSaplingRoot); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nTime),&ptr->nTime); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nBits),&ptr->nBits); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->nNonce),(uint8_t *)&ptr->nNonce); + len += iguana_rwbuf(rwflag,&serialized[len],sizeof(ptr->nSolution),ptr->nSolution); + return(len); +} + +int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsizep,uint8_t struct NSPV_equihdr **ptrp) +{ + int32_t i,vsize,len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); + if ( (vsize= *vecsizep) != 0 ) + { + if ( *ptrp == 0 ) + *ptrp = calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion + for (i=0; i beforentz,afterntz; + int32_t len = 0; + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->satoshis),&ptr->satoshis); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->extradata),&ptr->extradata); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + return(len); +} + +struct NSPV_utxosresp +{ + struct NSPV_utxoresp *utxos; + int64_t total,interest; + int32_t pad32; + uint16_t numutxos,pad16; }; -struct NSPV_MMRproof +int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,uint16_t *vecsizep,uint8_t struct NSPV_utxosresp **ptrp) // check mempool { - struct NSPV_ntzproofhdr hdr; - std::vector mmrproof; + int32_t i,vsize,len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); + if ( (vsize= *vecsizep) != 0 ) + { + if ( *ptrp == 0 ) + *ptrp = calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion + for (i=0; itotal),&ptr->total); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); + return(len); +} + +void NSPV_utxosresp_purge(struct NSPV_utxosresp *ptr) +{ + if ( ptr != 0 ) + { + if ( ptr->utxos != 0 ) + free(ptr->utxos); + memset(ptr,0,sizeof(*ptr)); + } +} + +struct NSPV_ntz +{ + uint256 blockhash,txid,othertxid; + int32_t height,txidheight; }; +int32_t NSPV_rwntz(int32_t rwflag,uint8_t *serialized,struct NSPV_ntz *ptr) +{ + int32_t len = 0; + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->blockhash),(uint8_t *)&ptr->blockhash); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->othertxid),(uint8_t *)&ptr->othertxid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->txidheight),&ptr->txidheight); + return(len); +} + +struct NSPV_ntzsresp +{ + struct NSPV_ntz prevntz,nextntz; +}; + +int32_t NSPV_rwntzsresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsresp *ptr) +{ + int32_t len = 0; + len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->prevntz),&ptr->prevntz); + len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->nextntz),&ptr->nextntz); + return(len); +} + +void NSPV_ntzsresp_purge(struct NSPV_ntzsresp *ptr) +{ + if ( ptr != 0 ) + memset(ptr,0,sizeof(*ptr)); +} + +struct NSPV_inforesp +{ + struct NSPV_ntz notarization; + uint256 blockhash; + int32_t height,pad32; +}; + +int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp *ptr) +{ + int32_t len = 0; + len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->notarization),&ptr->notarization); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->blockhash),(uint8_t *)&ptr->blockhash); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + return(len); +} + +void NSPV_inforesp_purge(struct NSPV_inforesp *ptr) +{ + if ( ptr != 0 ) + memset(ptr,0,sizeof(*ptr)); +} + struct NSPV_txproof { uint256 txid; - std::vector tx,txproof; int32_t height; + uint16_t txlen,txprooflen; + uint8_t *tx,*txproof; }; -uint32_t NSPV_lastinfo,NSPV_lastutxos; -std::vector NSPV_utxos; -std::vector NSPV_spends; +int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *ptr) +{ + int32_t len = 0; + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); + return(len); +} + +void NSPV_txproof_purge(struct NSPV_txproof *ptr) +{ + if ( ptr != 0 ) + { + if ( ptr->tx != 0 ) + free(ptr->tx); + if ( ptr->txproof != 0 ) + free(ptr->txproof); + memset(ptr,0,sizeof(*ptr)); + } +} + +struct NSPV_utxo +{ + struct NSPV_txproof T; + int64_t satoshis,extradata; + int32_t vout,prevht,nextht,pad32; +}; + +int32_t NSPV_rwutxo(int32_t rwflag,uint8_t *serialized,struct NSPV_utxo *ptr) +{ + int32_t len = 0; + len += NSPV_rwtxproof(rwflag,&serialized[len],&ptr->T); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->satoshis),&ptr->satoshis); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->extradata),&ptr->extradata); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevht),&ptr->prevht); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + return(len); +} + +struct NSPV_ntzproofshared +{ + struct NSPV_equihdr *hdrs; + int32_t prevht,nextht,pad32; + uint16_t numhdrs,pad16; +}; + +int32_t NSPV_rwntzproofshared(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzproofshared *ptr) +{ + int32_t len = 0; + len += iguana_rwequihdrvec(rwflag,&serialized[len],&ptr->numhdrs,&ptr->hdrs); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevht),&ptr->prevht); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); + return(len); +} + +struct NSPV_ntzsproofresp +{ + struct NSPV_ntzproofshared common; + uint32_t pad32; + uint16_t prevlen,nextlen; + uint8_t *prevntz,*nextntz; +}; + +int32_t NSPV_rwntzproof(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsproofresp *ptr) +{ + int32_t len = 0; + len += NSPV_rwntzproofshared(rwflag,&serialized[len],&ptr->common); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevlen,&ptr->prevntz); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nextlen,&ptr->nextntz); + return(len); +} + +void NSPV_ntzsproofresp_purge(struct NSPV_ntzsproofresp *ptr) +{ + if ( ptr != 0 ) + { + if ( ptr->common.hdrs != 0 ) + free(ptr->common.hdrs); + if ( ptr->prevntz != 0 ) + free(ptr->prevntz); + if ( ptr->nextntz != 0 ) + free(ptr->nextntz); + memset(ptr,0,sizeof(*ptr)); + } +} + +struct NSPV_MMRproof +{ + struct NSPV_ntzproofhdr hdr; + // tbd +}; + +struct NSPV_spentinfo +{ + struct NSPV_txproof spent; + uint256 txid; + int32_t height,spentvini; +}; + +int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinfo *ptr) // check mempool +{ + int32_t len = 0; + len += NSPV_rwtxproof(rwflag,&serialized[len],&ptr->spent); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->spentvini),&ptr->spentvini); + return(len); +} + +void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) +{ + if ( ptr != 0 ) + { + NSPV_txproof_purge(&ptr->spent); + memset(ptr,0,sizeof(*ptr)); + } +} // on fullnode: + +int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) +{ + int32_t prevMoMheight,len = 0; CBlockIndex *pindex; + if ( (pindex= chainActive.LastTip()) != 0 ) + { + ptr->height = pindex->GetHeight(); + ptr->blockhash = pindex->GetBlockHash(); + ptr->notarization.height = komodo_notarized_height(&prevMoMheight,&ptr->notarization.blockhash,&ptr->notarization.othertxid); + //ptr->notarization.txidheight = komodo_findnotarization(&ptr->notarization.txid,ptr->notarization.height,ptr->notarization.blockhash); + return(sizeof(*ptr)); + } else return(-1); +} + +int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check mempool +{ + int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,txheight,n = 0,len = 0; + std::vector > unspentOutputs; + fprintf(stderr,"getaddressutxos for %s\n",coinaddr); + SetCCunspents(unspentOutputs,coinaddr,false); + if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 ) + { + tipheight = chainActive.LastTip()->GetHeight(); + ptr->utxos = calloc(ptr->numutxos,sizeof(*ptr->utxos)); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + ptr->utxos[n].txid = it->first.txhash; + ptr->utxos[n].vout = (int32_t)it->first.index; + ptr->utxos[n].satoshis = it->second.satoshis; + ptr->utxos[n].height = it->second.blockHeight; + if ( ASSETCHAINS_SYMBOL[0] == 0 && it->second.satoshis >= 10*COIN ) + { + ptr->utxos[n].extradata = komodo_accrued_interest(&txheight,&locktime,ptr->utxos[n].txid,ptr->utxos[n].vout,ptr->utxos[n].height,ptr->utxos[n].satoshis,tipheight); + interest += ptr->utxos[n].extradata; + } + total += it->second.satoshis; + n++; + } + fprintf(stderr,"getaddressutxos for %s -> n.%d total %.8f interest %.8f\n",coinaddr,dstr(total),dstr(interest)); + if ( n == ptr->numutxos ) + { + ptr->total = total; + ptr->interest = interest; + return((int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos)); + } + } + if ( ptr->utxos != 0 ) + free(ptr->utxos); + memst(ptr,0,sizeof(*ptr)); + return(0); +} + +int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) +{ + int32_t len = 0; + return(sizeof(*ptr)); +} + +int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) +{ + int32_t len = 0; + return(len); +} + +int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) +{ + int32_t len = 0; + return(len); +} + +int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) +{ + int32_t len = 0; + return(len); +} + void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t len; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); + int32_t len,slen; std::vector response; uint32_t timestamp; + timestamp = (uint32_t)time(NULL); if ( (len= request.size()) > 0 ) { - response.resize(1); - if ( len == 1 && request[0] == NSPV_INFO ) // info + if ( request[0] == NSPV_INFO ) // info { if ( timestamp > pfrom->lastinfo + ASSETCHAINS_BLOCKTIME/2 ) { - response[0] = NSPV_INFORESP; - pfrom->lastinfo = timestamp; - pfrom->PushMessage("nSPV",response); + struct NSPV_inforesp I; + memset(&I,0,sizeof(I)); + if ( (slen= NSPV_getinfo(&I)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_INFORESP; + NSPV_rwinforesp(1,&response[1],&I); + pfrom->PushMessage("nSPV",response); + pfrom->lastinfo = timestamp; + NSPV_inforesp_purge(&I); + } } } else if ( request[0] == NSPV_UTXOS ) { if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { - response[0] = NSPV_UTXOSRESP; - pfrom->lastutxos = timestamp; - // check mempool - pfrom->PushMessage("nSPV",response); + struct NSPV_utxosresp U; char coinaddr[64]; + if ( len < 64 && request[1] == len-2 ) + { + memcpy(coinaddr,&request[2],request[1]); + memset(&U,0,sizeof(U)); + slen = NSPV_getaddressutxos(&U,coinaddr); + response.resize(1 + slen); + response[0] = NSPV_UTXOSRESP; + if ( NSPV_rwutxosresp(1,&response[1],&U.numutxos,&U.utxos) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastutxos = timestamp; + } + NSPV_utxosresp_purge(&U); + } } } else if ( request[0] == NSPV_NTZS ) { if ( timestamp > pfrom->lastntzs ) { - response[0] = NSPV_NTZSRESP; - pfrom->lastntzs = timestamp; - pfrom->PushMessage("nSPV",response); + struct NSPV_ntzsresp N; int32_t height; + if ( len == 1+sizeof(height) ) + { + iguana_rwnum(rwflag,&request[1],sizeof(height),&height); + memset(&N,0,sizeof(N)); + slen = NSPV_getntzsresp(&N,height); + response.resize(1 + slen); + response[0] = NSPV_NTZSRESP; + if ( NSPV_rwntzsresp(1,&response[1],&N) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastntzs = timestamp; + } + NSPV_ntzsresp_purge(&N); + } } } else if ( request[0] == NSPV_NTZPROOF ) { if ( timestamp > pfrom->lastproof ) { - response[0] = NSPV_NTZPROOFRESP; - pfrom->lastproof = timestamp; - pfrom->PushMessage("nSPV",response); + struct NSPV_ntzsproofresp P; int32_t prevht,nextht; + if ( len == 1+sizeof(prevht)+sizeof(nextht) ) + { + iguana_rwnum(rwflag,&request[1],sizeof(prevht),&prevht); + iguana_rwnum(rwflag,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); + if ( prevht != 0 && nextht != 0 && nextht >= prevht ) + { + memset(&N,0,sizeof(N)); + slen = NSPV_getntzsproofresp(&P,prevht,nextht); + response.resize(1 + slen); + response[0] = NSPV_NTZPROOFRESP; + if ( NSPV_rwntzsresp(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastproof = timestamp; + } + NSPV_ntzsproofresp_purge(&P); + } + } } } else if ( request[0] == NSPV_TXPROOF ) { if ( timestamp > pfrom->lastproof ) { - response[0] = NSPV_TXPROOFRESP; - pfrom->lastproof = timestamp; - pfrom->PushMessage("nSPV",response); + struct NSPV_spentinfo P; uint256 txid; int32_t height; + if ( len == 1+sizeof(txid)+sizeof(height) ) + { + iguana_rwnum(rwflag,&request[1],sizeof(height),&height); + iguana_rwbignum(rwflag,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); + memset(&P,0,sizeof(P)); + slen = NSPV_gettxproof(&P,txid,height); + response.resize(1 + slen); + response[0] = NSPV_TXPROOFRESP; + if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastproof = timestamp; + } + NSPV_txproof_purge(&P); + } } } else if ( request[0] == NSPV_SPENTINFO ) { if ( timestamp > pfrom->lastspent ) { - response[0] = NSPV_SPENTINFORESP; - // check mempool - pfrom->lastspent = timestamp; - pfrom->PushMessage("nSPV",response); + struct NSPV_spentinfo S; int32_t vout; uint256 txid; + if ( len == 1+sizeof(txid)+sizeof(vout) ) + { + iguana_rwnum(rwflag,&request[1],sizeof(vout),&vout); + iguana_rwbignum(rwflag,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); + memset(&S,0,sizeof(S)); + slen = NSPV_getspentinfo(&S,txid,vout); + response.resize(1 + slen); + response[0] = NSPV_SPENTINFORESP; + if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastspent = timestamp; + } + NSPV_spentinfo_purge(&S); + } } } } } // on nSPV client +uint32_t NSPV_lastinfo,NSPV_lastutxos; +int32_t NSPV_numutxos,NSPV_numspends; +struct NSPV_utxo *NSPV_utxos; +struct NSPV_spentinfo *NSPV_spends; + void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); @@ -176,22 +575,22 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: - fprintf(stderr,"got info response %u\n",timestamp); // update current height and ntrz status + fprintf(stderr,"got info response %u size.%d\n",timestamp,(int32_t)response.size()); // update current height and ntrz status break; case NSPV_UTXOSRESP: - fprintf(stderr,"got utxos response %u\n",timestamp); // update utxos list + fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list break; case NSPV_NTZSRESP: - fprintf(stderr,"got ntzs response %u\n",timestamp); // update utxos[i] + fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_NTZPROOFRESP: - fprintf(stderr,"got ntzproof response %u\n",timestamp); // update utxos[i] + fprintf(stderr,"got ntzproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_TXPROOFRESP: - fprintf(stderr,"got txproof response %u\n",timestamp); // update utxos[i] + fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_SPENTINFORESP: - fprintf(stderr,"got spentinfo response %u\n",timestamp); // update utxos[i] + fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); break; @@ -201,14 +600,17 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r void komodo_NSPV_spentinfoclear() { - NSPV_spends.resize(0); + if ( NSPV_spends != 0 ) + free(NSPV_spends); + NSPV_spends = 0; + NSPV_numspends = 0; } struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg { - std::vector request; struct NSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp = (uint32_t)time(NULL); - // lookup spentinfo - for (i=0; i request; struct NSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp; + timestamp = (uint32_t)time(NULL); + for (i=0; ihSocket == INVALID_SOCKET ) continue; - if ( (pnode->nServices & NODE_NSPV) != 0 && timestamp > pnode->lastspent ) + if ( (pnode->nServices & NODE_SPENTINDEX) != 0 && timestamp > pnode->lastspent ) { request.resize(1); request[0] = NSPV_SPENTINFO; @@ -238,12 +640,12 @@ void komodo_nSPV(CNode *pto) return; if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) { - for (i=0; i pto->lastntzs ) + if ( NSPV_utxos[i].prevlen == 0 && timestamp > pto->lastntzs ) { request[0] = NSPV_NTZS; pto->lastntzs = timestamp; @@ -276,10 +678,18 @@ void komodo_nSPV(CNode *pto) // get utxo since lastheight if ( (rand() % 100) < 10 ) { - request.resize(1); - request[0] = NSPV_UTXOS; - NSPV_lastutxos = pto->lastutxos = timestamp; - pto->PushMessage("getnSPV",request); + char coinaddr[64]; int32_t slen; + Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG); + slen = (int32_t)strlen(coinaddr); + if ( slen < 64 ) + { + request.resize(1 + 1 + slen); + request[0] = NSPV_UTXOS; + request[1] = slen; + memcpy(&request[2],coinaddr,slen); + NSPV_lastutxos = pto->lastutxos = timestamp; + pto->PushMessage("getnSPV",request); + } } } } From 3bf8b8862ff93fd82e87cd80eac067ce105c901f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 01:54:35 -1100 Subject: [PATCH 046/378] Syntax --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index e37a83746..98048e1fb 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -46,7 +46,7 @@ int32_t iguana_rwbuf(int32_t rwflag,uint8_t *serialized,uint16_t len,uint8_t *bu if ( rwflag != 0 ) memcpy(serialized,buf,len); else memcpy(buf,serialized,len); - return(len) + return(len); } struct NSPV_equihdr @@ -75,7 +75,7 @@ int32_t NSPV_rwequihdr(int32_t rwflag,uint8_t *serialized,struct NSPV_equihdr *p return(len); } -int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsizep,uint8_t struct NSPV_equihdr **ptrp) +int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsizep,struct NSPV_equihdr **ptrp) { int32_t i,vsize,len = 0; len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); From 665a272e72b0c2b326a0fa06882fea363fc22bee Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:02:05 -1100 Subject: [PATCH 047/378] Syntax --- src/komodo_nSPV.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 98048e1fb..668482560 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -82,7 +82,7 @@ int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsize if ( (vsize= *vecsizep) != 0 ) { if ( *ptrp == 0 ) - *ptrp = calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion + *ptrp = (struct NSPV_equihdr *)calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; inumutxos= (int32_t)unspentOutputs.size()) > 0 ) { tipheight = chainActive.LastTip()->GetHeight(); - ptr->utxos = calloc(ptr->numutxos,sizeof(*ptr->utxos)); + ptr->utxos = (struct NSPV_utxoresp *)calloc(ptr->numutxos,sizeof(*ptr->utxos)); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { ptr->utxos[n].txid = it->first.txhash; From 456d4a9bfb0bca6d907d45dc332c301a2c2306da Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:07:03 -1100 Subject: [PATCH 048/378] Fix utxos --- src/komodo_nSPV.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 668482560..ec853c682 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -128,16 +128,16 @@ struct NSPV_utxosresp uint16_t numutxos,pad16; }; -int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,uint16_t *vecsizep,struct NSPV_utxosresp **ptrp) // check mempool +int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxosresp *ptr) // check mempool { - int32_t i,vsize,len = 0; - len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); - if ( (vsize= *vecsizep) != 0 ) + int32_t i,len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->numutxos),&ptr->numutxos); + if ( ptr->numutxos != 0 ) { - if ( *ptrp == 0 ) - *ptrp = (struct NSPV_utxosresp *)calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion - for (i=0; iutxos == 0 ) + ptr->utxos = (struct NSPV_utxoresp *)calloc(sizeof(*ptr->utxos),ptr->numutxos); // relies on uint16_t being "small" to prevent mem exhaustion + for (i=0; inumutxos; i++) + len += NSPV_rwutxoresp(rwflag,&serialized[len],&ptr->utxos[i]); } len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->total),&ptr->total); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); @@ -459,7 +459,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req slen = NSPV_getaddressutxos(&U,coinaddr); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; - if ( NSPV_rwutxosresp(1,&response[1],&U.numutxos,&U.utxos) == slen ) + if ( NSPV_rwutxosresp(1,&response[1],&U.utxos) == slen ) { pfrom->PushMessage("nSPV",response); pfrom->lastutxos = timestamp; From 836ffe9ad4a0a7bbcd8dc29340b277536a057e23 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:12:34 -1100 Subject: [PATCH 049/378] Syntax --- src/komodo_nSPV.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index ec853c682..0085d356e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -181,8 +181,8 @@ struct NSPV_ntzsresp int32_t NSPV_rwntzsresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsresp *ptr) { int32_t len = 0; - len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->prevntz),&ptr->prevntz); - len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->nextntz),&ptr->nextntz); + len += NSPV_rwntz(rwflag,&serialized[len],&ptr->prevntz); + len += NSPV_rwntz(rwflag,&serialized[len],&ptr->nextntz); return(len); } @@ -202,7 +202,7 @@ struct NSPV_inforesp int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp *ptr) { int32_t len = 0; - len += NSPV_rwntz(rwflag,&serialized[len],sizeof(ptr->notarization),&ptr->notarization); + len += NSPV_rwntz(rwflag,&serialized[len],&ptr->notarization); len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->blockhash),(uint8_t *)&ptr->blockhash); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); @@ -317,7 +317,7 @@ void NSPV_ntzsproofresp_purge(struct NSPV_ntzsproofresp *ptr) struct NSPV_MMRproof { - struct NSPV_ntzproofhdr hdr; + struct NSPV_ntzproofshared common; // tbd }; From 2ad7732c8a87da5d89aa0126610c0311e5928297 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:14:58 -1100 Subject: [PATCH 050/378] Fix spent info --- src/komodo_defs.h | 5 +++-- src/komodo_nSPV.h | 7 ------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 5ea260db6..b36af620d 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -341,8 +341,9 @@ std::vector Mypubkey(); struct NSPV_spentinfo { - uint256 txid,spenttxid; - int32_t vout,height,spentvin,spentheight; + struct NSPV_txproof spent; + uint256 txid; + int32_t height,spentvini; }; void komodo_NSPV_spentinfoclear(); struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout); diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 0085d356e..9235916e1 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -321,13 +321,6 @@ struct NSPV_MMRproof // tbd }; -struct NSPV_spentinfo -{ - struct NSPV_txproof spent; - uint256 txid; - int32_t height,spentvini; -}; - int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinfo *ptr) // check mempool { int32_t len = 0; From 87d2264b2ad40fb07a271a8310020823f24d7c91 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:16:25 -1100 Subject: [PATCH 051/378] Syntax --- src/komodo_defs.h | 8 -------- src/komodo_nSPV.h | 7 +++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index b36af620d..0a628b216 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -339,14 +339,6 @@ int32_t komodo_currentheight(); bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); std::vector Mypubkey(); -struct NSPV_spentinfo -{ - struct NSPV_txproof spent; - uint256 txid; - int32_t height,spentvini; -}; -void komodo_NSPV_spentinfoclear(); -struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout); #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9235916e1..0085d356e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -321,6 +321,13 @@ struct NSPV_MMRproof // tbd }; +struct NSPV_spentinfo +{ + struct NSPV_txproof spent; + uint256 txid; + int32_t height,spentvini; +}; + int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinfo *ptr) // check mempool { int32_t len = 0; From f5be07ddfe4d528f157b109988e9f85fde1ca2e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:26:24 -1100 Subject: [PATCH 052/378] Syntax --- src/komodo_nSPV.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 0085d356e..85ce08ba9 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -386,7 +386,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check total += it->second.satoshis; n++; } - fprintf(stderr,"getaddressutxos for %s -> n.%d total %.8f interest %.8f\n",coinaddr,dstr(total),dstr(interest)); + fprintf(stderr,"getaddressutxos for %s -> n.%d total %.8f interest %.8f\n",coinaddr,n,dstr(total),dstr(interest)); if ( n == ptr->numutxos ) { ptr->total = total; @@ -396,7 +396,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check } if ( ptr->utxos != 0 ) free(ptr->utxos); - memst(ptr,0,sizeof(*ptr)); + memset(ptr,0,sizeof(*ptr)); return(0); } @@ -432,7 +432,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { if ( request[0] == NSPV_INFO ) // info { - if ( timestamp > pfrom->lastinfo + ASSETCHAINS_BLOCKTIME/2 ) + if ( len == 1 && timestamp > pfrom->lastinfo + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_inforesp I; memset(&I,0,sizeof(I)); @@ -459,7 +459,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req slen = NSPV_getaddressutxos(&U,coinaddr); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; - if ( NSPV_rwutxosresp(1,&response[1],&U.utxos) == slen ) + if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) { pfrom->PushMessage("nSPV",response); pfrom->lastutxos = timestamp; @@ -475,7 +475,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req struct NSPV_ntzsresp N; int32_t height; if ( len == 1+sizeof(height) ) { - iguana_rwnum(rwflag,&request[1],sizeof(height),&height); + iguana_rwnum(0,&request[1],sizeof(height),&height); memset(&N,0,sizeof(N)); slen = NSPV_getntzsresp(&N,height); response.resize(1 + slen); @@ -496,11 +496,11 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req struct NSPV_ntzsproofresp P; int32_t prevht,nextht; if ( len == 1+sizeof(prevht)+sizeof(nextht) ) { - iguana_rwnum(rwflag,&request[1],sizeof(prevht),&prevht); - iguana_rwnum(rwflag,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); + iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); + iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); if ( prevht != 0 && nextht != 0 && nextht >= prevht ) { - memset(&N,0,sizeof(N)); + memset(&P,0,sizeof(P)); slen = NSPV_getntzsproofresp(&P,prevht,nextht); response.resize(1 + slen); response[0] = NSPV_NTZPROOFRESP; @@ -518,11 +518,11 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { if ( timestamp > pfrom->lastproof ) { - struct NSPV_spentinfo P; uint256 txid; int32_t height; + struct NSPV_txproof P; uint256 txid; int32_t height; if ( len == 1+sizeof(txid)+sizeof(height) ) { - iguana_rwnum(rwflag,&request[1],sizeof(height),&height); - iguana_rwbignum(rwflag,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); + iguana_rwnum(0,&request[1],sizeof(height),&height); + iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); memset(&P,0,sizeof(P)); slen = NSPV_gettxproof(&P,txid,height); response.resize(1 + slen); @@ -543,8 +543,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req struct NSPV_spentinfo S; int32_t vout; uint256 txid; if ( len == 1+sizeof(txid)+sizeof(vout) ) { - iguana_rwnum(rwflag,&request[1],sizeof(vout),&vout); - iguana_rwbignum(rwflag,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); + iguana_rwnum(0,&request[1],sizeof(vout),&vout); + iguana_rwbignum(0,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); memset(&S,0,sizeof(S)); slen = NSPV_getspentinfo(&S,txid,vout); response.resize(1 + slen); From 1a45fa08da083f72321fdf26fca7d269d72a9fdf Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:33:05 -1100 Subject: [PATCH 053/378] Syntax --- src/komodo_nSPV.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 85ce08ba9..c2d8a6254 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -325,7 +325,7 @@ struct NSPV_spentinfo { struct NSPV_txproof spent; uint256 txid; - int32_t height,spentvini; + int32_t vout,height,spentvini; }; int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinfo *ptr) // check mempool @@ -333,6 +333,7 @@ int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinf int32_t len = 0; len += NSPV_rwtxproof(rwflag,&serialized[len],&ptr->spent); len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->spentvini),&ptr->spentvini); return(len); @@ -504,7 +505,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req slen = NSPV_getntzsproofresp(&P,prevht,nextht); response.resize(1 + slen); response[0] = NSPV_NTZPROOFRESP; - if ( NSPV_rwntzsresp(1,&response[1],&P) == slen ) + if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); pfrom->lastproof = timestamp; @@ -623,12 +624,15 @@ struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a continue; if ( (pnode->nServices & NODE_SPENTINDEX) != 0 && timestamp > pnode->lastspent ) { - request.resize(1); - request[0] = NSPV_SPENTINFO; - pnode->lastspent = timestamp; - pnode->PushMessage("getnSPV",request); - if ( ++numsent >= 3 ) - break; + if ( 0 ) // submit actual request + { + request.resize(1); + request[0] = NSPV_SPENTINFO; + pnode->lastspent = timestamp; + pnode->PushMessage("getnSPV",request); + if ( ++numsent >= 3 ) + break; + } } } } @@ -642,7 +646,7 @@ void komodo_nSPV(CNode *pto) { for (i=0; i pto->lastntzs ) From 0bc442146d754f8b203a6598197755ad3974b8ee Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:44:13 -1100 Subject: [PATCH 054/378] Syntax --- src/komodo_nSPV.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c2d8a6254..b7ad13931 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -291,7 +291,7 @@ struct NSPV_ntzsproofresp uint8_t *prevntz,*nextntz; }; -int32_t NSPV_rwntzproof(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsproofresp *ptr) +int32_t NSPV_rwntzsproofresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsproofresp *ptr) { int32_t len = 0; len += NSPV_rwntzproofshared(rwflag,&serialized[len],&ptr->common); @@ -646,10 +646,10 @@ void komodo_nSPV(CNode *pto) { for (i=0; i pto->lastntzs ) + if ( NSPV_utxos[i].prevht == 0 && timestamp > pto->lastntzs ) { request[0] = NSPV_NTZS; pto->lastntzs = timestamp; @@ -658,7 +658,7 @@ void komodo_nSPV(CNode *pto) } else if ( timestamp > pto->lastproof ) { - if ( NSPV_utxos[i].tx.size() == 0 ) + if ( NSPV_utxos[i].T.txlen == 0 ) { request[0] = NSPV_TXPROOF; pto->lastproof = timestamp; From ab0166b9974876b86da48fdc10fe8069557ba147 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 02:49:43 -1100 Subject: [PATCH 055/378] -defs --- src/komodo_defs.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 0a628b216..6106a6000 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -336,9 +336,5 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); -bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); -std::vector Mypubkey(); - - #endif From 6e0ce513e6dd28d41a20756073e4971fc1e19e20 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 03:01:36 -1100 Subject: [PATCH 056/378] Test --- src/komodo_nSPV.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b7ad13931..117438e51 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -124,7 +124,7 @@ struct NSPV_utxosresp { struct NSPV_utxoresp *utxos; int64_t total,interest; - int32_t pad32; + int32_t nodeheight; uint16_t numutxos,pad16; }; @@ -141,7 +141,7 @@ int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxosres } len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->total),&ptr->total); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nodeheight),&ptr->nodeheight); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); return(len); } @@ -372,6 +372,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 ) { tipheight = chainActive.LastTip()->GetHeight(); + ptr->nodeheight = tipheight; ptr->utxos = (struct NSPV_utxoresp *)calloc(ptr->numutxos,sizeof(*ptr->utxos)); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { From 1713a171cd5db7097685d67ce76a4bf69d1cc07a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 03:09:33 -1100 Subject: [PATCH 057/378] Adjust Len by sizeof ptr --- src/komodo_nSPV.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 117438e51..bd5b5cbef 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -388,12 +388,13 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check total += it->second.satoshis; n++; } - fprintf(stderr,"getaddressutxos for %s -> n.%d total %.8f interest %.8f\n",coinaddr,n,dstr(total),dstr(interest)); + len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); + fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); if ( n == ptr->numutxos ) { ptr->total = total; ptr->interest = interest; - return((int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos)); + return(len); } } if ( ptr->utxos != 0 ) @@ -461,6 +462,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req slen = NSPV_getaddressutxos(&U,coinaddr); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; + printf("slen.%d\n",slen); if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) { pfrom->PushMessage("nSPV",response); From feef1e6512477d848b8547ccf283530ca104430f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 03:20:09 -1100 Subject: [PATCH 058/378] Null term coinaddr --- src/komodo_nSPV.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index bd5b5cbef..06a251ca3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -452,12 +452,14 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } else if ( request[0] == NSPV_UTXOS ) { - if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) + //if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_utxosresp U; char coinaddr[64]; + printf("len.%d request[1] %d\n",len,request[1]); if ( len < 64 && request[1] == len-2 ) { memcpy(coinaddr,&request[2],request[1]); + coinaddr[request[1]] = 0; memset(&U,0,sizeof(U)); slen = NSPV_getaddressutxos(&U,coinaddr); response.resize(1 + slen); From 8d7a34bae0472cf22062ab63d725656f8ae256a6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 04:51:08 -1100 Subject: [PATCH 059/378] txproof and spent info --- src/komodo_nSPV.h | 101 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 24 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 06a251ca3..529e8adb6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -325,7 +325,7 @@ struct NSPV_spentinfo { struct NSPV_txproof spent; uint256 txid; - int32_t vout,height,spentvini; + int32_t vout,spentvini; }; int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinfo *ptr) // check mempool @@ -334,7 +334,6 @@ int32_t NSPV_rwspentinfo(int32_t rwflag,uint8_t *serialized,struct NSPV_spentinf len += NSPV_rwtxproof(rwflag,&serialized[len],&ptr->spent); len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->spentvini),&ptr->spentvini); return(len); } @@ -365,11 +364,13 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check mempool { - int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,txheight,n = 0,len = 0; + int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,maxlen,txheight,n = 0,len = 0; std::vector > unspentOutputs; fprintf(stderr,"getaddressutxos for %s\n",coinaddr); SetCCunspents(unspentOutputs,coinaddr,false); - if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 ) + maxlen = MAX_BLOCK_SIZE(tipheight) - 512; + maxlen /= sizeof(*ptr->utxos); + if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) { tipheight = chainActive.LastTip()->GetHeight(); ptr->nodeheight = tipheight; @@ -388,13 +389,16 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check total += it->second.satoshis; n++; } - len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); - fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); - if ( n == ptr->numutxos ) + if ( len < maxlen ) { - ptr->total = total; - ptr->interest = interest; - return(len); + len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); + fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); + if ( n == ptr->numutxos ) + { + ptr->total = total; + ptr->interest = interest; + return(len); + } } } if ( ptr->utxos != 0 ) @@ -409,13 +413,46 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) +int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) { - int32_t len = 0; - return(len); + int32_t flag = 0,len = 0; CTransaction tx; uint256 hashBlock; CBlock block; CBlockIndex *pindex; + { + LOCK(cs_main); + if (!GetTransaction(txid, tx, hashBlock, false)) + return(-1); + string strHex = EncodeHexTx(tx); + ptr->txlen = (int32_t)strHex.size() >> 1; + ptr->tx = (uint8_t *)calloc(1,ptr->txlen); + decode_hex(ptr->tx,ptr->txlen,strHex.c_str()); + ptr->txid = txid; + ptr->height = height; + if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) + { + BOOST_FOREACH(const CTransaction&tx, block.vtx) + { + if ( tx.GetHash() == txid ) + { + flag = 1; + break; + } + } + if ( flag != 0 ) + { + CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); + CMerkleBlock mb(block, setTxids); + ssMB << mb; + std::vector proof(ssMB.begin(), ssMB.end()); + ptr->txprooflen = (int32_t)proof.size(); + ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); + memcpy(ptr->txproof,&proof[0],ptr->txprooflen); + return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); + } + } + } + return(-1); } -int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) +int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) { int32_t len = 0; return(len); @@ -424,6 +461,20 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) { int32_t len = 0; + ptr->txid = txid; + ptr->vout = vout; + ptr->spentvini = -1; + len = (int32_t)(sizeof(*ptr) - sizeof(ptr->spent.tx) - sizeof(ptr->spent.txproof)); + if ( CCgetspenttxid(ptr->spent.txid,ptr->spentvini,ptr->spent.height,txid,vout) == 0 ) + { + if ( NSPV_gettxproof(&ptr->spent,ptr->spent.txid,ptr->spent.height) > 0 ) + len += ptr->spent.txlen + ptr->spent.txprooflen; + else + { + NSPV_txproof_purge(&ptr->spent); + return(-1); + } + } return(len); } @@ -452,10 +503,9 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } else if ( request[0] == NSPV_UTXOS ) { - //if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_utxosresp U; char coinaddr[64]; - printf("len.%d request[1] %d\n",len,request[1]); if ( len < 64 && request[1] == len-2 ) { memcpy(coinaddr,&request[2],request[1]); @@ -464,7 +514,6 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req slen = NSPV_getaddressutxos(&U,coinaddr); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; - printf("slen.%d\n",slen); if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) { pfrom->PushMessage("nSPV",response); @@ -552,15 +601,17 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(vout),&vout); iguana_rwbignum(0,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); memset(&S,0,sizeof(S)); - slen = NSPV_getspentinfo(&S,txid,vout); - response.resize(1 + slen); - response[0] = NSPV_SPENTINFORESP; - if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) + if ( (slen= NSPV_getspentinfo(&S,txid,vout)) > 0 ) { - pfrom->PushMessage("nSPV",response); - pfrom->lastspent = timestamp; + response.resize(1 + slen); + response[0] = NSPV_SPENTINFORESP; + if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastspent = timestamp; + } + NSPV_spentinfo_purge(&S); } - NSPV_spentinfo_purge(&S); } } } @@ -581,9 +632,11 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: + // deser and display fprintf(stderr,"got info response %u size.%d\n",timestamp,(int32_t)response.size()); // update current height and ntrz status break; case NSPV_UTXOSRESP: + // deser and process fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list break; case NSPV_NTZSRESP: From 64a690a5f602986be06d0a13d59e03be98c7210e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 04:56:58 -1100 Subject: [PATCH 060/378] Syntax --- src/komodo_nSPV.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 529e8adb6..dbe7c7775 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -423,7 +423,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) string strHex = EncodeHexTx(tx); ptr->txlen = (int32_t)strHex.size() >> 1; ptr->tx = (uint8_t *)calloc(1,ptr->txlen); - decode_hex(ptr->tx,ptr->txlen,strHex.c_str()); + decode_hex(ptr->tx,ptr->txlen,(char *)strHex.c_str()); ptr->txid = txid; ptr->height = height; if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) @@ -438,10 +438,12 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) } if ( flag != 0 ) { + set setTxids; CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); + setTxids.insert(txid); CMerkleBlock mb(block, setTxids); ssMB << mb; - std::vector proof(ssMB.begin(), ssMB.end()); + std::vector proof(ssMB.begin(), ssMB.end()); ptr->txprooflen = (int32_t)proof.size(); ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); memcpy(ptr->txproof,&proof[0],ptr->txprooflen); From d6833b0361c2d366f838eab923b574fee4f8f62a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:17:43 -1100 Subject: [PATCH 061/378] Finish all but one full node side functions --- src/komodo_defs.h | 1 + src/komodo_nSPV.h | 183 ++++++++++++++++++++++++++++++++------------ src/komodo_notary.h | 24 ++++++ 3 files changed, 158 insertions(+), 50 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 6106a6000..cef5c4d1c 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -335,6 +335,7 @@ int64_t komodo_pricemult(int32_t ind); int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblocks); uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); +int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height); #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index dbe7c7775..f5e3cb7d9 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1,7 +1,3 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2014 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. /****************************************************************************** * Copyright © 2014-2019 The SuperNET Developers. * @@ -18,12 +14,11 @@ * * ******************************************************************************/ -// todo: make sure no files are updated -// finalize structs, de/serialization -// rpc calls +// todo: +// make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) // validate proofs - // make sure to sanity check all vector lengths on receipt +// determine if it makes sense to be scanning mempool for the utxo/spentinfo requests #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H @@ -286,7 +281,8 @@ int32_t NSPV_rwntzproofshared(int32_t rwflag,uint8_t *serialized,struct NSPV_ntz struct NSPV_ntzsproofresp { struct NSPV_ntzproofshared common; - uint32_t pad32; + uint256 prevtxid,nexttxid; + uint32_t pad32,prevtxidht,nexttxidht; uint16_t prevlen,nextlen; uint8_t *prevntz,*nextntz; }; @@ -295,7 +291,11 @@ int32_t NSPV_rwntzsproofresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzs { int32_t len = 0; len += NSPV_rwntzproofshared(rwflag,&serialized[len],&ptr->common); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->prevtxid),(uint8_t *)&ptr->prevtxid); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->nexttxid),(uint8_t *)&ptr->nexttxid); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevtxidht),&ptr->prevtxidht); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nexttxidht),&ptr->nexttxidht); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevlen,&ptr->prevntz); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nextlen,&ptr->nextntz); return(len); @@ -348,6 +348,16 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) } // on fullnode: +// NSPV_get... functions need to return the exact serialized length, which is the size of the structure minus size of pointers, plus size of allocated data + +uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) +{ + uint256 ntztxid; + *ntzheightp = height + 2; // random value; + // find notarization for height, return its txid and set *ntzheightp + fprintf(stderr,"implement NSPV_getnotarization_txid\n"); + return(ntztxid); +} int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) { @@ -357,7 +367,7 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) ptr->height = pindex->GetHeight(); ptr->blockhash = pindex->GetBlockHash(); ptr->notarization.height = komodo_notarized_height(&prevMoMheight,&ptr->notarization.blockhash,&ptr->notarization.othertxid); - //ptr->notarization.txidheight = komodo_findnotarization(&ptr->notarization.txid,ptr->notarization.height,ptr->notarization.blockhash); + ptr->notarization.txid = NSPV_getnotarization_txid(&ptr->notarization.txidheight,ptr->notarization.height); return(sizeof(*ptr)); } else return(-1); } @@ -407,57 +417,128 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check return(0); } +int32_t NSPV_npextract(struct NSPV_ntz *ptr,struct notarized_checkpoint *np) +{ + int32_t ntzheight; + ptr->blockhash = np->notarized_hash; + ptr->height = np->notarized_height; + ptr->txidheight = np->nHeight; + ptr->othertxid = np->notarized_desttxid; + ptr->txid = NSPV_getnotarization_txid(&ntzheight,ptr->height); + if ( ntzheight != ptr->txidheight ) + { + fprintf(stderr,"NSPV_npextract ntzheight.%d != ptr->txidheight.%d\n",ntzheight,ptr->txidheight); + return(-1); + } else return(0); +} + int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { - int32_t len = 0; + struct notarized_checkpoint *nps[2]; + if ( komodo_notarized_bracket(nps,height) == 0 ) + { + if ( nps[0] != 0 ) + { + if ( NSPV_npextract(&ptr->prevntz,nps[0]) < 0 ) + return(-1); + } + if ( nps[1] != 0 ) + { + if ( NSPV_npextract(&ptr->nextntz,nps[1]) < 0 ) + return(-1); + } + } return(sizeof(*ptr)); } +uint8_t *NSPV_getrawtx(uint256 &hashBlock,int32_t *txlenp,uint256 txid) +{ + CTransaction tx; uint8_t *rawtx = 0; + *txlenp = 0; + { + LOCK(cs_main); + if (!GetTransaction(txid, tx, hashBlock, false)) + return(0); + string strHex = EncodeHexTx(tx); + *txlenp = (int32_t)strHex.size() >> 1; + rawtx = (uint8_t *)calloc(1,*txlenp); + decode_hex(rawtx,*txlenp,(char *)strHex.c_str()); + } + return(rawtx); +} + int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) { - int32_t flag = 0,len = 0; CTransaction tx; uint256 hashBlock; CBlock block; CBlockIndex *pindex; + int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; + if ( (ptr->tx= NSPV_getrawtx(hashBlock,&ptr->txlen,txid)) == 0 ) + return(-1); + ptr->txid = txid; + ptr->height = height; + if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) { - LOCK(cs_main); - if (!GetTransaction(txid, tx, hashBlock, false)) - return(-1); - string strHex = EncodeHexTx(tx); - ptr->txlen = (int32_t)strHex.size() >> 1; - ptr->tx = (uint8_t *)calloc(1,ptr->txlen); - decode_hex(ptr->tx,ptr->txlen,(char *)strHex.c_str()); - ptr->txid = txid; - ptr->height = height; - if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) + BOOST_FOREACH(const CTransaction&tx, block.vtx) { - BOOST_FOREACH(const CTransaction&tx, block.vtx) + if ( tx.GetHash() == txid ) { - if ( tx.GetHash() == txid ) - { - flag = 1; - break; - } - } - if ( flag != 0 ) - { - set setTxids; - CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); - setTxids.insert(txid); - CMerkleBlock mb(block, setTxids); - ssMB << mb; - std::vector proof(ssMB.begin(), ssMB.end()); - ptr->txprooflen = (int32_t)proof.size(); - ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); - memcpy(ptr->txproof,&proof[0],ptr->txprooflen); - return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); + flag = 1; + break; } } + if ( flag != 0 ) + { + set setTxids; + CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); + setTxids.insert(txid); + CMerkleBlock mb(block, setTxids); + ssMB << mb; + std::vector proof(ssMB.begin(), ssMB.end()); + ptr->txprooflen = (int32_t)proof.size(); + ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); + memcpy(ptr->txproof,&proof[0],ptr->txprooflen); + return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); + } + } + return(-1); +} + +int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) +{ + CBlockIndex *pindex; + if ( (pindex= komodo_chainactive(height)) != 0 ) + { + hdr->nVersion = pindex->nVersion; + hdr->hashPrevBlock = pindex->hashPrevBlock; + hdr->hashMerkleRoot = pindex->hashMerkleRoot; + hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; + hdr->nTime = pindex->nTime; + hdr->nBits = pindex->nBits; + hdr->nNonce = pindex->nNonce; + memcpy(hdr->nSolution,pindex->nSolution,sizeof(hdr->nSolution)); + return(sizeof(*hdr)); } return(-1); } int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) { - int32_t len = 0; - return(len); + int32_t i; + ptr->prevht = prevht; + ptr->nextht = nextht; + ptr->numhdrs = (nextht - prevht + 1); + ptr->hdrs = (struct NSPV_equihdr *)calloc(ptr->numhdrs,sizeof(*ptr->hdrs)); + for (i=0; inumhdrs; i++) + { + if ( NSPV_setequihdr(&ptr->hdrs[i],prevht+i) < 0 ) + { + free(ptr->hdrs); + return(-1); + } + } + ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); + ptr->prevntz = NSPV_getrawtx(&ptr->prevlen,ptr->prevtxid); + ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); + ptr->nextntz = NSPV_getrawtx(&ptr->nextlen,ptr->nexttxid); + return(sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); } int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) @@ -558,15 +639,17 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( prevht != 0 && nextht != 0 && nextht >= prevht ) { memset(&P,0,sizeof(P)); - slen = NSPV_getntzsproofresp(&P,prevht,nextht); - response.resize(1 + slen); - response[0] = NSPV_NTZPROOFRESP; - if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) + if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) { - pfrom->PushMessage("nSPV",response); - pfrom->lastproof = timestamp; + response.resize(1 + slen); + response[0] = NSPV_NTZPROOFRESP; + if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->lastproof = timestamp; + } + NSPV_ntzsproofresp_purge(&P); } - NSPV_ntzsproofresp_purge(&P); } } } diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 028bc4dc2..63531f7fe 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -392,6 +392,30 @@ int32_t komodo_MoMdata(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,in return(0); } +int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height) +{ + struct notarized_checkpoint *np; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; + memset(nps,0,sizeof(*nps)*2); + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + { + if ( sp->NUM_NPOINTS > 0 ) + { + for (i=0; iNUM_NPOINTS; i++) + { + np = &sp->NPOINTS[i]; + if ( np->nHeight <= height ) + nps[0] = np; + else + { + nps[1] = np; + return(0); + } + } + } + } + return(-1); +} + int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp) { struct notarized_checkpoint *np = 0; int32_t i=0,flag = 0; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; From 119cbe5f3e883ca665d4ce0d5a381d58cde3cc96 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:24:04 -1100 Subject: [PATCH 062/378] Syntax --- src/komodo_nSPV.h | 14 ++++++++++++-- src/komodo_notary.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f5e3cb7d9..6057fb59f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -353,7 +353,15 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) { uint256 ntztxid; - *ntzheightp = height + 2; // random value; + if ( ntxheightp == 0 ) // search consecutive blocks + { + *ntzheightp = height + 1; + // iterate + } + else + { + // check *ntzheightp + } // find notarization for height, return its txid and set *ntzheightp fprintf(stderr,"implement NSPV_getnotarization_txid\n"); return(ntztxid); @@ -367,6 +375,7 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) ptr->height = pindex->GetHeight(); ptr->blockhash = pindex->GetBlockHash(); ptr->notarization.height = komodo_notarized_height(&prevMoMheight,&ptr->notarization.blockhash,&ptr->notarization.othertxid); + ptr->notarization.txidheight = 0; ptr->notarization.txid = NSPV_getnotarization_txid(&ptr->notarization.txidheight,ptr->notarization.height); return(sizeof(*ptr)); } else return(-1); @@ -424,6 +433,7 @@ int32_t NSPV_npextract(struct NSPV_ntz *ptr,struct notarized_checkpoint *np) ptr->height = np->notarized_height; ptr->txidheight = np->nHeight; ptr->othertxid = np->notarized_desttxid; + ntzheight = ptr->txidheight; ptr->txid = NSPV_getnotarization_txid(&ntzheight,ptr->height); if ( ntzheight != ptr->txidheight ) { @@ -451,7 +461,7 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -uint8_t *NSPV_getrawtx(uint256 &hashBlock,int32_t *txlenp,uint256 txid) +uint8_t *NSPV_getrawtx(uint256 &hashBlock,int16_t *txlenp,uint256 txid) { CTransaction tx; uint8_t *rawtx = 0; *txlenp = 0; diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 63531f7fe..a80e035bd 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -394,7 +394,7 @@ int32_t komodo_MoMdata(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,in int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height) { - struct notarized_checkpoint *np; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; + int32_t i; struct notarized_checkpoint *np; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; memset(nps,0,sizeof(*nps)*2); if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) { From 6acf529efdd0c922dd352a7c19fd18989ef28354 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:28:13 -1100 Subject: [PATCH 063/378] Syntax --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6057fb59f..d53f00635 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -353,7 +353,7 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) { uint256 ntztxid; - if ( ntxheightp == 0 ) // search consecutive blocks + if ( ntzheightp == 0 ) // search consecutive blocks { *ntzheightp = height + 1; // iterate @@ -461,7 +461,7 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -uint8_t *NSPV_getrawtx(uint256 &hashBlock,int16_t *txlenp,uint256 txid) +uint8_t *NSPV_getrawtx(uint256 &hashBlock,uptr->notarization.txidheightint16_t *txlenp,uint256 txid) { CTransaction tx; uint8_t *rawtx = 0; *txlenp = 0; From 9a4243e2bfb97bfa1548a36ec1c334646e71e29e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:29:37 -1100 Subject: [PATCH 064/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d53f00635..67ef28246 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -461,7 +461,7 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -uint8_t *NSPV_getrawtx(uint256 &hashBlock,uptr->notarization.txidheightint16_t *txlenp,uint256 txid) +uint8_t *NSPV_getrawtx(uint256 &hashBlock,int16_t *txlenp,uint256 txid) { CTransaction tx; uint8_t *rawtx = 0; *txlenp = 0; From 8cfd799b29145f264d9960f6e08ed2a3829b79b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:31:03 -1100 Subject: [PATCH 065/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 67ef28246..ee400e3c8 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -461,7 +461,7 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -uint8_t *NSPV_getrawtx(uint256 &hashBlock,int16_t *txlenp,uint256 txid) +uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) { CTransaction tx; uint8_t *rawtx = 0; *txlenp = 0; From 031455e9b0669fdb1c0e8670d156b443e7f4849a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:37:31 -1100 Subject: [PATCH 066/378] Syntax --- src/komodo_nSPV.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index ee400e3c8..b868c3ad3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -532,15 +532,15 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) { int32_t i; - ptr->prevht = prevht; - ptr->nextht = nextht; - ptr->numhdrs = (nextht - prevht + 1); - ptr->hdrs = (struct NSPV_equihdr *)calloc(ptr->numhdrs,sizeof(*ptr->hdrs)); - for (i=0; inumhdrs; i++) + ptr->prevtxidht = prevht; + ptr->nexttxidht = nextht; + ptr->common.numhdrs = (nextht - prevht + 1); + ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); + for (i=0; icommon.numhdrs; i++) { - if ( NSPV_setequihdr(&ptr->hdrs[i],prevht+i) < 0 ) + if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { - free(ptr->hdrs); + free(ptr->common.hdrs); return(-1); } } From 42cfe3265b1ebbe3277df7b963061bf02442b4ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:41:05 -1100 Subject: [PATCH 067/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b868c3ad3..9f116e7aa 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -282,7 +282,7 @@ struct NSPV_ntzsproofresp { struct NSPV_ntzproofshared common; uint256 prevtxid,nexttxid; - uint32_t pad32,prevtxidht,nexttxidht; + int32_t pad32,prevtxidht,nexttxidht; uint16_t prevlen,nextlen; uint8_t *prevntz,*nextntz; }; From 29bdb27225b215a78284a6fb31a5c084fc9ac240 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:46:25 -1100 Subject: [PATCH 068/378] Syntax --- src/komodo_nSPV.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9f116e7aa..8126361e3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -523,7 +523,7 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) hdr->nTime = pindex->nTime; hdr->nBits = pindex->nBits; hdr->nNonce = pindex->nNonce; - memcpy(hdr->nSolution,pindex->nSolution,sizeof(hdr->nSolution)); + memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); return(sizeof(*hdr)); } return(-1); @@ -531,7 +531,7 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) { - int32_t i; + int32_t i; uint256 hashBlock; ptr->prevtxidht = prevht; ptr->nexttxidht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); @@ -545,9 +545,9 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 } } ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); - ptr->prevntz = NSPV_getrawtx(&ptr->prevlen,ptr->prevtxid); + ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); - ptr->nextntz = NSPV_getrawtx(&ptr->nextlen,ptr->nexttxid); + ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nextlen,ptr->nexttxid); return(sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); } From 1a16cdc29c4aebb6ee62d27d7ef7364aeba92b64 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 06:48:44 -1100 Subject: [PATCH 069/378] Pprev --- src/komodo_nSPV.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 8126361e3..75d7ff2bb 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -517,7 +517,9 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) if ( (pindex= komodo_chainactive(height)) != 0 ) { hdr->nVersion = pindex->nVersion; - hdr->hashPrevBlock = pindex->hashPrevBlock; + if ( pindex->pprev == 0 ) + return(-1); + hdr->hashPrevBlock = pindex->pprev->GetBlockHash(); hdr->hashMerkleRoot = pindex->hashMerkleRoot; hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; hdr->nTime = pindex->nTime; From bc89c1de30d3c40ddb75ed82f67e41109b87ad33 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 07:03:28 -1100 Subject: [PATCH 070/378] -prints --- src/main.cpp | 3 ++- src/net.h | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f214eaa5f..5b62f5747 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7052,7 +7052,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { const CChainParams& chainparams = Params(); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); -fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); + if ( KOMODO_NSPV != 0 ) + fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n"); diff --git a/src/net.h b/src/net.h index dce52aa8b..dc21d1133 100644 --- a/src/net.h +++ b/src/net.h @@ -463,7 +463,7 @@ public: void PushMessage(const char* pszCommand) { - fprintf(stderr,"push.(%s)\n",pszCommand); + //fprintf(stderr,"push.(%s)\n",pszCommand); try { BeginMessage(pszCommand); @@ -479,7 +479,7 @@ public: template void PushMessage(const char* pszCommand, const T1& a1) { - fprintf(stderr,"push.(%s)\n",pszCommand); + //fprintf(stderr,"push.(%s)\n",pszCommand); try { BeginMessage(pszCommand); From 00fb6f3169e039a983b4dec5d579f327b2a05529 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 07:24:21 -1100 Subject: [PATCH 071/378] +print --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 75d7ff2bb..2423b4919 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -607,6 +607,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req coinaddr[request[1]] = 0; memset(&U,0,sizeof(U)); slen = NSPV_getaddressutxos(&U,coinaddr); + printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) From d1c66a47aeb2e9d013daf1f17cc2cb3c3686b848 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 07:27:59 -1100 Subject: [PATCH 072/378] Test --- src/komodo_nSPV.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 2423b4919..fa02fe187 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -836,7 +836,7 @@ void komodo_nSPV(CNode *pto) if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) { // get utxo since lastheight - if ( (rand() % 100) < 10 ) + //if ( (rand() % 100) < 10 ) { char coinaddr[64]; int32_t slen; Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG); @@ -848,6 +848,7 @@ void komodo_nSPV(CNode *pto) request[1] = slen; memcpy(&request[2],coinaddr,slen); NSPV_lastutxos = pto->lastutxos = timestamp; + fprintf(stderr,"request address %s\n",coinaddr); pto->PushMessage("getnSPV",request); } } From c3343e237e2770a665dcc75c87d6e30033dc09ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 20:36:31 -1100 Subject: [PATCH 073/378] Getinfo, login, listunspent, spentinfo rpc calls --- src/komodo_defs.h | 1 + src/komodo_nSPV.h | 333 ++++++++++++++++++++++++++++------------- src/net.h | 2 +- src/rpc/server.cpp | 6 + src/rpc/server.h | 5 + src/wallet/rpcdump.cpp | 37 +++++ 6 files changed, 275 insertions(+), 109 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index cef5c4d1c..6ac777898 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -337,5 +337,6 @@ uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 int32_t komodo_currentheight(); int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height); +uint256 Parseuint256(const char *hexstr); #endif diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fa02fe187..64d432717 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -575,13 +575,14 @@ int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t len,slen; std::vector response; uint32_t timestamp; - timestamp = (uint32_t)time(NULL); + int32_t len,slen,ind; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); if ( (len= request.size()) > 0 ) { + if ( (ind= request[0]>>1) >= sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes) ) + ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; if ( request[0] == NSPV_INFO ) // info { - if ( len == 1 && timestamp > pfrom->lastinfo + ASSETCHAINS_BLOCKTIME/2 ) + if ( len == 1 && timestamp > pfrom->prevtimes[ind] + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_inforesp I; memset(&I,0,sizeof(I)); @@ -591,14 +592,14 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req response[0] = NSPV_INFORESP; NSPV_rwinforesp(1,&response[1],&I); pfrom->PushMessage("nSPV",response); - pfrom->lastinfo = timestamp; + pfrom->prevtimes[ind] = timestamp; NSPV_inforesp_purge(&I); } } } else if ( request[0] == NSPV_UTXOS ) { - if ( timestamp > pfrom->lastutxos + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_utxosresp U; char coinaddr[64]; if ( len < 64 && request[1] == len-2 ) @@ -606,43 +607,47 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req memcpy(coinaddr,&request[2],request[1]); coinaddr[request[1]] = 0; memset(&U,0,sizeof(U)); - slen = NSPV_getaddressutxos(&U,coinaddr); - printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); - response.resize(1 + slen); - response[0] = NSPV_UTXOSRESP; - if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) + if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) { - pfrom->PushMessage("nSPV",response); - pfrom->lastutxos = timestamp; + printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); + response.resize(1 + slen); + response[0] = NSPV_UTXOSRESP; + if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_utxosresp_purge(&U); } - NSPV_utxosresp_purge(&U); } } } else if ( request[0] == NSPV_NTZS ) { - if ( timestamp > pfrom->lastntzs ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_ntzsresp N; int32_t height; if ( len == 1+sizeof(height) ) { iguana_rwnum(0,&request[1],sizeof(height),&height); memset(&N,0,sizeof(N)); - slen = NSPV_getntzsresp(&N,height); - response.resize(1 + slen); - response[0] = NSPV_NTZSRESP; - if ( NSPV_rwntzsresp(1,&response[1],&N) == slen ) + if ( (slen= NSPV_getntzsresp(&N,height)) > 0 ) { - pfrom->PushMessage("nSPV",response); - pfrom->lastntzs = timestamp; + response.resize(1 + slen); + response[0] = NSPV_NTZSRESP; + if ( NSPV_rwntzsresp(1,&response[1],&N) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_ntzsresp_purge(&N); } - NSPV_ntzsresp_purge(&N); } } } else if ( request[0] == NSPV_NTZPROOF ) { - if ( timestamp > pfrom->lastproof ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_ntzsproofresp P; int32_t prevht,nextht; if ( len == 1+sizeof(prevht)+sizeof(nextht) ) @@ -659,7 +664,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); - pfrom->lastproof = timestamp; + pfrom->prevtimes[ind] = timestamp; } NSPV_ntzsproofresp_purge(&P); } @@ -669,7 +674,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } else if ( request[0] == NSPV_TXPROOF ) { - if ( timestamp > pfrom->lastproof ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_txproof P; uint256 txid; int32_t height; if ( len == 1+sizeof(txid)+sizeof(height) ) @@ -677,21 +682,23 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); memset(&P,0,sizeof(P)); - slen = NSPV_gettxproof(&P,txid,height); - response.resize(1 + slen); - response[0] = NSPV_TXPROOFRESP; - if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) + if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) { - pfrom->PushMessage("nSPV",response); - pfrom->lastproof = timestamp; + response.resize(1 + slen); + response[0] = NSPV_TXPROOFRESP; + if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_txproof_purge(&P); } - NSPV_txproof_purge(&P); } } } else if ( request[0] == NSPV_SPENTINFO ) { - if ( timestamp > pfrom->lastspent ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_spentinfo S; int32_t vout; uint256 txid; if ( len == 1+sizeof(txid)+sizeof(vout) ) @@ -706,7 +713,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) { pfrom->PushMessage("nSPV",response); - pfrom->lastspent = timestamp; + pfrom->prevtimes[ind] = timestamp; } NSPV_spentinfo_purge(&S); } @@ -716,12 +723,179 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } } -// on nSPV client -uint32_t NSPV_lastinfo,NSPV_lastutxos; +// nSPV client +uint32_t NSPV_lastinfo,NSPV_lastutxos,NSPV_logintime; +char NSPV_wifstr[64]; +std::string NSPV_address; int32_t NSPV_numutxos,NSPV_numspends; +struct NSPV_inforesp NSPV_inforesult; +struct NSPV_utxosresp NSPV_utxosresult; struct NSPV_utxo *NSPV_utxos; struct NSPV_spentinfo *NSPV_spends; +CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint32_t mask,int32_t ind) +{ + int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); + if ( pnode == 0 ) + { + LOCK(cs_vNodes); + BOOST_FOREACH(CNode *pnode,vNodes) + { + if ( pnode->hSocket == INVALID_SOCKET ) + continue; + if ( (pnode->nServices & mask) == mask && timestamp > pnode->prevtimes[ind] ) + { + flag = 1; + break; + } + } + } else flag = 1; + if ( pnode != 0 ) + { + std::vector request; + request.resize(len); + memcpy(&request[0],msg,len); + pnode->PushMessage("getnSPV",request); + pnode->prevtimes[ind] = timestamp; + return(pnode); + } + return(0); +} + +void komodo_NSPV_spentinfoclear() +{ + if ( NSPV_spends != 0 ) + free(NSPV_spends); + NSPV_spends = 0; + NSPV_numspends = 0; +} + +UniValue NSPV_spentinfo_json(struct NSPV_spentinfo *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("vout",(int64_t)ptr->vout)); + result.push_back(Pair("spentheight",(int64_t)ptr->spent.height)); + result.push_back(Pair("spenttxid",ptr->spent.txid.GetHex())); + result.push_back(Pair("spentvini",(int64_t)ptr->spentvini)); + result.push_back(Pair("spenttxlen",(int64_t)ptr->spent.txlen)); + result.push_back(Pair("spenttxprooflen",(int64_t)ptr->spent.txprooflen)); + return(result); +} + +UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("notarized_height",(int64_t)ptr->height)); + result.push_back(Pair("notarized_blockhash",ptr->blockhash.GetHex())); + result.push_back(Pair("notarization_txid",ptr->txid.GetHex())); + result.push_back(Pair("notarization_txidheight",(int64_t)ptr->txidheight)); + result.push_back(Pair("notarization_desttxid",ptr->othertxid.GetHex())); + return(result); +} + +UniValue NSPV_getinfo() +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); + result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); + return(result); +} + +UniValue NSPV_utxoresp_json(struct NSPV_utxosresp *utxos,int32_t numutxos) +{ + UniValue array(UniValue::VARR); int32_t i; + for (i=0; iutxos,ptr->numutxos))); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); + result.push_back(Pair("balance",AmountFromValue(ptr->total))); + result.push_back(Pair("interest",AmountFromValue(ptr->interest))); + return(result); +} + +UniValue NSPV_spentinfo(uint256 txid,int32_t vout) +{ + uint8_t msg[64]; struct NSPV_spentinfo I; int32_t i,len = 0; + for (i=0; i>1) != 0 ) + usleep(250000); + for (i=0; i>1) != 0 ) + usleep(250000); + } + if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) + { + return(NSPV_utxosresp_json(NSPV_utxosresult)); + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","no utxos result")); + return(result); +} + void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); @@ -730,11 +904,12 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: - // deser and display - fprintf(stderr,"got info response %u size.%d\n",timestamp,(int32_t)response.size()); // update current height and ntrz status + NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); + fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status break; case NSPV_UTXOSRESP: - // deser and process + NSPV_utxosresp_purge(&NSPV_utxosresult); + NSPV_rwutxosresp(0,&response[1],&NSPV_utxosresult); fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list break; case NSPV_NTZSRESP: @@ -755,50 +930,18 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r } } -void komodo_NSPV_spentinfoclear() +void komodo_nSPV(CNode *pto) // polling loop from SendMessages { - if ( NSPV_spends != 0 ) - free(NSPV_spends); - NSPV_spends = 0; - NSPV_numspends = 0; -} - -struct NSPV_spentinfo komodo_NSPV_spentinfo(uint256 txid,int32_t vout) // just a primitive example of how to add new rpc to p2p msg -{ - std::vector request; struct NSPV_spentinfo I; int32_t i,numsent = 0; uint32_t timestamp; - timestamp = (uint32_t)time(NULL); - for (i=0; i NSPV_logintime+60 ) { - I = NSPV_spends[i]; - if ( I.txid == txid && I.vout == vout ) - return(I); + fprintf(stderr,"scrub wif from NSPV memory\n"); + memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + NSPV_logintime = 0; } - LOCK(cs_vNodes); - BOOST_FOREACH(CNode *pnode, vNodes) - { - if ( pnode->hSocket == INVALID_SOCKET ) - continue; - if ( (pnode->nServices & NODE_SPENTINDEX) != 0 && timestamp > pnode->lastspent ) - { - if ( 0 ) // submit actual request - { - request.resize(1); - request[0] = NSPV_SPENTINFO; - pnode->lastspent = timestamp; - pnode->PushMessage("getnSPV",request); - if ( ++numsent >= 3 ) - break; - } - } - } -} - -void komodo_nSPV(CNode *pto) -{ - std::vector request; int32_t i; uint32_t timestamp = (uint32_t)time(NULL); if ( (pto->nServices & NODE_NSPV) == 0 ) return; - if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) + /*if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) { for (i=0; i NSPV_lastutxos + ASSETCHAINS_BLOCKTIME/2 ) + }*/ + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + ASSETCHAINS_BLOCKTIME/2 ) { - if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > pto->lastutxos + ASSETCHAINS_BLOCKTIME ) + if ( (rand() % 100) < 10 ) // randomize which peer to query { - // get utxo since lastheight - //if ( (rand() % 100) < 10 ) - { - char coinaddr[64]; int32_t slen; - Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG); - slen = (int32_t)strlen(coinaddr); - if ( slen < 64 ) - { - request.resize(1 + 1 + slen); - request[0] = NSPV_UTXOS; - request[1] = slen; - memcpy(&request[2],coinaddr,slen); - NSPV_lastutxos = pto->lastutxos = timestamp; - fprintf(stderr,"request address %s\n",coinaddr); - pto->PushMessage("getnSPV",request); - } - } - } - } - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->lastinfo + ASSETCHAINS_BLOCKTIME ) - { - if ( (rand() % 100) < 10 ) - { - // query current height, blockhash, notarization info - request.resize(1); - request[0] = NSPV_INFO; - NSPV_lastinfo = pto->lastinfo = timestamp; - pto->PushMessage("getnSPV",request); + msg[len++] = NSPV_INFO; + NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } } diff --git a/src/net.h b/src/net.h index dc21d1133..0b8dcd901 100644 --- a/src/net.h +++ b/src/net.h @@ -276,7 +276,7 @@ public: int64_t nLastRecv; int64_t nTimeConnected; int64_t nTimeOffset; - uint32_t lastutxos,lastinfo,lastntzs,lastproof,lastspent; + uint32_t prevtimes[8]; CAddress addr; std::string addrName; CService addrLocal; diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 75360fd35..937ae0632 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -416,6 +416,12 @@ static const CRPCCommand vRPCCommands[] = { "FSM", "FSMlist", &FSMlist, true }, { "FSM", "FSMinfo", &FSMinfo, true }, + // fsm + { "nSPV", "nspv_getinfo", &nspv_getinfo, true }, + { "nSPV", "nspv_login", &nspv_login, true }, + { "nSPV", "nspv_listunspent", &nspv_listunspent, true }, + { "nSPV", "nspv_spentinfo", &nspv_spentinfo, true }, + // rewards { "rewards", "rewardslist", &rewardslist, true }, { "rewards", "rewardsinfo", &rewardsinfo, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index d447d2472..bc895ee53 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -455,6 +455,11 @@ extern UniValue importgatewaypendingdeposits(const UniValue& params, bool fHelp) extern UniValue importgatewaypendingwithdraws(const UniValue& params, bool fHelp); extern UniValue importgatewayprocessed(const UniValue& params, bool fHelp); +extern UniValue nspv_getinfo(const UniValue& params, bool fHelp); +extern UniValue nspv_login(const UniValue& params, bool fHelp); +extern UniValue nspv_listunspent(const UniValue& params, bool fHelp); +extern UniValue nspv_spentinfo(const UniValue& params, bool fHelp); + extern UniValue getblocksubsidy(const UniValue& params, bool fHelp); extern UniValue z_exportkey(const UniValue& params, bool fHelp); // in rpcdump.cpp diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index d35b09a96..f133b1c07 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -971,3 +971,40 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) return EncodeViewingKey(vk); } + +UniValue NSPV_getinfo(); +UniValue NSPV_login(char *wifstr); +UniValue NSPV_addressutxos(char *coinaddr); +UniValue NSPV_spentinfo(uint256 txid,int32_t vout); + +UniValue nspv_getinfo(const UniValue& params, bool fHelp) +{ + if ( fHelp || params.size() != 0 ) + throw runtime_error("nspv_getinfo\n"); + return(NSPV_getinfo()); +} + +UniValue nspv_login(const UniValue& params, bool fHelp) +{ + if ( fHelp || params.size() != 1 ) + throw runtime_error("nspv_login wif\n"); + return(NSPV_login((char *)params[0].get_str().c_str())); +} + +UniValue nspv_listunspent(const UniValue& params, bool fHelp) +{ + if ( fHelp || params.size() != 1 ) + throw runtime_error("nspv_listunspent address\n"); + return(NSPV_addressutxos((char *)params[0].get_str().c_str())); +} + +UniValue nspv_spentinfo(const UniValue& params, bool fHelp) +{ + uint256 txid; int32_t vout; + if ( fHelp || params.size() != 2 ) + throw runtime_error("nspv_spentinfo txid vout\n"); + txid = Parseuint256((char *)params[0].get_str().c_str()); + vout = atoi((char *)params[1].get_str().c_str()); + return(NSPV_spentinfo(txid,vout)); +} + From eb851fe8833e041e0df6d5f573fc8d0e36313cc2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 20:53:31 -1100 Subject: [PATCH 074/378] Syntax --- src/komodo_nSPV.h | 18 +++++++++--------- src/main.cpp | 17 +++++------------ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 64d432717..fccdf7280 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -799,24 +799,24 @@ UniValue NSPV_getinfo() { UniValue result(UniValue::VOBJ); result.push_back(Pair("result","success")); - result.push_back(Pair("height",(int64_t)ptr->height)); - result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); - result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); + result.push_back(Pair("height",(int64_t)NSPV_inforesult.height)); + result.push_back(Pair("chaintip",NSPV_inforesult.blockhash.GetHex())); + result.push_back(Pair("notarization",NSPV_ntz_json(&NSPV_inforesult.notarization))); return(result); } -UniValue NSPV_utxoresp_json(struct NSPV_utxosresp *utxos,int32_t numutxos) +UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) { UniValue array(UniValue::VARR); int32_t i; for (i=0; i payload; - vRecv >> payload; - komodo_nSPVresp(pfrom,payload); - return(true); - } - fprintf(stderr,"ignore message %s\n",strCommand.c_str()); + std::vector payload; + vRecv >> payload; + komodo_nSPVresp(pfrom,payload); return(true); } @@ -8228,11 +8223,9 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } state.fShouldBan = false; } + komodo_nSPV(pto); if ( KOMODO_NSPV != 0 ) - { - komodo_nSPV(pto); return(true); - } BOOST_FOREACH(const CBlockReject& reject, state.rejects) pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); state.rejects.clear(); From f443435d003c406dd4897082a60bbe7d6f64dfe6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 20:56:12 -1100 Subject: [PATCH 075/378] Syntax --- src/komodo_nSPV.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fccdf7280..a0c078c77 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -810,7 +810,7 @@ UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) UniValue array(UniValue::VARR); int32_t i; for (i=0; iutxos,ptr->numutxos))); - result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("height",(int64_t)ptr->nodeheight)); result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); result.push_back(Pair("balance",AmountFromValue(ptr->total))); result.push_back(Pair("interest",AmountFromValue(ptr->interest))); @@ -866,7 +866,7 @@ UniValue NSPV_login(char *wifstr) NSPV_logintime = (uint32_t)time(NULL); result.push_back(Pair("result","success")); result.push_back(Pair("status","wif will expire in 60 seconds")); - CKey key = DecodeSecret(strSecret); + CKey key = DecodeSecret(wifstr); CPubKey pubkey = key.GetPubKey(); //assert(key.VerifyPubKey(pubkey)); CKeyID vchAddress = pubkey.GetID(); @@ -880,7 +880,7 @@ UniValue NSPV_addressutxos(char *coinaddr) UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t slen,len = 0; if ( NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) { - slen = (int32_t)strlen(coinaddr) + slen = (int32_t)strlen(coinaddr); msg[len++] = NSPV_UTXOS; msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; @@ -889,7 +889,7 @@ UniValue NSPV_addressutxos(char *coinaddr) } if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) { - return(NSPV_utxosresp_json(NSPV_utxosresult)); + return(NSPV_utxosresp_json(&NSPV_utxosresult)); } result.push_back(Pair("result","error")); result.push_back(Pair("error","no utxos result")); From c435986dd27936e6b0afaa2529c31956fa054318 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 20:57:42 -1100 Subject: [PATCH 076/378] extern CAmount AmountFromValue(const UniValue& value); --- src/komodo_nSPV.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a0c078c77..3c5d79b22 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -724,6 +724,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } // nSPV client +extern CAmount AmountFromValue(const UniValue& value); + uint32_t NSPV_lastinfo,NSPV_lastutxos,NSPV_logintime; char NSPV_wifstr[64]; std::string NSPV_address; From a0d5a1f612a8679e6c46be7015b53a01a7eb35ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:06:10 -1100 Subject: [PATCH 077/378] uint256 Parseuint256(const char *hexstr); --- src/wallet/rpcdump.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index f133b1c07..263145320 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -976,6 +976,7 @@ UniValue NSPV_getinfo(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); +uint256 Parseuint256(const char *hexstr); UniValue nspv_getinfo(const UniValue& params, bool fHelp) { From 90b1bdbedc678f0e798a19bec8d4769928e610f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:22:50 -1100 Subject: [PATCH 078/378] Wif err check --- src/komodo_nSPV.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3c5d79b22..18fa5afdc 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -724,7 +724,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } // nSPV client -extern CAmount AmountFromValue(const UniValue& value); +CAmount AmountFromValue(const UniValue& value); +int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); uint32_t NSPV_lastinfo,NSPV_lastutxos,NSPV_logintime; char NSPV_wifstr[64]; @@ -862,7 +863,14 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_login(char *wifstr) { - UniValue result(UniValue::VOBJ); char coinaddr[64]; + UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[64]; int32_t len; + if ( (len= bitcoin_base58decode(data,wifstr)) != 32 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid wif")); + result.push_back(Pair("len",(int64_t)len)); + return(result); + } memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); NSPV_logintime = (uint32_t)time(NULL); From dd0a99e2d9801dc48c18986dd0467ae84fa1c1d5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:26:01 -1100 Subject: [PATCH 079/378] Decoded wif 38 bytes --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 18fa5afdc..f2a41ff48 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -864,7 +864,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_login(char *wifstr) { UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[64]; int32_t len; - if ( (len= bitcoin_base58decode(data,wifstr)) != 32 ) + if ( (len= bitcoin_base58decode(data,wifstr)) != 38 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid wif")); From 27d8ccf5544a34c5316a73adda4227df15b829e7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:31:03 -1100 Subject: [PATCH 080/378] Compressed and uncompressed support --- src/komodo_nSPV.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f2a41ff48..a53488253 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -863,8 +863,11 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_login(char *wifstr) { - UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[64]; int32_t len; - if ( (len= bitcoin_base58decode(data,wifstr)) != 38 ) + UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[64]; int32_t len,valid = 0; + len = bitcoin_base58decode(data,wifstr); + if ( (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) + valid = 1; + if ( valid == 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid wif")); From 80b50da29be6906db475f7dbb64c014cab7fbc4d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:36:55 -1100 Subject: [PATCH 081/378] +print --- src/komodo_nSPV.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a53488253..642149cef 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -863,9 +863,9 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_login(char *wifstr) { - UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[64]; int32_t len,valid = 0; + UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; len = bitcoin_base58decode(data,wifstr); - if ( (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) + if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) valid = 1; if ( valid == 0 ) { @@ -885,6 +885,10 @@ UniValue NSPV_login(char *wifstr) CKeyID vchAddress = pubkey.GetID(); NSPV_address = EncodeDestination(vchAddress); result.push_back(Pair("address",NSPV_address)); + result.push_back(Pair("pubkey",HexStr(pubkey))); + result.push_back(Pair("wifprefix",(int64_t)data[0])); + result.push_back(Pair("compressed",(int64_t)(data[len-5] == 1))); + memset(data,0,sizeof(data)); return(result); } From 5263d3c74de6675fd1221a49e4301d76e53e8b90 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 21:56:43 -1100 Subject: [PATCH 082/378] +print --- src/komodo_nSPV.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 642149cef..5114dc999 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -756,8 +756,9 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint32_t mask,int32_t ind) if ( pnode != 0 ) { std::vector request; - request.resize(len); + request.resize(len+1); memcpy(&request[0],msg,len); + fprintf(stderr,"pushmessage len.%d\n",len); pnode->PushMessage("getnSPV",request); pnode->prevtimes[ind] = timestamp; return(pnode); @@ -895,12 +896,19 @@ UniValue NSPV_login(char *wifstr) UniValue NSPV_addressutxos(char *coinaddr) { UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t slen,len = 0; + if ( bitcoin_base58decode(msg,coinaddr) != 25 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid address")); + return(result); + } if ( NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) { slen = (int32_t)strlen(coinaddr); msg[len++] = NSPV_UTXOS; msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; + fprintf(stderr,"issue addressutxos.(%s)\n",coinaddr); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,NSPV_UTXOS>>1) != 0 ) usleep(250000); } From 165c26660917e563282b1a6720fd1511bf02442f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:06:43 -1100 Subject: [PATCH 083/378] Test --- src/komodo_nSPV.h | 11 ++++++++--- src/main.cpp | 23 ++++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 5114dc999..2305e445a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -756,9 +756,9 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint32_t mask,int32_t ind) if ( pnode != 0 ) { std::vector request; - request.resize(len+1); + request.resize(len); memcpy(&request[0],msg,len); - fprintf(stderr,"pushmessage len.%d\n",len); + fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); pnode->PushMessage("getnSPV",request); pnode->prevtimes[ind] = timestamp; return(pnode); @@ -902,8 +902,13 @@ UniValue NSPV_addressutxos(char *coinaddr) result.push_back(Pair("error","invalid address")); return(result); } - if ( NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) + if ( NSPV_inforesult.height == 0 || NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) { + if ( NSPV_inforesult.height == 0 ) + { + msg[0] = NSPV_INFO; + NSPV_req(0,msg,1,NODE_NSPV,NSPV_INFO>>1); + } slen = (int32_t)strlen(coinaddr); msg[len++] = NSPV_UTXOS; msg[len++] = slen; diff --git a/src/main.cpp b/src/main.cpp index 4438aa08e..334d1f276 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7052,7 +7052,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { const CChainParams& chainparams = Params(); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); - if ( KOMODO_NSPV != 0 ) + //if ( KOMODO_NSPV != 0 ) + if ( strCommand != "version" && strCommand != "verack" ) fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { @@ -7226,11 +7227,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION)); - if ( KOMODO_NSPV != 0 && (pfrom->nServices & NODE_NSPV) == 0 ) + if ( KOMODO_NSPV != 0 ) { - fprintf(stderr,"invalid nSPV peer.%d\n",pfrom->id); - pfrom->fDisconnect = true; - return false; + if ( (pfrom->nServices & NODE_NSPV) == 0 ) + { + fprintf(stderr,"invalid nSPV peer.%d\n",pfrom->id); + pfrom->fDisconnect = true; + return false; + } + } + else + { + if ( pfrom->nServices != 0 ) + { + fprintf(stderr,"debug mode, disconnect legacy peer.%d\n",pfrom->id); + pfrom->fDisconnect = true; + return false; + } } // Mark this node as currently connected, so we update its timestamp later. if (pfrom->fNetworkNode) { From fa46a6e347dd97aad72022ef2e0ae442de3c6c69 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:12:35 -1100 Subject: [PATCH 084/378] Revert debug disconnect --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 334d1f276..d30213d6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7236,7 +7236,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return false; } } - else + else if ( (0) ) { if ( pfrom->nServices != 0 ) { From 057f1e39cdddc10c00c0a5a7c9d2777344e4a858 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:20:15 -1100 Subject: [PATCH 085/378] +print --- src/komodo_nSPV.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 2305e445a..ef665a45c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -736,7 +736,7 @@ struct NSPV_utxosresp NSPV_utxosresult; struct NSPV_utxo *NSPV_utxos; struct NSPV_spentinfo *NSPV_spends; -CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint32_t mask,int32_t ind) +CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); if ( pnode == 0 ) @@ -751,6 +751,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint32_t mask,int32_t ind) flag = 1; break; } + else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)pnode->nServices,(long long)mask,timestamp,pnode->prevtimes[ind],ind); } } else flag = 1; if ( pnode != 0 ) @@ -913,7 +914,8 @@ UniValue NSPV_addressutxos(char *coinaddr) msg[len++] = NSPV_UTXOS; msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; - fprintf(stderr,"issue addressutxos.(%s)\n",coinaddr); + msg[len] = 0; + fprintf(stderr,"issue addressutxos.(%s) (%s)\n",coinaddr,(char *)&msg[2]); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,NSPV_UTXOS>>1) != 0 ) usleep(250000); } From b4b339af72b121da5e8759e96f3ad3236fa09e48 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:24:21 -1100 Subject: [PATCH 086/378] Fix NSPV_req --- src/komodo_nSPV.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index ef665a45c..bb54d88ae 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -742,16 +742,17 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) if ( pnode == 0 ) { LOCK(cs_vNodes); - BOOST_FOREACH(CNode *pnode,vNodes) + BOOST_FOREACH(CNode *ptr,vNodes) { - if ( pnode->hSocket == INVALID_SOCKET ) + if ( ptr->hSocket == INVALID_SOCKET ) continue; - if ( (pnode->nServices & mask) == mask && timestamp > pnode->prevtimes[ind] ) + if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) { flag = 1; + pnode = ptr; break; } - else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)pnode->nServices,(long long)mask,timestamp,pnode->prevtimes[ind],ind); + else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } } else flag = 1; if ( pnode != 0 ) From 1b4f65c45e8ebf4912bfc28b84f42c6ea4c4930f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:33:11 -1100 Subject: [PATCH 087/378] Use login address if nspv_listunspent doesn't specify --- src/komodo_nSPV.h | 10 ++++++---- src/wallet/rpcdump.cpp | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index bb54d88ae..16e86e8f1 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -133,6 +133,7 @@ int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxosres ptr->utxos = (struct NSPV_utxoresp *)calloc(sizeof(*ptr->utxos),ptr->numutxos); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; inumutxos; i++) len += NSPV_rwutxoresp(rwflag,&serialized[len],&ptr->utxos[i]); + fprintf(stderr,"parsed numutxos.%d\n",ptr->numutxos); } len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->total),&ptr->total); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); @@ -820,9 +821,9 @@ UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) item.push_back(Pair("height",(int64_t)utxos[i].height)); item.push_back(Pair("txid",utxos[i].txid.GetHex())); item.push_back(Pair("vout",(int64_t)utxos[i].vout)); - item.push_back(Pair("value",AmountFromValue(utxos[i].satoshis))); + item.push_back(Pair("value",(doublt)utxos[i].satoshis/COIN)); if ( ASSETCHAINS_SYMBOL[0] == 0 ) - item.push_back(Pair("interest",AmountFromValue(utxos[i].extradata))); + item.push_back(Pair("interest",(doublt)utxos[i].extradata/COIN)); array.push_back(item); } return(array); @@ -835,8 +836,9 @@ UniValue NSPV_utxosresp_json(struct NSPV_utxosresp *ptr) result.push_back(Pair("utxos",NSPV_utxoresp_json(ptr->utxos,ptr->numutxos))); result.push_back(Pair("height",(int64_t)ptr->nodeheight)); result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); - result.push_back(Pair("balance",AmountFromValue(ptr->total))); - result.push_back(Pair("interest",AmountFromValue(ptr->interest))); + result.push_back(Pair("balance",(doublt)ptr->total/COIN)); + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + result.push_back(Pair("interest",(doublt)ptr->interest/COIN)); return(result); } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 263145320..830ecc322 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -977,6 +977,7 @@ UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); uint256 Parseuint256(const char *hexstr); +extern std::string NSPV_address; UniValue nspv_getinfo(const UniValue& params, bool fHelp) { @@ -994,6 +995,8 @@ UniValue nspv_login(const UniValue& params, bool fHelp) UniValue nspv_listunspent(const UniValue& params, bool fHelp) { + if ( params.size() == 0 && NSPV_address.size() != 0 ) + return(NSPV_addressutxos((char *)NSPV_address.c_str())); if ( fHelp || params.size() != 1 ) throw runtime_error("nspv_listunspent address\n"); return(NSPV_addressutxos((char *)params[0].get_str().c_str())); From 259586c017448433ee7676a99694f5313fc46c8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:36:50 -1100 Subject: [PATCH 088/378] Double --- src/komodo_nSPV.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 16e86e8f1..9fd7ace1a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -821,9 +821,9 @@ UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) item.push_back(Pair("height",(int64_t)utxos[i].height)); item.push_back(Pair("txid",utxos[i].txid.GetHex())); item.push_back(Pair("vout",(int64_t)utxos[i].vout)); - item.push_back(Pair("value",(doublt)utxos[i].satoshis/COIN)); + item.push_back(Pair("value",(double)utxos[i].satoshis/COIN)); if ( ASSETCHAINS_SYMBOL[0] == 0 ) - item.push_back(Pair("interest",(doublt)utxos[i].extradata/COIN)); + item.push_back(Pair("interest",(double)utxos[i].extradata/COIN)); array.push_back(item); } return(array); @@ -836,9 +836,9 @@ UniValue NSPV_utxosresp_json(struct NSPV_utxosresp *ptr) result.push_back(Pair("utxos",NSPV_utxoresp_json(ptr->utxos,ptr->numutxos))); result.push_back(Pair("height",(int64_t)ptr->nodeheight)); result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); - result.push_back(Pair("balance",(doublt)ptr->total/COIN)); + result.push_back(Pair("balance",(double)ptr->total/COIN)); if ( ASSETCHAINS_SYMBOL[0] == 0 ) - result.push_back(Pair("interest",(doublt)ptr->interest/COIN)); + result.push_back(Pair("interest",(double)ptr->interest/COIN)); return(result); } From a71f92e8dfbd09706830a396c5d4791f65d3ff14 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:41:06 -1100 Subject: [PATCH 089/378] data[0] == 188 --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9fd7ace1a..95284f71c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -872,7 +872,7 @@ UniValue NSPV_login(char *wifstr) len = bitcoin_base58decode(data,wifstr); if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) valid = 1; - if ( valid == 0 ) + if ( valid == 0 || data[0] != 188 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid wif")); From d76498c52f940fa9000e7da78883b1e10e132460 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:43:05 -1100 Subject: [PATCH 090/378] Force wif prefix --- src/komodo_nSPV.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 95284f71c..18b0ce43e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -871,8 +871,11 @@ UniValue NSPV_login(char *wifstr) UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; len = bitcoin_base58decode(data,wifstr); if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) + { valid = 1; - if ( valid == 0 || data[0] != 188 ) + data[0] = 188; + } + if ( valid == 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid wif")); From aec5f25b762f9bea24b0570fb41fdb654468803e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 22:45:10 -1100 Subject: [PATCH 091/378] Revert --- src/komodo_nSPV.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 18b0ce43e..bbe3fb1f5 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -871,15 +871,13 @@ UniValue NSPV_login(char *wifstr) UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; len = bitcoin_base58decode(data,wifstr); if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) - { valid = 1; - data[0] = 188; - } - if ( valid == 0 ) + if ( valid == 0 || data[0] != 188 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid wif")); result.push_back(Pair("len",(int64_t)len)); + result.push_back(Pair("prefix",(int64_t)data[0])); return(result); } memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); From 146ccc202fb0f962a45fcdeca40176bf50bb9d1a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Jul 2019 23:12:34 -1100 Subject: [PATCH 092/378] +print --- src/komodo_nSPV.h | 7 +++++-- src/wallet/rpcdump.cpp | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index bbe3fb1f5..3fe3f978e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -583,7 +583,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; if ( request[0] == NSPV_INFO ) // info { - if ( len == 1 && timestamp > pfrom->prevtimes[ind] + ASSETCHAINS_BLOCKTIME/2 ) + fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; memset(&I,0,sizeof(I)); @@ -802,7 +803,7 @@ UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) return(result); } -UniValue NSPV_getinfo() +UniValue NSPV_getinfo_json() { UniValue result(UniValue::VOBJ); result.push_back(Pair("result","success")); @@ -940,6 +941,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: + NSPV_inforesp_purge(&NSPV_inforesult); NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status break; @@ -958,6 +960,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_SPENTINFORESP: + fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 830ecc322..d6863e88b 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -972,7 +972,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) return EncodeViewingKey(vk); } -UniValue NSPV_getinfo(); +UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); @@ -983,7 +983,7 @@ UniValue nspv_getinfo(const UniValue& params, bool fHelp) { if ( fHelp || params.size() != 0 ) throw runtime_error("nspv_getinfo\n"); - return(NSPV_getinfo()); + return(NSPV_getinfo_json()); } UniValue nspv_login(const UniValue& params, bool fHelp) From 591185a0b33bf86ba6f41435afa213490b927616 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 00:29:18 -1100 Subject: [PATCH 093/378] Rest of client side low level rpc, incl messages --- src/komodo_nSPV.h | 161 +++++++++++++++++++++++++++++++++-------- src/rpc/server.cpp | 11 ++- src/rpc/server.h | 3 + src/wallet/rpcdump.cpp | 31 ++++++++ 4 files changed, 172 insertions(+), 34 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3fe3f978e..52d4df060 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -29,8 +29,8 @@ #define NSPV_UTXOSRESP 0x03 #define NSPV_NTZS 0x04 #define NSPV_NTZSRESP 0x05 -#define NSPV_NTZPROOF 0x06 -#define NSPV_NTZPROOFRESP 0x07 +#define NSPV_NTZSPROOF 0x06 +#define NSPV_NTZSPROOFRESP 0x07 #define NSPV_TXPROOF 0x08 #define NSPV_TXPROOFRESP 0x09 #define NSPV_SPENTINFO 0x0a @@ -584,7 +584,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( request[0] == NSPV_INFO ) // info { fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); - if ( timestamp > pfrom->prevtimes[ind] ) + if ( timestamp > pfrom->prevtimes[ind] + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_inforesp I; memset(&I,0,sizeof(I)); @@ -647,7 +647,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } } } - else if ( request[0] == NSPV_NTZPROOF ) + else if ( request[0] == NSPV_NTZSPROOF ) { if ( timestamp > pfrom->prevtimes[ind] ) { @@ -662,7 +662,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) { response.resize(1 + slen); - response[0] = NSPV_NTZPROOFRESP; + response[0] = NSPV_NTZSPROOFRESP; if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); @@ -729,14 +729,17 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); -uint32_t NSPV_lastinfo,NSPV_lastutxos,NSPV_logintime; +uint32_t NSPV_lastinfo,NSPV_logintime; char NSPV_wifstr[64]; std::string NSPV_address; -int32_t NSPV_numutxos,NSPV_numspends; struct NSPV_inforesp NSPV_inforesult; struct NSPV_utxosresp NSPV_utxosresult; +struct NSPV_spentinfo NSPV_spentresult; +struct NSPV_ntzsresp NSPV_ntzsresult; +struct NSPV_ntzsproofresp NSPV_ntzsproofresult; +struct NSPV_txproof NSPV_txproofresult; + struct NSPV_utxo *NSPV_utxos; -struct NSPV_spentinfo *NSPV_spends; CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { @@ -778,6 +781,16 @@ void komodo_NSPV_spentinfoclear() NSPV_numspends = 0; } +UniValue NSPV_txproof_json(struct NSPV_txproof *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("txlen",(int64_t)ptr->txlen)); + result.push_back(Pair("txprooflen",(int64_t)ptr->txprooflen)); + return(result); +} + UniValue NSPV_spentinfo_json(struct NSPV_spentinfo *ptr) { UniValue result(UniValue::VOBJ); @@ -843,28 +856,43 @@ UniValue NSPV_utxosresp_json(struct NSPV_utxosresp *ptr) return(result); } -UniValue NSPV_spentinfo(uint256 txid,int32_t vout) +UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr) { - uint8_t msg[64]; struct NSPV_spentinfo I; int32_t i,len = 0; - for (i=0; iprevntz))); + result.push_back(Pair("next",NSPV_ntz_json(&ptr->nextntz))); + return(result); +} + +UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs) +{ + UniValue array(UniValue::VARR); int32_t i; + for (i=0; i>1) != 0 ) - usleep(250000); - for (i=0; icommon.prevht)); + result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); + result.push_back(Pair("prevtxid",ptr->prevtxid.GetHex())); + result.push_back(Pair("prevtxidht",(int64_t)ptr->prevtxidht)); + result.push_back(Pair("prevlen",(int64_t)ptr->prevlen)); + result.push_back(Pair("nexttxid",ptr->nexttxid.GetHex())); + result.push_back(Pair("nexttxidht",(int64_t)ptr->nexttxidht)); + result.push_back(Pair("nextlen",(int64_t)ptr->prevlen)); + result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); + result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs))); + return(result); } UniValue NSPV_login(char *wifstr) @@ -913,7 +941,7 @@ UniValue NSPV_addressutxos(char *coinaddr) if ( NSPV_inforesult.height == 0 ) { msg[0] = NSPV_INFO; - NSPV_req(0,msg,1,NODE_NSPV,NSPV_INFO>>1); + NSPV_req(0,msg,1,NODE_NSPV,msg[0]>>1); } slen = (int32_t)strlen(coinaddr); msg[len++] = NSPV_UTXOS; @@ -921,7 +949,7 @@ UniValue NSPV_addressutxos(char *coinaddr) memcpy(&msg[len],coinaddr,slen), len += slen; msg[len] = 0; fprintf(stderr,"issue addressutxos.(%s) (%s)\n",coinaddr,(char *)&msg[2]); - if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,NSPV_UTXOS>>1) != 0 ) + if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) usleep(250000); } if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) @@ -933,6 +961,71 @@ UniValue NSPV_addressutxos(char *coinaddr) return(result); } +UniValue NSPV_notarizations(int32_t height) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsresp N; + if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); + msg[len++] = NSPV_NTZS; + len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + usleep(250000); + if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); + memset(&N,0,sizeof(N)); + return(NSPV_ntzs_json(&NSPV_ntzsresult)); +} + +UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsproofresp H; + result.push_back(Pair("prevht",(int64_t)ptr->common.prevht)); + result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); + if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + msg[len++] = NSPV_NTZSPROOF; + len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); + len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + usleep(250000); + if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + memset(&H,0,sizeof(H)); + return(NSPV_ntzsproof_json(&NSPV_ntzsresult)); +} + +UniValue NSPV_txproof(uint256 txid,int32_t height) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_txproof P; + if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + return(NSPV_txproof_json(&NSPV_txproofresult)); + msg[len++] = NSPV_TXPROOF; + len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + usleep(250000); + if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + return(NSPV_txproof_json(&NSPV_txproofresult)); + memset(&P,0,sizeof(P)); + return(NSPV_txproof_json(&I)); +} + +UniValue NSPV_spentinfo(uint256 txid,int32_t vout) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_spentinfo I; + if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) + return(NSPV_spentinfo_json(&NSPV_spentresult)); + msg[len++] = NSPV_SPENTINFO; + len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); + len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); + if ( NSPV_req(0,msg,len,NODE_SPENTINDEX,msg[0]>>1) != 0 ) + usleep(250000); + if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) + return(NSPV_spentinfo_json(&NSPV_spentresult)); + memset(&I,0,sizeof(I)); + return(NSPV_spentinfo_json(&I)); +} + void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); @@ -951,16 +1044,24 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list break; case NSPV_NTZSRESP: + NSPV_ntzsresp_purge(&NSPV_ntzsresult); + NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; - case NSPV_NTZPROOFRESP: + case NSPV_NTZSPROOFRESP: + NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); + NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); fprintf(stderr,"got ntzproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_TXPROOFRESP: + NSPV_txproof_purge(&NSPV_txproofresult); + NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; case NSPV_SPENTINFORESP: + NSPV_spentinfo_purge(&NSPV_spentresult); + NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 937ae0632..810848f74 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -417,10 +417,13 @@ static const CRPCCommand vRPCCommands[] = { "FSM", "FSMinfo", &FSMinfo, true }, // fsm - { "nSPV", "nspv_getinfo", &nspv_getinfo, true }, - { "nSPV", "nspv_login", &nspv_login, true }, - { "nSPV", "nspv_listunspent", &nspv_listunspent, true }, - { "nSPV", "nspv_spentinfo", &nspv_spentinfo, true }, + { "nSPV", "nspv_getinfo", &nspv_getinfo, true }, + { "nSPV", "nspv_login", &nspv_login, true }, + { "nSPV", "nspv_listunspent", &nspv_listunspent, true }, + { "nSPV", "nspv_spentinfo", &nspv_spentinfo, true }, + { "nSPV", "nspv_notarizations", &nspv_notarizations, true }, + { "nSPV", "nspv_hdrsproof", &nspv_hdrsproof, true }, + { "nSPV", "nspv_txproof", &nspv_txproof, true }, // rewards { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index bc895ee53..64c78ea47 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -459,6 +459,9 @@ extern UniValue nspv_getinfo(const UniValue& params, bool fHelp); extern UniValue nspv_login(const UniValue& params, bool fHelp); extern UniValue nspv_listunspent(const UniValue& params, bool fHelp); extern UniValue nspv_spentinfo(const UniValue& params, bool fHelp); +extern UniValue nspv_notarizations(const UniValue& params, bool fHelp); +extern UniValue nspv_hdrsproof(const UniValue& params, bool fHelp); +extern UniValue nspv_txproof(const UniValue& params, bool fHelp); extern UniValue getblocksubsidy(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index d6863e88b..ef779491a 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -976,6 +976,9 @@ UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); +UniValue NSPV_notarizations(int32_t height); +UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight); +UniValue NSPV_txproof(uint256 txid,int32_t vout); uint256 Parseuint256(const char *hexstr); extern std::string NSPV_address; @@ -1012,3 +1015,31 @@ UniValue nspv_spentinfo(const UniValue& params, bool fHelp) return(NSPV_spentinfo(txid,vout)); } +UniValue nspv_notarizations(const UniValue& params, bool fHelp) +{ + int32_t height; + if ( fHelp || params.size() != 1 ) + throw runtime_error("nspv_notarizations height\n"); + height = atoi((char *)params[0].get_str().c_str()); + return(NSPV_notarizations(height)); +} + +UniValue nspv_hdrsproof(const UniValue& params, bool fHelp) +{ + int32_t prevheight,nextheight; + if ( fHelp || params.size() != 2 ) + throw runtime_error("nspv_proof prevheight nextheight\n"); + prevheight = atoi((char *)params[0].get_str().c_str()); + nextheight = atoi((char *)params[0].get_str().c_str()); + return(NSPV_proof(prevheight,nextheight)); +} + +UniValue nspv_txproof(const UniValue& params, bool fHelp) +{ + uint256 txid; int32_t height; + if ( fHelp || params.size() != 2 ) + throw runtime_error("nspv_txproof txid height\n"); + txid = Parseuint256((char *)params[0].get_str().c_str()); + height = atoi((char *)params[1].get_str().c_str()); + return(NSPV_txproof(txid,vout)); +} From aebbaba600543bfaed75cc4be3229f616b33f8c7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 00:32:33 -1100 Subject: [PATCH 094/378] Syntax --- src/komodo_nSPV.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 52d4df060..23f69b4a1 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -773,14 +773,6 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) return(0); } -void komodo_NSPV_spentinfoclear() -{ - if ( NSPV_spends != 0 ) - free(NSPV_spends); - NSPV_spends = 0; - NSPV_numspends = 0; -} - UniValue NSPV_txproof_json(struct NSPV_txproof *ptr) { UniValue result(UniValue::VOBJ); @@ -979,8 +971,6 @@ UniValue NSPV_notarizations(int32_t height) UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsproofresp H; - result.push_back(Pair("prevht",(int64_t)ptr->common.prevht)); - result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); msg[len++] = NSPV_NTZSPROOF; From 2d47ff5f5c72509a4d07fe6bf3f6fcaf6ed904c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 00:37:22 -1100 Subject: [PATCH 095/378] Syntax --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 23f69b4a1..c07a16003 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -965,7 +965,7 @@ UniValue NSPV_notarizations(int32_t height) if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) return(NSPV_ntzs_json(&NSPV_ntzsresult)); memset(&N,0,sizeof(N)); - return(NSPV_ntzs_json(&NSPV_ntzsresult)); + return(NSPV_ntzs_json(&N)); } UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) @@ -981,7 +981,7 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); memset(&H,0,sizeof(H)); - return(NSPV_ntzsproof_json(&NSPV_ntzsresult)); + return(NSPV_ntzsproof_json(&H)); } UniValue NSPV_txproof(uint256 txid,int32_t height) @@ -997,7 +997,7 @@ UniValue NSPV_txproof(uint256 txid,int32_t height) if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) return(NSPV_txproof_json(&NSPV_txproofresult)); memset(&P,0,sizeof(P)); - return(NSPV_txproof_json(&I)); + return(NSPV_txproof_json(&P)); } UniValue NSPV_spentinfo(uint256 txid,int32_t vout) From c7623db751390f32bf54ea8e17cc742e44c90cf4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 00:42:49 -1100 Subject: [PATCH 096/378] Syntax --- src/komodo_nSPV.h | 58 +++++++++++++++++++++++++++++------------- src/wallet/rpcdump.cpp | 4 +-- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c07a16003..8c4432717 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -942,11 +942,15 @@ UniValue NSPV_addressutxos(char *coinaddr) msg[len] = 0; fprintf(stderr,"issue addressutxos.(%s) (%s)\n",coinaddr,(char *)&msg[2]); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) - usleep(250000); - } - if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) - { - return(NSPV_utxosresp_json(&NSPV_utxosresult)); + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_inforesult.height != 0 + && NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) + return(NSPV_utxosresp_json(&NSPV_utxosresult)); + } + } } result.push_back(Pair("result","error")); result.push_back(Pair("error","no utxos result")); @@ -961,9 +965,14 @@ UniValue NSPV_notarizations(int32_t height) msg[len++] = NSPV_NTZS; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - usleep(250000); - if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) - return(NSPV_ntzs_json(&NSPV_ntzsresult)); + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); + } + } memset(&N,0,sizeof(N)); return(NSPV_ntzs_json(&N)); } @@ -977,9 +986,14 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - usleep(250000); - if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) - return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + } + } memset(&H,0,sizeof(H)); return(NSPV_ntzsproof_json(&H)); } @@ -993,9 +1007,14 @@ UniValue NSPV_txproof(uint256 txid,int32_t height) len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - usleep(250000); - if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) - return(NSPV_txproof_json(&NSPV_txproofresult)); + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + return(NSPV_txproof_json(&NSPV_txproofresult)); + } + } memset(&P,0,sizeof(P)); return(NSPV_txproof_json(&P)); } @@ -1009,9 +1028,14 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); if ( NSPV_req(0,msg,len,NODE_SPENTINDEX,msg[0]>>1) != 0 ) - usleep(250000); - if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) - return(NSPV_spentinfo_json(&NSPV_spentresult)); + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) + return(NSPV_spentinfo_json(&NSPV_spentresult)); + } + } memset(&I,0,sizeof(I)); return(NSPV_spentinfo_json(&I)); } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index ef779491a..3b1fadbf7 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1031,7 +1031,7 @@ UniValue nspv_hdrsproof(const UniValue& params, bool fHelp) throw runtime_error("nspv_proof prevheight nextheight\n"); prevheight = atoi((char *)params[0].get_str().c_str()); nextheight = atoi((char *)params[0].get_str().c_str()); - return(NSPV_proof(prevheight,nextheight)); + return(NSPV_hdrsproof(prevheight,nextheight)); } UniValue nspv_txproof(const UniValue& params, bool fHelp) @@ -1041,5 +1041,5 @@ UniValue nspv_txproof(const UniValue& params, bool fHelp) throw runtime_error("nspv_txproof txid height\n"); txid = Parseuint256((char *)params[0].get_str().c_str()); height = atoi((char *)params[1].get_str().c_str()); - return(NSPV_txproof(txid,vout)); + return(NSPV_txproof(txid,height)); } From e3b27c583ddf2bf7774ee7e3279c858288566713 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 00:47:14 -1100 Subject: [PATCH 097/378] Syntax --- src/komodo_nSPV.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 8c4432717..b9ee8897d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -921,7 +921,7 @@ UniValue NSPV_login(char *wifstr) UniValue NSPV_addressutxos(char *coinaddr) { - UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t slen,len = 0; + UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,slen,len = 0; if ( bitcoin_base58decode(msg,coinaddr) != 25 ) { result.push_back(Pair("result","error")); @@ -940,7 +940,6 @@ UniValue NSPV_addressutxos(char *coinaddr) msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; msg[len] = 0; - fprintf(stderr,"issue addressutxos.(%s) (%s)\n",coinaddr,(char *)&msg[2]); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) From 6bae7050c33b98844a2aabc715ec1aaa78cd4b8d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 01:13:17 -1100 Subject: [PATCH 098/378] Clear freed ptr --- src/komodo_nSPV.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b9ee8897d..701f144b7 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -535,15 +535,24 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) { int32_t i; uint256 hashBlock; + if ( prevht > nextht || (nextht-prevht) > 1440 ) + { + fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); + return(-1); + } ptr->prevtxidht = prevht; ptr->nexttxidht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); + fprintf(stderr,"allocate numhdrs.%d\n",ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { + fprintf(stderr,"%d ht.%d\n",i,prevht+i); if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { + fprintf(stderr,"error setting hdr.%d\n",prevht+i); free(ptr->common.hdrs); + ptr->common.hdrs = 0; return(-1); } } From 7958b7ecfc7263692d93f21c1f3fb11640900aa9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 01:32:35 -1100 Subject: [PATCH 099/378] Test --- src/komodo_nSPV.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 701f144b7..522adbcfa 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -76,10 +76,14 @@ int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsize len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); if ( (vsize= *vecsizep) != 0 ) { + fprintf(stderr,"vsize.%d ptrp.%p alloc %ld\n",*ptrp,sizeof(struct NSPV_equihdr)*vsize); if ( *ptrp == 0 ) - *ptrp = (struct NSPV_equihdr *)calloc(sizeof(**ptrp),vsize); // relies on uint16_t being "small" to prevent mem exhaustion + *ptrp = (struct NSPV_equihdr *)calloc(sizeof(struct NSPV_equihdr),vsize); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; ipad32),&ptr->pad32); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevtxidht),&ptr->prevtxidht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nexttxidht),&ptr->nexttxidht); + fprintf(stderr,"rwvecgtors lens %d %d\n",ptr->prevlen,ptr->nextlen); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevlen,&ptr->prevntz); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nextlen,&ptr->nextntz); + fprintf(stderr,"rwntzsproof len %d\n",len); return(len); } @@ -544,7 +550,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->nexttxidht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); - fprintf(stderr,"allocate numhdrs.%d\n",ptr->common.numhdrs); + fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { fprintf(stderr,"%d ht.%d\n",i,prevht+i); @@ -670,13 +676,16 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req memset(&P,0,sizeof(P)); if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) { + fprintf(stderr,"ntzsproof slen.%d\n",slen); response.resize(1 + slen); response[0] = NSPV_NTZSPROOFRESP; if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { + fprintf(stderr,"respond with %d\n",slen); pfrom->PushMessage("nSPV",response); pfrom->prevtimes[ind] = timestamp; } + fprintf(stderr,"purge.%d\n",P.common.numhdrs); NSPV_ntzsproofresp_purge(&P); } } From eadeedff7c59f07b9651b9edaa9bcf59d3eef10e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 01:41:15 -1100 Subject: [PATCH 100/378] Test --- src/komodo_nSPV.h | 4 +++- src/wallet/rpcdump.cpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 522adbcfa..005d9f330 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -76,7 +76,7 @@ int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsize len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); if ( (vsize= *vecsizep) != 0 ) { - fprintf(stderr,"vsize.%d ptrp.%p alloc %ld\n",*ptrp,sizeof(struct NSPV_equihdr)*vsize); + fprintf(stderr,"vsize.%d ptrp.%p alloc %ld\n",vsize,*ptrp,sizeof(struct NSPV_equihdr)*vsize); if ( *ptrp == 0 ) *ptrp = (struct NSPV_equihdr *)calloc(sizeof(struct NSPV_equihdr),vsize); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; inBits = pindex->nBits; hdr->nNonce = pindex->nNonce; memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); + printf("set equihdr size.%d\n",(int32_t)sizeof(*hdr)); return(sizeof(*hdr)); } return(-1); @@ -566,6 +567,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nextlen,ptr->nexttxid); + fprintf(stderr,"prevlen.%d nextlen.%d size %ld -> %ld\n",ptr->prevlen,ptr->nextlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); return(sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 3b1fadbf7..256a100f2 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1030,7 +1030,7 @@ UniValue nspv_hdrsproof(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 2 ) throw runtime_error("nspv_proof prevheight nextheight\n"); prevheight = atoi((char *)params[0].get_str().c_str()); - nextheight = atoi((char *)params[0].get_str().c_str()); + nextheight = atoi((char *)params[1].get_str().c_str()); return(NSPV_hdrsproof(prevheight,nextheight)); } From 9474454175be35067be9bc76b73ab2085301f599 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 01:43:25 -1100 Subject: [PATCH 101/378] Set numbers --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 005d9f330..66430b01e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -681,6 +681,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req fprintf(stderr,"ntzsproof slen.%d\n",slen); response.resize(1 + slen); response[0] = NSPV_NTZSPROOFRESP; + P.common.numhdrs = (nextht - prevht + 1); if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { fprintf(stderr,"respond with %d\n",slen); From 7b73eac2c85d0ad003511a579d883f5ee0b91884 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:00:49 -1100 Subject: [PATCH 102/378] Test --- src/komodo_nSPV.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 66430b01e..4604b7bfe 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -568,7 +568,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nextlen,ptr->nexttxid); fprintf(stderr,"prevlen.%d nextlen.%d size %ld -> %ld\n",ptr->prevlen,ptr->nextlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); - return(sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); } int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) @@ -671,6 +671,12 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req struct NSPV_ntzsproofresp P; int32_t prevht,nextht; if ( len == 1+sizeof(prevht)+sizeof(nextht) ) { + { + int32_t z; + for (z=0; z<9; z++) + fprintf(stderr,"%02x",request[z]); + fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); + } iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); if ( prevht != 0 && nextht != 0 && nextht >= prevht ) @@ -1005,6 +1011,13 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) msg[len++] = NSPV_NTZSPROOF; len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); + { + int32_t z; + for (z=0; z<9; z++) + fprintf(stderr,"%02x",msg[z]); + fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); + } + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) From 729de8bfdcd831c46eeb56e62f48179a24a47e03 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:02:03 -1100 Subject: [PATCH 103/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4604b7bfe..4272ee2f0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1015,7 +1015,7 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) int32_t z; for (z=0; z<9; z++) fprintf(stderr,"%02x",msg[z]); - fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); + fprintf(stderr," -> prevht.%d nextht.%d\n",prevheight,nextheight); } if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) From 88eb27ff47bae238b51e9b6bc6ed108e7947f5fe Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:08:34 -1100 Subject: [PATCH 104/378] Test --- src/komodo_nSPV.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4272ee2f0..dd7b77421 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -671,14 +671,14 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req struct NSPV_ntzsproofresp P; int32_t prevht,nextht; if ( len == 1+sizeof(prevht)+sizeof(nextht) ) { + iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); + iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); { int32_t z; for (z=0; z<9; z++) fprintf(stderr,"%02x",request[z]); fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); } - iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); - iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); if ( prevht != 0 && nextht != 0 && nextht >= prevht ) { memset(&P,0,sizeof(P)); @@ -1012,10 +1012,12 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); { - int32_t z; + int32_t z,prevht,nextht; for (z=0; z<9; z++) fprintf(stderr,"%02x",msg[z]); - fprintf(stderr," -> prevht.%d nextht.%d\n",prevheight,nextheight); + iguana_rwnum(0,&msg[1],sizeof(prevht),&prevht); + iguana_rwnum(0,&msg[5],sizeof(nextht),&nextht); + fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); } if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) From a514dd239fc5de77b2ab08dd71152627d9a9516b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:17:10 -1100 Subject: [PATCH 105/378] Test --- src/komodo_nSPV.h | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index dd7b77421..4cb0d71b0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -673,28 +673,19 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); - { - int32_t z; - for (z=0; z<9; z++) - fprintf(stderr,"%02x",request[z]); - fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); - } if ( prevht != 0 && nextht != 0 && nextht >= prevht ) { memset(&P,0,sizeof(P)); if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) { - fprintf(stderr,"ntzsproof slen.%d\n",slen); response.resize(1 + slen); response[0] = NSPV_NTZSPROOFRESP; P.common.numhdrs = (nextht - prevht + 1); if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { - fprintf(stderr,"respond with %d\n",slen); pfrom->PushMessage("nSPV",response); pfrom->prevtimes[ind] = timestamp; } - fprintf(stderr,"purge.%d\n",P.common.numhdrs); NSPV_ntzsproofresp_purge(&P); } } @@ -1011,22 +1002,16 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) msg[len++] = NSPV_NTZSPROOF; len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); - { - int32_t z,prevht,nextht; - for (z=0; z<9; z++) - fprintf(stderr,"%02x",msg[z]); - iguana_rwnum(0,&msg[1],sizeof(prevht),&prevht); - iguana_rwnum(0,&msg[5],sizeof(nextht),&nextht); - fprintf(stderr," -> prevht.%d nextht.%d\n",prevht,nextht); - } - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) { usleep(100000); if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) + { + fprintf(stderr,"got ntzsproof\n"); return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + } } } memset(&H,0,sizeof(H)); @@ -1100,7 +1085,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_NTZSPROOFRESP: NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); - fprintf(stderr,"got ntzproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); // update utxos[i] break; case NSPV_TXPROOFRESP: NSPV_txproof_purge(&NSPV_txproofresult); From 8716c1965253c2796256a765027d3c654b210bef Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:22:16 -1100 Subject: [PATCH 106/378] -print --- src/komodo_nSPV.h | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4cb0d71b0..3d1f00f97 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -76,14 +76,11 @@ int32_t iguana_rwequihdrvec(int32_t rwflag,uint8_t *serialized,uint16_t *vecsize len += iguana_rwnum(rwflag,&serialized[len],sizeof(*vecsizep),vecsizep); if ( (vsize= *vecsizep) != 0 ) { - fprintf(stderr,"vsize.%d ptrp.%p alloc %ld\n",vsize,*ptrp,sizeof(struct NSPV_equihdr)*vsize); + //fprintf(stderr,"vsize.%d ptrp.%p alloc %ld\n",vsize,*ptrp,sizeof(struct NSPV_equihdr)*vsize); if ( *ptrp == 0 ) *ptrp = (struct NSPV_equihdr *)calloc(sizeof(struct NSPV_equihdr),vsize); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; ipad32),&ptr->pad32); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevtxidht),&ptr->prevtxidht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nexttxidht),&ptr->nexttxidht); - fprintf(stderr,"rwvecgtors lens %d %d\n",ptr->prevlen,ptr->nextlen); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevlen,&ptr->prevntz); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nextlen,&ptr->nextntz); - fprintf(stderr,"rwntzsproof len %d\n",len); return(len); } @@ -392,7 +387,6 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check { int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,maxlen,txheight,n = 0,len = 0; std::vector > unspentOutputs; - fprintf(stderr,"getaddressutxos for %s\n",coinaddr); SetCCunspents(unspentOutputs,coinaddr,false); maxlen = MAX_BLOCK_SIZE(tipheight) - 512; maxlen /= sizeof(*ptr->utxos); @@ -533,7 +527,6 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) hdr->nBits = pindex->nBits; hdr->nNonce = pindex->nNonce; memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); - printf("set equihdr size.%d\n",(int32_t)sizeof(*hdr)); return(sizeof(*hdr)); } return(-1); @@ -547,14 +540,13 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); return(-1); } - ptr->prevtxidht = prevht; - ptr->nexttxidht = nextht; + ptr->common.prevht = prevht; + ptr->common.nextht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { - fprintf(stderr,"%d ht.%d\n",i,prevht+i); if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { fprintf(stderr,"error setting hdr.%d\n",prevht+i); @@ -567,7 +559,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nextlen,ptr->nexttxid); - fprintf(stderr,"prevlen.%d nextlen.%d size %ld -> %ld\n",ptr->prevlen,ptr->nextlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + //fprintf(stderr,"prevlen.%d nextlen.%d size %ld -> %ld\n",ptr->prevlen,ptr->nextlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); } @@ -600,7 +592,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; if ( request[0] == NSPV_INFO ) // info { - fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); + //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); if ( timestamp > pfrom->prevtimes[ind] + ASSETCHAINS_BLOCKTIME/2 ) { struct NSPV_inforesp I; @@ -628,7 +620,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req memset(&U,0,sizeof(U)); if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) { - printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); + //printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) @@ -774,8 +766,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) flag = 1; pnode = ptr; break; - } - else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + } // else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } } else flag = 1; if ( pnode != 0 ) @@ -783,7 +774,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) std::vector request; request.resize(len); memcpy(&request[0],msg,len); - fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); + //fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); pnode->PushMessage("getnSPV",request); pnode->prevtimes[ind] = timestamp; return(pnode); From 68f61aea99eeb8f278f2d3ca400ae60aba892d8c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:22:46 -1100 Subject: [PATCH 107/378] -print --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index d30213d6b..f73a3cdd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7231,7 +7231,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { if ( (pfrom->nServices & NODE_NSPV) == 0 ) { - fprintf(stderr,"invalid nSPV peer.%d\n",pfrom->id); + //fprintf(stderr,"invalid nSPV peer.%d\n",pfrom->id); pfrom->fDisconnect = true; return false; } From 14815c3a5ceafb20d32dcfc97892fa3ff65b2bfb Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:26:25 -1100 Subject: [PATCH 108/378] array.push_back(item); --- src/komodo_nSPV.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3d1f00f97..c96ef235d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -544,7 +544,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->common.nextht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); - fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); + //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) @@ -875,6 +875,7 @@ UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs) item.push_back(Pair("hashPrevBlock",hdrs[i].hashPrevBlock.GetHex())); item.push_back(Pair("hashMerkleRoot",hdrs[i].hashMerkleRoot.GetHex())); item.push_back(Pair("nTime",(int64_t)hdrs[i].nTime)); + array.push_back(item); } return(array); } @@ -999,10 +1000,7 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) { usleep(100000); if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) - { - fprintf(stderr,"got ntzsproof\n"); return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); - } } } memset(&H,0,sizeof(H)); From c617e6e9597e3d9795dd52c6f2c03a6d2fd24e30 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:41:54 -1100 Subject: [PATCH 109/378] Test --- src/komodo_nSPV.h | 22 ++++++++++++++++++---- src/main.cpp | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c96ef235d..56eb951ab 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -227,6 +227,7 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); + fprintf(stderr,"rwtxproof len.%d\n",len); return(len); } @@ -472,8 +473,11 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) return(0); string strHex = EncodeHexTx(tx); *txlenp = (int32_t)strHex.size() >> 1; - rawtx = (uint8_t *)calloc(1,*txlenp); - decode_hex(rawtx,*txlenp,(char *)strHex.c_str()); + if ( *txlenp > 0 ) + { + rawtx = (uint8_t *)calloc(1,*txlenp); + decode_hex(rawtx,*txlenp,(char *)strHex.c_str()); + } } return(rawtx); } @@ -504,8 +508,12 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) ssMB << mb; std::vector proof(ssMB.begin(), ssMB.end()); ptr->txprooflen = (int32_t)proof.size(); - ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); - memcpy(ptr->txproof,&proof[0],ptr->txprooflen); + if ( ptr->txprooflen > 0 ) + { + ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); + memcpy(ptr->txproof,&proof[0],ptr->txprooflen); + } + fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } } @@ -693,11 +701,13 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); + fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); memset(&P,0,sizeof(P)); if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_TXPROOFRESP; + fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); @@ -1015,13 +1025,17 @@ UniValue NSPV_txproof(uint256 txid,int32_t height) msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); + fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) { usleep(100000); if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + { + fprintf(stderr,"got txproof\n"); return(NSPV_txproof_json(&NSPV_txproofresult)); + } } } memset(&P,0,sizeof(P)); diff --git a/src/main.cpp b/src/main.cpp index f73a3cdd1..27646ec0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7053,8 +7053,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, const CChainParams& chainparams = Params(); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); //if ( KOMODO_NSPV != 0 ) - if ( strCommand != "version" && strCommand != "verack" ) - fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); + //if ( strCommand != "version" && strCommand != "verack" ) + // fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n"); From 1725ce18d5acca7e99154c62747751716c83b622 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:47:13 -1100 Subject: [PATCH 110/378] +print --- src/komodo_nSPV.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 56eb951ab..eba075623 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -227,7 +227,6 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); - fprintf(stderr,"rwtxproof len.%d\n",len); return(len); } @@ -391,6 +390,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check SetCCunspents(unspentOutputs,coinaddr,false); maxlen = MAX_BLOCK_SIZE(tipheight) - 512; maxlen /= sizeof(*ptr->utxos); + fprintf(stderr,"getaddressutxos %s\n",coinaddr); if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) { tipheight = chainActive.LastTip()->GetHeight(); @@ -513,7 +513,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); memcpy(ptr->txproof,&proof[0],ptr->txprooflen); } - fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); + //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } } @@ -628,7 +628,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req memset(&U,0,sizeof(U)); if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) { - //printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); + printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) @@ -701,13 +701,12 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); - fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); + //fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); memset(&P,0,sizeof(P)); if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_TXPROOFRESP; - fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); From a283a80dc118076c0e25f30c64df2a0ca1b404d4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 02:51:36 -1100 Subject: [PATCH 111/378] Speed up listunspent --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index eba075623..c14de849b 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -601,7 +601,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( request[0] == NSPV_INFO ) // info { //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); - if ( timestamp > pfrom->prevtimes[ind] + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; memset(&I,0,sizeof(I)); @@ -1150,7 +1150,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages } } }*/ - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + ASSETCHAINS_BLOCKTIME/2 ) + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) { if ( (rand() % 100) < 10 ) // randomize which peer to query { From 72131969548e13252d435a829b230a8b8c3184cc Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 03:00:03 -1100 Subject: [PATCH 112/378] +pront --- src/komodo_nSPV.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c14de849b..74d97506e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -134,7 +134,6 @@ int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxosres ptr->utxos = (struct NSPV_utxoresp *)calloc(sizeof(*ptr->utxos),ptr->numutxos); // relies on uint16_t being "small" to prevent mem exhaustion for (i=0; inumutxos; i++) len += NSPV_rwutxoresp(rwflag,&serialized[len],&ptr->utxos[i]); - fprintf(stderr,"parsed numutxos.%d\n",ptr->numutxos); } len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->total),&ptr->total); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); @@ -390,7 +389,6 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check SetCCunspents(unspentOutputs,coinaddr,false); maxlen = MAX_BLOCK_SIZE(tipheight) - 512; maxlen /= sizeof(*ptr->utxos); - fprintf(stderr,"getaddressutxos %s\n",coinaddr); if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) { tipheight = chainActive.LastTip()->GetHeight(); @@ -600,7 +598,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; if ( request[0] == NSPV_INFO ) // info { - //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); + fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; @@ -609,9 +607,11 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_INFORESP; - NSPV_rwinforesp(1,&response[1],&I); - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; + if ( NSPV_rwinforesp(1,&response[1],&I) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } NSPV_inforesp_purge(&I); } } @@ -628,7 +628,6 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req memset(&U,0,sizeof(U)); if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) { - printf("getaddressutxos.(%s) slen.%d\n",coinaddr,slen); response.resize(1 + slen); response[0] = NSPV_UTXOSRESP; if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) @@ -1152,11 +1151,9 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages }*/ if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) { - if ( (rand() % 100) < 10 ) // randomize which peer to query - { - msg[len++] = NSPV_INFO; - NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); - } + len = 0; + msg[len++] = NSPV_INFO; + NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } From f06ac1277fadaa9cb0391879c98ea1e4d6e22a74 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 03:08:28 -1100 Subject: [PATCH 113/378] Test --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 74d97506e..69cca2712 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -618,6 +618,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } else if ( request[0] == NSPV_UTXOS ) { + fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len); if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_utxosresp U; char coinaddr[64]; From a53d5c6d7ec183a0363c8ec0bf1907105f764cba Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 03:12:51 -1100 Subject: [PATCH 114/378] Skip 0 check --- src/komodo_nSPV.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 69cca2712..f61862622 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -952,6 +952,7 @@ UniValue NSPV_addressutxos(char *coinaddr) if ( NSPV_inforesult.height == 0 ) { msg[0] = NSPV_INFO; + fprintf(stderr,"issue getinfo\n"); NSPV_req(0,msg,1,NODE_NSPV,msg[0]>>1); } slen = (int32_t)strlen(coinaddr); @@ -959,13 +960,13 @@ UniValue NSPV_addressutxos(char *coinaddr) msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; msg[len] = 0; + fprintf(stderr,"issue addrindex\n"); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) { usleep(100000); - if ( NSPV_inforesult.height != 0 - && NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) + if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } } From 74440595f507def9be77ab7dea6db65eee11bdc3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 03:15:54 -1100 Subject: [PATCH 115/378] Remove guard --- src/komodo_nSPV.h | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f61862622..4ac396846 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -947,28 +947,25 @@ UniValue NSPV_addressutxos(char *coinaddr) result.push_back(Pair("error","invalid address")); return(result); } - if ( NSPV_inforesult.height == 0 || NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) + if ( NSPV_inforesult.height == 0 ) { - if ( NSPV_inforesult.height == 0 ) + msg[0] = NSPV_INFO; + fprintf(stderr,"issue getinfo\n"); + NSPV_req(0,msg,1,NODE_NSPV,msg[0]>>1); + } + slen = (int32_t)strlen(coinaddr); + msg[len++] = NSPV_UTXOS; + msg[len++] = slen; + memcpy(&msg[len],coinaddr,slen), len += slen; + msg[len] = 0; + fprintf(stderr,"issue addrindex\n"); + if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) + { + for (i=0; i<10; i++) { - msg[0] = NSPV_INFO; - fprintf(stderr,"issue getinfo\n"); - NSPV_req(0,msg,1,NODE_NSPV,msg[0]>>1); - } - slen = (int32_t)strlen(coinaddr); - msg[len++] = NSPV_UTXOS; - msg[len++] = slen; - memcpy(&msg[len],coinaddr,slen), len += slen; - msg[len] = 0; - fprintf(stderr,"issue addrindex\n"); - if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) - { - for (i=0; i<10; i++) - { - usleep(100000); - if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) - return(NSPV_utxosresp_json(&NSPV_utxosresult)); - } + usleep(100000); + if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height ) + return(NSPV_utxosresp_json(&NSPV_utxosresult)); } } result.push_back(Pair("result","error")); From 33d82c5bb6abcdd5064ae5fbba4a52e8d4ea1e59 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 03:20:07 -1100 Subject: [PATCH 116/378] -print --- src/komodo_nSPV.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4ac396846..e0eb47988 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -598,7 +598,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; if ( request[0] == NSPV_INFO ) // info { - fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); + //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; @@ -618,7 +618,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } else if ( request[0] == NSPV_UTXOS ) { - fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len); + //fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len); if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_utxosresp U; char coinaddr[64]; @@ -958,7 +958,6 @@ UniValue NSPV_addressutxos(char *coinaddr) msg[len++] = slen; memcpy(&msg[len],coinaddr,slen), len += slen; msg[len] = 0; - fprintf(stderr,"issue addrindex\n"); if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) @@ -1022,17 +1021,14 @@ UniValue NSPV_txproof(uint256 txid,int32_t height) msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); + //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i<10; i++) { usleep(100000); if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) - { - fprintf(stderr,"got txproof\n"); return(NSPV_txproof_json(&NSPV_txproofresult)); - } } } memset(&P,0,sizeof(P)); From 6fec63376ceb575f0ec05adecf9e472eff60e5ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 19:04:02 -1100 Subject: [PATCH 117/378] nspv_hdrsproof --- src/wallet/rpcdump.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 256a100f2..f7357aecf 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1028,7 +1028,7 @@ UniValue nspv_hdrsproof(const UniValue& params, bool fHelp) { int32_t prevheight,nextheight; if ( fHelp || params.size() != 2 ) - throw runtime_error("nspv_proof prevheight nextheight\n"); + throw runtime_error("nspv_hdrsproof prevheight nextheight\n"); prevheight = atoi((char *)params[0].get_str().c_str()); nextheight = atoi((char *)params[1].get_str().c_str()); return(NSPV_hdrsproof(prevheight,nextheight)); From 2338d9c0688483cba9af753f657fc36f0e669fed Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 19:20:17 -1100 Subject: [PATCH 118/378] Issue netinfo via rpc --- src/komodo_nSPV.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index e0eb47988..03997362c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -826,16 +826,35 @@ UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) return(result); } -UniValue NSPV_getinfo_json() +UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) { UniValue result(UniValue::VOBJ); result.push_back(Pair("result","success")); - result.push_back(Pair("height",(int64_t)NSPV_inforesult.height)); - result.push_back(Pair("chaintip",NSPV_inforesult.blockhash.GetHex())); - result.push_back(Pair("notarization",NSPV_ntz_json(&NSPV_inforesult.notarization))); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); + result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); return(result); } +UniValue NSPV_getinfo_json() +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; + if ( NSPV_inforesult.height != 0 ) + return(_NSPV_getinfo_json(&NSPV_inforesult)); + msg[len++] = NSPV_INFO; + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i<10; i++) + { + usleep(100000); + if ( NSPV_inforesult.height != 0 ) + return(_NSPV_getinfo_json(&NSPV_inforesult)); + } + } + memset(&I,0,sizeof(I)); + return(_NSPV_getinfo_json(&NSPV_inforesult)); +} + UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) { UniValue array(UniValue::VARR); int32_t i; From 99fb4fc1948ea98a3c2f1f4c5c7be1340c48583a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 20:05:17 -1100 Subject: [PATCH 119/378] Implement NSPV_getnotarization_txid --- src/komodo_nSPV.h | 21 ++++++++++----------- src/notarisationdb.cpp | 27 +++++++++++++++++++++++++++ src/notarisationdb.h | 1 + 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 03997362c..f84bc358a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -351,21 +351,20 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) // on fullnode: // NSPV_get... functions need to return the exact serialized length, which is the size of the structure minus size of pointers, plus size of allocated data +#include "notarisationdb.h" + uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) { - uint256 ntztxid; - if ( ntzheightp == 0 ) // search consecutive blocks + uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? "KMD" : ASSETCHAINS_SYMBOL; + memset(&txid,0,sizeof(txid)); + *ntzheightp = 0; + int32_t matchedHeight = ScanNotarisationsDB(height,symbol,1440,nota); + if ( matchedHeight != 0 ) { - *ntzheightp = height + 1; - // iterate + *ntzheightp = matchedHeight; + txid = nota.first; } - else - { - // check *ntzheightp - } - // find notarization for height, return its txid and set *ntzheightp - fprintf(stderr,"implement NSPV_getnotarization_txid\n"); - return(ntztxid); + return(txid); } int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) diff --git a/src/notarisationdb.cpp b/src/notarisationdb.cpp index 201743dbd..b148deccf 100644 --- a/src/notarisationdb.cpp +++ b/src/notarisationdb.cpp @@ -135,3 +135,30 @@ int ScanNotarisationsDB(int height, std::string symbol, int scanLimitBlocks, Not } return 0; } + +int ScanNotarisationsDB2(int height, std::string symbol, int scanLimitBlocks, Notarisation& out) +{ + int32_t i,maxheight,ht; + maxheight = chainActive.Height(); + if ( height < 0 || height > maxheight ) + return false; + for (i=0; i maxheight ) + break; + NotarisationsInBlock notarisations; + uint256 blockHash = *chainActive[ht]->phashBlock; + if ( !GetBlockNotarisations(blockHash,notarisations) ) + continue; + BOOST_FOREACH(Notarisation& nota,notarisations) + { + if ( strcmp(nota.second.symbol,symbol.data()) == 0 ) + { + out = nota; + return(ht); + } + } + } + return 0; +} diff --git a/src/notarisationdb.h b/src/notarisationdb.h index 38b7b3671..af5d4df28 100644 --- a/src/notarisationdb.h +++ b/src/notarisationdb.h @@ -24,6 +24,7 @@ bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n); void WriteBackNotarisations(const NotarisationsInBlock notarisations, CDBBatch &batch); void EraseBackNotarisations(const NotarisationsInBlock notarisations, CDBBatch &batch); int ScanNotarisationsDB(int height, std::string symbol, int scanLimitBlocks, Notarisation& out); +int ScanNotarisationsDB2(int height, std::string symbol, int scanLimitBlocks, Notarisation& out); bool IsTXSCL(const char* symbol); #endif /* NOTARISATIONDB_H */ From 89bcb9210af158028e4e18b00b40982c72d4737a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 20:07:13 -1100 Subject: [PATCH 120/378] Char * --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f84bc358a..f7a89bc56 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -355,7 +355,7 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) { - uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? "KMD" : ASSETCHAINS_SYMBOL; + uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(&txid,0,sizeof(txid)); *ntzheightp = 0; int32_t matchedHeight = ScanNotarisationsDB(height,symbol,1440,nota); From 11d3ffa86487db0a2650add40f6e178191ae1cf9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 21:18:25 -1100 Subject: [PATCH 121/378] Complete nota DB searching --- src/komodo_nSPV.h | 113 ++++++++++++++++++++++++++++---------------- src/komodo_notary.h | 24 ---------- 2 files changed, 72 insertions(+), 65 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f7a89bc56..d91b59998 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -358,7 +358,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(&txid,0,sizeof(txid)); *ntzheightp = 0; - int32_t matchedHeight = ScanNotarisationsDB(height,symbol,1440,nota); + int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); if ( matchedHeight != 0 ) { *ntzheightp = matchedHeight; @@ -367,16 +367,81 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) return(txid); } +uint256 NSPV_extract_desttxid(char *symbol,std::vector opret) +{ + uint256 desttxid; + memcpy(&desttxid,&opret[2 + 4 + 32],sizeof(desttxid)); + return(desttxid); +} + +int32_t komodo_notarized_bracket(uint256 txids[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) +{ + int32_t ntzht; Notarisation nota; char *symbol; + symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; + memset(txids,0,sizeof(*txids)*2); + memset(desttxids,0,sizeof(*desttxids)*2); + memset(ntzheights,0,sizeof(*ntzheights)*2); + if ( (ntzht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) + return(-1); + txids[0] = nota.first; + ntzheights[0] = ntzheight; + desttxids[0] = NSPV_extract_desttxid(symbol,E_MARSHAL(ss << nota.second)); + if ( ntzht == height ) + { + txids[1] = txids[0]; + ntzheights[1] = ntzht; + desttxids[1] = desttxids[0]; + return(0); + } + if ( (ntzht= ScanNotarisationsDB(height,symbol,1440,nota)) != 0 ) + { + txids[1] = nota.first; + ntzheights[1] = ntzht; + desttxids[1] = NSPV_extract_desttxid(symbol,E_MARSHAL(ss << nota.second)); + } + return(0); +} + +int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,uint256 desttxid,int32_t ntzheight) +{ + uint64_t value; uint32_t tiptime=0,txheighttime; + ptr->blockhash = *chainActive[ntzheight]->phashBlock; + ptr->height = ntzheight; + komodo_interest_args(&txheighttime,&ptr->txidheight,&tiptime,&value,ntztxid,0); + ptr->othertxid = desttxid; + ptr->txid = ntztxid; + return(0); +} + +int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) +{ + uint256 txids[2],desttxids[2]; int32_t ntzheights[2]; + if ( komodo_notarized_bracket(txids,ntzheights,height) == 0 ) + { + if ( ntzheights[0] != 0 ) + { + if ( NSPV_ntzextract(&ptr->prevntz,txids[0],desttxids[0],ntzheights[0]) < 0 ) + return(-1); + } + if ( ntzheights[1] != 0 ) + { + if ( NSPV_ntzextract(&ptr->nextntz,txids[1],desttxids[1],ntzheights[1]) < 0 ) + return(-1); + } + } + return(sizeof(*ptr)); +} + int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) { - int32_t prevMoMheight,len = 0; CBlockIndex *pindex; + int32_t prevMoMheight,len = 0; CBlockIndex *pindex; struct NSPV_ntzsresp pair; if ( (pindex= chainActive.LastTip()) != 0 ) { ptr->height = pindex->GetHeight(); ptr->blockhash = pindex->GetBlockHash(); - ptr->notarization.height = komodo_notarized_height(&prevMoMheight,&ptr->notarization.blockhash,&ptr->notarization.othertxid); - ptr->notarization.txidheight = 0; - ptr->notarization.txid = NSPV_getnotarization_txid(&ptr->notarization.txidheight,ptr->notarization.height); + if ( NSPV_getntzsresp(&pair,ptr->height) < 0 ) + return(-1); + ptr->notarization = pair.prevntz; return(sizeof(*ptr)); } else return(-1); } @@ -425,41 +490,6 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check return(0); } -int32_t NSPV_npextract(struct NSPV_ntz *ptr,struct notarized_checkpoint *np) -{ - int32_t ntzheight; - ptr->blockhash = np->notarized_hash; - ptr->height = np->notarized_height; - ptr->txidheight = np->nHeight; - ptr->othertxid = np->notarized_desttxid; - ntzheight = ptr->txidheight; - ptr->txid = NSPV_getnotarization_txid(&ntzheight,ptr->height); - if ( ntzheight != ptr->txidheight ) - { - fprintf(stderr,"NSPV_npextract ntzheight.%d != ptr->txidheight.%d\n",ntzheight,ptr->txidheight); - return(-1); - } else return(0); -} - -int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) -{ - struct notarized_checkpoint *nps[2]; - if ( komodo_notarized_bracket(nps,height) == 0 ) - { - if ( nps[0] != 0 ) - { - if ( NSPV_npextract(&ptr->prevntz,nps[0]) < 0 ) - return(-1); - } - if ( nps[1] != 0 ) - { - if ( NSPV_npextract(&ptr->nextntz,nps[1]) < 0 ) - return(-1); - } - } - return(sizeof(*ptr)); -} - uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) { CTransaction tx; uint8_t *rawtx = 0; @@ -743,7 +773,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } } -// nSPV client +// nSPV client. VERY simplistic "single threaded" networking model. for production GUI best to multithread, etc. + CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); diff --git a/src/komodo_notary.h b/src/komodo_notary.h index a80e035bd..028bc4dc2 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -392,30 +392,6 @@ int32_t komodo_MoMdata(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,in return(0); } -int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height) -{ - int32_t i; struct notarized_checkpoint *np; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; - memset(nps,0,sizeof(*nps)*2); - if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) - { - if ( sp->NUM_NPOINTS > 0 ) - { - for (i=0; iNUM_NPOINTS; i++) - { - np = &sp->NPOINTS[i]; - if ( np->nHeight <= height ) - nps[0] = np; - else - { - nps[1] = np; - return(0); - } - } - } - } - return(-1); -} - int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp) { struct notarized_checkpoint *np = 0; int32_t i=0,flag = 0; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; From 5c3ee387428042a9b3d49ecb2448a88d99106fc8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 21:26:43 -1100 Subject: [PATCH 122/378] Syntax --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d91b59998..eb7ecbba3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -384,7 +384,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],uint256 desttxids[2],int32_t n if ( (ntzht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) return(-1); txids[0] = nota.first; - ntzheights[0] = ntzheight; + ntzheights[0] = ntzht; desttxids[0] = NSPV_extract_desttxid(symbol,E_MARSHAL(ss << nota.second)); if ( ntzht == height ) { @@ -416,7 +416,7 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,uint256 desttxid,in int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { uint256 txids[2],desttxids[2]; int32_t ntzheights[2]; - if ( komodo_notarized_bracket(txids,ntzheights,height) == 0 ) + if ( komodo_notarized_bracket(txids,desttxids,ntzheights,height) == 0 ) { if ( ntzheights[0] != 0 ) { From 23bbfbd7b29c224f88e5337ddd6b304d17e42b2a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 21:32:41 -1100 Subject: [PATCH 123/378] Off by 1 --- src/komodo_nSPV.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index eb7ecbba3..be755fa79 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -358,7 +358,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(&txid,0,sizeof(txid)); *ntzheightp = 0; - int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); + int32_t matchedHeight = ScanNotarisationsDB2(height+1,symbol,1440,nota); if ( matchedHeight != 0 ) { *ntzheightp = matchedHeight; @@ -369,8 +369,9 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 NSPV_extract_desttxid(char *symbol,std::vector opret) { - uint256 desttxid; - memcpy(&desttxid,&opret[2 + 4 + 32],sizeof(desttxid)); + uint256 desttxid; int32_t i; + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[2 + 4 + 32 + 31 - i]; return(desttxid); } From 1c313a4adb594e94864a38fbca529b9e658491cd Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 21:44:17 -1100 Subject: [PATCH 124/378] Test --- src/komodo_nSPV.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index be755fa79..64d09d2f2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -358,7 +358,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(&txid,0,sizeof(txid)); *ntzheightp = 0; - int32_t matchedHeight = ScanNotarisationsDB2(height+1,symbol,1440,nota); + int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); if ( matchedHeight != 0 ) { *ntzheightp = matchedHeight; @@ -370,8 +370,11 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 NSPV_extract_desttxid(char *symbol,std::vector opret) { uint256 desttxid; int32_t i; + for (i=0; i Date: Thu, 4 Jul 2019 21:52:43 -1100 Subject: [PATCH 125/378] +print --- src/komodo_nSPV.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 64d09d2f2..5305fb434 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -370,11 +370,17 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 NSPV_extract_desttxid(char *symbol,std::vector opret) { uint256 desttxid; int32_t i; - for (i=0; i Date: Thu, 4 Jul 2019 22:03:08 -1100 Subject: [PATCH 126/378] Line up nth bracket --- src/komodo_nSPV.h | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 5305fb434..f717798a4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -367,57 +367,59 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) return(txid); } -uint256 NSPV_extract_desttxid(char *symbol,std::vector opret) +uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) { uint256 desttxid; int32_t i; for (i=0; i<32; i++) fprintf(stderr,"%02x",opret[i]); fprintf(stderr," blockhash, "); for (i=0; i<4; i++) - fprintf(stderr,"%02x",opret[i]); + fprintf(stderr,"%02x",opret[32+i]); fprintf(stderr," height, "); + iguana_rwnum(1,&opret[32],sizeof(*heightp),heightp); for (i=0; i<32; i++) - fprintf(stderr,"%02x",opret[i]); + fprintf(stderr,"%02x",opret[36+i]); fprintf(stderr," desttxid\n"); for (i=0; i<32; i++) ((uint8_t *)&desttxid)[i] = opret[4 + 32 + 31 - i]; return(desttxid); } -int32_t komodo_notarized_bracket(uint256 txids[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) +int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { - int32_t ntzht; Notarisation nota; char *symbol; + int32_t txidht; Notarisation nota; char *symbol; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(txids,0,sizeof(*txids)*2); memset(desttxids,0,sizeof(*desttxids)*2); memset(ntzheights,0,sizeof(*ntzheights)*2); - if ( (ntzht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) + memset(txidhts,0,sizeof(*txidhts)*2); + if ( (txidht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) return(-1); txids[0] = nota.first; - ntzheights[0] = ntzht; - desttxids[0] = NSPV_extract_desttxid(symbol,E_MARSHAL(ss << nota.second)); - if ( ntzht == height ) + txidhts[0] = txidht; + desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); + if ( ntzheights[0] == height ) { txids[1] = txids[0]; - ntzheights[1] = ntzht; + txidhts[1] = txidhts[0]; + ntzheights[1] = ntzheights[0]; desttxids[1] = desttxids[0]; return(0); } - if ( (ntzht= ScanNotarisationsDB2(height+1,symbol,1440,nota)) != 0 ) + if ( (txidht= ScanNotarisationsDB2(height+1,symbol,1440,nota)) != 0 ) { txids[1] = nota.first; - ntzheights[1] = ntzht; - desttxids[1] = NSPV_extract_desttxid(symbol,E_MARSHAL(ss << nota.second)); + txidhts[1] = txidht; + desttxids[1] = NSPV_extract_desttxid(&ntzheights[1],symbol,E_MARSHAL(ss << nota.second)); } return(0); } -int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,uint256 desttxid,int32_t ntzheight) +int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint256 desttxid,int32_t ntzheight) { - uint64_t value; uint32_t tiptime=0,txheighttime; ptr->blockhash = *chainActive[ntzheight]->phashBlock; ptr->height = ntzheight; - komodo_interest_args(&txheighttime,&ptr->txidheight,&tiptime,&value,ntztxid,0); + ptr->txidheight = txidht; ptr->othertxid = desttxid; ptr->txid = ntztxid; return(0); @@ -425,17 +427,17 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,uint256 desttxid,in int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { - uint256 txids[2],desttxids[2]; int32_t ntzheights[2]; - if ( komodo_notarized_bracket(txids,desttxids,ntzheights,height) == 0 ) + uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; + if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) { if ( ntzheights[0] != 0 ) { - if ( NSPV_ntzextract(&ptr->prevntz,txids[0],desttxids[0],ntzheights[0]) < 0 ) + if ( NSPV_ntzextract(&ptr->prevntz,txids[0],txidhts[0],desttxids[0],ntzheights[0]) < 0 ) return(-1); } if ( ntzheights[1] != 0 ) { - if ( NSPV_ntzextract(&ptr->nextntz,txids[1],desttxids[1],ntzheights[1]) < 0 ) + if ( NSPV_ntzextract(&ptr->nextntz,txids[1],txidhts[1],desttxids[1],ntzheights[1]) < 0 ) return(-1); } } From b512fb08c9edb0e876402727a65329239e721fb4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 22:12:19 -1100 Subject: [PATCH 127/378] Rev dir --- src/komodo_nSPV.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f717798a4..d70c1da33 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -376,12 +376,12 @@ uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector for (i=0; i<4; i++) fprintf(stderr,"%02x",opret[32+i]); fprintf(stderr," height, "); - iguana_rwnum(1,&opret[32],sizeof(*heightp),heightp); + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); for (i=0; i<32; i++) fprintf(stderr,"%02x",opret[36+i]); fprintf(stderr," desttxid\n"); for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + 31 - i]; + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; return(desttxid); } @@ -398,6 +398,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des txids[0] = nota.first; txidhts[0] = txidht; desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); + fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height ) { txids[1] = txids[0]; @@ -406,7 +407,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des desttxids[1] = desttxids[0]; return(0); } - if ( (txidht= ScanNotarisationsDB2(height+1,symbol,1440,nota)) != 0 ) + if ( (txidht= ScanNotarisationsDB2(height,symbol,1440,nota)) != 0 ) { txids[1] = nota.first; txidhts[1] = txidht; @@ -428,6 +429,8 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; + if ( height < chainActive.LastTip()->GetHeight() ) + height++; if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) { if ( ntzheights[0] != 0 ) From 9e392544bec0bbbed80ab000d38f1a25bfd382af Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 22:26:36 -1100 Subject: [PATCH 128/378] Remove +1 adjustment --- src/komodo_nSPV.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d70c1da33..9b14ddbd2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -370,16 +370,16 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) { uint256 desttxid; int32_t i; - for (i=0; i<32; i++) - fprintf(stderr,"%02x",opret[i]); - fprintf(stderr," blockhash, "); - for (i=0; i<4; i++) - fprintf(stderr,"%02x",opret[32+i]); - fprintf(stderr," height, "); + //for (i=0; i<32; i++) + // fprintf(stderr,"%02x",opret[i]); + //fprintf(stderr," blockhash, "); + //for (i=0; i<4; i++) + // fprintf(stderr,"%02x",opret[32+i]); + //fprintf(stderr," height, "); iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); - for (i=0; i<32; i++) - fprintf(stderr,"%02x",opret[36+i]); - fprintf(stderr," desttxid\n"); + //for (i=0; i<32; i++) + // fprintf(stderr,"%02x",opret[36+i]); + //fprintf(stderr," desttxid\n"); for (i=0; i<32; i++) ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; return(desttxid); @@ -398,7 +398,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des txids[0] = nota.first; txidhts[0] = txidht; desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); - fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); + //fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height ) { txids[1] = txids[0]; @@ -429,8 +429,8 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; - if ( height < chainActive.LastTip()->GetHeight() ) - height++; + //if ( height < chainActive.LastTip()->GetHeight() ) + // height++; if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) { if ( ntzheights[0] != 0 ) From c231439bb316890664b6f09ec0a6a9b8589e0282 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 22:36:47 -1100 Subject: [PATCH 129/378] Tweak --- src/komodo_nSPV.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9b14ddbd2..1af2f4570 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -398,8 +398,8 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des txids[0] = nota.first; txidhts[0] = txidht; desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); - //fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); - if ( ntzheights[0] == height ) + fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); + if ( ntzheights[0] == height-1 ) { txids[1] = txids[0]; txidhts[1] = txidhts[0]; @@ -429,8 +429,8 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; - //if ( height < chainActive.LastTip()->GetHeight() ) - // height++; + if ( height < chainActive.LastTip()->GetHeight() ) + height++; if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) { if ( ntzheights[0] != 0 ) @@ -454,7 +454,7 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) { ptr->height = pindex->GetHeight(); ptr->blockhash = pindex->GetBlockHash(); - if ( NSPV_getntzsresp(&pair,ptr->height) < 0 ) + if ( NSPV_getntzsresp(&pair,ptr->height-1) < 0 ) return(-1); ptr->notarization = pair.prevntz; return(sizeof(*ptr)); From 66c144d323c75de4df813a816575b165d2340e77 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:05:21 -1100 Subject: [PATCH 130/378] Filenames add "Tx" --- src/komodo_nSPV.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1af2f4570..f67fe7385 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -284,7 +284,7 @@ struct NSPV_ntzsproofresp struct NSPV_ntzproofshared common; uint256 prevtxid,nexttxid; int32_t pad32,prevtxidht,nexttxidht; - uint16_t prevlen,nextlen; + uint16_t prevtxlen,nexttxlen; uint8_t *prevntz,*nextntz; }; @@ -297,8 +297,8 @@ int32_t NSPV_rwntzsproofresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzs len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevtxidht),&ptr->prevtxidht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nexttxidht),&ptr->nexttxidht); - len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevlen,&ptr->prevntz); - len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nextlen,&ptr->nextntz); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->prevtxlen,&ptr->prevntz); + len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->nexttxlen,&ptr->nextntz); return(len); } @@ -398,8 +398,9 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des txids[0] = nota.first; txidhts[0] = txidht; desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); - fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); - if ( ntzheights[0] == height-1 ) + if ( height != 2668 ) + fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); + if ( ntzheights[0] == height-1 ) // offset the +1 from caller { txids[1] = txids[0]; txidhts[1] = txidhts[0]; @@ -606,11 +607,11 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 } } ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); - ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevlen,ptr->prevtxid); + ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); - ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nextlen,ptr->nexttxid); - //fprintf(stderr,"prevlen.%d nextlen.%d size %ld -> %ld\n",ptr->prevlen,ptr->nextlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); - return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); + //fprintf(stderr,"prevtxlen.%d nexttxlen.%d size %ld -> %ld\n",ptr->prevtxlen,ptr->nexttxlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); } int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) @@ -884,8 +885,8 @@ UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) UniValue NSPV_getinfo_json() { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; - if ( NSPV_inforesult.height != 0 ) - return(_NSPV_getinfo_json(&NSPV_inforesult)); + //if ( NSPV_inforesult.height != 0 ) + // return(_NSPV_getinfo_json(&NSPV_inforesult)); msg[len++] = NSPV_INFO; if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { @@ -961,10 +962,10 @@ UniValue NSPV_ntzsproof_json(struct NSPV_ntzsproofresp *ptr) result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); result.push_back(Pair("prevtxid",ptr->prevtxid.GetHex())); result.push_back(Pair("prevtxidht",(int64_t)ptr->prevtxidht)); - result.push_back(Pair("prevlen",(int64_t)ptr->prevlen)); + result.push_back(Pair("prevtxlen",(int64_t)ptr->prevtxlen)); result.push_back(Pair("nexttxid",ptr->nexttxid.GetHex())); result.push_back(Pair("nexttxidht",(int64_t)ptr->nexttxidht)); - result.push_back(Pair("nextlen",(int64_t)ptr->prevlen)); + result.push_back(Pair("nexttxlen",(int64_t)ptr->prevtxlen)); result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs))); return(result); From 95386444da68c8d290b22683e9982564cf39e264 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:16:37 -1100 Subject: [PATCH 131/378] Polling tweaks netinfo --- src/komodo_nSPV.h | 63 +++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f67fe7385..5b9e8eca0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -790,6 +790,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } // nSPV client. VERY simplistic "single threaded" networking model. for production GUI best to multithread, etc. +#define NSPV_POLLITERS 15 +#define NSPV_POLLMICROS 100000 CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); @@ -882,25 +884,6 @@ UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) return(result); } -UniValue NSPV_getinfo_json() -{ - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; - //if ( NSPV_inforesult.height != 0 ) - // return(_NSPV_getinfo_json(&NSPV_inforesult)); - msg[len++] = NSPV_INFO; - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - { - for (i=0; i<10; i++) - { - usleep(100000); - if ( NSPV_inforesult.height != 0 ) - return(_NSPV_getinfo_json(&NSPV_inforesult)); - } - } - memset(&I,0,sizeof(I)); - return(_NSPV_getinfo_json(&NSPV_inforesult)); -} - UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) { UniValue array(UniValue::VARR); int32_t i; @@ -1003,6 +986,28 @@ UniValue NSPV_login(char *wifstr) return(result); } +UniValue NSPV_getinfo_json() +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; + NSPV_inforesp_purge(&NSPV_inforesult); + msg[len++] = NSPV_INFO; + for (iters=0; iters<3; iters++) + { + fprintf(stderr,"issue getinfo\n"); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i>1) != 0 ) { - for (i=0; i<10; i++) + for (i=0; i= NSPV_inforesult.height ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } @@ -1046,9 +1051,9 @@ UniValue NSPV_notarizations(int32_t height) len += iguana_rwnum(1,&msg[len],sizeof(height),&height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { - for (i=0; i<10; i++) + for (i=0; i= height ) return(NSPV_ntzs_json(&NSPV_ntzsresult)); } @@ -1067,9 +1072,9 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { - for (i=0; i<10; i++) + for (i=0; i= nextheight ) return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); } @@ -1089,9 +1094,9 @@ UniValue NSPV_txproof(uint256 txid,int32_t height) //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { - for (i=0; i<10; i++) + for (i=0; i>1) != 0 ) { - for (i=0; i<10; i++) + for (i=0; i Date: Thu, 4 Jul 2019 23:22:44 -1100 Subject: [PATCH 132/378] Items --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 5b9e8eca0..b8bb5da2f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -988,7 +988,7 @@ UniValue NSPV_login(char *wifstr) UniValue NSPV_getinfo_json() { - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; + uint8_t msg[64]; int32_t i,iters,len = 0; struct NSPV_inforesp I; NSPV_inforesp_purge(&NSPV_inforesult); msg[len++] = NSPV_INFO; for (iters=0; iters<3; iters++) From 20d3e7195f85f82a3f2f077013e5b1328b97008c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:28:40 -1100 Subject: [PATCH 133/378] +prints --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b8bb5da2f..a570112a9 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -823,7 +823,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) flag = 1; pnode = ptr; break; - } // else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + } else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } } else flag = 1; if ( pnode != 0 ) @@ -831,11 +831,11 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) std::vector request; request.resize(len); memcpy(&request[0],msg,len); - //fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); + fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); pnode->PushMessage("getnSPV",request); pnode->prevtimes[ind] = timestamp; return(pnode); - } + } else fprintf(stderr,"no pnodes\n"); return(0); } From 6539eaa8bce0b979258ab0978c2075a265485863 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:29:02 -1100 Subject: [PATCH 134/378] Sleeps --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a570112a9..529919539 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1002,7 +1002,7 @@ UniValue NSPV_getinfo_json() if ( NSPV_inforesult.height != 0 ) return(_NSPV_getinfo_json(&NSPV_inforesult)); } - } + } else sleep(1); } memset(&I,0,sizeof(I)); return(_NSPV_getinfo_json(&NSPV_inforesult)); From 96cd0dc5bd3b00694ffc6985db185ed857d293e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:33:18 -1100 Subject: [PATCH 135/378] Clear garbage values --- src/komodo_nSPV.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 529919539..9afa9d19e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -816,6 +816,8 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) LOCK(cs_vNodes); BOOST_FOREACH(CNode *ptr,vNodes) { + if ( ptr->prevtimes[ind] > timestamp ) + ptr->prevtimes[ind] = 0; if ( ptr->hSocket == INVALID_SOCKET ) continue; if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) From 0fa040f54e510340252cb5903b0cdcb2ec0f6426 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:36:26 -1100 Subject: [PATCH 136/378] Clear full node prevtimes --- src/komodo_nSPV.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9afa9d19e..47926fb71 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -641,6 +641,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { if ( (ind= request[0]>>1) >= sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes) ) ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; + if ( pfrom->prevtimes[ind] > timestamp ) + pfrom->prevtimes[ind] = 0; if ( request[0] == NSPV_INFO ) // info { //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); From 3c8826dab3cf5e6ae55ae0df46e0c065a2ecb59d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 4 Jul 2019 23:57:03 -1100 Subject: [PATCH 137/378] -print --- src/komodo_nSPV.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 47926fb71..1a079eb70 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -827,7 +827,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) flag = 1; pnode = ptr; break; - } else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } } else flag = 1; if ( pnode != 0 ) @@ -835,7 +835,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) std::vector request; request.resize(len); memcpy(&request[0],msg,len); - fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); + //fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); pnode->PushMessage("getnSPV",request); pnode->prevtimes[ind] = timestamp; return(pnode); @@ -997,7 +997,7 @@ UniValue NSPV_getinfo_json() msg[len++] = NSPV_INFO; for (iters=0; iters<3; iters++) { - fprintf(stderr,"issue getinfo\n"); + //fprintf(stderr,"issue getinfo\n"); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i response) // received a r case NSPV_INFORESP: NSPV_inforesp_purge(&NSPV_inforesult); NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); - fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status + //fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status break; case NSPV_UTXOSRESP: NSPV_utxosresp_purge(&NSPV_utxosresult); From 085f99d44d2e64774cf32071ce22705393321e32 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 00:35:44 -1100 Subject: [PATCH 138/378] Add coinaddr to utxos --- src/komodo_nSPV.h | 28 ++++++++++++++++++++++++---- src/rpc/server.cpp | 1 + src/rpc/server.h | 1 + src/wallet/rpcdump.cpp | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1a079eb70..d3d17221b 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -119,6 +119,7 @@ int32_t NSPV_rwutxoresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxoresp struct NSPV_utxosresp { struct NSPV_utxoresp *utxos; + char coinaddr[64]; int64_t total,interest; int32_t nodeheight; uint16_t numutxos,pad16; @@ -139,6 +140,16 @@ int32_t NSPV_rwutxosresp(int32_t rwflag,uint8_t *serialized,struct NSPV_utxosres len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->interest),&ptr->interest); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nodeheight),&ptr->nodeheight); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); + if ( rwflag != 0 ) + { + memcpy(&serialized[len],ptr->coinaddr,sizeof(ptr->coinaddr)); + len += sizeof(ptr->coinaddr); + } + else + { + memcpy(ptr->coinaddr,&serialized[len],sizeof(ptr->coinaddr)); + len += sizeof(ptr->coinaddr); + } return(len); } @@ -469,6 +480,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check SetCCunspents(unspentOutputs,coinaddr,false); maxlen = MAX_BLOCK_SIZE(tipheight) - 512; maxlen /= sizeof(*ptr->utxos); + strncpy(ptr->coinaddr,coinaddr,sizeof(ptr->coinaddr)-1); if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) { tipheight = chainActive.LastTip()->GetHeight(); @@ -910,6 +922,7 @@ UniValue NSPV_utxosresp_json(struct NSPV_utxosresp *ptr) UniValue result(UniValue::VOBJ); result.push_back(Pair("result","success")); result.push_back(Pair("utxos",NSPV_utxoresp_json(ptr->utxos,ptr->numutxos))); + result.push_back(Pair("address",ptr->coinaddr)); result.push_back(Pair("height",(int64_t)ptr->nodeheight)); result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); result.push_back(Pair("balance",(double)ptr->total/COIN)); @@ -1218,12 +1231,19 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages } } }*/ - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) + if ( KOMODO_NSPV != 0 ) { - len = 0; - msg[len++] = NSPV_INFO; - NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) + { + len = 0; + msg[len++] = NSPV_INFO; + NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); + } } } +UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! +{ + +} #endif // KOMODO_NSPV_H diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 810848f74..c6a51d568 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -424,6 +424,7 @@ static const CRPCCommand vRPCCommands[] = { "nSPV", "nspv_notarizations", &nspv_notarizations, true }, { "nSPV", "nspv_hdrsproof", &nspv_hdrsproof, true }, { "nSPV", "nspv_txproof", &nspv_txproof, true }, + { "nSPV", "nspv_send", &nspv_send, true }, // rewards { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index 64c78ea47..37cb3d26a 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -462,6 +462,7 @@ extern UniValue nspv_spentinfo(const UniValue& params, bool fHelp); extern UniValue nspv_notarizations(const UniValue& params, bool fHelp); extern UniValue nspv_hdrsproof(const UniValue& params, bool fHelp); extern UniValue nspv_txproof(const UniValue& params, bool fHelp); +extern UniValue nspv_send(const UniValue& params, bool fHelp); extern UniValue getblocksubsidy(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index f7357aecf..b13a2b6c9 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -975,6 +975,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); +UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); UniValue NSPV_notarizations(int32_t height); UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight); @@ -1043,3 +1044,17 @@ UniValue nspv_txproof(const UniValue& params, bool fHelp) height = atoi((char *)params[1].get_str().c_str()); return(NSPV_txproof(txid,height)); } + +UniValue nspv_send(const UniValue& params, bool fHelp) +{ + uint64_t satoshis; + if ( fHelp || params.size() != 2 ) + throw runtime_error("nspv_send destaddr amount\n"); + if ( NSPV_address.size() == 0 ) + throw runtime_error("to nspv_send you need an active nspv_login\n"); + satoshis = atof(params[1].get_str().c_str())*COIN + 0.0000000049; + if ( satoshis < 1000 ) + throw runtime_error("amount too small\n"); + return(NSPV_send((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); +} + From c6f3c981ee33b41ecd05ee608018e1b3cf3984d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:46:29 -1100 Subject: [PATCH 139/378] Simple NSPV_send --- src/komodo_nSPV.h | 247 ++++++++++++++++++++++++++++++++++++-------- src/script/sign.cpp | 7 +- 2 files changed, 211 insertions(+), 43 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d3d17221b..4936c777d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -806,22 +806,28 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req // nSPV client. VERY simplistic "single threaded" networking model. for production GUI best to multithread, etc. #define NSPV_POLLITERS 15 #define NSPV_POLLMICROS 100000 +#define NSPV_MAXVINS 64 CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); uint32_t NSPV_lastinfo,NSPV_logintime; -char NSPV_wifstr[64]; +char NSPV_wifstr[64],NSPV_pubkeystr[67]; std::string NSPV_address; +CKey NSPV_key; struct NSPV_inforesp NSPV_inforesult; struct NSPV_utxosresp NSPV_utxosresult; struct NSPV_spentinfo NSPV_spentresult; struct NSPV_ntzsresp NSPV_ntzsresult; struct NSPV_ntzsproofresp NSPV_ntzsproofresult; struct NSPV_txproof NSPV_txproofresult; - struct NSPV_utxo *NSPV_utxos; +CKey *NSPV_defaultkey() +{ + return(&NSPV_key); +} + CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); @@ -990,13 +996,13 @@ UniValue NSPV_login(char *wifstr) NSPV_logintime = (uint32_t)time(NULL); result.push_back(Pair("result","success")); result.push_back(Pair("status","wif will expire in 60 seconds")); - CKey key = DecodeSecret(wifstr); - CPubKey pubkey = key.GetPubKey(); - //assert(key.VerifyPubKey(pubkey)); + NSPV_key = DecodeSecret(wifstr); + CPubKey pubkey = NSPV_key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); NSPV_address = EncodeDestination(vchAddress); result.push_back(Pair("address",NSPV_address)); result.push_back(Pair("pubkey",HexStr(pubkey))); + strcpy(NSPV_pubkeystr,HexStr(pubkey).c_str()); result.push_back(Pair("wifprefix",(int64_t)data[0])); result.push_back(Pair("compressed",(int64_t)(data[len-5] == 1))); memset(data,0,sizeof(data)); @@ -1187,6 +1193,199 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r } } +int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) +{ + int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; + abovei = belowi = -1; + for (above=below=i=0; i value ) + { + gap = (atx_value - value); + if ( above == 0 || gap < above ) + { + above = gap; + abovei = i; + } + } + else + { + gap = (value - atx_value); + if ( below == 0 || gap < below ) + { + below = gap; + belowi = i; + } + } + //printf("value %.8f gap %.8f abovei.%d %.8f belowi.%d %.8f\n",dstr(value),dstr(gap),abovei,dstr(above),belowi,dstr(below)); + } + *aboveip = abovei; + *abovep = above; + *belowip = belowi; + *belowp = below; + //printf("above.%d below.%d\n",abovei,belowi); + if ( abovei >= 0 && belowi >= 0 ) + { + if ( above < (below >> 1) ) + return(abovei); + else return(belowi); + } + else if ( abovei >= 0 ) + return(abovei); + else return(belowi); +} + +int64_t NSPV_addinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs) +{ + int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; + utxos = (struct NSPV_utxoresp *)calloc(NSPV_MAXVINS,sizeof(*utxos)); + if ( maxinputs > NSPV_MAXVINS ) + maxinputs = NSPV_MAXVINS; + if ( maxinputs > 0 ) + threshold = total/maxinputs; + else threshold = total; + remains = total; + for (i=0; i0; i++) + { + below = above = 0; + abovei = belowi = -1; + if ( NSPV_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) + { + printf("error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); + free(utxos); + return(0); + } + if ( belowi < 0 || abovei >= 0 ) + ind = abovei; + else ind = belowi; + if ( ind < 0 ) + { + printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); + free(utxos); + return(0); + } + up = &utxos[ind]; + mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); + totalinputs += up->nValue; + remains -= up->nValue; + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); + //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + if ( totalinputs >= total || (i+1) >= maxinputs ) + break; + } + free(utxos); + if ( totalinputs >= total ) + return(totalinputs); + return(0); +} + +std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) +{ + auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); + CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; + n = mtx.vout.size(); + for (i=0; i= totaloutputs+2*txfee ) + { + change = totalinputs - (totaloutputs+txfee); + mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); + } + if ( opret.size() > 0 ) + mtx.vout.push_back(CTxOut(0,opret)); + PrecomputedTransactionData txdata(mtx); + n = mtx.vin.size(); + for (i=0; i data,opret; std::string hex; + data.resize(20); + memcpy(&data[0],&rmd160[1],20); + if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) + { + mtx.vout.push_back(CTxOut(nValue,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); + hex = NSPV_signt(mtx,txfee,opret); + result.push_back(Pair("result","success")); + result.push_back(Pair("hex",hex)); + // prove all the vins + return(result); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt create tx")); + return(result); + } +} + void komodo_nSPV(CNode *pto) // polling loop from SendMessages { uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); @@ -1194,43 +1393,11 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages { fprintf(stderr,"scrub wif from NSPV memory\n"); memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + memset(&NSPV_key,0,sizeof(NSPV_key)); NSPV_logintime = 0; } if ( (pto->nServices & NODE_NSPV) == 0 ) return; - /*if ( timestamp > pto->lastntzs || timestamp > pto->lastproof ) - { - for (i=0; i pto->lastntzs ) - { - request[0] = NSPV_NTZS; - pto->lastntzs = timestamp; - pto->PushMessage("getnSPV",request); - return; - } - else if ( timestamp > pto->lastproof ) - { - if ( NSPV_utxos[i].T.txlen == 0 ) - { - request[0] = NSPV_TXPROOF; - pto->lastproof = timestamp; - pto->PushMessage("getnSPV",request); - } - else // need space for the headers... - { - request[0] = NSPV_NTZPROOF; - pto->lastproof = timestamp; - pto->PushMessage("getnSPV",request); - } - return; - } - } - } - }*/ if ( KOMODO_NSPV != 0 ) { if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) @@ -1242,8 +1409,4 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages } } -UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! -{ - -} #endif // KOMODO_NSPV_H diff --git a/src/script/sign.cpp b/src/script/sign.cpp index 685018828..fbc01669b 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -37,6 +37,7 @@ typedef vector valtype; extern uint8_t ASSETCHAINS_TXPOW; TransactionSignatureCreator::TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn) : BaseSignatureCreator(keystoreIn), txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn) {} +CKey *NSPV_defaultkey(); bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, const CKeyID& address, const CScript& scriptCode, uint32_t consensusBranchId, CKey *pprivKey, void *extraData) const { @@ -44,7 +45,11 @@ bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, if (pprivKey) key = *pprivKey; else if (!keystore || !keystore->GetKey(address, key)) - return false; + { + if ( (pprivKey= NSPV_defaultkey()) == 0 ) + return false; + else key = *pprivKey; + } uint256 hash; try { From 815ef1882ea979d687fe9a16cc9a275d583512ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:48:59 -1100 Subject: [PATCH 140/378] satoshis --- src/komodo_nSPV.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4936c777d..6dd7bdf53 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1193,13 +1193,15 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r } } +// nSPV wallet + int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) { int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; abovei = belowi = -1; for (above=below=i=0; itxid,up->vout,CScript())); - totalinputs += up->nValue; - remains -= up->nValue; + totalinputs += up->satoshis; + remains -= up->satoshis; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); @@ -1294,14 +1296,14 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; n = mtx.vout.size(); for (i=0; i= totaloutputs+2*txfee ) @@ -1318,7 +1320,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) if ( GetTransaction(mtx.vin[i].prevout.hash,vintx,hashBlock,false) != 0 ) { utxovout = mtx.vin[i].prevout.n; - if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) + if ( SignTx(mtx,i,vintx.vout[utxovout].satoshis,vintx.vout[utxovout].scriptPubKey) == 0 ) fprintf(stderr,"signing error for vini.%d of %llx\n",i,(long long)vinimask); } } @@ -1371,7 +1373,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al memcpy(&data[0],&rmd160[1],20); if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) { - mtx.vout.push_back(CTxOut(nValue,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); hex = NSPV_signt(mtx,txfee,opret); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); From f326cb4ed8819ac9958537bfc005d017af924814 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:49:35 -1100 Subject: [PATCH 141/378] syntax --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6dd7bdf53..fef2021f2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1245,7 +1245,7 @@ int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t else return(belowi); } -int64_t NSPV_addinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs) +int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs) { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; utxos = (struct NSPV_utxoresp *)calloc(NSPV_MAXVINS,sizeof(*utxos)); @@ -1374,7 +1374,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) { mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); - hex = NSPV_signt(mtx,txfee,opret); + hex = NSPV_sign(mtx,txfee,opret); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); // prove all the vins From 6f7f5bb3c63d6e5ff5a367e129f4f692f98a26bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:51:19 -1100 Subject: [PATCH 142/378] Syntax --- src/komodo_nSPV.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fef2021f2..597682175 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1296,14 +1296,13 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; n = mtx.vout.size(); for (i=0; i= totaloutputs+2*txfee ) @@ -1320,7 +1319,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) if ( GetTransaction(mtx.vin[i].prevout.hash,vintx,hashBlock,false) != 0 ) { utxovout = mtx.vin[i].prevout.n; - if ( SignTx(mtx,i,vintx.vout[utxovout].satoshis,vintx.vout[utxovout].scriptPubKey) == 0 ) + if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) fprintf(stderr,"signing error for vini.%d of %llx\n",i,(long long)vinimask); } } @@ -1374,7 +1373,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) { mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); - hex = NSPV_sign(mtx,txfee,opret); + hex = NSPV_signtx(mtx,txfee,opret); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); // prove all the vins From 56c5c2a6669f7ef4f66c1b730f94dc61db1a16ec Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:53:55 -1100 Subject: [PATCH 143/378] CScript --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 597682175..a1b71017e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1367,7 +1367,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al return(result); } CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - std::vector data,opret; std::string hex; + std::vector data; CScript opret; std::string hex; data.resize(20); memcpy(&data[0],&rmd160[1],20); if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) From 8b77ffcda918d244d868d55094f5d04a42e1b369 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:54:56 -1100 Subject: [PATCH 144/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a1b71017e..42bff4968 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1320,7 +1320,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) { utxovout = mtx.vin[i].prevout.n; if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) - fprintf(stderr,"signing error for vini.%d of %llx\n",i,(long long)vinimask); + fprintf(stderr,"signing error for vini.%d\n",i); } } return(EncodeHexTx(mtx)); From cb3c65aa4591b75784bd5826748b209fe0ba4cd6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:57:09 -1100 Subject: [PATCH 145/378] NSPV_defaultkey --- src/komodo-tx.cpp | 2 ++ src/komodo_nSPV.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/komodo-tx.cpp b/src/komodo-tx.cpp index 67ac68cd4..807c4fae4 100644 --- a/src/komodo-tx.cpp +++ b/src/komodo-tx.cpp @@ -52,6 +52,8 @@ uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 return(0); } +CKey *NSPV_defaultkey() { return(0); } + static bool fCreateBlank; static std::map registers; static const int CONTINUE_EXECUTION=-1; diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 42bff4968..a099c1795 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1387,6 +1387,8 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al } } +// polling loop + void komodo_nSPV(CNode *pto) // polling loop from SendMessages { uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); From 74cc7e050fade205bfa2b7f578cde366db17d213 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 01:59:08 -1100 Subject: [PATCH 146/378] +prints --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a099c1795..6b6408b42 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1261,7 +1261,7 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs) abovei = belowi = -1; if ( NSPV_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) { - printf("error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); + fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); free(utxos); return(0); } @@ -1270,7 +1270,7 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs) else ind = belowi; if ( ind < 0 ) { - printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); + fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); free(utxos); return(0); } @@ -1280,7 +1280,7 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs) remains -= up->satoshis; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); - //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; } From a82c26fa7d3eeda26d516c9ccb1d3169feeab977 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:03:03 -1100 Subject: [PATCH 147/378] +print --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6b6408b42..340d77d12 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1366,6 +1366,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al result.push_back(Pair("amount",(double)satoshis/COIN)); return(result); } + printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.common.numutxos,(double)NSPV_utxosresult.total/COIN); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); std::vector data; CScript opret; std::string hex; data.resize(20); From f0a715209ef8eccfa7eb4a444bc23f0135eb4509 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:04:55 -1100 Subject: [PATCH 148/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 340d77d12..54d748a5a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1366,7 +1366,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al result.push_back(Pair("amount",(double)satoshis/COIN)); return(result); } - printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.common.numutxos,(double)NSPV_utxosresult.total/COIN); + printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); std::vector data; CScript opret; std::string hex; data.resize(20); From c66229f90925ca19e8804b109c338ef4ae45772a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:11:08 -1100 Subject: [PATCH 149/378] NSPV_utxosresult.numutxos --- src/komodo_nSPV.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 54d748a5a..9317dd6e7 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1245,10 +1245,12 @@ int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t else return(belowi); } -int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs) +int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t n) { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; - utxos = (struct NSPV_utxoresp *)calloc(NSPV_MAXVINS,sizeof(*utxos)); + utxos = (struct NSPV_utxoresp *)calloc(n,sizeof(*utxos)); + for (i=0; i NSPV_MAXVINS ) maxinputs = NSPV_MAXVINS; if ( maxinputs > 0 ) @@ -1371,7 +1373,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al std::vector data; CScript opret; std::string hex; data.resize(20); memcpy(&data[0],&rmd160[1],20); - if ( NSPV_addinputs(mtx,satoshis+txfee,64) > 0 ) + if ( NSPV_addinputs(mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) { mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); hex = NSPV_signtx(mtx,txfee,opret); From c3324c8546bdea6ce26a952d40ebd5560c556c6d Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:12:18 -1100 Subject: [PATCH 150/378] Num --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9317dd6e7..a57dad634 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1245,12 +1245,12 @@ int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t else return(belowi); } -int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t n) +int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num) { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; utxos = (struct NSPV_utxoresp *)calloc(n,sizeof(*utxos)); - for (i=0; i NSPV_MAXVINS ) maxinputs = NSPV_MAXVINS; if ( maxinputs > 0 ) From fcbd27914ae912c14936f8f93b1c5dc68f9f935c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:18:21 -1100 Subject: [PATCH 151/378] +print --- src/komodo_nSPV.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a57dad634..9d0450c0f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1249,14 +1249,18 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs, { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; utxos = (struct NSPV_utxoresp *)calloc(n,sizeof(*utxos)); - for (i=0; i NSPV_MAXVINS ) maxinputs = NSPV_MAXVINS; if ( maxinputs > 0 ) threshold = total/maxinputs; else threshold = total; + for (i=0; i threshold ) + utxos[n++] = ptr[i]; + } remains = total; + fprintf(stderr,"n.%d for total %.8f\n",n,(double)total/COIN); for (i=0; i0; i++) { below = above = 0; @@ -1276,6 +1280,7 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs, free(utxos); return(0); } + fprintf(stderr,"i.%d ind.%d abovei.%d belowi.%d n.%d\n",i,ind,abovei,belowi,n); up = &utxos[ind]; mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); totalinputs += up->satoshis; @@ -1287,6 +1292,7 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs, break; } free(utxos); + fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); if ( totalinputs >= total ) return(totalinputs); return(0); @@ -1316,6 +1322,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) mtx.vout.push_back(CTxOut(0,opret)); PrecomputedTransactionData txdata(mtx); n = mtx.vin.size(); + fprintf(stderr,"sign inputs %.8f -> outputs %.8f change %.8f\n",(double)totalinputs/COIN,(double)totaloutputs/COIN,(double)change/COIN); for (i=0; i 0 ) { + fprintf(stderr,"vout\n"); mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); + fprintf(stderr,"signtx\n"); hex = NSPV_signtx(mtx,txfee,opret); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); From 7a45609a229de81f1f5dcb615c565a1f755e567e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:22:58 -1100 Subject: [PATCH 152/378] Handle single utxo case --- src/cc/CCtx.cpp | 9 +++++++-- src/komodo_nSPV.h | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 7435defbf..5c2fcf10d 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -585,8 +585,13 @@ int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int3 mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); totalinputs += up->nValue; remains -= up->nValue; - utxos[ind] = utxos[--n]; - memset(&utxos[n],0,sizeof(utxos[n])); + if ( n > 1 ) + { + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); + } else --n; + //utxos[ind] = utxos[--n]; + //memset(&utxos[n],0,sizeof(utxos[n])); //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9d0450c0f..f7fc577d5 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1285,8 +1285,11 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs, mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); totalinputs += up->satoshis; remains -= up->satoshis; - utxos[ind] = utxos[--n]; - memset(&utxos[n],0,sizeof(utxos[n])); + if ( n > 1 ) + { + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); + } else --n; fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; From e7a05fc61572edc705c4ac6ec135e48fa76a3d36 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 02:26:55 -1100 Subject: [PATCH 153/378] Alloc --- src/cc/CCtx.cpp | 9 ++------- src/komodo_nSPV.h | 9 +++------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 5c2fcf10d..7435defbf 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -585,13 +585,8 @@ int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int3 mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); totalinputs += up->nValue; remains -= up->nValue; - if ( n > 1 ) - { - utxos[ind] = utxos[--n]; - memset(&utxos[n],0,sizeof(utxos[n])); - } else --n; - //utxos[ind] = utxos[--n]; - //memset(&utxos[n],0,sizeof(utxos[n])); + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f7fc577d5..01c5b8bab 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -1248,7 +1248,7 @@ int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num) { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; - utxos = (struct NSPV_utxoresp *)calloc(n,sizeof(*utxos)); + utxos = (struct NSPV_utxoresp *)calloc(num,sizeof(*utxos)); if ( maxinputs > NSPV_MAXVINS ) maxinputs = NSPV_MAXVINS; if ( maxinputs > 0 ) @@ -1285,11 +1285,8 @@ int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs, mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); totalinputs += up->satoshis; remains -= up->satoshis; - if ( n > 1 ) - { - utxos[ind] = utxos[--n]; - memset(&utxos[n],0,sizeof(utxos[n])); - } else --n; + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; From 4d095fce5d93da908a68de7775a0d436f675f46c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:25:02 -1100 Subject: [PATCH 154/378] NSPV_gettransaction and split out into 4 files --- src/komodo_nSPV.h | 1067 +---------------------------------- src/komodo_nSPV_fullnode.h | 464 +++++++++++++++ src/komodo_nSPV_superlite.h | 413 ++++++++++++++ src/komodo_nSPV_wallet.h | 277 +++++++++ src/main.cpp | 5 +- 5 files changed, 1160 insertions(+), 1066 deletions(-) create mode 100644 src/komodo_nSPV_fullnode.h create mode 100644 src/komodo_nSPV_superlite.h create mode 100644 src/komodo_nSPV_wallet.h diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 01c5b8bab..91c6ba7f4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -23,6 +23,8 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H +// nSPV defines and struct definitions with serialization and purge functions + #define NSPV_INFO 0x00 #define NSPV_INFORESP 0x01 #define NSPV_UTXOS 0x02 @@ -359,1069 +361,4 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) } } -// on fullnode: -// NSPV_get... functions need to return the exact serialized length, which is the size of the structure minus size of pointers, plus size of allocated data - -#include "notarisationdb.h" - -uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) -{ - uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - memset(&txid,0,sizeof(txid)); - *ntzheightp = 0; - int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); - if ( matchedHeight != 0 ) - { - *ntzheightp = matchedHeight; - txid = nota.first; - } - return(txid); -} - -uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) -{ - uint256 desttxid; int32_t i; - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[i]); - //fprintf(stderr," blockhash, "); - //for (i=0; i<4; i++) - // fprintf(stderr,"%02x",opret[32+i]); - //fprintf(stderr," height, "); - iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[36+i]); - //fprintf(stderr," desttxid\n"); - for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; - return(desttxid); -} - -int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) -{ - int32_t txidht; Notarisation nota; char *symbol; - symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - memset(txids,0,sizeof(*txids)*2); - memset(desttxids,0,sizeof(*desttxids)*2); - memset(ntzheights,0,sizeof(*ntzheights)*2); - memset(txidhts,0,sizeof(*txidhts)*2); - if ( (txidht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) - return(-1); - txids[0] = nota.first; - txidhts[0] = txidht; - desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); - if ( height != 2668 ) - fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); - if ( ntzheights[0] == height-1 ) // offset the +1 from caller - { - txids[1] = txids[0]; - txidhts[1] = txidhts[0]; - ntzheights[1] = ntzheights[0]; - desttxids[1] = desttxids[0]; - return(0); - } - if ( (txidht= ScanNotarisationsDB2(height,symbol,1440,nota)) != 0 ) - { - txids[1] = nota.first; - txidhts[1] = txidht; - desttxids[1] = NSPV_extract_desttxid(&ntzheights[1],symbol,E_MARSHAL(ss << nota.second)); - } - return(0); -} - -int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint256 desttxid,int32_t ntzheight) -{ - ptr->blockhash = *chainActive[ntzheight]->phashBlock; - ptr->height = ntzheight; - ptr->txidheight = txidht; - ptr->othertxid = desttxid; - ptr->txid = ntztxid; - return(0); -} - -int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) -{ - uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; - if ( height < chainActive.LastTip()->GetHeight() ) - height++; - if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) - { - if ( ntzheights[0] != 0 ) - { - if ( NSPV_ntzextract(&ptr->prevntz,txids[0],txidhts[0],desttxids[0],ntzheights[0]) < 0 ) - return(-1); - } - if ( ntzheights[1] != 0 ) - { - if ( NSPV_ntzextract(&ptr->nextntz,txids[1],txidhts[1],desttxids[1],ntzheights[1]) < 0 ) - return(-1); - } - } - return(sizeof(*ptr)); -} - -int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) -{ - int32_t prevMoMheight,len = 0; CBlockIndex *pindex; struct NSPV_ntzsresp pair; - if ( (pindex= chainActive.LastTip()) != 0 ) - { - ptr->height = pindex->GetHeight(); - ptr->blockhash = pindex->GetBlockHash(); - if ( NSPV_getntzsresp(&pair,ptr->height-1) < 0 ) - return(-1); - ptr->notarization = pair.prevntz; - return(sizeof(*ptr)); - } else return(-1); -} - -int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check mempool -{ - int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,maxlen,txheight,n = 0,len = 0; - std::vector > unspentOutputs; - SetCCunspents(unspentOutputs,coinaddr,false); - maxlen = MAX_BLOCK_SIZE(tipheight) - 512; - maxlen /= sizeof(*ptr->utxos); - strncpy(ptr->coinaddr,coinaddr,sizeof(ptr->coinaddr)-1); - if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) - { - tipheight = chainActive.LastTip()->GetHeight(); - ptr->nodeheight = tipheight; - ptr->utxos = (struct NSPV_utxoresp *)calloc(ptr->numutxos,sizeof(*ptr->utxos)); - for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) - { - ptr->utxos[n].txid = it->first.txhash; - ptr->utxos[n].vout = (int32_t)it->first.index; - ptr->utxos[n].satoshis = it->second.satoshis; - ptr->utxos[n].height = it->second.blockHeight; - if ( ASSETCHAINS_SYMBOL[0] == 0 && it->second.satoshis >= 10*COIN ) - { - ptr->utxos[n].extradata = komodo_accrued_interest(&txheight,&locktime,ptr->utxos[n].txid,ptr->utxos[n].vout,ptr->utxos[n].height,ptr->utxos[n].satoshis,tipheight); - interest += ptr->utxos[n].extradata; - } - total += it->second.satoshis; - n++; - } - if ( len < maxlen ) - { - len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); - fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); - if ( n == ptr->numutxos ) - { - ptr->total = total; - ptr->interest = interest; - return(len); - } - } - } - if ( ptr->utxos != 0 ) - free(ptr->utxos); - memset(ptr,0,sizeof(*ptr)); - return(0); -} - -uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) -{ - CTransaction tx; uint8_t *rawtx = 0; - *txlenp = 0; - { - LOCK(cs_main); - if (!GetTransaction(txid, tx, hashBlock, false)) - return(0); - string strHex = EncodeHexTx(tx); - *txlenp = (int32_t)strHex.size() >> 1; - if ( *txlenp > 0 ) - { - rawtx = (uint8_t *)calloc(1,*txlenp); - decode_hex(rawtx,*txlenp,(char *)strHex.c_str()); - } - } - return(rawtx); -} - -int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) -{ - int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; - if ( (ptr->tx= NSPV_getrawtx(hashBlock,&ptr->txlen,txid)) == 0 ) - return(-1); - ptr->txid = txid; - ptr->height = height; - if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) - { - BOOST_FOREACH(const CTransaction&tx, block.vtx) - { - if ( tx.GetHash() == txid ) - { - flag = 1; - break; - } - } - if ( flag != 0 ) - { - set setTxids; - CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); - setTxids.insert(txid); - CMerkleBlock mb(block, setTxids); - ssMB << mb; - std::vector proof(ssMB.begin(), ssMB.end()); - ptr->txprooflen = (int32_t)proof.size(); - if ( ptr->txprooflen > 0 ) - { - ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); - memcpy(ptr->txproof,&proof[0],ptr->txprooflen); - } - //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); - return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); - } - } - return(-1); -} - -int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) -{ - CBlockIndex *pindex; - if ( (pindex= komodo_chainactive(height)) != 0 ) - { - hdr->nVersion = pindex->nVersion; - if ( pindex->pprev == 0 ) - return(-1); - hdr->hashPrevBlock = pindex->pprev->GetBlockHash(); - hdr->hashMerkleRoot = pindex->hashMerkleRoot; - hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; - hdr->nTime = pindex->nTime; - hdr->nBits = pindex->nBits; - hdr->nNonce = pindex->nNonce; - memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); - return(sizeof(*hdr)); - } - return(-1); -} - -int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) -{ - int32_t i; uint256 hashBlock; - if ( prevht > nextht || (nextht-prevht) > 1440 ) - { - fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); - return(-1); - } - ptr->common.prevht = prevht; - ptr->common.nextht = nextht; - ptr->common.numhdrs = (nextht - prevht + 1); - ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); - //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); - for (i=0; icommon.numhdrs; i++) - { - if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) - { - fprintf(stderr,"error setting hdr.%d\n",prevht+i); - free(ptr->common.hdrs); - ptr->common.hdrs = 0; - return(-1); - } - } - ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); - ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); - ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); - ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); - //fprintf(stderr,"prevtxlen.%d nexttxlen.%d size %ld -> %ld\n",ptr->prevtxlen,ptr->nexttxlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); - return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); -} - -int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) -{ - int32_t len = 0; - ptr->txid = txid; - ptr->vout = vout; - ptr->spentvini = -1; - len = (int32_t)(sizeof(*ptr) - sizeof(ptr->spent.tx) - sizeof(ptr->spent.txproof)); - if ( CCgetspenttxid(ptr->spent.txid,ptr->spentvini,ptr->spent.height,txid,vout) == 0 ) - { - if ( NSPV_gettxproof(&ptr->spent,ptr->spent.txid,ptr->spent.height) > 0 ) - len += ptr->spent.txlen + ptr->spent.txprooflen; - else - { - NSPV_txproof_purge(&ptr->spent); - return(-1); - } - } - return(len); -} - -void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request -{ - int32_t len,slen,ind; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); - if ( (len= request.size()) > 0 ) - { - if ( (ind= request[0]>>1) >= sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes) ) - ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; - if ( pfrom->prevtimes[ind] > timestamp ) - pfrom->prevtimes[ind] = 0; - if ( request[0] == NSPV_INFO ) // info - { - //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_inforesp I; - memset(&I,0,sizeof(I)); - if ( (slen= NSPV_getinfo(&I)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_INFORESP; - if ( NSPV_rwinforesp(1,&response[1],&I) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_inforesp_purge(&I); - } - } - } - else if ( request[0] == NSPV_UTXOS ) - { - //fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len); - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_utxosresp U; char coinaddr[64]; - if ( len < 64 && request[1] == len-2 ) - { - memcpy(coinaddr,&request[2],request[1]); - coinaddr[request[1]] = 0; - memset(&U,0,sizeof(U)); - if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_UTXOSRESP; - if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_utxosresp_purge(&U); - } - } - } - } - else if ( request[0] == NSPV_NTZS ) - { - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_ntzsresp N; int32_t height; - if ( len == 1+sizeof(height) ) - { - iguana_rwnum(0,&request[1],sizeof(height),&height); - memset(&N,0,sizeof(N)); - if ( (slen= NSPV_getntzsresp(&N,height)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_NTZSRESP; - if ( NSPV_rwntzsresp(1,&response[1],&N) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_ntzsresp_purge(&N); - } - } - } - } - else if ( request[0] == NSPV_NTZSPROOF ) - { - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_ntzsproofresp P; int32_t prevht,nextht; - if ( len == 1+sizeof(prevht)+sizeof(nextht) ) - { - iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); - iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); - if ( prevht != 0 && nextht != 0 && nextht >= prevht ) - { - memset(&P,0,sizeof(P)); - if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_NTZSPROOFRESP; - P.common.numhdrs = (nextht - prevht + 1); - if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_ntzsproofresp_purge(&P); - } - } - } - } - } - else if ( request[0] == NSPV_TXPROOF ) - { - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_txproof P; uint256 txid; int32_t height; - if ( len == 1+sizeof(txid)+sizeof(height) ) - { - iguana_rwnum(0,&request[1],sizeof(height),&height); - iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); - //fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); - memset(&P,0,sizeof(P)); - if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_TXPROOFRESP; - if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_txproof_purge(&P); - } - } - } - } - else if ( request[0] == NSPV_SPENTINFO ) - { - if ( timestamp > pfrom->prevtimes[ind] ) - { - struct NSPV_spentinfo S; int32_t vout; uint256 txid; - if ( len == 1+sizeof(txid)+sizeof(vout) ) - { - iguana_rwnum(0,&request[1],sizeof(vout),&vout); - iguana_rwbignum(0,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); - memset(&S,0,sizeof(S)); - if ( (slen= NSPV_getspentinfo(&S,txid,vout)) > 0 ) - { - response.resize(1 + slen); - response[0] = NSPV_SPENTINFORESP; - if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_spentinfo_purge(&S); - } - } - } - } - } -} - -// nSPV client. VERY simplistic "single threaded" networking model. for production GUI best to multithread, etc. -#define NSPV_POLLITERS 15 -#define NSPV_POLLMICROS 100000 -#define NSPV_MAXVINS 64 - -CAmount AmountFromValue(const UniValue& value); -int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); - -uint32_t NSPV_lastinfo,NSPV_logintime; -char NSPV_wifstr[64],NSPV_pubkeystr[67]; -std::string NSPV_address; -CKey NSPV_key; -struct NSPV_inforesp NSPV_inforesult; -struct NSPV_utxosresp NSPV_utxosresult; -struct NSPV_spentinfo NSPV_spentresult; -struct NSPV_ntzsresp NSPV_ntzsresult; -struct NSPV_ntzsproofresp NSPV_ntzsproofresult; -struct NSPV_txproof NSPV_txproofresult; -struct NSPV_utxo *NSPV_utxos; - -CKey *NSPV_defaultkey() -{ - return(&NSPV_key); -} - -CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) -{ - int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); - if ( pnode == 0 ) - { - LOCK(cs_vNodes); - BOOST_FOREACH(CNode *ptr,vNodes) - { - if ( ptr->prevtimes[ind] > timestamp ) - ptr->prevtimes[ind] = 0; - if ( ptr->hSocket == INVALID_SOCKET ) - continue; - if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) - { - flag = 1; - pnode = ptr; - break; - } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); - } - } else flag = 1; - if ( pnode != 0 ) - { - std::vector request; - request.resize(len); - memcpy(&request[0],msg,len); - //fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); - pnode->PushMessage("getnSPV",request); - pnode->prevtimes[ind] = timestamp; - return(pnode); - } else fprintf(stderr,"no pnodes\n"); - return(0); -} - -UniValue NSPV_txproof_json(struct NSPV_txproof *ptr) -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("txid",ptr->txid.GetHex())); - result.push_back(Pair("height",(int64_t)ptr->height)); - result.push_back(Pair("txlen",(int64_t)ptr->txlen)); - result.push_back(Pair("txprooflen",(int64_t)ptr->txprooflen)); - return(result); -} - -UniValue NSPV_spentinfo_json(struct NSPV_spentinfo *ptr) -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("result","success")); - result.push_back(Pair("txid",ptr->txid.GetHex())); - result.push_back(Pair("vout",(int64_t)ptr->vout)); - result.push_back(Pair("spentheight",(int64_t)ptr->spent.height)); - result.push_back(Pair("spenttxid",ptr->spent.txid.GetHex())); - result.push_back(Pair("spentvini",(int64_t)ptr->spentvini)); - result.push_back(Pair("spenttxlen",(int64_t)ptr->spent.txlen)); - result.push_back(Pair("spenttxprooflen",(int64_t)ptr->spent.txprooflen)); - return(result); -} - -UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("notarized_height",(int64_t)ptr->height)); - result.push_back(Pair("notarized_blockhash",ptr->blockhash.GetHex())); - result.push_back(Pair("notarization_txid",ptr->txid.GetHex())); - result.push_back(Pair("notarization_txidheight",(int64_t)ptr->txidheight)); - result.push_back(Pair("notarization_desttxid",ptr->othertxid.GetHex())); - return(result); -} - -UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("result","success")); - result.push_back(Pair("height",(int64_t)ptr->height)); - result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); - result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); - return(result); -} - -UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) -{ - UniValue array(UniValue::VARR); int32_t i; - for (i=0; iutxos,ptr->numutxos))); - result.push_back(Pair("address",ptr->coinaddr)); - result.push_back(Pair("height",(int64_t)ptr->nodeheight)); - result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); - result.push_back(Pair("balance",(double)ptr->total/COIN)); - if ( ASSETCHAINS_SYMBOL[0] == 0 ) - result.push_back(Pair("interest",(double)ptr->interest/COIN)); - return(result); -} - -UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr) -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("result","success")); - result.push_back(Pair("prev",NSPV_ntz_json(&ptr->prevntz))); - result.push_back(Pair("next",NSPV_ntz_json(&ptr->nextntz))); - return(result); -} - -UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs) -{ - UniValue array(UniValue::VARR); int32_t i; - for (i=0; icommon.prevht)); - result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); - result.push_back(Pair("prevtxid",ptr->prevtxid.GetHex())); - result.push_back(Pair("prevtxidht",(int64_t)ptr->prevtxidht)); - result.push_back(Pair("prevtxlen",(int64_t)ptr->prevtxlen)); - result.push_back(Pair("nexttxid",ptr->nexttxid.GetHex())); - result.push_back(Pair("nexttxidht",(int64_t)ptr->nexttxidht)); - result.push_back(Pair("nexttxlen",(int64_t)ptr->prevtxlen)); - result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); - result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs))); - return(result); -} - -UniValue NSPV_login(char *wifstr) -{ - UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; - len = bitcoin_base58decode(data,wifstr); - if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) - valid = 1; - if ( valid == 0 || data[0] != 188 ) - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","invalid wif")); - result.push_back(Pair("len",(int64_t)len)); - result.push_back(Pair("prefix",(int64_t)data[0])); - return(result); - } - memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); - strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); - NSPV_logintime = (uint32_t)time(NULL); - result.push_back(Pair("result","success")); - result.push_back(Pair("status","wif will expire in 60 seconds")); - NSPV_key = DecodeSecret(wifstr); - CPubKey pubkey = NSPV_key.GetPubKey(); - CKeyID vchAddress = pubkey.GetID(); - NSPV_address = EncodeDestination(vchAddress); - result.push_back(Pair("address",NSPV_address)); - result.push_back(Pair("pubkey",HexStr(pubkey))); - strcpy(NSPV_pubkeystr,HexStr(pubkey).c_str()); - result.push_back(Pair("wifprefix",(int64_t)data[0])); - result.push_back(Pair("compressed",(int64_t)(data[len-5] == 1))); - memset(data,0,sizeof(data)); - return(result); -} - -UniValue NSPV_getinfo_json() -{ - uint8_t msg[64]; int32_t i,iters,len = 0; struct NSPV_inforesp I; - NSPV_inforesp_purge(&NSPV_inforesult); - msg[len++] = NSPV_INFO; - for (iters=0; iters<3; iters++) - { - //fprintf(stderr,"issue getinfo\n"); - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - { - for (i=0; i>1); - } - slen = (int32_t)strlen(coinaddr); - msg[len++] = NSPV_UTXOS; - msg[len++] = slen; - memcpy(&msg[len],coinaddr,slen), len += slen; - msg[len] = 0; - if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) - { - for (i=0; i= NSPV_inforesult.height ) - return(NSPV_utxosresp_json(&NSPV_utxosresult)); - } - } - result.push_back(Pair("result","error")); - result.push_back(Pair("error","no utxos result")); - return(result); -} - -UniValue NSPV_notarizations(int32_t height) -{ - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsresp N; - if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) - return(NSPV_ntzs_json(&NSPV_ntzsresult)); - msg[len++] = NSPV_NTZS; - len += iguana_rwnum(1,&msg[len],sizeof(height),&height); - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - { - for (i=0; i= height ) - return(NSPV_ntzs_json(&NSPV_ntzsresult)); - } - } - memset(&N,0,sizeof(N)); - return(NSPV_ntzs_json(&N)); -} - -UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) -{ - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsproofresp H; - if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) - return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); - msg[len++] = NSPV_NTZSPROOF; - len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); - len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - { - for (i=0; i= nextheight ) - return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); - } - } - memset(&H,0,sizeof(H)); - return(NSPV_ntzsproof_json(&H)); -} - -UniValue NSPV_txproof(uint256 txid,int32_t height) -{ - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_txproof P; - if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) - return(NSPV_txproof_json(&NSPV_txproofresult)); - msg[len++] = NSPV_TXPROOF; - len += iguana_rwnum(1,&msg[len],sizeof(height),&height); - len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) - { - for (i=0; i>1) != 0 ) - { - for (i=0; i response) // received a response -{ - int32_t len; uint32_t timestamp = (uint32_t)time(NULL); - if ( (len= response.size()) > 0 ) - { - switch ( response[0] ) - { - case NSPV_INFORESP: - NSPV_inforesp_purge(&NSPV_inforesult); - NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); - //fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status - break; - case NSPV_UTXOSRESP: - NSPV_utxosresp_purge(&NSPV_utxosresult); - NSPV_rwutxosresp(0,&response[1],&NSPV_utxosresult); - fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list - break; - case NSPV_NTZSRESP: - NSPV_ntzsresp_purge(&NSPV_ntzsresult); - NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); - fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] - break; - case NSPV_NTZSPROOFRESP: - NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); - NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); - fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); // update utxos[i] - break; - case NSPV_TXPROOFRESP: - NSPV_txproof_purge(&NSPV_txproofresult); - NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); - fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] - break; - case NSPV_SPENTINFORESP: - - NSPV_spentinfo_purge(&NSPV_spentresult); - NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); - fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] - break; - default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); - break; - } - } -} - -// nSPV wallet - -int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) -{ - int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; - abovei = belowi = -1; - for (above=below=i=0; i value ) - { - gap = (atx_value - value); - if ( above == 0 || gap < above ) - { - above = gap; - abovei = i; - } - } - else - { - gap = (value - atx_value); - if ( below == 0 || gap < below ) - { - below = gap; - belowi = i; - } - } - //printf("value %.8f gap %.8f abovei.%d %.8f belowi.%d %.8f\n",dstr(value),dstr(gap),abovei,dstr(above),belowi,dstr(below)); - } - *aboveip = abovei; - *abovep = above; - *belowip = belowi; - *belowp = below; - //printf("above.%d below.%d\n",abovei,belowi); - if ( abovei >= 0 && belowi >= 0 ) - { - if ( above < (below >> 1) ) - return(abovei); - else return(belowi); - } - else if ( abovei >= 0 ) - return(abovei); - else return(belowi); -} - -int64_t NSPV_addinputs(CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num) -{ - int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp *utxos,*up; - utxos = (struct NSPV_utxoresp *)calloc(num,sizeof(*utxos)); - if ( maxinputs > NSPV_MAXVINS ) - maxinputs = NSPV_MAXVINS; - if ( maxinputs > 0 ) - threshold = total/maxinputs; - else threshold = total; - for (i=0; i threshold ) - utxos[n++] = ptr[i]; - } - remains = total; - fprintf(stderr,"n.%d for total %.8f\n",n,(double)total/COIN); - for (i=0; i0; i++) - { - below = above = 0; - abovei = belowi = -1; - if ( NSPV_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) - { - fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); - free(utxos); - return(0); - } - if ( belowi < 0 || abovei >= 0 ) - ind = abovei; - else ind = belowi; - if ( ind < 0 ) - { - fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); - free(utxos); - return(0); - } - fprintf(stderr,"i.%d ind.%d abovei.%d belowi.%d n.%d\n",i,ind,abovei,belowi,n); - up = &utxos[ind]; - mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); - totalinputs += up->satoshis; - remains -= up->satoshis; - utxos[ind] = utxos[--n]; - memset(&utxos[n],0,sizeof(utxos[n])); - fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); - if ( totalinputs >= total || (i+1) >= maxinputs ) - break; - } - free(utxos); - fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); - if ( totalinputs >= total ) - return(totalinputs); - return(0); -} - -std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret) -{ - auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; - n = mtx.vout.size(); - for (i=0; i= totaloutputs+2*txfee ) - { - change = totalinputs - (totaloutputs+txfee); - mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); - } - if ( opret.size() > 0 ) - mtx.vout.push_back(CTxOut(0,opret)); - PrecomputedTransactionData txdata(mtx); - n = mtx.vin.size(); - fprintf(stderr,"sign inputs %.8f -> outputs %.8f change %.8f\n",(double)totalinputs/COIN,(double)totaloutputs/COIN,(double)change/COIN); - for (i=0; i data; CScript opret; std::string hex; - data.resize(20); - memcpy(&data[0],&rmd160[1],20); - if ( NSPV_addinputs(mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) - { - fprintf(stderr,"vout\n"); - mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); - fprintf(stderr,"signtx\n"); - hex = NSPV_signtx(mtx,txfee,opret); - result.push_back(Pair("result","success")); - result.push_back(Pair("hex",hex)); - // prove all the vins - return(result); - } - else - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","couldnt create tx")); - return(result); - } -} - -// polling loop - -void komodo_nSPV(CNode *pto) // polling loop from SendMessages -{ - uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); - if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+60 ) - { - fprintf(stderr,"scrub wif from NSPV memory\n"); - memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); - memset(&NSPV_key,0,sizeof(NSPV_key)); - NSPV_logintime = 0; - } - if ( (pto->nServices & NODE_NSPV) == 0 ) - return; - if ( KOMODO_NSPV != 0 ) - { - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) - { - len = 0; - msg[len++] = NSPV_INFO; - NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); - } - } -} - #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h new file mode 100644 index 000000000..f493512a6 --- /dev/null +++ b/src/komodo_nSPV_fullnode.h @@ -0,0 +1,464 @@ + +/****************************************************************************** + * Copyright © 2014-2019 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef KOMODO_NSPVFULLNODE_H +#define KOMODO_NSPVFULLNODE_H + +// on fullnode: +// NSPV_get... functions need to return the exact serialized length, which is the size of the structure minus size of pointers, plus size of allocated data + +#include "notarisationdb.h" + +uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) +{ + uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; + memset(&txid,0,sizeof(txid)); + *ntzheightp = 0; + int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); + if ( matchedHeight != 0 ) + { + *ntzheightp = matchedHeight; + txid = nota.first; + } + return(txid); +} + +uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) +{ + uint256 desttxid; int32_t i; + //for (i=0; i<32; i++) + // fprintf(stderr,"%02x",opret[i]); + //fprintf(stderr," blockhash, "); + //for (i=0; i<4; i++) + // fprintf(stderr,"%02x",opret[32+i]); + //fprintf(stderr," height, "); + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + //for (i=0; i<32; i++) + // fprintf(stderr,"%02x",opret[36+i]); + //fprintf(stderr," desttxid\n"); + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + return(desttxid); +} + +int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) +{ + int32_t txidht; Notarisation nota; char *symbol; + symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; + memset(txids,0,sizeof(*txids)*2); + memset(desttxids,0,sizeof(*desttxids)*2); + memset(ntzheights,0,sizeof(*ntzheights)*2); + memset(txidhts,0,sizeof(*txidhts)*2); + if ( (txidht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) + return(-1); + txids[0] = nota.first; + txidhts[0] = txidht; + desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); + if ( height != 2668 ) + fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); + if ( ntzheights[0] == height-1 ) // offset the +1 from caller + { + txids[1] = txids[0]; + txidhts[1] = txidhts[0]; + ntzheights[1] = ntzheights[0]; + desttxids[1] = desttxids[0]; + return(0); + } + if ( (txidht= ScanNotarisationsDB2(height,symbol,1440,nota)) != 0 ) + { + txids[1] = nota.first; + txidhts[1] = txidht; + desttxids[1] = NSPV_extract_desttxid(&ntzheights[1],symbol,E_MARSHAL(ss << nota.second)); + } + return(0); +} + +int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint256 desttxid,int32_t ntzheight) +{ + ptr->blockhash = *chainActive[ntzheight]->phashBlock; + ptr->height = ntzheight; + ptr->txidheight = txidht; + ptr->othertxid = desttxid; + ptr->txid = ntztxid; + return(0); +} + +int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) +{ + uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; + if ( height < chainActive.LastTip()->GetHeight() ) + height++; + if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) + { + if ( ntzheights[0] != 0 ) + { + if ( NSPV_ntzextract(&ptr->prevntz,txids[0],txidhts[0],desttxids[0],ntzheights[0]) < 0 ) + return(-1); + } + if ( ntzheights[1] != 0 ) + { + if ( NSPV_ntzextract(&ptr->nextntz,txids[1],txidhts[1],desttxids[1],ntzheights[1]) < 0 ) + return(-1); + } + } + return(sizeof(*ptr)); +} + +int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) +{ + int32_t prevMoMheight,len = 0; CBlockIndex *pindex; struct NSPV_ntzsresp pair; + if ( (pindex= chainActive.LastTip()) != 0 ) + { + ptr->height = pindex->GetHeight(); + ptr->blockhash = pindex->GetBlockHash(); + if ( NSPV_getntzsresp(&pair,ptr->height-1) < 0 ) + return(-1); + ptr->notarization = pair.prevntz; + return(sizeof(*ptr)); + } else return(-1); +} + +int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check mempool +{ + int64_t total = 0,interest=0; uint32_t locktime; int32_t tipheight,maxlen,txheight,n = 0,len = 0; + std::vector > unspentOutputs; + SetCCunspents(unspentOutputs,coinaddr,false); + maxlen = MAX_BLOCK_SIZE(tipheight) - 512; + maxlen /= sizeof(*ptr->utxos); + strncpy(ptr->coinaddr,coinaddr,sizeof(ptr->coinaddr)-1); + if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) + { + tipheight = chainActive.LastTip()->GetHeight(); + ptr->nodeheight = tipheight; + ptr->utxos = (struct NSPV_utxoresp *)calloc(ptr->numutxos,sizeof(*ptr->utxos)); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + ptr->utxos[n].txid = it->first.txhash; + ptr->utxos[n].vout = (int32_t)it->first.index; + ptr->utxos[n].satoshis = it->second.satoshis; + ptr->utxos[n].height = it->second.blockHeight; + if ( ASSETCHAINS_SYMBOL[0] == 0 && it->second.satoshis >= 10*COIN ) + { + ptr->utxos[n].extradata = komodo_accrued_interest(&txheight,&locktime,ptr->utxos[n].txid,ptr->utxos[n].vout,ptr->utxos[n].height,ptr->utxos[n].satoshis,tipheight); + interest += ptr->utxos[n].extradata; + } + total += it->second.satoshis; + n++; + } + if ( len < maxlen ) + { + len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); + fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); + if ( n == ptr->numutxos ) + { + ptr->total = total; + ptr->interest = interest; + return(len); + } + } + } + if ( ptr->utxos != 0 ) + free(ptr->utxos); + memset(ptr,0,sizeof(*ptr)); + return(0); +} + +uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) +{ + CTransaction tx; uint8_t *rawtx = 0; + *txlenp = 0; + { + LOCK(cs_main); + if (!GetTransaction(txid, tx, hashBlock, false)) + return(0); + string strHex = EncodeHexTx(tx); + *txlenp = (int32_t)strHex.size() >> 1; + if ( *txlenp > 0 ) + { + rawtx = (uint8_t *)calloc(1,*txlenp); + decode_hex(rawtx,*txlenp,(char *)strHex.c_str()); + } + } + return(rawtx); +} + +int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) +{ + int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; + if ( (ptr->tx= NSPV_getrawtx(hashBlock,&ptr->txlen,txid)) == 0 ) + return(-1); + ptr->txid = txid; + ptr->height = height; + if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) + { + BOOST_FOREACH(const CTransaction&tx, block.vtx) + { + if ( tx.GetHash() == txid ) + { + flag = 1; + break; + } + } + if ( flag != 0 ) + { + set setTxids; + CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION); + setTxids.insert(txid); + CMerkleBlock mb(block, setTxids); + ssMB << mb; + std::vector proof(ssMB.begin(), ssMB.end()); + ptr->txprooflen = (int32_t)proof.size(); + if ( ptr->txprooflen > 0 ) + { + ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); + memcpy(ptr->txproof,&proof[0],ptr->txprooflen); + } + //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); + return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); + } + } + return(-1); +} + +int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) +{ + CBlockIndex *pindex; + if ( (pindex= komodo_chainactive(height)) != 0 ) + { + hdr->nVersion = pindex->nVersion; + if ( pindex->pprev == 0 ) + return(-1); + hdr->hashPrevBlock = pindex->pprev->GetBlockHash(); + hdr->hashMerkleRoot = pindex->hashMerkleRoot; + hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; + hdr->nTime = pindex->nTime; + hdr->nBits = pindex->nBits; + hdr->nNonce = pindex->nNonce; + memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); + return(sizeof(*hdr)); + } + return(-1); +} + +int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) +{ + int32_t i; uint256 hashBlock; + if ( prevht > nextht || (nextht-prevht) > 1440 ) + { + fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); + return(-1); + } + ptr->common.prevht = prevht; + ptr->common.nextht = nextht; + ptr->common.numhdrs = (nextht - prevht + 1); + ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); + //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); + for (i=0; icommon.numhdrs; i++) + { + if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) + { + fprintf(stderr,"error setting hdr.%d\n",prevht+i); + free(ptr->common.hdrs); + ptr->common.hdrs = 0; + return(-1); + } + } + ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); + ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); + ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); + ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); + //fprintf(stderr,"prevtxlen.%d nexttxlen.%d size %ld -> %ld\n",ptr->prevtxlen,ptr->nexttxlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); +} + +int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) +{ + int32_t len = 0; + ptr->txid = txid; + ptr->vout = vout; + ptr->spentvini = -1; + len = (int32_t)(sizeof(*ptr) - sizeof(ptr->spent.tx) - sizeof(ptr->spent.txproof)); + if ( CCgetspenttxid(ptr->spent.txid,ptr->spentvini,ptr->spent.height,txid,vout) == 0 ) + { + if ( NSPV_gettxproof(&ptr->spent,ptr->spent.txid,ptr->spent.height) > 0 ) + len += ptr->spent.txlen + ptr->spent.txprooflen; + else + { + NSPV_txproof_purge(&ptr->spent); + return(-1); + } + } + return(len); +} + +void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request +{ + int32_t len,slen,ind; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); + if ( (len= request.size()) > 0 ) + { + if ( (ind= request[0]>>1) >= sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes) ) + ind = (int32_t)(sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes)) - 1; + if ( pfrom->prevtimes[ind] > timestamp ) + pfrom->prevtimes[ind] = 0; + if ( request[0] == NSPV_INFO ) // info + { + //fprintf(stderr,"check info %u vs %u, ind.%d\n",timestamp,pfrom->prevtimes[ind],ind); + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_inforesp I; + memset(&I,0,sizeof(I)); + if ( (slen= NSPV_getinfo(&I)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_INFORESP; + if ( NSPV_rwinforesp(1,&response[1],&I) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_inforesp_purge(&I); + } + } + } + else if ( request[0] == NSPV_UTXOS ) + { + //fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len); + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_utxosresp U; char coinaddr[64]; + if ( len < 64 && request[1] == len-2 ) + { + memcpy(coinaddr,&request[2],request[1]); + coinaddr[request[1]] = 0; + memset(&U,0,sizeof(U)); + if ( (slen= NSPV_getaddressutxos(&U,coinaddr)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_UTXOSRESP; + if ( NSPV_rwutxosresp(1,&response[1],&U) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_utxosresp_purge(&U); + } + } + } + } + else if ( request[0] == NSPV_NTZS ) + { + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_ntzsresp N; int32_t height; + if ( len == 1+sizeof(height) ) + { + iguana_rwnum(0,&request[1],sizeof(height),&height); + memset(&N,0,sizeof(N)); + if ( (slen= NSPV_getntzsresp(&N,height)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_NTZSRESP; + if ( NSPV_rwntzsresp(1,&response[1],&N) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_ntzsresp_purge(&N); + } + } + } + } + else if ( request[0] == NSPV_NTZSPROOF ) + { + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_ntzsproofresp P; int32_t prevht,nextht; + if ( len == 1+sizeof(prevht)+sizeof(nextht) ) + { + iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); + iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); + if ( prevht != 0 && nextht != 0 && nextht >= prevht ) + { + memset(&P,0,sizeof(P)); + if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_NTZSPROOFRESP; + P.common.numhdrs = (nextht - prevht + 1); + if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_ntzsproofresp_purge(&P); + } + } + } + } + } + else if ( request[0] == NSPV_TXPROOF ) + { + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_txproof P; uint256 txid; int32_t height; + if ( len == 1+sizeof(txid)+sizeof(height) ) + { + iguana_rwnum(0,&request[1],sizeof(height),&height); + iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); + //fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); + memset(&P,0,sizeof(P)); + if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_TXPROOFRESP; + if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_txproof_purge(&P); + } + } + } + } + else if ( request[0] == NSPV_SPENTINFO ) + { + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_spentinfo S; int32_t vout; uint256 txid; + if ( len == 1+sizeof(txid)+sizeof(vout) ) + { + iguana_rwnum(0,&request[1],sizeof(vout),&vout); + iguana_rwbignum(0,&request[1+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); + memset(&S,0,sizeof(S)); + if ( (slen= NSPV_getspentinfo(&S,txid,vout)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_SPENTINFORESP; + if ( NSPV_rwspentinfo(1,&response[1],&S) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_spentinfo_purge(&S); + } + } + } + } + } +} + +#endif // KOMODO_NSPVFULLNODE_H diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h new file mode 100644 index 000000000..aeca64a9d --- /dev/null +++ b/src/komodo_nSPV_superlite.h @@ -0,0 +1,413 @@ + +/****************************************************************************** + * Copyright © 2014-2019 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef KOMODO_NSPVSUPERLITE_H +#define KOMODO_NSPVSUPERLITE_H + +// nSPV client. VERY simplistic "single threaded" networking model. for production GUI best to multithread, etc. +// no caching, no optimizations, no reducing the number of ntzsproofs needed by detecting overlaps, etc. +// advantage is that it is simpler to implement and understand to create a design for a more performant version + +#define NSPV_POLLITERS 15 +#define NSPV_POLLMICROS 100000 +#define NSPV_MAXVINS 64 + +CAmount AmountFromValue(const UniValue& value); +int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); + +uint32_t NSPV_lastinfo,NSPV_logintime; +char NSPV_wifstr[64],NSPV_pubkeystr[67]; +std::string NSPV_address; +CKey NSPV_key; +struct NSPV_inforesp NSPV_inforesult; +struct NSPV_utxosresp NSPV_utxosresult; +struct NSPV_spentinfo NSPV_spentresult; +struct NSPV_ntzsresp NSPV_ntzsresult; +struct NSPV_ntzsproofresp NSPV_ntzsproofresult; +struct NSPV_txproof NSPV_txproofresult; +struct NSPV_utxo *NSPV_utxos; + +CKey *NSPV_defaultkey() +{ + return(&NSPV_key); +} + +CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) +{ + int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); + if ( pnode == 0 ) + { + LOCK(cs_vNodes); + BOOST_FOREACH(CNode *ptr,vNodes) + { + if ( ptr->prevtimes[ind] > timestamp ) + ptr->prevtimes[ind] = 0; + if ( ptr->hSocket == INVALID_SOCKET ) + continue; + if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) + { + flag = 1; + pnode = ptr; + break; + } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + } + } else flag = 1; + if ( pnode != 0 ) + { + std::vector request; + request.resize(len); + memcpy(&request[0],msg,len); + //fprintf(stderr,"pushmessage [%d] len.%d\n",msg[0],len); + pnode->PushMessage("getnSPV",request); + pnode->prevtimes[ind] = timestamp; + return(pnode); + } else fprintf(stderr,"no pnodes\n"); + return(0); +} + +UniValue NSPV_txproof_json(struct NSPV_txproof *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("txlen",(int64_t)ptr->txlen)); + result.push_back(Pair("txprooflen",(int64_t)ptr->txprooflen)); + return(result); +} + +UniValue NSPV_spentinfo_json(struct NSPV_spentinfo *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("vout",(int64_t)ptr->vout)); + result.push_back(Pair("spentheight",(int64_t)ptr->spent.height)); + result.push_back(Pair("spenttxid",ptr->spent.txid.GetHex())); + result.push_back(Pair("spentvini",(int64_t)ptr->spentvini)); + result.push_back(Pair("spenttxlen",(int64_t)ptr->spent.txlen)); + result.push_back(Pair("spenttxprooflen",(int64_t)ptr->spent.txprooflen)); + return(result); +} + +UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("notarized_height",(int64_t)ptr->height)); + result.push_back(Pair("notarized_blockhash",ptr->blockhash.GetHex())); + result.push_back(Pair("notarization_txid",ptr->txid.GetHex())); + result.push_back(Pair("notarization_txidheight",(int64_t)ptr->txidheight)); + result.push_back(Pair("notarization_desttxid",ptr->othertxid.GetHex())); + return(result); +} + +UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("height",(int64_t)ptr->height)); + result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); + result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); + return(result); +} + +UniValue NSPV_utxoresp_json(struct NSPV_utxoresp *utxos,int32_t numutxos) +{ + UniValue array(UniValue::VARR); int32_t i; + for (i=0; iutxos,ptr->numutxos))); + result.push_back(Pair("address",ptr->coinaddr)); + result.push_back(Pair("height",(int64_t)ptr->nodeheight)); + result.push_back(Pair("numutxos",(int64_t)ptr->numutxos)); + result.push_back(Pair("balance",(double)ptr->total/COIN)); + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + result.push_back(Pair("interest",(double)ptr->interest/COIN)); + return(result); +} + +UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("prev",NSPV_ntz_json(&ptr->prevntz))); + result.push_back(Pair("next",NSPV_ntz_json(&ptr->nextntz))); + return(result); +} + +UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs) +{ + UniValue array(UniValue::VARR); int32_t i; + for (i=0; icommon.prevht)); + result.push_back(Pair("nextht",(int64_t)ptr->common.nextht)); + result.push_back(Pair("prevtxid",ptr->prevtxid.GetHex())); + result.push_back(Pair("prevtxidht",(int64_t)ptr->prevtxidht)); + result.push_back(Pair("prevtxlen",(int64_t)ptr->prevtxlen)); + result.push_back(Pair("nexttxid",ptr->nexttxid.GetHex())); + result.push_back(Pair("nexttxidht",(int64_t)ptr->nexttxidht)); + result.push_back(Pair("nexttxlen",(int64_t)ptr->prevtxlen)); + result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); + result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs))); + return(result); +} + +UniValue NSPV_login(char *wifstr) +{ + UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; + len = bitcoin_base58decode(data,wifstr); + if ( strlen(wifstr) < 64 && (len == 38 && data[len-5] == 1) || (len == 37 && data[len-5] != 1) ) + valid = 1; + if ( valid == 0 || data[0] != 188 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid wif")); + result.push_back(Pair("len",(int64_t)len)); + result.push_back(Pair("prefix",(int64_t)data[0])); + return(result); + } + memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); + NSPV_logintime = (uint32_t)time(NULL); + result.push_back(Pair("result","success")); + result.push_back(Pair("status","wif will expire in 60 seconds")); + NSPV_key = DecodeSecret(wifstr); + CPubKey pubkey = NSPV_key.GetPubKey(); + CKeyID vchAddress = pubkey.GetID(); + NSPV_address = EncodeDestination(vchAddress); + result.push_back(Pair("address",NSPV_address)); + result.push_back(Pair("pubkey",HexStr(pubkey))); + strcpy(NSPV_pubkeystr,HexStr(pubkey).c_str()); + result.push_back(Pair("wifprefix",(int64_t)data[0])); + result.push_back(Pair("compressed",(int64_t)(data[len-5] == 1))); + memset(data,0,sizeof(data)); + return(result); +} + +UniValue NSPV_getinfo_json() +{ + uint8_t msg[64]; int32_t i,iters,len = 0; struct NSPV_inforesp I; + NSPV_inforesp_purge(&NSPV_inforesult); + msg[len++] = NSPV_INFO; + for (iters=0; iters<3; iters++) + { + //fprintf(stderr,"issue getinfo\n"); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i>1); + } + slen = (int32_t)strlen(coinaddr); + msg[len++] = NSPV_UTXOS; + msg[len++] = slen; + memcpy(&msg[len],coinaddr,slen), len += slen; + msg[len] = 0; + if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 ) + { + for (i=0; i= NSPV_inforesult.height ) + return(NSPV_utxosresp_json(&NSPV_utxosresult)); + } + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","no utxos result")); + return(result); +} + +UniValue NSPV_notarizations(int32_t height) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsresp N; + if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); + msg[len++] = NSPV_NTZS; + len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); + } + } + memset(&N,0,sizeof(N)); + return(NSPV_ntzs_json(&N)); +} + +UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsproofresp H; + if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + msg[len++] = NSPV_NTZSPROOF; + len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); + len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i= nextheight ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); + } + } + memset(&H,0,sizeof(H)); + return(NSPV_ntzsproof_json(&H)); +} + +UniValue NSPV_txproof(uint256 txid,int32_t height) +{ + uint8_t msg[64]; int32_t i,len = 0; struct NSPV_txproof P; + if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + return(NSPV_txproof_json(&NSPV_txproofresult)); + msg[len++] = NSPV_TXPROOF; + len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); + //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i>1) != 0 ) + { + for (i=0; i response) // received a response +{ + int32_t len; uint32_t timestamp = (uint32_t)time(NULL); + if ( (len= response.size()) > 0 ) + { + switch ( response[0] ) + { + case NSPV_INFORESP: + NSPV_inforesp_purge(&NSPV_inforesult); + NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); + //fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status + break; + case NSPV_UTXOSRESP: + NSPV_utxosresp_purge(&NSPV_utxosresult); + NSPV_rwutxosresp(0,&response[1],&NSPV_utxosresult); + fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list + break; + case NSPV_NTZSRESP: + NSPV_ntzsresp_purge(&NSPV_ntzsresult); + NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); + fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + break; + case NSPV_NTZSPROOFRESP: + NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); + NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); + fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); // update utxos[i] + break; + case NSPV_TXPROOFRESP: + NSPV_txproof_purge(&NSPV_txproofresult); + NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); + fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + break; + case NSPV_SPENTINFORESP: + + NSPV_spentinfo_purge(&NSPV_spentresult); + NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); + fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + break; + default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); + break; + } + } +} + +#endif // KOMODO_NSPVSUPERLITE_H diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h new file mode 100644 index 000000000..1eb173dd5 --- /dev/null +++ b/src/komodo_nSPV_wallet.h @@ -0,0 +1,277 @@ + +/****************************************************************************** + * Copyright © 2014-2019 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef KOMODO_NSPVWALLET_H +#define KOMODO_NSPVWALLET_H + +// nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send + +int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) +{ + char *txstr; int32_t retval = 0; + NSPV_txproof(txid,height); + if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.height != height ) + return(-1); + txstr = (char *)malloc(NSPV_txproofresult.txlen*2 + 1); + init_hexbytes_noT(txstr,NSPV_txproofresult.tx,NSPV_txproofresult.txlen); + if ( !DecodeHexTx(tx,txstr) ) + retval = -1; + else + { + // need to validate txproof + // get the notarizations bracket of height + // get hdrsproof of the prev,next + // verify the hdrs link back from next to prev + // merkle prove txproof to the merkleroot in the corresponding hdr + } + free(txstr); + return(retval); +} + +int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) +{ + int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; + abovei = belowi = -1; + for (above=below=i=0; i value ) + { + gap = (atx_value - value); + if ( above == 0 || gap < above ) + { + above = gap; + abovei = i; + } + } + else + { + gap = (value - atx_value); + if ( below == 0 || gap < below ) + { + below = gap; + belowi = i; + } + } + //printf("value %.8f gap %.8f abovei.%d %.8f belowi.%d %.8f\n",dstr(value),dstr(gap),abovei,dstr(above),belowi,dstr(below)); + } + *aboveip = abovei; + *abovep = above; + *belowip = belowi; + *belowp = below; + //printf("above.%d below.%d\n",abovei,belowi); + if ( abovei >= 0 && belowi >= 0 ) + { + if ( above < (below >> 1) ) + return(abovei); + else return(belowi); + } + else if ( abovei >= 0 ) + return(abovei); + else return(belowi); +} + +int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num) +{ + int32_t abovei,belowi,ind,vout,i,n = 0; int64_t threshold,above,below; int64_t remains,totalinputs = 0; CTransaction tx; struct NSPV_utxoresp utxos[NSPV_MAXVINS],*up; + memset(utxos,0,sizeof(utxos)); + if ( maxinputs > NSPV_MAXVINS ) + maxinputs = NSPV_MAXVINS; + if ( maxinputs > 0 ) + threshold = total/maxinputs; + else threshold = total; + for (i=0; i threshold ) + utxos[n++] = ptr[i]; + } + remains = total; + //fprintf(stderr,"n.%d for total %.8f\n",n,(double)total/COIN); + for (i=0; i0; i++) + { + below = above = 0; + abovei = belowi = -1; + if ( NSPV_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) + { + fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); + free(utxos); + return(0); + } + if ( belowi < 0 || abovei >= 0 ) + ind = abovei; + else ind = belowi; + if ( ind < 0 ) + { + fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); + free(utxos); + return(0); + } + //fprintf(stderr,"i.%d ind.%d abovei.%d belowi.%d n.%d\n",i,ind,abovei,belowi,n); + up = &utxos[ind]; + mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); + used[i] = *up; + totalinputs += up->satoshis; + remains -= up->satoshis; + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); + fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + if ( totalinputs >= total || (i+1) >= maxinputs ) + break; + } + fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); + if ( totalinputs >= total ) + return(totalinputs); + return(0); +} + +std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,struct NSPV_utxoresp used[]) +{ + auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); + CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; + n = mtx.vout.size(); + for (i=0; i= totaloutputs+2*txfee ) + { + change = totalinputs - (totaloutputs+txfee); + mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); + } + if ( opret.size() > 0 ) + mtx.vout.push_back(CTxOut(0,opret)); + fprintf(stderr,"sign %d inputs %.8f -> %d outputs %.8f change %.8f\n",(int32_t)mtx.vin.size(),(double)totalinputs/COIN,(int32_t)mtx.vout.size(),(double)totaloutputs/COIN,(double)change/COIN); + return(EncodeHexTx(mtx)); +} + +UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! +{ + UniValue result(UniValue::VOBJ); uint8_t rmd160[128]; int64_t txfee = 10000; + if ( strcmp(srcaddr,NSPV_address.c_str()) != 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid address")); + result.push_back(Pair("mismatched",srcaddr)); + return(result); + } + else if ( bitcoin_base58decode(rmd160,destaddr) != 25 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid destaddr")); + return(result); + } + if ( NSPV_inforesult.height == 0 ) + NSPV_getinfo_json(); + if ( NSPV_inforesult.height == 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt getinfo")); + return(result); + } + if ( strcmp(NSPV_utxosresult.coinaddr,srcaddr) != 0 || NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) + NSPV_addressutxos(srcaddr); + if ( strcmp(NSPV_utxosresult.coinaddr,srcaddr) != 0 || NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt get addressutxos")); + return(result); + } + if ( NSPV_utxosresult.total < satoshis+txfee ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough funds")); + result.push_back(Pair("balance",(double)NSPV_utxosresult.total/COIN)); + result.push_back(Pair("amount",(double)satoshis/COIN)); + return(result); + } + printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + std::vector data; CScript opret; std::string hex; + struct NSPV_utxoresp used[NSPV_MAXVINS]; + memset(used,0,sizeof(used)); + data.resize(20); + memcpy(&data[0],&rmd160[1],20); + if ( NSPV_addinputs(used,mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) + { + fprintf(stderr,"vout\n"); + mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); + fprintf(stderr,"signtx\n"); + hex = NSPV_signtx(mtx,txfee,opret,used); + result.push_back(Pair("result","success")); + result.push_back(Pair("hex",hex)); + // prove all the vins + return(result); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt create tx")); + return(result); + } +} + +// polling loop (really this belongs in its own file, but it is so small, it ended up here) + +void komodo_nSPV(CNode *pto) // polling loop from SendMessages +{ + uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); + if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+60 ) + { + fprintf(stderr,"scrub wif from NSPV memory\n"); + memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + memset(&NSPV_key,0,sizeof(NSPV_key)); + NSPV_logintime = 0; + } + if ( (pto->nServices & NODE_NSPV) == 0 ) + return; + if ( KOMODO_NSPV != 0 ) + { + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) + { + len = 0; + msg[len++] = NSPV_INFO; + NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); + } + } +} + +#endif // KOMODO_NSPVWALLET_H diff --git a/src/main.cpp b/src/main.cpp index 27646ec0b..d6b3e0bb9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7046,7 +7046,10 @@ void static ProcessGetData(CNode* pfrom) } } -#include "komodo_nSPV.h" +#include "komodo_nSPV.h" // shared defines, structs, serdes, purge functions +#include "komodo_nSPV_fullnode.h" // nSPV fullnode handling of the getnSPV request messages +#include "komodo_nSPV_superlite.h" // nSPV superlite client, issuing requests and handling nSPV responses +#include "komodo_nSPV_wallet.h" // nSPV_send and support functions, really all the rest is to support this bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived) { From ad616b55c0f40541a7fe12b1f140af25f11e30a5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:26:54 -1100 Subject: [PATCH 155/378] Syntax --- src/komodo_nSPV_wallet.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 1eb173dd5..7f460f716 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -114,7 +114,6 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 if ( NSPV_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) { fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); - free(utxos); return(0); } if ( belowi < 0 || abovei >= 0 ) @@ -123,7 +122,6 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 if ( ind < 0 ) { fprintf(stderr,"error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); - free(utxos); return(0); } //fprintf(stderr,"i.%d ind.%d abovei.%d belowi.%d n.%d\n",i,ind,abovei,belowi,n); @@ -232,9 +230,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al memcpy(&data[0],&rmd160[1],20); if ( NSPV_addinputs(used,mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) { - fprintf(stderr,"vout\n"); mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); - fprintf(stderr,"signtx\n"); hex = NSPV_signtx(mtx,txfee,opret,used); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); From 3f43c61f92be694647ea974709923b29bbf2c145 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:38:19 -1100 Subject: [PATCH 156/378] Dynamic keystone --- src/komodo-tx.cpp | 2 -- src/komodo_nSPV_superlite.h | 5 ----- src/komodo_nSPV_wallet.h | 14 +++++++++++++- src/script/sign.cpp | 7 +------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/komodo-tx.cpp b/src/komodo-tx.cpp index 807c4fae4..67ac68cd4 100644 --- a/src/komodo-tx.cpp +++ b/src/komodo-tx.cpp @@ -52,8 +52,6 @@ uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 return(0); } -CKey *NSPV_defaultkey() { return(0); } - static bool fCreateBlank; static std::map registers; static const int CONTINUE_EXECUTION=-1; diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index aeca64a9d..301dc9a2c 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -40,11 +40,6 @@ struct NSPV_ntzsproofresp NSPV_ntzsproofresult; struct NSPV_txproof NSPV_txproofresult; struct NSPV_utxo *NSPV_utxos; -CKey *NSPV_defaultkey() -{ - return(&NSPV_key); -} - CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 7f460f716..cd54668b2 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -142,9 +142,21 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 return(0); } +bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) +{ + CTransaction txNewConst(mtx); SignatureData sigdata; const CKeyStore &keystore; + auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); + keystore.AddKey(NSPV_key); + if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) + { + UpdateTransaction(mtx,vini,sigdata); + return(true); + } else fprintf(stderr,"signing error for SignTx vini.%d %.8f\n",vini,(double)utxovalue/COIN); + return(false); +} + std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,struct NSPV_utxoresp used[]) { - auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; n = mtx.vout.size(); for (i=0; i valtype; extern uint8_t ASSETCHAINS_TXPOW; TransactionSignatureCreator::TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn) : BaseSignatureCreator(keystoreIn), txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn) {} -CKey *NSPV_defaultkey(); bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, const CKeyID& address, const CScript& scriptCode, uint32_t consensusBranchId, CKey *pprivKey, void *extraData) const { @@ -45,11 +44,7 @@ bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, if (pprivKey) key = *pprivKey; else if (!keystore || !keystore->GetKey(address, key)) - { - if ( (pprivKey= NSPV_defaultkey()) == 0 ) - return false; - else key = *pprivKey; - } + return false; uint256 hash; try { From bf39db459a64ea6bb33ab90251a201e86d3e6752 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:40:00 -1100 Subject: [PATCH 157/378] -& --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index cd54668b2..fa5f086e3 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -144,7 +144,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { - CTransaction txNewConst(mtx); SignatureData sigdata; const CKeyStore &keystore; + CTransaction txNewConst(mtx); SignatureData sigdata; const CKeyStore keystore; auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) From 20b81c6673c60304506daa76a208e2bfb0e83b50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:41:19 -1100 Subject: [PATCH 158/378] -const --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index fa5f086e3..4a33823ce 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -144,7 +144,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { - CTransaction txNewConst(mtx); SignatureData sigdata; const CKeyStore keystore; + CTransaction txNewConst(mtx); SignatureData sigdata; CKeyStore keystore; auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) From 62e8070ea7a891e41f559900eb96d19be2333956 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:46:44 -1100 Subject: [PATCH 159/378] CBasicKeyStore --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 4a33823ce..07d499a76 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -144,7 +144,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { - CTransaction txNewConst(mtx); SignatureData sigdata; CKeyStore keystore; + CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) From 8f29da13404dc0700a9a577099c3ad71363b3060 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 03:52:24 -1100 Subject: [PATCH 160/378] 0x76b809bb --- src/komodo_nSPV_wallet.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 07d499a76..3acbe836e 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -19,6 +19,8 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send +#define NSPV_BRANCHID 0x76b809bb + int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { char *txstr; int32_t retval = 0; @@ -145,7 +147,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; - auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); + auto consensusBranchId = NSPV_BRANCHID; keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) { From 6b6c7f19068734cc6cf6956766a5145879791585 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:06:27 -1100 Subject: [PATCH 161/378] Generate sapling tx --- src/komodo_nSPV_wallet.h | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 3acbe836e..01ad91cdb 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -134,11 +134,11 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 remains -= up->satoshis; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); - fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; } - fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); + //fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); if ( totalinputs >= total ) return(totalinputs); return(0); @@ -147,9 +147,8 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; - auto consensusBranchId = NSPV_BRANCHID; keystore.AddKey(NSPV_key); - if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,consensusBranchId) != 0 ) + if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,NSPV_BRANCHID) != 0 ) { UpdateTransaction(mtx,vini,sigdata); return(true); @@ -181,7 +180,10 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st return(""); } else if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) + { fprintf(stderr,"signing error for vini.%d\n",i); + return(""); + } } else fprintf(stderr,"couldnt find txid.%s\n",mtx.vin[i].prevout.hash.GetHex().c_str()); } if ( totalinputs >= totaloutputs+2*txfee ) @@ -236,19 +238,29 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al return(result); } printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - std::vector data; CScript opret; std::string hex; - struct NSPV_utxoresp used[NSPV_MAXVINS]; + std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; + mtx.fOverwintered = true; + mtx.nExpiryHeight = nHeight + expiryDelta; + mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; + mtx.nVersion = SAPLING_TX_VERSION; memset(used,0,sizeof(used)); data.resize(20); memcpy(&data[0],&rmd160[1],20); + if ( NSPV_addinputs(used,mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) { mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); hex = NSPV_signtx(mtx,txfee,opret,used); - result.push_back(Pair("result","success")); - result.push_back(Pair("hex",hex)); - // prove all the vins + if ( hex.size() > 0 ) + { + result.push_back(Pair("result","success")); + result.push_back(Pair("hex",hex)); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","signing error")); + } return(result); } else From 78f26462ed5fa70f09bcbf366982c14c2365e44c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:07:18 -1100 Subject: [PATCH 162/378] Don't expire --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 01ad91cdb..296d000f1 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -240,7 +240,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; mtx.fOverwintered = true; - mtx.nExpiryHeight = nHeight + expiryDelta; + mtx.nExpiryHeight = 0; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; mtx.nVersion = SAPLING_TX_VERSION; memset(used,0,sizeof(used)); From 3837c804a96dcc413be14f0bb6b404f6bf7875e4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:12:10 -1100 Subject: [PATCH 163/378] +print --- src/komodo_nSPV_wallet.h | 2 +- src/script/sign.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 296d000f1..e4f853aa2 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -152,7 +152,7 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C { UpdateTransaction(mtx,vini,sigdata); return(true); - } else fprintf(stderr,"signing error for SignTx vini.%d %.8f\n",vini,(double)utxovalue/COIN); + } // else fprintf(stderr,"signing error for SignTx vini.%d %.8f\n",vini,(double)utxovalue/COIN); return(false); } diff --git a/src/script/sign.cpp b/src/script/sign.cpp index 685018828..f97e1a83d 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -44,7 +44,10 @@ bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, if (pprivKey) key = *pprivKey; else if (!keystore || !keystore->GetKey(address, key)) + { + fprintf(stderr,"couldnt find key\n"); return false; + } uint256 hash; try { From 44f805ce9e7a51f2148a6e2a3ec32d7daeb05514 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:19:03 -1100 Subject: [PATCH 164/378] +print --- src/keystore.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/keystore.cpp b/src/keystore.cpp index 34bab456c..0a79f7df7 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -71,6 +71,7 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); mapKeys[pubkey.GetID()] = key; + fprintf(stderr,"addpubkey pub %s\n",pubkey.GetHex().c_str()); return true; } From a7685f00568e841ac643475e508fb21aba5565f5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:22:26 -1100 Subject: [PATCH 165/378] +print --- src/keystore.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/keystore.cpp b/src/keystore.cpp index 0a79f7df7..8b794f2ce 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -71,7 +71,12 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); mapKeys[pubkey.GetID()] = key; - fprintf(stderr,"addpubkey pub %s\n",pubkey.GetHex().c_str()); + { + int32_t i; + for (i=0; i<33; i++) + fprintf(stderr,"%02x",((uint8_t *)&pubkey)[i]); + fprintf(stderr," addpubkey pub\n"); + } return true; } From 9535f80ef9d64c4f56c84b7911ec0ccf6f631465 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:27:45 -1100 Subject: [PATCH 166/378] +print --- src/keystore.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/keystore.cpp b/src/keystore.cpp index 8b794f2ce..9da5a1e24 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -35,6 +35,7 @@ bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const } bool CKeyStore::AddKey(const CKey &key) { + fprintf(stderr,"addkey\n"); return AddKeyPubKey(key, key.GetPubKey()); } From 897981e00f1dd5830df9a9cc3e2feecb99235c5a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:33:02 -1100 Subject: [PATCH 167/378] +print --- src/komodo_nSPV_wallet.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index e4f853aa2..456d9e781 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -147,6 +147,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; + fprintf("call Addkey\n"); keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,NSPV_BRANCHID) != 0 ) { From b12183920c398125f952fd81328f2bcbc24b0c36 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:33:54 -1100 Subject: [PATCH 168/378] +stderr --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 456d9e781..7381b1777 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -147,7 +147,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; - fprintf("call Addkey\n"); + fprintf(stderr,"call Addkey\n"); keystore.AddKey(NSPV_key); if ( ProduceSignature(TransactionSignatureCreator(&keystore,&txNewConst,vini,utxovalue,SIGHASH_ALL),scriptPubKey,sigdata,NSPV_BRANCHID) != 0 ) { From 029a1865374b8cc5df7534da687ee67a5ec687f0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:37:22 -1100 Subject: [PATCH 169/378] Use NSPV_SignTx --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 7381b1777..11d894a12 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -180,7 +180,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st fprintf(stderr,"vintx vout mismatch %d != %d\n",utxovout,used[i].vout); return(""); } - else if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) + else if ( NSPV_SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) { fprintf(stderr,"signing error for vini.%d\n",i); return(""); From 81d0e44732ce24a996c25437eb5023a8a4048ab2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:58:30 -1100 Subject: [PATCH 170/378] Guards for nSPV in decode/send raw transaction --- src/keystore.cpp | 7 ------- src/rpc/rawtransaction.cpp | 40 ++++++++++++++++++++------------------ src/script/sign.cpp | 3 --- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/keystore.cpp b/src/keystore.cpp index 9da5a1e24..34bab456c 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -35,7 +35,6 @@ bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const } bool CKeyStore::AddKey(const CKey &key) { - fprintf(stderr,"addkey\n"); return AddKeyPubKey(key, key.GetPubKey()); } @@ -72,12 +71,6 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); mapKeys[pubkey.GetID()] = key; - { - int32_t i; - for (i=0; i<33; i++) - fprintf(stderr,"%02x",((uint8_t *)&pubkey)[i]); - fprintf(stderr," addpubkey pub\n"); - } return true; } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 7d490dff2..373eaae77 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -368,7 +368,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) entry.push_back(Pair("vin", vin)); UniValue vout(UniValue::VARR); BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock()); - CBlockIndex *tipindex,*pindex = it->second; + CBlockIndex *tipindex;//,*pindex = it->second; uint64_t interest; for (unsigned int i = 0; i < tx.vout.size(); i++) { const CTxOut& txout = tx.vout[i]; @@ -1317,27 +1317,29 @@ UniValue sendrawtransaction(const UniValue& params, bool fHelp) bool fOverrideFees = false; if (params.size() > 1) fOverrideFees = params[1].get_bool(); - - CCoinsViewCache &view = *pcoinsTip; - const CCoins* existingCoins = view.AccessCoins(hashTx); - bool fHaveMempool = mempool.exists(hashTx); - bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000; - if (!fHaveMempool && !fHaveChain) { - // push to local node and sync with wallets - CValidationState state; - bool fMissingInputs; - if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)) { - if (state.IsInvalid()) { - throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason())); - } else { - if (fMissingInputs) { - throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs"); + if ( KOMODO_NSPV == 0 ) + { + CCoinsViewCache &view = *pcoinsTip; + const CCoins* existingCoins = view.AccessCoins(hashTx); + bool fHaveMempool = mempool.exists(hashTx); + bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000; + if (!fHaveMempool && !fHaveChain) { + // push to local node and sync with wallets + CValidationState state; + bool fMissingInputs; + if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)) { + if (state.IsInvalid()) { + throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason())); + } else { + if (fMissingInputs) { + throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs"); + } + throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason()); } - throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason()); } + } else if (fHaveChain) { + throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain"); } - } else if (fHaveChain) { - throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain"); } RelayTransaction(tx); diff --git a/src/script/sign.cpp b/src/script/sign.cpp index f97e1a83d..685018828 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -44,10 +44,7 @@ bool TransactionSignatureCreator::CreateSig(std::vector& vchSig, if (pprivKey) key = *pprivKey; else if (!keystore || !keystore->GetKey(address, key)) - { - fprintf(stderr,"couldnt find key\n"); return false; - } uint256 hash; try { From e0489802a5ac8ad9223c8f73ff4c4d702c02d6aa Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 04:59:44 -1100 Subject: [PATCH 171/378] -pindex --- src/rpc/rawtransaction.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 373eaae77..d60f0c52e 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -374,7 +374,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) const CTxOut& txout = tx.vout[i]; UniValue out(UniValue::VOBJ); out.push_back(Pair("value", ValueFromAmount(txout.nValue))); - if ( ASSETCHAINS_SYMBOL[0] == 0 && pindex != 0 && tx.nLockTime >= 500000000 && (tipindex= chainActive.LastTip()) != 0 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 && tx.nLockTime >= 500000000 && (tipindex= chainActive.LastTip()) != 0 ) { int64_t interest; int32_t txheight; uint32_t locktime; interest = komodo_accrued_interest(&txheight,&locktime,tx.GetHash(),i,0,txout.nValue,(int32_t)tipindex->GetHeight()); From f3f63fea39cff2a841dca641cff8d2f956858f44 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 05:15:34 -1100 Subject: [PATCH 172/378] +print --- src/komodo_nSPV_wallet.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 11d894a12..faba32eeb 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -147,8 +147,13 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey) { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; - fprintf(stderr,"call Addkey\n"); keystore.AddKey(NSPV_key); + { + int32_t i; + for (i=0; i Date: Fri, 5 Jul 2019 05:17:32 -1100 Subject: [PATCH 173/378] & --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index faba32eeb..570ca7217 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -151,7 +151,7 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C { int32_t i; for (i=0; i Date: Fri, 5 Jul 2019 05:20:35 -1100 Subject: [PATCH 174/378] +4 --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 570ca7217..65140fe54 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -150,7 +150,7 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C keystore.AddKey(NSPV_key); { int32_t i; - for (i=0; i Date: Fri, 5 Jul 2019 05:30:14 -1100 Subject: [PATCH 175/378] Finalize mtx before signing --- src/komodo_nSPV_wallet.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 65140fe54..bfdab6428 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -33,6 +33,7 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) retval = -1; else { + printf("got tx.(%s)\n",txstr); // need to validate txproof // get the notarizations bracket of height // get hdrsproof of the prev,next @@ -168,13 +169,21 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st n = mtx.vout.size(); for (i=0; i= totaloutputs+2*txfee ) + { + change = totalinputs - (totaloutputs+txfee); + mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); + } + if ( opret.size() > 0 ) + mtx.vout.push_back(CTxOut(0,opret)); for (i=0; i= totaloutputs+2*txfee ) - { - change = totalinputs - (totaloutputs+txfee); - mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); - } - if ( opret.size() > 0 ) - mtx.vout.push_back(CTxOut(0,opret)); fprintf(stderr,"sign %d inputs %.8f -> %d outputs %.8f change %.8f\n",(int32_t)mtx.vin.size(),(double)totalinputs/COIN,(int32_t)mtx.vout.size(),(double)totaloutputs/COIN,(double)change/COIN); return(EncodeHexTx(mtx)); } From 42cafb57375a35e208f0d77ce0d2d3f05d12d2af Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 05:34:59 -1100 Subject: [PATCH 176/378] Fixes --- src/komodo_nSPV_wallet.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index bfdab6428..80b02d619 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -18,6 +18,7 @@ #define KOMODO_NSPVWALLET_H // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send +// interest calculations are currently just using what is returned, it should calculate it from scratch #define NSPV_BRANCHID 0x76b809bb @@ -33,7 +34,7 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) retval = -1; else { - printf("got tx.(%s)\n",txstr); + //printf("got tx.(%s)\n",txstr); // need to validate txproof // get the notarizations bracket of height // get hdrsproof of the prev,next @@ -165,17 +166,20 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,struct NSPV_utxoresp used[]) { - CTransaction vintx; std::string hex; uint256 hashBlock; int64_t change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; + CTransaction vintx; std::string hex; uint256 hashBlock; int64_t interest=0,change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; n = mtx.vout.size(); for (i=0; i= totaloutputs+2*txfee ) { - change = totalinputs - (totaloutputs+txfee); + totalinputs += used[i].satoshis; + interest += used[i].extradata; + } + //PrecomputedTransactionData txdata(mtx); + if ( (totalinputs+interest) >= totaloutputs+2*txfee ) + { + change = (totalinputs+interest) - (totaloutputs+txfee); mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(NSPV_pubkeystr) << OP_CHECKSIG)); } if ( opret.size() > 0 ) @@ -201,7 +205,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st } } else fprintf(stderr,"couldnt find txid.%s\n",mtx.vin[i].prevout.hash.GetHex().c_str()); } - fprintf(stderr,"sign %d inputs %.8f -> %d outputs %.8f change %.8f\n",(int32_t)mtx.vin.size(),(double)totalinputs/COIN,(int32_t)mtx.vout.size(),(double)totaloutputs/COIN,(double)change/COIN); + fprintf(stderr,"sign %d inputs %.8f + interest %.8f -> %d outputs %.8f change %.8f\n",(int32_t)mtx.vin.size(),(double)totalinputs/COIN,(double)interest/COIN,(int32_t)mtx.vout.size(),(double)totaloutputs/COIN,(double)change/COIN); return(EncodeHexTx(mtx)); } From 1c4447716b49ef48c54b335f83e4ad01a87e4b88 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 05:37:47 -1100 Subject: [PATCH 177/378] utxovout = mtx.vin[i].prevout.n; --- src/komodo_nSPV_wallet.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 80b02d619..07e5580fb 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -188,6 +188,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st { if ( NSPV_gettransaction(mtx.vin[i].prevout.hash,used[i].height,vintx) == 0 ) { + utxovout = mtx.vin[i].prevout.n; if ( vintx.vout[utxovout].nValue != used[i].satoshis ) { fprintf(stderr,"vintx mismatch %.8f != %.8f\n",(double)vintx.vout[utxovout].nValue/COIN,(double)used[i].satoshis/COIN); From d2ce4d8277d29caeeeacd0c93563e5a0cdce0f8b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 05:49:28 -1100 Subject: [PATCH 178/378] Add calls to get notarisation bracket and hdrsproof --- src/komodo_nSPV_wallet.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 07e5580fb..59a4acec0 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -36,9 +36,8 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { //printf("got tx.(%s)\n",txstr); // need to validate txproof - // get the notarizations bracket of height - // get hdrsproof of the prev,next - // verify the hdrs link back from next to prev + NSPV_notarizations(height); // gets the prev and next notarizations + NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment // merkle prove txproof to the merkleroot in the corresponding hdr } free(txstr); @@ -150,6 +149,7 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C { CTransaction txNewConst(mtx); SignatureData sigdata; CBasicKeyStore keystore; keystore.AddKey(NSPV_key); + if ( 0 ) { int32_t i; for (i=0; i Date: Fri, 5 Jul 2019 05:58:04 -1100 Subject: [PATCH 179/378] NSPV_AUTOLOGOUT --- src/komodo_nSPV_wallet.h | 10 ++++++++-- src/wallet/rpcdump.cpp | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 59a4acec0..cfe307eac 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -19,7 +19,7 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send // interest calculations are currently just using what is returned, it should calculate it from scratch - +#define NSPV_AUTOLOGOUT 60 #define NSPV_BRANCHID 0x76b809bb int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) @@ -213,6 +213,12 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! { UniValue result(UniValue::VOBJ); uint8_t rmd160[128]; int64_t txfee = 10000; + if ( NSPV_logintime == 0 || time(NULL) > NSPV_logintime+NSPV_AUTOLOGOUT ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","wif expired")); + return(result); + } if ( strcmp(srcaddr,NSPV_address.c_str()) != 0 ) { result.push_back(Pair("result","error")); @@ -289,7 +295,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al void komodo_nSPV(CNode *pto) // polling loop from SendMessages { uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); - if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+60 ) + if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+NSPV_AUTOLOGOUT ) { fprintf(stderr,"scrub wif from NSPV memory\n"); memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index b13a2b6c9..97b458070 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -999,10 +999,10 @@ UniValue nspv_login(const UniValue& params, bool fHelp) UniValue nspv_listunspent(const UniValue& params, bool fHelp) { + if ( fHelp || params.size() > 1 ) + throw runtime_error("nspv_listunspent address\n"); if ( params.size() == 0 && NSPV_address.size() != 0 ) return(NSPV_addressutxos((char *)NSPV_address.c_str())); - if ( fHelp || params.size() != 1 ) - throw runtime_error("nspv_listunspent address\n"); return(NSPV_addressutxos((char *)params[0].get_str().c_str())); } From 07fe2a3a783718f3687bce0b789b0ef36d8e0340 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:11:32 -1100 Subject: [PATCH 180/378] Bypass all the other messages starting from inv --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index d6b3e0bb9..098396533 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7450,7 +7450,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, komodo_nSPVresp(pfrom,payload); return(true); } - + else if ( KOMODO_NSPV != 0 ) + return(true); else if (strCommand == "inv") { vector vInv; From 4bfb81956b1d93a6dc227ad550fcb65e9757ebbb Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:22:40 -1100 Subject: [PATCH 181/378] +print --- src/net.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index c4c5fbc03..f3f763694 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1897,6 +1897,7 @@ void RelayTransaction(const CTransaction& tx) void RelayTransaction(const CTransaction& tx, const CDataStream& ss) { + fprintf(stderr,"RelayTransaction\n"); CInv inv(MSG_TX, tx.GetHash()); { LOCK(cs_mapRelay); @@ -1914,15 +1915,16 @@ void RelayTransaction(const CTransaction& tx, const CDataStream& ss) LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { + fprintf(stderr,"pnode relay tx.%d\n",(int32_t)pnode->fRelayTxes); if(!pnode->fRelayTxes) continue; + fprintf(stderr,"found pnode\n"); LOCK(pnode->cs_filter); if (pnode->pfilter) { if (pnode->pfilter->IsRelevantAndUpdate(tx)) pnode->PushInventory(inv); - } else - pnode->PushInventory(inv); + } else pnode->PushInventory(inv); } } From 21e87ec48fe629f6b44050c43927cac4aa6a102d Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:26:01 -1100 Subject: [PATCH 182/378] +print --- src/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 098396533..23f0acbce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7636,6 +7636,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, else if (strCommand == "tx") { + fprintf(stderr,"got tx message\n"); if (IsInitialBlockDownload()) return true; @@ -7657,6 +7658,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs)) { + fprintf(stderr,"process new tx\n"); mempool.check(pcoinsTip); RelayTransaction(tx); vWorkQueue.push_back(inv.hash); From 7ebd4457680389f3e6af909da5f913352afaa3d4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:33:15 -1100 Subject: [PATCH 183/378] -print --- src/komodo_nSPV_fullnode.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index f493512a6..6982e619e 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -67,8 +67,8 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des txids[0] = nota.first; txidhts[0] = txidht; desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); - if ( height != 2668 ) - fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); + //if ( height != 2668 ) + // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller { txids[1] = txids[0]; From 7c6f471173acfe851e4ac7c60636b9d29687f8fc Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:57:09 -1100 Subject: [PATCH 184/378] Allow "inv" to be sent from superlite client --- src/main.cpp | 57 +++++++++++++++++++------------------- src/net.cpp | 3 -- src/rpc/rawtransaction.cpp | 1 - 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 23f0acbce..5407c9e91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8243,35 +8243,35 @@ bool SendMessages(CNode* pto, bool fSendTrickle) state.fShouldBan = false; } komodo_nSPV(pto); - if ( KOMODO_NSPV != 0 ) - return(true); - BOOST_FOREACH(const CBlockReject& reject, state.rejects) - pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); - state.rejects.clear(); - - // Start block sync - if (pindexBestHeader == NULL) - pindexBestHeader = chainActive.Tip(); - bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. - if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { - // Only actively request headers from a single peer, unless we're close to today. - if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { - state.fSyncStarted = true; - nSyncStarted++; - CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader; - LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->GetHeight(), pto->id, pto->nStartingHeight); - pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256()); + if ( KOMODO_NSPV == 0 ) + { + BOOST_FOREACH(const CBlockReject& reject, state.rejects) + pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); + state.rejects.clear(); + + // Start block sync + if (pindexBestHeader == NULL) + pindexBestHeader = chainActive.Tip(); + bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. + if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { + // Only actively request headers from a single peer, unless we're close to today. + if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { + state.fSyncStarted = true; + nSyncStarted++; + CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader; + LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->GetHeight(), pto->id, pto->nStartingHeight); + pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256()); + } + } + + // Resend wallet transactions that haven't gotten in a block yet + // Except during reindex, importing and IBD, when old wallet + // transactions become unconfirmed and spams other nodes. + if (!fReindex && !fImporting && !IsInitialBlockDownload()) + { + GetMainSignals().Broadcast(nTimeBestReceived); } } - - // Resend wallet transactions that haven't gotten in a block yet - // Except during reindex, importing and IBD, when old wallet - // transactions become unconfirmed and spams other nodes. - if (!fReindex && !fImporting && !IsInitialBlockDownload()) - { - GetMainSignals().Broadcast(nTimeBestReceived); - } - // // Message: inventory // @@ -8319,7 +8319,8 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } if (!vInv.empty()) pto->PushMessage("inv", vInv); - + if ( KOMODO_NSPV != 0 ) + return(true); // Detect whether we're stalling int64_t nNow = GetTimeMicros(); if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) { diff --git a/src/net.cpp b/src/net.cpp index f3f763694..0586d7124 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1897,7 +1897,6 @@ void RelayTransaction(const CTransaction& tx) void RelayTransaction(const CTransaction& tx, const CDataStream& ss) { - fprintf(stderr,"RelayTransaction\n"); CInv inv(MSG_TX, tx.GetHash()); { LOCK(cs_mapRelay); @@ -1915,10 +1914,8 @@ void RelayTransaction(const CTransaction& tx, const CDataStream& ss) LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { - fprintf(stderr,"pnode relay tx.%d\n",(int32_t)pnode->fRelayTxes); if(!pnode->fRelayTxes) continue; - fprintf(stderr,"found pnode\n"); LOCK(pnode->cs_filter); if (pnode->pfilter) { diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index d60f0c52e..09247d311 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -1342,7 +1342,6 @@ UniValue sendrawtransaction(const UniValue& params, bool fHelp) } } RelayTransaction(tx); - return hashTx.GetHex(); } From 329e181592023a077358599ba340ef94290497f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 06:59:01 -1100 Subject: [PATCH 185/378] Declare fFetch --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 5407c9e91..9544c967a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8243,6 +8243,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) state.fShouldBan = false; } komodo_nSPV(pto); + bool fFetch = false; if ( KOMODO_NSPV == 0 ) { BOOST_FOREACH(const CBlockReject& reject, state.rejects) @@ -8252,7 +8253,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) // Start block sync if (pindexBestHeader == NULL) pindexBestHeader = chainActive.Tip(); - bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. + fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { // Only actively request headers from a single peer, unless we're close to today. if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { From 4ae6f232a8ae6aacc92edf8211a847b18bb4b55f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 07:00:18 -1100 Subject: [PATCH 186/378] -print --- src/main.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9544c967a..6b37fe32b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7636,7 +7636,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, else if (strCommand == "tx") { - fprintf(stderr,"got tx message\n"); if (IsInitialBlockDownload()) return true; @@ -7658,7 +7657,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs)) { - fprintf(stderr,"process new tx\n"); mempool.check(pcoinsTip); RelayTransaction(tx); vWorkQueue.push_back(inv.hash); From 4fd98d4af3860eb4ed7d717f5ea6af63811ab326 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 07:02:39 -1100 Subject: [PATCH 187/378] +print --- src/main.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 6b37fe32b..b0e4092cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8317,7 +8317,10 @@ bool SendMessages(CNode* pto, bool fSendTrickle) pto->vInventoryToSend = vInvWait; } if (!vInv.empty()) + { + fprintf(stderr,"send inv message\n"); pto->PushMessage("inv", vInv); + } if ( KOMODO_NSPV != 0 ) return(true); // Detect whether we're stalling From ad842844d5009d7ceca7af02814664273f48cc89 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 07:07:01 -1100 Subject: [PATCH 188/378] Revert --- src/main.cpp | 61 ++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b0e4092cb..098396533 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8241,36 +8241,35 @@ bool SendMessages(CNode* pto, bool fSendTrickle) state.fShouldBan = false; } komodo_nSPV(pto); - bool fFetch = false; - if ( KOMODO_NSPV == 0 ) - { - BOOST_FOREACH(const CBlockReject& reject, state.rejects) - pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); - state.rejects.clear(); - - // Start block sync - if (pindexBestHeader == NULL) - pindexBestHeader = chainActive.Tip(); - fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. - if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { - // Only actively request headers from a single peer, unless we're close to today. - if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { - state.fSyncStarted = true; - nSyncStarted++; - CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader; - LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->GetHeight(), pto->id, pto->nStartingHeight); - pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256()); - } - } - - // Resend wallet transactions that haven't gotten in a block yet - // Except during reindex, importing and IBD, when old wallet - // transactions become unconfirmed and spams other nodes. - if (!fReindex && !fImporting && !IsInitialBlockDownload()) - { - GetMainSignals().Broadcast(nTimeBestReceived); + if ( KOMODO_NSPV != 0 ) + return(true); + BOOST_FOREACH(const CBlockReject& reject, state.rejects) + pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); + state.rejects.clear(); + + // Start block sync + if (pindexBestHeader == NULL) + pindexBestHeader = chainActive.Tip(); + bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do. + if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && pindexBestHeader!=0) { + // Only actively request headers from a single peer, unless we're close to today. + if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { + state.fSyncStarted = true; + nSyncStarted++; + CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader; + LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->GetHeight(), pto->id, pto->nStartingHeight); + pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256()); } } + + // Resend wallet transactions that haven't gotten in a block yet + // Except during reindex, importing and IBD, when old wallet + // transactions become unconfirmed and spams other nodes. + if (!fReindex && !fImporting && !IsInitialBlockDownload()) + { + GetMainSignals().Broadcast(nTimeBestReceived); + } + // // Message: inventory // @@ -8317,12 +8316,8 @@ bool SendMessages(CNode* pto, bool fSendTrickle) pto->vInventoryToSend = vInvWait; } if (!vInv.empty()) - { - fprintf(stderr,"send inv message\n"); pto->PushMessage("inv", vInv); - } - if ( KOMODO_NSPV != 0 ) - return(true); + // Detect whether we're stalling int64_t nNow = GetTimeMicros(); if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) { From f542be0f7c461263ce9aba023a09ea76b74738b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 07:13:08 -1100 Subject: [PATCH 189/378] +fields --- src/komodo_nSPV_wallet.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index cfe307eac..3f4a4ec5b 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -245,6 +245,10 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al if ( strcmp(NSPV_utxosresult.coinaddr,srcaddr) != 0 || NSPV_utxosresult.nodeheight < NSPV_inforesult.height ) { result.push_back(Pair("result","error")); + result.push_back(Pair("address",NSPV_utxosresult.coinaddr)); + result.push_back(Pair("srcaddr",srcaddr.coinaddr)); + result.push_back(Pair("nodeheight",(int64_t)NSPV_utxosresult.nodeheight)); + result.push_back(Pair("infoheight",(int64_t)NSPV_inforesult.height)); result.push_back(Pair("error","couldnt get addressutxos")); return(result); } From 754c912c2fe901491ffe2fee28cd254f07c938f2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 19:56:07 -1100 Subject: [PATCH 190/378] Syntax --- src/komodo_nSPV_superlite.h | 3 +++ src/komodo_nSPV_wallet.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 301dc9a2c..982e751cf 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -21,6 +21,9 @@ // no caching, no optimizations, no reducing the number of ntzsproofs needed by detecting overlaps, etc. // advantage is that it is simpler to implement and understand to create a design for a more performant version +// interest calculations are currently just using what is returned, it should calculate it from scratch +// need to validate incoming data and update only if it is valid and more recent + #define NSPV_POLLITERS 15 #define NSPV_POLLMICROS 100000 #define NSPV_MAXVINS 64 diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 3f4a4ec5b..bada2cb33 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -18,7 +18,7 @@ #define KOMODO_NSPVWALLET_H // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send -// interest calculations are currently just using what is returned, it should calculate it from scratch + #define NSPV_AUTOLOGOUT 60 #define NSPV_BRANCHID 0x76b809bb @@ -246,7 +246,7 @@ UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its al { result.push_back(Pair("result","error")); result.push_back(Pair("address",NSPV_utxosresult.coinaddr)); - result.push_back(Pair("srcaddr",srcaddr.coinaddr)); + result.push_back(Pair("srcaddr",srcaddr)); result.push_back(Pair("nodeheight",(int64_t)NSPV_utxosresult.nodeheight)); result.push_back(Pair("infoheight",(int64_t)NSPV_inforesult.height)); result.push_back(Pair("error","couldnt get addressutxos")); From ffd75096928798aaeab681d05352cb45209f181b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 22:13:06 -1100 Subject: [PATCH 191/378] nspv_send -> spend --- src/komodo_nSPV_wallet.h | 4 ++-- src/rpc/server.cpp | 2 +- src/rpc/server.h | 3 ++- src/wallet/rpcdump.cpp | 8 ++++---- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index bada2cb33..8fc0b3ce4 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -17,7 +17,7 @@ #ifndef KOMODO_NSPVWALLET_H #define KOMODO_NSPVWALLET_H -// nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_send +// nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend #define NSPV_AUTOLOGOUT 60 #define NSPV_BRANCHID 0x76b809bb @@ -210,7 +210,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st return(EncodeHexTx(mtx)); } -UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! +UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its all about! { UniValue result(UniValue::VOBJ); uint8_t rmd160[128]; int64_t txfee = 10000; if ( NSPV_logintime == 0 || time(NULL) > NSPV_logintime+NSPV_AUTOLOGOUT ) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index c6a51d568..1d5cb83f1 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -424,7 +424,7 @@ static const CRPCCommand vRPCCommands[] = { "nSPV", "nspv_notarizations", &nspv_notarizations, true }, { "nSPV", "nspv_hdrsproof", &nspv_hdrsproof, true }, { "nSPV", "nspv_txproof", &nspv_txproof, true }, - { "nSPV", "nspv_send", &nspv_send, true }, + { "nSPV", "nspv_spend", &nspv_spend, true }, // rewards { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index 37cb3d26a..31da0e32f 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -462,7 +462,8 @@ extern UniValue nspv_spentinfo(const UniValue& params, bool fHelp); extern UniValue nspv_notarizations(const UniValue& params, bool fHelp); extern UniValue nspv_hdrsproof(const UniValue& params, bool fHelp); extern UniValue nspv_txproof(const UniValue& params, bool fHelp); -extern UniValue nspv_send(const UniValue& params, bool fHelp); +extern UniValue nspv_spend(const UniValue& params, bool fHelp); +extern UniValue nspv_broadcast(const UniValue& params, bool fHelp); extern UniValue getblocksubsidy(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 97b458070..c35b54965 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -975,7 +975,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); -UniValue NSPV_send(char *srcaddr,char *destaddr,int64_t satoshis); +UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); UniValue NSPV_notarizations(int32_t height); UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight); @@ -1045,16 +1045,16 @@ UniValue nspv_txproof(const UniValue& params, bool fHelp) return(NSPV_txproof(txid,height)); } -UniValue nspv_send(const UniValue& params, bool fHelp) +UniValue nspv_spend(const UniValue& params, bool fHelp) { uint64_t satoshis; if ( fHelp || params.size() != 2 ) - throw runtime_error("nspv_send destaddr amount\n"); + throw runtime_error("nspv_spend destaddr amount\n"); if ( NSPV_address.size() == 0 ) throw runtime_error("to nspv_send you need an active nspv_login\n"); satoshis = atof(params[1].get_str().c_str())*COIN + 0.0000000049; if ( satoshis < 1000 ) throw runtime_error("amount too small\n"); - return(NSPV_send((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); + return(NSPV_spend((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); } From cf4b5323fdab1f82c21a7cf52b962d94f416a332 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:26:01 -1100 Subject: [PATCH 192/378] Add nspv_broadcast, to all parts: rpc, superlite, full node A good example of how to add a new nSPV message, with corresponding rpc support --- src/komodo_nSPV.h | 26 +++++++++++++++++-- src/komodo_nSPV_fullnode.h | 47 ++++++++++++++++++++++++++++++++- src/komodo_nSPV_superlite.h | 52 ++++++++++++++++++++++++++++++++----- src/rpc/server.cpp | 1 + src/wallet/rpcdump.cpp | 7 +++++ 5 files changed, 123 insertions(+), 10 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 91c6ba7f4..fd90df5f4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -37,6 +37,8 @@ #define NSPV_TXPROOFRESP 0x09 #define NSPV_SPENTINFO 0x0a #define NSPV_SPENTINFORESP 0x0b +#define NSPV_BROADCAST 0x0c +#define NSPV_BROADCASTRESP 0x0d int32_t iguana_rwbuf(int32_t rwflag,uint8_t *serialized,uint16_t len,uint8_t *buf) { @@ -254,7 +256,7 @@ void NSPV_txproof_purge(struct NSPV_txproof *ptr) } } -struct NSPV_utxo +/*struct NSPV_utxo { struct NSPV_txproof T; int64_t satoshis,extradata; @@ -272,7 +274,7 @@ int32_t NSPV_rwutxo(int32_t rwflag,uint8_t *serialized,struct NSPV_utxo *ptr) len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); return(len); -} +}*/ struct NSPV_ntzproofshared { @@ -361,4 +363,24 @@ void NSPV_spentinfo_purge(struct NSPV_spentinfo *ptr) } } +struct NSPV_broadcastresp +{ + uint256 txid; + int32_t retcode; +}; + +int32_t NSPV_rwbroadcastresp(int32_t rwflag,uint8_t *serialized,struct NSPV_broadcastresp *ptr) +{ + int32_t len = 0; + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->retcode),&ptr->retcode); + return(len); +} + +void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) +{ + if ( ptr != 0 ) + memset(ptr,0,sizeof(*ptr)); +} + #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 6982e619e..49259f13f 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -195,6 +195,26 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) return(rawtx); } +int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *tx,int32_t n) +{ + CTransaction tx; std::string rawtx; + ptr->retcode = 0; + rawtx.resize(n*2+1); + init_hexbytes_noT(rawtx.data(),tx,n); + fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); + if ( DecodeHexTx(tx,rawtx) != 0 ) + { + ptr->txid = tx.GetHash(); + if ( myAddtomempool(tx) != 0 ) + { + fprintf(stderr,"relay transaction %s\n",ptr->txid.GetHex().c_str()); + RelayTransaction(tx); + ptr->retcode = 1; + } else ptr->retcode = 0; + } else ptr->retcode = -1; + return(sizeof(*ptr)); +} + int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) { int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; @@ -458,7 +478,32 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req } } } - } + else if ( request[0] == NSPV_BROADCAST ) + { + if ( timestamp > pfrom->prevtimes[ind] ) + { + struct NSPV_broadcastresp B; uint16_t n,offset; uint256 txid; + if ( len > 1+sizeof(txid)+sizeof(n) ) + { + iguana_rwbignum(0,&request[1],sizeof(txid),(uint8_t *)&txid); + iguana_rwnum(0,&request[1+sizeof(txid)],sizeof(n),&n); + memset(&B,0,sizeof(B)); + offset = 1 + sizeof(txid) + sizeof(n); + if ( request.size() == offset+n && (slen= NSPV_sendrawtransaction(&B,&request[offset],n)) > 0 ) + { + response.resize(1 + slen); + response[0] = NSPV_BROADCASTRESP; + if ( NSPV_rwbroadcastresp(1,&response[1],&B) == slen ) + { + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; + } + NSPV_broadcast_purge(&S); + } + } + } + } + } } #endif // KOMODO_NSPVFULLNODE_H diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 982e751cf..482a0f330 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -41,7 +41,7 @@ struct NSPV_spentinfo NSPV_spentresult; struct NSPV_ntzsresp NSPV_ntzsresult; struct NSPV_ntzsproofresp NSPV_ntzsproofresult; struct NSPV_txproof NSPV_txproofresult; -struct NSPV_utxo *NSPV_utxos; +struct NSPV_broadcastresp NSPV_broadcastresult; CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { @@ -192,6 +192,15 @@ UniValue NSPV_ntzsproof_json(struct NSPV_ntzsproofresp *ptr) return(result); } +UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr) +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("retcode",(int64_t)ptr->retcode)); + return(result); +} + UniValue NSPV_login(char *wifstr) { UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; @@ -364,6 +373,31 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) return(NSPV_spentinfo_json(&I)); } +UniValue NSPV_broadcast(char *hex) +{ + uint8_t msg[64],*tx; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresult B; + n = (int32_t)strlen(hex) >> 1; + tx = malloc(n); + decode_hex(tx,n,hex); + _txid = bits256_doublesha256(0,tx,n); + memcpy(&txid,_txid,sizeof(txid)); + msg[len++] = NSPV_BROADCAST; + len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); + len += iguana_rwnum(1,&msg[len],sizeof(n),&n); + memcpy(&msg[len],tx,n), len += n; + if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + { + for (i=0; i response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); @@ -384,25 +418,29 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_NTZSRESP: NSPV_ntzsresp_purge(&NSPV_ntzsresult); NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); - fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); break; case NSPV_NTZSPROOFRESP: NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); - fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); // update utxos[i] + fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); break; case NSPV_TXPROOFRESP: NSPV_txproof_purge(&NSPV_txproofresult); NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); - fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); break; case NSPV_SPENTINFORESP: - NSPV_spentinfo_purge(&NSPV_spentresult); NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); - fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos[i] + fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); break; - default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); + case NSPV_BROADCASTRESP: + NSPV_broadcast_purge(&NSPV_broadcastresult); + NSPV_rwbroadcast(0,&response[1],&NSPV_broadcastresult); + fprintf(stderr,"got broadcast response %u size.%d\n",timestamp,(int32_t)response.size()); + break; + default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); break; } } diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 1d5cb83f1..36e4a8fda 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -425,6 +425,7 @@ static const CRPCCommand vRPCCommands[] = { "nSPV", "nspv_hdrsproof", &nspv_hdrsproof, true }, { "nSPV", "nspv_txproof", &nspv_txproof, true }, { "nSPV", "nspv_spend", &nspv_spend, true }, + { "nSPV", "nspv_broadcast", &nspv_broadcast, true }, // rewards { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index c35b54965..225d32c80 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -975,6 +975,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); UniValue NSPV_addressutxos(char *coinaddr); +UniValue NSPV_broadcast(char *hex); UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); UniValue NSPV_notarizations(int32_t height); @@ -1058,3 +1059,9 @@ UniValue nspv_spend(const UniValue& params, bool fHelp) return(NSPV_spend((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); } +UniValue nspv_broadcast(const UniValue& params, bool fHelp) +{ + if ( fHelp || params.size() != 1 ) + throw runtime_error("nspv_broadcast hex\n"); + return(NSPV_broadcast((char *)params[0].get_str().c_str())); +} From b9f914a323712a46669699501ec7d04f47870c27 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:29:44 -1100 Subject: [PATCH 193/378] Syntax --- src/komodo_nSPV_fullnode.h | 6 +++--- src/komodo_nSPV_superlite.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 49259f13f..af6c5d3f1 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -195,12 +195,12 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) return(rawtx); } -int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *tx,int32_t n) +int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int32_t n) { CTransaction tx; std::string rawtx; ptr->retcode = 0; rawtx.resize(n*2+1); - init_hexbytes_noT(rawtx.data(),tx,n); + init_hexbytes_noT((char *)rawtx.data(),data,n); fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); if ( DecodeHexTx(tx,rawtx) != 0 ) { @@ -498,7 +498,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req pfrom->PushMessage("nSPV",response); pfrom->prevtimes[ind] = timestamp; } - NSPV_broadcast_purge(&S); + NSPV_broadcast_purge(&B); } } } diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 482a0f330..3ed2e2f8f 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -375,7 +375,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_broadcast(char *hex) { - uint8_t msg[64],*tx; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresult B; + uint8_t msg[64],*tx; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; n = (int32_t)strlen(hex) >> 1; tx = malloc(n); decode_hex(tx,n,hex); From 4732e8a0ee91f1f6657fcef621bda2f04663563b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:31:20 -1100 Subject: [PATCH 194/378] Syntax --- src/komodo_nSPV_superlite.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 3ed2e2f8f..264add97e 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -377,10 +377,10 @@ UniValue NSPV_broadcast(char *hex) { uint8_t msg[64],*tx; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; n = (int32_t)strlen(hex) >> 1; - tx = malloc(n); + tx = (uint8_t *)malloc(n); decode_hex(tx,n,hex); _txid = bits256_doublesha256(0,tx,n); - memcpy(&txid,_txid,sizeof(txid)); + memcpy(&txid,&_txid,sizeof(txid)); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); len += iguana_rwnum(1,&msg[len],sizeof(n),&n); @@ -437,7 +437,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r break; case NSPV_BROADCASTRESP: NSPV_broadcast_purge(&NSPV_broadcastresult); - NSPV_rwbroadcast(0,&response[1],&NSPV_broadcastresult); + NSPV_rwbroadcastresp(0,&response[1],&NSPV_broadcastresult); fprintf(stderr,"got broadcast response %u size.%d\n",timestamp,(int32_t)response.size()); break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); From f1e69bfde2f4a6e17fbfbe85b044ba424e48a06c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:42:01 -1100 Subject: [PATCH 195/378] Fix stack smashing --- src/komodo_nSPV_superlite.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 264add97e..d5dd7e269 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -375,25 +375,31 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_broadcast(char *hex) { - uint8_t msg[64],*tx; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; + uint8_t *msg,*data; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; n = (int32_t)strlen(hex) >> 1; - tx = (uint8_t *)malloc(n); + data = (uint8_t *)malloc(n); decode_hex(tx,n,hex); - _txid = bits256_doublesha256(0,tx,n); + _txid = bits256_doublesha256(0,data,n); memcpy(&txid,&_txid,sizeof(txid)); + msg = malloc(1 + sizeof(txid) + sizeof(n) + n); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); len += iguana_rwnum(1,&msg[len],sizeof(n),&n); - memcpy(&msg[len],tx,n), len += n; + memcpy(&msg[len],data,n), len += n; + free(data); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i Date: Fri, 5 Jul 2019 23:45:23 -1100 Subject: [PATCH 196/378] Syntax --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_superlite.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index af6c5d3f1..320bdbd15 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -201,7 +201,7 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int ptr->retcode = 0; rawtx.resize(n*2+1); init_hexbytes_noT((char *)rawtx.data(),data,n); - fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); + //fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); if ( DecodeHexTx(tx,rawtx) != 0 ) { ptr->txid = tx.GetHash(); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index d5dd7e269..bea83c38c 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -378,10 +378,10 @@ UniValue NSPV_broadcast(char *hex) uint8_t *msg,*data; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; n = (int32_t)strlen(hex) >> 1; data = (uint8_t *)malloc(n); - decode_hex(tx,n,hex); + decode_hex(data,n,hex); _txid = bits256_doublesha256(0,data,n); memcpy(&txid,&_txid,sizeof(txid)); - msg = malloc(1 + sizeof(txid) + sizeof(n) + n); + msg = (uint8_t *)malloc(1 + sizeof(txid) + sizeof(n) + n); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); len += iguana_rwnum(1,&msg[len],sizeof(n),&n); From 21932cbbd52982752bbe46ecb83444f00908d6e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:51:31 -1100 Subject: [PATCH 197/378] +print --- src/komodo_nSPV_fullnode.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 320bdbd15..a29176255 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -204,13 +204,14 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int //fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); if ( DecodeHexTx(tx,rawtx) != 0 ) { + LOCK(cs_main); ptr->txid = tx.GetHash(); + fprintf(stderr,"addmempool transaction %s\n",ptr->txid.GetHex().c_str()); if ( myAddtomempool(tx) != 0 ) - { - fprintf(stderr,"relay transaction %s\n",ptr->txid.GetHex().c_str()); - RelayTransaction(tx); ptr->retcode = 1; - } else ptr->retcode = 0; + else ptr->retcode = 0; + fprintf(stderr,"relay transaction %s\n",ptr->txid.GetHex().c_str()); + RelayTransaction(tx); } else ptr->retcode = -1; return(sizeof(*ptr)); } From a04b17f8df77f7cf8412d7f063f80ba847ffd9a9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Jul 2019 23:58:16 -1100 Subject: [PATCH 198/378] Tweaks --- src/komodo_nSPV.h | 1 + src/komodo_nSPV_fullnode.h | 10 +++++----- src/komodo_nSPV_superlite.h | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fd90df5f4..98ec7cd52 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -374,6 +374,7 @@ int32_t NSPV_rwbroadcastresp(int32_t rwflag,uint8_t *serialized,struct NSPV_broa int32_t len = 0; len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->retcode),&ptr->retcode); + fprintf(stderr,"broadcastresp[%d]\n",len); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index a29176255..bc3003dd4 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -197,12 +197,11 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int32_t n) { - CTransaction tx; std::string rawtx; + CTransaction tx; std::vector rawdata; ptr->retcode = 0; - rawtx.resize(n*2+1); - init_hexbytes_noT((char *)rawtx.data(),data,n); - //fprintf(stderr,"rawtx.(%s)\n",rawtx.c_str()); - if ( DecodeHexTx(tx,rawtx) != 0 ) + rawdata.resize(n); + memcpy(&rawdata[0],data,n); + if ( DecodeHexTx(tx,rawdata.GetHex()) != 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); @@ -494,6 +493,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_BROADCASTRESP; + fprintf(stderr,"slen.%d\n"); if ( NSPV_rwbroadcastresp(1,&response[1],&B) == slen ) { pfrom->PushMessage("nSPV",response); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index bea83c38c..c7aec72ad 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -401,6 +401,7 @@ UniValue NSPV_broadcast(char *hex) } free(msg); memset(&B,0,sizeof(B)); + B.retcode = -2; return(NSPV_broadcast_json(&B)); } From 7c0ad48d79f60d03e2f83ff54c284a34aa4b1ff4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:00:58 -1100 Subject: [PATCH 199/378] ToString --- src/komodo_nSPV_fullnode.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index bc3003dd4..3705e06b3 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -201,7 +201,7 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int ptr->retcode = 0; rawdata.resize(n); memcpy(&rawdata[0],data,n); - if ( DecodeHexTx(tx,rawdata.GetHex()) != 0 ) + if ( DecodeHexTx(tx,rawdata.ToString()) != 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); @@ -493,7 +493,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_BROADCASTRESP; - fprintf(stderr,"slen.%d\n"); + fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwbroadcastresp(1,&response[1],&B) == slen ) { pfrom->PushMessage("nSPV",response); From 9aef2cddf29d16e110f15def92a29fcbccd6c2d0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:04:44 -1100 Subject: [PATCH 200/378] Hexstr --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 3705e06b3..3eb2538c8 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -201,7 +201,7 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int ptr->retcode = 0; rawdata.resize(n); memcpy(&rawdata[0],data,n); - if ( DecodeHexTx(tx,rawdata.ToString()) != 0 ) + if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); From 1d7abe07044a3617feb50224c919a03ba12d3ebc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:10:52 -1100 Subject: [PATCH 201/378] +print --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index c7aec72ad..6bac1e1f2 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -445,7 +445,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_BROADCASTRESP: NSPV_broadcast_purge(&NSPV_broadcastresult); NSPV_rwbroadcastresp(0,&response[1],&NSPV_broadcastresult); - fprintf(stderr,"got broadcast response %u size.%d\n",timestamp,(int32_t)response.size()); + fprintf(stderr,"got broadcast response %u size.%d %s retcode.%d\n",timestamp,(int32_t)response.size(),NSPV_broadcastresult.txid.GetHex().c_str(),NSPV_broadcastresult.retcode); break; default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); break; diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 8fc0b3ce4..b4d914729 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -261,7 +261,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a return(result); } printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); - std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; + std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; CTransaction; mtx.fOverwintered = true; mtx.nExpiryHeight = 0; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; @@ -276,8 +276,17 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a hex = NSPV_signtx(mtx,txfee,opret,used); if ( hex.size() > 0 ) { - result.push_back(Pair("result","success")); - result.push_back(Pair("hex",hex)); + if ( DecodeHexTx(tx,hex) != 0 ) + { + TxToJSON(tx,uint256(),result); + result.push_back(Pair("result","success")); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt decode")); + result.push_back(Pair("hex",hex)); + } } else { From 818019a6dc819d1ba03635c2f7e47563be8799ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:13:00 -1100 Subject: [PATCH 202/378] Syntax --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_wallet.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 3eb2538c8..62d1411bd 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -209,7 +209,7 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int if ( myAddtomempool(tx) != 0 ) ptr->retcode = 1; else ptr->retcode = 0; - fprintf(stderr,"relay transaction %s\n",ptr->txid.GetHex().c_str()); + fprintf(stderr,"relay transaction %s retcode.%d\n",ptr->txid.GetHex().c_str(),ptr->retcode); RelayTransaction(tx); } else ptr->retcode = -1; return(sizeof(*ptr)); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index b4d914729..5aa1fd038 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -18,6 +18,7 @@ #define KOMODO_NSPVWALLET_H // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend +extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); #define NSPV_AUTOLOGOUT 60 #define NSPV_BRANCHID 0x76b809bb @@ -261,7 +262,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a return(result); } printf("%s numutxos.%d balance %.8f\n",NSPV_utxosresult.coinaddr,NSPV_utxosresult.numutxos,(double)NSPV_utxosresult.total/COIN); - std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; CTransaction; + std::vector data; CScript opret; std::string hex; struct NSPV_utxoresp used[NSPV_MAXVINS]; CMutableTransaction mtx; CTransaction tx; mtx.fOverwintered = true; mtx.nExpiryHeight = 0; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; From 744718b1c99b4f2bdb1d678cdc5aa111a15fba88 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:19:12 -1100 Subject: [PATCH 203/378] +print --- src/komodo_nSPV.h | 1 - src/komodo_nSPV_fullnode.h | 5 ++--- src/komodo_nSPV_superlite.h | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 98ec7cd52..fd90df5f4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -374,7 +374,6 @@ int32_t NSPV_rwbroadcastresp(int32_t rwflag,uint8_t *serialized,struct NSPV_broa int32_t len = 0; len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->retcode),&ptr->retcode); - fprintf(stderr,"broadcastresp[%d]\n",len); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 62d1411bd..5a05d01ae 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -205,11 +205,11 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int { LOCK(cs_main); ptr->txid = tx.GetHash(); - fprintf(stderr,"addmempool transaction %s\n",ptr->txid.GetHex().c_str()); + //fprintf(stderr,"addmempool transaction %s\n",ptr->txid.GetHex().c_str()); if ( myAddtomempool(tx) != 0 ) ptr->retcode = 1; else ptr->retcode = 0; - fprintf(stderr,"relay transaction %s retcode.%d\n",ptr->txid.GetHex().c_str(),ptr->retcode); + //fprintf(stderr,"relay transaction %s retcode.%d\n",ptr->txid.GetHex().c_str(),ptr->retcode); RelayTransaction(tx); } else ptr->retcode = -1; return(sizeof(*ptr)); @@ -493,7 +493,6 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_BROADCASTRESP; - fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwbroadcastresp(1,&response[1],&B) == slen ) { pfrom->PushMessage("nSPV",response); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 6bac1e1f2..da34b4c34 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -387,6 +387,7 @@ UniValue NSPV_broadcast(char *hex) len += iguana_rwnum(1,&msg[len],sizeof(n),&n); memcpy(&msg[len],data,n), len += n; free(data); + fprintf(stderr,"send txid.%s\n",txid.GetHex().c_str()); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i Date: Sat, 6 Jul 2019 00:24:26 -1100 Subject: [PATCH 204/378] Fix --- src/komodo_nSPV_superlite.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index da34b4c34..35da59efd 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -192,12 +192,21 @@ UniValue NSPV_ntzsproof_json(struct NSPV_ntzsproofresp *ptr) return(result); } -UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr) +UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr,uint256 txid) { UniValue result(UniValue::VOBJ); result.push_back(Pair("result","success")); - result.push_back(Pair("txid",ptr->txid.GetHex())); + result.push_back(Pair("expected",txid.GetHex())); + result.push_back(Pair("broadcast",ptr->txid.GetHex())); result.push_back(Pair("retcode",(int64_t)ptr->retcode)); + switch ( ptr->retcode ) + { + case 1: result.push_back(Pair("type","broadcast and mempool")); break; + case 0: result.push_back(Pair("type","broadcast")); break; + case -1: result.push_back(Pair("type","decode error")); break; + case -2: result.push_back(Pair("type","timeout")); break; + default: result.push_back(Pair("type","unknown")); break; + } return(result); } @@ -380,7 +389,8 @@ UniValue NSPV_broadcast(char *hex) data = (uint8_t *)malloc(n); decode_hex(data,n,hex); _txid = bits256_doublesha256(0,data,n); - memcpy(&txid,&_txid,sizeof(txid)); + for (i=0; i<32; i++) + ((uint8_t *)&txid[i] = _txid.bytes[31 - i]; msg = (uint8_t *)malloc(1 + sizeof(txid) + sizeof(n) + n); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); @@ -396,14 +406,14 @@ UniValue NSPV_broadcast(char *hex) if ( NSPV_broadcastresult.txid == txid ) { free(msg); - return(NSPV_broadcast_json(&NSPV_broadcastresult)); + return(NSPV_broadcast_json(&NSPV_broadcastresult,txid)); } } } free(msg); memset(&B,0,sizeof(B)); B.retcode = -2; - return(NSPV_broadcast_json(&B)); + return(NSPV_broadcast_json(&B,txid)); } void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response From 3bf999989d35bc4e1249298c7d6e268a6fbacc6e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:26:28 -1100 Subject: [PATCH 205/378] ) --- src/komodo_nSPV_superlite.h | 4 ++-- src/rpc/rawtransaction.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 35da59efd..a2f80b79e 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -390,14 +390,14 @@ UniValue NSPV_broadcast(char *hex) decode_hex(data,n,hex); _txid = bits256_doublesha256(0,data,n); for (i=0; i<32; i++) - ((uint8_t *)&txid[i] = _txid.bytes[31 - i]; + ((uint8_t *)&txid)[i] = _txid.bytes[31 - i]; msg = (uint8_t *)malloc(1 + sizeof(txid) + sizeof(n) + n); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); len += iguana_rwnum(1,&msg[len],sizeof(n),&n); memcpy(&msg[len],data,n), len += n; free(data); - fprintf(stderr,"send txid.%s\n",txid.GetHex().c_str()); + //fprintf(stderr,"send txid.%s\n",txid.GetHex().c_str()); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i 2) @@ -1340,8 +1342,12 @@ UniValue sendrawtransaction(const UniValue& params, bool fHelp) } else if (fHaveChain) { throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain"); } + RelayTransaction(tx); + } + else + { + NSPV_broadcast((char *)params[0].get_str().c_str()); } - RelayTransaction(tx); return hashTx.GetHex(); } From b35d0253887e519f58846bd1352ecddef224b871 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 00:30:28 -1100 Subject: [PATCH 206/378] Add hex --- src/komodo_nSPV_wallet.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 5aa1fd038..c96aa99db 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -281,6 +281,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a { TxToJSON(tx,uint256(),result); result.push_back(Pair("result","success")); + result.push_back(Pair("hex",hex)); } else { From 64915ad6d2ce2702ff691fc1548e2524fb03dee2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 03:03:01 -1100 Subject: [PATCH 207/378] Fix spamming --- src/bitcoind.cpp | 3 ++- src/komodo_nSPV.h | 29 ++++++++----------------- src/komodo_nSPV_superlite.h | 6 ++---- src/komodo_nSPV_wallet.h | 43 +++++++++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 68e0da8b3..ff329a0d6 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -77,7 +77,8 @@ void WaitForShutdown(boost::thread_group* threadGroup) //fprintf(stderr,"call passport iteration\n"); if ( ASSETCHAINS_SYMBOL[0] == 0 ) { - komodo_passport_iteration(); + if ( KOMODO_NSPV == 0 ) + komodo_passport_iteration(); for (i=0; i<10; i++) { fShutdown = ShutdownRequested(); diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fd90df5f4..56ec85488 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -256,26 +256,6 @@ void NSPV_txproof_purge(struct NSPV_txproof *ptr) } } -/*struct NSPV_utxo -{ - struct NSPV_txproof T; - int64_t satoshis,extradata; - int32_t vout,prevht,nextht,pad32; -}; - -int32_t NSPV_rwutxo(int32_t rwflag,uint8_t *serialized,struct NSPV_utxo *ptr) -{ - int32_t len = 0; - len += NSPV_rwtxproof(rwflag,&serialized[len],&ptr->T); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->satoshis),&ptr->satoshis); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->extradata),&ptr->extradata); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->prevht),&ptr->prevht); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); - return(len); -}*/ - struct NSPV_ntzproofshared { struct NSPV_equihdr *hdrs; @@ -383,4 +363,13 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) memset(ptr,0,sizeof(*ptr)); } +uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) +{ + bits256 _hash; uint256 hash; + _hash = bits256_doublesha256(0,data,datalen); + for (i=0; i<32; i++) + ((uint8_t *)&hash)[i] = _hash.bytes[31 - i]; + return(hash); +} + #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index a2f80b79e..ade2c8115 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -384,13 +384,11 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_broadcast(char *hex) { - uint8_t *msg,*data; bits256 _txid; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; + uint8_t *msg,*data; uint256 txid; uint16_t n; int32_t i,len = 0; struct NSPV_broadcastresp B; n = (int32_t)strlen(hex) >> 1; data = (uint8_t *)malloc(n); decode_hex(data,n,hex); - _txid = bits256_doublesha256(0,data,n); - for (i=0; i<32; i++) - ((uint8_t *)&txid)[i] = _txid.bytes[31 - i]; + txid = NSPV_doublesha256(data,n); msg = (uint8_t *)malloc(1 + sizeof(txid) + sizeof(n) + n); msg[len++] = NSPV_BROADCAST; len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index c96aa99db..d53d0c8a1 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -23,6 +23,37 @@ extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& #define NSPV_AUTOLOGOUT 60 #define NSPV_BRANCHID 0x76b809bb + +/*struct NSPV_ntzproofshared +{ + struct NSPV_equihdr *hdrs; + int32_t prevht,nextht,pad32; + uint16_t numhdrs,pad16; +}; + +struct NSPV_ntzsproofresp +{ + struct NSPV_ntzproofshared common; + uint256 prevtxid,nexttxid; + int32_t pad32,prevtxidht,nexttxidht; + uint16_t prevtxlen,nexttxlen; + uint8_t *prevntz,*nextntz; +};*/ + +int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresult *ptr) +{ + int32_t i; + // verify nextntz is valid notarization + // validate blockhash of lasthdr with nextntz value, and height + for (i=numhdrs-1; i>0; i--) + { + // make sure the hash of i-1 matches the prevBlockhash of i + } + // verify prevntz is valid notarization + // verify blockhash of first hdr with prevntz value and height + return(0); +} + int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { char *txstr; int32_t retval = 0; @@ -35,11 +66,15 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) retval = -1; else { - //printf("got tx.(%s)\n",txstr); - // need to validate txproof NSPV_notarizations(height); // gets the prev and next notarizations - NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment - // merkle prove txproof to the merkleroot in the corresponding hdr + if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) + { + NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment + if ( NSPV_validatehdrs(&NSPV_ntzsproofresult) == 0 ) + { + // merkle prove txproof to the merkleroot in the corresponding hdr + } + } else retval = -1; } free(txstr); return(retval); From 6db0b511b9c1c738279583c34bdf4d7b9f14fdd1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 03:05:04 -1100 Subject: [PATCH 208/378] Syntax --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_wallet.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 56ec85488..1df9aedf4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -365,7 +365,7 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { - bits256 _hash; uint256 hash; + bits256 _hash; uint256 hash; int32_t i; _hash = bits256_doublesha256(0,data,datalen); for (i=0; i<32; i++) ((uint8_t *)&hash)[i] = _hash.bytes[31 - i]; diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d53d0c8a1..d97821fb1 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -40,12 +40,12 @@ struct NSPV_ntzsproofresp uint8_t *prevntz,*nextntz; };*/ -int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresult *ptr) +int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { int32_t i; // verify nextntz is valid notarization // validate blockhash of lasthdr with nextntz value, and height - for (i=numhdrs-1; i>0; i--) + for (i=ptr->common.numhdrs-1; i>0; i--) { // make sure the hash of i-1 matches the prevBlockhash of i } From 252f51adc58cec35d892e8df2112220630127b3f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 03:34:59 -1100 Subject: [PATCH 209/378] Very partial CC signing support --- src/cc/CCinclude.h | 1 + src/cc/CCtx.cpp | 20 ++++++++++++++++++-- src/cc/CCutils.cpp | 6 ++++++ src/komodo_nSPV_superlite.h | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 1d8cf669e..85ff380ca 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -289,6 +289,7 @@ void SetCCtxids(std::vector > &addressIndex int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs); int64_t AddNormalinputs2(CMutableTransaction &mtx,int64_t total,int32_t maxinputs); int64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout,int32_t CCflag); +bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey); // curve25519 and sha256 bits256 curve25519_shared(bits256 privkey,bits256 otherpub); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 7435defbf..b51342be7 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -150,8 +150,16 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran utxovout = mtx.vin[i].prevout.n; if ( vintx.vout[utxovout].scriptPubKey.IsPayToCryptoCondition() == 0 ) { - if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) - fprintf(stderr,"signing error for vini.%d of %llx\n",i,(long long)vinimask); + if ( KOMODO_NSPV == 0 ) + { + if ( SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) + fprintf(stderr,"signing error for vini.%d of %llx\n",i,(long long)vinimask); + } + else + { + if ( NSPV_SignTx(mtx,i,vintx.vout[utxovout].nValue,vintx.vout[utxovout].scriptPubKey) == 0 ) + fprintf(stderr,"NSPV signing error for vini.%d of %llx\n",i,(long long)vinimask); + } } else { @@ -510,6 +518,10 @@ int32_t CC_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t * int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs) { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t sum,threshold,above,below; int64_t remains,nValue,totalinputs = 0; uint256 txid,hashBlock; std::vector vecOutputs; CTransaction tx; struct CC_utxo *utxos,*up; + if ( KOMODO_NSPV != 0 ) + { + //return(NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num)); + } #ifdef ENABLE_WALLET assert(pwalletMain != NULL); const CKeyStore& keystore = *pwalletMain; @@ -605,6 +617,10 @@ int64_t AddNormalinputs2(CMutableTransaction &mtx,int64_t total,int32_t maxinput { int32_t abovei,belowi,ind,vout,i,n = 0; int64_t sum,threshold,above,below; int64_t remains,nValue,totalinputs = 0; char coinaddr[64]; uint256 txid,hashBlock; CTransaction tx; struct CC_utxo *utxos,*up; std::vector > unspentOutputs; + if ( KOMODO_NSPV != 0 ) + { + //return(NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64_t total,int32_t maxinputs,struct NSPV_utxoresp *ptr,int32_t num)); + } utxos = (struct CC_utxo *)calloc(CC_MAXVINS,sizeof(*utxos)); if ( maxinputs > CC_MAXVINS ) maxinputs = CC_MAXVINS; diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 5de5ed0db..699b84727 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -437,9 +437,15 @@ std::vector Mypubkey() return(pubkey); } +extern CKey NSPV_key; bool Myprivkey(uint8_t myprivkey[]) { char coinaddr[64],checkaddr[64]; std::string strAddress; char *dest; int32_t i,n; CBitcoinAddress address; CKeyID keyID; CKey vchSecret; uint8_t buf33[33]; + if ( KOMODO_NSPV != 0 ) + { + memcpy(myprivkey,NSPV_key.vch,32); + return true; + } if ( Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG) != 0 ) { n = (int32_t)strlen(coinaddr); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index ade2c8115..9a3f459b0 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -236,6 +236,8 @@ UniValue NSPV_login(char *wifstr) result.push_back(Pair("address",NSPV_address)); result.push_back(Pair("pubkey",HexStr(pubkey))); strcpy(NSPV_pubkeystr,HexStr(pubkey).c_str()); + if ( KOMODO_NSPV != 0 ) + decode_hex(NOTARY_PUBKEY33,33,NSPV_pubkeystr); result.push_back(Pair("wifprefix",(int64_t)data[0])); result.push_back(Pair("compressed",(int64_t)(data[len-5] == 1))); memset(data,0,sizeof(data)); From 559c2cf8ffa1b14f57e641893f862ad160443760 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 03:38:51 -1100 Subject: [PATCH 210/378] myprivkey --- src/cc/CCutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 699b84727..3c67b4068 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -443,7 +443,7 @@ bool Myprivkey(uint8_t myprivkey[]) char coinaddr[64],checkaddr[64]; std::string strAddress; char *dest; int32_t i,n; CBitcoinAddress address; CKeyID keyID; CKey vchSecret; uint8_t buf33[33]; if ( KOMODO_NSPV != 0 ) { - memcpy(myprivkey,NSPV_key.vch,32); + NSPV_key.SetKey32(myprivkey); return true; } if ( Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG) != 0 ) From 1d4b40c9123f8bb42dc1cf9095d525f79739c244 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 03:43:28 -1100 Subject: [PATCH 211/378] Skip params if nSPV --- src/init.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 7e131b759..f2335ce38 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1302,9 +1302,11 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) libsnark::inhibit_profiling_info = true; libsnark::inhibit_profiling_counters = true; - // Initialize Zcash circuit parameters - ZC_LoadParams(chainparams); - + if ( KOMODO_NSPV == 0 ) + { + // Initialize Zcash circuit parameters + ZC_LoadParams(chainparams); + } /* Start the RPC server already. It will be started in "warmup" mode * and not really process calls already (but it will signify connections * that the server is there and will be ready later). Warmup mode will From 685056e890d90eba50883d6bd69e34f22dcc450a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:19:44 -1100 Subject: [PATCH 212/378] Getnewaddress for nSPV mode --- src/wallet/rpcwallet.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 955118346..e463af829 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -176,6 +176,19 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) + HelpExampleRpc("getnewaddress", "") ); + if ( KOMODO_NSPV != 0 ) + { + uint8_t priv32[32]; +#ifndef __WIN32 + OS_randombytes(priv32,sizeof(priv32)); +#else + randombytes_buf(priv32,sizeof(priv32)); +#endif + key.Set(&priv[0],&priv[32], true); + CPubKey pubkey = key.GetPubKey(); + CKeyID vchAddress = pubkey.GetID(); + return(EncodeDestination(vchAddress)); + } LOCK2(cs_main, pwalletMain->cs_wallet); // Parse the account first so we don't generate a key if there's an error From 6ebbdf83033780ef608f0dd109ae3afc5df9d70d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:24:18 -1100 Subject: [PATCH 213/378] Set nLockTime --- src/komodo_nSPV_wallet.h | 1 + src/wallet/rpcwallet.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d97821fb1..bfcb5fecb 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -302,6 +302,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a mtx.nExpiryHeight = 0; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; mtx.nVersion = SAPLING_TX_VERSION; + mtx.nLockTime = (uint32_t)time(NULL) - 777; memset(used,0,sizeof(used)); data.resize(20); memcpy(&data[0],&rmd160[1],20); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e463af829..3ac705dff 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -158,6 +158,8 @@ char *komodo_chainname() return(ASSETCHAINS_SYMBOL[0] == 0 ? (char *)"KMD" : ASSETCHAINS_SYMBOL); } +void OS_randombytes(unsigned char *x,long xlen); + UniValue getnewaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) @@ -184,7 +186,7 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) #else randombytes_buf(priv32,sizeof(priv32)); #endif - key.Set(&priv[0],&priv[32], true); + key.Set(&priv32[0],&priv[32], true); CPubKey pubkey = key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); return(EncodeDestination(vchAddress)); From b587a18dd3a56cb8807ac549df758cbc7e0012c7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:26:36 -1100 Subject: [PATCH 214/378] Priv32 --- src/komodo_nSPV_wallet.h | 3 ++- src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index bfcb5fecb..2f93ee2d5 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -302,7 +302,8 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a mtx.nExpiryHeight = 0; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; mtx.nVersion = SAPLING_TX_VERSION; - mtx.nLockTime = (uint32_t)time(NULL) - 777; + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + mtx.nLockTime = (uint32_t)time(NULL) - 777; memset(used,0,sizeof(used)); data.resize(20); memcpy(&data[0],&rmd160[1],20); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 3ac705dff..ef511e78d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -186,7 +186,7 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) #else randombytes_buf(priv32,sizeof(priv32)); #endif - key.Set(&priv32[0],&priv[32], true); + key.Set(&priv32[0],&priv32[32], true); CPubKey pubkey = key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); return(EncodeDestination(vchAddress)); From 94d248033db65464e77c0431bc6ee4d4e2df980e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:29:08 -1100 Subject: [PATCH 215/378] Key --- src/wallet/rpcwallet.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ef511e78d..a21ca868e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -180,16 +180,19 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) if ( KOMODO_NSPV != 0 ) { - uint8_t priv32[32]; + UniValue result(UniValue::VOBJ); uint8_t priv32[32]; #ifndef __WIN32 OS_randombytes(priv32,sizeof(priv32)); #else randombytes_buf(priv32,sizeof(priv32)); #endif + CKey key; key.Set(&priv32[0],&priv32[32], true); CPubKey pubkey = key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); - return(EncodeDestination(vchAddress)); + result.push_back(Pair("address",EncodeDestination(vchAddress))); + result.push_back(Pair("pubkey",HexStr(pubkey))); + return(result); } LOCK2(cs_main, pwalletMain->cs_wallet); From 5982e769966f4455a17992c65cb5e67f41fe4718 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:31:25 -1100 Subject: [PATCH 216/378] Allow nSPV getnewaddress --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index a21ca868e..9e1cb4784 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -162,7 +162,7 @@ void OS_randombytes(unsigned char *x,long xlen); UniValue getnewaddress(const UniValue& params, bool fHelp) { - if (!EnsureWalletIsAvailable(fHelp)) + if ( KOMODO_NSPV == 0 && !EnsureWalletIsAvailable(fHelp) ) return NullUniValue; if (fHelp || params.size() > 1) From cecf3f5de59e43088a82667caf5310d39ccb595a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:34:47 -1100 Subject: [PATCH 217/378] Display wif --- src/wallet/rpcwallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9e1cb4784..1d7768255 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -190,6 +190,7 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) key.Set(&priv32[0],&priv32[32], true); CPubKey pubkey = key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); + result.push_back(Pair("wif",EncodeSecret(key))); result.push_back(Pair("address",EncodeDestination(vchAddress))); result.push_back(Pair("pubkey",HexStr(pubkey))); return(result); From d3730db5d8b7f60d26d85d29fa811adfc0a88f24 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 07:52:38 -1100 Subject: [PATCH 218/378] Prevent crash when displaying interest --- src/rpc/rawtransaction.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 97544bd06..c9d16a0de 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -374,7 +374,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) const CTxOut& txout = tx.vout[i]; UniValue out(UniValue::VOBJ); out.push_back(Pair("value", ValueFromAmount(txout.nValue))); - if ( ASSETCHAINS_SYMBOL[0] == 0 && tx.nLockTime >= 500000000 && (tipindex= chainActive.LastTip()) != 0 ) + if ( KOMODO_NSPV == 0 && ASSETCHAINS_SYMBOL[0] == 0 && tx.nLockTime >= 500000000 && (tipindex= chainActive.LastTip()) != 0 ) { int64_t interest; int32_t txheight; uint32_t locktime; interest = komodo_accrued_interest(&txheight,&locktime,tx.GetHash(),i,0,txout.nValue,(int32_t)tipindex->GetHeight()); From a099b3a0256a8375a8b31bb41ad7aec8e91c2d56 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 08:05:19 -1100 Subject: [PATCH 219/378] nSPV full nodes to ignore nSPV messages --- src/main.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 098396533..8f54bc71d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7239,15 +7239,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return false; } } - else if ( (0) ) - { - if ( pfrom->nServices != 0 ) - { - fprintf(stderr,"debug mode, disconnect legacy peer.%d\n",pfrom->id); - pfrom->fDisconnect = true; - return false; - } - } // Mark this node as currently connected, so we update its timestamp later. if (pfrom->fNetworkNode) { LOCK(cs_main); @@ -7445,9 +7436,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, } else if (strCommand == "nSPV") { - std::vector payload; - vRecv >> payload; - komodo_nSPVresp(pfrom,payload); + if ( KOMODO_NSPV != 0 ) + { + std::vector payload; + vRecv >> payload; + komodo_nSPVresp(pfrom,payload); + } return(true); } else if ( KOMODO_NSPV != 0 ) @@ -8240,9 +8234,11 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } state.fShouldBan = false; } - komodo_nSPV(pto); if ( KOMODO_NSPV != 0 ) + { + komodo_nSPV(pto); return(true); + } BOOST_FOREACH(const CBlockReject& reject, state.rejects) pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock); state.rejects.clear(); From 912f50a0e456c51f9a7b2dcbc384cb5dd69b4674 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 08:13:37 -1100 Subject: [PATCH 220/378] Autologout 777 --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 9a3f459b0..864106b31 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -228,7 +228,7 @@ UniValue NSPV_login(char *wifstr) strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); NSPV_logintime = (uint32_t)time(NULL); result.push_back(Pair("result","success")); - result.push_back(Pair("status","wif will expire in 60 seconds")); + result.push_back(Pair("status","wif will expire in 777 seconds")); NSPV_key = DecodeSecret(wifstr); CPubKey pubkey = NSPV_key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 2f93ee2d5..e9703177a 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,7 +20,7 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); -#define NSPV_AUTOLOGOUT 60 +#define NSPV_AUTOLOGOUT 777 #define NSPV_BRANCHID 0x76b809bb From 68c31807c383f10916275436c861099b91df36ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 08:50:01 -1100 Subject: [PATCH 221/378] Catch expired wif before assert --- src/komodo_nSPV_wallet.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index e9703177a..22986db04 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -311,6 +311,12 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a if ( NSPV_addinputs(used,mtx,satoshis+txfee,64,NSPV_utxosresult.utxos,NSPV_utxosresult.numutxos) > 0 ) { mtx.vout.push_back(CTxOut(satoshis,CScript() << OP_DUP << OP_HASH160 << ParseHex(HexStr(data)) << OP_EQUALVERIFY << OP_CHECKSIG)); + if ( NSPV_logintime == 0 || time(NULL) > NSPV_logintime+NSPV_AUTOLOGOUT ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","wif expired")); + return(result); + } hex = NSPV_signtx(mtx,txfee,opret,used); if ( hex.size() > 0 ) { From 76ca220c937af15bba7b0cf62806e954fbd0ce32 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 08:50:55 -1100 Subject: [PATCH 222/378] +print --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 864106b31..916fea8aa 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -32,9 +32,9 @@ CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); uint32_t NSPV_lastinfo,NSPV_logintime; +CKey NSPV_key; char NSPV_wifstr[64],NSPV_pubkeystr[67]; std::string NSPV_address; -CKey NSPV_key; struct NSPV_inforesp NSPV_inforesult; struct NSPV_utxosresp NSPV_utxosresult; struct NSPV_spentinfo NSPV_spentresult; diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 22986db04..3ebc7523c 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -355,7 +355,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+NSPV_AUTOLOGOUT ) { - fprintf(stderr,"scrub wif from NSPV memory\n"); + fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); memset(&NSPV_key,0,sizeof(NSPV_key)); NSPV_logintime = 0; From b715a05c069479bcf66260dd93ca6de0bbc39c0e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 08:56:11 -1100 Subject: [PATCH 223/378] Boost max connections to 8000 --- src/compat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compat.h b/src/compat.h index 44b7dcee2..b0730d1aa 100644 --- a/src/compat.h +++ b/src/compat.h @@ -39,7 +39,7 @@ #ifdef FD_SETSIZE #undef FD_SETSIZE // prevent redefinition compiler warning #endif -#define FD_SETSIZE 1024 // max number of fds in fd_set +#define FD_SETSIZE (8*1024) // max number of fds in fd_set #include // Must be included before mswsock.h and windows.h From c36802edc3bf4227e84bc8ad767295dbb22332c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 09:16:00 -1100 Subject: [PATCH 224/378] +print --- src/init.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/init.cpp b/src/init.cpp index f2335ce38..2ab35fd5c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -976,13 +976,15 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // Make sure enough file descriptors are available int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1); nMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS); + fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0); int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS); + fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); if (nFD < MIN_CORE_FILEDESCRIPTORS) return InitError(_("Not enough file descriptors available.")); if (nFD - MIN_CORE_FILEDESCRIPTORS < nMaxConnections) nMaxConnections = nFD - MIN_CORE_FILEDESCRIPTORS; - + fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); // if using block pruning, then disable txindex // also disable the wallet (for now, until SPV support is implemented in wallet) if (GetArg("-prune", 0)) { From d77b5f657c63d4d6a09df85b37e3623b66384cd9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 09:19:21 -1100 Subject: [PATCH 225/378] +print --- src/init.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 2ab35fd5c..3cc2d8f63 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -976,10 +976,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // Make sure enough file descriptors are available int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1); nMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS); - fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); + //fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0); int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS); - fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); + fprintf(stderr,"nMaxConnections %d FD_SETSIZE.%d nBind.%d expr.%d \n",nMaxConnections,FD_SETSIZE,nBind,(int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)); if (nFD < MIN_CORE_FILEDESCRIPTORS) return InitError(_("Not enough file descriptors available.")); if (nFD - MIN_CORE_FILEDESCRIPTORS < nMaxConnections) From 1d4be8defb49d0924dbbc26def422b385f26c80e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 09:24:44 -1100 Subject: [PATCH 226/378] Revert FD_SETSIZE --- src/compat.h | 2 +- src/init.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compat.h b/src/compat.h index b0730d1aa..44b7dcee2 100644 --- a/src/compat.h +++ b/src/compat.h @@ -39,7 +39,7 @@ #ifdef FD_SETSIZE #undef FD_SETSIZE // prevent redefinition compiler warning #endif -#define FD_SETSIZE (8*1024) // max number of fds in fd_set +#define FD_SETSIZE 1024 // max number of fds in fd_set #include // Must be included before mswsock.h and windows.h diff --git a/src/init.cpp b/src/init.cpp index 3cc2d8f63..738d94377 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -979,7 +979,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) //fprintf(stderr,"nMaxConnections %d\n",nMaxConnections); nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0); int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS); - fprintf(stderr,"nMaxConnections %d FD_SETSIZE.%d nBind.%d expr.%d \n",nMaxConnections,FD_SETSIZE,nBind,(int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)); + //fprintf(stderr,"nMaxConnections %d FD_SETSIZE.%d nBind.%d expr.%d \n",nMaxConnections,FD_SETSIZE,nBind,(int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)); if (nFD < MIN_CORE_FILEDESCRIPTORS) return InitError(_("Not enough file descriptors available.")); if (nFD - MIN_CORE_FILEDESCRIPTORS < nMaxConnections) From 6e730ec1504d564f4e1431d4da7c8402aaf570c7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 09:56:25 -1100 Subject: [PATCH 227/378] +getinfo fields --- src/komodo_nSPV.h | 13 ++++++++++++- src/komodo_nSPV_fullnode.h | 1 - src/komodo_nSPV_superlite.h | 18 +++++++++++------- src/komodo_nSPV_wallet.h | 2 -- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1df9aedf4..18f85a036 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -16,13 +16,24 @@ // todo: // make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) + +// determine if it makes sense to be scanning mempool for the utxo/spentinfo requests, maybe can add isspent flag to txproof, or just use getspentinfo if broadcast fails + // validate proofs +// interest calculations are currently just using what is returned, it should calculate it from scratch + +// need to validate incoming data and update only if it is valid and more recent // make sure to sanity check all vector lengths on receipt -// determine if it makes sense to be scanning mempool for the utxo/spentinfo requests #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H +#define NSPV_POLLITERS 15 +#define NSPV_POLLMICROS 100000 +#define NSPV_MAXVINS 64 +#define NSPV_AUTOLOGOUT 777 +#define NSPV_BRANCHID 0x76b809bb + // nSPV defines and struct definitions with serialization and purge functions #define NSPV_INFO 0x00 diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 5a05d01ae..ee837e6ad 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -17,7 +17,6 @@ #ifndef KOMODO_NSPVFULLNODE_H #define KOMODO_NSPVFULLNODE_H -// on fullnode: // NSPV_get... functions need to return the exact serialized length, which is the size of the structure minus size of pointers, plus size of allocated data #include "notarisationdb.h" diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 916fea8aa..71281d087 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -21,12 +21,6 @@ // no caching, no optimizations, no reducing the number of ntzsproofs needed by detecting overlaps, etc. // advantage is that it is simpler to implement and understand to create a design for a more performant version -// interest calculations are currently just using what is returned, it should calculate it from scratch -// need to validate incoming data and update only if it is valid and more recent - -#define NSPV_POLLITERS 15 -#define NSPV_POLLMICROS 100000 -#define NSPV_MAXVINS 64 CAmount AmountFromValue(const UniValue& value); int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); @@ -113,8 +107,18 @@ UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) { - UniValue result(UniValue::VOBJ); + UniValue result(UniValue::VOBJ); int32_t expiration; uint32_t timestamp = (uint32_t)time(NULL); result.push_back(Pair("result","success")); + if ( NSPV_address.size() != 0 ) + { + result.push_back(Pair("address",NSPV_address)); + result.push_back(Pair("pubkey",NSPV_pubkeystr)); + } + if ( NSPV_logintime != 0 ) + { + expiration = (NSPV_logintime + NSPV_AUTOLOGOUT - timestamp); + result.push_back(Pair("wifexpires",expiration)); + } result.push_back(Pair("height",(int64_t)ptr->height)); result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 3ebc7523c..7db65b60e 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,8 +20,6 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); -#define NSPV_AUTOLOGOUT 777 -#define NSPV_BRANCHID 0x76b809bb /*struct NSPV_ntzproofshared From 24b67c623b68c28eb960d332b8360ad2e7205d34 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 19:41:55 -1100 Subject: [PATCH 228/378] Small fixes and logout --- src/komodo_nSPV_superlite.h | 18 ++++++++++++++++-- src/komodo_nSPV_wallet.h | 5 +---- src/rpc/server.cpp | 1 + src/rpc/server.h | 1 + src/wallet/rpcdump.cpp | 20 +++++++++++++++++--- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 71281d087..f91df8566 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -214,6 +214,17 @@ UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr,uint256 txid) return(result); } +UniValue NSPV_logout() +{ + UniValue result(UniValue::VOBJ); + fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); + memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + memset(&NSPV_key,0,sizeof(NSPV_key)); + NSPV_logintime = 0; + result.push_back(Pair("result","success")); + return(result); +} + UniValue NSPV_login(char *wifstr) { UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; @@ -229,11 +240,14 @@ UniValue NSPV_login(char *wifstr) return(result); } memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); - strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); NSPV_logintime = (uint32_t)time(NULL); + if ( strcmp(NSPV_wifstr,wifstr) != 0 ) + { + strncpy(NSPV_wifstr,wifstr,sizeof(NSPV_wifstr)-1); + NSPV_key = DecodeSecret(wifstr); + } result.push_back(Pair("result","success")); result.push_back(Pair("status","wif will expire in 777 seconds")); - NSPV_key = DecodeSecret(wifstr); CPubKey pubkey = NSPV_key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); NSPV_address = EncodeDestination(vchAddress); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 7db65b60e..ba70a3b8a 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -353,10 +353,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+NSPV_AUTOLOGOUT ) { - fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); - memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); - memset(&NSPV_key,0,sizeof(NSPV_key)); - NSPV_logintime = 0; + NSPV_logout(); } if ( (pto->nServices & NODE_NSPV) == 0 ) return; diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 36e4a8fda..d3e0646ea 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -426,6 +426,7 @@ static const CRPCCommand vRPCCommands[] = { "nSPV", "nspv_txproof", &nspv_txproof, true }, { "nSPV", "nspv_spend", &nspv_spend, true }, { "nSPV", "nspv_broadcast", &nspv_broadcast, true }, + { "nSPV", "nspv_logout", &nspv_logout, true }, // rewards { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index 31da0e32f..35af45a18 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -464,6 +464,7 @@ extern UniValue nspv_hdrsproof(const UniValue& params, bool fHelp); extern UniValue nspv_txproof(const UniValue& params, bool fHelp); extern UniValue nspv_spend(const UniValue& params, bool fHelp); extern UniValue nspv_broadcast(const UniValue& params, bool fHelp); +extern UniValue nspv_logout(const UniValue& params, bool fHelp); extern UniValue getblocksubsidy(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 225d32c80..7069dcaec 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -974,6 +974,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) UniValue NSPV_getinfo_json(); UniValue NSPV_login(char *wifstr); +UniValue NSPV_logout(); UniValue NSPV_addressutxos(char *coinaddr); UniValue NSPV_broadcast(char *hex); UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis); @@ -991,6 +992,13 @@ UniValue nspv_getinfo(const UniValue& params, bool fHelp) return(NSPV_getinfo_json()); } +UniValue nspv_logout(const UniValue& params, bool fHelp) +{ + if ( fHelp || params.size() != 0 ) + throw runtime_error("nspv_logout\n"); + return(NSPV_getinfo_logout()); +} + UniValue nspv_login(const UniValue& params, bool fHelp) { if ( fHelp || params.size() != 1 ) @@ -1002,9 +1010,15 @@ UniValue nspv_listunspent(const UniValue& params, bool fHelp) { if ( fHelp || params.size() > 1 ) throw runtime_error("nspv_listunspent address\n"); - if ( params.size() == 0 && NSPV_address.size() != 0 ) - return(NSPV_addressutxos((char *)NSPV_address.c_str())); - return(NSPV_addressutxos((char *)params[0].get_str().c_str())); + if ( params.size() == 0 ) + { + if ( NSPV_address.size() != 0 ) + return(NSPV_addressutxos((char *)NSPV_address.c_str())); + else throw runtime_error("nspv_listunspent address\n"); + } + if ( params.size() == 1 ) + return(NSPV_addressutxos((char *)params[0].get_str().c_str())); + else throw runtime_error("nspv_listunspent address\n"); } UniValue nspv_spentinfo(const UniValue& params, bool fHelp) From 5e741c03f2ef8740de12fd612195acd22e890804 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 19:46:38 -1100 Subject: [PATCH 229/378] Syntax --- src/wallet/rpcdump.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 7069dcaec..43cb4f420 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -996,7 +996,7 @@ UniValue nspv_logout(const UniValue& params, bool fHelp) { if ( fHelp || params.size() != 0 ) throw runtime_error("nspv_logout\n"); - return(NSPV_getinfo_logout()); + return(NSPV_logout()); } UniValue nspv_login(const UniValue& params, bool fHelp) From 3e334911871c0922111b39082a774638e7bc1b54 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 19:49:17 -1100 Subject: [PATCH 230/378] Add status --- src/komodo_nSPV_superlite.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index f91df8566..e12fd7954 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -217,11 +217,13 @@ UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr,uint256 txid) UniValue NSPV_logout() { UniValue result(UniValue::VOBJ); - fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); + result.push_back(Pair("result","success")); + if ( NSPV_logintime != 0 ) + fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); + else result.push_back(Pair("status","wasnt logged in")); memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); memset(&NSPV_key,0,sizeof(NSPV_key)); NSPV_logintime = 0; - result.push_back(Pair("result","success")); return(result); } From 8cde0ef5e4ef61e74181f35c969461b2c2ad2edb Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 19:56:29 -1100 Subject: [PATCH 231/378] +print --- src/komodo_nSPV_superlite.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index e12fd7954..8d3124faf 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -289,6 +289,7 @@ UniValue NSPV_getinfo_json() UniValue NSPV_addressutxos(char *coinaddr) { UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,slen,len = 0; + fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str()); if ( bitcoin_base58decode(msg,coinaddr) != 25 ) { result.push_back(Pair("result","error")); From 87f816960367fdc344cdfecf78749bdf62c6f32d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 20:01:00 -1100 Subject: [PATCH 232/378] Prevent returning cached inspects --- src/komodo_nSPV_superlite.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 8d3124faf..ebafb186c 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -289,7 +289,7 @@ UniValue NSPV_getinfo_json() UniValue NSPV_addressutxos(char *coinaddr) { UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,slen,len = 0; - fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str()); + //fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str()); if ( bitcoin_base58decode(msg,coinaddr) != 25 ) { result.push_back(Pair("result","error")); @@ -312,7 +312,7 @@ UniValue NSPV_addressutxos(char *coinaddr) for (i=0; i= NSPV_inforesult.height ) + if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } } From b948cd45de3e5ef2cb460db2648627e8f10344fa Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 20:55:20 -1100 Subject: [PATCH 233/378] +prints --- src/komodo_nSPV_fullnode.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index ee837e6ad..e4e8f7518 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -200,11 +200,12 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int ptr->retcode = 0; rawdata.resize(n); memcpy(&rawdata[0],data,n); + fprintf(stderr,"sendraw.(%s)\n",HexStr(rawdata)); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); - //fprintf(stderr,"addmempool transaction %s\n",ptr->txid.GetHex().c_str()); + fprintf(stderr,"addmempool transaction %s\n",ptr->txid.GetHex().c_str()); if ( myAddtomempool(tx) != 0 ) ptr->retcode = 1; else ptr->retcode = 0; From b9e7f023ae47925b58f8e07546ac9eca6f526731 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 20:57:12 -1100 Subject: [PATCH 234/378] c_str --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index e4e8f7518..b93e299f1 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -200,7 +200,7 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int ptr->retcode = 0; rawdata.resize(n); memcpy(&rawdata[0],data,n); - fprintf(stderr,"sendraw.(%s)\n",HexStr(rawdata)); + fprintf(stderr,"sendraw.(%s)\n",HexStr(rawdata).c_str()); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) { LOCK(cs_main); From 6deebc41c792b9ea2efc2ac8978188b48a2bb5d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:09:34 -1100 Subject: [PATCH 235/378] +print --- src/komodo_nSPV_wallet.h | 14 +++++++++++--- src/wallet/rpcdump.cpp | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ba70a3b8a..5c34bcb14 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -40,15 +40,23 @@ struct NSPV_ntzsproofresp int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { - int32_t i; - // verify nextntz is valid notarization - // validate blockhash of lasthdr with nextntz value, and height + /* int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) + return(-1); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) + return(-2); + else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + return(-3); for (i=ptr->common.numhdrs-1; i>0; i--) { // make sure the hash of i-1 matches the prevBlockhash of i } // verify prevntz is valid notarization // verify blockhash of first hdr with prevntz value and height + if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) + return(-1); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) + return(-2);*/ return(0); } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 43cb4f420..d5bc66209 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1068,6 +1068,7 @@ UniValue nspv_spend(const UniValue& params, bool fHelp) if ( NSPV_address.size() == 0 ) throw runtime_error("to nspv_send you need an active nspv_login\n"); satoshis = atof(params[1].get_str().c_str())*COIN + 0.0000000049; + fprintf(stderr,"satoshis.%lld from %.8f\n",(long long)satoshis,atof(params[1].get_str().c_str())); if ( satoshis < 1000 ) throw runtime_error("amount too small\n"); return(NSPV_spend((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); From 50120563347dffe613df9807e409fff21d173a94 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:38:58 -1100 Subject: [PATCH 236/378] Patch --- src/komodo_nSPV.h | 27 ++++++++++++++++ src/komodo_nSPV_fullnode.h | 31 +++--------------- src/komodo_nSPV_wallet.h | 65 ++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 18f85a036..31756881f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -374,6 +374,8 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) memset(ptr,0,sizeof(*ptr)); } +// useful utility functions + uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; @@ -383,4 +385,29 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) return(hash); } +int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) +{ + std::vector rawdata; + rawdata.resize(datalen); + memcpy(&rawdata[0],data,datalen); + if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) + return(0); + else return(-1); +} + +uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +{ + uint256 desttxid; int32_t i; + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + for (i=0; i<32; i++) + ((uint8_t *)blockhashp)[i] = opret[4 + i]; + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + return(desttxid); +} + +int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,int32_t *txidhtp,uint256 *desttxidp,CTransaction tx) +{ + return(0); +} #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index b93e299f1..fb45bd4f5 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -35,27 +35,9 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) return(txid); } -uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) -{ - uint256 desttxid; int32_t i; - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[i]); - //fprintf(stderr," blockhash, "); - //for (i=0; i<4; i++) - // fprintf(stderr,"%02x",opret[32+i]); - //fprintf(stderr," height, "); - iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[36+i]); - //fprintf(stderr," desttxid\n"); - for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; - return(desttxid); -} - int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { - int32_t txidht; Notarisation nota; char *symbol; + int32_t txidht; Notarisation nota; char *symbol; uint256 bhash; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(txids,0,sizeof(*txids)*2); memset(desttxids,0,sizeof(*desttxids)*2); @@ -65,7 +47,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second)); //if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller @@ -80,7 +62,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_extract_desttxid(&ntzheights[1],symbol,E_MARSHAL(ss << nota.second)); + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second)); } return(0); } @@ -196,12 +178,9 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int32_t n) { - CTransaction tx; std::vector rawdata; + CTransaction tx; ptr->retcode = 0; - rawdata.resize(n); - memcpy(&rawdata[0],data,n); - fprintf(stderr,"sendraw.(%s)\n",HexStr(rawdata).c_str()); - if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) + if ( NSPV_txextract(tx,data,n) == 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 5c34bcb14..ac3b22963 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,70 +20,61 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); - - -/*struct NSPV_ntzproofshared -{ - struct NSPV_equihdr *hdrs; - int32_t prevht,nextht,pad32; - uint16_t numhdrs,pad16; -}; - -struct NSPV_ntzsproofresp -{ - struct NSPV_ntzproofshared common; - uint256 prevtxid,nexttxid; - int32_t pad32,prevtxidht,nexttxidht; - uint16_t prevtxlen,nexttxlen; - uint8_t *prevntz,*nextntz; -};*/ - int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { - /* int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; - if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) + int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) return(-1); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) + else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) return(-2); - else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + else if ( tx.GetHash() != ptr->nexttxid ) return(-3); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + return(-4); + else if ( height != ptr->common.nextht ) + return(-5); + else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - // make sure the hash of i-1 matches the prevBlockhash of i + if ( NSPV_doublesha256(&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.prevblockhash ) + return(-i-11); } - // verify prevntz is valid notarization - // verify blockhash of first hdr with prevntz value and height if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-1); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) - return(-2);*/ + return(-6); + else if ( tx.GetHash() != ptr->prevtxid ) + return(-7); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + return(-8); + else if ( height != ptr->common.prevht ) + return(-9); + else if ( NSPV_doublesha256(&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) + return(-10); return(0); } int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { - char *txstr; int32_t retval = 0; + int32_t retval = 0; NSPV_txproof(txid,height); - if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.height != height ) + if ( NSPV_txproofresult.txid != txid ) return(-1); - txstr = (char *)malloc(NSPV_txproofresult.txlen*2 + 1); - init_hexbytes_noT(txstr,NSPV_txproofresult.tx,NSPV_txproofresult.txlen); - if ( !DecodeHexTx(tx,txstr) ) - retval = -1; + if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 ) + retval = -20; else { NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment - if ( NSPV_validatehdrs(&NSPV_ntzsproofresult) == 0 ) + if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { // merkle prove txproof to the merkleroot in the corresponding hdr } } else retval = -1; } - free(txstr); - return(retval); + fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); + return(0); } int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) From 674ebb06cdd0836d31ae06c4625a0e6f1f77ab5c Mon Sep 17 00:00:00 2001 From: TonyL Date: Sun, 7 Jul 2019 11:40:57 +0300 Subject: [PATCH 237/378] nspv client rpc calls unittest --- qa/rpc-tests/src/nspv_client_test.py | 114 +++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 qa/rpc-tests/src/nspv_client_test.py diff --git a/qa/rpc-tests/src/nspv_client_test.py b/qa/rpc-tests/src/nspv_client_test.py new file mode 100644 index 000000000..fbf0e5360 --- /dev/null +++ b/qa/rpc-tests/src/nspv_client_test.py @@ -0,0 +1,114 @@ +import sys +sys.path.append('../../src/tui') + +from lib import tuilib +import unittest + +''' +specify chain ticker (daemon should be up), wif which will be imported and address to which you want to broadcast +added 1 second sleep after each case to surely not face the nSPV server limitation (1 call/second) +''' + +wif = '' +dest_address = 'RMjy5VkHFJkXTJDTJ3XX1zVzukP48sKyva' +amount = '0.1' +chain = 'ILN' + +rpc_proxy = tuilib.def_credentials(chain) + + +class TestNspvClient(unittest.TestCase): + + def test_nspv_getinfo(self): + print("testing nspv_getinfo") + result = rpc_proxy.nspv_getinfo() + self.assertEqual(result["result"], "success") + self.assertGreater(result["height"], 2689) + time.sleep(1) + + def test_nspv_notarizations(self): + print("testing nspv_notarizations") + result = rpc_proxy.nspv_notarizations("2000") + self.assertEqual(result["result"], "success") + self.assertEqual(result["prev"]["notarized_height"], 1998) + self.assertEqual(result["next"]["notarized_height"], 2002) + time.sleep(1) + + def test_nspv_hdrsproof(self): + print("testing nspv_hdrsproof") + result = rpc_proxy.nspv_hdrsproof("2000", "2100") + self.assertEqual(result["result"], "success") + self.assertEqual(result["numhdrs"], 101) + time.sleep(1) + + def test_nspv_login(self): + print("testing nspv_login") + result = rpc_proxy.nspv_login(wif) + self.assertEqual(result["result"], "success") + self.assertEqual(result["status"], "wif will expire in 777 seconds") + time.sleep(1) + + def test_nspv_listunspent(self): + print("testing nspv_listunspent") + result = rpc_proxy.nspv_listunspent() + self.assertEqual(result["result"], "success") + time.sleep(1) + result = rpc_proxy.nspv_listunspent("RQ1mvCUcziWzRwE8Ugtex29VjoFjRzxQJT") + self.assertEqual(result["result"], "error") + + def test_nspv_spend(self): + print("testing nspv_spend") + result = rpc_proxy.nspv_login(wif) + result = rpc_proxy.nspv_spend(dest_address, amount) + self.assertEqual(result["result"], "success") + self.assertEqual(result["vout"][0]["valueZat"], 10000000) + time.sleep(1) + + def test_nspv_broadcast(self): + print("testing nspv_broadcast") + result = rpc_proxy.nspv_login(wif) + broadcast_hex = rpc_proxy.nspv_spend(dest_address, amount)["hex"] + time.sleep(1) + result = rpc_proxy.nspv_broadcast(broadcast_hex) + self.assertEqual(result["result"], "success") + self.assertEqual(result["retcode"], 1) + self.assertEqual(result["expected"], result["broadcast"]) + print("Broadcast txid: " + result["broadcast"]) + time.sleep(1) + + def test_nspv_logout(self): + print("testing nspv_logout") + rpc_proxy.nspv_login(wif) + time.sleep(1) + rpc_proxy.nspv_logout() + time.sleep(1) + result = rpc_proxy.nspv_spend(dest_address, amount) + self.assertEqual(result["result"], "error") + self.assertEqual(result["error"], "wif expired") + time.sleep(1) + + def test_nspv_spentinfo(self): + print("testing nspv_spent_info") + result = rpc_proxy.nspv_spentinfo("67ffe0eaecd6081de04675c492a59090b573ee78955c4e8a85b8ac0be0e8e418", "1") + self.assertEqual(result["result"], "success") + self.assertEqual(result["spentheight"], 2681) + time.sleep(1) + + def test_nspv_txproof(self): + print("testing nspv_txproof") + result = rpc_proxy.nspv_txproof("67ffe0eaecd6081de04675c492a59090b573ee78955c4e8a85b8ac0be0e8e418", "2673") + self.assertEqual(result["txid"], "67ffe0eaecd6081de04675c492a59090b573ee78955c4e8a85b8ac0be0e8e418") + time.sleep(1) + + def test_nspv_login_timout(self): + print("testing auto-logout in 777 seconds") + rpc_proxy.nspv_login(wif) + time.sleep(777) + result = rpc_proxy.nspv_spend(dest_address, amount) + self.assertEqual(result["result"], "error") + self.assertEqual(result["error"], "wif expired") + time.sleep(1) + + +if __name__ == '__main__': + unittest.main() From 60ea15808ae78d6b63fb23af0938c85c87768bba Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:41:03 -1100 Subject: [PATCH 238/378] Void --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 31756881f..c7c8e6d70 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -376,7 +376,7 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) // useful utility functions -uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) +uint256 NSPV_doublesha256(void *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; _hash = bits256_doublesha256(0,data,datalen); From 27d4e82f0cfa908cdd3e2e3c0123c95ec6997406 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:42:31 -1100 Subject: [PATCH 239/378] (uint8_t *) --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_wallet.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c7c8e6d70..31756881f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -376,7 +376,7 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) // useful utility functions -uint256 NSPV_doublesha256(void *data,int32_t datalen) +uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; _hash = bits256_doublesha256(0,data,datalen); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ac3b22963..00b1b27ec 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -33,11 +33,11 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-4); else if ( height != ptr->common.nextht ) return(-5); - else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - if ( NSPV_doublesha256(&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.prevblockhash ) + if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.prevblockhash ) return(-i-11); } if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) @@ -48,7 +48,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-8); else if ( height != ptr->common.prevht ) return(-9); - else if ( NSPV_doublesha256(&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) + else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) return(-10); return(0); } From 0d45abf20f040b9bf7202bb8a3125e173a42a6ad Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:44:01 -1100 Subject: [PATCH 240/378] hashPrevBlock --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 31756881f..c0c45d8e2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -379,7 +379,7 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; - _hash = bits256_doublesha256(0,data,datalen); + _hash = bits256_doublesha256(0,(void *)data,datalen); for (i=0; i<32; i++) ((uint8_t *)&hash)[i] = _hash.bytes[31 - i]; return(hash); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 00b1b27ec..081af7b05 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -37,7 +37,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.prevblockhash ) + if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.hashPrevBlock ) return(-i-11); } if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) From c7f68f34e7be93da3b4946b5fc7de57b06122c0a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:45:19 -1100 Subject: [PATCH 241/378] [I] --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 081af7b05..ab0007571 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -37,7 +37,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.hashPrevBlock ) + if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs[i].hashPrevBlock ) return(-i-11); } if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) From 79ac3e8bac4490c2f890c4868fddf83abe530804 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:46:56 -1100 Subject: [PATCH 242/378] - --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c0c45d8e2..31756881f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -379,7 +379,7 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; - _hash = bits256_doublesha256(0,(void *)data,datalen); + _hash = bits256_doublesha256(0,data,datalen); for (i=0; i<32; i++) ((uint8_t *)&hash)[i] = _hash.bytes[31 - i]; return(hash); From 2d5a19ef66ce5cbc8e73143995d5f40c4302cd1d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:51:18 -1100 Subject: [PATCH 243/378] Test non-not validation --- src/komodo_nSPV_wallet.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ab0007571..d6ef904af 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -31,10 +31,10 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-3); else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) return(-4); - else if ( height != ptr->common.nextht ) - return(-5); - else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) - return(-6); + //else if ( height != ptr->common.nextht ) + // return(-5); + //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + // return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs[i].hashPrevBlock ) @@ -46,10 +46,10 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-7); else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) return(-8); - else if ( height != ptr->common.prevht ) - return(-9); - else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) - return(-10); + //else if ( height != ptr->common.prevht ) + // return(-9); + //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) + // return(-10); return(0); } From 386c1b8ced5ce8b78efcfd89d501c9be0bbd0c93 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 21:55:46 -1100 Subject: [PATCH 244/378] +field --- src/komodo_nSPV_superlite.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index ebafb186c..d404d3f64 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -165,15 +165,18 @@ UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr) return(result); } -UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs) +UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs,int32_t height) { UniValue array(UniValue::VARR); int32_t i; for (i=0; inexttxidht)); result.push_back(Pair("nexttxlen",(int64_t)ptr->prevtxlen)); result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); - result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs))); + result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs,ptr->common.prevht))); return(result); } From cff0f5ea4e098bd03ffc60eb4329ee57be513c6e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:06:05 -1100 Subject: [PATCH 245/378] Print block hash --- src/komodo_nSPV_wallet.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d6ef904af..3a9b2b5cc 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -37,7 +37,9 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) // return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs[i].hashPrevBlock ) + blockhash = NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)); + fprintf(stderr,"i.%d ht.%d blockhash.%s vs [i+1].prev %s\n",i-1,ptr->common.prevht+i-1,blockhash.GetHex().c_str(),ptr->common.hdrs[i].hashPrevBlock.GetHex().c_str()); + if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) return(-i-11); } if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) From b3a982b391f84dd8931bdd6905e32e77bf487fb4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:11:24 -1100 Subject: [PATCH 246/378] Test --- src/komodo_nSPV_wallet.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 3a9b2b5cc..15ac01cd0 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -23,6 +23,7 @@ extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + fprintf(stderr,"NSPV_validatehdrs.%d\n",ptr->common.numhdrs); if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) return(-1); else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) @@ -35,13 +36,15 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) // return(-5); //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) // return(-6); + fprintf(stderr,"numhdrs.%d\n",ptr->common.numhdrs); for (i=ptr->common.numhdrs-1; i>0; i--) { - blockhash = NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)); + blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); fprintf(stderr,"i.%d ht.%d blockhash.%s vs [i+1].prev %s\n",i-1,ptr->common.prevht+i-1,blockhash.GetHex().c_str(),ptr->common.hdrs[i].hashPrevBlock.GetHex().c_str()); if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) return(-i-11); } + fprintf(stderr,"i.%d\n",i); if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) return(-6); else if ( tx.GetHash() != ptr->prevtxid ) From 1be923d1cfb6c618a759fb98adbfb37185e3a71c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:15:22 -1100 Subject: [PATCH 247/378] NSPV_hdrhash --- src/komodo_nSPV.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 31756881f..2bc3dacc3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -385,6 +385,13 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) return(hash); } +uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) +{ + uint8_t data[sizeof(*hdr)]; uint16_t hdrsize = sizeof(*hdr); + iguana_rwequihdrvec(1,data,&hdrsize,&hdr) + return(NSPV_doublesha256(data,sizeof(*hdr))); +} + int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) { std::vector rawdata; From 5e05f1d467e3729cb5a7dddebc25a1f42e53ca0f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:16:13 -1100 Subject: [PATCH 248/378] ; --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 2bc3dacc3..4250c948c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -388,7 +388,7 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { uint8_t data[sizeof(*hdr)]; uint16_t hdrsize = sizeof(*hdr); - iguana_rwequihdrvec(1,data,&hdrsize,&hdr) + iguana_rwequihdrvec(1,data,&hdrsize,&hdr); return(NSPV_doublesha256(data,sizeof(*hdr))); } From 52ade7db42e8383741ef8ceb3e09ef4031fc5e0b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:25:13 -1100 Subject: [PATCH 249/378] Test --- src/komodo_nSPV_fullnode.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index fb45bd4f5..bae0870ce 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -264,9 +264,11 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->common.nextht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); - //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); + fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { + hashBlock = NSPV_hdrhash(&ptr->common.hdrs[i]); + fprintf(stderr,"hdr[%d] %s\n",prevht+i,hashBlock.GetHex().c_str()); if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { fprintf(stderr,"error setting hdr.%d\n",prevht+i); @@ -279,7 +281,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); - //fprintf(stderr,"prevtxlen.%d nexttxlen.%d size %ld -> %ld\n",ptr->prevtxlen,ptr->nexttxlen,sizeof(*ptr),sizeof(*ptr) - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevlen + ptr->nextlen); + fprintf(stderr,"ptr->prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); } From 5679aa0247b04b8d2f58bd32a51c415429ea72dd Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:42:08 -1100 Subject: [PATCH 250/378] Redo block hash --- src/komodo_nSPV.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4250c948c..a0bebdb19 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,9 +387,16 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - uint8_t data[sizeof(*hdr)]; uint16_t hdrsize = sizeof(*hdr); - iguana_rwequihdrvec(1,data,&hdrsize,&hdr); - return(NSPV_doublesha256(data,sizeof(*hdr))); + CBlockIndex block; + block.nVersion = hdr->nVersion; + block.hashPrevBlock = hdr->hashPrevBlock; + block.hashMerkleRoot = hdr->hashMerkleRoot; + block.hashFinalSaplingRoot = hdr->hashFinalSaplingRoot; + block.nTime = hdr->nTime; + block.nBits = hdr->nBits; + block.nNonce = hdr->nNonce; + memcpy(block.nSolution,hdr->nSolution,sizeof(hdr->nSolution)); + return(block.GetHash()); } int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) From f0038e1d009dffa908de295d5a73720161dceddf Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:45:40 -1100 Subject: [PATCH 251/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a0bebdb19..8bf0d7a37 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,7 +387,7 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - CBlockIndex block; + CBlockHeader block; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; block.hashMerkleRoot = hdr->hashMerkleRoot; From 27cb736a2f8b89b11afc06ae826ed49d353a2217 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:46:58 -1100 Subject: [PATCH 252/378] &[0] --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 8bf0d7a37..1d94dddd7 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -395,7 +395,7 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nTime = hdr->nTime; block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; - memcpy(block.nSolution,hdr->nSolution,sizeof(hdr->nSolution)); + memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); return(block.GetHash()); } From 6994f4195169f3d5f103061b3785209f1cd05a9a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:51:08 -1100 Subject: [PATCH 253/378] +print --- src/komodo_nSPV.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1d94dddd7..74e64d83d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,7 +387,7 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - CBlockHeader block; + CBlockHeader block; uint256 blockhash; uint8_t data[sizeof(*hdr)]; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; block.hashMerkleRoot = hdr->hashMerkleRoot; @@ -396,6 +396,8 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); + blockhash = NSPV_doublesha256(data,sizeof(*hdr)); + fprintf(stderr,"blockhash.%s vs %s\n",blockhash.GetHex().c_str(),block.GetHash().GetHex().c_str()); return(block.GetHash()); } From 00aad8e85350dfc75c77c1bcaa893fcd30e7fbfb Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:53:42 -1100 Subject: [PATCH 254/378] Use function --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index d404d3f64..745f4e85e 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -172,7 +172,7 @@ UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs,int32_t hei { UniValue item(UniValue::VOBJ); item.push_back(Pair("height",(int64_t)height+i)); - item.push_back(Pair("blockhash",NSPV_doublesha256((uint8_t *)&hdrs[i],sizeof(hdrs[i])).GetHex())); + item.push_back(Pair("blockhash",NSPV_hdrhash(&hdrs[i]))); item.push_back(Pair("hashPrevBlock",hdrs[i].hashPrevBlock.GetHex())); item.push_back(Pair("hashMerkleRoot",hdrs[i].hashMerkleRoot.GetHex())); item.push_back(Pair("nTime",(int64_t)hdrs[i].nTime)); From 0bc975c33afeedbbff3472f85a2ff6f057b69052 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:54:40 -1100 Subject: [PATCH 255/378] Gather --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 745f4e85e..21d0c1bc0 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -172,7 +172,7 @@ UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs,int32_t hei { UniValue item(UniValue::VOBJ); item.push_back(Pair("height",(int64_t)height+i)); - item.push_back(Pair("blockhash",NSPV_hdrhash(&hdrs[i]))); + item.push_back(Pair("blockhash",NSPV_hdrhash(&hdrs[i]).GetHex())); item.push_back(Pair("hashPrevBlock",hdrs[i].hashPrevBlock.GetHex())); item.push_back(Pair("hashMerkleRoot",hdrs[i].hashMerkleRoot.GetHex())); item.push_back(Pair("nTime",(int64_t)hdrs[i].nTime)); From a84ce71083dec32c68976e1fed3bb6fdd5658397 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 22:57:50 -1100 Subject: [PATCH 256/378] Simplify --- src/komodo_nSPV.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 74e64d83d..40b18f064 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,7 +387,8 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - CBlockHeader block; uint256 blockhash; uint8_t data[sizeof(*hdr)]; + uint256 blockhash; uint8_t data[sizeof(*hdr)]; uint16_t datalen = sizeof(*hdr); + /*CBlockHeader block; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; block.hashMerkleRoot = hdr->hashMerkleRoot; @@ -395,10 +396,11 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nTime = hdr->nTime; block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; - memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); + memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution));*/ + iguana_rwequihdrvec(1,data,&datalen,&hdr); blockhash = NSPV_doublesha256(data,sizeof(*hdr)); - fprintf(stderr,"blockhash.%s vs %s\n",blockhash.GetHex().c_str(),block.GetHash().GetHex().c_str()); - return(block.GetHash()); + return(blockhash); + //return(block.GetHash()); } int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) From c80721db9c5e5f5545ff046c6d50a056e2667420 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:01:51 -1100 Subject: [PATCH 257/378] Use proper functions --- src/komodo_nSPV.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 40b18f064..2b98bb313 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,7 +387,7 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - uint256 blockhash; uint8_t data[sizeof(*hdr)]; uint16_t datalen = sizeof(*hdr); + uint256 blockhash; uint8_t data[sizeof(*hdr)]; /*CBlockHeader block; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; @@ -397,8 +397,8 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution));*/ - iguana_rwequihdrvec(1,data,&datalen,&hdr); - blockhash = NSPV_doublesha256(data,sizeof(*hdr)); + n = NSPV_rwequihdr(1,data,hdr); + blockhash = NSPV_doublesha256(data,sizeof(data)); return(blockhash); //return(block.GetHash()); } From 295f6a04d65d60eaa66f2c12c4b2b56a872865e9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:02:45 -1100 Subject: [PATCH 258/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 2b98bb313..b9f86cc83 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -397,7 +397,7 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution));*/ - n = NSPV_rwequihdr(1,data,hdr); + NSPV_rwequihdr(1,data,hdr); blockhash = NSPV_doublesha256(data,sizeof(data)); return(blockhash); //return(block.GetHash()); From caaedc37ced6ad09a93db7dfe7c88105112e4685 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:05:22 -1100 Subject: [PATCH 259/378] block.hash --- src/komodo_nSPV.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b9f86cc83..46db622c3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -388,7 +388,7 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { uint256 blockhash; uint8_t data[sizeof(*hdr)]; - /*CBlockHeader block; + CBlockHeader block; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; block.hashMerkleRoot = hdr->hashMerkleRoot; @@ -396,11 +396,12 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nTime = hdr->nTime; block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; - memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution));*/ + memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); NSPV_rwequihdr(1,data,hdr); blockhash = NSPV_doublesha256(data,sizeof(data)); - return(blockhash); - //return(block.GetHash()); + fprintf(stderr,"%s vs block %s\n",blockhash.GetHex().c_str(),block.GetHash().GetHex().c_str()); + //return(blockhash); + return(block.GetHash()); } int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) From a31beda7771db9b8f1fef0afa8e2398d75df91a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:09:30 -1100 Subject: [PATCH 260/378] Alloc space --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 46db622c3..47976f1be 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -396,6 +396,7 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nTime = hdr->nTime; block.nBits = hdr->nBits; block.nNonce = hdr->nNonce; + block.nSolution.resize(sizeof(hdr->nSolution)); memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); NSPV_rwequihdr(1,data,hdr); blockhash = NSPV_doublesha256(data,sizeof(data)); From 076ee43baeddca07abfb59de67b10f9519d9446c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:13:08 -1100 Subject: [PATCH 261/378] -print --- src/komodo_nSPV.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 47976f1be..9d00d0ba6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -387,7 +387,6 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) { - uint256 blockhash; uint8_t data[sizeof(*hdr)]; CBlockHeader block; block.nVersion = hdr->nVersion; block.hashPrevBlock = hdr->hashPrevBlock; @@ -398,10 +397,6 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) block.nNonce = hdr->nNonce; block.nSolution.resize(sizeof(hdr->nSolution)); memcpy(&block.nSolution[0],hdr->nSolution,sizeof(hdr->nSolution)); - NSPV_rwequihdr(1,data,hdr); - blockhash = NSPV_doublesha256(data,sizeof(data)); - fprintf(stderr,"%s vs block %s\n",blockhash.GetHex().c_str(),block.GetHash().GetHex().c_str()); - //return(blockhash); return(block.GetHash()); } From b29d20a9b421df3db3af2278e81cad9af1a44436 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:15:58 -1100 Subject: [PATCH 262/378] Test --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 21d0c1bc0..597ed3800 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -460,7 +460,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_NTZSRESP: NSPV_ntzsresp_purge(&NSPV_ntzsresult); NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); - fprintf(stderr,"got ntzs response %u size.%d\n",timestamp,(int32_t)response.size()); + fprintf(stderr,"got ntzs response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); break; case NSPV_NTZSPROOFRESP: NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 15ac01cd0..1065224c9 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -71,6 +71,7 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { + fprintf(stderr,"gettx prev.%d next.%d\n",NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { From ec8c41e79259c435fcbe0a5c8eacd4bb522ab59d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:28:23 -1100 Subject: [PATCH 263/378] Test --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 597ed3800..97c0dc1c2 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -470,7 +470,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_TXPROOFRESP: NSPV_txproof_purge(&NSPV_txproofresult); NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); - fprintf(stderr,"got txproof response %u size.%d\n",timestamp,(int32_t)response.size()); + fprintf(stderr,"got txproof response %u size.%d %s ht.%d\n",timestamp,(int32_t)response.size(),NSPV_txproofresult.txid.GetHex().c_str(),NSPV_txproofresult.height); break; case NSPV_SPENTINFORESP: NSPV_spentinfo_purge(&NSPV_spentresult); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 1065224c9..8559293c4 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -23,7 +23,6 @@ extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; - fprintf(stderr,"NSPV_validatehdrs.%d\n",ptr->common.numhdrs); if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) return(-1); else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) @@ -40,11 +39,9 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) for (i=ptr->common.numhdrs-1; i>0; i--) { blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); - fprintf(stderr,"i.%d ht.%d blockhash.%s vs [i+1].prev %s\n",i-1,ptr->common.prevht+i-1,blockhash.GetHex().c_str(),ptr->common.hdrs[i].hashPrevBlock.GetHex().c_str()); if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) return(-i-11); } - fprintf(stderr,"i.%d\n",i); if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) return(-6); else if ( tx.GetHash() != ptr->prevtxid ) @@ -71,12 +68,15 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { - fprintf(stderr,"gettx prev.%d next.%d\n",NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); - NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment - if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) + fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); + if ( NSPV_ntzsresult.prevntz.height <= height && height <= NSPV_ntzsresult.nextntz.height ) { - // merkle prove txproof to the merkleroot in the corresponding hdr - } + NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment + if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) + { + // merkle prove txproof to the merkleroot in the corresponding hdr + } + } else retval = -22; } else retval = -1; } fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); From f508845b1af987c1418387e1d5a90ed2e18b58a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:35:32 -1100 Subject: [PATCH 264/378] -prints --- src/komodo_nSPV_fullnode.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index bae0870ce..d54cec36e 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -264,11 +264,11 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->common.nextht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); - fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); + //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { hashBlock = NSPV_hdrhash(&ptr->common.hdrs[i]); - fprintf(stderr,"hdr[%d] %s\n",prevht+i,hashBlock.GetHex().c_str()); + //fprintf(stderr,"hdr[%d] %s\n",prevht+i,hashBlock.GetHex().c_str()); if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { fprintf(stderr,"error setting hdr.%d\n",prevht+i); @@ -281,7 +281,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); - fprintf(stderr,"ptr->prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); + //fprintf(stderr,"ptr->prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); } From 23b5c8f4289e8067ffae6be08b573a3f4ad29d41 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:41:17 -1100 Subject: [PATCH 265/378] Bracket --- src/komodo_nSPV_wallet.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 8559293c4..ba2f66464 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -35,7 +35,6 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) // return(-5); //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) // return(-6); - fprintf(stderr,"numhdrs.%d\n",ptr->common.numhdrs); for (i=ptr->common.numhdrs-1; i>0; i--) { blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); @@ -66,6 +65,13 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) else { NSPV_notarizations(height); // gets the prev and next notarizations + if ( NSPV_ntzsresult.prevntz.height == 0 ) + { + fprintf(stderr,"issue manual bracket\n"); + NSPV_notarizations(height-1); + NSPV_notarizations(height+1); + NSPV_notarizations(height); // gets the prev and next notarizations + } if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); From 49ec4ca2a356f9749d3ea009c7af3983395b345c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Jul 2019 23:43:36 -1100 Subject: [PATCH 266/378] Fix --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ba2f66464..d5702f816 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -65,7 +65,7 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) else { NSPV_notarizations(height); // gets the prev and next notarizations - if ( NSPV_ntzsresult.prevntz.height == 0 ) + if ( NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height ) { fprintf(stderr,"issue manual bracket\n"); NSPV_notarizations(height-1); From 253223e58e224600bc7a3ef1aa3e6eb669600915 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:07:16 -1100 Subject: [PATCH 267/378] Calc merkle proofroot --- src/komodo_nSPV_wallet.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d5702f816..6bd669a48 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -56,11 +56,11 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { - int32_t retval = 0; + int32_t offset,retval = 0; NSPV_txproof(txid,height); if ( NSPV_txproofresult.txid != txid ) return(-1); - if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 ) + if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; else { @@ -75,12 +75,22 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); - if ( NSPV_ntzsresult.prevntz.height <= height && height <= NSPV_ntzsresult.nextntz.height ) + offset = (height - NSPV_ntzsresult.prevntz.height); + if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { - NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment + NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { - // merkle prove txproof to the merkleroot in the corresponding hdr + std::vector txids; std::vector proof; + proof.resize(NSPV_txproofresult.txlen); + memcpy(&proof[0],NSPV_txproofresult.tx,NSPV_txproofresult.txlen); + txids.push_back(txid); + proofroot = BitcoinGetProofMerkleRoot(proof,txids); + if ( proofroot != NSPV_ntzsproofresult.common[offset].hashMerkleRoot ) + { + fprintf(stderr,"proofroot.%s vs %s\n",proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common[offset].hashMerkleRoot.GetHex().c_str()); + return(-23); + } } } else retval = -22; } else retval = -1; From 129039d685caba2974b7ad6906eda09ffa6f4549 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:08:40 -1100 Subject: [PATCH 268/378] Syntax --- src/komodo_nSPV_wallet.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 6bd669a48..c54bfd894 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -81,14 +81,14 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { - std::vector txids; std::vector proof; + std::vector txids; std::vector proof; uint256 proofroot; proof.resize(NSPV_txproofresult.txlen); memcpy(&proof[0],NSPV_txproofresult.tx,NSPV_txproofresult.txlen); txids.push_back(txid); proofroot = BitcoinGetProofMerkleRoot(proof,txids); - if ( proofroot != NSPV_ntzsproofresult.common[offset].hashMerkleRoot ) + if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) { - fprintf(stderr,"proofroot.%s vs %s\n",proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common[offset].hashMerkleRoot.GetHex().c_str()); + fprintf(stderr,"proofroot.%s vs %s\n",proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); return(-23); } } From 2498cdfc88e827be9dd8dbc18595ac1e3636bcb0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:14:11 -1100 Subject: [PATCH 269/378] Fix --- src/komodo_nSPV_wallet.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index c54bfd894..346c1d678 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -82,14 +82,13 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { std::vector txids; std::vector proof; uint256 proofroot; - proof.resize(NSPV_txproofresult.txlen); - memcpy(&proof[0],NSPV_txproofresult.tx,NSPV_txproofresult.txlen); - txids.push_back(txid); + proof.resize(NSPV_txproofresult.txprooflen); + memcpy(&proof[0],NSPV_txproofresult.tx,NSPV_txproofresult.txprooflen); proofroot = BitcoinGetProofMerkleRoot(proof,txids); if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) { - fprintf(stderr,"proofroot.%s vs %s\n",proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); - return(-23); + fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); + retval = -23; } } } else retval = -22; From 01fe40136f000fafd712575504be7c09e4ed48c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:37:07 -1100 Subject: [PATCH 270/378] Initial notarisation validation --- src/komodo_nSPV.h | 20 ++++++++++++++++++-- src/komodo_nSPV_wallet.h | 20 ++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9d00d0ba6..87d8cb865 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -421,8 +421,24 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: return(desttxid); } -int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,int32_t *txidhtp,uint256 *desttxidp,CTransaction tx) +int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elected[64][33]) { - return(0); + int32_t numsigs = 0; + return(numsigs); +} + +int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,uint256 *desttxidp,CTransaction tx,int32_t ntzheight) +{ + int32_t numsigs; uint8_t elected[64][33],sigkeys[64][33]; char *symbol; std::vector opret; + if ( tx.vout.size() >= 2 ) + { + symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; + komodo_notaries(pubkeys,ntzheight,0); + numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); + GetOpReturnData(tx.vout[1].scriptPubKey,opret); + *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); + *txidp = tx.GetHash(); + return(0); + } else return(-1); } #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 346c1d678..4809aae81 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -29,12 +29,12 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-2); else if ( tx.GetHash() != ptr->nexttxid ) return(-3); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.nextht) < 0 ) return(-4); - //else if ( height != ptr->common.nextht ) - // return(-5); - //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) - // return(-6); + else if ( height != ptr->common.nextht ) + return(-5); + else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) + return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); @@ -45,12 +45,12 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-6); else if ( tx.GetHash() != ptr->prevtxid ) return(-7); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.prevht) < 0 ) return(-8); - //else if ( height != ptr->common.prevht ) - // return(-9); - //else if ( NSPV_doublesha256((uint8_t *)&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) - // return(-10); + else if ( height != ptr->common.prevht ) + return(-9); + else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) + return(-10); return(0); } From 5a3e0afd42fa767114b83a0051787fd1fe6b4ad3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:38:21 -1100 Subject: [PATCH 271/378] Syntax --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 87d8cb865..74a1b3866 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -433,7 +433,7 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t if ( tx.vout.size() >= 2 ) { symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - komodo_notaries(pubkeys,ntzheight,0); + komodo_notaries(elected,ntzheight,0); numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); From 2869910d2b6f0cf3fcf9ff93c362663f060665b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:42:51 -1100 Subject: [PATCH 272/378] +print --- src/komodo_nSPV_wallet.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 4809aae81..73a3acdca 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -34,23 +34,26 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) else if ( height != ptr->common.nextht ) return(-5); else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) + { + fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1].GetHex().c_str(),blockhash.GetHex().c_str())); return(-6); + } for (i=ptr->common.numhdrs-1; i>0; i--) { blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) - return(-i-11); + return(-i-12); } if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-6); - else if ( tx.GetHash() != ptr->prevtxid ) return(-7); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.prevht) < 0 ) + else if ( tx.GetHash() != ptr->prevtxid ) return(-8); - else if ( height != ptr->common.prevht ) + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.prevht) < 0 ) return(-9); - else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) + else if ( height != ptr->common.prevht ) return(-10); + else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) + return(-11); return(0); } From cb388ff8b1573129f97e12a1596a08fea129d1c9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:45:38 -1100 Subject: [PATCH 273/378] Test --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 73a3acdca..5b8710c56 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -35,7 +35,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-5); else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) { - fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1].GetHex().c_str(),blockhash.GetHex().c_str())); + fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str())); return(-6); } for (i=ptr->common.numhdrs-1; i>0; i--) From 8c0f0c8ebf0e0fda0b23652903ac37f4f1407430 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:52:17 -1100 Subject: [PATCH 274/378] -) --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 5b8710c56..4a864c3c2 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -35,7 +35,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-5); else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) { - fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str())); + fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str()); return(-6); } for (i=ptr->common.numhdrs-1; i>0; i--) From b6b6e45f0233001329690e0bb85f8a28b885c6b6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 00:58:08 -1100 Subject: [PATCH 275/378] +print --- src/komodo_nSPV.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 74a1b3866..3b1182440 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -436,7 +436,9 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t komodo_notaries(elected,ntzheight,0); numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); + if ( opret.size() >= 32*2+4*2 ) + *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,&opret[4]); + fprintf(stderr,"ntzht.%d %s txid.%s\n",*heightp,*blockhashp.GetHex().c_str(),*desttxidp.GetHex().c_str()); *txidp = tx.GetHash(); return(0); } else return(-1); From e1f53fb668fca90a20e6d22a7314b8f547c90274 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:00:38 -1100 Subject: [PATCH 276/378] Offset --- src/komodo_nSPV.h | 10 +++++----- src/komodo_nSPV_fullnode.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3b1182440..c339e78df 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -410,14 +410,14 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) else return(-1); } -uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret,int32_t offset) { uint256 desttxid; int32_t i; - iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + iguana_rwnum(0,&opret[32+offset],sizeof(*heightp),heightp); for (i=0; i<32; i++) - ((uint8_t *)blockhashp)[i] = opret[4 + i]; + ((uint8_t *)blockhashp)[i] = opret[4 + i + offset]; for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i + offset]; return(desttxid); } @@ -437,7 +437,7 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) - *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,&opret[4]); + *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,4); fprintf(stderr,"ntzht.%d %s txid.%s\n",*heightp,*blockhashp.GetHex().c_str(),*desttxidp.GetHex().c_str()); *txidp = tx.GetHash(); return(0); diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index d54cec36e..cce334e8d 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -47,7 +47,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second)); + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second),0); //if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller @@ -62,7 +62,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second)); + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second),0); } return(0); } From 03fe4d6627efc6fdbb57fbbc3f74a04ddd8d017b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:01:57 -1100 Subject: [PATCH 277/378] () --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c339e78df..3aa661738 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -438,7 +438,7 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,4); - fprintf(stderr,"ntzht.%d %s txid.%s\n",*heightp,*blockhashp.GetHex().c_str(),*desttxidp.GetHex().c_str()); + fprintf(stderr,"ntzht.%d %s txid.%s\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str()); *txidp = tx.GetHash(); return(0); } else return(-1); From 2f4ff0b103aa24b726c24b63943968073dd46c7f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:07:20 -1100 Subject: [PATCH 278/378] Print --- src/komodo_nSPV.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3aa661738..ab358f5d6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -438,7 +438,12 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,4); - fprintf(stderr,"ntzht.%d %s txid.%s\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str()); + { + int z; + for (z=0; z<68; z++) + fprintf(stderr,"%02x",opret[z]); + } + fprintf(stderr," ntzht.%d %s txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str(),(int32_t)opret.size()); *txidp = tx.GetHash(); return(0); } else return(-1); From 03ce22bf093a9c549cb6faf82f6449b8d689d0c8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:15:14 -1100 Subject: [PATCH 279/378] Tweak --- src/komodo_nSPV.h | 35 +++++++++++++++++++++++------------ src/komodo_nSPV_fullnode.h | 4 ++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index ab358f5d6..d77f97d0c 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -410,23 +410,34 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) else return(-1); } -uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret,int32_t offset) -{ - uint256 desttxid; int32_t i; - iguana_rwnum(0,&opret[32+offset],sizeof(*heightp),heightp); - for (i=0; i<32; i++) - ((uint8_t *)blockhashp)[i] = opret[4 + i + offset]; - for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i + offset]; - return(desttxid); -} - int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elected[64][33]) { int32_t numsigs = 0; return(numsigs); } +uint256 NSPV_DBopretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +{ + uint256 desttxid; int32_t i; + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + for (i=0; i<32; i++) + ((uint8_t *)blockhashp)[i] = opret[i]; + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + return(desttxid); +} + +uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +{ + uint256 desttxid; int32_t i; + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + for (i=0; i<32; i++) + ((uint8_t *)blockhashp)[i] = opret[i]; + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + return(desttxid); +} + int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,uint256 *desttxidp,CTransaction tx,int32_t ntzheight) { int32_t numsigs; uint8_t elected[64][33],sigkeys[64][33]; char *symbol; std::vector opret; @@ -437,7 +448,7 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) - *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,4); + *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); { int z; for (z=0; z<68; z++) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index cce334e8d..01e07cb07 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -47,7 +47,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second),0); + desttxids[0] = NSPV_DBopretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second)); //if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller @@ -62,7 +62,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second),0); + desttxids[1] = NSPV_DBopretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second)); } return(0); } From 80e9cec7bc23e3c9880aef3ae07c365a1a367676 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:31:29 -1100 Subject: [PATCH 280/378] Tweak --- src/komodo_nSPV.h | 15 ++------------- src/komodo_nSPV_fullnode.h | 11 ++++++----- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d77f97d0c..f1f0cc82b 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -416,17 +416,6 @@ int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elec return(numsigs); } -uint256 NSPV_DBopretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) -{ - uint256 desttxid; int32_t i; - iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); - for (i=0; i<32; i++) - ((uint8_t *)blockhashp)[i] = opret[i]; - for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; - return(desttxid); -} - uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) { uint256 desttxid; int32_t i; @@ -449,11 +438,11 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); - { + /*{ int z; for (z=0; z<68; z++) fprintf(stderr,"%02x",opret[z]); - } + }*/ fprintf(stderr," ntzht.%d %s txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str(),(int32_t)opret.size()); *txidp = tx.GetHash(); return(0); diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 01e07cb07..579d02fa4 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -37,7 +37,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { - int32_t txidht; Notarisation nota; char *symbol; uint256 bhash; + int32_t txidht; Notarisation nota; char *symbol; uint256 bhash0,bhash1; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(txids,0,sizeof(*txids)*2); memset(desttxids,0,sizeof(*desttxids)*2); @@ -47,8 +47,8 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_DBopretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second)); - //if ( height != 2668 ) + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); + /*//if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller { @@ -57,13 +57,14 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des ntzheights[1] = ntzheights[0]; desttxids[1] = desttxids[0]; return(0); - } + }*/ if ( (txidht= ScanNotarisationsDB2(height,symbol,1440,nota)) != 0 ) { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_DBopretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second)); + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); } + fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); return(0); } From a043cef2fc6e83281dfb885ced54531aa7790353 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:43:03 -1100 Subject: [PATCH 281/378] Re-query ntz tx --- src/komodo_nSPV_fullnode.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 579d02fa4..2608ad3f5 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -37,7 +37,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { - int32_t txidht; Notarisation nota; char *symbol; uint256 bhash0,bhash1; + int32_t txidht; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; uint256 bhash0,bhash1,hashBlock; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(txids,0,sizeof(*txids)*2); memset(desttxids,0,sizeof(*desttxids)*2); @@ -47,8 +47,14 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); - /*//if ( height != 2668 ) + if ( !GetTransaction(txids[0],tx,hashBlock,false) || tx.vout.size() < 2 ) + return(-2); + GetOpReturnData(tx.vout[1].scriptPubKey,opret); + if ( opret.size() >= 32*2+4*2 ) + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,opret); + + /*desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); + //if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller { @@ -62,7 +68,12 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); + if ( !GetTransaction(txids[1],tx,hashBlock,false) || tx.vout.size() < 2 ) + return(-2); + GetOpReturnData(tx.vout[1].scriptPubKey,opret); + if ( opret.size() >= 32*2+4*2 ) + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret); + //desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); } fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); return(0); From 7cd2faf561401710a500227e17b24c9a1d9705d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:43:43 -1100 Subject: [PATCH 282/378] -print --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_fullnode.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index f1f0cc82b..27c015ea3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -443,7 +443,7 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t for (z=0; z<68; z++) fprintf(stderr,"%02x",opret[z]); }*/ - fprintf(stderr," ntzht.%d %s txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str(),(int32_t)opret.size()); + //fprintf(stderr," ntzht.%d %s txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str(),(int32_t)opret.size()); *txidp = tx.GetHash(); return(0); } else return(-1); diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 2608ad3f5..615084aff 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -75,7 +75,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret); //desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); } - fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); + //fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); return(0); } From 27111cb1a38d491910409d08351c480a8c824a1f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:50:13 -1100 Subject: [PATCH 283/378] Print --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_fullnode.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 27c015ea3..4da0df07e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -443,8 +443,8 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t for (z=0; z<68; z++) fprintf(stderr,"%02x",opret[z]); }*/ - //fprintf(stderr," ntzht.%d %s txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*desttxidp).GetHex().c_str(),(int32_t)opret.size()); *txidp = tx.GetHash(); + fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*txidp).GetHex().c_str(),(int32_t)opret.size()); return(0); } else return(-1); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 615084aff..97d2824d3 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -50,7 +50,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des if ( !GetTransaction(txids[0],tx,hashBlock,false) || tx.vout.size() < 2 ) return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - if ( opret.size() >= 32*2+4*2 ) + if ( opret.size() >= 32*2+4 ) desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,opret); /*desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); @@ -71,7 +71,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des if ( !GetTransaction(txids[1],tx,hashBlock,false) || tx.vout.size() < 2 ) return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - if ( opret.size() >= 32*2+4*2 ) + if ( opret.size() >= 32*2+4 ) desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret); //desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); } From 24c33299169ccd9dd7c20116691d7a1c76f374d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:55:27 -1100 Subject: [PATCH 284/378] Test --- src/komodo_nSPV.h | 6 +++--- src/komodo_nSPV_fullnode.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4da0df07e..c7e5226d5 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -416,7 +416,7 @@ int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elec return(numsigs); } -uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret,uint256 txid) { uint256 desttxid; int32_t i; iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); @@ -424,6 +424,7 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: ((uint8_t *)blockhashp)[i] = opret[i]; for (i=0; i<32; i++) ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(txid).GetHex().c_str(),(int32_t)opret.size()); return(desttxid); } @@ -437,14 +438,13 @@ int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *t numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) - *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret); + *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,tx.GetHash()); /*{ int z; for (z=0; z<68; z++) fprintf(stderr,"%02x",opret[z]); }*/ *txidp = tx.GetHash(); - fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(*txidp).GetHex().c_str(),(int32_t)opret.size()); return(0); } else return(-1); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 97d2824d3..f8c753bee 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -50,8 +50,8 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des if ( !GetTransaction(txids[0],tx,hashBlock,false) || tx.vout.size() < 2 ) return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - if ( opret.size() >= 32*2+4 ) - desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,opret); + //if ( opret.size() >= 32*2+4 ) + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,opret,txids[0]); /*desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); //if ( height != 2668 ) @@ -71,8 +71,8 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des if ( !GetTransaction(txids[1],tx,hashBlock,false) || tx.vout.size() < 2 ) return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - if ( opret.size() >= 32*2+4 ) - desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret); + //if ( opret.size() >= 32*2+4 ) + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret,txids[1]); //desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); } //fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); From 7143b2a0e3fb18a22003763a3d0fdb134893a689 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 01:57:47 -1100 Subject: [PATCH 285/378] Guard --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c7e5226d5..4dc6bc324 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -424,6 +424,7 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: ((uint8_t *)blockhashp)[i] = opret[i]; for (i=0; i<32; i++) ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + if ( *heightp != 2690 ) fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(txid).GetHex().c_str(),(int32_t)opret.size()); return(desttxid); } From 269c9120725fbd623b5ee7e61d6d227f4a20248e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 02:04:13 -1100 Subject: [PATCH 286/378] +print --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 97c0dc1c2..456ba6385 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -460,7 +460,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r case NSPV_NTZSRESP: NSPV_ntzsresp_purge(&NSPV_ntzsresult); NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); - fprintf(stderr,"got ntzs response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); + fprintf(stderr,"got ntzs response %u size.%d %s prev.%d, %s next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.height); break; case NSPV_NTZSPROOFRESP: NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); From 88617145c60cc97b9415705115411a76d10bf264 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 03:39:03 -1100 Subject: [PATCH 287/378] Change hdrsproof to taxied based, add unspentvalue to taproot --- src/cc/CCinclude.h | 2 +- src/cc/CCtx.cpp | 2 +- src/komodo_nSPV.h | 26 ++++++------ src/komodo_nSPV_fullnode.h | 81 +++++++++++++++++++++---------------- src/komodo_nSPV_superlite.h | 23 +++++++---- src/komodo_nSPV_wallet.h | 6 +-- src/wallet/rpcdump.cpp | 4 +- 7 files changed, 83 insertions(+), 61 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 85ff380ca..c4ef6c52c 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -174,7 +174,7 @@ static int32_t ignorevin; bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); int32_t is_hexstr(char *str,int32_t n); bool myAddtomempool(CTransaction &tx, CValidationState *pstate = NULL, bool fSkipExpiry = false); -int32_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag); +int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag); bool myIsutxo_spentinmempool(uint256 &spenttxid,int32_t &spentvini,uint256 txid,int32_t vout); bool mytxid_inmempool(uint256 txid); int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index b51342be7..a3624fd67 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -365,7 +365,7 @@ int64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout,int32_t CCf return(0); } -int32_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag) +int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag) { CCoins coins; //fprintf(stderr,"CCgettxoud %s/v%d\n",txid.GetHex().c_str(),vout); diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4dc6bc324..c2fd7b40d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -240,7 +240,8 @@ void NSPV_inforesp_purge(struct NSPV_inforesp *ptr) struct NSPV_txproof { uint256 txid; - int32_t height; + int64_t unspentvalue; + int32_t height,vout; uint16_t txlen,txprooflen; uint8_t *tx,*txproof; }; @@ -249,7 +250,9 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p { int32_t len = 0; len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->txid),(uint8_t *)&ptr->txid); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->unspentvalue),&ptr->unspentvalue); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); return(len); @@ -412,7 +415,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elected[64][33]) { - int32_t numsigs = 0; + int32_t numsigs = 13; return(numsigs); } @@ -429,24 +432,21 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: return(desttxid); } -int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,uint256 *desttxidp,CTransaction tx,int32_t ntzheight) +int32_t NSPV_notarizationextract(int32_t *ntzheightp,uint256 *blockhashp,uint256 *desttxidp,CTransaction tx) { int32_t numsigs; uint8_t elected[64][33],sigkeys[64][33]; char *symbol; std::vector opret; if ( tx.vout.size() >= 2 ) { symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - komodo_notaries(elected,ntzheight,0); - numsigs = NSPV_pubkeysextract(sigkeys,tx,elected); GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4*2 ) - *desttxidp = NSPV_opretextract(heightp,blockhashp,symbol,opret,tx.GetHash()); - /*{ - int z; - for (z=0; z<68; z++) - fprintf(stderr,"%02x",opret[z]); - }*/ - *txidp = tx.GetHash(); - return(0); + { + *desttxidp = NSPV_opretextract(ntzheightp,blockhashp,symbol,opret,tx.GetHash()); + komodo_notaries(elected,*ntzheightp,0); + if ( (numsigs= NSPV_pubkeysextract(sigkeys,tx,elected)) < 12 ) + return(-3); + return(0); + } else return(-2); } else return(-1); } #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index f8c753bee..b958e877d 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -21,7 +21,7 @@ #include "notarisationdb.h" -uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) +/*uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) { uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(&txid,0,sizeof(txid)); @@ -33,7 +33,7 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) txid = nota.first; } return(txid); -} +}*/ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { @@ -206,12 +206,13 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int return(sizeof(*ptr)); } -int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) +int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32_t height) { int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; if ( (ptr->tx= NSPV_getrawtx(hashBlock,&ptr->txlen,txid)) == 0 ) return(-1); ptr->txid = txid; + ptr->vout = vout; ptr->height = height; if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) { @@ -237,6 +238,8 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,uint256 txid,int32_t height) ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); memcpy(ptr->txproof,&proof[0],ptr->txprooflen); } + //int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag); + ptr->unspentvalue = CCgettxout(txid,vout,1,1); //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } @@ -264,22 +267,40 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) return(-1); } -int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int32_t nextht) +int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid,uint256 nextntxtxid) { - int32_t i; uint256 hashBlock; - if ( prevht > nextht || (nextht-prevht) > 1440 ) + int32_t i; uint256 hashBlock,bhash0,bhash1,desttxid0,desttxid1; CTransaction tx; + ptr->prevtxid = prevntztxid; + ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); + ptr->prevtxidht = komodo_blockheight(hashBlock); + if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) + return(-1); + else if ( NSPV_notarizationextract(&ptr->common.prevht,&bhash0,&desttxid0,tx) < 0 ) + return(-2); + else if ( komodo_blockheight(bhash0) != ptr->common.prevht ) + return(-3); + + ptr->nexttxid = nextntztxid; + ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); + ptr->nexttxidht = komodo_blockheight(hashBlock); + if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) + return(-4); + else if ( NSPV_notarizationextract(&ptr->common.nextht,&bhash1,&desttxid1,tx) < 0 ) + return(-5); + else if ( komodo_blockheight(bhash1) != ptr->common.nextht ) + return(-6); + + else if ( ptr->common.prevht > ptr->common.nextht || (ptr->common.nextht - ptr->common.prevht) > 1440 ) { fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); - return(-1); + return(-7); } - ptr->common.prevht = prevht; - ptr->common.nextht = nextht; ptr->common.numhdrs = (nextht - prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { - hashBlock = NSPV_hdrhash(&ptr->common.hdrs[i]); + //hashBlock = NSPV_hdrhash(&ptr->common.hdrs[i]); //fprintf(stderr,"hdr[%d] %s\n",prevht+i,hashBlock.GetHex().c_str()); if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) { @@ -289,11 +310,6 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,int32_t prevht,int3 return(-1); } } - ptr->prevtxid = NSPV_getnotarization_txid(&ptr->prevtxidht,prevht); - ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); - ptr->nexttxid = NSPV_getnotarization_txid(&ptr->nexttxidht,nextht); - ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); - //fprintf(stderr,"ptr->prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(sizeof(*ptr) + sizeof(*ptr->common.hdrs)*ptr->common.numhdrs - sizeof(ptr->common.hdrs) - sizeof(ptr->prevntz) - sizeof(ptr->nextntz) + ptr->prevtxlen + ptr->nexttxlen); } @@ -306,7 +322,7 @@ int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) len = (int32_t)(sizeof(*ptr) - sizeof(ptr->spent.tx) - sizeof(ptr->spent.txproof)); if ( CCgetspenttxid(ptr->spent.txid,ptr->spentvini,ptr->spent.height,txid,vout) == 0 ) { - if ( NSPV_gettxproof(&ptr->spent,ptr->spent.txid,ptr->spent.height) > 0 ) + if ( NSPV_gettxproof(&ptr->spent,0,ptr->spent.txid,ptr->spent.height) > 0 ) len += ptr->spent.txlen + ptr->spent.txprooflen; else { @@ -398,26 +414,22 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { if ( timestamp > pfrom->prevtimes[ind] ) { - struct NSPV_ntzsproofresp P; int32_t prevht,nextht; + struct NSPV_ntzsproofresp P; uint256 prevntz,nextntz; if ( len == 1+sizeof(prevht)+sizeof(nextht) ) { - iguana_rwnum(0,&request[1],sizeof(prevht),&prevht); - iguana_rwnum(0,&request[1+sizeof(prevht)],sizeof(nextht),&nextht); - if ( prevht != 0 && nextht != 0 && nextht >= prevht ) + iguana_rwbignum(0,&request[1],sizeof(prevntz),(uint8_t *)&prevntz); + iguana_rwbignum(0,&request[1+sizeof(prevntz)],sizeof(nextntz),(uint8_t *)&nextntz); + memset(&P,0,sizeof(P)); + if ( (slen= NSPV_getntzsproofresp(&P,prevntz,nextntz)) > 0 ) { - memset(&P,0,sizeof(P)); - if ( (slen= NSPV_getntzsproofresp(&P,prevht,nextht)) > 0 ) + response.resize(1 + slen); + response[0] = NSPV_NTZSPROOFRESP; + if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) { - response.resize(1 + slen); - response[0] = NSPV_NTZSPROOFRESP; - P.common.numhdrs = (nextht - prevht + 1); - if ( NSPV_rwntzsproofresp(1,&response[1],&P) == slen ) - { - pfrom->PushMessage("nSPV",response); - pfrom->prevtimes[ind] = timestamp; - } - NSPV_ntzsproofresp_purge(&P); + pfrom->PushMessage("nSPV",response); + pfrom->prevtimes[ind] = timestamp; } + NSPV_ntzsproofresp_purge(&P); } } } @@ -426,14 +438,15 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { if ( timestamp > pfrom->prevtimes[ind] ) { - struct NSPV_txproof P; uint256 txid; int32_t height; + struct NSPV_txproof P; uint256 txid; int32_t height,vout; if ( len == 1+sizeof(txid)+sizeof(height) ) { iguana_rwnum(0,&request[1],sizeof(height),&height); - iguana_rwbignum(0,&request[1+sizeof(height)],sizeof(txid),(uint8_t *)&txid); + iguana_rwnum(0,&request[1+sizeof(height)],sizeof(vout),&vout); + iguana_rwbignum(0,&request[1+sizeof(height)+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); //fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); memset(&P,0,sizeof(P)); - if ( (slen= NSPV_gettxproof(&P,txid,height)) > 0 ) + if ( (slen= NSPV_gettxproof(&P,vout,txid,height)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_TXPROOFRESP; diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 456ba6385..bda60e1ea 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -344,20 +344,18 @@ UniValue NSPV_notarizations(int32_t height) return(NSPV_ntzs_json(&N)); } -UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) +UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsproofresp H; - if ( NSPV_ntzsproofresult.common.prevht == prevheight && NSPV_ntzsproofresult.common.nextht >= nextheight ) - return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); msg[len++] = NSPV_NTZSPROOF; - len += iguana_rwnum(1,&msg[len],sizeof(prevheight),&prevheight); - len += iguana_rwnum(1,&msg[len],sizeof(nextheight),&nextheight); + len += iguana_rwbignum(1,&msg[len],sizeof(prevtxid),(uint8_t *)&prevtxid); + len += iguana_rwbignum(1,&msg[len],sizeof(nexttxid),(uint8_t *)&nexttxid); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i= nextheight ) + if ( NSPV_ntzsproofresult.prevtxid == prevtxid && NSPV_ntzsproofresult.nexttxid == nexttxid ) return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); } } @@ -365,13 +363,24 @@ UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight) return(NSPV_ntzsproof_json(&H)); } -UniValue NSPV_txproof(uint256 txid,int32_t height) +UniValue NSPV_hdrsproof(int32_t prevht,int32_t nextht) +{ + uint256 prevtxid,nexttxid; + NSPV_notarizations(prevht); + prevtxid = NSPV_ntzsresult.prevntz.txid; + NSPV_notarizations(nextht); + nexttxid = NSPV_ntzsresult.nextntz.txid; + return(NSPV_txidhdrsproof(prevtxid,nexttxid)); +} + +UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_txproof P; if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) return(NSPV_txproof_json(&NSPV_txproofresult)); msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 4a864c3c2..1cfad0d29 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -29,7 +29,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-2); else if ( tx.GetHash() != ptr->nexttxid ) return(-3); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.nextht) < 0 ) + else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) return(-4); else if ( height != ptr->common.nextht ) return(-5); @@ -48,7 +48,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-7); else if ( tx.GetHash() != ptr->prevtxid ) return(-8); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&desttxid,tx,ptr->common.prevht) < 0 ) + else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) return(-9); else if ( height != ptr->common.prevht ) return(-10); @@ -81,7 +81,7 @@ int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) offset = (height - NSPV_ntzsresult.prevntz.height); if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { - NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); + NSPV_hdrsproof(NSPV_ntzsresult.prevtxid,NSPV_ntzsresult.nexttxid); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { std::vector txids; std::vector proof; uint256 proofroot; diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index d5bc66209..a99d6f071 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -981,7 +981,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); UniValue NSPV_notarizations(int32_t height); UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight); -UniValue NSPV_txproof(uint256 txid,int32_t vout); +UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t vout); uint256 Parseuint256(const char *hexstr); extern std::string NSPV_address; @@ -1057,7 +1057,7 @@ UniValue nspv_txproof(const UniValue& params, bool fHelp) throw runtime_error("nspv_txproof txid height\n"); txid = Parseuint256((char *)params[0].get_str().c_str()); height = atoi((char *)params[1].get_str().c_str()); - return(NSPV_txproof(txid,height)); + return(NSPV_txproof(0,txid,height)); } UniValue nspv_spend(const UniValue& params, bool fHelp) From db1c0bb740eb87054b7bf3b64a87bf3ba28a6796 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 03:46:56 -1100 Subject: [PATCH 288/378] Syntax --- src/komodo_nSPV_fullnode.h | 10 +++++----- src/komodo_nSPV_wallet.h | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index b958e877d..e2bc60acc 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -267,7 +267,7 @@ int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) return(-1); } -int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid,uint256 nextntxtxid) +int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid,uint256 nextntztxid) { int32_t i; uint256 hashBlock,bhash0,bhash1,desttxid0,desttxid1; CTransaction tx; ptr->prevtxid = prevntztxid; @@ -292,19 +292,19 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid else if ( ptr->common.prevht > ptr->common.nextht || (ptr->common.nextht - ptr->common.prevht) > 1440 ) { - fprintf(stderr,"illegal prevht.%d nextht.%d\n",prevht,nextht); + fprintf(stderr,"illegal prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(-7); } - ptr->common.numhdrs = (nextht - prevht + 1); + ptr->common.numhdrs = (ptr->common.nextht - ptr->common.prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); for (i=0; icommon.numhdrs; i++) { //hashBlock = NSPV_hdrhash(&ptr->common.hdrs[i]); //fprintf(stderr,"hdr[%d] %s\n",prevht+i,hashBlock.GetHex().c_str()); - if ( NSPV_setequihdr(&ptr->common.hdrs[i],prevht+i) < 0 ) + if ( NSPV_setequihdr(&ptr->common.hdrs[i],ptr->common.prevht+i) < 0 ) { - fprintf(stderr,"error setting hdr.%d\n",prevht+i); + fprintf(stderr,"error setting hdr.%d\n",ptr->common.prevht+i); free(ptr->common.hdrs); ptr->common.hdrs = 0; return(-1); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 1cfad0d29..615eff2ad 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -57,10 +57,10 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(0); } -int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) +int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransaction &tx) { int32_t offset,retval = 0; - NSPV_txproof(txid,height); + NSPV_txproof(vout,txid,height); if ( NSPV_txproofresult.txid != txid ) return(-1); if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) @@ -243,9 +243,9 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st mtx.vout.push_back(CTxOut(0,opret)); for (i=0; i Date: Sun, 7 Jul 2019 03:49:44 -1100 Subject: [PATCH 289/378] Syntax --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index e2bc60acc..ba73a2127 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -415,7 +415,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_ntzsproofresp P; uint256 prevntz,nextntz; - if ( len == 1+sizeof(prevht)+sizeof(nextht) ) + if ( len == 1+sizeof(prevntz)+sizeof(nextntz) ) { iguana_rwbignum(0,&request[1],sizeof(prevntz),(uint8_t *)&prevntz); iguana_rwbignum(0,&request[1+sizeof(prevntz)],sizeof(nextntz),(uint8_t *)&nextntz); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 615eff2ad..35221b65b 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -81,7 +81,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio offset = (height - NSPV_ntzsresult.prevntz.height); if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { - NSPV_hdrsproof(NSPV_ntzsresult.prevtxid,NSPV_ntzsresult.nexttxid); + NSPV_hdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { std::vector txids; std::vector proof; uint256 proofroot; From 2181bcd9c43cafee10354a1fc88b65c6fd818ce3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 03:51:01 -1100 Subject: [PATCH 290/378] Syntax --- src/komodo_nSPV_wallet.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 35221b65b..2ebabaa0c 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -35,7 +35,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-5); else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) { - fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str()); + //fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str()); return(-6); } for (i=ptr->common.numhdrs-1; i>0; i--) @@ -81,7 +81,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio offset = (height - NSPV_ntzsresult.prevntz.height); if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { - NSPV_hdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); + NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { std::vector txids; std::vector proof; uint256 proofroot; From 38a622681125266eb103af54c0d1f6316defbd8c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 03:54:26 -1100 Subject: [PATCH 291/378] Syntax --- src/wallet/rpcdump.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index a99d6f071..e13276960 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -981,7 +981,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis); UniValue NSPV_spentinfo(uint256 txid,int32_t vout); UniValue NSPV_notarizations(int32_t height); UniValue NSPV_hdrsproof(int32_t prevheight,int32_t nextheight); -UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t vout); +UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height); uint256 Parseuint256(const char *hexstr); extern std::string NSPV_address; From 899e223796c7b8f68da9fde34c36eec4ca6dc4f2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:05:17 -1100 Subject: [PATCH 292/378] Test --- src/komodo_nSPV_superlite.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index bda60e1ea..e3dad5752 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -376,13 +376,13 @@ UniValue NSPV_hdrsproof(int32_t prevht,int32_t nextht) UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_txproof P; - if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) - return(NSPV_txproof_json(&NSPV_txproofresult)); + //if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) + // return(NSPV_txproof_json(&NSPV_txproofresult)); msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - //fprintf(stderr,"req txproof %s at height.%d\n",txid.GetHex().c_str(),height); + fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i Date: Sun, 7 Jul 2019 04:07:27 -1100 Subject: [PATCH 293/378] Fix error check --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index ba73a2127..c4b13f173 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -439,7 +439,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_txproof P; uint256 txid; int32_t height,vout; - if ( len == 1+sizeof(txid)+sizeof(height) ) + if ( len == 1+sizeof(txid)+sizeof(height)+sizeof(vout) ) { iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwnum(0,&request[1+sizeof(height)],sizeof(vout),&vout); From 011a4f90aeafcc8ec500c7b74162df7d61347be4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:11:52 -1100 Subject: [PATCH 294/378] Prints --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index c4b13f173..746ca83d5 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -444,7 +444,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwnum(0,&request[1+sizeof(height)],sizeof(vout),&vout); iguana_rwbignum(0,&request[1+sizeof(height)+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); - //fprintf(stderr,"got txid ht.%d\n",txid.GetHex().c_str(),height); + fprintf(stderr,"got txid %s/v%d ht.%d\n",txid.GetHex().c_str(),vout,height); memset(&P,0,sizeof(P)); if ( (slen= NSPV_gettxproof(&P,vout,txid,height)) > 0 ) { From 6274db79a66812c052720e5b001b92545dfc54ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:17:57 -1100 Subject: [PATCH 295/378] Test --- src/komodo_nSPV.h | 1 + src/komodo_nSPV_fullnode.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index c2fd7b40d..1655fcf9a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -255,6 +255,7 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); + fprintf(stderr,"rwtxproof len.%d\n",len); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 746ca83d5..9092cbc30 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -450,6 +450,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_TXPROOFRESP; + fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); From 63732deda7b979f92488908f50d4240ce1f39317 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:23:08 -1100 Subject: [PATCH 296/378] Struct padding --- src/komodo_nSPV.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1655fcf9a..61cedc77d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -241,7 +241,7 @@ struct NSPV_txproof { uint256 txid; int64_t unspentvalue; - int32_t height,vout; + int32_t height,vout,pad; uint16_t txlen,txprooflen; uint8_t *tx,*txproof; }; @@ -253,6 +253,7 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->unspentvalue),&ptr->unspentvalue); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad),&ptr->pad); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); fprintf(stderr,"rwtxproof len.%d\n",len); From 786da24f91d2d464c2920f16ec905b566a105bbc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:28:56 -1100 Subject: [PATCH 297/378] +print --- src/komodo_nSPV.h | 5 ++++- src/komodo_nSPV_fullnode.h | 1 - src/komodo_nSPV_wallet.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 61cedc77d..d44a7ddae 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -256,7 +256,6 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad),&ptr->pad); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx); len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof); - fprintf(stderr,"rwtxproof len.%d\n",len); return(len); } @@ -408,10 +407,14 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) { std::vector rawdata; + fprintf(stderr,"extract %d bytes\n",datalen); rawdata.resize(datalen); memcpy(&rawdata[0],data,datalen); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) + { + fprintf(stderr,"extracted %s\n",tx.GetHex().c_str()); return(0); + } else return(-1); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 9092cbc30..746ca83d5 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -450,7 +450,6 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { response.resize(1 + slen); response[0] = NSPV_TXPROOFRESP; - fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwtxproof(1,&response[1],&P) == slen ) { pfrom->PushMessage("nSPV",response); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 2ebabaa0c..899a2707b 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -63,7 +63,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio NSPV_txproof(vout,txid,height); if ( NSPV_txproofresult.txid != txid ) return(-1); - if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) + else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; else { From d6028580a3b48af7ebf920d21c010da2abc9ed3a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:43:05 -1100 Subject: [PATCH 298/378] +print --- src/komodo_nSPV.h | 1 + src/komodo_nSPV_fullnode.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d44a7ddae..3b79ff2e7 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -286,6 +286,7 @@ int32_t NSPV_rwntzproofshared(int32_t rwflag,uint8_t *serialized,struct NSPV_ntz len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); + fprintf(stderr,"rwcommon prev.%d next.%d\n",ptr->prevht,ptr->nextht); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 746ca83d5..4981219f8 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -295,6 +295,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid fprintf(stderr,"illegal prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(-7); } + fprintf(stderr,"prevht.%d nexht.%d\n",ptr->common.prevht,ptr->common.nextht); ptr->common.numhdrs = (ptr->common.nextht - ptr->common.prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); From 4fd0f12e72c5b7c78e024eca09f4b0b1b796ef0d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:44:31 -1100 Subject: [PATCH 299/378] +print --- src/komodo_nSPV_fullnode.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 4981219f8..d09f3375f 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -421,6 +421,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwbignum(0,&request[1],sizeof(prevntz),(uint8_t *)&prevntz); iguana_rwbignum(0,&request[1+sizeof(prevntz)],sizeof(nextntz),(uint8_t *)&nextntz); memset(&P,0,sizeof(P)); + fprintf(stderr,"msg prev.%s next.%s\n",prevntz.GetHex().c_str(),nextntz.GetHex().c_str()); if ( (slen= NSPV_getntzsproofresp(&P,prevntz,nextntz)) > 0 ) { response.resize(1 + slen); From 98ae008b1acfa9fd3eb50210f8a308c1c101e9f0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:47:09 -1100 Subject: [PATCH 300/378] Test --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3b79ff2e7..14dc314b3 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -413,7 +413,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) memcpy(&rawdata[0],data,datalen); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) { - fprintf(stderr,"extracted %s\n",tx.GetHex().c_str()); + fprintf(stderr,"extracted %s\n",tx.GetHash().GetHex().c_str()); return(0); } else return(-1); From b37111a2b0a206b8d994f0e2099f78a804a87cc8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 04:57:37 -1100 Subject: [PATCH 301/378] Tweak --- src/komodo_nSPV.h | 1 - src/komodo_nSPV_fullnode.h | 22 +++++++++------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 14dc314b3..5e05b4627 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -408,7 +408,6 @@ uint256 NSPV_hdrhash(struct NSPV_equihdr *hdr) int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) { std::vector rawdata; - fprintf(stderr,"extract %d bytes\n",datalen); rawdata.resize(datalen); memcpy(&rawdata[0],data,datalen); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index d09f3375f..210c721b9 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -169,9 +169,9 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check return(0); } -uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) +uint8_t *NSPV_getrawtx(CTransaction &tx,uint256 &hashBlock,uint16_t *txlenp,uint256 txid) { - CTransaction tx; uint8_t *rawtx = 0; + uint8_t *rawtx = 0; *txlenp = 0; { LOCK(cs_main); @@ -208,8 +208,8 @@ int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32_t height) { - int32_t flag = 0,len = 0; uint256 hashBlock; CBlock block; CBlockIndex *pindex; - if ( (ptr->tx= NSPV_getrawtx(hashBlock,&ptr->txlen,txid)) == 0 ) + int32_t flag = 0,len = 0; CTransaction _tx; uint256 hashBlock; CBlock block; CBlockIndex *pindex; + if ( (ptr->tx= NSPV_getrawtx(_tx,hashBlock,&ptr->txlen,txid)) == 0 ) return(-1); ptr->txid = txid; ptr->vout = vout; @@ -271,21 +271,17 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid { int32_t i; uint256 hashBlock,bhash0,bhash1,desttxid0,desttxid1; CTransaction tx; ptr->prevtxid = prevntztxid; - ptr->prevntz = NSPV_getrawtx(hashBlock,&ptr->prevtxlen,ptr->prevtxid); + ptr->prevntz = NSPV_getrawtx(tx,hashBlock,&ptr->prevtxlen,ptr->prevtxid); ptr->prevtxidht = komodo_blockheight(hashBlock); - if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-1); - else if ( NSPV_notarizationextract(&ptr->common.prevht,&bhash0,&desttxid0,tx) < 0 ) + if ( NSPV_notarizationextract(&ptr->common.prevht,&bhash0,&desttxid0,tx) < 0 ) return(-2); else if ( komodo_blockheight(bhash0) != ptr->common.prevht ) return(-3); ptr->nexttxid = nextntztxid; - ptr->nextntz = NSPV_getrawtx(hashBlock,&ptr->nexttxlen,ptr->nexttxid); + ptr->nextntz = NSPV_getrawtx(tx,hashBlock,&ptr->nexttxlen,ptr->nexttxid); ptr->nexttxidht = komodo_blockheight(hashBlock); - if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-4); - else if ( NSPV_notarizationextract(&ptr->common.nextht,&bhash1,&desttxid1,tx) < 0 ) + if ( NSPV_notarizationextract(&ptr->common.nextht,&bhash1,&desttxid1,tx) < 0 ) return(-5); else if ( komodo_blockheight(bhash1) != ptr->common.nextht ) return(-6); @@ -295,7 +291,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid fprintf(stderr,"illegal prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(-7); } - fprintf(stderr,"prevht.%d nexht.%d\n",ptr->common.prevht,ptr->common.nextht); + fprintf(stderr,"%s -> prevht.%d, %s -> nexht.%d\n",ptr->prevtxid.GetHex().c_str(),ptr->common.prevht,ptr->nexttxid.GetHex().c_str(),ptr->common.nextht); ptr->common.numhdrs = (ptr->common.nextht - ptr->common.prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); From 1eefc92892dba0e2115a1b1b6dd0fa412af4327d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:08:49 -1100 Subject: [PATCH 302/378] +print --- src/komodo_nSPV.h | 6 +++--- src/komodo_nSPV_fullnode.h | 8 ++++---- src/komodo_nSPV_wallet.h | 5 +---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 5e05b4627..d8f9745e4 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -412,7 +412,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) memcpy(&rawdata[0],data,datalen); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) { - fprintf(stderr,"extracted %s\n",tx.GetHash().GetHex().c_str()); + //fprintf(stderr,"extracted %s\n",tx.GetHash().GetHex().c_str()); return(0); } else return(-1); @@ -432,8 +432,8 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: ((uint8_t *)blockhashp)[i] = opret[i]; for (i=0; i<32; i++) ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; - if ( *heightp != 2690 ) - fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(txid).GetHex().c_str(),(int32_t)opret.size()); + if ( 0 && *heightp != 2690 ) + fprintf(stderr," ntzht.%d %s <- txid.%s size.%d\n",*heightp,(*blockhashp).GetHex().c_str(),(txid).GetHex().c_str(),(int32_t)opret.size()); return(desttxid); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 210c721b9..374474dd9 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -233,12 +233,12 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 ssMB << mb; std::vector proof(ssMB.begin(), ssMB.end()); ptr->txprooflen = (int32_t)proof.size(); + fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof)); if ( ptr->txprooflen > 0 ) { ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); memcpy(ptr->txproof,&proof[0],ptr->txprooflen); } - //int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag); ptr->unspentvalue = CCgettxout(txid,vout,1,1); //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); @@ -291,7 +291,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid fprintf(stderr,"illegal prevht.%d nextht.%d\n",ptr->common.prevht,ptr->common.nextht); return(-7); } - fprintf(stderr,"%s -> prevht.%d, %s -> nexht.%d\n",ptr->prevtxid.GetHex().c_str(),ptr->common.prevht,ptr->nexttxid.GetHex().c_str(),ptr->common.nextht); + //fprintf(stderr,"%s -> prevht.%d, %s -> nexht.%d\n",ptr->prevtxid.GetHex().c_str(),ptr->common.prevht,ptr->nexttxid.GetHex().c_str(),ptr->common.nextht); ptr->common.numhdrs = (ptr->common.nextht - ptr->common.prevht + 1); ptr->common.hdrs = (struct NSPV_equihdr *)calloc(ptr->common.numhdrs,sizeof(*ptr->common.hdrs)); //fprintf(stderr,"prev.%d next.%d allocate numhdrs.%d\n",prevht,nextht,ptr->common.numhdrs); @@ -417,7 +417,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwbignum(0,&request[1],sizeof(prevntz),(uint8_t *)&prevntz); iguana_rwbignum(0,&request[1+sizeof(prevntz)],sizeof(nextntz),(uint8_t *)&nextntz); memset(&P,0,sizeof(P)); - fprintf(stderr,"msg prev.%s next.%s\n",prevntz.GetHex().c_str(),nextntz.GetHex().c_str()); + //fprintf(stderr,"msg prev.%s next.%s\n",prevntz.GetHex().c_str(),nextntz.GetHex().c_str()); if ( (slen= NSPV_getntzsproofresp(&P,prevntz,nextntz)) > 0 ) { response.resize(1 + slen); @@ -442,7 +442,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(height),&height); iguana_rwnum(0,&request[1+sizeof(height)],sizeof(vout),&vout); iguana_rwbignum(0,&request[1+sizeof(height)+sizeof(vout)],sizeof(txid),(uint8_t *)&txid); - fprintf(stderr,"got txid %s/v%d ht.%d\n",txid.GetHex().c_str(),vout,height); + //fprintf(stderr,"got txid %s/v%d ht.%d\n",txid.GetHex().c_str(),vout,height); memset(&P,0,sizeof(P)); if ( (slen= NSPV_gettxproof(&P,vout,txid,height)) > 0 ) { diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 899a2707b..39c2af809 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -34,10 +34,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) else if ( height != ptr->common.nextht ) return(-5); else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) - { - //fprintf(stderr,"hdr.%s vs blockhash.%s\n",NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]).GetHex().c_str(),blockhash.GetHex().c_str()); return(-6); - } for (i=ptr->common.numhdrs-1; i>0; i--) { blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); @@ -77,7 +74,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio } if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { - fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); + //fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); offset = (height - NSPV_ntzsresult.prevntz.height); if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { From ccdc1c8080c0b4b4a0d3080b0cefd1f4a5556d89 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:13:08 -1100 Subject: [PATCH 303/378] Test --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_superlite.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d8f9745e4..a18c0c363 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -286,7 +286,7 @@ int32_t NSPV_rwntzproofshared(int32_t rwflag,uint8_t *serialized,struct NSPV_ntz len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->nextht),&ptr->nextht); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad16),&ptr->pad16); - fprintf(stderr,"rwcommon prev.%d next.%d\n",ptr->prevht,ptr->nextht); + //fprintf(stderr,"rwcommon prev.%d next.%d\n",ptr->prevht,ptr->nextht); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 374474dd9..d781e4de0 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -233,7 +233,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 ssMB << mb; std::vector proof(ssMB.begin(), ssMB.end()); ptr->txprooflen = (int32_t)proof.size(); - fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof)); + fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof).c_str()); if ( ptr->txprooflen > 0 ) { ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index e3dad5752..533e3f8b8 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -382,7 +382,7 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); + //fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i Date: Sun, 7 Jul 2019 05:25:57 -1100 Subject: [PATCH 304/378] Use txproof for txproof --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 39c2af809..99cf14dba 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -83,7 +83,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio { std::vector txids; std::vector proof; uint256 proofroot; proof.resize(NSPV_txproofresult.txprooflen); - memcpy(&proof[0],NSPV_txproofresult.tx,NSPV_txproofresult.txprooflen); + memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); proofroot = BitcoinGetProofMerkleRoot(proof,txids); if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) { From 45161da432bffd2e9ce9c1caa846d6937dc5e599 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:39:21 -1100 Subject: [PATCH 305/378] Activate validation --- src/komodo_nSPV.h | 3 ++- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_wallet.h | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a18c0c363..3feeec7d0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -19,7 +19,8 @@ // determine if it makes sense to be scanning mempool for the utxo/spentinfo requests, maybe can add isspent flag to txproof, or just use getspentinfo if broadcast fails -// validate proofs +// pubkeys extract +// headers "sync" // interest calculations are currently just using what is returned, it should calculate it from scratch // need to validate incoming data and update only if it is valid and more recent diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index d781e4de0..4a506cc3e 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -233,7 +233,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 ssMB << mb; std::vector proof(ssMB.begin(), ssMB.end()); ptr->txprooflen = (int32_t)proof.size(); - fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof).c_str()); + //fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof).c_str()); if ( ptr->txprooflen > 0 ) { ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 99cf14dba..cfecd2a31 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -94,8 +94,8 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio } else retval = -22; } else retval = -1; } - fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); - return(0); + //fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); + return(retval); } int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) From 4f4db5299b4c89862976f048fc4dadb78600ab45 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:46:50 -1100 Subject: [PATCH 306/378] Check for spent utxo --- src/komodo_nSPV.h | 2 -- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 3feeec7d0..b0dca61c6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -17,8 +17,6 @@ // todo: // make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) -// determine if it makes sense to be scanning mempool for the utxo/spentinfo requests, maybe can add isspent flag to txproof, or just use getspentinfo if broadcast fails - // pubkeys extract // headers "sync" // interest calculations are currently just using what is returned, it should calculate it from scratch diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index cfecd2a31..74bd61639 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -58,7 +58,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio { int32_t offset,retval = 0; NSPV_txproof(vout,txid,height); - if ( NSPV_txproofresult.txid != txid ) + if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentsatoshis <= 0 ) return(-1); else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; From fdca2066bca46db125b61044a152cbfdac3876c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:48:12 -1100 Subject: [PATCH 307/378] +print --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 74bd61639..ce330f604 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -258,7 +258,7 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st fprintf(stderr,"signing error for vini.%d\n",i); return(""); } - } else fprintf(stderr,"couldnt find txid.%s\n",mtx.vin[i].prevout.hash.GetHex().c_str()); + } else fprintf(stderr,"couldnt find txid.%s/v%d or it was spent\n",mtx.vin[i].prevout.hash.GetHex().c_str(),utxovout); // of course much better handling is needed } fprintf(stderr,"sign %d inputs %.8f + interest %.8f -> %d outputs %.8f change %.8f\n",(int32_t)mtx.vin.size(),(double)totalinputs/COIN,(double)interest/COIN,(int32_t)mtx.vout.size(),(double)totaloutputs/COIN,(double)change/COIN); return(EncodeHexTx(mtx)); From 456959ad7e6009a4566802a2f65775301a9107d4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 05:49:42 -1100 Subject: [PATCH 308/378] unspentvalue --- src/komodo_nSPV_wallet.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ce330f604..1666f7deb 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -58,7 +58,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio { int32_t offset,retval = 0; NSPV_txproof(vout,txid,height); - if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentsatoshis <= 0 ) + if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentvalue <= 0 ) return(-1); else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; @@ -230,7 +230,6 @@ std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,st totalinputs += used[i].satoshis; interest += used[i].extradata; } - //PrecomputedTransactionData txdata(mtx); if ( (totalinputs+interest) >= totaloutputs+2*txfee ) { change = (totalinputs+interest) - (totaloutputs+txfee); From 6762cc3bb80273403d65567dc7d8646b6258a345 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 07:07:23 -1100 Subject: [PATCH 309/378] Debug vers --- src/komodo_nSPV_wallet.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 1666f7deb..25c844dc3 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -94,8 +94,9 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio } else retval = -22; } else retval = -1; } - //fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); - return(retval); + if ( retval != 0 ) + fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); + return(retval*0); } int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) From e6d258234506cc45f4b538cf7d0458f111b765f8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 19:33:01 -1100 Subject: [PATCH 310/378] Scan future ntz --- src/komodo_nSPV.h | 6 +++ src/komodo_nSPV_fullnode.h | 88 ++++++++++++++++---------------------- 2 files changed, 43 insertions(+), 51 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b0dca61c6..1133bfb7d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -24,6 +24,12 @@ // need to validate incoming data and update only if it is valid and more recent // make sure to sanity check all vector lengths on receipt +// bugs: +// got txproof response 1562526470 size.2112 a9ea92a1bbc68ae850b4bedabcbe60132844e5791ba2678bcba520910f1ad0c6 ht.1432768 +// got ntzs response 1562526471 size.209 bbf7ac47c67b5f84ddeffa5621dae6a9c3e5e8fad59cfc7359be2aa2937bd135 prev.1432750, 8255451aa5a5f8ba8f06ea8f4f83879f346bfd782d72c16d034693d123aa4b68 next.1432760 +// NSPV_gettransaction retval would have been -22 + + #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 4a506cc3e..a8c4cf036 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -21,61 +21,47 @@ #include "notarisationdb.h" -/*uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) +struct NSPV_ntzargs { - uint256 txid; Notarisation nota; char *symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - memset(&txid,0,sizeof(txid)); - *ntzheightp = 0; - int32_t matchedHeight = ScanNotarisationsDB2(height,symbol,1440,nota); - if ( matchedHeight != 0 ) - { - *ntzheightp = matchedHeight; - txid = nota.first; - } - return(txid); -}*/ + uint256 txid,desttxid,blockhash; + int32_t txidht,ntzheight; +}; -int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) +int32_t NSPV_notarization_find(struct NSPV_ntzargs *args,int32_t height,int32_t dir) { - int32_t txidht; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; uint256 bhash0,bhash1,hashBlock; + int32_t ntzheight = 0; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - memset(txids,0,sizeof(*txids)*2); - memset(desttxids,0,sizeof(*desttxids)*2); - memset(ntzheights,0,sizeof(*ntzheights)*2); - memset(txidhts,0,sizeof(*txidhts)*2); - if ( (txidht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) + memset(*args,0,sizeof(*args)); + if ( (args->txidht= (dir < 0 ? ScanNotarisationsDB : ScanNotarisationsDB2)(height,symbol,1440,nota)) == 0 ) return(-1); - txids[0] = nota.first; - txidhts[0] = txidht; - if ( !GetTransaction(txids[0],tx,hashBlock,false) || tx.vout.size() < 2 ) + args->txid = nota.first; + if ( !GetTransaction(args->txid,tx,hashBlock,false) || tx.vout.size() < 2 ) return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); - //if ( opret.size() >= 32*2+4 ) - desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,opret,txids[0]); + if ( opret.size() >= 32*2+4 ) + args->desttxids = NSPV_opretextract(&args->ntzheight,&args->blockhash,symbol,opret,args->txid); + return(args->ntzheight); +} - /*desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash0,symbol,E_MARSHAL(ss << nota.second)); - //if ( height != 2668 ) - // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); - if ( ntzheights[0] == height-1 ) // offset the +1 from caller +int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *next,int32_t height) +{ + uint256 bhash; int32_t txidht,ntzht,nextht; + memset(prev,0,sizeof(*prev)); + memset(next,0,sizeof(*next)); + if ( (ntzht= NSPV_notarization_find(prev,height,-1)) < 0 || ntzht > height || ntzht == 0 ) + return(-1); + txidht = height+1; + while ( (ntzht= NSPV_notarization_find(next,txidht,1)) < height ) { - txids[1] = txids[0]; - txidhts[1] = txidhts[0]; - ntzheights[1] = ntzheights[0]; - desttxids[1] = desttxids[0]; - return(0); - }*/ - if ( (txidht= ScanNotarisationsDB2(height,symbol,1440,nota)) != 0 ) - { - txids[1] = nota.first; - txidhts[1] = txidht; - if ( !GetTransaction(txids[1],tx,hashBlock,false) || tx.vout.size() < 2 ) - return(-2); - GetOpReturnData(tx.vout[1].scriptPubKey,opret); - //if ( opret.size() >= 32*2+4 ) - desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,opret,txids[1]); - //desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash1,symbol,E_MARSHAL(ss << nota.second)); + nextht = next->txidht + 1; + fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); + memset(next,0,sizeof(*next)); + txidht = nextht; + if ( nextht <= txidht ) // not advancing error + break; + if ( ntzht <= 0 ) + break; } - //fprintf(stderr,"prev.(%s -> ht.%d %s) next.(%s -> ht.%d %s)\n",txids[0].GetHex().c_str(),ntzheights[0],bhash0.GetHex().c_str(),txids[1].GetHex().c_str(),ntzheights[1],bhash1.GetHex().c_str()); return(0); } @@ -91,19 +77,19 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) { - uint256 txids[2],desttxids[2]; int32_t ntzheights[2],txidhts[2]; + struct NSPV_ntzargs prev,next; if ( height < chainActive.LastTip()->GetHeight() ) height++; - if ( komodo_notarized_bracket(txids,txidhts,desttxids,ntzheights,height) == 0 ) + if ( NSPV_notarized_bracket(&prev,&next,height) == 0 ) { - if ( ntzheights[0] != 0 ) + if ( prev.ntzheight != 0 ) { - if ( NSPV_ntzextract(&ptr->prevntz,txids[0],txidhts[0],desttxids[0],ntzheights[0]) < 0 ) + if ( NSPV_ntzextract(&ptr->prevntz,prev.txid,prev.txidht,prev.desttxid,prev.ntzheight) < 0 ) return(-1); } - if ( ntzheights[1] != 0 ) + if ( next.ntzheight != 0 ) { - if ( NSPV_ntzextract(&ptr->nextntz,txids[1],txidhts[1],desttxids[1],ntzheights[1]) < 0 ) + if ( NSPV_ntzextract(&ptr->nextntz,next.txid,next.txidht,next.desttxid,next.ntzheight) < 0 ) return(-1); } } From e21703d6489a4d9edad691413208100c42372ec9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 19:35:07 -1100 Subject: [PATCH 311/378] Syntax --- src/komodo_nSPV_fullnode.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index a8c4cf036..7abec636d 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -29,9 +29,9 @@ struct NSPV_ntzargs int32_t NSPV_notarization_find(struct NSPV_ntzargs *args,int32_t height,int32_t dir) { - int32_t ntzheight = 0; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; + int32_t ntzheight = 0; uint256 hashBlock; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; - memset(*args,0,sizeof(*args)); + memset(args,0,sizeof(*args)); if ( (args->txidht= (dir < 0 ? ScanNotarisationsDB : ScanNotarisationsDB2)(height,symbol,1440,nota)) == 0 ) return(-1); args->txid = nota.first; @@ -39,7 +39,7 @@ int32_t NSPV_notarization_find(struct NSPV_ntzargs *args,int32_t height,int32_t return(-2); GetOpReturnData(tx.vout[1].scriptPubKey,opret); if ( opret.size() >= 32*2+4 ) - args->desttxids = NSPV_opretextract(&args->ntzheight,&args->blockhash,symbol,opret,args->txid); + args->desttxid = NSPV_opretextract(&args->ntzheight,&args->blockhash,symbol,opret,args->txid); return(args->ntzheight); } From 10ac1c2454809a627e3caa397343648ef4f675ed Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 19:59:26 -1100 Subject: [PATCH 312/378] Display retcodes --- src/komodo_nSPV_wallet.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 25c844dc3..65c5ce068 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -92,11 +92,9 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio } } } else retval = -22; - } else retval = -1; + } else retval = -2; } - if ( retval != 0 ) - fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); - return(retval*0); + return(retval); } int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) @@ -219,9 +217,9 @@ bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const C return(false); } -std::string NSPV_signtx(CMutableTransaction &mtx,uint64_t txfee,CScript opret,struct NSPV_utxoresp used[]) +std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txfee,CScript opret,struct NSPV_utxoresp used[]) { - CTransaction vintx; std::string hex; uint256 hashBlock; int64_t interest=0,change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n; + CTransaction vintx; std::string hex; uint256 hashBlock; int64_t interest=0,change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,validation; n = mtx.vout.size(); for (i=0; i NSPV_logintime+NSPV_AUTOLOGOUT ) { result.push_back(Pair("result","error")); @@ -335,7 +335,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a result.push_back(Pair("error","wif expired")); return(result); } - hex = NSPV_signtx(mtx,txfee,opret,used); + hex = NSPV_signtx(retcodes,mtx,txfee,opret,used); if ( hex.size() > 0 ) { if ( DecodeHexTx(tx,hex) != 0 ) @@ -343,6 +343,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a TxToJSON(tx,uint256(),result); result.push_back(Pair("result","success")); result.push_back(Pair("hex",hex)); + result.push_back(Pair("retcodes",retcodes)); } else { @@ -354,6 +355,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a else { result.push_back(Pair("result","error")); + result.push_back(Pair("retcodes",retcodes)); result.push_back(Pair("error","signing error")); } return(result); From 473609a1e4e1e7ce14412893cffe1a961a3c9cd9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 20:01:08 -1100 Subject: [PATCH 313/378] Typo --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 65c5ce068..c7ce08054 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -241,7 +241,7 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf utxovout = mtx.vin[i].prevout.n; validation = NSPV_gettransaction(utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); - if ( validaton != -1 ) // most others are degraded security + if ( validation != -1 ) // most others are degraded security { if ( vintx.vout[utxovout].nValue != used[i].satoshis ) { From a025c5d9e430dff372fd43b75b5a7b6b6c12bd3f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 20:08:51 -1100 Subject: [PATCH 314/378] -print --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 7abec636d..db86348a0 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -54,7 +54,7 @@ int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *ne while ( (ntzht= NSPV_notarization_find(next,txidht,1)) < height ) { nextht = next->txidht + 1; - fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); + //fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); memset(next,0,sizeof(*next)); txidht = nextht; if ( nextht <= txidht ) // not advancing error From a8cc298d5b862c70dbf94afd5816523a9252746a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:20:15 -1100 Subject: [PATCH 315/378] New netinfo tweak bracket --- src/komodo_nSPV.h | 12 +++---- src/komodo_nSPV_fullnode.h | 4 ++- src/komodo_nSPV_superlite.h | 69 +++++++++++++++++-------------------- src/komodo_nSPV_wallet.h | 2 ++ src/wallet/rpcdump.cpp | 11 +++--- 5 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 1133bfb7d..4b4b3510a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -24,12 +24,6 @@ // need to validate incoming data and update only if it is valid and more recent // make sure to sanity check all vector lengths on receipt -// bugs: -// got txproof response 1562526470 size.2112 a9ea92a1bbc68ae850b4bedabcbe60132844e5791ba2678bcba520910f1ad0c6 ht.1432768 -// got ntzs response 1562526471 size.209 bbf7ac47c67b5f84ddeffa5621dae6a9c3e5e8fad59cfc7359be2aa2937bd135 prev.1432750, 8255451aa5a5f8ba8f06ea8f4f83879f346bfd782d72c16d034693d123aa4b68 next.1432760 -// NSPV_gettransaction retval would have been -22 - - #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H @@ -223,7 +217,8 @@ struct NSPV_inforesp { struct NSPV_ntz notarization; uint256 blockhash; - int32_t height,pad32; + int32_t height,hdrheight; + struct NSPV_equihdr H; }; int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp *ptr) @@ -232,7 +227,8 @@ int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp len += NSPV_rwntz(rwflag,&serialized[len],&ptr->notarization); len += iguana_rwbignum(rwflag,&serialized[len],sizeof(ptr->blockhash),(uint8_t *)&ptr->blockhash); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); - len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->pad32),&ptr->pad32); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->hdrheight),&ptr->hdrheight); + len += NSPV_rwequihdr(rwflag,&serialized[len],&ptr->H); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index db86348a0..c2e4b4e52 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -32,7 +32,9 @@ int32_t NSPV_notarization_find(struct NSPV_ntzargs *args,int32_t height,int32_t int32_t ntzheight = 0; uint256 hashBlock; CTransaction tx; Notarisation nota; char *symbol; std::vector opret; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(args,0,sizeof(*args)); - if ( (args->txidht= (dir < 0 ? ScanNotarisationsDB : ScanNotarisationsDB2)(height,symbol,1440,nota)) == 0 ) + if ( dir > 0 ) + height += 10; + if ( (args->txidht= ScanNotarisationsDB(height,symbol,1440,nota)) == 0 ) return(-1); args->txid = nota.first; if ( !GetTransaction(args->txid,tx,hashBlock,false) || tx.vout.size() < 2 ) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 533e3f8b8..4b791880b 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -105,7 +105,27 @@ UniValue NSPV_ntz_json(struct NSPV_ntz *ptr) return(result); } -UniValue _NSPV_getinfo_json(struct NSPV_inforesp *ptr) +UniValue NSPV_header_json(struct NSPV_equihdr *hdr,int32_t height) +{ + UniValue item(UniValue::VOBJ); + item.push_back(Pair("height",(int64_t)height)); + item.push_back(Pair("blockhash",NSPV_hdrhash(hdr).GetHex())); + item.push_back(Pair("hashPrevBlock",hdr->hashPrevBlock.GetHex())); + item.push_back(Pair("hashMerkleRoot",hdr->hashMerkleRoot.GetHex())); + item.push_back(Pair("nTime",(int64_t)hdr->nTime)); + item.push_back(Pair("nBits",(int64_t)hdr->nBits)); + return(item); +} + +UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs,int32_t height) +{ + UniValue array(UniValue::VARR); int32_t i; + for (i=0; iheight)); result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); + result.push_back(Pair("hdr",NSPV_header_json(&ptr->H))); return(result); } @@ -165,23 +186,6 @@ UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr) return(result); } -UniValue NSPV_headers_json(struct NSPV_equihdr *hdrs,int32_t numhdrs,int32_t height) -{ - UniValue array(UniValue::VARR); int32_t i; - for (i=0; i>1) != 0 ) { - //fprintf(stderr,"issue getinfo\n"); - if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) + for (i=0; i>1); - } slen = (int32_t)strlen(coinaddr); msg[len++] = NSPV_UTXOS; msg[len++] = slen; diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index c7ce08054..9ab477e3e 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -383,8 +383,10 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages { if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) { + int32_t reqht = 0; len = 0; msg[len++] = NSPV_INFO; + len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index e13276960..0aac30799 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -972,7 +972,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp) return EncodeViewingKey(vk); } -UniValue NSPV_getinfo_json(); +UniValue NSPV_getinfo_req(int32_t reqht); UniValue NSPV_login(char *wifstr); UniValue NSPV_logout(); UniValue NSPV_addressutxos(char *coinaddr); @@ -987,9 +987,12 @@ extern std::string NSPV_address; UniValue nspv_getinfo(const UniValue& params, bool fHelp) { - if ( fHelp || params.size() != 0 ) - throw runtime_error("nspv_getinfo\n"); - return(NSPV_getinfo_json()); + int32_t reqht = 0; + if ( fHelp || params.size() > 1 ) + throw runtime_error("nspv_getinfo [hdrheight]\n"); + if ( params.size() == 0 ) + reqht = atoi((char *)params[0].get_str().c_str()); + return(NSPV_getinfo_req(reqht)); } UniValue nspv_logout(const UniValue& params, bool fHelp) From 72884c3722bb799a08126603b4c7de19136c971e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:21:07 -1100 Subject: [PATCH 316/378] +print --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index c2e4b4e52..9fc6157e0 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -56,7 +56,7 @@ int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *ne while ( (ntzht= NSPV_notarization_find(next,txidht,1)) < height ) { nextht = next->txidht + 1; - //fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); +fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); memset(next,0,sizeof(*next)); txidht = nextht; if ( nextht <= txidht ) // not advancing error diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 9ab477e3e..6bc3c0b4e 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -74,7 +74,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio } if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { - //fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); +fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); offset = (height - NSPV_ntzsresult.prevntz.height); if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) { From db687d05d03ca61cbf3cd6daa70507a0009af927 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:23:35 -1100 Subject: [PATCH 317/378] Fix --- src/komodo_nSPV_fullnode.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 9fc6157e0..8c213e370 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -47,7 +47,7 @@ int32_t NSPV_notarization_find(struct NSPV_ntzargs *args,int32_t height,int32_t int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *next,int32_t height) { - uint256 bhash; int32_t txidht,ntzht,nextht; + uint256 bhash; int32_t txidht,ntzht,nextht,i=0; memset(prev,0,sizeof(*prev)); memset(next,0,sizeof(*next)); if ( (ntzht= NSPV_notarization_find(prev,height,-1)) < 0 || ntzht > height || ntzht == 0 ) @@ -55,14 +55,14 @@ int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *ne txidht = height+1; while ( (ntzht= NSPV_notarization_find(next,txidht,1)) < height ) { - nextht = next->txidht + 1; + nextht = next->txidht + 10*i; fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); memset(next,0,sizeof(*next)); txidht = nextht; - if ( nextht <= txidht ) // not advancing error - break; if ( ntzht <= 0 ) break; + if ( i++ > 10 ) + break; } return(0); } From fc20386d8d5a5a68f5b5289a84417ee62a133caa Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:24:42 -1100 Subject: [PATCH 318/378] Syntax --- src/komodo_nSPV_superlite.h | 2 +- src/komodo_nSPV_wallet.h | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 4b791880b..c5ac37159 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -142,7 +142,7 @@ UniValue NSPV_getinfo_json(struct NSPV_inforesp *ptr) result.push_back(Pair("height",(int64_t)ptr->height)); result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); - result.push_back(Pair("hdr",NSPV_header_json(&ptr->H))); + result.push_back(Pair("hdr",NSPV_header_json(&ptr->H,ptr->hdrheight))); return(result); } diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 6bc3c0b4e..1f47a6469 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -286,8 +286,6 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a result.push_back(Pair("error","invalid destaddr")); return(result); } - if ( NSPV_inforesult.height == 0 ) - NSPV_getinfo_json(); if ( NSPV_inforesult.height == 0 ) { result.push_back(Pair("result","error")); From 5fc26ebcbe6b8491a93ae6a12b94a35dcc4a5de3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:26:48 -1100 Subject: [PATCH 319/378] Syntax --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index c5ac37159..48420f312 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -283,7 +283,7 @@ UniValue NSPV_getinfo_req(int32_t reqht) { usleep(NSPV_POLLMICROS); if ( NSPV_inforesult.height != 0 ) - return(_NSPV_getinfo_json(&NSPV_inforesult)); + return(NSPV_getinfo_json(&NSPV_inforesult,NSPV_inforesult.hdrheight)); } } memset(&I,0,sizeof(I)); From 99fc3cfd8afd2afb8bad1a454839ac4fa48e3f5b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:30:03 -1100 Subject: [PATCH 320/378] Syntax --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 48420f312..41ec3c571 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -283,7 +283,7 @@ UniValue NSPV_getinfo_req(int32_t reqht) { usleep(NSPV_POLLMICROS); if ( NSPV_inforesult.height != 0 ) - return(NSPV_getinfo_json(&NSPV_inforesult,NSPV_inforesult.hdrheight)); + return(NSPV_getinfo_json(&NSPV_inforesult)); } } memset(&I,0,sizeof(I)); From 640cb30c56af4d4cd02d1ca859ba5512a155f816 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:37:42 -1100 Subject: [PATCH 321/378] Prints --- src/komodo_nSPV.h | 1 + src/komodo_nSPV_fullnode.h | 3 +++ src/wallet/rpcdump.cpp | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4b4b3510a..9ffd67030 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -229,6 +229,7 @@ int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->hdrheight),&ptr->hdrheight); len += NSPV_rwequihdr(rwflag,&serialized[len],&ptr->H); + fprintf(stderr,"hdr rwlen.%d\n",len); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 8c213e370..26f0f44dc 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -333,11 +333,14 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; + iguana_rwnum(0,&request[1],sizeof(height),&height); + fprintf(stderr,"request height.%d\n",height); memset(&I,0,sizeof(I)); if ( (slen= NSPV_getinfo(&I)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_INFORESP; + fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwinforesp(1,&response[1],&I) == slen ) { pfrom->PushMessage("nSPV",response); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 0aac30799..071e3d8a7 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -990,7 +990,7 @@ UniValue nspv_getinfo(const UniValue& params, bool fHelp) int32_t reqht = 0; if ( fHelp || params.size() > 1 ) throw runtime_error("nspv_getinfo [hdrheight]\n"); - if ( params.size() == 0 ) + if ( params.size() == 1 ) reqht = atoi((char *)params[0].get_str().c_str()); return(NSPV_getinfo_req(reqht)); } From a3cf4b19517b05751d873976f8c3e8d49edf9cac Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:39:49 -1100 Subject: [PATCH 322/378] Return reqhdr --- src/komodo_nSPV_fullnode.h | 48 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 26f0f44dc..d555d6468 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -98,7 +98,27 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height) return(sizeof(*ptr)); } -int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) +int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) +{ + CBlockIndex *pindex; + if ( (pindex= komodo_chainactive(height)) != 0 ) + { + hdr->nVersion = pindex->nVersion; + if ( pindex->pprev == 0 ) + return(-1); + hdr->hashPrevBlock = pindex->pprev->GetBlockHash(); + hdr->hashMerkleRoot = pindex->hashMerkleRoot; + hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; + hdr->nTime = pindex->nTime; + hdr->nBits = pindex->nBits; + hdr->nNonce = pindex->nNonce; + memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); + return(sizeof(*hdr)); + } + return(-1); +} + +int32_t NSPV_getinfo(struct NSPV_inforesp *ptr,int32_t reqheight) { int32_t prevMoMheight,len = 0; CBlockIndex *pindex; struct NSPV_ntzsresp pair; if ( (pindex= chainActive.LastTip()) != 0 ) @@ -108,6 +128,10 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr) if ( NSPV_getntzsresp(&pair,ptr->height-1) < 0 ) return(-1); ptr->notarization = pair.prevntz; + if ( reqheight == 0 ) + reqheight = pindex->nHeight; + if ( NSPV_setequihdr(&ptr->H,reqheight) < 0 ) + return(-1); return(sizeof(*ptr)); } else return(-1); } @@ -235,26 +259,6 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 return(-1); } -int32_t NSPV_setequihdr(struct NSPV_equihdr *hdr,int32_t height) -{ - CBlockIndex *pindex; - if ( (pindex= komodo_chainactive(height)) != 0 ) - { - hdr->nVersion = pindex->nVersion; - if ( pindex->pprev == 0 ) - return(-1); - hdr->hashPrevBlock = pindex->pprev->GetBlockHash(); - hdr->hashMerkleRoot = pindex->hashMerkleRoot; - hdr->hashFinalSaplingRoot = pindex->hashFinalSaplingRoot; - hdr->nTime = pindex->nTime; - hdr->nBits = pindex->nBits; - hdr->nNonce = pindex->nNonce; - memcpy(hdr->nSolution,&pindex->nSolution[0],sizeof(hdr->nSolution)); - return(sizeof(*hdr)); - } - return(-1); -} - int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid,uint256 nextntztxid) { int32_t i; uint256 hashBlock,bhash0,bhash1,desttxid0,desttxid1; CTransaction tx; @@ -336,7 +340,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(height),&height); fprintf(stderr,"request height.%d\n",height); memset(&I,0,sizeof(I)); - if ( (slen= NSPV_getinfo(&I)) > 0 ) + if ( (slen= NSPV_getinfo(&I,height)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_INFORESP; From c3e92dba4f59ace5cd8ebe841f09a34d897ab44b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:42:49 -1100 Subject: [PATCH 323/378] Syntax --- src/komodo_nSPV_fullnode.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index d555d6468..26515993f 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -129,7 +129,7 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr,int32_t reqheight) return(-1); ptr->notarization = pair.prevntz; if ( reqheight == 0 ) - reqheight = pindex->nHeight; + reqheight = pindex->GetHeight(); if ( NSPV_setequihdr(&ptr->H,reqheight) < 0 ) return(-1); return(sizeof(*ptr)); @@ -324,7 +324,7 @@ int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout) void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a request { - int32_t len,slen,ind; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); + int32_t len,slen,ind,reqheight; std::vector response; uint32_t timestamp = (uint32_t)time(NULL); if ( (len= request.size()) > 0 ) { if ( (ind= request[0]>>1) >= sizeof(pfrom->prevtimes)/sizeof(*pfrom->prevtimes) ) @@ -337,8 +337,8 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; - iguana_rwnum(0,&request[1],sizeof(height),&height); - fprintf(stderr,"request height.%d\n",height); + iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight); + fprintf(stderr,"request height.%d\n",reqheight); memset(&I,0,sizeof(I)); if ( (slen= NSPV_getinfo(&I,height)) > 0 ) { From 9c020edb521cefa6ae9e57c0894788f5e3a65d4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:44:32 -1100 Subject: [PATCH 324/378] Syntax --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 26515993f..9b89c6b7e 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -340,7 +340,7 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight); fprintf(stderr,"request height.%d\n",reqheight); memset(&I,0,sizeof(I)); - if ( (slen= NSPV_getinfo(&I,height)) > 0 ) + if ( (slen= NSPV_getinfo(&I,reqheight)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_INFORESP; From 96bb0c21d547b3fa403901e0d69451a89c3d4dce Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 21:58:07 -1100 Subject: [PATCH 325/378] Fixes --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_fullnode.h | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9ffd67030..d625d387a 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -229,7 +229,7 @@ int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height); len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->hdrheight),&ptr->hdrheight); len += NSPV_rwequihdr(rwflag,&serialized[len],&ptr->H); - fprintf(stderr,"hdr rwlen.%d\n",len); + //fprintf(stderr,"hdr rwlen.%d\n",len); return(len); } diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 9b89c6b7e..b496a6fb1 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -129,7 +129,8 @@ int32_t NSPV_getinfo(struct NSPV_inforesp *ptr,int32_t reqheight) return(-1); ptr->notarization = pair.prevntz; if ( reqheight == 0 ) - reqheight = pindex->GetHeight(); + reqheight = ptr->height; + ptr->hdrheight = reqheight; if ( NSPV_setequihdr(&ptr->H,reqheight) < 0 ) return(-1); return(sizeof(*ptr)); @@ -338,13 +339,13 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req { struct NSPV_inforesp I; iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight); - fprintf(stderr,"request height.%d\n",reqheight); + //fprintf(stderr,"request height.%d\n",reqheight); memset(&I,0,sizeof(I)); if ( (slen= NSPV_getinfo(&I,reqheight)) > 0 ) { response.resize(1 + slen); response[0] = NSPV_INFORESP; - fprintf(stderr,"slen.%d\n",slen); + //fprintf(stderr,"slen.%d\n",slen); if ( NSPV_rwinforesp(1,&response[1],&I) == slen ) { pfrom->PushMessage("nSPV",response); From 6b7ebf2d9f27db663ce6ce1790a30040c7bf8c80 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:02:04 -1100 Subject: [PATCH 326/378] Set fields --- src/komodo_nSPV_wallet.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 1f47a6469..47edab3b3 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -381,7 +381,8 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages { if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) { - int32_t reqht = 0; + int32_t reqht; + reqht = 0; len = 0; msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); From 2061bf4130bec4f29b6cb63fabf37ea774e82db5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:03:29 -1100 Subject: [PATCH 327/378] Default reqheight --- src/komodo_nSPV_fullnode.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index b496a6fb1..298a96ff8 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -338,7 +338,9 @@ void komodo_nSPVreq(CNode *pfrom,std::vector request) // received a req if ( timestamp > pfrom->prevtimes[ind] ) { struct NSPV_inforesp I; - iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight); + if ( len == 1+sizeof(reqheight) ) + iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight); + else reqheight = 0; //fprintf(stderr,"request height.%d\n",reqheight); memset(&I,0,sizeof(I)); if ( (slen= NSPV_getinfo(&I,reqheight)) > 0 ) From d35951cb1bef71c1b8c435af19a86f600e171f9b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:09:11 -1100 Subject: [PATCH 328/378] Initial handling of non-notarised --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 47edab3b3..bc619b40f 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -65,7 +65,7 @@ int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransactio else { NSPV_notarizations(height); // gets the prev and next notarizations - if ( NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height ) + if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) { fprintf(stderr,"issue manual bracket\n"); NSPV_notarizations(height-1); From dcbf1539a27e5fc568fb6ce44c242c6f47db5834 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:25:59 -1100 Subject: [PATCH 329/378] Rearrange code --- src/komodo_nSPV_superlite.h | 79 +++++++++++++++++++++++++++++++++++++ src/komodo_nSPV_wallet.h | 79 +------------------------------------ 2 files changed, 80 insertions(+), 78 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 41ec3c571..3710f04ac 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -445,6 +445,85 @@ UniValue NSPV_broadcast(char *hex) return(NSPV_broadcast_json(&B,txid)); } +int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) +{ + int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) + return(-1); + else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) + return(-2); + else if ( tx.GetHash() != ptr->nexttxid ) + return(-3); + else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) + return(-4); + else if ( height != ptr->common.nextht ) + return(-5); + else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) + return(-6); + for (i=ptr->common.numhdrs-1; i>0; i--) + { + blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); + if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) + return(-i-12); + } + if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) + return(-7); + else if ( tx.GetHash() != ptr->prevtxid ) + return(-8); + else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) + return(-9); + else if ( height != ptr->common.prevht ) + return(-10); + else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) + return(-11); + return(0); +} + +int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx) +{ + int32_t offset,retval = 0; + NSPV_txproof(vout,txid,height); + if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentvalue <= 0 ) + return(-1); + else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) + retval = -20; + else if ( skipvalidation == 0 ) + { + NSPV_notarizations(height); // gets the prev and next notarizations + if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) + { + fprintf(stderr,"issue manual bracket\n"); + NSPV_notarizations(height-1); + NSPV_notarizations(height+1); + NSPV_notarizations(height); // gets the prev and next notarizations + } + if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) + { + fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); + offset = (height - NSPV_ntzsresult.prevntz.height); + if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) + { + NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); + if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) + { + std::vector txids; std::vector proof; uint256 proofroot; + proof.resize(NSPV_txproofresult.txprooflen); + memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); + proofroot = BitcoinGetProofMerkleRoot(proof,txids); + if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) + { + fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); + retval = -23; + } + } + } else retval = -22; + } else retval = -2; + } + return(retval); +} + +// called from async message processing + void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response { int32_t len; uint32_t timestamp = (uint32_t)time(NULL); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index bc619b40f..e62fa4430 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,83 +20,6 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); -int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) -{ - int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; - if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) - return(-1); - else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) - return(-2); - else if ( tx.GetHash() != ptr->nexttxid ) - return(-3); - else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) - return(-4); - else if ( height != ptr->common.nextht ) - return(-5); - else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) - return(-6); - for (i=ptr->common.numhdrs-1; i>0; i--) - { - blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); - if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) - return(-i-12); - } - if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-7); - else if ( tx.GetHash() != ptr->prevtxid ) - return(-8); - else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) - return(-9); - else if ( height != ptr->common.prevht ) - return(-10); - else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) - return(-11); - return(0); -} - -int32_t NSPV_gettransaction(int32_t vout,uint256 txid,int32_t height,CTransaction &tx) -{ - int32_t offset,retval = 0; - NSPV_txproof(vout,txid,height); - if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentvalue <= 0 ) - return(-1); - else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) - retval = -20; - else - { - NSPV_notarizations(height); // gets the prev and next notarizations - if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) - { - fprintf(stderr,"issue manual bracket\n"); - NSPV_notarizations(height-1); - NSPV_notarizations(height+1); - NSPV_notarizations(height); // gets the prev and next notarizations - } - if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) - { -fprintf(stderr,"gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); - offset = (height - NSPV_ntzsresult.prevntz.height); - if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) - { - NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); - if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) - { - std::vector txids; std::vector proof; uint256 proofroot; - proof.resize(NSPV_txproofresult.txprooflen); - memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); - proofroot = BitcoinGetProofMerkleRoot(proof,txids); - if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) - { - fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); - retval = -23; - } - } - } else retval = -22; - } else retval = -2; - } - return(retval); -} - int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) { int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; @@ -239,7 +162,7 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf for (i=0; i Date: Sun, 7 Jul 2019 22:37:58 -1100 Subject: [PATCH 330/378] Guard getinfo HDR --- src/komodo_nSPV_superlite.h | 116 +++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 3710f04ac..5e1014dae 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -37,6 +37,64 @@ struct NSPV_ntzsproofresp NSPV_ntzsproofresult; struct NSPV_txproof NSPV_txproofresult; struct NSPV_broadcastresp NSPV_broadcastresult; +// komodo_nSPVresp is called from async message processing + +void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response +{ + struct NSPV_inforesp I; int32_t len; uint32_t timestamp = (uint32_t)time(NULL); + if ( (len= response.size()) > 0 ) + { + switch ( response[0] ) + { + case NSPV_INFORESP: + I = NSPV_inforesult; + NSPV_inforesp_purge(&NSPV_inforesult); + NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); + if ( NSPV_inforesult.height < I.height ) + { + fprintf(stderr,"got old info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status + NSPV_inforesp_purge(&NSPV_inforesult); + NSPV_inforesult = I; + } // else update headers state + break; + case NSPV_UTXOSRESP: + NSPV_utxosresp_purge(&NSPV_utxosresult); + NSPV_rwutxosresp(0,&response[1],&NSPV_utxosresult); + fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list + break; + case NSPV_NTZSRESP: + NSPV_ntzsresp_purge(&NSPV_ntzsresult); + NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); + fprintf(stderr,"got ntzs response %u size.%d %s prev.%d, %s next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.height); + break; + case NSPV_NTZSPROOFRESP: + NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); + NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); + fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); + break; + case NSPV_TXPROOFRESP: + NSPV_txproof_purge(&NSPV_txproofresult); + NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); + fprintf(stderr,"got txproof response %u size.%d %s ht.%d\n",timestamp,(int32_t)response.size(),NSPV_txproofresult.txid.GetHex().c_str(),NSPV_txproofresult.height); + break; + case NSPV_SPENTINFORESP: + NSPV_spentinfo_purge(&NSPV_spentresult); + NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); + fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); + break; + case NSPV_BROADCASTRESP: + NSPV_broadcast_purge(&NSPV_broadcastresult); + NSPV_rwbroadcastresp(0,&response[1],&NSPV_broadcastresult); + fprintf(stderr,"got broadcast response %u size.%d %s retcode.%d\n",timestamp,(int32_t)response.size(),NSPV_broadcastresult.txid.GetHex().c_str(),NSPV_broadcastresult.retcode); + break; + default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); + break; + } + } +} + +// superlite message issuing + CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); @@ -322,8 +380,8 @@ UniValue NSPV_addressutxos(char *coinaddr) UniValue NSPV_notarizations(int32_t height) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_ntzsresp N; - if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) - return(NSPV_ntzs_json(&NSPV_ntzsresult)); + //if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + // return(NSPV_ntzs_json(&NSPV_ntzsresult)); msg[len++] = NSPV_NTZS; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) @@ -395,8 +453,8 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) UniValue NSPV_spentinfo(uint256 txid,int32_t vout) { uint8_t msg[64]; int32_t i,len = 0; struct NSPV_spentinfo I; - if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) - return(NSPV_spentinfo_json(&NSPV_spentresult)); + //if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) + // return(NSPV_spentinfo_json(&NSPV_spentresult)); msg[len++] = NSPV_SPENTINFO; len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); @@ -522,54 +580,4 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int return(retval); } -// called from async message processing - -void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response -{ - int32_t len; uint32_t timestamp = (uint32_t)time(NULL); - if ( (len= response.size()) > 0 ) - { - switch ( response[0] ) - { - case NSPV_INFORESP: - NSPV_inforesp_purge(&NSPV_inforesult); - NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); - //fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status - break; - case NSPV_UTXOSRESP: - NSPV_utxosresp_purge(&NSPV_utxosresult); - NSPV_rwutxosresp(0,&response[1],&NSPV_utxosresult); - fprintf(stderr,"got utxos response %u size.%d\n",timestamp,(int32_t)response.size()); // update utxos list - break; - case NSPV_NTZSRESP: - NSPV_ntzsresp_purge(&NSPV_ntzsresult); - NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult); - fprintf(stderr,"got ntzs response %u size.%d %s prev.%d, %s next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.height); - break; - case NSPV_NTZSPROOFRESP: - NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); - NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult); - fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht); - break; - case NSPV_TXPROOFRESP: - NSPV_txproof_purge(&NSPV_txproofresult); - NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult); - fprintf(stderr,"got txproof response %u size.%d %s ht.%d\n",timestamp,(int32_t)response.size(),NSPV_txproofresult.txid.GetHex().c_str(),NSPV_txproofresult.height); - break; - case NSPV_SPENTINFORESP: - NSPV_spentinfo_purge(&NSPV_spentresult); - NSPV_rwspentinfo(0,&response[1],&NSPV_spentresult); - fprintf(stderr,"got spentinfo response %u size.%d\n",timestamp,(int32_t)response.size()); - break; - case NSPV_BROADCASTRESP: - NSPV_broadcast_purge(&NSPV_broadcastresult); - NSPV_rwbroadcastresp(0,&response[1],&NSPV_broadcastresult); - fprintf(stderr,"got broadcast response %u size.%d %s retcode.%d\n",timestamp,(int32_t)response.size(),NSPV_broadcastresult.txid.GetHex().c_str(),NSPV_broadcastresult.retcode); - break; - default: fprintf(stderr,"unexpected response %02x size.%d at %u\n",response[0],(int32_t)response.size(),timestamp); - break; - } - } -} - #endif // KOMODO_NSPVSUPERLITE_H From 48e0e4fe856ba1a88538504b7c1202f0d75d516d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:45:14 -1100 Subject: [PATCH 331/378] -print --- src/komodo_nSPV_fullnode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 298a96ff8..e71d47d07 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -56,7 +56,7 @@ int32_t NSPV_notarized_bracket(struct NSPV_ntzargs *prev,struct NSPV_ntzargs *ne while ( (ntzht= NSPV_notarization_find(next,txidht,1)) < height ) { nextht = next->txidht + 10*i; -fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); +//fprintf(stderr,"found forward ntz, but ntzht.%d vs height.%d, txidht.%d -> nextht.%d\n",next->ntzheight,height,txidht,nextht); memset(next,0,sizeof(*next)); txidht = nextht; if ( ntzht <= 0 ) From ddd2f1fb0e9bed3392ce11a179b1f4d6657d5c37 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 7 Jul 2019 22:54:08 -1100 Subject: [PATCH 332/378] HDR -> header --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 5e1014dae..585784eec 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -200,7 +200,7 @@ UniValue NSPV_getinfo_json(struct NSPV_inforesp *ptr) result.push_back(Pair("height",(int64_t)ptr->height)); result.push_back(Pair("chaintip",ptr->blockhash.GetHex())); result.push_back(Pair("notarization",NSPV_ntz_json(&ptr->notarization))); - result.push_back(Pair("hdr",NSPV_header_json(&ptr->H,ptr->hdrheight))); + result.push_back(Pair("header",NSPV_header_json(&ptr->H,ptr->hdrheight))); return(result); } From aac97b50995426a142cf57327c52cff0408c15a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:13:05 -1100 Subject: [PATCH 333/378] +print --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_wallet.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d625d387a..4fb2742a6 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -15,7 +15,6 @@ ******************************************************************************/ // todo: -// make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) // pubkeys extract // headers "sync" @@ -23,6 +22,7 @@ // need to validate incoming data and update only if it is valid and more recent // make sure to sanity check all vector lengths on receipt +// make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index e62fa4430..df54fc627 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -111,11 +111,11 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 remains -= up->satoshis; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); - //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; } - //fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); + fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); if ( totalinputs >= total ) return(totalinputs); return(0); From 36a0e0a9c23accd6196b17df28704b5d5e6d56af Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:13:44 -1100 Subject: [PATCH 334/378] +print --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index df54fc627..d748e4d57 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -85,7 +85,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 utxos[n++] = ptr[i]; } remains = total; - //fprintf(stderr,"n.%d for total %.8f\n",n,(double)total/COIN); + fprintf(stderr,"threshold %.8f n.%d for total %.8f\n",(double)threshold/COIN,n,(double)total/COIN); for (i=0; i0; i++) { below = above = 0; From a2b6e876af282301f6ffcc0c3ec94a61e1a873aa Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:17:55 -1100 Subject: [PATCH 335/378] Fix utxo selection bug --- src/komodo_nSPV_wallet.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d748e4d57..485d05b9f 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -81,11 +81,11 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 else threshold = total; for (i=0; i threshold ) + if ( num < NSPV_MAXVINS || ptr[i].satoshis > threshold ) utxos[n++] = ptr[i]; } remains = total; - fprintf(stderr,"threshold %.8f n.%d for total %.8f\n",(double)threshold/COIN,n,(double)total/COIN); + //fprintf(stderr,"threshold %.8f n.%d for total %.8f\n",(double)threshold/COIN,n,(double)total/COIN); for (i=0; i0; i++) { below = above = 0; @@ -115,7 +115,7 @@ int64_t NSPV_addinputs(struct NSPV_utxoresp *used,CMutableTransaction &mtx,int64 if ( totalinputs >= total || (i+1) >= maxinputs ) break; } - fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); + //fprintf(stderr,"totalinputs %.8f vs total %.8f\n",(double)totalinputs/COIN,(double)total/COIN); if ( totalinputs >= total ) return(totalinputs); return(0); From e80685bc2d98c539aa4dff5fd1f41ea089995736 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:27:03 -1100 Subject: [PATCH 336/378] Retry loop --- src/komodo_nSPV_superlite.h | 41 ++++++++++++++++++++++++++----------- src/wallet/rpcdump.cpp | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 585784eec..3caf8473e 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -97,24 +97,41 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { - int32_t flag = 0; uint32_t timestamp = (uint32_t)time(NULL); - if ( pnode == 0 ) + int32_t iter,num; CNode *firstpnode = 0; uint32_t timestamp = (uint32_t)time(NULL); + while ( pnode == 0 ) { LOCK(cs_vNodes); - BOOST_FOREACH(CNode *ptr,vNodes) + num = 0; + for (iter=0; iter<2; iter++) { - if ( ptr->prevtimes[ind] > timestamp ) - ptr->prevtimes[ind] = 0; - if ( ptr->hSocket == INVALID_SOCKET ) - continue; - if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) + BOOST_FOREACH(CNode *ptr,vNodes) { - flag = 1; - pnode = ptr; + if ( ptr->prevtimes[ind] > timestamp ) + ptr->prevtimes[ind] = 0; + if ( ptr->hSocket == INVALID_SOCKET ) + continue; + if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) + { + if ( firstpnode == 0 ) + firstpnode = ptr; + if ( iter != 0 ) + { + if ( num == 1 || (rand() % num) == 0 ) + { + pnode = ptr; + break; + } + } else num++; + } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + } + if ( iter == 0 && num == 0 ) break; - } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); + if ( pnode == 0 ) + pnode = firstpnode; } - } else flag = 1; + if ( pnode != 0 && time(NULL) > timestamp+2 ) + break; + } if ( pnode != 0 ) { std::vector request; diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 071e3d8a7..b197b73b0 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1071,7 +1071,7 @@ UniValue nspv_spend(const UniValue& params, bool fHelp) if ( NSPV_address.size() == 0 ) throw runtime_error("to nspv_send you need an active nspv_login\n"); satoshis = atof(params[1].get_str().c_str())*COIN + 0.0000000049; - fprintf(stderr,"satoshis.%lld from %.8f\n",(long long)satoshis,atof(params[1].get_str().c_str())); + //fprintf(stderr,"satoshis.%lld from %.8f\n",(long long)satoshis,atof(params[1].get_str().c_str())); if ( satoshis < 1000 ) throw runtime_error("amount too small\n"); return(NSPV_spend((char *)NSPV_address.c_str(),(char *)params[0].get_str().c_str(),satoshis)); From d43ef615ff22c8f94e56a5271fa22cef0e1730ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:34:55 -1100 Subject: [PATCH 337/378] Rand selection of peer --- src/komodo_nSPV_superlite.h | 48 ++++++++++++++----------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 3caf8473e..5210f7f87 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -97,41 +97,29 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) { - int32_t iter,num; CNode *firstpnode = 0; uint32_t timestamp = (uint32_t)time(NULL); - while ( pnode == 0 ) + int32_t n,flag = 0; CNode *pnodes[64]; uint32_t timestamp = (uint32_t)time(NULL); + if ( pnode == 0 ) { + memset(pnodes,0,sizeof(pnodes)); LOCK(cs_vNodes); - num = 0; - for (iter=0; iter<2; iter++) + n = 0; + BOOST_FOREACH(CNode *ptr,vNodes) { - BOOST_FOREACH(CNode *ptr,vNodes) + if ( ptr->prevtimes[ind] > timestamp ) + ptr->prevtimes[ind] = 0; + if ( ptr->hSocket == INVALID_SOCKET ) + continue; + if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) { - if ( ptr->prevtimes[ind] > timestamp ) - ptr->prevtimes[ind] = 0; - if ( ptr->hSocket == INVALID_SOCKET ) - continue; - if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) - { - if ( firstpnode == 0 ) - firstpnode = ptr; - if ( iter != 0 ) - { - if ( num == 1 || (rand() % num) == 0 ) - { - pnode = ptr; - break; - } - } else num++; - } //else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); - } - if ( iter == 0 && num == 0 ) - break; - if ( pnode == 0 ) - pnode = firstpnode; + flag = 1; + pnodes[n++] = ptr; + if ( n == sizeof(pnodes)/sizeof(*pnodes) ) + break; + } // else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } - if ( pnode != 0 && time(NULL) > timestamp+2 ) - break; - } + if ( n > 0 ) + pnode = pnodes[rand() % n]; + } else flag = 1; if ( pnode != 0 ) { std::vector request; From dfe68a9e2897088214c2e8f933060bb37d2077ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:41:54 -1100 Subject: [PATCH 338/378] 3 items --- src/komodo_nSPV_superlite.h | 23 +++++++++++++++-------- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 5210f7f87..c1494f721 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -336,10 +336,11 @@ UniValue NSPV_login(char *wifstr) UniValue NSPV_getinfo_req(int32_t reqht) { - uint8_t msg[64]; int32_t i,len = 0; struct NSPV_inforesp I; + uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_inforesp I; NSPV_inforesp_purge(&NSPV_inforesult); msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); + for (iter=0; iter<3; iter++,sleep(1)); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i= height ) // return(NSPV_ntzs_json(&NSPV_ntzsresult)); msg[len++] = NSPV_NTZS; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); + for (iter=0; iter<3; iter++,sleep(1)); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i> 1; data = (uint8_t *)malloc(n); decode_hex(data,n,hex); @@ -490,6 +496,7 @@ UniValue NSPV_broadcast(char *hex) memcpy(&msg[len],data,n), len += n; free(data); //fprintf(stderr,"send txid.%s\n",txid.GetHex().c_str()); + for (iter=0; iter<3; iter++,sleep(1)); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; isatoshis; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); - fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); + //fprintf(stderr,"totalinputs %.8f vs total %.8f i.%d vs max.%d\n",(double)totalinputs/COIN,(double)total/COIN,i,maxinputs); if ( totalinputs >= total || (i+1) >= maxinputs ) break; } From b2bdb24672ae6bc352c9e72445e97a87bea82276 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:47:31 -1100 Subject: [PATCH 339/378] Remove sleeps --- src/komodo_nSPV_superlite.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index c1494f721..7cf2c4fca 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -340,7 +340,7 @@ UniValue NSPV_getinfo_req(int32_t reqht) NSPV_inforesp_purge(&NSPV_inforesult); msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); - for (iter=0; iter<3; iter++,sleep(1)); + for (iter=0; iter<3; iter++); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i>1) != 0 ) { for (i=0; i Date: Mon, 8 Jul 2019 00:51:47 -1100 Subject: [PATCH 340/378] else sleep(1); --- src/komodo_nSPV_superlite.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 7cf2c4fca..c94513a40 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -349,7 +349,7 @@ UniValue NSPV_getinfo_req(int32_t reqht) if ( NSPV_inforesult.height != 0 ) return(NSPV_getinfo_json(&NSPV_inforesult)); } - } + } else sleep(1); memset(&I,0,sizeof(I)); return(NSPV_getinfo_json(&NSPV_inforesult)); } @@ -378,7 +378,7 @@ UniValue NSPV_addressutxos(char *coinaddr) if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } - } + } else sleep(1); result.push_back(Pair("result","error")); result.push_back(Pair("error","no utxos result")); return(result); @@ -400,7 +400,7 @@ UniValue NSPV_notarizations(int32_t height) if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) return(NSPV_ntzs_json(&NSPV_ntzsresult)); } - } + } else sleep(1); memset(&N,0,sizeof(N)); return(NSPV_ntzs_json(&N)); } @@ -420,7 +420,7 @@ UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid) if ( NSPV_ntzsproofresult.prevtxid == prevtxid && NSPV_ntzsproofresult.nexttxid == nexttxid ) return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); } - } + } else sleep(1); memset(&H,0,sizeof(H)); return(NSPV_ntzsproof_json(&H)); } @@ -454,7 +454,7 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) return(NSPV_txproof_json(&NSPV_txproofresult)); } - } + } else sleep(1); //fprintf(stderr,"txproof timeout\n"); memset(&P,0,sizeof(P)); return(NSPV_txproof_json(&P)); @@ -477,7 +477,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) return(NSPV_spentinfo_json(&NSPV_spentresult)); } - } + } else sleep(1); memset(&I,0,sizeof(I)); return(NSPV_spentinfo_json(&I)); } @@ -508,7 +508,7 @@ UniValue NSPV_broadcast(char *hex) return(NSPV_broadcast_json(&NSPV_broadcastresult,txid)); } } - } + } else sleep(1); free(msg); memset(&B,0,sizeof(B)); B.retcode = -2; From 79a080292d5d5fb27b42f7d94ff6e95eb64e778a Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 00:58:12 -1100 Subject: [PATCH 341/378] Add sleep --- src/komodo_nSPV_wallet.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 557ac3846..4dbcd8f7b 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -162,6 +162,8 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf for (i=0; i 0 ) + sleep(1); validation = NSPV_gettransaction(0,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); if ( validation != -1 ) // most others are degraded security From 654b87cef4f54cdd07d43ae9ccda7997b184cc87 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 01:11:02 -1100 Subject: [PATCH 342/378] 2 --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 4dbcd8f7b..232ffec8a 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -163,7 +163,7 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf { utxovout = mtx.vin[i].prevout.n; if ( i > 0 ) - sleep(1); + sleep(2); validation = NSPV_gettransaction(0,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); if ( validation != -1 ) // most others are degraded security From b184f1430302620ecab0d5434e89bd71acd9e9b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 01:42:15 -1100 Subject: [PATCH 343/378] Revert --- src/komodo_nSPV.h | 8 +++++++- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4fb2742a6..03c051b2e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -422,7 +422,13 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elected[64][33]) { - int32_t numsigs = 13; + uint256 hashBlock; CTransaction vintx; int32_t i,numsigs = 13; + /*LOCK(cs_main); + for (i=0; i 0 ) - sleep(2); + sleep(1); validation = NSPV_gettransaction(0,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); if ( validation != -1 ) // most others are degraded security From 52445b8430398321ace2a1ddd94ec0246dc7d53b Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 02:56:48 -1100 Subject: [PATCH 344/378] Slowflag into CBOPRET if --- src/gtest/test_checktransaction.cpp | 22 +++++++++++----------- src/gtest/test_transaction_builder.cpp | 4 ++-- src/komodo_nSPV.h | 3 --- src/main.cpp | 18 +++++++++--------- src/main.h | 4 ++-- src/test/transaction_tests.cpp | 4 ++-- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/gtest/test_checktransaction.cpp b/src/gtest/test_checktransaction.cpp index c6f66cc64..0efac93db 100644 --- a/src/gtest/test_checktransaction.cpp +++ b/src/gtest/test_checktransaction.cpp @@ -166,7 +166,7 @@ TEST(checktransaction_tests, BadTxnsOversize) { // ... but fails contextual ones! EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-txns-oversize", false)).Times(1); - EXPECT_FALSE(ContextualCheckTransaction(tx, state, 1, 100)); + EXPECT_FALSE(ContextualCheckTransaction(0,tx, state, 1, 100)); } { @@ -188,7 +188,7 @@ TEST(checktransaction_tests, BadTxnsOversize) { MockCValidationState state; EXPECT_TRUE(CheckTransactionWithoutProofVerification(tx, state)); - EXPECT_TRUE(ContextualCheckTransaction(tx, state, 1, 100)); + EXPECT_TRUE(ContextualCheckTransaction(0,tx, state, 1, 100)); // Revert to default UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); @@ -508,9 +508,9 @@ TEST(checktransaction_tests, bad_txns_invalid_joinsplit_signature) { MockCValidationState state; // during initial block download, DoS ban score should be zero, else 100 EXPECT_CALL(state, DoS(0, false, REJECT_INVALID, "bad-txns-invalid-joinsplit-signature", false)).Times(1); - ContextualCheckTransaction(tx, state, 0, 100, []() { return true; }); + ContextualCheckTransaction(0,tx, state, 0, 100, []() { return true; }); EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-txns-invalid-joinsplit-signature", false)).Times(1); - ContextualCheckTransaction(tx, state, 0, 100, []() { return false; }); + ContextualCheckTransaction(0,tx, state, 0, 100, []() { return false; }); } TEST(checktransaction_tests, non_canonical_ed25519_signature) { @@ -522,7 +522,7 @@ TEST(checktransaction_tests, non_canonical_ed25519_signature) { { CTransaction tx(mtx); MockCValidationState state; - EXPECT_TRUE(ContextualCheckTransaction(tx, state, 0, 100)); + EXPECT_TRUE(ContextualCheckTransaction(0,tx, state, 0, 100)); } // Copied from libsodium/crypto_sign/ed25519/ref10/open.c @@ -544,9 +544,9 @@ TEST(checktransaction_tests, non_canonical_ed25519_signature) { MockCValidationState state; // during initial block download, DoS ban score should be zero, else 100 EXPECT_CALL(state, DoS(0, false, REJECT_INVALID, "bad-txns-invalid-joinsplit-signature", false)).Times(1); - ContextualCheckTransaction(tx, state, 0, 100, []() { return true; }); + ContextualCheckTransaction(0,tx, state, 0, 100, []() { return true; }); EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-txns-invalid-joinsplit-signature", false)).Times(1); - ContextualCheckTransaction(tx, state, 0, 100, []() { return false; }); + ContextualCheckTransaction(0,tx, state, 0, 100, []() { return false; }); } TEST(checktransaction_tests, OverwinterConstructors) { @@ -801,7 +801,7 @@ TEST(checktransaction_tests, OverwinterVersionNumberHigh) { UNSAFE_CTransaction tx(mtx); MockCValidationState state; EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-tx-overwinter-version-too-high", false)).Times(1); - ContextualCheckTransaction(tx, state, 1, 100); + ContextualCheckTransaction(0,tx, state, 1, 100); // Revert to default UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); @@ -837,9 +837,9 @@ TEST(checktransaction_tests, OverwinterNotActive) { MockCValidationState state; // during initial block download, DoS ban score should be zero, else 100 EXPECT_CALL(state, DoS(0, false, REJECT_INVALID, "tx-overwinter-not-active", false)).Times(1); - ContextualCheckTransaction(tx, state, 1, 100, []() { return true; }); + ContextualCheckTransaction(0,tx, state, 1, 100, []() { return true; }); EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "tx-overwinter-not-active", false)).Times(1); - ContextualCheckTransaction(tx, state, 1, 100, []() { return false; }); + ContextualCheckTransaction(0,tx, state, 1, 100, []() { return false; }); } // This tests a transaction without the fOverwintered flag set, against the Overwinter consensus rule set. @@ -856,7 +856,7 @@ TEST(checktransaction_tests, OverwinterFlagNotSet) { CTransaction tx(mtx); MockCValidationState state; EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "tx-overwinter-flag-not-set", false)).Times(1); - ContextualCheckTransaction(tx, state, 1, 100); + ContextualCheckTransaction(0,tx, state, 1, 100); // Revert to default UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); diff --git a/src/gtest/test_transaction_builder.cpp b/src/gtest/test_transaction_builder.cpp index 05a8cb601..a04e30b0e 100644 --- a/src/gtest/test_transaction_builder.cpp +++ b/src/gtest/test_transaction_builder.cpp @@ -51,7 +51,7 @@ TEST(TransactionBuilder, Invoke) EXPECT_EQ(tx1.valueBalance, -40000); CValidationState state; - EXPECT_TRUE(ContextualCheckTransaction(tx1, state, 2, 0)); + EXPECT_TRUE(ContextualCheckTransaction(0,tx1, state, 2, 0)); EXPECT_EQ(state.GetRejectReason(), ""); // Prepare to spend the note that was just created @@ -85,7 +85,7 @@ TEST(TransactionBuilder, Invoke) EXPECT_EQ(tx2.vShieldedOutput.size(), 2); EXPECT_EQ(tx2.valueBalance, 10000); - EXPECT_TRUE(ContextualCheckTransaction(tx2, state, 3, 0)); + EXPECT_TRUE(ContextualCheckTransaction(0,tx2, state, 3, 0)); EXPECT_EQ(state.GetRejectReason(), ""); // Revert to default diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 03c051b2e..83bb98cf7 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -413,10 +413,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) rawdata.resize(datalen); memcpy(&rawdata[0],data,datalen); if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) - { - //fprintf(stderr,"extracted %s\n",tx.GetHash().GetHex().c_str()); return(0); - } else return(-1); } diff --git a/src/main.cpp b/src/main.cpp index 8f54bc71d..8ae2cd47f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1103,7 +1103,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& in * Ensure that a coinbase transaction is structured according to the consensus rules of the * chain */ -bool ContextualCheckCoinbaseTransaction(const CBlock *block,CBlockIndex * const previndex,const CTransaction& tx, const int nHeight,int32_t validateprices) +bool ContextualCheckCoinbaseTransaction(int32_t slowflag,const CBlock *block,CBlockIndex * const previndex,const CTransaction& tx, const int nHeight,int32_t validateprices) { // if time locks are on, ensure that this coin base is time locked exactly as it should be if (((uint64_t)(tx.GetValueOut()) >= ASSETCHAINS_TIMELOCKGTE) || @@ -1144,7 +1144,7 @@ bool ContextualCheckCoinbaseTransaction(const CBlock *block,CBlockIndex * const { } - else if ( ASSETCHAINS_CBOPRET != 0 && validateprices != 0 && nHeight > 0 && tx.vout.size() > 0 ) + else if ( slowflag != 0 && ASSETCHAINS_CBOPRET != 0 && validateprices != 0 && nHeight > 0 && tx.vout.size() > 0 ) { if ( komodo_opretvalidate(block,previndex,nHeight,tx.vout[tx.vout.size()-1].scriptPubKey) < 0 ) return(false); @@ -1161,7 +1161,7 @@ bool ContextualCheckCoinbaseTransaction(const CBlock *block,CBlockIndex * const * and ContextualCheckBlock (which calls this function). * 3. The isInitBlockDownload argument is only to assist with testing. */ -bool ContextualCheckTransaction(const CBlock *block, CBlockIndex * const previndex, +bool ContextualCheckTransaction(int32_t slowflag,const CBlock *block, CBlockIndex * const previndex, const CTransaction& tx, CValidationState &state, const int nHeight, @@ -1297,7 +1297,7 @@ bool ContextualCheckTransaction(const CBlock *block, CBlockIndex * const prevind if (tx.IsCoinBase()) { - if (!ContextualCheckCoinbaseTransaction(block,previndex,tx, nHeight,validateprices)) + if (!ContextualCheckCoinbaseTransaction(slowflag,block,previndex,tx, nHeight,validateprices)) return state.DoS(100, error("CheckTransaction(): invalid script data for coinbase time lock"), REJECT_INVALID, "bad-txns-invalid-script-data-for-coinbase-time-lock"); } @@ -3404,7 +3404,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin } if ( fCheckPOW != 0 && (pindex->nStatus & BLOCK_VALID_CONTEXT) != BLOCK_VALID_CONTEXT ) // Activate Jan 15th, 2019 { - if ( !ContextualCheckBlock(block, state, pindex->pprev) ) + if ( !ContextualCheckBlock(1,block, state, pindex->pprev) ) { fprintf(stderr,"ContextualCheckBlock failed ht.%d\n",(int32_t)pindex->GetHeight()); if ( pindex->nTime > 1547510400 ) @@ -5296,7 +5296,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta return true; } -bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev) +bool ContextualCheckBlock(int32_t slowflag,const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev) { const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->GetHeight() + 1; const Consensus::Params& consensusParams = Params().GetConsensus(); @@ -5307,7 +5307,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn const CTransaction& tx = block.vtx[i]; // Check transaction contextually against consensus rules at block height - if (!ContextualCheckTransaction(&block,pindexPrev,tx, state, nHeight, 100)) { + if (!ContextualCheckTransaction(slowflag,&block,pindexPrev,tx, state, nHeight, 100)) { return false; // Failure reason has been set in validation state object } @@ -5476,7 +5476,7 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C // See method docstring for why this is always disabled auto verifier = libzcash::ProofVerifier::Disabled(); - bool fContextualCheckBlock = ContextualCheckBlock(block, state, pindex->pprev); + bool fContextualCheckBlock = ContextualCheckBlock(0,block, state, pindex->pprev); if ( (!CheckBlock(futureblockp,pindex->GetHeight(),pindex,block, state, verifier,0)) || !fContextualCheckBlock ) { static int32_t saplinght = -1; @@ -5744,7 +5744,7 @@ bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex //fprintf(stderr,"TestBlockValidity failure B checkPOW.%d\n",fCheckPOW); return false; } - if (!ContextualCheckBlock(block, state, pindexPrev)) + if (!ContextualCheckBlock(0,block, state, pindexPrev)) { //fprintf(stderr,"TestBlockValidity failure C checkPOW.%d\n",fCheckPOW); return false; diff --git a/src/main.h b/src/main.h index d507f9dd6..931954da0 100644 --- a/src/main.h +++ b/src/main.h @@ -706,7 +706,7 @@ bool ContextualCheckInputs(const CTransaction& tx, CValidationState &state, cons std::vector *pvChecks = NULL); /** Check a transaction contextually against a set of consensus rules */ -bool ContextualCheckTransaction(const CBlock *block, CBlockIndex * const pindexPrev,const CTransaction& tx, CValidationState &state, int nHeight, int dosLevel, +bool ContextualCheckTransaction(int32_t slowflag,const CBlock *block, CBlockIndex * const pindexPrev,const CTransaction& tx, CValidationState &state, int nHeight, int dosLevel, bool (*isInitBlockDownload)() = IsInitialBlockDownload,int32_t validateprices=1); /** Apply the effects of this transaction on the UTXO set represented by view */ @@ -829,7 +829,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C /** Context-dependent validity checks */ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex *pindexPrev); -bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev); +bool ContextualCheckBlock(int32_t slowflag,const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev); /** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */ bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex *pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true); diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 65524a07e..b6a6ebc26 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -491,7 +491,7 @@ void test_simple_joinsplit_invalidity(uint32_t consensusBranchId, CMutableTransa jsdesc->nullifiers[1] = GetRandHash(); BOOST_CHECK(CheckTransactionWithoutProofVerification(newTx, state)); - BOOST_CHECK(!ContextualCheckTransaction(newTx, state, 0, 100)); + BOOST_CHECK(!ContextualCheckTransaction(0,newTx, state, 0, 100)); BOOST_CHECK(state.GetRejectReason() == "bad-txns-invalid-joinsplit-signature"); // Empty output script. @@ -505,7 +505,7 @@ void test_simple_joinsplit_invalidity(uint32_t consensusBranchId, CMutableTransa ) == 0); BOOST_CHECK(CheckTransactionWithoutProofVerification(newTx, state)); - BOOST_CHECK(ContextualCheckTransaction(newTx, state, 0, 100)); + BOOST_CHECK(ContextualCheckTransaction(0,newTx, state, 0, 100)); } { // Ensure that values within the joinsplit are well-formed. From de0ebde5c3ad94613ab9a60b2b00fb4c35dfddd5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 03:01:50 -1100 Subject: [PATCH 345/378] Syntax --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8ae2cd47f..4c4dbad75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1804,7 +1804,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa } // DoS level set to 10 to be more forgiving. // Check transaction contextually against the set of consensus rules which apply in the next block to be mined. - if (!fSkipExpiry && !ContextualCheckTransaction(0,0,tx, state, nextBlockHeight, (dosLevel == -1) ? 10 : dosLevel,0)) + if (!fSkipExpiry && !ContextualCheckTransaction(0,0,0,tx, state, nextBlockHeight, (dosLevel == -1) ? 10 : dosLevel,0)) { return error("AcceptToMemoryPool: ContextualCheckTransaction failed"); } From 8441b72f00368b144859e22e578379d818a2cfb7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 03:33:46 -1100 Subject: [PATCH 346/378] Enable notary sig count --- src/komodo_nSPV.h | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 83bb98cf7..765ca8745 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -417,15 +417,22 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) else return(-1); } -int32_t NSPV_pubkeysextract(uint8_t pubkeys[64][33],CTransaction tx,uint8_t elected[64][33]) +int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { - uint256 hashBlock; CTransaction vintx; int32_t i,numsigs = 13; - /*LOCK(cs_main); + uint8_t scriptPubkey[64]; int32_t i,j,scriptlen,numsigs = 0; for (i=0; i opret; + int32_t numsigs; uint8_t elected[64][33]; char *symbol; std::vector opret; if ( tx.vout.size() >= 2 ) { symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; @@ -453,7 +460,7 @@ int32_t NSPV_notarizationextract(int32_t *ntzheightp,uint256 *blockhashp,uint256 { *desttxidp = NSPV_opretextract(ntzheightp,blockhashp,symbol,opret,tx.GetHash()); komodo_notaries(elected,*ntzheightp,0); - if ( (numsigs= NSPV_pubkeysextract(sigkeys,tx,elected)) < 12 ) + if ( (numsigs= NSPV_notariescount(tx,elected)) < 12 ) return(-3); return(0); } else return(-2); From 60005cf28cc59c016fcbe3a4e1f41479279abd09 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 03:35:47 -1100 Subject: [PATCH 347/378] Script --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 765ca8745..7218d6c6e 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -419,10 +419,10 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { - uint8_t scriptPubkey[64]; int32_t i,j,scriptlen,numsigs = 0; + uint8_t script[64]; int32_t i,j,scriptlen,numsigs = 0; for (i=0; i Date: Mon, 8 Jul 2019 03:37:51 -1100 Subject: [PATCH 348/378] Script --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 7218d6c6e..d8f624a2d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -425,7 +425,7 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) if ( (scriptlen= gettxout_scriptPubKey(script,sizeof(script),tx.vin[i].prevout.hash,tx.vin[i].prevout.n)) == 35 ) { for (j=0; j<64; j++) - if ( memcmp(&scriptPubKey[1],elected[j],33) == 0 ) + if ( memcmp(&script[1],elected[j],33) == 0 ) { numsigs++; break; From 959b2e1484b3461f7a52259e138d4592606963ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 03:43:51 -1100 Subject: [PATCH 349/378] +print --- src/komodo_nSPV_superlite.h | 31 ++++++++++++++++++++++++++++++- src/komodo_nSPV_wallet.h | 25 ------------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index c94513a40..8a7e9830e 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -37,6 +37,7 @@ struct NSPV_ntzsproofresp NSPV_ntzsproofresult; struct NSPV_txproof NSPV_txproofresult; struct NSPV_broadcastresp NSPV_broadcastresult; + // komodo_nSPVresp is called from async message processing void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a response @@ -55,7 +56,11 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r fprintf(stderr,"got old info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status NSPV_inforesp_purge(&NSPV_inforesult); NSPV_inforesult = I; - } // else update headers state + } + else if ( NSPV_inforesult.height > I.height ) + { + NSPV_lastinfo = 0; + } break; case NSPV_UTXOSRESP: NSPV_utxosresp_purge(&NSPV_utxosresult); @@ -133,6 +138,30 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) return(0); } +// komodo_nSPV from main polling loop (really this belongs in its own file, but it is so small, it ended up here) + +void komodo_nSPV(CNode *pto) // polling loop from SendMessages +{ + uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); + if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+NSPV_AUTOLOGOUT ) + NSPV_logout(); + if ( (pto->nServices & NODE_NSPV) == 0 ) + return; + if ( KOMODO_NSPV != 0 ) + { + if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) + { + int32_t reqht; + reqht = 0; + len = 0; + msg[len++] = NSPV_INFO; + len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); + fprintf(stderr,"issue getinfo\n"); + NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); + } + } +} + UniValue NSPV_txproof_json(struct NSPV_txproof *ptr) { UniValue result(UniValue::VOBJ); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 4dbcd8f7b..13339122c 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -291,29 +291,4 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a } } -// polling loop (really this belongs in its own file, but it is so small, it ended up here) - -void komodo_nSPV(CNode *pto) // polling loop from SendMessages -{ - uint8_t msg[256]; int32_t i,len=0; uint32_t timestamp = (uint32_t)time(NULL); - if ( NSPV_logintime != 0 && timestamp > NSPV_logintime+NSPV_AUTOLOGOUT ) - { - NSPV_logout(); - } - if ( (pto->nServices & NODE_NSPV) == 0 ) - return; - if ( KOMODO_NSPV != 0 ) - { - if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) - { - int32_t reqht; - reqht = 0; - len = 0; - msg[len++] = NSPV_INFO; - len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); - NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); - } - } -} - #endif // KOMODO_NSPVWALLET_H From de22b66bd2a04c64ad1f1523907b5cd7db452b22 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 03:46:01 -1100 Subject: [PATCH 350/378] Rearrange --- src/komodo_nSPV_superlite.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 8a7e9830e..efa3f03c1 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -138,6 +138,19 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) return(0); } +UniValue NSPV_logout() +{ + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result","success")); + if ( NSPV_logintime != 0 ) + fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); + else result.push_back(Pair("status","wasnt logged in")); + memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); + memset(&NSPV_key,0,sizeof(NSPV_key)); + NSPV_logintime = 0; + return(result); +} + // komodo_nSPV from main polling loop (really this belongs in its own file, but it is so small, it ended up here) void komodo_nSPV(CNode *pto) // polling loop from SendMessages @@ -313,19 +326,6 @@ UniValue NSPV_broadcast_json(struct NSPV_broadcastresp *ptr,uint256 txid) return(result); } -UniValue NSPV_logout() -{ - UniValue result(UniValue::VOBJ); - result.push_back(Pair("result","success")); - if ( NSPV_logintime != 0 ) - fprintf(stderr,"scrub wif and privkey from NSPV memory\n"); - else result.push_back(Pair("status","wasnt logged in")); - memset(NSPV_wifstr,0,sizeof(NSPV_wifstr)); - memset(&NSPV_key,0,sizeof(NSPV_key)); - NSPV_logintime = 0; - return(result); -} - UniValue NSPV_login(char *wifstr) { UniValue result(UniValue::VOBJ); char coinaddr[64]; uint8_t data[128]; int32_t len,valid = 0; From d6883ab589760cafa897fb2c604050321ed19a67 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 04:12:07 -1100 Subject: [PATCH 351/378] +print --- src/komodo_nSPV.h | 4 ++-- src/komodo_nSPV_superlite.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index d8f624a2d..8d717ec98 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -430,9 +430,9 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) numsigs++; break; } - } + } else fprintf(stderr,"invalid scriptlen.%d\n",scriptlen); } - fprintf(stderr,"numsigs.%d\n",numsigs); + fprintf(stderr,"numvins.%d numsigs.%d\n",(int32_t)tx.vin.size(),numsigs); return(numsigs); } diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index efa3f03c1..5a0e152fd 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -169,7 +169,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages len = 0; msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); - fprintf(stderr,"issue getinfo\n"); + //fprintf(stderr,"issue getinfo\n"); NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } @@ -616,7 +616,7 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int } } } else retval = -22; - } else retval = -2; + } else retval = -24; } return(retval); } From 496efd01624ba8757d99861d628ffe5f17d25afd Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 04:58:56 -1100 Subject: [PATCH 352/378] Remote get --- src/komodo_nSPV.h | 32 ++++++++++++++++++++------------ src/komodo_nSPV_fullnode.h | 5 ++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 8d717ec98..9691b0eb8 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -50,6 +50,8 @@ #define NSPV_BROADCAST 0x0c #define NSPV_BROADCASTRESP 0x0d +int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx); + int32_t iguana_rwbuf(int32_t rwflag,uint8_t *serialized,uint16_t len,uint8_t *buf) { if ( rwflag != 0 ) @@ -419,21 +421,27 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { - uint8_t script[64]; int32_t i,j,scriptlen,numsigs = 0; + uint8_t *script[64]; int32_t i,j,utxovout,scriptlen,numsigs = 0; for (i=0; i opret,uint256 txid) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index e71d47d07..396fa1105 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -227,7 +227,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 ptr->txid = txid; ptr->vout = vout; ptr->height = height; - if ( (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) + if ( height != 0 && (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) { BOOST_FOREACH(const CTransaction&tx, block.vtx) { @@ -254,10 +254,9 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 } ptr->unspentvalue = CCgettxout(txid,vout,1,1); //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); - return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } } - return(-1); + return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid,uint256 nextntztxid) From 7e4c4146e6f2c3ba5c022c0645cdf709b007f6a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:02:01 -1100 Subject: [PATCH 353/378] Fix --- src/komodo_nSPV.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9691b0eb8..0ea5ddf19 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -421,7 +421,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { - uint8_t *script[64]; int32_t i,j,utxovout,scriptlen,numsigs = 0; + uint8_t *script[64]; CTransaction vintx; int32_t i,j,utxovout,scriptlen,numsigs = 0; for (i=0; i Date: Mon, 8 Jul 2019 05:04:22 -1100 Subject: [PATCH 354/378] Syntax --- src/komodo_nSPV.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 0ea5ddf19..becd08015 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -421,7 +421,7 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { - uint8_t *script[64]; CTransaction vintx; int32_t i,j,utxovout,scriptlen,numsigs = 0; + uint8_t *script; CTransaction vintx; int32_t i,j,utxovout,scriptlen,numsigs = 0; for (i=0; i opret,uint256 txid) From 28d438336ed04a3f773a4e4457df7168e30b1342 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:06:40 -1100 Subject: [PATCH 355/378] Utxovout --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index becd08015..10a2abecf 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -430,7 +430,7 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) if ( utxovout < vintx.vout.size() ) { script = (uint8_t *)&vintx.vout[utxovout].scriptPubKey[0]; - if ( (scriptlen= vintx.vout[n].scriptPubKey.size()) == 35 ) + if ( (scriptlen= vintx.vout[utxovout].scriptPubKey.size()) == 35 ) { for (j=0; j<64; j++) if ( memcmp(&script[1],elected[j],33) == 0 ) From 0219ccbe6f04844b3fcbdde5eadb1b8e59f0f0ab Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:25:52 -1100 Subject: [PATCH 356/378] +prints --- src/komodo_nSPV.h | 9 ++++++--- src/komodo_nSPV_fullnode.h | 4 ++-- src/komodo_nSPV_superlite.h | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 10a2abecf..fabfef560 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -426,7 +426,10 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { utxovout = tx.vin[i].prevout.n; if ( NSPV_gettransaction(1,utxovout,tx.vin[i].prevout.hash,0,tx) != 0 ) + { + fprintf(stderr,"error getting %s/v%d\n",tx.vin[i].prevout.hash.GetHex().c_str(),utxovout); return(numsigs); + } if ( utxovout < vintx.vout.size() ) { script = (uint8_t *)&vintx.vout[utxovout].scriptPubKey[0]; @@ -439,7 +442,7 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) break; } } else fprintf(stderr,"invalid scriptlen.%d\n",scriptlen); - } + } else fprintf(stderr,"invalid utxovout.%d vs %d\n",utxovout,(int32_t)vintx.vout.size()); } fprintf(stderr,"numvins.%d numsigs.%d\n",(int32_t)tx.vin.size(),numsigs); return(numsigs); @@ -458,7 +461,7 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: return(desttxid); } -int32_t NSPV_notarizationextract(int32_t *ntzheightp,uint256 *blockhashp,uint256 *desttxidp,CTransaction tx) +int32_t NSPV_notarizationextract(int32_t verifyntz,int32_t *ntzheightp,uint256 *blockhashp,uint256 *desttxidp,CTransaction tx) { int32_t numsigs; uint8_t elected[64][33]; char *symbol; std::vector opret; if ( tx.vout.size() >= 2 ) @@ -469,7 +472,7 @@ int32_t NSPV_notarizationextract(int32_t *ntzheightp,uint256 *blockhashp,uint256 { *desttxidp = NSPV_opretextract(ntzheightp,blockhashp,symbol,opret,tx.GetHash()); komodo_notaries(elected,*ntzheightp,0); - if ( (numsigs= NSPV_notariescount(tx,elected)) < 12 ) + if ( verifyntz != 0 && (numsigs= NSPV_notariescount(tx,elected)) < 12 ) return(-3); return(0); } else return(-2); diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 396fa1105..ff78948f6 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -265,7 +265,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid ptr->prevtxid = prevntztxid; ptr->prevntz = NSPV_getrawtx(tx,hashBlock,&ptr->prevtxlen,ptr->prevtxid); ptr->prevtxidht = komodo_blockheight(hashBlock); - if ( NSPV_notarizationextract(&ptr->common.prevht,&bhash0,&desttxid0,tx) < 0 ) + if ( NSPV_notarizationextract(0,&ptr->common.prevht,&bhash0,&desttxid0,tx) < 0 ) return(-2); else if ( komodo_blockheight(bhash0) != ptr->common.prevht ) return(-3); @@ -273,7 +273,7 @@ int32_t NSPV_getntzsproofresp(struct NSPV_ntzsproofresp *ptr,uint256 prevntztxid ptr->nexttxid = nextntztxid; ptr->nextntz = NSPV_getrawtx(tx,hashBlock,&ptr->nexttxlen,ptr->nexttxid); ptr->nexttxidht = komodo_blockheight(hashBlock); - if ( NSPV_notarizationextract(&ptr->common.nextht,&bhash1,&desttxid1,tx) < 0 ) + if ( NSPV_notarizationextract(0,&ptr->common.nextht,&bhash1,&desttxid1,tx) < 0 ) return(-5); else if ( komodo_blockheight(bhash1) != ptr->common.nextht ) return(-6); diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 5a0e152fd..eb42bedd8 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -553,7 +553,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-2); else if ( tx.GetHash() != ptr->nexttxid ) return(-3); - else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) + else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) return(-4); else if ( height != ptr->common.nextht ) return(-5); @@ -569,7 +569,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) return(-7); else if ( tx.GetHash() != ptr->prevtxid ) return(-8); - else if ( NSPV_notarizationextract(&height,&blockhash,&desttxid,tx) < 0 ) + else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) return(-9); else if ( height != ptr->common.prevht ) return(-10); From 2e56335f4332d2993ae7589a4959a0e5be40b9f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:32:11 -1100 Subject: [PATCH 357/378] +prints --- src/komodo_nSPV.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index fabfef560..87110ff5d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -463,19 +463,28 @@ uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std: int32_t NSPV_notarizationextract(int32_t verifyntz,int32_t *ntzheightp,uint256 *blockhashp,uint256 *desttxidp,CTransaction tx) { - int32_t numsigs; uint8_t elected[64][33]; char *symbol; std::vector opret; + int32_t numsigs=0; uint8_t elected[64][33]; char *symbol; std::vector opret; + fprintf(stderr,"ntz vouts[%d]\n",(int32_t)tx.vout.size()); if ( tx.vout.size() >= 2 ) { symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; GetOpReturnData(tx.vout[1].scriptPubKey,opret); - if ( opret.size() >= 32*2+4*2 ) + if ( opret.size() >= 32*2+4 ) { *desttxidp = NSPV_opretextract(ntzheightp,blockhashp,symbol,opret,tx.GetHash()); komodo_notaries(elected,*ntzheightp,0); if ( verifyntz != 0 && (numsigs= NSPV_notariescount(tx,elected)) < 12 ) + { + fprintf(stderr,"numsigs.%d error\n",numsigs); return(-3); + } return(0); - } else return(-2); + } + else + { + fprintf(stderr,"opretsize.%d error\n",(int32_t)opret.size()); + return(-2); + } } else return(-1); } #endif // KOMODO_NSPV_H From 33e1c2f08fa54ed96f75d73c6bacddb852b0d200 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:38:26 -1100 Subject: [PATCH 358/378] Set unspentvalue for ht.0 req --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_superlite.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index ff78948f6..7d74dbcf2 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -252,10 +252,10 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32 ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen); memcpy(ptr->txproof,&proof[0],ptr->txprooflen); } - ptr->unspentvalue = CCgettxout(txid,vout,1,1); //fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen)); } } + ptr->unspentvalue = CCgettxout(txid,vout,1,1); return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen); } diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index eb42bedd8..5d400def1 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -583,7 +583,10 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int int32_t offset,retval = 0; NSPV_txproof(vout,txid,height); if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentvalue <= 0 ) + { + fprintf(stderr,"txproof error value %.8f\n",(double)NSPV_txproofresult.unspentvalue/COIN); return(-1); + } else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; else if ( skipvalidation == 0 ) From 9c9bf90c45c99bb8874af37a04bf3fdd865f070c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 05:52:15 -1100 Subject: [PATCH 359/378] Test --- src/komodo_nSPV.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 87110ff5d..4cc84729b 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -425,6 +425,7 @@ int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) for (i=0; i Date: Mon, 8 Jul 2019 06:03:51 -1100 Subject: [PATCH 360/378] Test --- src/komodo_nSPV_superlite.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 5d400def1..334df4489 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -582,13 +582,15 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int { int32_t offset,retval = 0; NSPV_txproof(vout,txid,height); - if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.unspentvalue <= 0 ) + if ( NSPV_txproofresult.txid != txid ) { fprintf(stderr,"txproof error value %.8f\n",(double)NSPV_txproofresult.unspentvalue/COIN); return(-1); } else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) retval = -20; + else if ( skipvalidation == 0 && NSPV_txproofresult.unspentvalue <= 0 ) + retval = -21; else if ( skipvalidation == 0 ) { NSPV_notarizations(height); // gets the prev and next notarizations From f15cdae1854d74b8f61973a24a8cfecf82abdd0d Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:17:14 -1100 Subject: [PATCH 361/378] Use vintx! --- src/komodo_nSPV.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 4cc84729b..921879730 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -419,14 +419,21 @@ int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) else return(-1); } +/* + NSPV_notariescount is the slowest process during full validation as it requires looking up 13 transactions. + one way that would be 10000x faster would be to bruteforce validate the signatures in each vin, against all 64 pubkeys! for a valid tx, that is on average 13*32 secp256k1/sapling verify operations, which is much faster than even a single network request. + Unfortunately, due to the complexity of calculating the hash to sign for a tx, this bruteforcing would require determining what type of signature method and having sapling vs legacy methods of calculating the txhash. + It could be that the fullnode side could calculate this and send it back to the superlite side as any hash that would validate 13 different ways has to be the valid txhash. + However, since the vouts being spent by the notaries are highly constrained p2pk vouts, the txhash can be deduced if a specific notary pubkey is indeed the signer + */ + int32_t NSPV_notariescount(CTransaction tx,uint8_t elected[64][33]) { uint8_t *script; CTransaction vintx; int32_t i,j,utxovout,scriptlen,numsigs = 0; for (i=0; i opret; - fprintf(stderr,"ntz vouts[%d]\n",(int32_t)tx.vout.size()); if ( tx.vout.size() >= 2 ) { symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; From 86fe5d41d927ad164c9601fc74ed56d541b992e3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:24:32 -1100 Subject: [PATCH 362/378] +print --- src/komodo_nSPV_superlite.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 334df4489..8bf7d289d 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -48,6 +48,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: + fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status I = NSPV_inforesult; NSPV_inforesp_purge(&NSPV_inforesult); NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); From ea2fd93812937577c3394ce0a5250c1db5005f28 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:25:15 -1100 Subject: [PATCH 363/378] +print --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 8bf7d289d..328924ef3 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -170,7 +170,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages len = 0; msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); - //fprintf(stderr,"issue getinfo\n"); + fprintf(stderr,"issue getinfo\n"); NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } From b3462d0342404252c06266997e9d14db77415d95 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:28:51 -1100 Subject: [PATCH 364/378] Autoclear bad timestamp --- src/komodo_nSPV_superlite.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 328924ef3..c1af16bfc 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -161,6 +161,8 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages NSPV_logout(); if ( (pto->nServices & NODE_NSPV) == 0 ) return; + if ( pto->prevtimes[NSPV_INFO>>1] > timestamp ) + pto->prevtimes[NSPV_INFO>>1] = 0; if ( KOMODO_NSPV != 0 ) { if ( timestamp > NSPV_lastinfo + ASSETCHAINS_BLOCKTIME/2 && timestamp > pto->prevtimes[NSPV_INFO>>1] + 2*ASSETCHAINS_BLOCKTIME/3 ) From e324c8b063b8f029f6cf3a135389ef69bcca4c8b Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:34:10 -1100 Subject: [PATCH 365/378] .GetHex().c_str() --- src/komodo_nSPV_superlite.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index c1af16bfc..35b16182b 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -48,7 +48,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r switch ( response[0] ) { case NSPV_INFORESP: - fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status + //fprintf(stderr,"got info response %u size.%d height.%d\n",timestamp,(int32_t)response.size(),NSPV_inforesult.height); // update current height and ntrz status I = NSPV_inforesult; NSPV_inforesp_purge(&NSPV_inforesult); NSPV_rwinforesp(0,&response[1],&NSPV_inforesult); @@ -172,7 +172,7 @@ void komodo_nSPV(CNode *pto) // polling loop from SendMessages len = 0; msg[len++] = NSPV_INFO; len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht); - fprintf(stderr,"issue getinfo\n"); + //fprintf(stderr,"issue getinfo\n"); NSPV_req(pto,msg,len,NODE_NSPV,NSPV_INFO>>1); } } @@ -587,7 +587,7 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int NSPV_txproof(vout,txid,height); if ( NSPV_txproofresult.txid != txid ) { - fprintf(stderr,"txproof error value %.8f\n",(double)NSPV_txproofresult.unspentvalue/COIN); + fprintf(stderr,"txproof error %s != %s\n",NSPV_txproofresult.txid.GetHex().c_str(),txid.GetHex().c_str()); return(-1); } else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) From 6b78dde414a7d054996ca648b96142ee0f4ecf05 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:39:45 -1100 Subject: [PATCH 366/378] Allow ht.0 in taproot --- src/komodo_nSPV_superlite.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 35b16182b..910d3ef84 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -59,9 +59,7 @@ void komodo_nSPVresp(CNode *pfrom,std::vector response) // received a r NSPV_inforesult = I; } else if ( NSPV_inforesult.height > I.height ) - { - NSPV_lastinfo = 0; - } + NSPV_lastinfo = timestamp - ASSETCHAINS_BLOCKTIME/4; break; case NSPV_UTXOSRESP: NSPV_utxosresp_purge(&NSPV_utxosresult); @@ -476,14 +474,14 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - //fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); + fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); for (iter=0; iter<3; iter++); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { for (i=0; i Date: Mon, 8 Jul 2019 06:41:19 -1100 Subject: [PATCH 367/378] Speedup listunspent --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 910d3ef84..3c306aba5 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -405,7 +405,7 @@ UniValue NSPV_addressutxos(char *coinaddr) for (i=0; i= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 ) + if ( strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } } else sleep(1); From f94ed2c3b3a6d877d9a669dfdf346309c1b01968 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:43:51 -1100 Subject: [PATCH 368/378] Speedup --- src/komodo_nSPV.h | 4 ++-- src/komodo_nSPV_fullnode.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 921879730..9029318a0 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -27,8 +27,8 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H -#define NSPV_POLLITERS 15 -#define NSPV_POLLMICROS 100000 +#define NSPV_POLLITERS 10 +#define NSPV_POLLMICROS 100777 #define NSPV_MAXVINS 64 #define NSPV_AUTOLOGOUT 777 #define NSPV_BRANCHID 0x76b809bb diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 7d74dbcf2..2795e1c65 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -145,7 +145,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check maxlen = MAX_BLOCK_SIZE(tipheight) - 512; maxlen /= sizeof(*ptr->utxos); strncpy(ptr->coinaddr,coinaddr,sizeof(ptr->coinaddr)-1); - if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) > 0 && ptr->numutxos < maxlen ) + if ( (ptr->numutxos= (int32_t)unspentOutputs.size()) >= 0 && ptr->numutxos < maxlen ) { tipheight = chainActive.LastTip()->GetHeight(); ptr->nodeheight = tipheight; From ee5e804292cf544f9b2e9ef9cd6b5800c300ccc8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:45:27 -1100 Subject: [PATCH 369/378] Test for timeout --- src/komodo_nSPV_superlite.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 3c306aba5..f1d68002d 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -405,7 +405,7 @@ UniValue NSPV_addressutxos(char *coinaddr) for (i=0; i= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 ) return(NSPV_utxosresp_json(&NSPV_utxosresult)); } } else sleep(1); @@ -582,7 +582,13 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx) { int32_t i,offset,retval = 0; - NSPV_txproof(vout,txid,height); + for (i=0; i<3; i++) + { + NSPV_txproof(vout,txid,height); + if ( NSPV_txproofresult.txlen != 0 ) + break; + sleep(1); + } if ( NSPV_txproofresult.txid != txid ) { fprintf(stderr,"txproof error %s != %s\n",NSPV_txproofresult.txid.GetHex().c_str(),txid.GetHex().c_str()); From c35927ae31a01a49ba1c52f49eebfddc45debbf2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:51:04 -1100 Subject: [PATCH 370/378] Purge all global requests on issuing --- src/komodo_nSPV_superlite.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index f1d68002d..ff1fc7a30 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -388,6 +388,7 @@ UniValue NSPV_addressutxos(char *coinaddr) { UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,iter,slen,len = 0; //fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str()); + NSPV_utxosresp_purge(&NSPV_utxosresult); if ( bitcoin_base58decode(msg,coinaddr) != 25 ) { result.push_back(Pair("result","error")); @@ -419,6 +420,7 @@ UniValue NSPV_notarizations(int32_t height) uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N; //if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) // return(NSPV_ntzs_json(&NSPV_ntzsresult)); + NSPV_ntzsresp_purge(&NSPV_ntzsresult); msg[len++] = NSPV_NTZS; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); for (iter=0; iter<3; iter++); @@ -438,6 +440,7 @@ UniValue NSPV_notarizations(int32_t height) UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid) { uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp H; + NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); msg[len++] = NSPV_NTZSPROOF; len += iguana_rwbignum(1,&msg[len],sizeof(prevtxid),(uint8_t *)&prevtxid); len += iguana_rwbignum(1,&msg[len],sizeof(nexttxid),(uint8_t *)&nexttxid); @@ -470,6 +473,7 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_txproof P; //if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) // return(NSPV_txproof_json(&NSPV_txproofresult)); + NSPV_txproof_purge(&NSPV_txproofresult); msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); @@ -495,6 +499,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_spentinfo I; //if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) // return(NSPV_spentinfo_json(&NSPV_spentresult)); + NSPV_spentinfo_purge(&NSPV_spentinforesult); msg[len++] = NSPV_SPENTINFO; len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); @@ -515,6 +520,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) UniValue NSPV_broadcast(char *hex) { uint8_t *msg,*data; uint256 txid; uint16_t n; int32_t i,iter,len = 0; struct NSPV_broadcastresp B; + NSPV_broadcast_purge(&NSPV_broadcastresult); n = (int32_t)strlen(hex) >> 1; data = (uint8_t *)malloc(n); decode_hex(data,n,hex); From 927aca6f01faf5ca953129d376141bcc3b5a6522 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 06:53:01 -1100 Subject: [PATCH 371/378] Spent result --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index ff1fc7a30..feab3859c 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -499,7 +499,7 @@ UniValue NSPV_spentinfo(uint256 txid,int32_t vout) uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_spentinfo I; //if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout ) // return(NSPV_spentinfo_json(&NSPV_spentresult)); - NSPV_spentinfo_purge(&NSPV_spentinforesult); + NSPV_spentinfo_purge(&NSPV_spentresult); msg[len++] = NSPV_SPENTINFO; len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); From b069d6a74a703778435c0bdcd3402f6761e980b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:03:35 -1100 Subject: [PATCH 372/378] Grab proof before overwritten --- src/komodo_nSPV_fullnode.h | 2 +- src/komodo_nSPV_superlite.h | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index 2795e1c65..494c511e1 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -167,7 +167,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr) // check if ( len < maxlen ) { len = (int32_t)(sizeof(*ptr) + sizeof(*ptr->utxos)*ptr->numutxos - sizeof(ptr->utxos)); - fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); + //fprintf(stderr,"getaddressutxos for %s -> n.%d:%d total %.8f interest %.8f len.%d\n",coinaddr,n,ptr->numutxos,dstr(total),dstr(interest),len); if ( n == ptr->numutxos ) { ptr->total = total; diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index feab3859c..ef67d86d9 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -587,7 +587,7 @@ int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx) { - int32_t i,offset,retval = 0; + int32_t i,offset,retval = 0; std::vector proof; for (i=0; i<3; i++) { NSPV_txproof(vout,txid,height); @@ -606,6 +606,11 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int retval = -21; else if ( skipvalidation == 0 ) { + if ( NSPV_txproofresult.txprooflen > 0 ) + { + proof.resize(NSPV_txproofresult.txprooflen); + memcpy(&proof[0],NSPV_txproofresult.txproof,txprooflen); + } NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) { @@ -623,9 +628,7 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { - std::vector txids; std::vector proof; uint256 proofroot; - proof.resize(NSPV_txproofresult.txprooflen); - memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); + std::vector txids; uint256 proofroot; proofroot = BitcoinGetProofMerkleRoot(proof,txids); if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) { From b46f0079d93c5b45b1e2bd3543f19ef2269ee634 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:08:55 -1100 Subject: [PATCH 373/378] NSPV_txproofresult. --- src/komodo_nSPV.h | 1 + src/komodo_nSPV_superlite.h | 5 +++-- src/komodo_nSPV_wallet.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 9029318a0..a5de92f4d 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -27,6 +27,7 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H +#define NSPV_FULLVALIDATION 1 // changing to 1 issues 26 remote gettransaction, per utxo! need to change to bruteforcer #define NSPV_POLLITERS 10 #define NSPV_POLLMICROS 100777 #define NSPV_MAXVINS 64 diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index ef67d86d9..43112ed70 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -478,7 +478,7 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) len += iguana_rwnum(1,&msg[len],sizeof(height),&height); len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout); len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid); - fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); + //fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height); for (iter=0; iter<3; iter++); if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 ) { @@ -609,7 +609,8 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int if ( NSPV_txproofresult.txprooflen > 0 ) { proof.resize(NSPV_txproofresult.txprooflen); - memcpy(&proof[0],NSPV_txproofresult.txproof,txprooflen); + memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); + fprintf(stderr,"copy txproof[%d]\n",txprooflen); } NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 13339122c..d0d079e61 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -164,7 +164,7 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf utxovout = mtx.vin[i].prevout.n; if ( i > 0 ) sleep(1); - validation = NSPV_gettransaction(0,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); + validation = NSPV_gettransaction(NSPV_FULLVALIDATION,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); if ( validation != -1 ) // most others are degraded security { From 3064ea35789e6b969627e6d49488584a9a5e7459 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:10:03 -1100 Subject: [PATCH 374/378] -print --- src/komodo_nSPV_superlite.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 43112ed70..2b7f5b688 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -610,7 +610,6 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int { proof.resize(NSPV_txproofresult.txprooflen); memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); - fprintf(stderr,"copy txproof[%d]\n",txprooflen); } NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) From f02128fb82fd5629fd713265ccc1e73ce911c344 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:13:44 -1100 Subject: [PATCH 375/378] Test --- src/komodo_nSPV.h | 2 +- src/komodo_nSPV_wallet.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a5de92f4d..a84d48ac2 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -27,7 +27,7 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H -#define NSPV_FULLVALIDATION 1 // changing to 1 issues 26 remote gettransaction, per utxo! need to change to bruteforcer +#define NSPV_SKIPFULLVALIDATION 0 // changing to 0 issues 26 remote gettransaction, per utxo! need to change to bruteforcer #define NSPV_POLLITERS 10 #define NSPV_POLLMICROS 100777 #define NSPV_MAXVINS 64 diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index d0d079e61..f04cef11b 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -164,7 +164,7 @@ std::string NSPV_signtx(UniValue &retcodes,CMutableTransaction &mtx,uint64_t txf utxovout = mtx.vin[i].prevout.n; if ( i > 0 ) sleep(1); - validation = NSPV_gettransaction(NSPV_FULLVALIDATION,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); + validation = NSPV_gettransaction(NSPV_SKIPFULLVALIDATION,utxovout,mtx.vin[i].prevout.hash,used[i].height,vintx); retcodes.push_back(validation); if ( validation != -1 ) // most others are degraded security { From 5685c7f49c2abfc1ec3f486ca4c746eafca98b22 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:18:24 -1100 Subject: [PATCH 376/378] Disable full validation --- src/komodo_nSPV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index a84d48ac2..ba59252cc 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -27,7 +27,7 @@ #ifndef KOMODO_NSPV_H #define KOMODO_NSPV_H -#define NSPV_SKIPFULLVALIDATION 0 // changing to 0 issues 26 remote gettransaction, per utxo! need to change to bruteforcer +#define NSPV_SKIPFULLVALIDATION 1 // changing to 0 issues 26 remote gettransaction, per utxo! need to change to bruteforcer #define NSPV_POLLITERS 10 #define NSPV_POLLMICROS 100777 #define NSPV_MAXVINS 64 From 19f55b42ab08df53078610c616bf5a740b81add4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 07:27:52 -1100 Subject: [PATCH 377/378] Error code separation --- src/komodo_nSPV.h | 5 ++--- src/komodo_nSPV_superlite.h | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index ba59252cc..b7b38cf57 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -16,11 +16,10 @@ // todo: -// pubkeys extract -// headers "sync" +// headers "sync" make sure it connects to prior blocks to notarization. use getinfo hdrht to get missing hdrs + // interest calculations are currently just using what is returned, it should calculate it from scratch -// need to validate incoming data and update only if it is valid and more recent // make sure to sanity check all vector lengths on receipt // make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 2b7f5b688..813390c01 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -601,9 +601,9 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int return(-1); } else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) - retval = -20; + retval = -2000; else if ( skipvalidation == 0 && NSPV_txproofresult.unspentvalue <= 0 ) - retval = -21; + retval = -2001; else if ( skipvalidation == 0 ) { if ( NSPV_txproofresult.txprooflen > 0 ) @@ -633,11 +633,11 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) { fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); - retval = -23; + retval = -2003; } } - } else retval = -22; - } else retval = -24; + } else retval = -2002; + } else retval = -2004; } return(retval); } From 2d259720080d18d50c69b5f05d33529156148b55 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 8 Jul 2019 17:52:51 -1100 Subject: [PATCH 378/378] +comments --- src/komodo_nSPV.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index b7b38cf57..cba34c290 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -20,6 +20,8 @@ // interest calculations are currently just using what is returned, it should calculate it from scratch +// bruteforce sig -> NN pubkey validator +// CC signing // make sure to sanity check all vector lengths on receipt // make sure no files are updated (this is to allow nSPV=1 and later nSPV=0 without affecting database)