Skip to content

Commit 63bf54d

Browse files
committed
- reenabling unit tests
- stubbing CRISP calculations -saving work
1 parent b58a64c commit 63bf54d

22 files changed

Lines changed: 386 additions & 42 deletions

dnotes-qt.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ HEADERS += src/qt/bitcoingui.h \
173173
src/core.h \
174174
src/main.h \
175175
src/miner.h \
176+
src/crisp.h \
176177
src/net.h \
177178
src/key.h \
178179
src/db.h \
@@ -284,6 +285,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
284285
src/core.cpp \
285286
src/main.cpp \
286287
src/miner.cpp \
288+
src/crisp.cpp \
287289
src/init.cpp \
288290
src/net.cpp \
289291
src/checkpoints.cpp \

src/chainparams.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void MineGenesis(CBlock genesis){
3939
printf("New best: %s\n", newhash.GetHex().c_str());
4040
}
4141
}
42-
printf("Found Genesis, Nonce: %ld, Hash: %s\n", genesis.nNonce, genesis.GetHash().GetHex().c_str());
42+
printf("Found Genesis, Nonce: %u, Hash: %s\n", genesis.nNonce, genesis.GetHash().GetHex().c_str());
4343
printf("Gensis Hash Merkle: %s\n", genesis.hashMerkleRoot.ToString().c_str());
4444
}
4545

@@ -126,6 +126,13 @@ class CMainParams : public CChainParams {
126126
convertSeed6(vFixedSeeds, pnSeed6_main, ARRAYLEN(pnSeed6_main));
127127

128128
nLastPOWBlock = 2000;
129+
130+
nCRISPPayoutInterval = 60 * 24 * 30; //43200 minutes in 30 days
131+
nCRISPPayoutLag = 60 * 24 * 7; //10080 minutes in 30 days
132+
nMaxCRISPPayoutsPerBlock = 10000;
133+
nMaxTransactionsPerBlock = 100;
134+
nMaxInputsAndOutputsPerBlock = 300;
135+
nCRISPPayoutPercentage = .005; //half a percent, paid out roughly monthly
129136
}
130137

131138
virtual const CBlock& GenesisBlock() const { return genesis; }
@@ -212,6 +219,10 @@ class CRegTestParams : public CTestNetParams {
212219
assert(hashGenesisBlock == uint256("0x0000074d707edc8763d1f1a295f474e59a8e827d73b150bef1611279c08fc068"));
213220

214221
vSeeds.clear(); // Regtest mode doesn't have any DNS seeds.
222+
223+
nCRISPPayoutInterval = 100;
224+
nCRISPPayoutLag = 25;
225+
fPOWNoRetargeting = true;
215226
}
216227

217228
virtual bool RequireRPCPassword() const { return false; }

src/chainparams.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class CChainParams
6868
virtual const vector<CAddress>& FixedSeeds() const = 0;
6969
int RPCPort() const { return nRPCPort; }
7070
int LastPOWBlock() const { return nLastPOWBlock; }
71+
bool POWNoRetargeting() const { return fPOWNoRetargeting; }
72+
73+
int CRISPPayoutInterval() const {return nCRISPPayoutInterval;}
74+
int CRISPPayoutLag() const {return nCRISPPayoutLag;}
75+
double CRISPPayoutPercentage() const {return nCRISPPayoutPercentage;}
76+
int MaxCRISPPayoutsPerBlock() const {return nMaxCRISPPayoutsPerBlock;}
77+
int MaxTransactionsPerBlock() const {return nMaxTransactionsPerBlock;}
78+
int MaxInputsAndOutputsPerBlock() const {return nMaxInputsAndOutputsPerBlock;}
7179
protected:
7280
CChainParams() {};
7381

@@ -83,6 +91,14 @@ class CChainParams
8391
vector<CDNSSeedData> vSeeds;
8492
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
8593
int nLastPOWBlock;
94+
bool fPOWNoRetargeting;
95+
96+
int nCRISPPayoutInterval;
97+
int nCRISPPayoutLag;
98+
double nCRISPPayoutPercentage;
99+
int nMaxCRISPPayoutsPerBlock;
100+
int nMaxTransactionsPerBlock;
101+
int nMaxInputsAndOutputsPerBlock;
86102
};
87103

