Browse Source
There are only a few uses of `insecure_random` outside the tests. This PR replaces uses of insecure_random (and its accompanying global state) in the core code with an FastRandomContext that is automatically seeded on creation. This is meant to be used for inner loops. The FastRandomContext can be in the outer scope, or the class itself, then rand32() is used inside the loop. Useful e.g. for pushing addresses in CNode or the fee rounding, or randomization for coin selection. As a context is created per purpose, thus it gets rid of cross-thread unprotected shared usage of a single set of globals, this should also get rid of the potential race conditions. - I'd say TxMempool::check is not called enough to warrant using a special fast random context, this is switched to GetRand() (open for discussion...) - The use of `insecure_rand` in ConnectThroughProxy has been replaced by an atomic integer counter. The only goal here is to have a different credentials pair for each connection to go on a different Tor circuit, it does not need to be random nor unpredictable. - To avoid having a FastRandomContext on every CNode, the context is passed into PushAddress as appropriate. There remains an insecure_random for test usage in `test_random.h`.0.14
Wladimir J. van der Laan
8 years ago
28 changed files with 92 additions and 66 deletions
@ -0,0 +1,23 @@ |
|||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2014 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_TEST_RANDOM_H |
||||||
|
#define BITCOIN_TEST_RANDOM_H |
||||||
|
|
||||||
|
#include "random.h" |
||||||
|
|
||||||
|
extern FastRandomContext insecure_rand_ctx; |
||||||
|
|
||||||
|
static inline void seed_insecure_rand(bool fDeterministic = false) |
||||||
|
{ |
||||||
|
insecure_rand_ctx = FastRandomContext(fDeterministic); |
||||||
|
} |
||||||
|
|
||||||
|
static inline uint32_t insecure_rand(void) |
||||||
|
{ |
||||||
|
return insecure_rand_ctx.rand32(); |
||||||
|
} |
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue