nochowderforyou / clams

Clam Project
MIT License
61 stars 58 forks source link

race condition initialising staking rewards map #289

Open dooglus opened 8 years ago

dooglus commented 8 years ago

I just saw 'getstakedbyaddress' giving incorrect results after initialising its state twice at the same time:

2016-04-03 00:09:33 init message: Done loading
2016-04-03 00:09:33 opencon thread start
2016-04-03 00:09:33 initializing staking rewards map
2016-04-03 00:09:35 SetBestChain: new best=e4d8de927400bb966ea0a562fae3a834af6c9cd935671f3be802d8be5f3e9a9d  height=930041  trust=188915589813919675228  blocktrust=49701721070808  date=04/03/16 00:08:00
2016-04-03 00:09:35 ProcessBlock: ACCEPTED
2016-04-03 00:09:35 SetBestChain: new best=a5df21e3e08c3e8df2636dc2fd1d8bd43efe0b615dc33075672f3b04b02e81d8  height=930042  trust=188915639515640746036  blocktrust=49701721070808  date=04/03/16 00:08:32
2016-04-03 00:09:35 ProcessBlock: ACCEPTED
2016-04-03 00:09:35 SetBestChain: new best=4ca880d3703a3f990a4ac11254a1e26310c782c2362fbf9fe1a26948353f82a7  height=930043  trust=188915689242952392393  blocktrust=49727311646357  date=04/03/16 00:09:20
2016-04-03 00:09:35 ProcessBlock: ACCEPTED
2016-04-03 00:09:37 initializing staking rewards map
2016-04-03 00:09:44 P2P peers available. Skipped DNS seeding.
2016-04-03 00:09:44 dnsseed thread exit
2016-04-03 00:09:47 CWallet::SumStakingRewards() found 407388 of 409100 tx to be stakes
2016-04-03 00:09:52 CWallet::SumStakingRewards() found 407388 of 409100 tx to be stakes

It was initialised once because I have a -stakenotify in my clam.conf:

    // if we're going to be running a command each time we stake, sum all existing stake rewards now so we're ready
    if (!GetArg("-stakenotify", "").empty() && !pwalletMain->fAddressRewardsReady) {
        LogPrintf("initializing staking rewards map\n");
        pwalletMain->SumStakingRewards();
    }

and once when I ran 'getstakedbyaddress' before the first initialisation had finished:

    if (!pwalletMain->fAddressRewardsReady) {
        LogPrintf("initializing staking rewards map\n");
        pwalletMain->SumStakingRewards();
    }

We need a semaphore around this, I guess.