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

fix: TAP addresses formatting in DB #116

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 6 additions & 10 deletions common/src/tap_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023-, GraphOps and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use alloy_primitives::hex::ToHex;
use alloy_primitives::Address;
use alloy_sol_types::Eip712Domain;
use anyhow::anyhow;
Expand Down Expand Up @@ -133,13 +134,8 @@ impl TapManager {
INSERT INTO scalar_tap_receipts (allocation_id, signer_address, timestamp_ns, value, receipt)
VALUES ($1, $2, $3, $4, $5)
"#,
format!("{:?}", allocation_id)
.trim_start_matches("0x")
.to_owned(),
receipt_signer
.to_string()
.trim_start_matches("0x")
.to_owned(),
allocation_id.encode_hex::<String>(),
receipt_signer.encode_hex::<String>(),
BigDecimal::from(receipt.message.timestamp_ns),
BigDecimal::from_str(&receipt.message.value.to_string())?,
serde_json::to_value(receipt).map_err(|e| anyhow!(e))?
Expand Down Expand Up @@ -351,7 +347,7 @@ mod test {
INSERT INTO scalar_tap_denylist (sender_address)
VALUES ($1)
"#,
TAP_SENDER.1.to_string().trim_start_matches("0x").to_owned()
TAP_SENDER.1.encode_hex::<String>()
)
.execute(&pgpool)
.await
Expand Down Expand Up @@ -390,7 +386,7 @@ mod test {
INSERT INTO scalar_tap_denylist (sender_address)
VALUES ($1)
"#,
TAP_SENDER.1.to_string().trim_start_matches("0x").to_owned()
TAP_SENDER.1.encode_hex::<String>()
)
.execute(&pgpool)
.await
Expand All @@ -409,7 +405,7 @@ mod test {
DELETE FROM scalar_tap_denylist
WHERE sender_address = $1
"#,
TAP_SENDER.1.to_string().trim_start_matches("0x").to_owned()
TAP_SENDER.1.encode_hex::<String>()
)
.execute(&pgpool)
.await
Expand Down
4 changes: 2 additions & 2 deletions tap-agent/src/tap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-, GraphOps and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use alloy_primitives::Address;
use alloy_primitives::{hex::ToHex, Address};
use anyhow::anyhow;
use eventuals::Eventual;
use indexer_common::escrow_accounts::EscrowAccounts;
Expand All @@ -26,7 +26,7 @@ async fn signers_trimmed(
.map_err(|e| anyhow!("Error while getting escrow accounts: {:?}", e))?
.get_signers_for_sender(&sender)?
.iter()
.map(|s| s.to_string().trim_start_matches("0x").to_owned())
.map(|s| s.encode_hex::<String>())
.collect::<Vec<String>>();

Ok(signers)
Expand Down
15 changes: 5 additions & 10 deletions tap-agent/src/tap/rav_storage_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023-, GraphOps and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use alloy_primitives::hex::ToHex;
use alloy_primitives::Address;
use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -34,11 +35,8 @@ impl RAVStorageAdapterTrait for RAVStorageAdapter {
ON CONFLICT (allocation_id, sender_address)
DO UPDATE SET rav = $3
"#,
self.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
self.sender.to_string().trim_start_matches("0x").to_owned(),
self.allocation_id.encode_hex::<String>(),
self.sender.encode_hex::<String>(),
serde_json::to_value(rav).map_err(|e| AdapterError::AdapterError {
error: e.to_string()
})?
Expand All @@ -58,11 +56,8 @@ impl RAVStorageAdapterTrait for RAVStorageAdapter {
FROM scalar_tap_ravs
WHERE allocation_id = $1 AND sender_address = $2
"#,
self.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
self.sender.to_string().trim_start_matches("0x").to_owned()
self.allocation_id.encode_hex::<String>(),
self.sender.encode_hex::<String>()
)
.fetch_optional(&self.pgpool)
.await
Expand Down
12 changes: 3 additions & 9 deletions tap-agent/src/tap/receipt_storage_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
ops::{Bound, RangeBounds},
};

use alloy_primitives::Address;
use alloy_primitives::{hex::ToHex, Address};
use async_trait::async_trait;
use eventuals::Eventual;
use indexer_common::escrow_accounts::EscrowAccounts;
Expand Down Expand Up @@ -116,10 +116,7 @@ impl ReceiptStorageAdapterTrait for ReceiptStorageAdapter {
WHERE allocation_id = $1 AND signer_address IN (SELECT unnest($2::text[]))
AND $3::numrange @> timestamp_ns
"#,
self.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
self.allocation_id.encode_hex::<String>(),
&signers,
rangebounds_to_pgrange(timestamp_range_ns)
)
Expand Down Expand Up @@ -162,10 +159,7 @@ impl ReceiptStorageAdapterTrait for ReceiptStorageAdapter {
WHERE allocation_id = $1 AND signer_address IN (SELECT unnest($2::text[]))
AND $3::numrange @> timestamp_ns
"#,
self.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
self.allocation_id.encode_hex::<String>(),
&signers,
rangebounds_to_pgrange(timestamp_ns)
)
Expand Down
48 changes: 13 additions & 35 deletions tap-agent/src/tap/sender_allocation_relationship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::{str::FromStr, sync::Arc, time::Duration};

use alloy_primitives::Address;
use alloy_primitives::{hex::ToHex, Address};
use alloy_sol_types::Eip712Domain;
use anyhow::{anyhow, ensure, Result};

Expand Down Expand Up @@ -249,12 +249,8 @@ impl SenderAllocationRelationship {
rav
) ELSE TRUE END
"#,
inner
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
inner.sender.to_string().trim_start_matches("0x").to_owned(),
inner.allocation_id.encode_hex::<String>(),
inner.sender.encode_hex::<String>(),
&signers
)
.fetch_one(&inner.pgpool)
Expand Down Expand Up @@ -415,12 +411,8 @@ impl SenderAllocationRelationship {
WHERE allocation_id = $1 AND sender_address = $2
RETURNING *
"#,
inner
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
inner.sender.to_string().trim_start_matches("0x").to_owned(),
inner.allocation_id.encode_hex::<String>(),
inner.sender.encode_hex::<String>(),
)
.fetch_all(&inner.pgpool)
.await?;
Expand Down Expand Up @@ -451,12 +443,8 @@ impl SenderAllocationRelationship {
)
VALUES ($1, $2, $3, $4, $5)
"#,
inner
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
inner.sender.to_string().trim_start_matches("0x").to_owned(),
inner.allocation_id.encode_hex::<String>(),
inner.sender.encode_hex::<String>(),
BigDecimal::from(received_receipt.signed_receipt().message.timestamp_ns),
BigDecimal::from_str(&received_receipt.signed_receipt().message.value.to_string())?,
serde_json::to_value(received_receipt)?
Expand Down Expand Up @@ -486,12 +474,8 @@ impl SenderAllocationRelationship {
)
VALUES ($1, $2, $3, $4, $5)
"#,
inner
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
inner.sender.to_string().trim_start_matches("0x").to_owned(),
inner.allocation_id.encode_hex::<String>(),
inner.sender.encode_hex::<String>(),
serde_json::to_value(expected_rav)?,
serde_json::to_value(rav)?,
reason
Expand Down Expand Up @@ -922,11 +906,8 @@ mod tests {
FROM scalar_tap_ravs
WHERE allocation_id = $1 AND sender_address = $2
"#,
ALLOCATION_ID
.to_string()
.trim_start_matches("0x")
.to_owned(),
SENDER.1.to_string().trim_start_matches("0x").to_owned()
ALLOCATION_ID.encode_hex::<String>(),
SENDER.1.encode_hex::<String>()
)
.fetch_optional(&pgpool)
.await
Expand Down Expand Up @@ -1008,11 +989,8 @@ mod tests {
FROM scalar_tap_ravs
WHERE allocation_id = $1 AND sender_address = $2
"#,
ALLOCATION_ID
.to_string()
.trim_start_matches("0x")
.to_owned(),
SENDER.1.to_string().trim_start_matches("0x").to_owned()
ALLOCATION_ID.encode_hex::<String>(),
SENDER.1.encode_hex::<String>()
)
.fetch_optional(&pgpool)
.await
Expand Down
21 changes: 5 additions & 16 deletions tap-agent/src/tap/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::str::FromStr;

use alloy_primitives::hex::ToHex;
use alloy_primitives::Address;
use alloy_sol_types::{eip712_domain, Eip712Domain};
use anyhow::Result;
Expand Down Expand Up @@ -98,18 +99,11 @@ pub async fn store_receipt(pgpool: &PgPool, signed_receipt: SignedReceipt) -> Re
VALUES ($1, $2, $3, $4, $5)
RETURNING id
"#,
signed_receipt
.message
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
signed_receipt.message.allocation_id.encode_hex::<String>(),
signed_receipt
.recover_signer(&TAP_EIP712_DOMAIN_SEPARATOR)
.unwrap()
.to_string()
.trim_start_matches("0x")
.to_owned(),
.encode_hex::<String>(),
BigDecimal::from(signed_receipt.message.timestamp_ns),
BigDecimal::from_str(&signed_receipt.message.value.to_string())?,
serde_json::to_value(signed_receipt)?
Expand All @@ -130,13 +124,8 @@ pub async fn store_rav(pgpool: &PgPool, signed_rav: SignedRAV, sender: Address)
)
VALUES ($1, $2, $3)
"#,
signed_rav
.message
.allocation_id
.to_string()
.trim_start_matches("0x")
.to_owned(),
sender.to_string().trim_start_matches("0x").to_owned(),
signed_rav.message.allocation_id.encode_hex::<String>(),
sender.encode_hex::<String>(),
serde_json::to_value(signed_rav).unwrap(),
)
.execute(pgpool)
Expand Down
Loading