improve get_block_by_tx.

This commit is contained in:
Christopher Jeffrey 2014-12-10 16:22:30 -08:00
parent 20934c285e
commit 78aceaf12e

View File

@ -192,7 +192,7 @@ using namespace v8;
// LevelDB options // LevelDB options
#define USE_LDB_ADDR 0 #define USE_LDB_ADDR 0
#define USE_LDB_TX 0 #define USE_LDB_TX 1
static termios orig_termios; static termios orig_termios;
@ -479,6 +479,7 @@ struct async_block_tx_data {
std::string txid; std::string txid;
CBlock cblock; CBlock cblock;
CBlockIndex* cblock_index; CBlockIndex* cblock_index;
CTransaction ctx;
Persistent<Function> callback; Persistent<Function> callback;
}; };
@ -613,7 +614,7 @@ read_addr(const std::string addr, const int64_t blockheight, const int64_t block
#if USE_LDB_TX #if USE_LDB_TX
static bool static bool
get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_index); get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_index, CTransaction& rctx);
#endif #endif
#if 0 #if 0
@ -2230,10 +2231,11 @@ parse:
CBlockIndex* pblockindex = chainActive[i]; CBlockIndex* pblockindex = chainActive[i];
CBlock cblock; CBlock cblock;
if (ReadBlockFromDisk(cblock, pblockindex)) { if (ReadBlockFromDisk(cblock, pblockindex)) {
BOOST_FOREACH(const CTransaction& ctx, cblock.vtx) { BOOST_FOREACH(const CTransaction& tx, cblock.vtx) {
if (ctx.GetHash().GetHex() == data->txid) { if (tx.GetHash().GetHex() == data->txid) {
data->cblock = cblock; data->cblock = cblock;
data->cblock_index = pblockindex; data->cblock_index = pblockindex;
data->ctx = tx;
return; return;
} }
} }
@ -2243,12 +2245,7 @@ parse:
return; return;
#if USE_LDB_TX #if USE_LDB_TX
} }
CBlock cblock; if (!get_block_by_tx(data->txid, data->cblock, &data->cblock_index, data->ctx)) {
CBlockIndex *cblock_index;
if (get_block_by_tx(data->txid, cblock, &cblock_index)) {
data->cblock = cblock;
data->cblock_index = cblock_index;
} else {
goto parse; goto parse;
} }
#endif #endif
@ -2271,14 +2268,19 @@ async_block_tx_after(uv_work_t *req) {
} else { } else {
const CBlock& cblock = data->cblock; const CBlock& cblock = data->cblock;
CBlockIndex* cblock_index = data->cblock_index; CBlockIndex* cblock_index = data->cblock_index;
const CTransaction& ctx = data->ctx;
Local<Object> jsblock = NanNew<Object>(); Local<Object> jsblock = NanNew<Object>();
cblock_to_jsblock(cblock, cblock_index, jsblock, false); cblock_to_jsblock(cblock, cblock_index, jsblock, false);
Local<Object> jstx = NanNew<Object>();
ctx_to_jstx(ctx, cblock.GetHash(), jstx);
const unsigned argc = 2; const unsigned argc = 2;
Local<Value> argv[argc] = { Local<Value> argv[argc] = {
Local<Value>::New(Null()), Local<Value>::New(Null()),
Local<Value>::New(jsblock) Local<Value>::New(jsblock),
Local<Value>::New(jstx)
}; };
TryCatch try_catch; TryCatch try_catch;
data->callback->Call(Context::GetCurrent()->Global(), argc, argv); data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
@ -6541,20 +6543,15 @@ error:
#if USE_LDB_TX #if USE_LDB_TX
static bool static bool
get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_index) { get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_index, CTransaction& rctx) {
leveldb::Iterator* pcursor = pblocktree->pdb->NewIterator(pblocktree->iteroptions); const char *txkey = std::string(std::string("t") + itxid).c_str();
std::string slValue;
//leveldb::Slice slValue;
pcursor->SeekToFirst(); pblocktree->pdb->Get(leveldb::ReadOptions(), txkey, &slValue);
while (pcursor->Valid()) { CDataStream ssValue(slValue.begin(), slValue.end(), SER_DISK, CLIENT_VERSION);
boost::this_thread::interruption_point(); //CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
try {
leveldb::Slice slKey = pcursor->key();
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
char type;
ssKey >> type;
// Blockchain Index Structure: // Blockchain Index Structure:
// http://bitcoin.stackexchange.com/questions/28168 // http://bitcoin.stackexchange.com/questions/28168
@ -6564,13 +6561,6 @@ get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_
// Which block file the tx is stored in // Which block file the tx is stored in
// Which offset in the block file the tx resides // Which offset in the block file the tx resides
// The offset from the top of the block containing the tx // The offset from the top of the block containing the tx
if (type == 't') {
uint256 txid;
ssKey >> txid;
if (txid.GetHex() == itxid) {
leveldb::Slice slValue = pcursor->value();
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
// struct CDiskBlockPos { // struct CDiskBlockPos {
// int nFile; // int nFile;
@ -6608,23 +6598,10 @@ get_block_by_tx(const std::string itxid, CBlock& rcblock, CBlockIndex **rcblock_
if (ReadBlockFromDisk(rcblock, pblockindex)) { if (ReadBlockFromDisk(rcblock, pblockindex)) {
*rcblock_index = pblockindex; *rcblock_index = pblockindex;
delete pcursor; rctx = ctx;
return true; return true;
} }
goto error;
}
}
pcursor->Next();
} catch (std::exception &e) {
delete pcursor;
return false;
}
}
error:
delete pcursor;
return false; return false;
} }
#endif #endif