From 29cb630f6741b57e2be43f41f6b461a135ce734d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Sep 2014 12:06:04 -0700 Subject: [PATCH] cleanup tx notes. --- src/bitcoindjs.cc | 354 ---------------------------------------------- 1 file changed, 354 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index d1df9f89..070911f2 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -875,9 +875,6 @@ async_get_block_after(uv_work_t *req) { * bitcoind.getTx(hash, callback) */ -// ~/bitcoin/src/rpcwallet.cpp -// void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDepth, bool fLong, Array& ret) - NAN_METHOD(GetTx) { NanScope(); @@ -899,13 +896,6 @@ NAN_METHOD(GetTx) { std::string txHash = std::string(*txHash_); std::string blockHash = std::string(*blockHash_); - // uint256 hash = ParseHashV(params[0], "parameter 1"); - - // uint256 hashBlock(strHash); - // uint256 hashBlock = block.GetHash(); - // uint256 hashBlock = 0; - - if (txHash[1] != 'x') { txHash = "0x" + txHash; } @@ -938,8 +928,6 @@ NAN_METHOD(GetTx) { Local obj = NanNew(); obj->Set(NanNew("hex"), NanNew(strHex.c_str())); - // TxToJSON(tx, hashBlock, obj); - const unsigned argc = 2; Local argv[argc] = { Local::New(Null()), @@ -952,350 +940,8 @@ NAN_METHOD(GetTx) { } cb.Dispose(); NanReturnValue(Undefined()); - -#if 0 - const CWalletTx& wtx = pwalletMain->mapWallet[hash]; - - int64_t nFee; - string strSentAccount; - list > listReceived; - list > listSent; - - wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount); - - // Sent - if (!listSent.empty() || nFee != 0) { - BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64_t)& s, listSent) { - Object entry; - entry.push_back(Pair("account", strSentAccount)); - MaybePushAddress(entry, s.first); - entry.push_back(Pair("category", "send")); - entry.push_back(Pair("amount", ValueFromAmount(-s.second))); - entry.push_back(Pair("fee", ValueFromAmount(-nFee))); - if (fLong) - WalletTxToJSON(wtx, entry); - ret.push_back(entry); - } - } - - // Received - if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) { - BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64_t)& r, listReceived) { - string account; - if (pwalletMain->mapAddressBook.count(r.first)) { - account = pwalletMain->mapAddressBook[r.first].name; - } - Object entry; - entry.push_back(Pair("account", account)); - MaybePushAddress(entry, r.first); - if (wtx.IsCoinBase()) { - if (wtx.GetDepthInMainChain() < 1) { - entry.push_back(Pair("category", "orphan")); - } else if (wtx.GetBlocksToMaturity() > 0) { - entry.push_back(Pair("category", "immature")); - } else { - entry.push_back(Pair("category", "generate")); - } - } else { - entry.push_back(Pair("category", "receive")); - } - entry.push_back(Pair("amount", ValueFromAmount(r.second))); - if (fLong) { - WalletTxToJSON(wtx, entry); - } - ret.push_back(entry); - } - } -#endif } -#if 0 -void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry) -{ - entry.push_back(Pair("txid", tx.GetHash().GetHex())); - entry.push_back(Pair("version", tx.nVersion)); - entry.push_back(Pair("locktime", (boost::int64_t)tx.nLockTime)); - Array vin; - BOOST_FOREACH(const CTxIn& txin, tx.vin) - { - Object in; - if (tx.IsCoinBase()) - in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); - else - { - in.push_back(Pair("txid", txin.prevout.hash.GetHex())); - in.push_back(Pair("vout", (boost::int64_t)txin.prevout.n)); - Object o; - o.push_back(Pair("asm", txin.scriptSig.ToString())); - o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); - in.push_back(Pair("scriptSig", o)); - } - in.push_back(Pair("sequence", (boost::int64_t)txin.nSequence)); - vin.push_back(in); - } - entry.push_back(Pair("vin", vin)); - Array vout; - for (unsigned int i = 0; i < tx.vout.size(); i++) - { - const CTxOut& txout = tx.vout[i]; - Object out; - out.push_back(Pair("value", ValueFromAmount(txout.nValue))); - out.push_back(Pair("n", (boost::int64_t)i)); - Object o; - ScriptPubKeyToJSON(txout.scriptPubKey, o, true); - out.push_back(Pair("scriptPubKey", o)); - vout.push_back(out); - } - entry.push_back(Pair("vout", vout)); - - if (hashBlock != 0) - { - entry.push_back(Pair("blockhash", hashBlock.GetHex())); - map::iterator mi = mapBlockIndex.find(hashBlock); - if (mi != mapBlockIndex.end() && (*mi).second) - { - CBlockIndex* pindex = (*mi).second; - if (chainActive.Contains(pindex)) - { - entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight)); - entry.push_back(Pair("time", (boost::int64_t)pindex->nTime)); - entry.push_back(Pair("blocktime", (boost::int64_t)pindex->nTime)); - } - else - entry.push_back(Pair("confirmations", 0)); - } - } -} -#endif - -#if 0 -void WalletTxToJSON(const CWalletTx& wtx, Object& entry) { - int confirms = wtx.GetDepthInMainChain(); - entry.push_back(Pair("confirmations", confirms)); - if (wtx.IsCoinBase()) - entry.push_back(Pair("generated", true)); - if (confirms > 0) - { - entry.push_back(Pair("blockhash", wtx.hashBlock.GetHex())); - entry.push_back(Pair("blockindex", wtx.nIndex)); - entry.push_back(Pair("blocktime", (boost::int64_t)(mapBlockIndex[wtx.hashBlock]->nTime))); - } - uint256 hash = wtx.GetHash(); - entry.push_back(Pair("txid", hash.GetHex())); - Array conflicts; - BOOST_FOREACH(const uint256& conflict, wtx.GetConflicts()) - conflicts.push_back(conflict.GetHex()); - entry.push_back(Pair("walletconflicts", conflicts)); - entry.push_back(Pair("time", (boost::int64_t)wtx.GetTxTime())); - entry.push_back(Pair("timereceived", (boost::int64_t)wtx.nTimeReceived)); - BOOST_FOREACH(const PAIRTYPE(string,string)& item, wtx.mapValue) - entry.push_back(Pair(item.first, item.second)); -} -#endif - -#if 0 -Reading from disk: -Primary functions: -~/bitcoin/src/serialize.h - class CAutoFile; - CAutoFile() - class -~/bitcoin/src/main.cpp - OpenBlockFile() - function to read block file - OpenDiskFile() - lowlevel function to open file - FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) - { - if (pos.IsNull()) - return NULL; - boost::filesystem::path path = GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile); - boost::filesystem::create_directories(path.parent_path()); - FILE* file = fopen(path.string().c_str(), "rb+"); - if (!file && !fReadOnly) - file = fopen(path.string().c_str(), "wb+"); - if (!file) { - LogPrintf("Unable to open file %s\n", path.string()); - return NULL; - } - if (pos.nPos) { - if (fseek(file, pos.nPos, SEEK_SET)) { - LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string()); - fclose(file); - return NULL; - } - } - return file; - } - FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) { - return OpenDiskFile(pos, "blk", fReadOnly); - } - // main.h: - // pindex->GetBlockPos() - // In CBlockIndex class: - // CDiskBlockPos GetBlockPos() const { - // CDiskBlockPos ret; - // if (nStatus & BLOCK_HAVE_DATA) { - // ret.nFile = nFile; - // ret.nPos = nDataPos; - // } - // return ret; - // } - // txdb.cpp: - // bool CBlockTreeDB::LoadBlockIndexGuts() - // pindexNew->nDataPos = diskindex.nDataPos; - // CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); - // CDiskBlockIndex diskindex; - // ssValue >> diskindex; - bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos) { - block.SetNull(); - CAutoFile filein = CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); - // OpenBlockFile(pos, true); - if (!filein) { - return error("ReadBlockFromDisk : OpenBlockFile failed"); - } - // Read block - try { - filein >> block; - } catch (std::exception &e) { - return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); - } - } - // NEW: - FILE* OpenTxFile(const CDiskBlockPos &pos, bool fReadOnly) { - return OpenDiskFile(pos, "tx", fReadOnly); - } -#endif - -#if 0 -bool CBlockTreeDB::LoadBlockIndexGuts() { - leveldb::Iterator *pcursor = NewIterator(); - - CDataStream ssKeySet(SER_DISK, CLIENT_VERSION); - ssKeySet << make_pair('b', uint256(0)); - pcursor->Seek(ssKeySet.str()); - - // Load mapBlockIndex - while (pcursor->Valid()) { - boost::this_thread::interruption_point(); - try { - leveldb::Slice slKey = pcursor->key(); - CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); - char chType; - ssKey >> chType; - if (chType == 'b') { - leveldb::Slice slValue = pcursor->value(); - CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); - CDiskBlockIndex diskindex; - ssValue >> diskindex; - - // Construct block index object - CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); - pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); - pindexNew->nHeight = diskindex.nHeight; - pindexNew->nFile = diskindex.nFile; - pindexNew->nDataPos = diskindex.nDataPos; - pindexNew->nUndoPos = diskindex.nUndoPos; - pindexNew->nVersion = diskindex.nVersion; - pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot; - pindexNew->nTime = diskindex.nTime; - pindexNew->nBits = diskindex.nBits; - pindexNew->nNonce = diskindex.nNonce; - pindexNew->nStatus = diskindex.nStatus; - pindexNew->nTx = diskindex.nTx; - - if (!pindexNew->CheckIndex()) - return error("LoadBlockIndex() : CheckIndex failed: %s", pindexNew->ToString()); - - pcursor->Next(); - } else { - break; // if shutdown requested or finished loading block index - } - } catch (std::exception &e) { - return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); - } - } - delete pcursor; - - return true; -} -#endif - -#if 0 -static bool -LoadTxs() { - leveldb::Iterator *pcursor = NewIterator(); - - CDataStream ssKeySet(SER_DISK, CLIENT_VERSION); - ssKeySet << make_pair('t', uint256(0)); - pcursor->Seek(ssKeySet.str()); - - // Load mapBlockIndex - while (pcursor->Valid()) { - boost::this_thread::interruption_point(); - try { - leveldb::Slice slKey = pcursor->key(); - CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION); - char chType; - ssKey >> chType; - if (chType == 't') { - leveldb::Slice slValue = pcursor->value(); - // ~/bitcoin/src/serialize.h : - CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); - // use ssValue to grab tx! - ssValue.nType - ssValue.nVersion - ssValue.nReadPos - // CDataStream& read(char* pch, int nSize) - pcursor->Next(); - } else { - break; // if shutdown requested or finished loading block index - } - } catch (std::exception &e) { - return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); - } - } - delete pcursor; - - return true; -} -#endif - -#if 0 -// ~/bitcoin/src/txdb.cpp -bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) { - return Read(make_pair('t', txid), pos); -} -// ~/bitcoin/src/main.h -struct CDiskTxPos : public CDiskBlockPos -// ~/bitcoin/src/main.cpp -// Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock -bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow) -// ~/bitcoin/src/rpcrawtransaction.cpp -Value getrawtransaction(const Array& params, bool fHelp) { - uint256 hash = ParseHashV(params[0], "parameter 1"); - - bool fVerbose = false; - if (params.size() > 1) - fVerbose = (params[1].get_int() != 0); - - CTransaction tx; - uint256 hashBlock = 0; - if (!GetTransaction(hash, tx, hashBlock, true)) - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction"); - - CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); - ssTx << tx; - string strHex = HexStr(ssTx.begin(), ssTx.end()); - - if (!fVerbose) - return strHex; - - Object result; - result.push_back(Pair("hex", strHex)); - TxToJSON(tx, hashBlock, result); - return result; -} -#endif - - /** * Init */