Skip to content

Commit

Permalink
Add 'wallet' column in the 'backup_txs' table
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Oct 23, 2024
1 parent 7760cf7 commit 7b15c73
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions clients/libs/rust/migrations/0001_signer_data_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS wallet (
);

CREATE TABLE IF NOT EXISTS backup_txs (
wallet_name TEXT NOT NULL,
statechain_id TEXT NOT NULL,
txs TEXT NOT NULL
);
2 changes: 1 addition & 1 deletion clients/libs/rust/src/broadcast_backup_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain
}
}

let backup_txs = get_backup_txs(&client_config.pool, &statechain_id).await?;
let backup_txs = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?;

// If the user sends to himself, he will have two coins with same statechain_id
// In this case, we need to find the one with the lowest locktime
Expand Down
2 changes: 1 addition & 1 deletion clients/libs/rust/src/coin_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub async fn update_coins(client_config: &ClientConfig, wallet_name: &str) -> Re
let backup_tx = deposit_result.backup_tx;

wallet.activities.push(activity);
insert_backup_txs(&client_config.pool, &coin.statechain_id.as_ref().unwrap(), &[backup_tx].to_vec()).await?;
insert_backup_txs(&client_config.pool, &wallet.name, &coin.statechain_id.as_ref().unwrap(), &[backup_tx].to_vec()).await?;
}
} else if coin.status == CoinStatus::IN_TRANSFER {

Expand Down
23 changes: 14 additions & 9 deletions clients/libs/rust/src/sqlite_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ pub async fn update_wallet(pool: &Pool<Sqlite>, wallet: &Wallet) -> Result<()> {
Ok(())
}

pub async fn insert_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {
pub async fn insert_backup_txs(pool: &Pool<Sqlite>, wallet_name: &str, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {

let backup_txs_json = json!(backup_txs).to_string();

let query = "INSERT INTO backup_txs (statechain_id, txs) VALUES ($1, $2)";
let query = "INSERT INTO backup_txs (wallet_name, statechain_id, txs) VALUES ($1, $2, $3)";

let _ = sqlx::query(query)
.bind(wallet_name)
.bind(statechain_id)
.bind(backup_txs_json)
.execute(pool)
Expand All @@ -68,27 +69,29 @@ pub async fn insert_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str, backup_
Ok(())
}

pub async fn update_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {
pub async fn update_backup_txs(pool: &Pool<Sqlite>, wallet_name: &str, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {

let backup_txs_json = json!(backup_txs).to_string();

let query = "UPDATE backup_txs SET txs = $1 WHERE statechain_id = $2";
let query = "UPDATE backup_txs SET txs = $1 WHERE statechain_id = $2 AND wallet_name = $3";

let _ = sqlx::query(query)
.bind(backup_txs_json)
.bind(statechain_id)
.bind(wallet_name)
.execute(pool)
.await?;

Ok(())
}

pub async fn get_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str,) -> Result<Vec<BackupTx>> {
pub async fn get_backup_txs(pool: &Pool<Sqlite>, wallet_name: &str, statechain_id: &str,) -> Result<Vec<BackupTx>> {

let query = "SELECT txs FROM backup_txs WHERE statechain_id = $1";
let query = "SELECT txs FROM backup_txs WHERE statechain_id = $1 AND wallet_name = $2";

let row = sqlx::query(query)
.bind(statechain_id)
.bind(wallet_name)
.fetch_one(pool)
.await?;

Expand All @@ -103,23 +106,25 @@ pub async fn get_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str,) -> Result
Ok(backup_txs)
}

pub async fn insert_or_update_backup_txs(pool: &Pool<Sqlite>, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {
pub async fn insert_or_update_backup_txs(pool: &Pool<Sqlite>, wallet_name: &str, statechain_id: &str, backup_txs: &Vec<BackupTx>) -> Result<()> {

let mut transaction = pool.begin().await?;

let backup_txs_json = json!(backup_txs).to_string();

let query = "DELETE FROM backup_txs WHERE statechain_id = $1";
let query = "DELETE FROM backup_txs WHERE statechain_id = $1 AND wallet_name = $2";

let _ = sqlx::query(query)
.bind(statechain_id)
.bind(wallet_name)
.execute(&mut *transaction)
.await?;

let query = "INSERT INTO backup_txs (statechain_id, txs) VALUES ($1, $2)";
let query = "INSERT INTO backup_txs (statechain_id, wallet_name, txs) VALUES ($1, $2, $3)";

let _ = sqlx::query(query)
.bind(statechain_id)
.bind(wallet_name)
.bind(backup_txs_json)
.execute(&mut *transaction)
.await?;
Expand Down
8 changes: 4 additions & 4 deletions clients/libs/rust/src/transfer_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str) -> Result<

let mut coin = coin.unwrap();

let message_result = process_encrypted_message(client_config, &mut coin, enc_message, &wallet.network, &info_config, blockheight, &mut temp_activities).await;
let message_result = process_encrypted_message(client_config, &mut coin, enc_message, &wallet.network, &wallet.name, &info_config, blockheight, &mut temp_activities).await;

if message_result.is_err() {
println!("Error: {}", message_result.err().unwrap().to_string());
Expand Down Expand Up @@ -102,7 +102,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str) -> Result<

let mut new_coin = new_coin.unwrap();

let message_result = process_encrypted_message(client_config, &mut new_coin, enc_message, &wallet.network, &info_config, blockheight, &mut temp_activities).await;
let message_result = process_encrypted_message(client_config, &mut new_coin, enc_message, &wallet.network, &wallet.name, &info_config, blockheight, &mut temp_activities).await;

if message_result.is_err() {
println!("Error: {}", message_result.err().unwrap().to_string());
Expand Down Expand Up @@ -154,7 +154,7 @@ pub struct MessageResult {
pub statechain_id: Option<String>,
}

async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin, enc_message: &str, network: &str, info_config: &InfoConfig, blockheight: u32, activities: &mut Vec<Activity>) -> Result<MessageResult> {
async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin, enc_message: &str, network: &str, wallet_name: &str, info_config: &InfoConfig, blockheight: u32, activities: &mut Vec<Activity>) -> Result<MessageResult> {

let client_auth_key = coin.auth_privkey.clone();
let new_user_pubkey = coin.user_pubkey.clone();
Expand Down Expand Up @@ -278,7 +278,7 @@ async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin

activities.push(activity);

insert_or_update_backup_txs(&client_config.pool, &transfer_msg.statechain_id, &transfer_msg.backup_transactions).await?;
insert_or_update_backup_txs(&client_config.pool, wallet_name, &transfer_msg.statechain_id, &transfer_msg.backup_transactions).await?;

Ok(MessageResult {
is_batch_locked: false,
Expand Down
4 changes: 2 additions & 2 deletions clients/libs/rust/src/transfer_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn execute(
return Err(anyhow!("Invalid address"));
}

let mut backup_transactions = get_backup_txs(&client_config.pool, &statechain_id).await?;
let mut backup_transactions = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?;

if backup_transactions.len() == 0 {
return Err(anyhow!("No backup transaction associated with this statechain ID were found"));
Expand Down Expand Up @@ -129,7 +129,7 @@ pub async fn execute(
return Err(anyhow::anyhow!("Failed to update transfer message".to_string()));
}

update_backup_txs(&client_config.pool, &coin.statechain_id.as_ref().unwrap(), &backup_transactions).await?;
update_backup_txs(&client_config.pool, &wallet.name, &coin.statechain_id.as_ref().unwrap(), &backup_transactions).await?;

let date = Utc::now(); // This will get the current date and time in UTC
let iso_string = date.to_rfc3339(); // Converts the date to an ISO 8601 string
Expand Down
2 changes: 1 addition & 1 deletion clients/libs/rust/src/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain
return Err(anyhow!("Invalid address"));
}

let backup_txs = get_backup_txs(&client_config.pool, &statechain_id).await?;
let backup_txs = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?;

if backup_txs.len() == 0 {
return Err(anyhow!("No backup transaction associated with this statechain ID were found"));
Expand Down

0 comments on commit 7b15c73

Please sign in to comment.