From 615c1adfb07b9b466173166dc2e53ace540e4b32 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Wed, 16 Jun 2021 16:27:20 -0400 Subject: refactor: wrap CCoinsViewCursor in unique_ptr Specifically with CCoinsViewDB, if a raw cursor is allocated and not freed, a cryptic leveldb assertion failure occurs on CCoinsViewDB destruction. See: https://github.com/google/leveldb/issues/142#issuecomment-414418135 --- src/txdb.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/txdb.cpp') diff --git a/src/txdb.cpp b/src/txdb.cpp index 762f71feb1..bd0e9f7317 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -168,9 +168,10 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) { return Read(DB_LAST_BLOCK, nFile); } -CCoinsViewCursor *CCoinsViewDB::Cursor() const +std::unique_ptr CCoinsViewDB::Cursor() const { - CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast(*m_db).NewIterator(), GetBestBlock()); + auto i = std::make_unique( + const_cast(*m_db).NewIterator(), GetBestBlock()); /* It seems that there are no "const iterators" for LevelDB. Since we only need read operations on it, use a const-cast to get around that restriction. */ -- cgit v1.2.3 From 0f8a5a4dd530549d37c43da52c923ac3b2af1a03 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Fri, 18 Jun 2021 14:14:15 -0400 Subject: move-only(ish): don't expose CCoinsViewDBCursor No need for this to be a part of the header anymore. Includes a small reference type style change. --- src/txdb.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/txdb.cpp') diff --git a/src/txdb.cpp b/src/txdb.cpp index bd0e9f7317..e6469ee159 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -168,6 +168,28 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) { return Read(DB_LAST_BLOCK, nFile); } +/** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */ +class CCoinsViewDBCursor: public CCoinsViewCursor +{ +public: + CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256&hashBlockIn): + CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {} + ~CCoinsViewDBCursor() {} + + bool GetKey(COutPoint &key) const override; + bool GetValue(Coin &coin) const override; + unsigned int GetValueSize() const override; + + bool Valid() const override; + void Next() override; + +private: + std::unique_ptr pcursor; + std::pair keyTmp; + + friend class CCoinsViewDB; +}; + std::unique_ptr CCoinsViewDB::Cursor() const { auto i = std::make_unique( -- cgit v1.2.3 From 7ad414f4bfa74595ee5726e66f3527045c02a977 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Fri, 18 Jun 2021 14:15:39 -0400 Subject: doc: add comment about CCoinsViewDBCursor constructor --- src/txdb.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/txdb.cpp') diff --git a/src/txdb.cpp b/src/txdb.cpp index e6469ee159..4b76bee5ab 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -172,6 +172,8 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) { class CCoinsViewDBCursor: public CCoinsViewCursor { public: + // Prefer using CCoinsViewDB::Cursor() since we want to perform some + // cache warmup on instantiation. CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256&hashBlockIn): CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {} ~CCoinsViewDBCursor() {} -- cgit v1.2.3