Auto-calibrate mining sleep time.

This commit is contained in:
chriseth 2016-08-06 13:18:00 +02:00
parent 53f68a155f
commit b9f5b675a6
2 changed files with 30 additions and 9 deletions

View File

@ -244,19 +244,38 @@ void RPCSession::test_mineBlocks(int _number)
u256 startBlock = fromBigEndian<u256>(fromHex(rpcCall("eth_blockNumber").asString())); u256 startBlock = fromBigEndian<u256>(fromHex(rpcCall("eth_blockNumber").asString()));
rpcCall("test_mineBlocks", { to_string(_number) }, true); rpcCall("test_mineBlocks", { to_string(_number) }, true);
//@TODO do not use polling - but that would probably need a change to the test client bool mined = false;
unsigned sleepTime = 10;
for (size_t polls = 0; polls < 10; ++polls) // We auto-calibrate the time it takes to mine the transaction.
// It would be better to go without polling, but that would probably need a change to the test client
unsigned sleepTime = m_sleepTime;
size_t polls = 0;
for (; polls < 10 && !mined; ++polls)
{ {
if (fromBigEndian<u256>(fromHex(rpcCall("eth_blockNumber").asString())) >= startBlock + _number)
return;
std::this_thread::sleep_for(chrono::milliseconds(sleepTime)); std::this_thread::sleep_for(chrono::milliseconds(sleepTime));
if (sleepTime > 500) if (fromBigEndian<u256>(fromHex(rpcCall("eth_blockNumber").asString())) >= startBlock + _number)
cout << "Mining timeout, sleeping for " << sleepTime << " ms" << endl; mined = true;
sleepTime *= 2; else
sleepTime *= 2;
}
if (polls > 1)
{
m_successfulMineRuns = 0;
m_sleepTime += 2;
}
else if (polls == 1)
{
m_successfulMineRuns++;
if (m_successfulMineRuns > 5)
{
m_successfulMineRuns = 0;
m_sleepTime--;
}
} }
BOOST_FAIL("Error in test_mineBlocks: block mining timeout!"); if (!mined)
BOOST_FAIL("Error in test_mineBlocks: block mining timeout!");
} }
void RPCSession::test_modifyTimestamp(size_t _timestamp) void RPCSession::test_modifyTimestamp(size_t _timestamp)

View File

@ -124,6 +124,8 @@ private:
IPCSocket m_ipcSocket; IPCSocket m_ipcSocket;
size_t m_rpcSequence = 1; size_t m_rpcSequence = 1;
unsigned m_sleepTime = 10;
unsigned m_successfulMineRuns = 0;
std::vector<std::string> m_accounts; std::vector<std::string> m_accounts;
}; };