123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- This file is part of cpp-ethereum.
- cpp-ethereum is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- cpp-ethereum is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
- */
- /** @file BlockChainSync.h
- * @author Gav Wood <i@gavwood.com>
- * @date 2014
- */
- #pragma once
- #include <mutex>
- #include <unordered_map>
- #include <libdevcore/Guards.h>
- #include <libethcore/Common.h>
- #include <libp2p/Common.h>
- #include "CommonNet.h"
- namespace dev
- {
- class RLPStream;
- namespace eth
- {
- class EthereumHost;
- class BlockQueue;
- class EthereumPeer;
- /**
- * @brief Base BlockChain synchronization strategy class.
- * Syncs to peers and keeps up to date. Base class handles blocks downloading but does not contain any details on state transfer logic.
- */
- class BlockChainSync: public HasInvariants
- {
- public:
- BlockChainSync(EthereumHost& _host);
- ~BlockChainSync();
- void abortSync(); ///< Abort all sync activity
- /// @returns true is Sync is in progress
- bool isSyncing() const;
- /// Restart sync
- void restartSync();
- /// Called by peer to report status
- void onPeerStatus(std::shared_ptr<EthereumPeer> _peer);
- /// Called by peer once it has new block headers during sync
- void onPeerBlockHeaders(std::shared_ptr<EthereumPeer> _peer, RLP const& _r);
- /// Called by peer once it has new block bodies
- void onPeerBlockBodies(std::shared_ptr<EthereumPeer> _peer, RLP const& _r);
- /// Called by peer once it has new block bodies
- void onPeerNewBlock(std::shared_ptr<EthereumPeer> _peer, RLP const& _r);
- void onPeerNewHashes(std::shared_ptr<EthereumPeer> _peer, std::vector<std::pair<h256, u256>> const& _hashes);
- /// Called by peer when it is disconnecting
- void onPeerAborting();
- /// @returns Synchonization status
- SyncStatus status() const;
- static char const* stateName(SyncState _s) { return s_stateNames[static_cast<int>(_s)]; }
- private:
- /// Resume downloading after witing state
- void continueSync();
- /// Called after all blocks have been donloaded
- void completeSync();
- /// Enter waiting state
- void pauseSync();
- EthereumHost& host() { return m_host; }
- EthereumHost const& host() const { return m_host; }
- void resetSync();
- void syncPeer(std::shared_ptr<EthereumPeer> _peer, bool _force);
- void requestBlocks(std::shared_ptr<EthereumPeer> _peer);
- void clearPeerDownload(std::shared_ptr<EthereumPeer> _peer);
- void clearPeerDownload();
- void collectBlocks();
- private:
- struct Header
- {
- bytes data; ///< Header data
- h256 hash; ///< Block hash
- h256 parent; ///< Parent hash
- };
- /// Used to identify header by transactions and uncles hashes
- struct HeaderId
- {
- h256 transactionsRoot;
- h256 uncles;
- bool operator==(HeaderId const& _other) const
- {
- return transactionsRoot == _other.transactionsRoot && uncles == _other.uncles;
- }
- };
- struct HeaderIdHash
- {
- std::size_t operator()(const HeaderId& _k) const
- {
- size_t seed = 0;
- h256::hash hasher;
- boost::hash_combine(seed, hasher(_k.transactionsRoot));
- boost::hash_combine(seed, hasher(_k.uncles));
- return seed;
- }
- };
- EthereumHost& m_host;
- Handler<> m_bqRoomAvailable; ///< Triggered once block queue has space for more blocks
- mutable RecursiveMutex x_sync;
- SyncState m_state = SyncState::Idle; ///< Current sync state
- h256Hash m_knownNewHashes; ///< New hashes we know about use for logging only
- unsigned m_startingBlock = 0; ///< Last block number for the start of sync
- unsigned m_highestBlock = 0; ///< Highest block number seen
- std::unordered_set<unsigned> m_downloadingHeaders; ///< Set of block body numbers being downloaded
- std::unordered_set<unsigned> m_downloadingBodies; ///< Set of block header numbers being downloaded
- std::map<unsigned, std::vector<Header>> m_headers; ///< Downloaded headers
- std::map<unsigned, std::vector<bytes>> m_bodies; ///< Downloaded block bodies
- std::map<std::weak_ptr<EthereumPeer>, std::vector<unsigned>, std::owner_less<std::weak_ptr<EthereumPeer>>> m_headerSyncPeers; ///< Peers to m_downloadingSubchain number map
- std::map<std::weak_ptr<EthereumPeer>, std::vector<unsigned>, std::owner_less<std::weak_ptr<EthereumPeer>>> m_bodySyncPeers; ///< Peers to m_downloadingSubchain number map
- std::unordered_map<HeaderId, unsigned, HeaderIdHash> m_headerIdToNumber;
- bool m_haveCommonHeader = false; ///< True if common block for our and remote chain has been found
- unsigned m_lastImportedBlock = 0; ///< Last imported block number
- h256 m_lastImportedBlockHash; ///< Last imported block hash
- u256 m_syncingTotalDifficulty; ///< Highest peer difficulty
- private:
- static char const* const s_stateNames[static_cast<int>(SyncState::Size)];
- bool invariants() const override;
- void logNewBlock(h256 const& _h);
- };
- std::ostream& operator<<(std::ostream& _out, SyncStatus const& _sync);
- }
- }
|