88104
/**

src/crisp.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
2+
using namespace std;
3+
4+
5+
#include "main.h"
6+
#include "script.h"
7+
8+
namespace CRISP
9+
{
10+
std::map<CTxDestination, int64_t> GetStartingAddressBalances(int blockHeight);
11+
std::map<CTxDestination, int64_t> CalculateAddressBalanceDeltas(int startingHeight, int endingHeight);
12+
std::map<CTxDestination, int64_t> CalculateAddressPayouts(std::map<CTxDestination, int64_t> startingAddressBalances, std::map<CTxDestination, int64_t> addressBalanceDeltas);
13+
14+
//returns address balances to set on the
15+
std::map<CTxDestination, int64_t> AddCRISPPayouts(int currentBlockHeight, CTransaction coinbaseTransaction)
16+
{
17+
bool makeCRISPPayouts = false;
18+
bool makeCRISPCatchupPayouts = false;
19+
//make CRISP payouts every CRISPPayoutInterval blocks
20+
if(currentBlockHeight > Params().CRISPPayoutInterval() && currentBlockHeight % Params().CRISPPayoutInterval() == Params().CRISPPayoutLag())
21+
{
22+
makeCRISPPayouts = true;
23+
}
24+
else if(false) //todo: if we had 10k crisp payouts in the previous block, re-do above calculation as of that previous block,
25+
{
26+
makeCRISPPayouts = true;
27+
makeCRISPCatchupPayouts = true;
28+
}
29+
30+
if(makeCRISPPayouts)
31+
{
32+
int crispPayoutBlockHeight = currentBlockHeight;
33+
if(makeCRISPCatchupPayouts)
34+
{
35+
//if we're catching up, determine the original block that the payouts should have been for
36+
crispPayoutBlockHeight = currentBlockHeight % Params().CRISPPayoutInterval() - Params().CRISPPayoutLag();
37+
}
38+
39+
std::map<CTxDestination, int64_t> startingBalances = GetStartingAddressBalances(crispPayoutBlockHeight);
40+
41+
int deltaStartingHeight, deltaEndingHeight;
42+
deltaStartingHeight = crispPayoutBlockHeight - Params().CRISPPayoutInterval() - Params().CRISPPayoutLag();
43+
deltaEndingHeight = crispPayoutBlockHeight - Params().CRISPPayoutLag() - 1;
44+
std::map<CTxDestination, int64_t> balanceDeltas = CalculateAddressBalanceDeltas(deltaStartingHeight, deltaEndingHeight);
45+
std::map<CTxDestination, int64_t> payouts = CalculateAddressPayouts(startingBalances, balanceDeltas);
46+
47+
//iterate through payouts to build vouts for each (up to maximum)
48+
//build up list of CRISP payouts from recent blocks to avoid double paying them
49+
50+
if(!makeCRISPCatchupPayouts)
51+
{
52+
std::map<CTxDestination, int64_t> currentBalances;
53+
//combine initial balance and delta and store that value into the block (if we're not playing catchup)
54+
55+
//throw std::runtime_error("not yet implemented");
56+
return currentBalances; //what i know about heap, pointers, and c++ memory tells me that this will not work how i want.
57+
}
58+
59+
}
60+
61+
std::map<CTxDestination, int64_t> currentBalances;
62+
return currentBalances;
63+
}
64+
65+
std::map<CTxDestination, int64_t> GetStartingAddressBalances(int blockHeight)
66+
{
67+
int blockHeightForAddressBalances = blockHeight - Params().CRISPPayoutInterval();
68+
69+
//get address balances as of blockheight - Params().CRISPPayoutInterval - Params().CRISPPayoutLag - 1
70+
//which should be stored in block: blockheight - Params().CRISPPayoutInterval
71+
if(blockHeightForAddressBalances > Params().CRISPPayoutInterval()){
72+
//get a block.
73+
CBlock block;
74+
CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
75+
while (pblockindex->nHeight > blockHeightForAddressBalances)
76+
pblockindex = pblockindex->pprev;
77+
78+
uint256 hash = *pblockindex->phashBlock;
79+
80+
pblockindex = mapBlockIndex[hash];
81+
block.ReadFromDisk(pblockindex, false);
82+
83+
return block.addressBalances;
84+
}
85+
86+
std::map<CTxDestination, int64_t> addressBalances;
87+
return addressBalances;
88+
}
89+
90+
std::map<CTxDestination, int64_t> CalculateAddressBalanceDeltas(int startingHeight, int endingHeight)
91+
{
92+
std::map<CTxDestination, int64_t> addressBalanceDeltas;
93+
94+
//iterate through blocks backward from tip.
95+
LogPrint("crisp", "starting balance delta loop");
96+
CBlock block;
97+
CBlockIndex *pblockindex = mapBlockIndex[hashBestChain];
98+
while (pblockindex->nHeight >= startingHeight)
99+
{
100+
LogPrint("crisp", "balance delta loop %d", pblockindex->nHeight);
101+
pblockindex = pblockindex->pprev;
102+
if (pblockindex->nHeight <= endingHeight)
103+
{
104+
uint256 hash = *pblockindex->phashBlock;
105+
106+
107+
108+
pblockindex = mapBlockIndex[hash];
109+
block.ReadFromDisk(pblockindex, true);
110+
111+
//transactions
112+
BOOST_FOREACH (CTransaction &transaction, block.vtx)
113+
{
114+
//inputs
115+
BOOST_FOREACH (CTxIn &input, transaction.vin)
116+
{
117+
//addressBalanceDeltas.count(NoTxDestination)
118+
CTransaction inputTransaction;
119+
uint256 hashBlock = 0;
120+
if (GetTransaction(input.prevout.hash, inputTransaction, hashBlock))
121+
{
122+
txnouttype type;
123+
vector<CTxDestination> addresses;
124+
int nRequired;
125+
CTxOut originatingOutput = inputTransaction.vout[input.prevout.n];
126+
127+
if (!ExtractDestinations(originatingOutput.scriptPubKey, type, addresses, nRequired))
128+
{
129+
}
130+
131+
BOOST_FOREACH (const CTxDestination &addr, addresses)
132+
{
133+
//a.push_back(CBitcoinAddress(addr).ToString());
134+
}
135+
}
136+
else
137+
{
138+
//something has gone wrong.
139+
}
140+
}
141+
142+
//outputs
143+
BOOST_FOREACH (CTxOut &output, transaction.vout)
144+
{
145+
}
146+
}
147+
}
148+
}
149+
150+
return addressBalanceDeltas;
151+
}
152+
153+
std::map<CTxDestination, int64_t> CalculateAddressPayouts(std::map<CTxDestination, int64_t> startingAddressBalances, std::map<CTxDestination, int64_t> addressBalanceDeltas)
154+
{
155+
std::map<CTxDestination, int64_t> addressPayouts;
156+
157+
158+
159+
return addressPayouts;
160+
}
161+
}

src/crisp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CRISP_H
2+
#define CRISP_H
3+
4+
#include "main.h"
5+
#include "wallet.h"
6+
#include "crisp.h"
7+
8+
namespace CRISP
9+
{
10+
std::map<CTxDestination, int64_t> AddCRISPPayouts(int currentBlockHeight, CTransaction coinbaseTransaction);
11+
}
12+
13+
#endif // CRISP_H

src/main.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CBigNum bnProofOfStakeLimit(~uint256(0) >> 20);
4444
CBigNum bnProofOfStakeLimitV2(~uint256(0) >> 48);
4545

4646
int nStakeMinConfirmations = 10;
47-
unsigned int nStakeMinAge = 60; // 8 hours. DNote -> Obsolete in V3, now use nStakeMinconfirmations. Used to be involved in coin age calcualtions for coin age staking.
47+
unsigned int nStakeMinAge = 60; // 8 hours. DNote -> Obsolete in V3, now use nStakeMinconfirmations. Used to be involved in coin age calculations for coin age staking.
4848
unsigned int nModifierInterval = 10 * 60; // time to elapse before new modifier is computed
4949

5050
int nCoinbaseMaturity = 50;
@@ -1036,14 +1036,15 @@ unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfS
10361036

10371037
if (pindexLast == NULL)
10381038
return bnTargetLimit.GetCompact(); // genesis block
1039-
10401039
const CBlockIndex* pindexPrev = GetLastBlockIndex(pindexLast, fProofOfStake);
10411040
if (pindexPrev->pprev == NULL)
10421041
return bnTargetLimit.GetCompact(); // first block
10431042
const CBlockIndex* pindexPrevPrev = GetLastBlockIndex(pindexPrev->pprev, fProofOfStake);
10441043
if (pindexPrevPrev->pprev == NULL)
10451044
return bnTargetLimit.GetCompact(); // second block
10461045

1046+
1047+
10471048
int64_t nTargetSpacing = GetTargetSpacing(pindexLast->nHeight);
10481049
int64_t nActualSpacing = pindexPrev->GetBlockTime() - pindexPrevPrev->GetBlockTime();
10491050
if (IsProtocolV1RetargetingFixed(pindexLast->nHeight)) {
@@ -1059,12 +1060,13 @@ unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfS
10591060
// ppcoin: retarget with exponential moving toward target spacing
10601061
CBigNum bnNew;
10611062
bnNew.SetCompact(pindexPrev->nBits);
1062-
int64_t nInterval = nTargetTimespan / nTargetSpacing;
1063-
bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
1064-
bnNew /= ((nInterval + 1) * nTargetSpacing);
10651063

1066-
string val1 = bnNew.GetHex();
1067-
string val2 = bnTargetLimit.GetHex();
1064+
//turn off retargetting if doing pow on regtest
1065+
if(!Params().POWNoRetargeting() || fProofOfStake){
1066+
int64_t nInterval = nTargetTimespan / nTargetSpacing;
1067+
bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
1068+
bnNew /= ((nInterval + 1) * nTargetSpacing);
1069+
}
10681070

10691071
if (bnNew <= 0 || bnNew > bnTargetLimit)
10701072
bnNew = bnTargetLimit;

src/main.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ class CBlock
598598

599599
// network and disk
600600
std::vector<CTransaction> vtx;
601+
602+
//Running tally of addresses and their balance on the blockchain for use in CRISP calculations.
603+
//Only hydrated in every nCRISPPayoutInterval blocks.
604+
//is valid thru the end of block height: block's height - nCRISPPayoutLag - 1
605+
//needs to be serialized for network and disk.
606+
std::map<CTxDestination, int64_t> addressBalances;
601607

602608
// ppcoin: block signature - signed by one of the coin base txout[N]'s owner
603609
std::vector<unsigned char> vchBlockSig;

src/makefile.unix

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ DEFS=-DBOOST_SPIRIT_THREADSAFE
1313
DEFS += $(addprefix -I,$(CURDIR) $(CURDIR)/obj $(BOOST_INCLUDE_PATH) $(BDB_INCLUDE_PATH) $(OPENSSL_INCLUDE_PATH))
1414
LIBS = $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH) $(OPENSSL_LIB_PATH))
1515

16+
TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
17+
1618
LMODE = dynamic
1719
LMODE2 = dynamic
1820
ifdef STATIC
1921
LMODE = static
2022
ifeq (${STATIC}, all)
2123
LMODE2 = static
2224
endif
25+
else
26+
TESTDEFS += -DBOOST_TEST_DYN_LINK
2327
endif
2428

2529
# for boost 1.37, add -mt to the boost libraries
@@ -106,6 +110,7 @@ OBJS= \
106110
obj/netbase.o \
107111
obj/addrman.o \
108112
obj/crypter.o \
113+
obj/crisp.o \
109114
obj/key.o \
110115
obj/init.o \
111116
obj/bitcoind.o \
@@ -175,6 +180,7 @@ obj/txdb-leveldb.o: leveldb/libleveldb.a
175180

176181
# auto-generated dependencies:
177182
-include obj/*.P
183+
-include obj-test/*.P
178184

179185
obj/build.h: FORCE
180186
/bin/sh ../share/genbuild.sh obj/build.h
@@ -207,8 +213,25 @@ obj/%.o: %.c
207213
dnotesd: $(OBJS:obj/%=obj/%)
208214
$(LINK) $(xCXXFLAGS) -o $@ $^ $(xLDFLAGS) $(LIBS)
209215

216+
TESTOBJS := $(patsubst test/%.cpp,obj-test/%.o,$(wildcard test/*.cpp))
217+
218+
obj-test/%.o: test/%.cpp
219+
$(CXX) -c $(TESTDEFS) $(xCXXFLAGS) -MMD -o $@ $<
220+
@cp $(@:%.o=%.d) $(@:%.o=%.P); \
221+
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
222+
-e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \
223+
rm -f $(@:%.o=%.d)
224+
225+
test_dnotes: $(TESTOBJS) $(filter-out obj/bitcoind.o,$(OBJS:obj/%=obj/%))
226+
$(CXX) $(xCXXFLAGS) -o $@ $(LIBPATHS) $^ -Wl,-B$(LMODE) -lboost_unit_test_framework $(xLDFLAGS) $(LIBS)
227+
210228
clean:
211229
-rm -f dnotesd
230+
-rm -f test_bitcoin
231+
-rm -f test_dnotes
232+
-rm -f obj-test/*.o
233+
-rm -f obj-test/*.P
234+
-rm -f obj-test/build.h
212235
-rm -f obj/*.o
213236
-rm -f obj/*.P
214237
-rm -f obj/build.h

0 commit comments

Comments
 (0)