11defmodule ComputorGen do
22 use GenServer
33
4+ @ batch_iterations 2000
5+
46 def start ( type \\ nil ) do
57 send ( __MODULE__ , { :start , type } )
68 end
@@ -24,22 +26,22 @@ defmodule ComputorGen do
2426 end
2527
2628 def handle_info ( :tick , state ) do
27- state = cond do
28- ! state [ :enabled ] -> state
29- ! Application . fetch_env! ( :ama , :testnet ) ->
30- IO . puts "Computor currently cannot find sols on mainnet due to difficulty. Do not waste CPU running it."
31- state
29+ next_ms = cond do
30+ ! state [ :enabled ] -> 1000
3231 ! FabricSyncAttestGen . isQuorumIsInEpoch ( ) ->
3332 IO . puts "🔴 cannot compute: out_of_sync"
34- state
33+ 1000
3534 true ->
3635 tick ( state )
36+ 0
3737 end
38- :erlang . send_after ( 1000 , self ( ) , :tick )
38+ :erlang . send_after ( next_ms , self ( ) , :tick )
3939 { :noreply , state }
4040 end
4141
4242 def handle_info ( { :start , type } , state ) do
43+ threads = Application . get_env ( :ama , :computor_upow_threads , 0 )
44+ IO . puts "🔢 computor enabled (type=#{ inspect type } , upow_threads=#{ if threads == 0 , do: "auto" , else: threads } )"
4345 state = Map . put ( state , :enabled , true )
4446 state = Map . put ( state , :type , type )
4547 { :noreply , state }
@@ -53,23 +55,28 @@ defmodule ComputorGen do
5355 def handle_info ( _msg , state ) , do: { :noreply , state }
5456
5557 def tick ( state ) do
56- IO . puts "computor running #{ DateTime . utc_now ( ) } "
5758 pk = Application . fetch_env! ( :ama , :trainer_pk )
5859 pop = Application . fetch_env! ( :ama , :trainer_pop )
60+ threads = Application . get_env ( :ama , :computor_upow_threads , 0 )
5961
6062 coins = DB.Chain . balance ( pk )
6163 epoch = DB.Chain . epoch ( )
64+ segment_vr_hash = DB.Chain . segment_vr_hash ( )
65+ diff_bits = DB.Chain . diff_bits ( )
6266 hasExecCoins = coins >= BIC.Coin . to_cents ( 100 )
6367 cond do
68+ is_nil ( segment_vr_hash ) -> :ok # epoch segment_vr not set yet; nothing to compute against
69+
6470 ( state . type == :trainer and ! hasExecCoins ) or state . type == nil ->
65- sol = UPOW . compute_for ( epoch , EntryGenesis . signer ( ) , EntryGenesis . pop ( ) , pk , :crypto . strong_rand_bytes ( 96 ) , 100 )
71+ # Compute to ourselves as a node: own pk is the computor (reward recipient).
72+ sol = UPOW . compute ( epoch , EntryGenesis . signer ( ) , EntryGenesis . pop ( ) , pk , segment_vr_hash , diff_bits , @ batch_iterations , threads )
6673 if sol do
6774 IO . puts "🔢 tensor matmul complete! broadcasting sol.."
6875 NodeGen . broadcast ( % { op: :sol , sol: sol } )
6976 end
7077
7178 true ->
72- sol = UPOW . compute_for ( epoch , pk , pop , pk , :crypto . strong_rand_bytes ( 96 ) , 100 )
79+ sol = UPOW . compute ( epoch , pk , pop , pk , segment_vr_hash , diff_bits , @ batch_iterations , threads )
7380 if sol do
7481 sk = Application . fetch_env! ( :ama , :trainer_sk )
7582 packed_tx = TX . build ( sk , "Epoch" , "submit_sol" , [ sol ] )
0 commit comments