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+ }
0 commit comments