diff --git a/src/aqua/chain/deal_status.aqua b/src/aqua/chain/deal_status.aqua index be472859..67fa6629 100644 --- a/src/aqua/chain/deal_status.aqua +++ b/src/aqua/chain/deal_status.aqua @@ -3,7 +3,7 @@ aqua DealStatus declares * import Worker from "@fluencelabs/aqua-lib/workers.aqua" import JoinedDeal from "../decider/deal_storage.aqua" -import DEAL_STATUS_ENDED, DEAL_STATUS_ACTIVE, DEAL_STATUS_INACTIVE from "../decider/consts.aqua" +import DEAL_STATUS_ENDED, DEAL_STATUS_ACTIVE, DEAL_STATUS_INSUFFICIENT_FUNDS, DEAL_STATUS_NOT_ENOUGH_WORKERS from "../decider/consts.aqua" import spell_log, deal_log from "../fluence/spell.aqua" import SpellId, DealId from "../types.aqua" import remove_ended_deal from "../decider/deal_removed.aqua" @@ -41,10 +41,10 @@ func poll_deal_statuses_by_ids(spell_id: SpellId, api_endpoint: string, deal_ids if deal_status.status == DEAL_STATUS_ACTIVE: activate_deal(spell_id, deal_status.deal_id) else: - if deal_status.status == DEAL_STATUS_INACTIVE: + if deal_status.status == DEAL_STATUS_NOT_ENOUGH_WORKERS: deactivate_deal(spell_id, deal_status.deal_id) else: - if deal_status.status == DEAL_STATUS_ENDED: + if deal_status.status == DEAL_STATUS_ENDED || deal_status.status == DEAL_STATUS_INSUFFICIENT_FUNDS: remove_ended_deal(spell_id, deal_status.deal_id) else: spell_log(spell_id, ["unsupported deal status: ", deal_status]) diff --git a/src/aqua/decider/consts.aqua b/src/aqua/decider/consts.aqua index 7e93a21f..136ea05f 100644 --- a/src/aqua/decider/consts.aqua +++ b/src/aqua/decider/consts.aqua @@ -31,7 +31,8 @@ const SYNC_INFO = "sync_info" -- Deal statuses const DEAL_STATUS_ENDED = "ENDED" const DEAL_STATUS_ACTIVE = "ACTIVE" -const DEAL_STATUS_INACTIVE = "INACTIVE" +const DEAL_STATUS_INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS" +const DEAL_STATUS_NOT_ENOUGH_WORKERS = "NOT_ENOUGH_WORKERS" -- Keys prefixes func removed_state_key(deal_id: string) -> string: diff --git a/src/services/chain-connector/modules/chain_connector/src/jsonrpc/deal_status.rs b/src/services/chain-connector/modules/chain_connector/src/jsonrpc/deal_status.rs index b62e36b0..a783b997 100644 --- a/src/services/chain-connector/modules/chain_connector/src/jsonrpc/deal_status.rs +++ b/src/services/chain-connector/modules/chain_connector/src/jsonrpc/deal_status.rs @@ -1,3 +1,4 @@ +use std::fmt::Display; use crate::chain::chain_data::{parse_chain_data, ChainDataError}; use crate::curl::send_jsonrpc_batch; use crate::jsonrpc::deal_status::DealStatusError::UnknownStatus; @@ -23,29 +24,33 @@ pub enum DealStatusError { #[derive(Debug)] pub enum DealStatus { - INACTIVE = 0, - ACTIVE, - ENDED, + InsufficientFunds = 0, + Active, + Ended, + NotEnoughWorkers } impl DealStatus { fn from(num: u8) -> Option { match num { - 0 => Some(DealStatus::INACTIVE), - 1 => Some(DealStatus::ACTIVE), - 2 => Some(DealStatus::ENDED), + 0 => Some(DealStatus::InsufficientFunds), + 1 => Some(DealStatus::Active), + 2 => Some(DealStatus::Ended), + 3 => Some(DealStatus::NotEnoughWorkers), _ => None, } } } -impl ToString for DealStatus { - fn to_string(&self) -> String { - match self { - DealStatus::INACTIVE => "INACTIVE".to_string(), - DealStatus::ACTIVE => "ACTIVE".to_string(), - DealStatus::ENDED => "ENDED".to_string(), - } +impl Display for DealStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = match self { + DealStatus::InsufficientFunds => "INSUFFICIENT_FUNDS".to_string(), + DealStatus::Active => "ACTIVE".to_string(), + DealStatus::Ended => "ENDED".to_string(), + DealStatus::NotEnoughWorkers => "NOT_ENOUGH_WORKERS".to_string(), + }; + write!(f, "{}", str) } } @@ -202,7 +207,7 @@ mod tests { const DEAL_ID: &'static str = "0x6328bb918a01603adc91eae689b848a9ecaef26d"; const DEAL_ID_2: &'static str = "0x6328bb918a01603adc91eae689b848a9ecaef26f"; - let jsonrpc_inactive = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000000"}]"#; + let jsonrpc_INSUFFICIENT_FUNDS = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000000"}]"#; let jsonrpc_active = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000001"}]"#; let jsonrpc_ended = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000002"}]"#; let jsonrpc_unknown = r#"[{"jsonrpc":"2.0","id":0,"result":"0x"}]"#; @@ -216,7 +221,7 @@ mod tests { jsonrpc_unknown, jsonrpc_ended, jsonrpc_active, - jsonrpc_inactive, + jsonrpc_INSUFFICIENT_FUNDS, ])); let mock = server .mock("POST", "/") @@ -264,7 +269,7 @@ mod tests { ); assert!(result.statuses[0].error.is_empty()); - assert_eq!(result.statuses[0].status, "INACTIVE"); + assert_eq!(result.statuses[0].status, "INSUFFICIENT_FUNDS"); assert_eq!(result.statuses[0].deal_id, DEAL_ID); let result = connector.get_status_batch(url.clone(), vec![DEAL_ID.to_string()]); @@ -315,7 +320,7 @@ mod tests { assert!(result.statuses[0].error.is_empty()); assert_eq!(result.statuses[0].deal_id, DEAL_ID); - assert_eq!(result.statuses[0].status, "INACTIVE"); + assert_eq!(result.statuses[0].status, "INSUFFICIENT_FUNDS"); assert!(!result.statuses[1].success,); assert!(!result.statuses[1].error.is_empty()); diff --git a/src/tests/decider-distro-tests-rs/tests/test_activate.rs b/src/tests/decider-distro-tests-rs/tests/test_activate.rs index 77c69652..5f10488a 100644 --- a/src/tests/decider-distro-tests-rs/tests/test_activate.rs +++ b/src/tests/decider-distro-tests-rs/tests/test_activate.rs @@ -5,7 +5,7 @@ use crate::utils::chain::{DealStatusReq, LogsReq}; use crate::utils::control::{ update_config, update_decider_script_for_tests, wait_decider_stopped, wait_worker_spell_stopped, }; -use crate::utils::default::{default_receipt, DEAL_IDS, DEAL_STATUS_ACTIVE, DEAL_STATUS_INACTIVE}; +use crate::utils::default::{default_receipt, DEAL_IDS, DEAL_STATUS_ACTIVE, DEAL_STATUS_NOT_ENOUGH_WORKERS}; use crate::utils::distro::make_distro_with_api; use crate::utils::oneshot_config; use crate::utils::setup::{setup_nox, setup_rpc_empty_run_with_status}; @@ -20,7 +20,7 @@ pub mod utils; /// Test plan: /// - Use standard nox setup. -/// - On the first run, deploy a deal to create a worker. The deal is INACTIVE. +/// - On the first run, deploy a deal to create a worker. The deal is NOT_ENOUGH_WORKERS. /// - On the second run, the deal is ACTIVE, so decider should activate the worker. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_activate() { @@ -63,7 +63,7 @@ async fn test_activate() { "eth_call" => { let req = serde_json::from_value::(params[0].clone()).unwrap(); assert_eq!(req.to, deal_address, "request the status of the wrong deal"); - json!(DEAL_STATUS_INACTIVE) + json!(DEAL_STATUS_NOT_ENOUGH_WORKERS) } _ => panic!("mock http got an unexpected rpc method: {}", method), }; @@ -109,7 +109,7 @@ async fn test_activate() { setup_rpc_empty_run_with_status( &mut server, LATEST_BLOCK + 10, - DEAL_STATUS_INACTIVE, + DEAL_STATUS_NOT_ENOUGH_WORKERS, joined.len(), ) .await diff --git a/src/tests/decider-distro-tests-rs/tests/utils/default.rs b/src/tests/decider-distro-tests-rs/tests/utils/default.rs index 176fbc84..877471d5 100644 --- a/src/tests/decider-distro-tests-rs/tests/utils/default.rs +++ b/src/tests/decider-distro-tests-rs/tests/utils/default.rs @@ -24,8 +24,8 @@ pub fn default_receipt() -> Value { pub const DEAL_STATUS_ACTIVE: &str = "0x0000000000000000000000000000000000000000000000000000000000000001"; -pub const DEAL_STATUS_INACTIVE: &str = - "0x0000000000000000000000000000000000000000000000000000000000000000"; +pub const DEAL_STATUS_NOT_ENOUGH_WORKERS: &str = + "0x0000000000000000000000000000000000000000000000000000000000000003"; pub const DEAL_STATUS_ENDED: &str = "0x0000000000000000000000000000000000000000000000000000000000000002"; pub fn default_status() -> Value {