@@ -2,13 +2,19 @@ defmodule BIC.Epoch do
22 import ConsensusKV
33
44 @ epoch_emission_base BIC.Coin . to_flat ( 1_000_000 )
5+ @ epoch_emission_fixed BIC.Coin . to_flat ( 100_000 )
56 @ epoch_interval 100_000
67
7- def epoch_emission ( _epoch , acc \\ @ epoch_emission_base )
8- def epoch_emission ( 0 , acc ) do acc end
9- def epoch_emission ( epoch , acc ) do
8+ def epoch_emission ( epoch ) do
9+ epoch_emission_1 ( epoch ) + @ epoch_emission_fixed
10+ end
11+
12+ defp epoch_emission_1 ( _epoch , acc \\ @ epoch_emission_base )
13+ defp epoch_emission_1 ( 0 , acc ) do acc end
14+ defp epoch_emission_1 ( epoch , acc ) do
1015 sub = div ( acc * 333 , 1000000 )
11- epoch_emission ( epoch - 1 , acc - sub )
16+ emitted = acc - sub
17+ epoch_emission_1 ( epoch - 1 , emitted )
1218 end
1319
1420 def circulating_without_burn ( _epoch , _acc \\ 0 )
@@ -49,21 +55,20 @@ defmodule BIC.Epoch do
4955 end
5056
5157 # slash sols for malicious trainers
52- removedTrainers = kv_get ( "bic:epoch:trainers:removed:#{ epoch_fin } " ) || [ ]
53- kv_get_prefix ( "bic:epoch:solutions:" )
54- |> Enum . each ( fn ( { sol , sol_pk } ) ->
55- if sol_pk in removedTrainers do
56- kv_delete ( "bic:epoch:solutions:#{ sol } " )
57- end
58- end )
59-
58+ removedTrainers = kv_get ( "bic:epoch:trainers:removed:#{ epoch_fin } " )
59+ removedTrainers = if removedTrainers do :erlang . binary_to_term ( removedTrainers , [ :safe ] ) else [ ] end
6060 leaders = kv_get_prefix ( "bic:epoch:solutions:" )
6161 |> Enum . reduce ( % { } , fn ( { _sol , pk } , acc ) ->
62- Map . put ( acc , pk , Map . get ( acc , pk , 0 ) + 1 )
62+ if pk in removedTrainers do
63+ acc
64+ else
65+ Map . put ( acc , pk , Map . get ( acc , pk , 0 ) + 1 )
66+ end
6367 end )
6468 |> Enum . sort_by ( & { elem ( & 1 , 1 ) , elem ( & 1 , 0 ) } , :desc )
6569
6670 trainers = kv_get ( "bic:epoch:trainers:#{ epoch_fin } " )
71+ trainers = if trainers do :erlang . binary_to_term ( trainers , [ :safe ] ) else [ ] end
6772 trainers_to_recv_emissions = leaders
6873 |> Enum . filter ( & elem ( & 1 , 0 ) in trainers )
6974 |> Enum . take ( top_x )
@@ -74,9 +79,9 @@ defmodule BIC.Epoch do
7479
7580 emission_address = kv_get ( "bic:epoch:emission_address:#{ trainer } " )
7681 if emission_address do
77- kv_increment ( "bic:coin:balance:#{ emission_address } " , coins )
82+ kv_increment ( "bic:coin:balance:#{ emission_address } :AMA " , coins |> :erlang . integer_to_binary ( ) )
7883 else
79- kv_increment ( "bic:coin:balance:#{ trainer } " , coins )
84+ kv_increment ( "bic:coin:balance:#{ trainer } :AMA " , coins |> :erlang . integer_to_binary ( ) )
8085 end
8186 end )
8287
@@ -96,10 +101,10 @@ defmodule BIC.Epoch do
96101 #end
97102 end
98103 new_trainers = Enum . shuffle ( new_trainers )
99- kv_put ( "bic:epoch:trainers:#{ epoch_next } " , new_trainers )
104+ kv_put ( "bic:epoch:trainers:#{ epoch_next } " , new_trainers |> :erlang . term_to_binary ( [ :deterministic ] ) )
100105
101106 height = String . pad_leading ( "#{ env . entry . header_unpacked . height + 1 } " , 12 , "0" )
102- kv_put ( "bic:epoch:trainers:height:#{ height } " , new_trainers )
107+ kv_put ( "bic:epoch:trainers:height:#{ height } " , new_trainers |> :erlang . term_to_binary ( [ :deterministic ] ) )
103108 end
104109
105110 def slash_trainer_verify ( cur_epoch , malicious_pk , trainers , mask , signature ) do
@@ -117,12 +122,16 @@ defmodule BIC.Epoch do
117122 end
118123
119124 def call ( :slash_trainer , env , [ epoch , malicious_pk , signature , mask_size , mask ] ) do
125+ epoch = if is_binary ( epoch ) do :erlang . binary_to_integer ( epoch ) else epoch end
126+ mask_size = if is_binary ( mask_size ) do :erlang . binary_to_integer ( mask_size ) else mask_size end
127+
120128 cur_epoch = Entry . epoch ( env . entry )
121129 << mask :: size ( mask_size ) - bitstring , _ :: bitstring >> = mask
122130
123131 if cur_epoch != epoch , do: throw ( % { error: :invalid_epoch } )
124132
125133 trainers = kv_get ( "bic:epoch:trainers:#{ cur_epoch } " )
134+ trainers = if trainers do :erlang . binary_to_term ( trainers , [ :safe ] ) else [ ] end
126135 if malicious_pk not in trainers , do: throw ( % { error: :invalid_trainer_pk } )
127136
128137 # 75% vote
@@ -134,14 +143,15 @@ defmodule BIC.Epoch do
134143 msg = << "slash_trainer" , cur_epoch :: 32 - little , malicious_pk :: binary >>
135144 if ! BlsEx . verify? ( apk , signature , msg , BLS12AggSig . dst_motion ( ) ) , do: throw % { error: :invalid_signature }
136145
137- removed = kv_get ( "bic:epoch:trainers:removed:#{ cur_epoch } " ) || [ ]
138- kv_put ( "bic:epoch:trainers:removed:#{ cur_epoch } " , removed ++ [ malicious_pk ] )
146+ removed = kv_get ( "bic:epoch:trainers:removed:#{ cur_epoch } " )
147+ removed = if removed do :erlang . binary_to_term ( removed , [ :safe ] ) else [ ] end
148+ kv_put ( "bic:epoch:trainers:removed:#{ cur_epoch } " , ( removed ++ [ malicious_pk ] ) |> :erlang . term_to_binary ( [ :deterministic ] ) )
139149
140150 new_trainers = trainers -- [ malicious_pk ]
141- kv_put ( "bic:epoch:trainers:#{ cur_epoch } " , new_trainers )
151+ kv_put ( "bic:epoch:trainers:#{ cur_epoch } " , new_trainers |> :erlang . term_to_binary ( [ :deterministic ] ) )
142152
143153 height = String . pad_leading ( "#{ env . entry . header_unpacked . height + 1 } " , 12 , "0" )
144- kv_put ( "bic:epoch:trainers:height:#{ height } " , new_trainers )
154+ kv_put ( "bic:epoch:trainers:height:#{ height } " , new_trainers |> :erlang . term_to_binary ( [ :deterministic ] ) )
145155 end
146156
147157 @ doc """
0 commit comments