Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logical timestamp indexing of block hashes #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions qa/rpc-tests/timestampindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,25 @@ def setup_network(self):
self.sync_all()

def run_test(self):
print "Mining 5 blocks..."
blockhashes = self.nodes[0].generate(5)
low = self.nodes[0].getblock(blockhashes[0])["time"]
high = self.nodes[0].getblock(blockhashes[4])["time"]
print "Mining 25 blocks..."
blockhashes = self.nodes[0].generate(25)
time.sleep(3)
print "Mining 25 blocks..."
blockhashes.extend(self.nodes[0].generate(25))
time.sleep(3)
print "Mining 25 blocks..."
blockhashes.extend(self.nodes[0].generate(25))
self.sync_all()
low = self.nodes[1].getblock(blockhashes[0])["time"]
high = low + 76

print "Checking timestamp index..."
hashes = self.nodes[1].getblockhashes(high, low)
assert_equal(len(hashes), 5)
assert_equal(sorted(blockhashes), sorted(hashes))

assert_equal(len(hashes), len(blockhashes))

assert_equal(hashes, blockhashes)

print "Passed\n"


Expand Down
23 changes: 20 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,9 +2648,26 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
if (!pblocktree->UpdateSpentIndex(spentIndex))
return AbortNode(state, "Failed to write transaction index");

if (fTimestampIndex)
if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
return AbortNode(state, "Failed to write timestamp index");
if (fTimestampIndex) {
unsigned int logicalTS = pindex->nTime;
unsigned int prevLogicalTS = 0;

// retrieve logical timestamp of the previous block
if (pindex->pprev)
if (!pblocktree->ReadBlockhashIndex(pindex->pprev->GetBlockHash(), prevLogicalTS))
LogPrintf("%s: Failed to read previous block's logical timestamp", __func__);

if (logicalTS <= prevLogicalTS) {
logicalTS = prevLogicalTS + 1;
LogPrintf("%s: Previous logical timestamp is newer Actual[%d] prevLogical[%d] Logical[%d]", __func__, pindex->nTime, prevLogicalTS, logicalTS);
}

if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(logicalTS, pindex->GetBlockHash())))
return AbortNode(state, "Failed to write timestamp index");

if (!pblocktree->WriteBlockhashIndex(CBlockhashIndexKey(pindex->GetBlockHash(), logicalTS)))
return AbortNode(state, "Failed to write blockhash index");
}

// add this block to the view's block chain
view.SetBestBlock(pindex->GetBlockHash());
Expand Down
63 changes: 63 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,69 @@ struct CTimestampIndexKey {
}
};

struct CBlockhashIndexKey {
uint256 blockHash;
unsigned int ltimestamp;

size_t GetSerializeSize(int nType, int nVersion) const {
return 36;
}

template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const {
blockHash.Serialize(s, nType, nVersion);
ser_writedata32be(s, ltimestamp);
}

template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersion) {
blockHash.Unserialize(s, nType, nVersion);
ltimestamp = ser_readdata32be(s);
}

CBlockhashIndexKey(uint256 hash, unsigned int time) {
blockHash = hash;
ltimestamp = time;
}

CBlockhashIndexKey() {
SetNull();
}

void SetNull() {
blockHash.SetNull();
ltimestamp = 0;
}
};

struct CBlockhashIndexIteratorKey {
uint256 blockHash;

size_t GetSerializeSize(int nType, int nVersion) const {
return 32;
}
template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const {
blockHash.Serialize(s, nType, nVersion);
}
template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersion) {
blockHash.Unserialize(s, nType, nVersion);
}

CBlockhashIndexIteratorKey(uint256 hash) {
blockHash = hash;
}

CBlockhashIndexIteratorKey() {
SetNull();
}

void SetNull() {
blockHash.SetNull();
}
};

struct CAddressUnspentKey {
unsigned int type;
uint160 hashBytes;
Expand Down
42 changes: 41 additions & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
Expand All @@ -24,6 +25,7 @@ static const char DB_TXINDEX = 't';
static const char DB_ADDRESSINDEX = 'a';
static const char DB_ADDRESSUNSPENTINDEX = 'u';
static const char DB_TIMESTAMPINDEX = 's';
static const char DB_BLOCKHASHINDEX = 'z';
static const char DB_SPENTINDEX = 'p';
static const char DB_BLOCK_INDEX = 'b';

Expand Down Expand Up @@ -285,7 +287,10 @@ bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned i
boost::this_thread::interruption_point();
std::pair<char, CTimestampIndexKey> key;
if (pcursor->GetKey(key) && key.first == DB_TIMESTAMPINDEX && key.second.timestamp <= high) {
hashes.push_back(key.second.blockHash);
if (blockOnchainActive(key.second.blockHash)) {
hashes.push_back(key.second.blockHash);
}

pcursor->Next();
} else {
break;
Expand All @@ -295,6 +300,31 @@ bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned i
return true;
}

bool CBlockTreeDB::WriteBlockhashIndex(const CBlockhashIndexKey &blockhashIndex) {
CDBBatch batch(&GetObfuscateKey());
batch.Write(make_pair(DB_BLOCKHASHINDEX, blockhashIndex), 0);
return WriteBatch(batch);
}

bool CBlockTreeDB::ReadBlockhashIndex(const uint256 &hash, unsigned int &ltimestamp) {
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());

pcursor->Seek(make_pair(DB_BLOCKHASHINDEX, CBlockhashIndexIteratorKey(hash)));

while (pcursor->Valid()) {
boost::this_thread::interruption_point();
std::pair<char, CBlockhashIndexKey> key;
if (pcursor->GetKey(key) && key.first == DB_BLOCKHASHINDEX && key.second.blockHash == hash) {
ltimestamp = key.second.ltimestamp;
pcursor->Next();
} else {
break;
}
}

return true;
}

bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
}
Expand All @@ -307,6 +337,16 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
return true;
}

bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) {
CBlockIndex* pblockindex = mapBlockIndex[hash];

if (!chainActive.Contains(pblockindex)) {
return false;
}

return true;
}

bool CBlockTreeDB::LoadBlockIndexGuts()
{
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
Expand Down
7 changes: 7 additions & 0 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class CBlockFileInfo;
class CBlockIndex;
class CChain;
struct CDiskTxPos;
struct CAddressUnspentKey;
struct CAddressUnspentValue;
Expand All @@ -24,9 +25,12 @@ struct CAddressIndexIteratorKey;
struct CAddressIndexIteratorHeightKey;
struct CTimestampIndexKey;
struct CTimestampIndexIteratorKey;
struct CBlockhashIndexKey;
struct CBlockhashIndexIteratorKey;
struct CSpentIndexKey;
struct CSpentIndexValue;
class uint256;
extern CChain activeChain;

//! -dbcache default (MiB)
static const int64_t nDefaultDbCache = 100;
Expand Down Expand Up @@ -78,9 +82,12 @@ class CBlockTreeDB : public CDBWrapper
int start = 0, int end = 0);
bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex);
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &vect);
bool WriteBlockhashIndex(const CBlockhashIndexKey &blockhashIndex);
bool ReadBlockhashIndex(const uint256 &hash, unsigned int &logicalTS);
bool WriteFlag(const std::string &name, bool fValue);
bool ReadFlag(const std::string &name, bool &fValue);
bool LoadBlockIndexGuts();
bool blockOnchainActive(const uint256 &hash);
};

#endif // BITCOIN_TXDB_H