/
ar_pricing.erl
672 lines (618 loc) · 25.4 KB
/
ar_pricing.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
-module(ar_pricing).
%% 2.6 exports.
-export([is_v2_pricing_height/1, get_price_per_gib_minute/2, get_tx_fee/1,
get_miner_reward_endowment_pool_debt_supply/1, recalculate_price_per_gib_minute/1,
redenominate/3, may_be_redenominate/1]).
%% 2.5 exports.
-export([get_tx_fee/4, get_miner_reward_and_endowment_pool/1, get_tx_fee_pre_fork_2_4/4,
usd_to_ar_rate/1, usd_to_ar/3, recalculate_usd_to_ar_rate/1, usd_to_ar_pre_fork_2_4/3,
get_miner_reward_and_endowment_pool_pre_fork_2_4/1, get_storage_cost/4,
get_expected_min_decline_rate/6]).
-include_lib("arweave/include/ar.hrl").
-include_lib("arweave/include/ar_inflation.hrl").
-include_lib("arweave/include/ar_pricing.hrl").
-include_lib("arweave/include/ar_consensus.hrl").
-include_lib("eunit/include/eunit.hrl").
%%%===================================================================
%%% Types.
%%%===================================================================
-type nonegint() :: non_neg_integer().
-type fraction() :: {integer(), integer()}.
-type usd() :: float() | fraction().
-type date() :: {nonegint(), nonegint(), nonegint()}.
-type time() :: {nonegint(), nonegint(), nonegint()}.
-type datetime() :: {date(), time()}.
%%%===================================================================
%%% Public interface 2.6.
%%%===================================================================
%% @doc Return true if the given height is a height where the transition to the
%% new pricing algorithm is complete.
is_v2_pricing_height(Height) ->
Fork_2_6_8 = ar_fork:height_2_6_8(),
Height >= Fork_2_6_8 % First check just this because it may be infinity.
andalso Height >= Fork_2_6_8 + (?PRICE_2_6_8_TRANSITION_START)
+ (?PRICE_2_6_8_TRANSITION_BLOCKS).
%% @doc Return the price per gibibyte minute estimated from the given history of
%% network hash rates and block rewards. The total reward used in calculations
%% is at least 1 Winston, even if all block rewards from the given history are 0.
%% Also, the returned price is always at least 1 Winston.
get_price_per_gib_minute(RewardHistory, Denomination2) ->
{HashRateTotal, RewardTotal} =
lists:foldl(
fun({_Addr, HashRate, Reward, Denomination}, {Acc1, Acc2}) ->
Reward2 = redenominate(Reward, Denomination, Denomination2),
{Acc1 + HashRate, Acc2 + Reward2}
end,
{0, 0},
RewardHistory
),
%% 2 recall ranges per partition per second.
SolutionsPerPartitionPerSecond = 2 * (?RECALL_RANGE_SIZE) div (?DATA_CHUNK_SIZE),
SolutionsPerPartitionPerMinute = SolutionsPerPartitionPerSecond * 60,
SolutionsPerPartitionPerTwoMinutes = SolutionsPerPartitionPerMinute * 2,
%% Estimated partition count = hash rate / 2 / solutions per partition per minute.
%% 2 minutes is the average block time.
%% Estimated data size = estimated partition count * partition size.
%% Estimated price per gib minute = total block reward / estimated data size in gibibytes.
(max(1, RewardTotal) * (?GiB) * SolutionsPerPartitionPerTwoMinutes)
div (max(1, HashRateTotal)
* (?PARTITION_SIZE)
* 2 % The reward is paid every two minutes whereas we are calculating
% the minute rate here.
).
%% @doc Return the minimum required transaction fee for the given number of
%% total bytes stored and gibibyte minute price.
get_tx_fee(Args) ->
{DataSize, GiBMinutePrice, KryderPlusRateMultiplier, Height} = Args,
FirstYearPrice = DataSize * GiBMinutePrice * 60 * 24 * 365,
{LnDecayDividend, LnDecayDivisor} = ?LN_PRICE_DECAY_ANNUAL,
PerpetualPrice = {-FirstYearPrice * LnDecayDivisor * KryderPlusRateMultiplier
* (?N_REPLICATIONS(Height)), LnDecayDividend * (?GiB)},
MinerShare = ar_fraction:multiply(PerpetualPrice,
?MINER_MINIMUM_ENDOWMENT_CONTRIBUTION_SHARE),
{Dividend, Divisor} = ar_fraction:add(PerpetualPrice, MinerShare),
Dividend div Divisor.
%% @doc Return the block reward, the new endowment pool, and the new debt supply.
get_miner_reward_endowment_pool_debt_supply(Args) ->
{EndowmentPool, DebtSupply, TXs, WeaveSize, Height, GiBMinutePrice,
KryderPlusRateMultiplierLatch, KryderPlusRateMultiplier, Denomination} = Args,
Inflation = redenominate(ar_inflation:calculate(Height), 1, Denomination),
ExpectedReward = (?N_REPLICATIONS(Height)) * WeaveSize * GiBMinutePrice * 2 div (?GiB),
{EndowmentPoolFeeShare, MinerFeeShare} = distribute_transaction_fees2(TXs, Denomination),
BaseReward = Inflation + MinerFeeShare,
EndowmentPool2 = EndowmentPool + EndowmentPoolFeeShare,
case BaseReward >= ExpectedReward of
true ->
{BaseReward, EndowmentPool2, DebtSupply, KryderPlusRateMultiplierLatch,
KryderPlusRateMultiplier};
false ->
Take = ExpectedReward - BaseReward,
{EndowmentPool3, DebtSupply2} =
case Take > EndowmentPool2 of
true ->
{0, DebtSupply + Take - EndowmentPool2};
false ->
{EndowmentPool2 - Take, DebtSupply}
end,
{KryderPlusRateMultiplierLatch2, KryderPlusRateMultiplier2} =
case {Take > EndowmentPool2, KryderPlusRateMultiplierLatch} of
{true, 0} ->
{1, KryderPlusRateMultiplier * 2};
{false, 1} ->
Threshold = redenominate(?RESET_KRYDER_PLUS_LATCH_THRESHOLD, 1,
Denomination),
case EndowmentPool3 > Threshold of
true ->
{0, KryderPlusRateMultiplier};
false ->
{1, KryderPlusRateMultiplier}
end;
_ ->
{KryderPlusRateMultiplierLatch, KryderPlusRateMultiplier}
end,
{BaseReward + Take, EndowmentPool3, DebtSupply2, KryderPlusRateMultiplierLatch2,
KryderPlusRateMultiplier2}
end.
%% @doc Return the new current and scheduled prices per byte minute.
recalculate_price_per_gib_minute(B) ->
#block{ height = PrevHeight } = B,
Height = PrevHeight + 1,
Fork_2_6 = ar_fork:height_2_6(),
true = Height >= Fork_2_6,
case Height > Fork_2_6 of
false ->
get_initial_current_and_scheduled_price_per_gib_minute(B);
true ->
recalculate_price_per_gib_minute2(B)
end.
%% @doc Return the denominated amount.
redenominate(Amount, 0, _Denomination) ->
Amount;
redenominate(Amount, BaseDenomination, BaseDenomination) ->
Amount;
redenominate(Amount, BaseDenomination, Denomination) when Denomination > BaseDenomination ->
redenominate(Amount * 1000, BaseDenomination, Denomination - 1).
%% @doc Increase the amount of base currency units in the system if
%% the available supply is too low.
may_be_redenominate(B) ->
#block{ height = Height, denomination = Denomination,
redenomination_height = RedenominationHeight } = B,
case is_v2_pricing_height(Height + 1) of
false ->
{Denomination, RedenominationHeight};
true ->
may_be_redenominate2(B)
end.
may_be_redenominate2(B) ->
#block{ height = Height, denomination = Denomination,
redenomination_height = RedenominationHeight } = B,
case Height == RedenominationHeight of
true ->
{Denomination + 1, RedenominationHeight};
false ->
case Height < RedenominationHeight of
true ->
{Denomination, RedenominationHeight};
false ->
may_be_redenominate3(B)
end
end.
may_be_redenominate3(B) ->
#block{ height = Height, debt_supply = DebtSupply, reward_pool = EndowmentPool,
denomination = Denomination, redenomination_height = RedenominationHeight } = B,
TotalSupply = get_total_supply(Denomination),
case TotalSupply + DebtSupply - EndowmentPool < (?REDENOMINATION_THRESHOLD) of
true ->
{Denomination, Height + (?REDENOMINATION_DELAY_BLOCKS)};
false ->
{Denomination, RedenominationHeight}
end.
get_initial_current_and_scheduled_price_per_gib_minute(B) ->
#block{ diff = Diff } = B,
HashRate = ar_difficulty:get_hash_rate(Diff),
Reward = ar_inflation:calculate(B#block.height),
Denomination = 1,
Price = get_price_per_gib_minute([{B#block.reward_addr, HashRate, Reward, Denomination}],
Denomination),
{Price, Price}.
recalculate_price_per_gib_minute2(B) ->
#block{ height = PrevHeight, reward_history = RewardHistory,
denomination = Denomination, price_per_gib_minute = Price,
scheduled_price_per_gib_minute = ScheduledPrice } = B,
case is_price_adjustment_height(PrevHeight + 1) of
false ->
{Price, ScheduledPrice};
true ->
RewardHistory2 = lists:sublist(RewardHistory, ?REWARD_HISTORY_BLOCKS),
Price2 = min(Price * 2, get_price_per_gib_minute(RewardHistory2, Denomination)),
Price3 = max(Price div 2, Price2),
{ScheduledPrice, Price3}
end.
is_price_adjustment_height(Height) ->
Height rem ?PRICE_ADJUSTMENT_FREQUENCY == 0.
distribute_transaction_fees2(TXs, Denomination) ->
distribute_transaction_fees2(TXs, 0, 0, Denomination).
distribute_transaction_fees2([], EndowmentPoolTotal, MinerTotal, _Denomination) ->
{EndowmentPoolTotal, MinerTotal};
distribute_transaction_fees2([TX | TXs], EndowmentPoolTotal, MinerTotal, Denomination) ->
TXFee = redenominate(TX#tx.reward, TX#tx.denomination, Denomination),
{Dividend, Divisor} = ?MINER_FEE_SHARE,
MinerFee = TXFee * Dividend div Divisor,
EndowmentPoolTotal2 = EndowmentPoolTotal + TXFee - MinerFee,
MinerTotal2 = MinerTotal + MinerFee,
distribute_transaction_fees2(TXs, EndowmentPoolTotal2, MinerTotal2, Denomination).
get_total_supply(Denomination) ->
redenominate(?TOTAL_SUPPLY, 1, Denomination).
%%%===================================================================
%%% Public interface 2.5.
%%%===================================================================
%% @doc Return the perpetual cost of storing the given amount of data.
get_storage_cost(DataSize, Timestamp, Rate, Height) ->
Size = ?TX_SIZE_BASE + DataSize,
PerpetualGBStorageCost =
usd_to_ar(
get_perpetual_gb_cost_at_timestamp(Timestamp, Height),
Rate,
Height
),
StorageCost = max(1, PerpetualGBStorageCost div (1024 * 1024 * 1024)) * Size,
HashingCost = StorageCost,
StorageCost + HashingCost.
%% @doc Calculate the transaction fee.
get_tx_fee(DataSize, Timestamp, Rate, Height) ->
MaintenanceCost = get_storage_cost(DataSize, Timestamp, Rate, Height),
MinerFeeShare = get_miner_fee_share(MaintenanceCost, Height),
MaintenanceCost + MinerFeeShare.
%% @doc Return the miner reward and the new endowment pool.
get_miner_reward_and_endowment_pool({Pool, TXs, unclaimed, _, _, _, _}) ->
{0, Pool + lists:sum([TX#tx.reward || TX <- TXs])};
get_miner_reward_and_endowment_pool(Args) ->
{Pool, TXs, _Addr, WeaveSize, Height, Timestamp, Rate} = Args,
Inflation = trunc(ar_inflation:calculate(Height)),
{PoolFeeShare, MinerFeeShare} = distribute_transaction_fees(TXs, Height),
BaseReward = Inflation + MinerFeeShare,
StorageCostPerGBPerBlock =
usd_to_ar(
get_gb_cost_per_block_at_timestamp(Timestamp, Height),
Rate,
Height
),
Burden = WeaveSize * StorageCostPerGBPerBlock div (1024 * 1024 * 1024),
Pool2 = Pool + PoolFeeShare,
case BaseReward >= Burden of
true ->
{BaseReward, Pool2};
false ->
Take = min(Pool2, Burden - BaseReward),
{BaseReward + Take, Pool2 - Take}
end.
%% @doc Calculate the transaction fee.
get_tx_fee_pre_fork_2_4(Size, Diff, Height, Timestamp) ->
GBs = (?TX_SIZE_BASE + Size) / (1024 * 1024 * 1024),
true = Height >= ar_fork:height_2_0(),
PerGB =
usd_to_ar_pre_fork_2_4(
get_perpetual_gb_cost_at_timestamp(Timestamp, Height),
Diff,
Height
),
StorageCost = PerGB * GBs,
HashingCost = StorageCost,
MaintenanceCost = erlang:trunc(StorageCost + HashingCost),
MinerFeeShare = get_miner_fee_share(MaintenanceCost, Height),
MaintenanceCost + MinerFeeShare.
%% @doc Return the miner reward and the new endowment pool.
get_miner_reward_and_endowment_pool_pre_fork_2_4({Pool, TXs, unclaimed, _, _, _, _}) ->
{0, Pool + lists:sum([TX#tx.reward || TX <- TXs])};
get_miner_reward_and_endowment_pool_pre_fork_2_4(Args) ->
{Pool, TXs, _RewardAddr, WeaveSize, Height, Diff, Timestamp} = Args,
true = Height >= ar_fork:height_2_0(),
Inflation = trunc(ar_inflation:calculate(Height)),
{PoolFeeShare, MinerFeeShare} = distribute_transaction_fees(TXs, Height),
BaseReward = Inflation + MinerFeeShare,
StorageCostPerGBPerBlock =
usd_to_ar_pre_fork_2_4(
get_gb_cost_per_block_at_timestamp(Timestamp, Height),
Diff,
Height
),
Burden = trunc(WeaveSize * StorageCostPerGBPerBlock / (1024 * 1024 * 1024)),
Pool2 = Pool + PoolFeeShare,
case BaseReward >= Burden of
true ->
{BaseReward, Pool2};
false ->
Take = min(Pool2, Burden - BaseReward),
{BaseReward + Take, Pool2 - Take}
end.
%% @doc Return the effective USD to AR rate corresponding to the given block
%% considering its previous block.
usd_to_ar_rate(#block{ height = PrevHeight } = PrevB) ->
Height_2_5 = ar_fork:height_2_5(),
Height = PrevHeight + 1,
case PrevHeight < Height_2_5 of
true ->
?INITIAL_USD_TO_AR(Height)();
false ->
PrevB#block.usd_to_ar_rate
end.
%% @doc Return the amount of AR the given number of USD is worth.
usd_to_ar(USD, Rate, Height) when is_number(USD) ->
usd_to_ar({USD, 1}, Rate, Height);
usd_to_ar({Dividend, Divisor}, Rate, Height) ->
InitialInflation = trunc(ar_inflation:calculate(?INITIAL_USD_TO_AR_HEIGHT(Height)())),
CurrentInflation = trunc(ar_inflation:calculate(Height)),
{InitialRateDividend, InitialRateDivisor} = Rate,
trunc( Dividend
* ?WINSTON_PER_AR
* CurrentInflation
* InitialRateDividend )
div Divisor
div InitialInflation
div InitialRateDivisor.
recalculate_usd_to_ar_rate(#block{ height = PrevHeight } = B) ->
Height = PrevHeight + 1,
Fork_2_5 = ar_fork:height_2_5(),
true = Height >= Fork_2_5,
case Height > Fork_2_5 of
false ->
Rate = ?INITIAL_USD_TO_AR(Height)(),
{Rate, Rate};
true ->
Fork_2_6 = ar_fork:height_2_6(),
case Height == Fork_2_6 of
true ->
{B#block.usd_to_ar_rate, ?FORK_2_6_PRE_TRANSITION_USD_TO_AR_RATE};
false ->
recalculate_usd_to_ar_rate2(B)
end
end.
%% @doc Return the amount of AR the given number of USD is worth.
usd_to_ar_pre_fork_2_4(USD, Diff, Height) ->
InitialDiff =
ar_retarget:switch_to_linear_diff_pre_fork_2_4(?INITIAL_USD_TO_AR_DIFF(Height)()),
MaxDiff = ?MAX_DIFF,
DeltaP = (MaxDiff - InitialDiff) / (MaxDiff - Diff),
InitialInflation = ar_inflation:calculate(?INITIAL_USD_TO_AR_HEIGHT(Height)()),
DeltaInflation = ar_inflation:calculate(Height) / InitialInflation,
erlang:trunc(
(USD * ?WINSTON_PER_AR * DeltaInflation) / (?INITIAL_USD_PER_AR(Height)() * DeltaP)
).
%% @doc Return an estimation for the minimum required decline rate making the given
%% Amount (in Winston) sufficient to subsidize storage for Period seconds starting from
%% Timestamp and assuming the given USD to AR rate.
%% When computing the exponent, the function accounts for the first 16 summands in
%% the Taylor series. The fraction is reduced to the 1/1000000 precision.
get_expected_min_decline_rate(Timestamp, Period, Amount, Size, Rate, Height) ->
{USDDiv1, USDDivisor1} = get_gb_cost_per_year_at_timestamp(Timestamp, Height),
%% Multiply by 2 to account for hashing costs.
Sum1 = 2 * usd_to_ar({USDDiv1, USDDivisor1}, Rate, Height),
{USDDiv2, USDDivisor2} = get_gb_cost_per_year_at_timestamp(Timestamp + Period, Height),
Sum2 = 2 * usd_to_ar({USDDiv2, USDDivisor2}, Rate, Height),
%% Sum1 / -logRate - Sum2 / -logRate = Amount
%% => -logRate = (Sum1 - Sum2) / Amount
%% => 1 / Rate = exp((Sum1 - Sum2) / Amount)
%% => Rate = 1 / exp((Sum1 - Sum2) / Amount)
{ExpDiv, ExpDivisor} = ar_fraction:natural_exponent(
{(Sum1 - Sum2) * Size, Amount * (1024 * 1024 * 1024)}, 16),
ar_fraction:reduce({ExpDivisor, ExpDiv}, 1000000).
%%%===================================================================
%%% Private functions.
%%%===================================================================
%% @doc Get the share of the maintenance cost the miner receives for a transation.
get_miner_fee_share(MaintenanceCost, Height) ->
{Dividend, Divisor} = ?MINING_REWARD_MULTIPLIER,
case Height >= ar_fork:height_2_5() of
false ->
erlang:trunc(MaintenanceCost * (Dividend / Divisor));
true ->
MaintenanceCost * Dividend div Divisor
end.
distribute_transaction_fees(TXs, Height) ->
distribute_transaction_fees(TXs, 0, 0, Height).
distribute_transaction_fees([], EndowmentPool, Miner, _Height) ->
{EndowmentPool, Miner};
distribute_transaction_fees([TX | TXs], EndowmentPool, Miner, Height) ->
TXFee = TX#tx.reward,
{Dividend, Divisor} = ?MINING_REWARD_MULTIPLIER,
MinerFee =
case Height >= ar_fork:height_2_5() of
false ->
erlang:trunc((Dividend / Divisor) * TXFee / ((Dividend / Divisor) + 1));
true ->
TXFee * Dividend div (Dividend + Divisor)
end,
distribute_transaction_fees(TXs, EndowmentPool + TXFee - MinerFee, Miner + MinerFee,
Height).
%% @doc Return the cost of storing 1 GB in the network perpetually.
%% Integral of the exponential decay curve k*e^(-at), i.e. k/a.
%% @end
-spec get_perpetual_gb_cost_at_timestamp(Timestamp::integer(), Height::nonegint()) -> usd().
get_perpetual_gb_cost_at_timestamp(Timestamp, Height) ->
K = get_gb_cost_per_year_at_timestamp(Timestamp, Height),
get_perpetual_gb_cost(K, Height).
-spec get_perpetual_gb_cost(Init::usd(), Height::nonegint()) -> usd().
get_perpetual_gb_cost(Init, Height) ->
case Height >= ar_fork:height_2_5() of
true ->
{LnDecayDividend, LnDecayDivisor} = ?LN_PRICE_DECAY_ANNUAL,
{InitDividend, InitDivisor} = Init,
{-InitDividend * LnDecayDivisor, InitDivisor * LnDecayDividend};
false ->
{Dividend, Divisor} = ?PRICE_DECAY_ANNUAL,
Init / -math:log(Dividend / Divisor)
end.
%% @doc Return the cost in USD of storing 1 GB per year at the given time.
-spec get_gb_cost_per_year_at_timestamp(Timestamp::integer(), Height::nonegint()) -> usd().
get_gb_cost_per_year_at_timestamp(Timestamp, Height) ->
Datetime = system_time_to_universal_time(Timestamp, seconds),
get_gb_cost_per_year_at_datetime(Datetime, Height).
%% @doc Return the cost in USD of storing 1 GB per average block time at the given time.
-spec get_gb_cost_per_block_at_timestamp(integer(), nonegint()) -> usd().
get_gb_cost_per_block_at_timestamp(Timestamp, Height) ->
Datetime = system_time_to_universal_time(Timestamp, seconds),
get_gb_cost_per_block_at_datetime(Datetime, Height).
%% @doc Return the cost in USD of storing 1 GB per year.
-spec get_gb_cost_per_year_at_datetime(DT::datetime(), Height::nonegint()) -> usd().
get_gb_cost_per_year_at_datetime({{Y, M, _}, _} = DT, Height) ->
PrevY = prev_jun_30_year(Y, M),
NextY = next_jun_30_year(Y, M),
FracY = fraction_of_year(PrevY, NextY, DT, Height),
PrevYCost = usd_p_gby(PrevY, Height),
NextYCost = usd_p_gby(NextY, Height),
case Height >= ar_fork:height_2_5() of
true ->
{FracYDividend, FracYDivisor} = FracY,
{PrevYCostDividend, PrevYCostDivisor} = PrevYCost,
{NextYCostDividend, NextYCostDivisor} = NextYCost,
Dividend =
(?N_REPLICATIONS(Height))
* (
PrevYCostDividend * NextYCostDivisor * FracYDivisor
- FracYDividend
* (
PrevYCostDividend
* NextYCostDivisor
- NextYCostDividend
* PrevYCostDivisor
)
),
Divisor =
PrevYCostDivisor
* NextYCostDivisor
* FracYDivisor,
{Dividend, Divisor};
false ->
CY = PrevYCost - (FracY * (PrevYCost - NextYCost)),
CY * (?N_REPLICATIONS(Height))
end.
prev_jun_30_year(Y, M) when M < 7 ->
Y - 1;
prev_jun_30_year(Y, _M) ->
Y.
next_jun_30_year(Y, M) when M < 7 ->
Y;
next_jun_30_year(Y, _M) ->
Y + 1.
%% @doc Return the cost in USD of storing 1 GB per average block time.
-spec get_gb_cost_per_block_at_datetime(DT::datetime(), Height::nonegint()) -> usd().
get_gb_cost_per_block_at_datetime(DT, Height) ->
case Height >= ar_fork:height_2_5() of
true ->
{Dividend, Divisor} = get_gb_cost_per_year_at_datetime(DT, Height),
{Dividend, Divisor * ?BLOCKS_PER_YEAR};
false ->
get_gb_cost_per_year_at_datetime(DT, Height) / ?BLOCKS_PER_YEAR
end.
%% @doc Return the cost in USD of storing 1 GB per year. Estmimated from empirical data.
%% Assumes a year after 2019 inclusive. Uses data figures for 2018 and 2019.
%% Extrapolates the exponential decay curve k*e^(-at) to future years.
%% @end
-spec usd_p_gby(nonegint(), nonegint()) -> usd().
usd_p_gby(2018, Height) ->
{Dividend, Divisor} = ?USD_PER_GBY_2018,
case Height >= ar_fork:height_2_5() of
true ->
{Dividend, Divisor};
false ->
Dividend / Divisor
end;
usd_p_gby(2019, Height) ->
{Dividend, Divisor} = ?USD_PER_GBY_2019,
case Height >= ar_fork:height_2_5() of
true ->
{Dividend, Divisor};
false ->
Dividend / Divisor
end;
usd_p_gby(Y, Height) ->
case Height >= ar_fork:height_2_5() of
true ->
{KDividend, KDivisor} = ?USD_PER_GBY_2019,
{ADividend, ADivisor} = ?LN_PRICE_DECAY_ANNUAL,
T = Y - 2019,
P = ?TX_PRICE_NATURAL_EXPONENT_DECIMAL_FRACTION_PRECISION,
{EDividend, EDivisor} = ar_fraction:natural_exponent({ADividend * T, ADivisor}, P),
{EDividend * KDividend, EDivisor * KDivisor};
false ->
{Dividend, Divisor} = ?USD_PER_GBY_2019,
K = Dividend / Divisor,
{DecayDividend, DecayDivisor} = ?PRICE_DECAY_ANNUAL,
A = math:log(DecayDividend / DecayDivisor),
T = Y - 2019,
K * math:exp(A * T)
end.
%% @doc Return elapsed time as the fraction of the year
%% between Jun 30th of PrevY and Jun 30th of NextY.
%% @end
-spec fraction_of_year(nonegint(), nonegint(), datetime(), nonegint()) -> float() | fraction().
fraction_of_year(PrevY, NextY, {{Y, Mo, D}, {H, Mi, S}}, Height) ->
Start = calendar:datetime_to_gregorian_seconds({{PrevY, 6, 30}, {23, 59, 59}}),
Now = calendar:datetime_to_gregorian_seconds({{Y, Mo, D}, {H, Mi, S}}),
End = calendar:datetime_to_gregorian_seconds({{NextY, 6, 30}, {23, 59, 59}}),
case Height >= ar_fork:height_2_5() of
true ->
{Now - Start, End - Start};
false ->
(Now - Start) / (End - Start)
end.
%% TODO Use calendar:system_time_to_universal_time/2 in Erlang OTP-21.
system_time_to_universal_time(Time, TimeUnit) ->
Seconds = erlang:convert_time_unit(Time, TimeUnit, seconds),
DaysFrom0To1970 = 719528,
SecondsPerDay = 86400,
calendar:gregorian_seconds_to_datetime(Seconds + (DaysFrom0To1970 * SecondsPerDay)).
recalculate_usd_to_ar_rate2(#block{ height = PrevHeight } = B) ->
case is_price_adjustment_height(PrevHeight + 1) of
false ->
{B#block.usd_to_ar_rate, B#block.scheduled_usd_to_ar_rate};
true ->
Fork_2_6 = ar_fork:height_2_6(),
true = PrevHeight + 1 /= Fork_2_6,
case PrevHeight + 1 > Fork_2_6 of
true ->
%% Keep the rate fixed after the 2.6 fork till the transition to the
%% new pricing scheme ends. Then it won't be used any longer.
{B#block.scheduled_usd_to_ar_rate, B#block.scheduled_usd_to_ar_rate};
false ->
recalculate_usd_to_ar_rate3(B)
end
end.
recalculate_usd_to_ar_rate3(#block{ height = PrevHeight, diff = Diff } = B) ->
Height = PrevHeight + 1,
InitialDiff = ar_retarget:switch_to_linear_diff(?INITIAL_USD_TO_AR_DIFF(Height)()),
MaxDiff = ?MAX_DIFF,
InitialRate = ?INITIAL_USD_TO_AR(Height)(),
{Dividend, Divisor} = InitialRate,
ScheduledRate = {Dividend * (MaxDiff - Diff), Divisor * (MaxDiff - InitialDiff)},
Rate = B#block.scheduled_usd_to_ar_rate,
MaxAdjustmentUp = ar_fraction:multiply(Rate, ?USD_TO_AR_MAX_ADJUSTMENT_UP_MULTIPLIER),
MaxAdjustmentDown = ar_fraction:multiply(Rate, ?USD_TO_AR_MAX_ADJUSTMENT_DOWN_MULTIPLIER),
CappedScheduledRate = ar_fraction:reduce(ar_fraction:maximum(
ar_fraction:minimum(ScheduledRate, MaxAdjustmentUp), MaxAdjustmentDown),
?USD_TO_AR_FRACTION_REDUCTION_LIMIT),
?LOG_DEBUG([{event, recalculated_rate},
{new_rate, ar_util:safe_divide(element(1, Rate), element(2, Rate))},
{new_scheduled_rate, ar_util:safe_divide(element(1, CappedScheduledRate),
element(2, CappedScheduledRate))},
{new_scheduled_rate_without_capping,
ar_util:safe_divide(element(1, ScheduledRate), element(2, ScheduledRate))},
{max_adjustment_up, ar_util:safe_divide(element(1, MaxAdjustmentUp),
element(2,MaxAdjustmentUp))},
{max_adjustment_down, ar_util:safe_divide(element(1, MaxAdjustmentDown),
element(2,MaxAdjustmentDown))}]),
{Rate, CappedScheduledRate}.
%%%===================================================================
%%% Tests.
%%%===================================================================
get_gb_cost_per_year_at_datetime_is_monotone_test_() ->
[
ar_test_fork:test_on_fork(
height_2_5, infinity, fun test_get_gb_cost_per_year_at_datetime_is_monotone/0
) | [
ar_test_fork:test_on_fork(
height_2_5, Height, fun test_get_gb_cost_per_year_at_datetime_is_monotone/0
)
|| Height <- lists:seq(0, 20)
]
].
test_get_gb_cost_per_year_at_datetime_is_monotone() ->
InitialDT = {{2019, 1, 1}, {0, 0, 0}},
FollowingDTs = [
{{2019, 1, 1}, {10, 0, 0}},
{{2019, 6, 15}, {0, 0, 0}},
{{2019, 6, 29}, {23, 59, 59}},
{{2019, 6, 30}, {0, 0, 0}},
{{2019, 6, 30}, {23, 59, 59}},
{{2019, 7, 1}, {0, 0, 0}},
{{2019, 12, 31}, {23, 59, 59}},
{{2020, 1, 1}, {0, 0, 0}},
{{2020, 1, 2}, {0, 0, 0}},
{{2020, 10, 1}, {0, 0, 0}},
{{2020, 12, 31}, {23, 59, 59}},
{{2021, 1, 1}, {0, 0, 0}},
{{2021, 2, 1}, {0, 0, 0}},
{{2021, 12, 31}, {23, 59, 59}},
{{2022, 1, 1}, {0, 0, 0}},
{{2022, 6, 29}, {23, 59, 59}},
{{2022, 6, 30}, {0, 0, 0}},
{{2050, 3, 1}, {10, 10, 10}},
{{2100, 2, 1}, {0, 0, 0}}
],
lists:foldl(
fun(CurrDT, {PrevDT, PrevHeight}) ->
CurrCost = get_gb_cost_per_year_at_datetime(CurrDT, PrevHeight + 1),
PrevCost = get_gb_cost_per_year_at_datetime(PrevDT, PrevHeight),
assert_less_than_or_equal_to(CurrCost, PrevCost),
{CurrDT, PrevHeight + 1}
end,
{InitialDT, 0},
FollowingDTs
).
assert_less_than_or_equal_to(X1, X2) when is_number(X1), is_number(X2) ->
?assert(X1 =< X2, io_lib:format("~p is bigger than ~p", [X1, X2]));
assert_less_than_or_equal_to({Dividend1, Divisor1} = X1, X2) when is_number(X2) ->
?assert((Dividend1 div Divisor1) =< X2, io_lib:format("~p is bigger than ~p", [X1, X2]));
assert_less_than_or_equal_to({Dividend1, Divisor1} = X1, {Dividend2, Divisor2} = X2) ->
?assert(Dividend1 * Divisor2 =< Dividend2 * Divisor1,
io_lib:format("~p is bigger than ~p", [X1, X2])).