From 1765f2eb7ddb108dd8a995fabd1b52ee54e64dc0 Mon Sep 17 00:00:00 2001 From: etotheipi Date: Sun, 15 Jan 2012 15:40:04 -0500 Subject: [PATCH] Working on the zero-conf updates --- cppForSwig/BinaryData.h | 4 +- cppForSwig/BlockObj.h | 2 + cppForSwig/BlockUtils.cpp | 142 ++++++++++++++++++++++++++++++++++++++ cppForSwig/BlockUtils.h | 12 +++- qtdialogs.py | 11 ++- 5 files changed, 160 insertions(+), 11 deletions(-) diff --git a/cppForSwig/BinaryData.h b/cppForSwig/BinaryData.h index a95395c3e..e40af3ca1 100644 --- a/cppForSwig/BinaryData.h +++ b/cppForSwig/BinaryData.h @@ -888,7 +888,7 @@ class BinaryReader uint32_t getSizeRemaining(void) const { return totalSize_ - pos_; } bool isEndOfStream(void) const { return pos_ >= totalSize_; } uint8_t* exposeDataPtr(void) { return bdStr_.getPtr(); } - uint8_t const * getCurrPtr(void) { return bdStr_.getPtr() + pos_; } + uint8_t const * getCurrPtr(void) { return bdStr_.getPtr() + pos_; } private: BinaryData bdStr_; @@ -1018,7 +1018,7 @@ class BinaryRefReader uint32_t getSizeRemaining(void) const { return totalSize_ - pos_; } bool isEndOfStream(void) const { return pos_ >= totalSize_; } uint8_t const * exposeDataPtr(void) { return bdRef_.getPtr(); } - uint8_t const * getCurrPtr(void) { return bdRef_.getPtr() + pos_; } + uint8_t const * getCurrPtr(void) { return bdRef_.getPtr() + pos_; } private: BinaryDataRef bdRef_; diff --git a/cppForSwig/BlockObj.h b/cppForSwig/BlockObj.h index 67d4ba04e..da4d11f43 100644 --- a/cppForSwig/BlockObj.h +++ b/cppForSwig/BlockObj.h @@ -304,6 +304,8 @@ class Tx }; + + //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // This class is mainly for sorting by priority diff --git a/cppForSwig/BlockUtils.cpp b/cppForSwig/BlockUtils.cpp index 4913a6425..926469c41 100644 --- a/cppForSwig/BlockUtils.cpp +++ b/cppForSwig/BlockUtils.cpp @@ -7,6 +7,7 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include "BlockUtils.h" @@ -823,6 +824,12 @@ BlockDataManager_FullRAM::BlockDataManager_FullRAM(void) : blockchainData_NEW_.clear(); headerHashMap_.clear(); txHashMap_.clear(); + + zeroConfTxList_.clear(); + zeroConfMap_.clear(); + zcEnabled_ = false; + zcFilename_ = string(""); + headersByHeight_.clear(); txFileRefs_.clear(); headerFileRefs_.clear(); @@ -852,6 +859,7 @@ void BlockDataManager_FullRAM::SetBtcNetworkParams( } + void BlockDataManager_FullRAM::SelectNetwork(string netName) { if(netName.compare("Main") == 0) @@ -2098,5 +2106,139 @@ BlockDataManager_FullRAM::getNonStdUnspentTxOutsForWallet( BtcWallet & wlt) cout << "Not implemented yet to retrieve non-std TxOuts..."<< endl; return vector(0); } + + + +//////////////////////////////////////////////////////////////////////////////// +// Methods for handling zero-confirmation transactions +//////////////////////////////////////////////////////////////////////////////// +void BlockDataManager_FullRAM::enableZeroConf(string zcFilename) +{ + zcEnabled_ = true; + zcFilename_ = zcFilename; + + readZeroConfFile(zcFilename_); // does nothing if DNE +} + +//////////////////////////////////////////////////////////////////////////////// +void BlockDataManager_FullRAM::readZeroConfFile(string zcFilename) +{ + ifstream zcFile(zcFilename_, ios::in | ios::binary); + if(zcFile) + { + zcFile.seekg(0, ios::end); + uint64_t filesize = (size_t)zcFile.tellg(); + zcFile.seekg(0, ios::beg); + BinaryData zcData(filesize); + zcFile.read((char*)zcData.getPtr(), filesize); + + // We succeeded opening the file... + BinaryRefReader brr(zcData); + while(brr.getSizeRemaining() > 8) + { + uint64_t txTime = brr.get_uint64_t(); + uint32_t txLen = BtcUtils::TxCalcLength(brr.getCurrPtr()); + BinaryData rawtx(txLen); + brr.get_BinaryData(rawtx.getPtr(), txLen); + } + zcFile.close(); + } + //brr.isEndOfStream() || +} + +//////////////////////////////////////////////////////////////////////////////// +void BlockDataManager_FullRAM::disableZeroConf(string zcFilename) +{ + zcEnabled_ = false; +} + + +//////////////////////////////////////////////////////////////////////////////// +void BlockDataManager_FullRAM::addNewZeroConfTx(BinaryData const & rawTx, + uint64_t txtime, + bool writeToFile) +{ + + if(txtime==0) + txtime = time(NULL); + + BinaryData txHash = BtcUtils::getHash256(rawTx) + if(zeroConfMap_.find(txHash) != zeroConfMap_.end()) + return; + + + zeroConfMap_[txHash] = ZeroConfData(); + ZeroConfData & zc = zeroConfMap_[txHash]; + zc.iter_ = zeroConfTxList_.insert(rawTx); + zc.txref_.unserialize(*newTxData); + zc.txtime_ = txtime; + + + // Record time. Write to file + if(writeToFile) + { + ofstream zcFile(zcFilename_, ios::app | ios::binary); + zcFile.write( (char*)(&zc.txtime_), sizeof(uint64_t) ); + zcFile.write( (char*)(&zc.txref_.getPtr()), zc.txref_.getSize()) + zcFile.close(); + } +} + + + +//////////////////////////////////////////////////////////////////////////////// +void BlockDataManager_FullRAM::purgeZeroConfPool(void) +{ + list< map::iterator > mapRmList; + + map::iterator iter; + for(iter = zeroConfMap_.begin(); + iter != zeroConfMap_.end(); + iter++) + { + TxRef* txInBlockchain = getTxByHash(iter->first); + if(txInBlockchain != NULL) + mapRmList.push_back(ter); + } + + + list< map::iterator >::iterator iter; + for(iter = mapRmList.begin(); + iter != mapRmList.end(); + iter++) + { + zeroConfTxList_.erase( (*iter)->second.iter_ ); + zeroConfMap_.erase( *iter ) + } +} + +void BlockDataManager_FullRAM::updateWalletWithZeroConf(BtcWallet & wlt) +{ + +} + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cppForSwig/BlockUtils.h b/cppForSwig/BlockUtils.h index 26bbbcd42..058669c92 100644 --- a/cppForSwig/BlockUtils.h +++ b/cppForSwig/BlockUtils.h @@ -396,6 +396,14 @@ class BtcWallet +class ZeroConfData +{ +public: + TxRef txref_; + uint64_t txtime_; + list::iterator iter_; +}; + // Some might argue that inheritance would be useful here. I'm not a software // guy, and I have to write all the methods for each class anyway. So I'm @@ -494,9 +502,7 @@ class BlockDataManager_FullRAM // We need the second map to make sure we can find the data to remove // it, when necessary list zeroConfTxList_; - map zeroConfTxRefMap_; - map::iterator> zeroConfIterMap_; + map zeroConfMap_; bool zcEnabled_; string zcFilename_; diff --git a/qtdialogs.py b/qtdialogs.py index e8ddcdf37..085fc0e6d 100755 --- a/qtdialogs.py +++ b/qtdialogs.py @@ -1572,7 +1572,7 @@ def processUserString(self): if not TheBDM.isInitialized(): reply = QMessageBox.critical(self, 'Cannot Sweep Address', \ - 'You need access to the internet and the blockchain in order ' + 'You need access to the Bitcoin network and the blockchain in order ' 'to find the balance of this address and sweep its funds. ', \ QMessageBox.Ok) return @@ -3796,9 +3796,9 @@ def makeRecipFrame(self, nRecip): self.widgetTable[-1].append( QLabel('Address %d:' % (i+1,)) ) self.widgetTable[-1].append( QLineEdit() ) - self.widgetTable[-1][-1].setMinimumWidth(relaxedSizeNChar(GETFONT('var'), 33)[0]) + self.widgetTable[-1][-1].setMinimumWidth(relaxedSizeNChar(GETFONT('var'), 40)[0]) self.widgetTable[-1][-1].setMaximumHeight(self.maxHeight) - self.widgetTable[-1][-1].setFont(GETFONT('var')) + self.widgetTable[-1][-1].setFont(GETFONT('var',9)) self.widgetTable[-1].append( QLabel('BTC:') ) @@ -3809,9 +3809,8 @@ def makeRecipFrame(self, nRecip): self.widgetTable[-1][-1].setAlignment(Qt.AlignRight) self.widgetTable[-1].append( QLabel('Comment:') ) - self.widgetTable[-1].append( QLineEdit() ) - self.widgetTable[-1][-1].setFont(GETFONT('var')) + self.widgetTable[-1][-1].setFont(GETFONT('var', 9)) self.widgetTable[-1][-1].setMaximumHeight(self.maxHeight) if i[None]'))