Skip to content

Instantly share code, notes, and snippets.

@tomasvdw
Created June 14, 2018 12:35
Show Gist options
  • Save tomasvdw/a9399c657f6dd2d0c5de30a57fa264c5 to your computer and use it in GitHub Desktop.
Save tomasvdw/a9399c657f6dd2d0c5de30a57fa264c5 to your computer and use it in GitHub Desktop.
AddCursor.cpp
CUtxoCommit BuildUtxoCommit(CCoinsViewCursor* pcursor, size_t numworkers)
{
if (numworkers == 0)
throw std::invalid_argument("numworkers == 0");
std::atomic<int> count(0);
std::mutex cs;
// spin up workers
auto worker = [&pcursor, &cs, &count]() {
CUtxoCommit commitment;
bool eof = false;
while (!eof) {
std::vector<std::pair<COutPoint, Coin>> utxos;
utxos.reserve(50);
{
std::unique_lock<std::mutex> lock(cs);
for(int n =0; n < 50; n++) {
auto utxo = std::pair<COutPoint, Coin>();
if (!pcursor->Valid()) {
eof = true;
break;
}
if (!pcursor->GetKey(utxo.first)
|| !pcursor->GetValue(utxo.second)) {
throw std::runtime_error("failed retrieve from UTXO cursor");
}
utxos.push_back(utxo);
pcursor->Next();
}
}
for(auto utxo : utxos) {
commitment.Add(utxo.first, utxo.second);
if ((count++ % 1000000) == 0) {
uint8_t c = *utxo.first.hash.begin();
LogPrintf("Generating UTXO commitment; progress %d\n",
uint32_t(c) * 100 / 256);
}
}
}
return commitment;
};
std::vector<std::future<CUtxoCommit> > workers;
for (size_t i = 0; i < numworkers; ++i) {
workers.push_back(std::async(std::launch::async, worker));
}
CUtxoCommit result;
for (auto& w : workers) {
w.wait();
result.Add(w.get());
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment