Skip to content
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

Staking and mining code cleanup, improvements #301

Merged
merged 7 commits into from
Jun 9, 2017

Conversation

tomasbrod
Copy link
Member

@tomasbrod tomasbrod commented May 14, 2017

Read the commit messages.
Experimental until declared ready!
Interest and Research subsidy visible in getmininginfo.
Check out -debug=true -printtoconsole;
Block PoS hash and target visible in log;
Refactoring StakeMiner miner.cpp;

@denravonska
Copy link
Member

Hey, cool. It looks like this one still tries to sign the same block over and over again, is that correct?

@@ -19,7 +19,7 @@ using namespace std;
// BitcoinMiner
//

unsigned int nMinerSleep;
volatile unsigned int nMinerSleep;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for thread safety? IMO we should change all the current volatiles to std::atomic. At least that's what I think the intention was.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not changed after stake miner thread is started. Without the volatile modifier optimizer prevents me from changing it with debugger at run-time. It is just for debugger.

src/miner.cpp Outdated
nLastBlockSubmitted = 0;
return error("CheckStake: Nonce too low\r\n");
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray return.

src/miner.cpp Outdated
MilliSleep(nMinerSleep);
}
SkipStake:
MilliSleep(nMinerSleep);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the minersleep flag altogether and just sleep 500ms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again. For use with debugger.

src/miner.cpp Outdated
}


//Verify we are still on the main chain

