Skip to content

Commit

Permalink
dev: cleaning (#1339)
Browse files Browse the repository at this point in the history
* prefer macro over `from_str`

* add macro + fix test
  • Loading branch information
greged93 authored Aug 21, 2024
1 parent d539188 commit fddab3b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test: katana-genesis load-env

# Example: `make test-target TARGET=test_raw_transaction`
test-target: load-env
cargo test --tests --features testing $(TARGET) -- --nocapture
cargo test --tests --all-features $(TARGET) -- --nocapture

benchmark:
cd benchmarks && bun i && bun run benchmark
Expand Down
1 change: 0 additions & 1 deletion scripts/upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ if [ "${ENV}" = "staging" ]; then
export KAKAROT_STAGING_RPC_URL="https://juno-kakarot-testnet-stage.karnot.xyz"
export KAKAROT_STAGING_ACCOUNT_ADDRESS="0x7ecf6cd45c32ce84812e660cc176cb8b4de2e7a6d5916fe326bf871466fbe02"
export WEB3_HTTP_PROVIDER_URI="https://kkrt-rpc-kakarot-testnet-stage.karnot.xyz"
export KAKAROT_STAGING_ACCOUNT_ADDRESS="0x7ecf6cd45c32ce84812e660cc176cb8b4de2e7a6d5916fe326bf871466fbe02"
if [ -z "${KAKAROT_STAGING_PRIVATE_KEY}" ]; then
echo "Please provide the KAKAROT_STAGING_PRIVATE_KEY environment variable. The private key should be loaded using gpg: gpg -r [email protected] --decrypt path/to/encrypted/key.gpg"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions src/providers/eth_provider/database/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,16 @@ where
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::b256;
use reth_rpc_types::FilterSet;
use std::str::FromStr;

#[test]
fn test_into_filter_with_padding() {
assert_eq!(into_filter::<u64>("test_key", &0x1234, 10), doc! {"test_key": "0x0000001234"});
assert_eq!(
into_filter::<B256>(
"test_key",
&B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(),
&b256!("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"),
64
),
doc! {"test_key": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}
Expand Down
25 changes: 8 additions & 17 deletions src/providers/eth_provider/starknet/kakarot_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,20 @@ pub fn to_starknet_transaction(
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{address, bytes};
use alloy_rlp::{Decodable, Encodable};
use reth_primitives::{hex, transaction::TxEip2930, Bytes, Signature, Transaction, TxKind, U256};
use reth_primitives::{hex, transaction::TxEip2930, Signature, Transaction, TxKind, U256};
use starknet::macros::felt;

#[test]
fn test_to_starknet_transaction() {
// Define a sample signed transaction.
// Using https://sepolia.kakarotscan.org/tx/0x5be347c9eb86cf04b884c7e6f432c6daa2054b46c3c70c7d4536e4c009765abe
let transaction = TransactionSigned::from_transaction_and_signature(Transaction::Eip2930(TxEip2930 { chain_id: 1_802_203_764, nonce: 33, gas_price: 0, gas_limit: 302_606, to: TxKind::Create, value: U256::ZERO, access_list: Default::default(), input: Bytes::from_str("0x608060405260405161040a38038061040a83398101604081905261002291610268565b61002c8282610033565b5050610352565b61003c82610092565b6040516001600160a01b038316907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a280511561008657610081828261010e565b505050565b61008e610185565b5050565b806001600160a01b03163b6000036100cd57604051634c9c8ce360e01b81526001600160a01b03821660048201526024015b60405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc80546001600160a01b0319166001600160a01b0392909216919091179055565b6060600080846001600160a01b03168460405161012b9190610336565b600060405180830381855af49150503d8060008114610166576040519150601f19603f3d011682016040523d82523d6000602084013e61016b565b606091505b50909250905061017c8583836101a6565b95945050505050565b34156101a45760405163b398979f60e01b815260040160405180910390fd5b565b6060826101bb576101b682610205565b6101fe565b81511580156101d257506001600160a01b0384163b155b156101fb57604051639996b31560e01b81526001600160a01b03851660048201526024016100c4565b50805b9392505050565b8051156102155780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b634e487b7160e01b600052604160045260246000fd5b60005b8381101561025f578181015183820152602001610247565b50506000910152565b6000806040838503121561027b57600080fd5b82516001600160a01b038116811461029257600080fd5b60208401519092506001600160401b03808211156102af57600080fd5b818501915085601f8301126102c357600080fd5b8151818111156102d5576102d561022e565b604051601f8201601f19908116603f011681019083821181831017156102fd576102fd61022e565b8160405282815288602084870101111561031657600080fd5b610327836020830160208801610244565b80955050505050509250929050565b60008251610348818460208701610244565b9190910192915050565b60aa806103606000396000f3fe6080604052600a600c565b005b60186014601a565b6051565b565b6000604c7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b905090565b3660008037600080366000845af43d6000803e808015606f573d6000f35b3d6000fdfea2646970667358221220d0232cfa81216c3e4973e570f043b57ccb69ae4a81b8bc064338713721c87a9f64736f6c6343000814003300000000000000000000000009635f643e140090a9a8dcd712ed6285858cebef000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000647a1ac61e00000000000000000000000084ea74d481ee0a5332c457a4d796187f6ba67feb00000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000").unwrap() }), Signature { r: U256::from_str("0x6290c177b6ee7b16d87909474a792d9ac022385505161e91191c57d666b61496").unwrap(), s: U256::from_str("0x7ba95168843acb8b888de596c28033c6c66a9cb6c7621cfc996bc5851115634d").unwrap(), odd_y_parity: true });
let transaction = TransactionSigned::from_transaction_and_signature(Transaction::Eip2930(TxEip2930 { chain_id: 1_802_203_764, nonce: 33, gas_price: 0, gas_limit: 302_606, to: TxKind::Create, value: U256::ZERO, access_list: Default::default(), input: bytes!("608060405260405161040a38038061040a83398101604081905261002291610268565b61002c8282610033565b5050610352565b61003c82610092565b6040516001600160a01b038316907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a280511561008657610081828261010e565b505050565b61008e610185565b5050565b806001600160a01b03163b6000036100cd57604051634c9c8ce360e01b81526001600160a01b03821660048201526024015b60405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc80546001600160a01b0319166001600160a01b0392909216919091179055565b6060600080846001600160a01b03168460405161012b9190610336565b600060405180830381855af49150503d8060008114610166576040519150601f19603f3d011682016040523d82523d6000602084013e61016b565b606091505b50909250905061017c8583836101a6565b95945050505050565b34156101a45760405163b398979f60e01b815260040160405180910390fd5b565b6060826101bb576101b682610205565b6101fe565b81511580156101d257506001600160a01b0384163b155b156101fb57604051639996b31560e01b81526001600160a01b03851660048201526024016100c4565b50805b9392505050565b8051156102155780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b634e487b7160e01b600052604160045260246000fd5b60005b8381101561025f578181015183820152602001610247565b50506000910152565b6000806040838503121561027b57600080fd5b82516001600160a01b038116811461029257600080fd5b60208401519092506001600160401b03808211156102af57600080fd5b818501915085601f8301126102c357600080fd5b8151818111156102d5576102d561022e565b604051601f8201601f19908116603f011681019083821181831017156102fd576102fd61022e565b8160405282815288602084870101111561031657600080fd5b610327836020830160208801610244565b80955050505050509250929050565b60008251610348818460208701610244565b9190910192915050565b60aa806103606000396000f3fe6080604052600a600c565b005b60186014601a565b6051565b565b6000604c7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b905090565b3660008037600080366000845af43d6000803e808015606f573d6000f35b3d6000fdfea2646970667358221220d0232cfa81216c3e4973e570f043b57ccb69ae4a81b8bc064338713721c87a9f64736f6c6343000814003300000000000000000000000009635f643e140090a9a8dcd712ed6285858cebef000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000647a1ac61e00000000000000000000000084ea74d481ee0a5332c457a4d796187f6ba67feb00000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000") }), Signature { r: U256::from_str("0x6290c177b6ee7b16d87909474a792d9ac022385505161e91191c57d666b61496").unwrap(), s: U256::from_str("0x7ba95168843acb8b888de596c28033c6c66a9cb6c7621cfc996bc5851115634d").unwrap(), odd_y_parity: true });

// Invoke the function to convert the transaction to Starknet format.
if let BroadcastedInvokeTransaction::V1(tx) = to_starknet_transaction(
&transaction,
Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap(),
0,
)
.unwrap()
if let BroadcastedInvokeTransaction::V1(tx) =
to_starknet_transaction(&transaction, address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), 0).unwrap()
{
// Transaction signature assertion.
assert_eq!(
Expand Down Expand Up @@ -172,16 +170,9 @@ mod tests {
assert_eq!(
tx.sender_address,
get_contract_address(
Felt::from(Felt252Wrapper::from(
Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap()
)),
felt!("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),
*UNINITIALIZED_ACCOUNT_CLASS_HASH,
&[
*KAKAROT_ADDRESS,
Felt::from(Felt252Wrapper::from(
Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap()
)),
],
&[*KAKAROT_ADDRESS, felt!("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),],
Felt::ZERO,
)
);
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/alchemy_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(clippy::used_underscore_binding)]
#![cfg(feature = "testing")]
use std::str::FromStr;

use alloy_dyn_abi::DynSolValue;
use alloy_primitives::address;
use kakarot_rpc::{
models::{
felt::Felt252Wrapper,
Expand Down Expand Up @@ -149,7 +149,7 @@ async fn test_token_allowance(#[future] erc20: (Katana, KakarotEvmContract), _se
Felt252Wrapper::from(erc20.evm_address).try_into().expect("Failed to convert EVM address");

// Set the spender address for testing allowance
let spender_address = Address::from_str("0x1234567890123456789012345678901234567890").expect("Invalid address");
let spender_address = address!("1234567890123456789012345678901234567890");

// Start the Kakarot RPC server
let (server_addr, server_handle) =
Expand Down
55 changes: 33 additions & 22 deletions tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::used_underscore_binding)]
#![cfg(feature = "testing")]
use alloy_primitives::{address, bytes};
use alloy_sol_types::{sol, SolCall};
use kakarot_rpc::{
models::felt::Felt252Wrapper,
Expand Down Expand Up @@ -27,7 +28,7 @@ use reth_rpc_types::{
};
use rstest::*;
use starknet::core::types::{BlockTag, Felt};
use std::{collections::HashMap, str::FromStr, sync::Arc};
use std::{collections::HashMap, sync::Arc};

#[rstest]
#[awt]
Expand Down Expand Up @@ -514,7 +515,7 @@ async fn test_estimate_gas(#[future] counter: (Katana, KakarotEvmContract), _set
let request = TransactionRequest {
from: Some(eoa.evm_address().unwrap()),
to: Some(TxKind::Call(counter_address.try_into().unwrap())),
input: TransactionInput { input: None, data: Some(Bytes::from_str("0x371303c0").unwrap()) }, // selector of "function inc()"
input: TransactionInput { input: None, data: Some(bytes!("371303c0")) }, // selector of "function inc()"
chain_id: Some(chain_id.to::<u64>()),
..Default::default()
};
Expand Down Expand Up @@ -563,45 +564,58 @@ async fn test_fee_history(#[future] katana: Katana, _setup: ()) {
#[tokio::test(flavor = "multi_thread")]
#[cfg(feature = "hive")]
async fn test_predeploy_eoa(#[future] katana: Katana, _setup: ()) {
use alloy_primitives::b256;
use futures::future::join_all;
use kakarot_rpc::{providers::eth_provider::constant::CHAIN_ID, test_utils::eoa::KakarotEOA};
use reth_primitives::B256;
use starknet::providers::Provider;

// Given
let one_ether = 1_000_000_000_000_000_000u128;
let one_tenth_ether = one_ether / 10;

let eoa = katana.eoa();
let eth_provider = katana.eth_provider();
let starknet_provider = eth_provider.starknet_provider();
let other_eoa_1 = KakarotEOA::new(B256::from_str(&format!("0x{:0>64}", "0abde1")).unwrap(), eth_provider.clone());
let other_eoa_2 = KakarotEOA::new(B256::from_str(&format!("0x{:0>64}", "0abde2")).unwrap(), eth_provider.clone());
let other_eoa_1 = KakarotEOA::new(
b256!("00000000000000012330000000000000000000000000000000000000000abde1"),
eth_provider.clone(),
);
let other_eoa_2 = KakarotEOA::new(
b256!("00000000000000123123456000000000000000000000000000000000000abde2"),
eth_provider.clone(),
);
let chain_id = starknet_provider.chain_id().await.unwrap();
CHAIN_ID.set(chain_id).expect("Failed to set chain id");

let evm_address = eoa.evm_address().unwrap();
let balance_before = eth_provider.balance(eoa.evm_address().unwrap(), None).await.unwrap();
eoa.transfer(other_eoa_1.evm_address().unwrap(), 1).await.expect("Failed to transfer funds to other eoa 1");
eoa.transfer(other_eoa_1.evm_address().unwrap(), 2 * one_ether)
.await
.expect("Failed to transfer funds to other eoa 1");
// Sleep for 2 seconds to let the transaction pass
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
eoa.transfer(other_eoa_2.evm_address().unwrap(), 2).await.expect("Failed to transfer funds to other eoa 2");
eoa.transfer(other_eoa_2.evm_address().unwrap(), one_ether).await.expect("Failed to transfer funds to other eoa 2");
// Sleep for 2 seconds to let the transaction pass
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

// When
let t1 = tokio::task::spawn(async move {
other_eoa_1.transfer(evm_address, 1).await.expect("Failed to transfer funds back to eoa");
let jh1 = tokio::task::spawn(async move {
let _ = other_eoa_1
.transfer(evm_address, 17 * one_tenth_ether)
.await
.expect("Failed to transfer funds back to eoa");
});
let t2 = tokio::task::spawn(async move {
other_eoa_2.transfer(evm_address, 1).await.expect("Failed to transfer funds back to eoa");
let jh2 = tokio::task::spawn(async move {
let _ =
other_eoa_2.transfer(evm_address, 3 * one_tenth_ether).await.expect("Failed to transfer funds back to eoa");
});
join_all([t1, t2]).await;
join_all([jh1, jh2]).await;

// Then
// Await all transactions to pass
while starknet_provider.block_number().await.unwrap() < 6 {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
let balance_after = eth_provider.balance(evm_address, None).await.unwrap();
assert_eq!(balance_after, balance_before - U256::from(1));
assert_eq!(balance_after, balance_before - U256::from(one_ether));
}

#[rstest]
Expand Down Expand Up @@ -739,7 +753,7 @@ async fn test_send_raw_transaction_pre_eip_155(#[future] katana: Katana, _setup:

// Use the transaction for the Arachnid deployer
// https://github.com/Arachnid/deterministic-deployment-proxy
let transaction = Transaction::Legacy(TxLegacy{value:U256::ZERO, chain_id: None, nonce, gas_price: 100_000_000_000, gas_limit: 100_000, to: TxKind::Create, input: Bytes::from_str("604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3").unwrap()});
let transaction = Transaction::Legacy(TxLegacy{value:U256::ZERO, chain_id: None, nonce, gas_price: 100_000_000_000, gas_limit: 100_000, to: TxKind::Create, input: bytes!("604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3") });

// Sign the transaction
let signature = sign_message(katana.eoa().private_key(), transaction.signature_hash()).unwrap();
Expand All @@ -766,10 +780,7 @@ async fn test_send_raw_transaction_pre_eip_155(#[future] katana: Katana, _setup:

// Then
// Check that the Arachnid deployer contract was deployed
let code = eth_provider
.get_code(Address::from_str("0x5fbdb2315678afecb367f032d93f642f64180aa3").unwrap(), None)
.await
.unwrap();
let code = eth_provider.get_code(address!("5fbdb2315678afecb367f032d93f642f64180aa3"), None).await.unwrap();
assert!(!code.is_empty());
}

Expand Down Expand Up @@ -841,7 +852,7 @@ async fn test_call_with_state_override_balance_success(#[future] katana: Katana,
let eth_provider = katana.eth_provider();

// Generate an EOA address
let eoa_address = Address::from_str("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5").unwrap();
let eoa_address = address!("95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5");

// Create the second transaction request with a higher value
let request = TransactionRequest {
Expand Down

0 comments on commit fddab3b

Please sign in to comment.