From d729d6a8dd482fe39f7d8c3e9c7def704fcd4401 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Mon, 13 Nov 2023 17:49:27 +0100 Subject: [PATCH] refactor: move receipt related fixtures into test_vectors --- common/src/tap_manager.rs | 59 +++++--------------------------------- common/src/test_vectors.rs | 46 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/common/src/tap_manager.rs b/common/src/tap_manager.rs index ba4a3e187..42d4d848d 100644 --- a/common/src/tap_manager.rs +++ b/common/src/tap_manager.rs @@ -109,63 +109,15 @@ mod test { use crate::prelude::{AllocationStatus, SubgraphDeployment}; use alloy_primitives::Address; - use alloy_sol_types::{eip712_domain, Eip712Domain}; - use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; use keccak_hash::H256; use sqlx::postgres::PgListener; - use tap_core::tap_manager::SignedReceipt; - use tap_core::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt}; use toolshed::thegraph::DeploymentId; - use crate::test_vectors; + use crate::test_vectors::{self, create_signed_receipt}; use super::*; - /// Fixture to generate a wallet and address - pub fn keys() -> (LocalWallet, Address) { - let wallet: LocalWallet = MnemonicBuilder::::default() - .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") - .build() - .unwrap(); - let address = wallet.address(); - - (wallet, Address::from_slice(address.as_bytes())) - } - - pub fn domain() -> Eip712Domain { - eip712_domain! { - name: "TAP", - version: "1", - chain_id: 1, - verifying_contract: Address::from([0x11u8; 20]), - } - } - - /// Fixture to generate a signed receipt using the wallet from `keys()` - /// and the given `query_id` and `value` - pub async fn create_signed_receipt( - allocation_id: Address, - nonce: u64, - timestamp_ns: u64, - value: u128, - ) -> SignedReceipt { - let (wallet, _) = keys(); - - EIP712SignedMessage::new( - &domain(), - Receipt { - allocation_id, - nonce, - timestamp_ns, - value, - }, - &wallet, - ) - .await - .unwrap() - } - #[ignore] #[sqlx::test] async fn test_verify_and_store_receipt(pgpool: PgPool) { @@ -178,7 +130,6 @@ mod test { let allocation_id = Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(); - let domain = domain(); let signed_receipt = create_signed_receipt(allocation_id, u64::MAX, u64::MAX, u128::MAX).await; @@ -211,8 +162,12 @@ mod test { U256::from(123), )])); - let tap_manager = - TapManager::new(pgpool.clone(), indexer_allocations, escrow_accounts, domain); + let tap_manager = TapManager::new( + pgpool.clone(), + indexer_allocations, + escrow_accounts, + test_vectors::TAP_EIP712_DOMAIN.to_owned(), + ); tap_manager .verify_and_store_receipt(signed_receipt.clone()) diff --git a/common/src/test_vectors.rs b/common/src/test_vectors.rs index 198906806..75d1fdab4 100644 --- a/common/src/test_vectors.rs +++ b/common/src/test_vectors.rs @@ -4,8 +4,13 @@ use std::{collections::HashMap, str::FromStr}; use alloy_primitives::Address; +use alloy_sol_types::{eip712_domain, Eip712Domain}; +use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; use ethers_core::types::U256; use lazy_static::lazy_static; +use tap_core::{ + eip_712_signed_message::EIP712SignedMessage, tap_manager::SignedReceipt, tap_receipt::Receipt, +}; use toolshed::thegraph::DeploymentId; use crate::prelude::{Allocation, AllocationStatus, SubgraphDeployment}; @@ -239,4 +244,45 @@ lazy_static! { (Address::from_str("0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1").unwrap(), U256::from(24)), (Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(), U256::from(42)), ]); + + /// Fixture to generate a wallet and address + pub static ref TAP_SENDER: (LocalWallet, Address) = { + let wallet: LocalWallet = MnemonicBuilder::::default() + .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") + .build() + .unwrap(); + let address = wallet.address(); + + (wallet, Address::from_slice(address.as_bytes())) + }; + + pub static ref TAP_EIP712_DOMAIN: Eip712Domain = eip712_domain! { + name: "TAP", + version: "1", + chain_id: 1, + verifying_contract: Address::from([0x11u8; 20]), + }; +} + +/// Function to generate a signed receipt using the TAP_SENDER wallet. +pub async fn create_signed_receipt( + allocation_id: Address, + nonce: u64, + timestamp_ns: u64, + value: u128, +) -> SignedReceipt { + let (wallet, _) = &*self::TAP_SENDER; + + EIP712SignedMessage::new( + &self::TAP_EIP712_DOMAIN, + Receipt { + allocation_id, + nonce, + timestamp_ns, + value, + }, + wallet, + ) + .await + .unwrap() }