diff --git a/tig-protocol/src/context.rs b/tig-protocol/src/context.rs index 63a9882..41c9584 100644 --- a/tig-protocol/src/context.rs +++ b/tig-protocol/src/context.rs @@ -10,12 +10,6 @@ pub trait Context { details: AdvanceDetails, evidence: String, ) -> Result; - async fn get_algorithm_state(&self, algorithm_id: &String) -> Option; - async fn add_algorithm_to_mempool( - &self, - details: AlgorithmDetails, - code: AlgorithmCode, - ) -> Result; async fn get_benchmark_details(&self, benchmark_id: &String) -> Option; async fn get_benchmark_data( &self, @@ -28,12 +22,8 @@ pub trait Context { solution_nonces: HashSet, discarded_solution_nonces: HashSet, ) -> Result<()>; - async fn get_binary_details(&self, algorithm_id: &String) -> Option; - async fn add_binary_to_mempool( - &self, - algorithm_id: String, - details: BinaryDetails, - ) -> Result<()>; + async fn get_binary_details(&self, code_id: &String) -> Option; + async fn add_binary_to_mempool(&self, code_id: String, details: BinaryDetails) -> Result<()>; async fn get_latest_block_id(&self) -> String; async fn get_block_details(&self, block_id: &String) -> Option; async fn get_challenge_state(&self, challenge_id: &String) -> Option; @@ -42,6 +32,8 @@ pub trait Context { challenge_id: &String, block_id: &String, ) -> Option; + async fn get_code_state(&self, code_id: &String) -> Option; + async fn add_code_to_mempool(&self, details: CodeDetails, code: SourceCode) -> Result; async fn get_config(&self) -> ProtocolConfig; async fn add_deposit_to_mempool(&self, details: DepositDetails) -> Result; async fn get_player_details(&self, player_id: &String) -> Option; @@ -95,9 +87,9 @@ pub struct AddBlockCache { pub active_opow_block_data: HashMap, pub active_challenges_block_data: HashMap, pub active_challenges_prev_block_data: HashMap, - pub active_algorithms_state: HashMap, - pub active_algorithms_details: HashMap, - pub active_algorithms_block_data: HashMap, + pub active_codes_state: HashMap, + pub active_codes_details: HashMap, + pub active_codes_block_data: HashMap, pub voting_advances_state: HashMap, pub active_advances_state: HashMap, pub active_advances_details: HashMap, diff --git a/tig-protocol/src/contracts/algorithms.rs b/tig-protocol/src/contracts/algorithms.rs index 35025dc..e67b469 100644 --- a/tig-protocol/src/contracts/algorithms.rs +++ b/tig-protocol/src/contracts/algorithms.rs @@ -9,7 +9,7 @@ use tig_utils::*; pub async fn submit_advance( ctx: &T, player_id: String, - advance_name: String, + name: String, challenge_id: String, evidence: String, ) -> Result { @@ -35,7 +35,7 @@ pub async fn submit_advance( let advance_id = ctx .add_advance_to_mempool( AdvanceDetails { - name: advance_name, + name, challenge_id, player_id, fee_paid: config.advances.submission_fee, @@ -47,13 +47,13 @@ pub async fn submit_advance( } #[time] -pub async fn submit_algorithm( +pub async fn submit_code( ctx: &T, player_id: String, - algorithm_name: String, + name: String, challenge_id: String, - advance_id: Option, - code: AlgorithmCode, + algorithm_id: Option, + code: SourceCode, ) -> Result { let config = ctx.get_config().await; let latest_block_id = ctx.get_latest_block_id().await; @@ -66,13 +66,13 @@ pub async fn submit_algorithm( <= latest_block_details.round + config.advances.vote_start_delay + config.advances.vote_period - + config.algorithms.push_delay_period + + config.codes.push_delay_period }) { return Err(anyhow!("Invalid challenge '{}'", challenge_id)); } - if let Some(advance_id) = &advance_id { + if let Some(advance_id) = &algorithm_id { if ctx.get_advance_state(advance_id).await.is_none() { return Err(anyhow!("Invalid advance '{}'", advance_id)); } @@ -81,19 +81,19 @@ pub async fn submit_algorithm( if !ctx .get_player_state(&player_id) .await - .is_some_and(|s| s.available_fee_balance >= config.algorithms.submission_fee) + .is_some_and(|s| s.available_fee_balance >= config.codes.submission_fee) { return Err(anyhow!("Insufficient balance")); } let algorithm_id = ctx - .add_algorithm_to_mempool( - AlgorithmDetails { - name: algorithm_name, + .add_code_to_mempool( + CodeDetails { + name, challenge_id, player_id, - advance_id, - fee_paid: config.algorithms.submission_fee, + algorithm_id, + fee_paid: config.codes.submission_fee, }, code, ) @@ -108,12 +108,12 @@ pub async fn submit_binary( compile_success: bool, download_url: Option, ) -> Result<()> { - if ctx.get_algorithm_state(&algorithm_id).await.is_none() { + if ctx.get_code_state(&algorithm_id).await.is_none() { return Err(anyhow!("Invalid algorithm: {}", algorithm_id)); } if ctx.get_binary_details(&algorithm_id).await.is_some() { return Err(anyhow!( - "WASM already submitted for algorithm: {}", + "Binary already submitted for algorithm: {}", algorithm_id )); } @@ -138,9 +138,9 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { config, block_details, block_data, - active_algorithms_details, - active_algorithms_state, - active_algorithms_block_data, + active_codes_details, + active_codes_state, + active_codes_block_data, active_advances_state, active_advances_block_data, active_opow_block_data, @@ -150,7 +150,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { .. } = cache; - let active_algorithm_ids = &block_data.active_ids[&ActiveType::Algorithm]; + let active_code_ids = &block_data.active_ids[&ActiveType::Code]; let active_advance_ids = &block_data.active_ids[&ActiveType::Advance]; let active_challenge_ids = &block_data.active_ids[&ActiveType::Challenge]; let active_player_ids = &block_data.active_ids[&ActiveType::Player]; @@ -180,17 +180,17 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { } // update adoption - let mut algorithms_by_challenge = HashMap::>::new(); - for algorithm_id in active_algorithm_ids.iter() { - let algorithm_details = &active_algorithms_details[algorithm_id]; - algorithms_by_challenge - .entry(algorithm_details.challenge_id.clone()) + let mut codes_by_challenge = HashMap::>::new(); + for algorithm_id in active_code_ids.iter() { + let code_details = &active_codes_details[algorithm_id]; + codes_by_challenge + .entry(code_details.challenge_id.clone()) .or_default() .push(algorithm_id.clone()); } for challenge_id in active_challenge_ids.iter() { - let algorithm_ids = match algorithms_by_challenge.get(challenge_id) { + let algorithm_ids = match codes_by_challenge.get(challenge_id) { None => continue, Some(ids) => ids, }; @@ -198,7 +198,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { let mut weights = Vec::::new(); for algorithm_id in algorithm_ids.iter() { let mut weight = PreciseNumber::from(0); - for (player_id, &num_qualifiers) in active_algorithms_block_data[algorithm_id] + for (player_id, &num_qualifiers) in active_codes_block_data[algorithm_id] .num_qualifiers_by_player .iter() { @@ -214,27 +214,27 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { let adoption = weights.normalise(); for (algorithm_id, adoption) in algorithm_ids.iter().zip(adoption) { - active_algorithms_block_data + active_codes_block_data .get_mut(algorithm_id) .unwrap() .adoption = adoption.clone(); - if let Some(advance_id) = &active_algorithms_details[algorithm_id].advance_id { - if let Some(block_data) = active_advances_block_data.get_mut(advance_id) { + if let Some(algorithm_id) = &active_codes_details[algorithm_id].algorithm_id { + if let Some(block_data) = active_advances_block_data.get_mut(algorithm_id) { block_data.adoption += adoption; } } } } - // update algorithm merge points - let adoption_threshold = PreciseNumber::from_f64(config.algorithms.adoption_threshold); - for algorithm_id in active_algorithm_ids.iter() { - let is_merged = active_algorithms_state[algorithm_id].round_merged.is_some(); - let algorithm_data = active_algorithms_block_data.get_mut(algorithm_id).unwrap(); + // update code merge points + let adoption_threshold = PreciseNumber::from_f64(config.codes.adoption_threshold); + for algorithm_id in active_code_ids.iter() { + let is_merged = active_codes_state[algorithm_id].round_merged.is_some(); + let code_data = active_codes_block_data.get_mut(algorithm_id).unwrap(); - if !is_merged && algorithm_data.adoption >= adoption_threshold { - algorithm_data.merge_points += 1; + if !is_merged && code_data.adoption >= adoption_threshold { + code_data.merge_points += 1; } } @@ -251,33 +251,33 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { // update merges at last block of the round if (block_details.height + 1) % config.rounds.blocks_per_round == 0 { - for algorithm_ids in algorithms_by_challenge.values() { + for algorithm_ids in codes_by_challenge.values() { let algorithm_id = algorithm_ids .iter() - .max_by_key(|&id| active_algorithms_block_data[id].merge_points) + .max_by_key(|&id| active_codes_block_data[id].merge_points) .unwrap(); - if active_algorithms_block_data[algorithm_id].merge_points - < config.algorithms.merge_points_threshold + if active_codes_block_data[algorithm_id].merge_points + < config.codes.merge_points_threshold { continue; } - active_algorithms_state + active_codes_state .get_mut(algorithm_id) .unwrap() .round_merged = Some(block_details.round + 1); } - for advance_id in active_advance_ids.iter() { - if active_advances_block_data[advance_id].merge_points + for algorithm_id in active_advance_ids.iter() { + if active_advances_block_data[algorithm_id].merge_points < config.advances.merge_points_threshold { continue; } active_advances_state - .get_mut(advance_id) + .get_mut(algorithm_id) .unwrap() .round_merged = Some(block_details.round + 1); } diff --git a/tig-protocol/src/contracts/benchmarks.rs b/tig-protocol/src/contracts/benchmarks.rs index 80d1db8..ebd480a 100644 --- a/tig-protocol/src/contracts/benchmarks.rs +++ b/tig-protocol/src/contracts/benchmarks.rs @@ -42,7 +42,7 @@ pub async fn submit_precommit( // verify algorithm is active if !ctx - .get_algorithm_state(&settings.algorithm_id) + .get_code_state(&settings.algorithm_id) .await .is_some_and(|s| !s.banned && s.round_active.is_some_and(|r| r <= block_details.round)) { diff --git a/tig-protocol/src/contracts/opow.rs b/tig-protocol/src/contracts/opow.rs index f34e584..fef1fcf 100644 --- a/tig-protocol/src/contracts/opow.rs +++ b/tig-protocol/src/contracts/opow.rs @@ -12,9 +12,9 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { block_data, active_challenges_block_data, active_challenges_prev_block_data, - active_algorithms_state, - active_algorithms_details, - active_algorithms_block_data, + active_codes_state, + active_codes_details, + active_codes_block_data, active_solutions, active_players_state, active_players_block_data, @@ -23,7 +23,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { .. } = cache; - let active_algorithm_ids = &block_data.active_ids[&ActiveType::Algorithm]; + let active_code_ids = &block_data.active_ids[&ActiveType::Code]; let active_challenge_ids = &block_data.active_ids[&ActiveType::Challenge]; let active_player_ids = &block_data.active_ids[&ActiveType::Player]; let active_opow_ids = &block_data.active_ids[&ActiveType::OPoW]; @@ -44,13 +44,13 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { .collect::>(); let mut phase_in_challenge_ids: HashSet = active_challenge_ids.clone(); - for algorithm_id in active_algorithm_ids.iter() { - if active_algorithms_state[algorithm_id] + for algorithm_id in active_code_ids.iter() { + if active_codes_state[algorithm_id] .round_active .as_ref() .is_some_and(|r| *r + 1 <= block_details.round) { - phase_in_challenge_ids.remove(&active_algorithms_details[algorithm_id].challenge_id); + phase_in_challenge_ids.remove(&active_codes_details[algorithm_id].challenge_id); } } @@ -175,7 +175,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { let challenge_data = active_challenges_block_data.get_mut(challenge_id).unwrap(); let min_num_nonces = config.opow.min_num_nonces as u64; - let mut player_algorithm_solutions = HashMap::>::new(); + let mut player_code_solutions = HashMap::>::new(); let mut player_solutions = HashMap::::new(); let mut player_discarded_solutions = HashMap::::new(); let mut player_nonces = HashMap::::new(); @@ -201,7 +201,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { { continue; } - *player_algorithm_solutions + *player_code_solutions .entry(player_id.clone()) .or_default() .entry(algorithm_id.clone()) @@ -261,14 +261,13 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { player_solution_ratio[player_id] * player_qualifiers[player_id] as f64; if player_qualifiers[player_id] > 0 { - for algorithm_id in player_algorithm_solutions[player_id].keys() { - let algorithm_data = - active_algorithms_block_data.get_mut(algorithm_id).unwrap(); + for algorithm_id in player_code_solutions[player_id].keys() { + let code_data = active_codes_block_data.get_mut(algorithm_id).unwrap(); - algorithm_data.num_qualifiers_by_player.insert( + code_data.num_qualifiers_by_player.insert( player_id.clone(), (player_qualifiers[player_id] as f64 - * player_algorithm_solutions[player_id][algorithm_id] as f64 + * player_code_solutions[player_id][algorithm_id] as f64 / player_solutions[player_id] as f64) .ceil() as u32, ); diff --git a/tig-protocol/src/contracts/rewards.rs b/tig-protocol/src/contracts/rewards.rs index 78fd942..e0733ac 100644 --- a/tig-protocol/src/contracts/rewards.rs +++ b/tig-protocol/src/contracts/rewards.rs @@ -9,9 +9,9 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { config, block_details, block_data, - active_algorithms_state, - active_algorithms_block_data, - active_algorithms_details, + active_codes_state, + active_codes_block_data, + active_codes_details, active_opow_block_data, active_players_block_data, active_players_state, @@ -21,7 +21,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { .. } = cache; - let active_algorithm_ids = &block_data.active_ids[&ActiveType::Algorithm]; + let active_code_ids = &block_data.active_ids[&ActiveType::Code]; let active_advance_ids = &block_data.active_ids[&ActiveType::Advance]; let active_challenge_ids = &block_data.active_ids[&ActiveType::Challenge]; @@ -45,41 +45,39 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { ); let scaled_reward = block_reward * PreciseNumber::from_f64(block_details.gamma_value.clone()); - // update algorithm rewards - let adoption_threshold = PreciseNumber::from_f64(config.algorithms.adoption_threshold); - let algorithms_reward_pool = - scaled_reward * PreciseNumber::from_f64(config.rewards.distribution.algorithms); + // update code rewards + let adoption_threshold = PreciseNumber::from_f64(config.codes.adoption_threshold); + let codes_reward_pool = + scaled_reward * PreciseNumber::from_f64(config.rewards.distribution.codes); let reward_pool_per_challenge = - algorithms_reward_pool / PreciseNumber::from(active_challenge_ids.len()); - let mut total_algorithms_reward = zero.clone(); - for algorithm_id in active_algorithm_ids.iter() { - let algorithm_state = &active_algorithms_state[algorithm_id]; - let algorithm_data = active_algorithms_block_data.get_mut(algorithm_id).unwrap(); - let algorithm_details = &active_algorithms_details[algorithm_id]; + codes_reward_pool / PreciseNumber::from(active_challenge_ids.len()); + let mut total_codes_reward = zero.clone(); + for algorithm_id in active_code_ids.iter() { + let code_state = &active_codes_state[algorithm_id]; + let code_data = active_codes_block_data.get_mut(algorithm_id).unwrap(); + let code_details = &active_codes_details[algorithm_id]; - let is_merged = algorithm_state.round_merged.is_some(); - if algorithm_state.banned { + let is_merged = code_state.round_merged.is_some(); + if code_state.banned { continue; } let player_data = active_players_block_data - .get_mut(&algorithm_details.player_id) + .get_mut(&code_details.player_id) .unwrap(); player_data .reward_by_type - .insert(EmissionsType::Algorithm, zero.clone()); + .insert(EmissionsType::Code, zero.clone()); - if algorithm_data.adoption >= adoption_threshold - || (is_merged && algorithm_data.adoption > zero) - { - let reward = reward_pool_per_challenge * algorithm_data.adoption; - algorithm_data.reward = reward; - total_algorithms_reward += reward; + if code_data.adoption >= adoption_threshold || (is_merged && code_data.adoption > zero) { + let reward = reward_pool_per_challenge * code_data.adoption; + code_data.reward = reward; + total_codes_reward += reward; *player_data .reward_by_type - .get_mut(&EmissionsType::Algorithm) - .unwrap() += algorithm_data.reward; + .get_mut(&EmissionsType::Code) + .unwrap() += code_data.reward; } } @@ -187,13 +185,12 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { block_details.emissions.insert( EmissionsType::Bootstrap, - (advances_reward_pool - total_advances_reward) - + (algorithms_reward_pool - total_algorithms_reward), + (advances_reward_pool - total_advances_reward) + (codes_reward_pool - total_codes_reward), ); block_details.emissions.insert( EmissionsType::Vault, block_reward - - algorithms_reward_pool + - codes_reward_pool - advances_reward_pool - total_benchmarkers_reward - total_delegators_reward @@ -201,7 +198,7 @@ pub(crate) async fn update(cache: &mut AddBlockCache) { ); block_details .emissions - .insert(EmissionsType::Algorithm, total_algorithms_reward); + .insert(EmissionsType::Code, total_codes_reward); block_details .emissions .insert(EmissionsType::Advance, total_advances_reward); diff --git a/tig-protocol/src/lib.rs b/tig-protocol/src/lib.rs index 8199279..7887ae2 100644 --- a/tig-protocol/src/lib.rs +++ b/tig-protocol/src/lib.rs @@ -3,7 +3,7 @@ mod contracts; use context::*; pub use contracts::{ - algorithms::{submit_advance, submit_algorithm, submit_binary}, + algorithms::{submit_advance, submit_binary, submit_code}, benchmarks::{submit_benchmark, submit_precommit, submit_proof}, players::{set_coinbase, set_delegatees, set_reward_share, set_vote}, }; diff --git a/tig-structs/src/config.rs b/tig-structs/src/config.rs index 8d6f9f9..480b25d 100644 --- a/tig-structs/src/config.rs +++ b/tig-structs/src/config.rs @@ -7,9 +7,9 @@ use tig_utils::PreciseNumber; serializable_struct_with_getters! { ProtocolConfig { advances: AdvancesConfig, - algorithms: AlgorithmsConfig, benchmarks: BenchmarksConfig, challenges: ChallengesConfig, + codes: CodesConfig, deposits: DepositsConfig, erc20: ERC20Config, opow: OPoWConfig, @@ -124,7 +124,7 @@ serializable_struct_with_getters! { } } serializable_struct_with_getters! { - AlgorithmsConfig { + CodesConfig { submission_fee: PreciseNumber, adoption_threshold: f64, merge_points_threshold: u32, @@ -140,7 +140,7 @@ serializable_struct_with_getters! { serializable_struct_with_getters! { DistributionConfig { opow: f64, - algorithms: f64, + codes: f64, advances: f64, challenge_owners: f64, } diff --git a/tig-structs/src/core.rs b/tig-structs/src/core.rs index 0897916..0519636 100644 --- a/tig-structs/src/core.rs +++ b/tig-structs/src/core.rs @@ -5,14 +5,6 @@ use std::collections::{HashMap, HashSet}; use tig_utils::{jsonify, u64s_from_str, u8s_from_str}; pub use tig_utils::{Frontier, MerkleBranch, MerkleHash, Point, PreciseNumber, U256}; -serializable_struct_with_getters! { - Algorithm { - id: String, - details: AlgorithmDetails, - state: AlgorithmState, - block_data: Option, - } -} serializable_struct_with_getters! { Benchmark { id: String, @@ -53,6 +45,14 @@ serializable_struct_with_getters! { block_data: Option, } } +serializable_struct_with_getters! { + Code { + id: String, + details: CodeDetails, + state: CodeState, + block_data: Option, + } +} serializable_struct_with_getters! { Deposit { id: String, @@ -141,41 +141,6 @@ serializable_struct_with_getters! { } } -// Algorithm child structs -serializable_struct_with_getters! { - AlgorithmCode { - rust: String, - cuda: Option, - } -} -serializable_struct_with_getters! { - AlgorithmDetails { - name: String, - player_id: String, - challenge_id: String, - advance_id: Option, - fee_paid: PreciseNumber, - } -} -serializable_struct_with_getters! { - AlgorithmState { - block_confirmed: u32, - round_submitted: u32, - round_pushed: Option, - round_active: Option, - round_merged: Option, - banned: bool, - } -} -serializable_struct_with_getters! { - AlgorithmBlockData { - num_qualifiers_by_player: HashMap, - adoption: PreciseNumber, - merge_points: u32, - reward: PreciseNumber, - } -} - // Benchmark child structs serializable_struct_with_getters! { BenchmarkSettings { @@ -251,10 +216,10 @@ serializable_struct_with_getters! { #[serde(rename_all = "lowercase")] pub enum TxType { Advance, - Algorithm, Benchmark, Binary, Challenge, + Code, Deposit, Fraud, Precommit, @@ -266,9 +231,9 @@ pub enum TxType { #[serde(rename_all = "lowercase")] pub enum ActiveType { Advance, - Algorithm, Benchmark, Challenge, + Code, Deposit, OPoW, Player, @@ -277,10 +242,10 @@ pub enum ActiveType { #[serde(rename_all = "snake_case")] pub enum EmissionsType { Advance, - Algorithm, Benchmarker, Bootstrap, ChallengeOwner, + Code, Delegator, Vault, } @@ -335,6 +300,41 @@ serializable_struct_with_getters! { } } +// Code child structs +serializable_struct_with_getters! { + SourceCode { + rust: String, + cuda: Option, + } +} +serializable_struct_with_getters! { + CodeDetails { + name: String, + player_id: String, + challenge_id: String, + algorithm_id: Option, + fee_paid: PreciseNumber, + } +} +serializable_struct_with_getters! { + CodeState { + block_confirmed: u32, + round_submitted: u32, + round_pushed: Option, + round_active: Option, + round_merged: Option, + banned: bool, + } +} +serializable_struct_with_getters! { + CodeBlockData { + num_qualifiers_by_player: HashMap, + adoption: PreciseNumber, + merge_points: u32, + reward: PreciseNumber, + } +} + // Deposit child structs #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")]