diff --git a/common/src/rpc_primitives/requests.rs b/common/src/rpc_primitives/requests.rs index 9b62923..159c4f6 100644 --- a/common/src/rpc_primitives/requests.rs +++ b/common/src/rpc_primitives/requests.rs @@ -36,6 +36,8 @@ pub struct GetLastBlockRequest {} #[derive(Serialize, Deserialize, Debug)] pub struct GetInitialTestnetAccountsRequest {} + +#[derive(Serialize, Deserialize, Debug)] pub struct GetAccountBalanceRequest { pub address: String, } diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index 7e4342a..456e409 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -972,7 +972,7 @@ impl NodeCore { let sc_addr = hex::encode([0; 32]); - //Native does not change its state + //Native contract does not change its state let state_changes: Vec = vec![]; let new_len = 0; let state_changes = (serde_json::to_value(state_changes).unwrap(), new_len); @@ -990,8 +990,7 @@ impl NodeCore { secret_r, sc_addr, state_changes, - ) - .into(); + ); tx.log(); { diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 8307f04..d99a782 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -111,35 +111,6 @@ impl SequencerCore { ); } - //Balance check - if let Ok(native_transfer_action) = - serde_json::from_slice::(&execution_input) - { - let from_balance = self - .store - .acc_store - .get_account_balance(&native_transfer_action.from); - let to_balance = self - .store - .acc_store - .get_account_balance(&native_transfer_action.to); - - if from_balance >= native_transfer_action.moved_balance { - self.store.acc_store.set_account_balance( - &native_transfer_action.from, - from_balance - native_transfer_action.moved_balance, - ); - self.store.acc_store.set_account_balance( - &native_transfer_action.to, - to_balance + native_transfer_action.moved_balance, - ); - } else { - return Err(TransactionMalformationErrorKind::BalanceMismatch { tx: tx_hash }); - } - } else { - return Err(TransactionMalformationErrorKind::FailedToDecode { tx: tx_hash }); - } - //Sanity check match tx_kind { TxKind::Public => { @@ -206,6 +177,33 @@ impl SequencerCore { ); } + //Balance check + if let Ok(native_transfer_action) = + serde_json::from_slice::(execution_input) + { + let from_balance = self + .store + .acc_store + .get_account_balance(&native_transfer_action.from); + let to_balance = self + .store + .acc_store + .get_account_balance(&native_transfer_action.to); + + if from_balance >= native_transfer_action.moved_balance { + self.store.acc_store.set_account_balance( + &native_transfer_action.from, + from_balance - native_transfer_action.moved_balance, + ); + self.store.acc_store.set_account_balance( + &native_transfer_action.to, + to_balance + native_transfer_action.moved_balance, + ); + } else { + return Err(TransactionMalformationErrorKind::BalanceMismatch { tx: tx_hash }); + } + } + Ok(tx) } diff --git a/sequencer_core/src/sequencer_store/accounts_store.rs b/sequencer_core/src/sequencer_store/accounts_store.rs index 6f09eaa..f44b5fe 100644 --- a/sequencer_core/src/sequencer_store/accounts_store.rs +++ b/sequencer_core/src/sequencer_store/accounts_store.rs @@ -170,7 +170,7 @@ mod tests { let acc1 = Account::new_with_balance(12); let acc2 = Account::new_with_balance(100); - let acc1_addr = acc1.address.clone(); + let acc1_addr = acc1.address; let mut seq_acc_store = SequencerAccountsStore::new(&[acc1, acc2]); @@ -197,8 +197,8 @@ mod tests { let acc1 = Account::new_with_balance(12); let acc2 = Account::new_with_balance(100); - let acc1_addr = acc1.address.clone(); - let acc2_addr = acc2.address.clone(); + let acc1_addr = acc1.address; + let acc2_addr = acc2.address; let seq_acc_store = SequencerAccountsStore::new(&[acc1, acc2]); @@ -220,9 +220,9 @@ mod tests { let acc2 = Account::new_with_balance(15); let acc3 = Account::new_with_balance(10); - let acc1_addr = acc1.address.clone(); - let acc2_addr = acc2.address.clone(); - let acc3_addr = acc3.address.clone(); + let acc1_addr = acc1.address; + let acc2_addr = acc2.address; + let acc3_addr = acc3.address; let seq_acc_store = SequencerAccountsStore::new(&[acc1, acc2, acc3]); @@ -230,15 +230,15 @@ mod tests { assert!(seq_acc_store.contains_account(&acc2_addr)); assert!(seq_acc_store.contains_account(&acc3_addr)); - let acc_balance = seq_acc_store.get_account_balance(&[6; 32]); + let acc_balance = seq_acc_store.get_account_balance(&acc1_addr); assert_eq!(acc_balance, 120); - let acc_balance = seq_acc_store.get_account_balance(&[7; 32]); + let acc_balance = seq_acc_store.get_account_balance(&acc2_addr); assert_eq!(acc_balance, 15); - let acc_balance = seq_acc_store.get_account_balance(&[8; 32]); + let acc_balance = seq_acc_store.get_account_balance(&acc3_addr); assert_eq!(acc_balance, 10); } diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index a867b2f..4375f9c 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -137,6 +137,8 @@ impl JsonHandler { respond(helperstruct) } + /// Returns the initial accounts for testnet + /// ToDo: Useful only for testnet and needs to be removed later async fn get_initial_testnet_accounts(&self, request: Request) -> Result { let _get_initial_testnet_accounts_request = GetInitialTestnetAccountsRequest::parse(Some(request.params))?; @@ -153,6 +155,8 @@ impl JsonHandler { }; respond(accounts_for_serialization) + } + /// Returns the balance of the account at the given address. /// The address must be a valid hex string of the correct length. async fn process_get_account_balance(&self, request: Request) -> Result { @@ -165,8 +169,7 @@ impl JsonHandler { let balance = { let state = self.sequencer_state.lock().await; state.store.acc_store.get_account_balance(&address) - } - .unwrap_or(0); + }; let helperstruct = GetAccountBalanceResponse { balance }; @@ -193,6 +196,7 @@ mod tests { use std::sync::Arc; use crate::{rpc_handler, JsonHandler}; + use accounts::account_core::Account; use common::rpc_primitives::RpcPollingConfig; use sequencer_core::{ config::{AccountInitialData, SequencerConfig}, @@ -206,14 +210,8 @@ mod tests { let tempdir = tempdir().unwrap(); let home = tempdir.path().to_path_buf(); let initial_accounts = vec![ - AccountInitialData { - addr: "cafe".repeat(16).to_string(), - balance: 100, - }, - AccountInitialData { - addr: "feca".repeat(16).to_string(), - balance: 200, - }, + AccountInitialData { balance: 100 }, + AccountInitialData { balance: 200 }, ]; SequencerConfig { @@ -228,14 +226,24 @@ mod tests { } } - fn json_handler_for_tests() -> JsonHandler { + fn json_handler_for_tests() -> (JsonHandler, Vec) { let config = sequencer_config_for_tests(); - let sequencer_core = Arc::new(Mutex::new(SequencerCore::start_from_config(config))); - JsonHandler { - polling_config: RpcPollingConfig::default(), - sequencer_state: sequencer_core, - } + let sequencer_core = SequencerCore::start_from_config(config); + let initial_accounts = sequencer_core + .store + .testnet_initial_accounts_full_data + .clone(); + + let sequencer_core = Arc::new(Mutex::new(sequencer_core)); + + ( + JsonHandler { + polling_config: RpcPollingConfig::default(), + sequencer_state: sequencer_core, + }, + initial_accounts, + ) } async fn call_rpc_handler_with_json(handler: JsonHandler, request_json: Value) -> Value { @@ -261,7 +269,7 @@ mod tests { #[actix_web::test] async fn test_get_account_balance_for_non_existent_account() { - let json_handler = json_handler_for_tests(); + let (json_handler, _) = json_handler_for_tests(); let request = serde_json::json!({ "jsonrpc": "2.0", "method": "get_account_balance", @@ -283,7 +291,7 @@ mod tests { #[actix_web::test] async fn test_get_account_balance_for_invalid_hex() { - let json_handler = json_handler_for_tests(); + let (json_handler, _) = json_handler_for_tests(); let request = serde_json::json!({ "jsonrpc": "2.0", "method": "get_account_balance", @@ -306,7 +314,7 @@ mod tests { #[actix_web::test] async fn test_get_account_balance_for_invalid_length() { - let json_handler = json_handler_for_tests(); + let (json_handler, _) = json_handler_for_tests(); let request = serde_json::json!({ "jsonrpc": "2.0", "method": "get_account_balance", @@ -329,11 +337,14 @@ mod tests { #[actix_web::test] async fn test_get_account_balance_for_existing_account() { - let json_handler = json_handler_for_tests(); + let (json_handler, initial_accounts) = json_handler_for_tests(); + + let acc1_addr = hex::encode(initial_accounts[0].address); + let request = serde_json::json!({ "jsonrpc": "2.0", "method": "get_account_balance", - "params": { "address": "cafe".repeat(16) }, + "params": { "address": acc1_addr }, "id": 1 }); let expected_response = serde_json::json!({