diff --git a/src/chain.h b/src/chain.h index a28e4e44a..e2b03c4df 100644 --- a/src/chain.h +++ b/src/chain.h @@ -16,6 +16,7 @@ #include +uint32_t komodo_txtime(uint64_t *valuep,uint256 hash,int32_t n,char *destaddr); static const int SPROUT_VALUE_VERSION = 1001400; struct CDiskBlockPos @@ -321,6 +322,31 @@ public: //! Efficiently find an ancestor of this block. CBlockIndex* GetAncestor(int height); const CBlockIndex* GetAncestor(int height) const; + + bool IsPOSBlock() const + { + if ( ASSETCHAINS_STAKED != 0 ) + { + int32_t n,vout; uint32_t txtime; uint64_t value; char voutaddr[64],destaddr[64]; CTxDestination voutaddress; uint256 txid; + const CBlockIndex *pindex = this; + if ( (n= pindex->vtx.size()) > 1 && pblock->vtx[n-1].vin.size() == 1 && pblock->vtx[n-1].vout.size() == 1 ) + { + txid = pblock->vtx[n-1].vin[0].prevout.hash; + vout = pblock->vtx[n-1].vin[0].prevout.n; + txtime = komodo_txtime(&value,txid,vout,destaddr); + if ( ExtractDestination(pindex->vtx[n-1].vout[0].scriptPubKey,voutaddress) ) + { + strcpy(voutaddr,CBitcoinAddress(voutaddress).ToString().c_str()); + if ( strcmp(destaddr,voutaddr) == 0 && pindex->vtx[n-1].vout[0].nValue == value ) + { + fprintf(stderr,"is PoS block!\n"); + return(true); + } + } + } + } + return(false); + } }; /** Used to marshal pointers into hashes for db storage. */ diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 1c0a6414d..2c27589cc 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1328,7 +1328,7 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_uint256 bnTarget) { - CBlockIndex *previndex; char voutaddr[64],destaddr[64]; uint256 txid; uint32_t txtime,prevtime=0; int32_t vout,txn_count,eligible,isPoS = 0; uint64_t value; CTxDestination voutaddress; + CBlockIndex *previndex; char voutaddr[64],destaddr[64]; uint256 txid; uint32_t txtime,prevtime=0; int32_t vout,txn_count,eligible=0,isPoS = 0; uint64_t value; CTxDestination voutaddress; if ( ASSETCHAINS_STAKED == 100 && height <= 10 ) return(1); txn_count = pblock->vtx.size(); @@ -1343,7 +1343,8 @@ int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_ vout = pblock->vtx[txn_count-1].vin[0].prevout.n; if ( prevtime != 0 ) { - eligible = komodo_stake(1,bnTarget,height,txid,vout,pblock->nTime,prevtime,(char *)""); + if ( pblock->IsPOSBlock() != 0 ) + eligible = komodo_stake(1,bnTarget,height,txid,vout,pblock->nTime,prevtime,(char *)""); if ( eligible == 0 || eligible > pblock->nTime ) { fprintf(stderr,"komodo_is_PoSblock PoS failure ht.%d eligible.%u vs blocktime.%u, lag.%d -> check to see if it is PoW block\n",height,eligible,(uint32_t)pblock->nTime,(int32_t)(eligible - pblock->nTime)); @@ -1351,14 +1352,16 @@ int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_ } if ( slowflag == 0 ) // maybe previous block is not seen yet, do the best approx { - txtime = komodo_txtime(&value,txid,vout,destaddr); + if ( pblock->IsPOSBlock() != 0 ) + isPoS = 1; + /*txtime = komodo_txtime(&value,txid,vout,destaddr); if ( ExtractDestination(pblock->vtx[txn_count-1].vout[0].scriptPubKey,voutaddress) ) { strcpy(voutaddr,CBitcoinAddress(voutaddress).ToString().c_str()); if ( strcmp(destaddr,voutaddr) == 0 && pblock->vtx[txn_count-1].vout[0].nValue == value ) isPoS = 1; // close enough for a pre-filter //else fprintf(stderr,"komodo_is_PoSblock ht.%d (%s) != (%s) or %.8f != %.8f\n",height,destaddr,voutaddr,dstr(value),dstr(pblock->vtx[txn_count-1].vout[0].nValue)); - } else fprintf(stderr,"komodo_is_PoSblock ht.%d couldnt extract voutaddress\n",height); + } else fprintf(stderr,"komodo_is_PoSblock ht.%d couldnt extract voutaddress\n",height);*/ } //else return(-1); } //fprintf(stderr,"slow.%d ht.%d isPoS.%d\n",slowflag,height,isPoS); @@ -1443,10 +1446,10 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) return(-1); else { + if ( slowflag == 0 ) // need all past 100 blocks to calculate PoW target + return(0); if ( slowflag != 0 ) bnTarget = komodo_PoWtarget(&PoSperc,bnTarget,height,ASSETCHAINS_STAKED); - else if ( height > 100 ) - bnTarget = (bnTarget / arith_uint256(KOMODO_POWMINMULT)); // lower bound if ( bhash > bnTarget ) { for (i=31; i>=16; i--) diff --git a/src/main.cpp b/src/main.cpp index d4dfae7ae..bd6aef8ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3252,13 +3252,17 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED)) return false; - if (!fBare) { + if (!fBare) + { // Resurrect mempool transactions from the disconnected block. - BOOST_FOREACH(const CTransaction &tx, block.vtx) { + //BOOST_FOREACH(const CTransaction &tx, block.vtx) { + for (int i = 0; i < block.vtx.size(); i++) + { + CTransaction &tx = block.vtx[i]; // ignore validation errors in resurrected transactions list removed; CValidationState stateDummy; - if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL)) + if (tx.IsCoinBase() || (block.IsPOSBlock() && (i == (block.vtx.size() - 1))) || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL)) mempool.remove(tx, removed, true); } if (anchorBeforeDisconnect != anchorAfterDisconnect) { @@ -3275,11 +3279,23 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { assert(pcoinsTip->GetAnchorAt(pcoinsTip->GetBestAnchor(), newTree)); // Let wallets know transactions went from 1-confirmed to // 0-confirmed or conflicted: - BOOST_FOREACH(const CTransaction &tx, block.vtx) { - SyncWithWallets(tx, NULL); - } + //BOOST_FOREACH(const CTransaction &tx, block.vtx) { + // SyncWithWallets(tx, NULL); + //} // Update cached incremental witnesses //fprintf(stderr,"chaintip false\n"); + for (int i = 0; i < block.vtx.size(); i++) + { + CTransaction &tx = block.vtx[i]; + if (block.IsPOSBlock() && (i == (block.vtx.size() - 1))) + { + EraseFromWallets(tx.GetHash()); + } + else + { + SyncWithWallets(tx, NULL); + } + } GetMainSignals().ChainTip(pindexDelete, &block, newTree, false); return true; } diff --git a/src/miner.cpp b/src/miner.cpp index 8824959e1..c837268f9 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -939,7 +939,7 @@ void static BitcoinMiner() solutionTargetChecks.increment(); B = *pblock; h = UintToArith256(B.GetHash()); - for (z=31; z>=16; z--) + /*for (z=31; z>=16; z--) fprintf(stderr,"%02x",((uint8_t *)&h)[z]); fprintf(stderr," mined "); for (z=31; z>=16; z--) @@ -947,7 +947,7 @@ void static BitcoinMiner() fprintf(stderr," hashTarget "); for (z=31; z>=16; z--) fprintf(stderr,"%02x",((uint8_t *)&HASHTarget_POW)[z]); - fprintf(stderr," POW\n"); + fprintf(stderr," POW\n");*/ if ( h > hashTarget ) return false; if ( IS_KOMODO_NOTARY != 0 && B.nTime > GetAdjustedTime() ) diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index cd3e30f3d..a8e15db99 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -51,3 +51,7 @@ void UnregisterAllValidationInterfaces() { void SyncWithWallets(const CTransaction &tx, const CBlock *pblock) { g_signals.SyncTransaction(tx, pblock); } + +void EraseFromWallets(const uint256 &hash) { + g_signals.EraseTransaction(hash); +} diff --git a/src/validationinterface.h b/src/validationinterface.h index 1855dacd7..a1f111753 100644 --- a/src/validationinterface.h +++ b/src/validationinterface.h @@ -28,6 +28,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn); void UnregisterAllValidationInterfaces(); /** Push an updated transaction to all registered wallets */ void SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL); +void EraseFromWallets(const uint256 &hash); class CValidationInterface { protected: diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 10bf93598..1cf52d1b4 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1358,6 +1358,18 @@ bool CWallet::IsFromMe(const uint256& nullifier) const return false; } +void CWallet::EraseFromWallet(const uint256 &hash) +{ + if (!fFileBacked) + return; + { + LOCK(cs_wallet); + if (mapWallet.erase(hash)) + CWalletDB(strWalletFile).EraseTx(hash); + } + return; +} + void CWallet::GetNoteWitnesses(std::vector notes, std::vector>& witnesses, uint256 &final_anchor) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index f77a55a04..f6c8563ef 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -995,6 +995,7 @@ public: bool UpdateNullifierNoteMap(); void UpdateNullifierNoteMapWithTx(const CWalletTx& wtx); bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb); + void EraseFromWallet(const uint256 &hash); void SyncTransaction(const CTransaction& tx, const CBlock* pblock); bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate); void EraseFromWallet(const uint256 &hash);