forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 4
Experimental: Block Filter Digest support. #1
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright (c) 2017 The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include "blockdigest.h" | ||
| #include "streams.h" | ||
| #include "version.h" | ||
| #include "uint256.h" | ||
| #include "primitives/transaction.h" | ||
| #include "utiltime.h" | ||
| #include "utilstrencodings.h" | ||
|
|
||
| namespace DigestFilter { | ||
|
|
||
| std::vector<uint8_t> base_filter::outpoint_data(const COutPoint& outpoint) const | ||
| { | ||
| CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); | ||
| stream << outpoint; | ||
| return std::vector<uint8_t>(stream.begin(), stream.end()); | ||
| } | ||
|
|
||
| std::vector<std::vector<uint8_t>> base_filter::tx_data_vec(const CTransaction& tx) const | ||
| { | ||
| std::vector<std::vector<uint8_t>> tx_data; | ||
| const uint256& hash = tx.GetHash(); | ||
| printf("- adding %s\n", hash.ToString().c_str()); | ||
| tx_data.emplace_back(hash.begin(), hash.end()); | ||
|
|
||
| for (unsigned int i = 0; i < tx.vout.size(); i++) { | ||
| const CTxOut& txout = tx.vout[i]; | ||
| CScript::const_iterator pc = txout.scriptPubKey.begin(); | ||
| std::vector<uint8_t> data; | ||
| while (pc < txout.scriptPubKey.end()) { | ||
| opcodetype opcode; | ||
| if (!txout.scriptPubKey.GetOp(pc, opcode, data)) break; | ||
| if (data.size() != 0) { | ||
| printf("- adding data: %s\n", HexStr(data).c_str()); | ||
| tx_data.emplace_back(data.begin(), data.end()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const CTxIn& txin : tx.vin) { | ||
| tx_data.emplace_back(outpoint_data(txin.prevout)); | ||
|
|
||
| CScript::const_iterator pc = txin.scriptSig.begin(); | ||
| std::vector<uint8_t> data; | ||
| while (pc < txin.scriptSig.end()) { | ||
| opcodetype opcode; | ||
| if (!txin.scriptSig.GetOp(pc, opcode, data)) break; | ||
| if (data.size() != 0) { | ||
| printf("- adding in data %s\n", HexStr(data).c_str()); | ||
| } | ||
| } | ||
| } | ||
| return tx_data; | ||
| } | ||
|
|
||
| void base_filter::digest_tx(const CTransaction& tx) | ||
| { | ||
| insert(tx_data_vec(tx)); | ||
| } | ||
|
|
||
| void base_filter::digest_block(const CBlock& block) | ||
| { | ||
| for (const CTransactionRef& tx : block.vtx) { | ||
| insert(tx_data_vec(*tx)); | ||
| } | ||
| } | ||
|
|
||
| void bloom_filter::insert(const std::vector<uint8_t>& data) | ||
| { | ||
| filter.insert(data); | ||
| } | ||
|
|
||
| bool bloom_filter::contains(const std::vector<uint8_t>& data) const | ||
| { | ||
| return filter.contains(data); | ||
| } | ||
|
|
||
| void bloom_filter::clear() | ||
| { | ||
| filter.clear(); | ||
| } | ||
|
|
||
|
|
||
| } // namespace DigestFilter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright (c) 2017 The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #ifndef _BITCOIN_SRC_BLOCKDIGEST_H_ | ||
| #define _BITCOIN_SRC_BLOCKDIGEST_H_ | ||
|
|
||
| #include <vector> | ||
| #include <boost/filesystem.hpp> | ||
|
|
||
| #include "bloom.h" | ||
| #include "serialize.h" | ||
| #include "uint256.h" | ||
| #include "primitives/block.h" | ||
| #include "chainparams.h" | ||
| #include "validation.h" | ||
|
|
||
| class COutPoint; | ||
| class CBlock; | ||
| class CTransaction; | ||
|
|
||
| namespace DigestFilter { | ||
|
|
||
| class base_filter { | ||
| public: | ||
| virtual void insert(const std::vector<uint8_t>& data) = 0; | ||
| virtual void clear() = 0; | ||
| virtual bool contains(const std::vector<uint8_t>& data) const = 0; | ||
|
|
||
| std::vector<uint8_t> outpoint_data(const COutPoint& outpoint) const; | ||
| std::vector<std::vector<uint8_t>> tx_data_vec(const CTransaction& tx) const; | ||
|
|
||
| void digest_block(const CBlock& block); | ||
| void digest_tx(const CTransaction& tx); | ||
|
|
||
| virtual void insert(const std::vector<std::vector<uint8_t>>& data_vec) { | ||
| for (const std::vector<uint8_t>& d : data_vec) insert(d); | ||
| } | ||
|
|
||
| virtual bool contains(const std::vector<std::vector<uint8_t>>& data_vec) const { | ||
| for (const std::vector<uint8_t>& d : data_vec) if (contains(d)) return true; | ||
| return false; | ||
| } | ||
|
|
||
| virtual void insert(const COutPoint& outpoint) { insert(outpoint_data(outpoint)); } | ||
| virtual void insert(const uint256& hash) { insert(std::vector<uint8_t>(hash.begin(), hash.end())); } | ||
| virtual void insert(const CTransaction& tx) { insert(tx_data_vec(tx)); } | ||
|
|
||
| virtual bool contains(const COutPoint& outpoint) const { return contains(outpoint_data(outpoint)); } | ||
| virtual bool contains(const uint256& hash) const { return contains(std::vector<uint8_t>(hash.begin(), hash.end())); } | ||
| virtual bool contains(const CTransaction& tx) const { return contains(tx_data_vec(tx)); } | ||
| }; | ||
|
|
||
| class bloom_filter : public base_filter { | ||
| private: | ||
| CBloomFilter filter; | ||
| public: | ||
| using base_filter::insert; | ||
| using base_filter::contains; | ||
|
|
||
| // TODO: obtain and use # of elements instead of using hard coded 128 value | ||
| bloom_filter() : filter(128, 0.000001, 0, BLOOM_UPDATE_ALL) {} | ||
| virtual ~bloom_filter() {} | ||
|
|
||
| void insert(const std::vector<uint8_t>& data) override; | ||
| bool contains(const std::vector<uint8_t>& data) const override; | ||
|
|
||
| void clear() override; | ||
|
|
||
| ADD_SERIALIZE_METHODS; | ||
|
|
||
| template <typename Stream, typename Operation> | ||
| inline void SerializationOp(Stream& s, Operation ser_action) | ||
| { | ||
| READWRITE(filter); | ||
| } | ||
| }; | ||
|
|
||
| template <typename F> | ||
| class digest { | ||
| public: | ||
| F filter; | ||
| int32_t block_height; | ||
| uint256 block_hash; | ||
|
|
||
| bool contains(const std::vector<uint8_t>& data) { return filter.contains(data); } | ||
| bool contains(const COutPoint& outpoint) { return filter.contains(outpoint); } | ||
| bool contains(const uint256& hash) { return filter.contains(hash); } | ||
| bool contains(const CTransaction& tx) { return filter.contains(tx); } | ||
|
|
||
| void populate(const int32_t desired_block_height) | ||
| { | ||
| CBlock block; | ||
| filter.clear(); | ||
| CBlockIndex* pindex = chainActive[desired_block_height]; | ||
| block_height = pindex->nHeight; | ||
| block_hash = pindex->phashBlock ? *pindex->phashBlock : uint256(); | ||
| const Consensus::Params& consensusParams = Params().GetConsensus(); | ||
| if (pindex) { | ||
| if (!ReadBlockFromDisk(block, pindex, consensusParams)) { | ||
| assert(!"cannot load block from disk"); | ||
| } | ||
| filter.digest_block(block); | ||
| } | ||
| } | ||
|
|
||
| ADD_SERIALIZE_METHODS; | ||
|
|
||
| template <typename Stream, typename Operation> | ||
| inline void SerializationOp(Stream& s, Operation ser_action) | ||
| { | ||
| READWRITE(block_height); | ||
| READWRITE(block_hash); | ||
| READWRITE(filter); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace DigestFilter | ||
|
|
||
| #endif // _BITCOIN_SRC_BLOCKDIGEST_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a command VERBFD, where you send the hash of a BFD and then the node returns if the hash of that BFD matches their records. This makes it cheap to get consensus from another node. Alternatively, GETBFDH which just returns the hash (or make it an arg to GETBFD).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's awesome. Thanks, will definitely do that.