Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle optional binaries with transaction request #34

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions db/migrations/004_transactions_binaries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE transactions ADD COLUMN binaries BYTEA[]
18 changes: 11 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
value: U256,
gas_limit: U256,
priority: TransactionPriority,
binaries: Option<Vec<Vec<u8>>>,
relayer_id: &str,
) -> eyre::Result<()> {
let mut tx = self.pool.begin().await?;
Expand All @@ -288,8 +289,8 @@

sqlx::query(
r#"
INSERT INTO transactions (id, tx_to, data, value, gas_limit, priority, relayer_id, nonce)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
INSERT INTO transactions (id, tx_to, data, value, gas_limit, priority, relayer_id, nonce, binaries)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
"#,
)
.bind(tx_id)
Expand All @@ -300,7 +301,8 @@
.bind(priority)
.bind(relayer_id)
.bind(nonce)
.bind(binaries.map(|b| b))

Check failure on line 304 in src/db.rs

View workflow job for this annotation

GitHub Actions / cargo test

unnecessary map of the identity function
Copy link
Contributor

@Dzejkop Dzejkop Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As clippy says the .map(|b| b) is a no-op

.execute(tx.as_mut())

Check failure on line 305 in src/db.rs

View workflow job for this annotation

GitHub Actions / cargo test

unnecessary map of the identity function
.await?;

tx.commit().await?;
Expand All @@ -312,7 +314,7 @@
pub async fn get_unsent_txs(&self) -> eyre::Result<Vec<UnsentTx>> {
Ok(sqlx::query_as(
r#"
SELECT r.id as relayer_id, t.id, t.tx_to, t.data, t.value, t.gas_limit, t.priority, t.nonce, r.key_id, r.chain_id
SELECT r.id as relayer_id, t.id, t.tx_to, t.data, t.value, t.gas_limit, t.priority, t.nonce, t.binaries, r.key_id, r.chain_id
FROM transactions t
LEFT JOIN sent_transactions s ON (t.id = s.tx_id)
INNER JOIN relayers r ON (t.relayer_id = r.id)
Expand Down Expand Up @@ -763,7 +765,7 @@
Ok(sqlx::query_as(
r#"
SELECT r.id as relayer_id, t.id, t.tx_to, t.data, t.value, t.gas_limit, t.nonce,
r.key_id, r.chain_id,
t.binaries, r.key_id, r.chain_id,
s.initial_max_fee_per_gas, s.initial_max_priority_fee_per_gas, s.escalation_count
FROM transactions t
JOIN sent_transactions s ON t.id = s.tx_id
Expand Down Expand Up @@ -847,7 +849,7 @@
Ok(sqlx::query_as(
r#"
SELECT t.id as tx_id, t.tx_to as to, t.data, t.value, t.gas_limit, t.nonce,
h.tx_hash, s.status
t.binaries, h.tx_hash, s.status
FROM transactions t
LEFT JOIN sent_transactions s ON t.id = s.tx_id
LEFT JOIN tx_hashes h ON s.valid_tx_hash = h.tx_hash
Expand All @@ -873,7 +875,7 @@
Ok(sqlx::query_as(
r#"
SELECT t.id as tx_id, t.tx_to as to, t.data, t.value, t.gas_limit, t.nonce,
h.tx_hash, s.status
t. binaries, h.tx_hash, s.status
FROM transactions t
LEFT JOIN sent_transactions s ON t.id = s.tx_id
LEFT JOIN tx_hashes h ON s.valid_tx_hash = h.tx_hash
Expand Down Expand Up @@ -1405,12 +1407,13 @@
let value = U256::from(0);
let gas_limit = U256::from(0);
let priority = TransactionPriority::Regular;
let binaries = None;

let tx = db.read_tx(tx_id).await?;
assert!(tx.is_none(), "Tx has not been sent yet");

db.create_transaction(
tx_id, to, data, value, gas_limit, priority, relayer_id,
tx_id, to, data, value, gas_limit, priority, binaries, relayer_id,
)
.await?;

Expand All @@ -1423,6 +1426,7 @@
assert_eq!(tx.gas_limit.0, gas_limit);
assert_eq!(tx.nonce, 0);
assert_eq!(tx.tx_hash, None);
assert_eq!(tx.binaries, None);

let unsent_txs = db.read_txs(relayer_id, None).await?;
assert_eq!(unsent_txs.len(), 1, "1 unsent tx");
Expand Down
3 changes: 3 additions & 0 deletions src/db/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct UnsentTx {
pub priority: TransactionPriority,
#[sqlx(try_from = "i64")]
pub nonce: u64,
pub binaries: Option<Vec<Vec<u8>>>,
pub key_id: String,
#[sqlx(try_from = "i64")]
pub chain_id: u64,
Expand All @@ -34,6 +35,7 @@ pub struct TxForEscalation {
pub gas_limit: U256Wrapper,
#[sqlx(try_from = "i64")]
pub nonce: u64,
pub binaries: Option<Vec<Vec<u8>>>,
pub key_id: String,
#[sqlx(try_from = "i64")]
pub chain_id: u64,
Expand All @@ -52,6 +54,7 @@ pub struct ReadTxData {
pub gas_limit: U256Wrapper,
#[sqlx(try_from = "i64")]
pub nonce: u64,
pub binaries: Option<Vec<Vec<u8>>>,

// Sent tx data
pub tx_hash: Option<H256Wrapper>,
Expand Down
1 change: 1 addition & 0 deletions src/serde_utils.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod base64_binary;
pub mod decimal_u256;
47 changes: 47 additions & 0 deletions src/serde_utils/base64_binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use base64::engine::general_purpose;
use base64::Engine as _;
use serde::Deserialize;

pub fn serialize<S>(
binaries: &Option<Vec<Vec<u8>>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match binaries {
Some(binaries) => {
let base64_vec: Vec<String> = binaries
.iter()
.map(|binary| general_purpose::STANDARD.encode(binary))
.collect();

serializer.serialize_some(&base64_vec)
}
None => serializer.serialize_none(),
}
}

pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<Option<Vec<Vec<u8>>>, D::Error>
where
D: serde::Deserializer<'de>,
{
let base64_strings: Option<Vec<String>> =
Option::deserialize(deserializer)?;

match base64_strings {
Some(base64_vec) => {
let decoded_vec: Vec<Vec<u8>> = base64_vec
.into_iter()
.map(|base64_str| {
general_purpose::STANDARD.decode(base64_str).unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use custom for proper error handling

})
.collect();

Ok(Some(decoded_vec))
}
None => Ok(None),
}
}
3 changes: 3 additions & 0 deletions src/server/routes/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct SendTxRequest {
pub priority: TransactionPriority,
#[serde(default)]
pub tx_id: Option<String>,
#[serde(default, with = "crate::serde_utils::base64_binary")]
pub binaries: Option<Vec<Vec<u8>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -116,6 +118,7 @@ pub async fn send_tx(
req.value,
req.gas_limit,
req.priority,
req.binaries,
api_token.relayer_id(),
)
.await?;
Expand Down
3 changes: 3 additions & 0 deletions tests/send_too_many_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async fn send_too_many_txs() -> eyre::Result<()> {
gas_limit: U256::from(21_000),
priority: TransactionPriority::Regular,
tx_id: None,
binaries: None,
},
)
.await?;
Expand All @@ -75,6 +76,7 @@ async fn send_too_many_txs() -> eyre::Result<()> {
gas_limit: U256::from(21_000),
priority: TransactionPriority::Regular,
tx_id: None,
binaries: None,
},
)
.await;
Expand Down Expand Up @@ -102,6 +104,7 @@ async fn send_too_many_txs() -> eyre::Result<()> {
gas_limit: U256::from(21_000),
priority: TransactionPriority::Regular,
tx_id: None,
binaries: None,
},
)
.await?;
Expand Down
Loading