if (IsLockTimeWithinMinutes(nLastBlockSolved,5))
if (false && IsLockTimeWithinMinutes(nLastBlockSolved,4))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not do this check anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@@ -51,6 +51,10 @@ Value getmininginfo(const Array& params, bool fHelp)
obj.push_back(Pair("blocks", (int)nBestHeight));
obj.push_back(Pair("currentblocksize",(uint64_t)nLastBlockSize));
obj.push_back(Pair("currentblocktx",(uint64_t)nLastBlockTx));
try{
std::string superblock_num = mvApplicationCache["superblock;block_number"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be changed to ReadCache("superblock", "block_number");. That implementation also injects a new object if one is missing, but I intend to fix it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no intention to improve this api. Shoud I remove it then?

@@ -614,42 +614,30 @@ static bool CheckStakeKernelHashV3(CBlockIndex* pindexPrev, unsigned int nBits,
double coin_age = std::abs((double)nTimeTx-(double)txPrev.nTime);
double BitsAge = PORDiff * 144; //For every 100 Diff in Bits, two hours of coin age for researchers
if (BitsAge > 86400) BitsAge=86400; //Limit to 24 hours (possibility of astronomical diff)
// Halford : Explain to the Researcher why they are not staking:
if (checking_local && LessVerbose(100))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this always evaluate to false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. The code is refactored just few lines below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to stop reviewing that early. Read that code 3 times and realised twice that it was refactored :/

src/miner.cpp Outdated
@@ -925,14 +918,16 @@ void StakeMiner(CWallet *pwallet)
else
{
msMiningErrors = "Stake block rejected";
if (fDebug) printf("Stake block rejected \r\n");
printf("Stake block rejected \r\n");
MilliSleep(1000);
}
SetThreadPriority(THREAD_PRIORITY_LOWEST);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove these thread priorities. A medium priority across the line should be fine since we're not doing any heavy work.

Copy link
Member Author

@tomasbrod tomasbrod May 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok
But i doubt it does anything since only root can ever raise priority of thread on linux.

//12-20-2015 Support for Binary Superblocks
std::string superblock=UnpackBinarySuperblock(bb.superblock);
std::string neural_hash = GetQuorumHash(superblock);
result.push_back(Pair("SuperblockHash", neural_hash));
result.push_back(Pair("SuperblockLength", (double)bb.superblock.length()));
result.push_back(Pair("SuperblockLength", (long)superblock.length()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the meaning of "SuperblockLength". Will this cause any compatibility issues?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll change that

@tomasbrod
Copy link
Member Author

tomasbrod commented May 14, 2017

@denravonska performed the changes, please verify

It looks like this one still tries to sign the same block over and over again, is that correct?

Yes that unfortunately still true. Bigger refactoring required to close #295.

@tomasbrod tomasbrod force-pushed the stakeclean branch 2 times, most recently from 84b6deb to ef0b0ef Compare May 14, 2017 17:38
src/init.cpp Outdated
@@ -558,7 +558,7 @@ bool AppInit2()

fUseFastIndex = GetBoolArg("-fastindex", false);

nMinerSleep = GetArg("-minersleep", 500);
nMinerSleep = GetArg("-minersleep", 8000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this will affect the shutdown time, right? Might be a non-issue though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, thinking about this again... There's a very specific second where staking is allowed, given the staking mask, right? With the previous 500ms sleep you were pretty much guaranteed to hit that window but with the new sleep there's a risk that you always sleep when the staking window is open. Or did I misinterpret the purpose of the mask?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a single second. There is specific period of 16 seconds where staking is allowed. The (bit inverse of) stake mask is applied to timestamp effectively eliminating the difference when of staking anywhere during that 16 seconds. It was introduced to save stakers from having to check every second.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaaah, gotcha!

@denravonska
Copy link
Member

Excellent, I will try it out first thing tomorrow morning.

@denravonska
Copy link
Member

Trying this on testnet now and I noticed that it started receiving blocks from the network while still loading the blocks from disk. I'm also seeing a lot of these in the log, but I guess that is to be expected with 0 mag and a semi-broken testnet:

ERROR: CheckStakeKernelHashV3: Magnitude too low for POR reward: 1494818032.000

src/kernel.cpp Outdated
if (payment_age <= BitsAge)
narr="Payment age < BitsAge: " + RoundToString(payment_age,3) + "; " + RoundToString(BitsAge,3);
if (boincblock.Magnitude <= 1)
narr="Magnitude too low for POR reward: " + RoundToString(payment_age,3);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spam of this message in testnet. Remove warning? Print only when debug enabled? But it is important warning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep it if the user is intended to get rewards. I think testnet doesn't have any projects whitelisted right now.

@tomasbrod
Copy link
Member Author

I would like to point you, @Ravon, to this wiki page. I am confused about what outputs is which flag meant to enable.

src/kernel.cpp Outdated
if (coin_age <= RSA_WEIGHT)
narr=" Coin Age < RSA_Weight: " + RoundToString(coin_age,0) + "; " + RoundToString(RSA_WEIGHT,0);
if (coin_age <= 4*60*60)
narr=" Coin Age Immature: " + RoundToString(coin_age,0) + "; 14400";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe i lost this condition
that's why my stakes are not accepted on testnet sometimes

@tomasbrod
Copy link
Member Author

tomasbrod commented May 18, 2017

I will close this request now. This one is only half done and I feel that more refactoring is needed.

@tomasbrod tomasbrod closed this May 18, 2017
@denravonska
Copy link
Member

Are you sure? It's still an improvement over the current implementation.

@tomasbrod
Copy link
Member Author

tomasbrod commented May 20, 2017

I misunderstood what a Close really means so I open this again with a push :)
The diff of src/miner.cpp is crazyconfusing. I suggest reading the after file, not the diff.
Remember: this code is experimental and not complete.

@tomasbrod
Copy link
Member Author

tomasbrod commented May 31, 2017

New status of this PR: @denravonska
New stake code is pretty stable and stakes blocks just right. Less complex code and unnoticeably lower cpu usage.
Some info is not displayed in status. Other info added.
#317 is still an issue: watch out for 1GRC research reward and report there.
Sightly more debug output when enabled.
Only one set of coins (UTXO) is ever added to stake transaction, thus sightly slower interest.
All stake coins and reward is placed in single output: no more 1 cent outputs but may not split outputs as expected.
And due to the PoW bug/feature (#332) minersleep option does affect staking frequency.
I would appreciate some testing.

@denravonska
Copy link
Member

We should try to include this in the next staging merge if possible. Regarding the minersleep option I'd prefer if we removed it. When debugging it's trivial to alter the code.

I can test it on testnet if you want to.

@denravonska
Copy link
Member

Running this in testnet now. What's missing from the status?

Getting -ERROR: CheckStakeKernelHashV3: Coin Age < RSA_Weight: 49104; 106280 :(

@iFoggz
Copy link
Member

iFoggz commented Jun 5, 2017

Those indents thou in debug print for RSA_WEIGHT ;o) looks nice thou to have that added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants