From 811b3ebf4a00a27ed9485152603c58b75e480fe9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 4 Dec 2014 15:38:35 -0800 Subject: [PATCH] debug "Unknown" bug. --- src/bitcoindjs.cc | 114 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 24 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 311a7b0e..1eb4f1ac 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -401,6 +401,9 @@ ListTransactions_V8(const CWalletTx& wtx, const string& strAccount, static int64_t SatoshiFromAmount(const CAmount& amount); +static int +get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx); + extern "C" void init(Handle); @@ -1118,8 +1121,7 @@ NAN_METHOD(GetTransaction) { std::string blockHash = std::string(*blockHash_); if (blockHash == "") { - blockHash = std::string( - "0000000000000000000000000000000000000000000000000000000000000000"); + blockHash = uint256(0).GetHex(); } async_tx_data *data = new async_tx_data(); @@ -1148,30 +1150,29 @@ async_get_tx(uv_work_t *req) { uint256 block_hash(data->blockHash); CTransaction ctx; - if (GetTransaction(hash, ctx, block_hash, true)) { + if (get_tx(hash, block_hash, ctx)) { data->ctx = ctx; - goto collect_prev; } else { - if (data->blockHash != "0000000000000000000000000000000000000000000000000000000000000000") { - CBlock block; - CBlockIndex* pblockindex = mapBlockIndex[block_hash]; - if (ReadBlockFromDisk(block, pblockindex)) { - BOOST_FOREACH(const CTransaction &tx, block.vtx) { - if (tx.GetHash() == hash) { - data->ctx = tx; - goto collect_prev; - } - } - } - } data->err_msg = std::string("get_tx(): failed."); } - return; - -collect_prev: - return; - +#if 0 + if (GetTransaction(hash, ctx, block_hash, true)) { + data->ctx = ctx; + } else if (block_hash != 0) { + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[block_hash]; + if (ReadBlockFromDisk(block, pblockindex)) { + BOOST_FOREACH(const CTransaction &tx, block.vtx) { + if (tx.GetHash() == hash) { + data->ctx = tx; + return; + } + } + } + data->err_msg = std::string("GetTransaction(): failed."); + } +#endif } static void @@ -5841,6 +5842,25 @@ cblock_to_jsblock(const CBlock& cblock, CBlockIndex* cblock_index, Local } } +static int +get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx) { + if (GetTransaction(txid, ctx, blockhash, true)) { + return 1; + } else if (blockhash != 0) { + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[blockhash]; + if (ReadBlockFromDisk(block, pblockindex)) { + BOOST_FOREACH(const CTransaction& tx, block.vtx) { + if (tx.GetHash() == txid) { + ctx = tx; + return -1; + } + } + } + } + return 0; +} + static inline void ctx_to_jstx(const CTransaction& ctx, uint256 block_hash, Local jstx) { // With v0.9.0 @@ -5873,17 +5893,30 @@ ctx_to_jstx(const CTransaction& ctx, uint256 block_hash, Local jstx) { Local jsprev = NanNew(); CTransaction prev_tx; + //if (get_tx(txin.prevout.hash, block_hash, prev_tx)) { if (GetTransaction(txin.prevout.hash, prev_tx, block_hash, true)) { CTxDestination from; CTxOut prev_out = prev_tx.vout[txin.prevout.n]; ExtractDestination(prev_out.scriptPubKey, from); CBitcoinAddress addrFrom(from); - jsprev->Set(NanNew("address"), NanNew(addrFrom.ToString())); jsprev->Set(NanNew("value"), NanNew((int64_t)prev_out.nValue)->ToInteger()); } else { - jsprev->Set(NanNew("address"), NanNew(std::string("Unknown"))); - jsprev->Set(NanNew("value"), NanNew(0)); + const CTxOut& txout = ctx.vout[0]; + const CScript& scriptPubKey = txout.scriptPubKey; + txnouttype type; + vector addresses; + int nRequired; + if (ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { + // Unknowns usually have the same first addr as the first output: + // https://blockexplorer.com/testnet/block/ + const CTxDestination& addr = addresses[0]; + jsprev->Set(NanNew("address"), NanNew(CBitcoinAddress(addr).ToString())); + jsprev->Set(NanNew("value"), NanNew((int64_t)txout.nValue)->ToInteger()); + } else { + jsprev->Set(NanNew("address"), NanNew(std::string("Unknown"))); + jsprev->Set(NanNew("value"), NanNew(0)); + } } in->Set(NanNew("prev"), jsprev); @@ -5934,6 +5967,39 @@ ctx_to_jstx(const CTransaction& ctx, uint256 block_hash, Local jstx) { } jstx->Set(NanNew("vout"), vout); +#if 0 + int jvi = 0; + Local jsvin = Local::Cast(jstx->Get(NanNew("vin"))); + for (; jvi < jsvin->Length(); jvi++) { + Local jsprev = Local::Cast(jsvin->Get(NanNew("prev"))); + Utf8Value jsaddr_(jsprev->Get(NanNew("address"))->ToString()); + std::string jsaddr = std::string(*jsaddr_); + if (jsaddr == "Unknown") { + Local jsvout = Local::Cast(jstx->Get(NanNew("vout"))); + Local jsspk = Local::Cast(jsvout->Get(NanNew("scriptPubKey"))); + Local jsaddrs = Local::Cast(jsvout->Get(NanNew("addresses"))); + Utf8Value jsa_(jsaddrs->Get(0)->ToString()); + std::string jsa = std::string(*jsa_); + jsprev->Set(NanNew("address"), NanNew(std::string(jsa + std::string("-fixed")))); + } + } + + const CTxOut& txout = ctx.vout[txin.prevout.n]; + for (unsigned int vo = 0; vo < ctx.vout.size(); vo++) { + const CTxOut& txout = ctx.vout[vo]; + out->Set(NanNew("n"), NanNew((unsigned int)vo)->ToUint32()); + const CScript& scriptPubKey = txout.scriptPubKey; + txnouttype type; + vector addresses; + int nRequired; + if (ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { + BOOST_FOREACH(const CTxDestination& addr, addresses) { + std::string addr = CBitcoinAddress(addr).ToString(); + } + } + } +#endif + CWalletTx cwtx(pwalletMain, ctx); // XXX Determine wether this is our transaction bool is_mine = cwtx.hashBlock != 0;