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

[Main] Refactor Accumulator Code #339

Merged
merged 6 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ endif
# pivx core #
BITCOIN_CORE_H = \
activemasternode.h \
accumulators.h \
accumulatormap.h \
addrman.h \
alert.h \
allocators.h \
Expand Down Expand Up @@ -169,7 +171,6 @@ BITCOIN_CORE_H = \
zmq/zmqconfig.h \
zmq/zmqnotificationinterface.h \
zmq/zmqpublishnotifier.h \
accumulators.h \
compat/sanity.h

JSON_H = \
Expand Down Expand Up @@ -334,6 +335,8 @@ libzerocoin_libbitcoin_zerocoin_a_SOURCES = \
# common: shared between pivxd, and pivx-qt and non-server tools
libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES)
libbitcoin_common_a_SOURCES = \
accumulators.cpp \
accumulatormap.cpp \
allocators.cpp \
amount.cpp \
base58.cpp \
Expand Down Expand Up @@ -361,7 +364,6 @@ libbitcoin_common_a_SOURCES = \
script/script_error.cpp \
spork.cpp \
sporkdb.cpp \
accumulators.cpp \
$(BITCOIN_CORE_H)

# util: shared between all executables.
Expand Down
88 changes: 88 additions & 0 deletions src/accumulatormap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2017 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "accumulatormap.h"
#include "accumulators.h"
#include "main.h"
#include "txdb.h"
#include "libzerocoin/Denominations.h"

using namespace libzerocoin;
using namespace std;

//Construct accumulators for all denominations
AccumulatorMap::AccumulatorMap()
{
for (auto& denom : zerocoinDenomList) {
unique_ptr<Accumulator> uptr(new Accumulator(Params().Zerocoin_Params(), denom));
mapAccumulators.insert(make_pair(denom, std::move(uptr)));
}
}

//Reset each accumulator to its default state
void AccumulatorMap::Reset()
{
mapAccumulators.clear();
for (auto& denom : zerocoinDenomList) {
unique_ptr<Accumulator> uptr(new Accumulator(Params().Zerocoin_Params(), denom));
mapAccumulators.insert(make_pair(denom, std::move(uptr)));
}
}

//Load a checkpoint containing 8 32bit checksums of accumulator values.
bool AccumulatorMap::Load(uint256 nCheckpoint)
{
for (auto& denom : zerocoinDenomList) {
uint32_t nChecksum = ParseChecksum(nCheckpoint, denom);

CBigNum bnValue;
if (!zerocoinDB->ReadAccumulatorValue(nChecksum, bnValue)) {
LogPrintf("%s : cannot find checksum %d", __func__, nChecksum);
return false;
}

mapAccumulators.at(denom)->setValue(bnValue);
}
return true;
}

//Add a zerocoin to the accumulator of its denomination.
bool AccumulatorMap::Accumulate(PublicCoin pubCoin, bool fSkipValidation)
{
CoinDenomination denom = pubCoin.getDenomination();
if (denom == CoinDenomination::ZQ_ERROR)
return false;

if (fSkipValidation)
mapAccumulators.at(denom)->increment(pubCoin.getValue());
else
mapAccumulators.at(denom)->accumulate(pubCoin);
return true;
}

//Get the value of a specific accumulator
CBigNum AccumulatorMap::GetValue(CoinDenomination denom)
{
if (denom == CoinDenomination::ZQ_ERROR)
return CBigNum(0);
return mapAccumulators.at(denom)->getValue();
}

//Calculate a 32bit checksum of each accumulator value. Concatenate checksums into uint256
uint256 AccumulatorMap::GetCheckpoint()
{
uint256 nCheckpoint;

//Prevent possible overflows from future changes to the list and forgetting to update this code
assert(zerocoinDenomList.size() == 8);
for (auto& denom : zerocoinDenomList) {
CBigNum bnValue = mapAccumulators.at(denom)->getValue();
uint32_t nCheckSum = GetChecksum(bnValue);
nCheckpoint = nCheckpoint << 32 | nCheckSum;
}

return nCheckpoint;
}


23 changes: 23 additions & 0 deletions src/accumulatormap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2017 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef PIVX_ACCUMULATORMAP_H
#define PIVX_ACCUMULATORMAP_H

#include "libzerocoin/Accumulator.h"
#include "libzerocoin/Coin.h"

//A map with an accumulator for each denomination
class AccumulatorMap
{
private:
std::map<libzerocoin::CoinDenomination, std::unique_ptr<libzerocoin::Accumulator> > mapAccumulators;
public:
AccumulatorMap();
bool Load(uint256 nCheckpoint);
bool Accumulate(libzerocoin::PublicCoin pubCoin, bool fSkipValidation = false);
CBigNum GetValue(libzerocoin::CoinDenomination denom);
uint256 GetCheckpoint();
void Reset();
};
#endif //PIVX_ACCUMULATORMAP_H
Loading