10 #include <validation.h> 12 #include <test/util/setup_common.h> 14 #include <boost/test/unit_test.hpp> 26 const std::vector<Slot> oneslot = {{100, 100, 23}};
44 const std::vector<Slot> twoslots = {{100, 100, 69}, {300, 100, 42}};
73 std::vector<Slot> slots;
77 for (
int i = 0; i < 100; i++) {
78 slots.emplace_back(max, 1, i);
85 for (
int i = 0; i < 100; i++) {
93 slots[99] = slots[99].withScore(101);
94 max = slots[99].getStop();
97 for (
int i = 0; i < 100; i++) {
108 for (
int i = 0; i < 100; i++) {
109 slots[i] = slots[i].withStart(slots[i].getStart() + 100);
112 slots[0] =
Slot(1, slots[0].getStop() - 1, slots[0].getPeerId());
113 slots[99] = slots[99].withScore(1);
114 max = slots[99].getStop();
121 for (
int i = 0; i < 100; i++) {
128 for (
int c = 0; c < 1000; c++) {
129 size_t size = InsecureRandBits(10) + 1;
130 std::vector<Slot> slots;
133 uint64_t max = InsecureRandBits(3);
136 max += InsecureRandBits(3);
140 for (
size_t i = 0; i < size; i++) {
141 const uint64_t start = next();
142 const uint32_t score = InsecureRandBits(3);
144 slots.emplace_back(start, score, i);
147 for (
int k = 0; k < 100; k++) {
148 uint64_t s = max > 0 ? InsecureRandRange(max) : 0;
162 const NodeId node0 = 42, node1 = 69, node2 = 37;
167 pm.
addNode(node0, proof0, dg0);
173 pm.
addNode(node1, proof1, dg1);
175 std::unordered_map<PeerId, int> results = {};
176 for (
int i = 0; i < 10000; i++) {
182 BOOST_CHECK(abs(2 * results[0] - results[1]) < 500);
187 pm.
addNode(node2, proof2, dg2);
190 for (
int i = 0; i < 10000; i++) {
192 BOOST_CHECK(n == node0 || n == node1 || n == node2);
196 BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500);
205 std::array<PeerId, 8> peerids;
206 for (
int i = 0; i < 4; i++) {
216 for (
int i = 0; i < 100; i++) {
218 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] ||
233 for (
int i = 0; i < 100; i++) {
235 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]);
239 for (
int i = 0; i < 4; i++) {
264 for (
int i = 0; i < 100; i++) {
266 BOOST_CHECK(p == peerids[1] || p == peerids[3] || p == peerids[4] ||
267 p == peerids[5] || p == peerids[6]);
281 std::array<PeerId, 4> peerids;
282 for (
int i = 0; i < 4; i++) {
290 for (
auto p : peerids) {
297 for (
int i = 0; i < 100; i++) {
316 for (
int i = 0; i < 4; i++) {
320 for (
int i = 0; i < 100; i++) {
330 for (
int i = 0; i < 100; i++) {
339 std::chrono::hours(24)));
341 for (
int i = 0; i < 100; i++) {
354 int node3selected = 0;
355 for (
int i = 0; i < 100; i++) {
378 const int height = 1234;
384 for (
int i = 0; i < 10; i++) {
386 Coin(
CTxOut(v, script), height,
false),
false);
388 Coin(
CTxOut(v, script), height,
false),
false);
393 const auto getPeerId = [&](
const std::vector<COutPoint> &outpoints) {
395 for (
const auto &o : outpoints) {
396 pb.
addUTXO(o, v, height,
false, key);
static constexpr NodeId NO_NODE
Special NodeId that represent no node.
NodeId selectNode()
Randomly select a node to poll.
uint64_t getFragmentation() const
CPubKey GetPubKey() const
Compute the public key from a private key.
uint256 GetRandHash() noexcept
uint64_t compact()
Trigger maintenance of internal datastructures.
bool updateNextRequestTime(NodeId nodeid, TimePoint timeout)
PeerId selectPeerImpl(const std::vector< Slot > &slots, const uint64_t slot, const uint64_t max)
This is an internal method that is exposed for testing purposes.
Proof buildRandomProof(uint32_t score, const CPubKey &master)
CChainState & ChainstateActive()
static constexpr Amount COIN
bool removePeer(const PeerId peerid)
Remove an existing peer.
An encapsulated public key.
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
RecursiveMutex cs_main
Global state.
bool removeNode(NodeId nodeid)
BOOST_AUTO_TEST_CASE(select_peer_linear)
An output of a transaction.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
bool verify() const
Perform consistency check on internal data structures.
An outpoint - a combination of a transaction hash and an index n into its vout.
bool addNode(NodeId nodeid, const Proof &proof, const Delegation &delegation)
Node API.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
Serialized script, used inside transaction inputs and outputs.
A TxId is the identifier of a transaction.
#define BOOST_AUTO_TEST_SUITE_END()
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void AddCoin(const COutPoint &outpoint, Coin coin, bool potential_overwrite)
Add a coin.
An encapsulated secp256k1 private key.
uint64_t getSlotCount() const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static constexpr PeerId NO_PEER
PeerId selectPeer() const
Randomly select a peer to poll.
PeerId getPeerId(const Proof &proof)
Provide the PeerId associated with the given proof.
#define BOOST_CHECK(expr)