]>
Commit | Line | Data |
---|---|---|
1 | /****************************************************************************** | |
2 | * Copyright © 2014-2018 The SuperNET Developers. * | |
3 | * * | |
4 | * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * | |
5 | * the top-level directory of this distribution for the individual copyright * | |
6 | * holder information and the developer policies on copyright and licensing. * | |
7 | * * | |
8 | * Unless otherwise agreed in a custom licensing agreement, no part of the * | |
9 | * SuperNET software, including this file may be copied, modified, propagated * | |
10 | * or distributed except according to the terms contained in the LICENSE file * | |
11 | * * | |
12 | * Removal or modification of this copyright notice is prohibited. * | |
13 | * * | |
14 | ******************************************************************************/ | |
15 | ||
16 | #include "komodo_defs.h" | |
17 | ||
18 | void komodo_prefetch(FILE *fp); | |
19 | uint32_t komodo_heightstamp(int32_t height); | |
20 | void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t kheight,uint32_t ktime,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout,uint256 MoM,int32_t MoMdepth); | |
21 | void komodo_init(int32_t height); | |
22 | int32_t komodo_MoMdata(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight,uint256 *MoMoMp,int32_t *MoMoMoffsetp,int32_t *MoMoMdepthp,int32_t *kmdstartip,int32_t *kmdendip); | |
23 | int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp); | |
24 | char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port); | |
25 | void komodo_init(int32_t height); | |
26 | int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33,uint32_t timestamp); | |
27 | int32_t komodo_isrealtime(int32_t *kmdheightp); | |
28 | uint64_t komodo_paxtotal(); | |
29 | int32_t komodo_longestchain(); | |
30 | uint64_t komodo_maxallowed(int32_t baseid); | |
31 | int32_t komodo_bannedset(int32_t *indallvoutsp,uint256 *array,int32_t max); | |
32 | ||
33 | pthread_mutex_t komodo_mutex; | |
34 | ||
35 | #define KOMODO_ELECTION_GAP 2000 //((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) | |
36 | #define IGUANA_MAXSCRIPTSIZE 10001 | |
37 | #define KOMODO_ASSETCHAIN_MAXLEN 65 | |
38 | ||
39 | struct pax_transaction *PAX; | |
40 | int32_t NUM_PRICES; uint32_t *PVALS; | |
41 | struct knotaries_entry *Pubkeys; | |
42 | ||
43 | struct komodo_state KOMODO_STATES[34]; | |
44 | ||
45 | #define _COINBASE_MATURITY 100 | |
46 | int COINBASE_MATURITY = _COINBASE_MATURITY;//100; | |
47 | ||
48 | int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND; | |
49 | int32_t KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE = 1; | |
50 | std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY; | |
51 | uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33]; | |
52 | ||
53 | char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN],ASSETCHAINS_USERPASS[4096]; | |
54 | uint16_t ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT; | |
55 | uint32_t ASSETCHAIN_INIT,ASSETCHAINS_CC; | |
56 | uint32_t ASSETCHAINS_MAGIC = 2387029918; | |
57 | int64_t ASSETCHAINS_GENESISTXVAL = 5000000000; | |
58 | ||
59 | // consensus variables for coinbase timelock control and timelock transaction support | |
60 | // time locks are specified enough to enable their use initially to lock specific coinbase transactions for emission control | |
61 | // to be verifiable, timelocks require additional data that enables them to be validated and their ownership and | |
62 | // release time determined from the blockchain. to do this, every time locked output according to this | |
63 | // spec will use an op_return with CLTV at front and anything after |OP_RETURN|PUSH of rest|OPRETTYPE_TIMELOCK|script| | |
64 | #define _ASSETCHAINS_TIMELOCKOFF -1 | |
65 | int64_t ASSETCHAINS_TIMELOCKGTE = _ASSETCHAINS_TIMELOCKOFF; | |
66 | uint64_t ASSETCHAINS_TIMEUNLOCKFROM = 0, ASSETCHAINS_TIMEUNLOCKTO = 0; | |
67 | ||
68 | uint32_t ASSETCHAINS_LASTERA = 1; | |
69 | uint64_t ASSETCHAINS_ENDSUBSIDY[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_HALVING[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_DECAY[ASSETCHAINS_MAX_ERAS]; | |
70 | ||
71 | #define _ASSETCHAINS_EQUIHASH 0 | |
72 | uint32_t ASSETCHAINS_NUMALGOS = 2; | |
73 | uint32_t ASSETCHAINS_EQUIHASH = _ASSETCHAINS_EQUIHASH; | |
74 | uint32_t ASSETCHAINS_VERUSHASH = 1; | |
75 | const char *ASSETCHAINS_ALGORITHMS[] = {"equihash", "verushash"}; | |
76 | uint64_t ASSETCHAINS_NONCEMASK[] = {0xffff,0xffffff}; | |
77 | uint32_t ASSETCHAINS_NONCESHIFT[] = {32,40}; | |
78 | uint32_t ASSETCHAINS_HASHESPERROUND[] = {1,512}; | |
79 | uint32_t ASSETCHAINS_ALGO = _ASSETCHAINS_EQUIHASH; | |
80 | ||
81 | // Verus proof of stake controls | |
82 | int32_t ASSETCHAINS_LWMAPOS = 0; // percentage of blocks should be PoS | |
83 | int32_t VERUS_BLOCK_POSUNITS = 1000; // one block is 1000 units | |
84 | int32_t VERUS_MIN_STAKEAGE = 200; // 1/2 this should also be a cap on the POS averaging window, or startup could be too easy | |
85 | ||
86 | uint64_t KOMODO_INTERESTSUM,KOMODO_WALLETBALANCE; | |
87 | uint64_t ASSETCHAINS_COMMISSION,ASSETCHAINS_STAKED,ASSETCHAINS_SUPPLY = 10; | |
88 | ||
89 | uint32_t KOMODO_INITDONE; | |
90 | char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t KMD_PORT = 7771,BITCOIND_RPCPORT = 7771; | |
91 | uint64_t PENDING_KOMODO_TX; | |
92 | extern int32_t KOMODO_LOADINGBLOCKS; | |
93 | unsigned int MAX_BLOCK_SIGOPS = 20000; | |
94 | ||
95 | struct komodo_kv *KOMODO_KV; | |
96 | pthread_mutex_t KOMODO_KV_mutex,KOMODO_CC_mutex; | |
97 | ||
98 | #define MAX_CURRENCIES 32 | |
99 | char CURRENCIES[][8] = { "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", // major currencies | |
100 | "CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK", | |
101 | "KMD" }; | |
102 | ||
103 | int32_t komodo_baseid(char *origbase) | |
104 | { | |
105 | int32_t i; char base[64]; | |
106 | for (i=0; origbase[i]!=0&&i<sizeof(base); i++) | |
107 | base[i] = toupper((int32_t)(origbase[i] & 0xff)); | |
108 | base[i] = 0; | |
109 | for (i=0; i<=MAX_CURRENCIES; i++) | |
110 | if ( strcmp(CURRENCIES[i],base) == 0 ) | |
111 | return(i); | |
112 | //printf("illegal base.(%s) %s\n",origbase,base); | |
113 | return(-1); | |
114 | } | |
115 | ||
116 | #ifndef SATOSHIDEN | |
117 | #define SATOSHIDEN ((uint64_t)100000000L) | |
118 | #endif | |
119 | int64_t komodo_current_supply(uint32_t nHeight) | |
120 | { | |
121 | uint64_t cur_money; | |
122 | int32_t baseid; | |
123 | ||
124 | if ( (baseid = komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 && baseid < 32 ) | |
125 | cur_money = ASSETCHAINS_GENESISTXVAL + ASSETCHAINS_SUPPLY + nHeight * ASSETCHAINS_REWARD[0] / SATOSHIDEN; | |
126 | else | |
127 | { | |
128 | // figure out max_money by adding up supply to a maximum of 10,000,000 blocks | |
129 | cur_money = (ASSETCHAINS_SUPPLY+1) * SATOSHIDEN + (ASSETCHAINS_MAGIC & 0xffffff) + ASSETCHAINS_GENESISTXVAL; | |
130 | if ( ASSETCHAINS_LASTERA == 0 && ASSETCHAINS_REWARD[0] == 0 ) | |
131 | { | |
132 | cur_money += (nHeight * 10000) / SATOSHIDEN; | |
133 | } | |
134 | else | |
135 | { | |
136 | for ( int j = 0; j <= ASSETCHAINS_LASTERA; j++ ) | |
137 | { | |
138 | // if any condition means we have no more rewards, break | |
139 | if (j != 0 && (nHeight <= ASSETCHAINS_ENDSUBSIDY[j - 1] || (ASSETCHAINS_ENDSUBSIDY[j - 1] == 0 && | |
140 | (ASSETCHAINS_REWARD[j] == 0 && (j == ASSETCHAINS_LASTERA || ASSETCHAINS_DECAY[j] != SATOSHIDEN))))) | |
141 | break; | |
142 | ||
143 | // add rewards from this era, up to nHeight | |
144 | int64_t reward = ASSETCHAINS_REWARD[j]; | |
145 | if ( reward > 0 ) | |
146 | { | |
147 | uint64_t lastEnd = j == 0 ? 0 : ASSETCHAINS_ENDSUBSIDY[j - 1]; | |
148 | uint64_t curEnd = ASSETCHAINS_ENDSUBSIDY[j] == 0 ? nHeight : nHeight > ASSETCHAINS_ENDSUBSIDY[j] ? ASSETCHAINS_ENDSUBSIDY[j] : nHeight; | |
149 | uint64_t period = ASSETCHAINS_HALVING[j]; | |
150 | uint32_t nSteps = (curEnd - lastEnd) / period; | |
151 | uint32_t modulo = (curEnd - lastEnd) % period; | |
152 | uint64_t decay = ASSETCHAINS_DECAY[j]; | |
153 | ||
154 | // if exactly SATOSHIDEN, linear decay to zero or to next era, same as: | |
155 | // (next_era_reward + (starting reward - next_era_reward) / 2) * num_blocks | |
156 | if ( decay == SATOSHIDEN ) | |
157 | { | |
158 | int64_t lowestSubsidy, subsidyDifference, stepDifference, stepTriangle; | |
159 | int64_t denominator, modulo; | |
160 | int32_t sign = 1; | |
161 | ||
162 | if ( j == ASSETCHAINS_LASTERA ) | |
163 | { | |
164 | subsidyDifference = reward; | |
165 | lowestSubsidy = 0; | |
166 | } | |
167 | else | |
168 | { | |
169 | // Ex: -ac_eras=3 -ac_reward=0,384,24 -ac_end=1440,260640,0 -ac_halving=1,1440,2103840 -ac_decay 100000000,97750000,0 | |
170 | subsidyDifference = reward - ASSETCHAINS_REWARD[j + 1]; | |
171 | if (subsidyDifference < 0) | |
172 | { | |
173 | sign = -1; | |
174 | subsidyDifference *= sign; | |
175 | lowestSubsidy = reward; | |
176 | } | |
177 | else | |
178 | { | |
179 | lowestSubsidy = ASSETCHAINS_REWARD[j + 1]; | |
180 | } | |
181 | } | |
182 | ||
183 | // if we have not finished the current era, we need to caluclate a total as if we are at the end, with the current | |
184 | // subsidy. we will calculate the total of a linear era as follows. Each item represents an area calculation: | |
185 | // a) the rectangle from 0 to the lowest reward in the era * the number of blocks | |
186 | // b) the rectangle of the remainder of blocks from the lowest point of the era to the highest point of the era if any remainder | |
187 | // c) the minor triangle from the start of transition from the lowest point to the start of transition to the highest point | |
188 | // d) one halving triangle (half area of one full step) | |
189 | // | |
190 | // we also need: | |
191 | // e) number of steps = (n - erastart) / halving interval | |
192 | // | |
193 | // the total supply from era start up to height is: | |
194 | // a + b + c + (d * e) | |
195 | ||
196 | // calculate amount in one step's triangular protrusion over minor triangle's hypotenuse | |
197 | denominator = nSteps * period; | |
198 | ||
199 | // difference of one step vs. total | |
200 | stepDifference = (period * subsidyDifference) / denominator; | |
201 | ||
202 | // area == coin holding of one step triangle, protruding from minor triangle's hypotenuse | |
203 | stepTriangle = (period * stepDifference) >> 1; | |
204 | ||
205 | // sign is negative if slope is positive (start is less than end) | |
206 | if (sign < 0) | |
207 | { | |
208 | // use steps minus one for our calculations, and add the potentially partial rectangle | |
209 | // at the end | |
210 | cur_money += stepTriangle * (nSteps - 1); | |
211 | cur_money += stepTriangle * (nSteps - 1) * (nSteps - 1); | |
212 | ||
213 | // difference times number of steps is height of rectangle above lowest subsidy | |
214 | cur_money += modulo * stepDifference * nSteps; | |
215 | } | |
216 | else | |
217 | { | |
218 | // if negative slope, the minor triangle is the full number of steps, as the highest | |
219 | // level step is full. lowest subsidy is just the lowest so far | |
220 | lowestSubsidy = reward - (stepDifference * nSteps); | |
221 | ||
222 | // add the step triangles, one per step | |
223 | cur_money += stepTriangle * nSteps; | |
224 | ||
225 | // add the minor triangle | |
226 | cur_money += stepTriangle * nSteps * nSteps; | |
227 | } | |
228 | ||
229 | // add more for the base rectangle if lowest subsidy is not 0 | |
230 | cur_money += lowestSubsidy * (curEnd - lastEnd); | |
231 | } | |
232 | else | |
233 | { | |
234 | for ( int k = lastEnd; k < curEnd; k += period ) | |
235 | { | |
236 | cur_money += period * reward; | |
237 | // if zero, we do straight halving | |
238 | reward = decay ? (reward * decay) / SATOSHIDEN : reward >> 1; | |
239 | } | |
240 | cur_money += modulo * reward; | |
241 | } | |
242 | } | |
243 | } | |
244 | } | |
245 | } | |
246 | return((int64_t)(cur_money + (cur_money * ASSETCHAINS_COMMISSION) / SATOSHIDEN)); | |
247 | } | |
248 |