mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Test manually implemented serializations in the Router lib
This commit is contained in:
@@ -207,7 +207,7 @@ impl From<&[(SeraiAddress, U256)]> for OutInstructions {
|
|||||||
/// An action which was executed by the Router.
|
/// An action which was executed by the Router.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
pub enum Executed {
|
pub enum Executed {
|
||||||
/// Set a new key.
|
/// New key was set.
|
||||||
SetKey {
|
SetKey {
|
||||||
/// The nonce this was done with.
|
/// The nonce this was done with.
|
||||||
nonce: u64,
|
nonce: u64,
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ use ethereum_deployer::Deployer;
|
|||||||
|
|
||||||
use crate::{Coin, OutInstructions, Router};
|
use crate::{Coin, OutInstructions, Router};
|
||||||
|
|
||||||
|
mod read_write;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn execute_reentrancy_guard() {
|
fn execute_reentrancy_guard() {
|
||||||
let hash = alloy_core::primitives::keccak256(b"ReentrancyGuard Router.execute");
|
let hash = alloy_core::primitives::keccak256(b"ReentrancyGuard Router.execute");
|
||||||
@@ -88,7 +90,7 @@ async fn setup_test(
|
|||||||
// Publish it
|
// Publish it
|
||||||
let receipt = ethereum_test_primitives::publish_tx(&provider, tx).await;
|
let receipt = ethereum_test_primitives::publish_tx(&provider, tx).await;
|
||||||
assert!(receipt.status());
|
assert!(receipt.status());
|
||||||
assert_eq!(u128::from(Router::DEPLOYMENT_GAS), ((receipt.gas_used + 1000) / 1000) * 1000);
|
assert_eq!(Router::DEPLOYMENT_GAS, ((receipt.gas_used + 1000) / 1000) * 1000);
|
||||||
|
|
||||||
let router = Router::new(provider.clone(), &public_key).await.unwrap().unwrap();
|
let router = Router::new(provider.clone(), &public_key).await.unwrap().unwrap();
|
||||||
|
|
||||||
@@ -126,10 +128,7 @@ async fn confirm_next_serai_key(
|
|||||||
let tx = ethereum_primitives::deterministically_sign(tx);
|
let tx = ethereum_primitives::deterministically_sign(tx);
|
||||||
let receipt = ethereum_test_primitives::publish_tx(provider, tx).await;
|
let receipt = ethereum_test_primitives::publish_tx(provider, tx).await;
|
||||||
assert!(receipt.status());
|
assert!(receipt.status());
|
||||||
assert_eq!(
|
assert_eq!(Router::CONFIRM_NEXT_SERAI_KEY_GAS, ((receipt.gas_used + 1000) / 1000) * 1000);
|
||||||
u128::from(Router::CONFIRM_NEXT_SERAI_KEY_GAS),
|
|
||||||
((receipt.gas_used + 1000) / 1000) * 1000
|
|
||||||
);
|
|
||||||
receipt
|
receipt
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +166,7 @@ async fn test_update_serai_key() {
|
|||||||
let tx = ethereum_primitives::deterministically_sign(tx);
|
let tx = ethereum_primitives::deterministically_sign(tx);
|
||||||
let receipt = ethereum_test_primitives::publish_tx(&provider, tx).await;
|
let receipt = ethereum_test_primitives::publish_tx(&provider, tx).await;
|
||||||
assert!(receipt.status());
|
assert!(receipt.status());
|
||||||
assert_eq!(u128::from(Router::UPDATE_SERAI_KEY_GAS), ((receipt.gas_used + 1000) / 1000) * 1000);
|
assert_eq!(Router::UPDATE_SERAI_KEY_GAS, ((receipt.gas_used + 1000) / 1000) * 1000);
|
||||||
|
|
||||||
assert_eq!(router.key(receipt.block_hash.unwrap().into()).await.unwrap(), Some(key.1));
|
assert_eq!(router.key(receipt.block_hash.unwrap().into()).await.unwrap(), Some(key.1));
|
||||||
assert_eq!(router.next_key(receipt.block_hash.unwrap().into()).await.unwrap(), Some(update_to));
|
assert_eq!(router.next_key(receipt.block_hash.unwrap().into()).await.unwrap(), Some(update_to));
|
||||||
@@ -270,7 +269,7 @@ async fn test_eth_address_out_instruction() {
|
|||||||
let instructions = OutInstructions::from([].as_slice());
|
let instructions = OutInstructions::from([].as_slice());
|
||||||
let receipt = publish_outs(&provider, &router, key, 2, Coin::Ether, fee, instructions).await;
|
let receipt = publish_outs(&provider, &router, key, 2, Coin::Ether, fee, instructions).await;
|
||||||
assert!(receipt.status());
|
assert!(receipt.status());
|
||||||
assert_eq!(u128::from(Router::EXECUTE_BASE_GAS), ((receipt.gas_used + 1000) / 1000) * 1000);
|
assert_eq!(Router::EXECUTE_BASE_GAS, ((receipt.gas_used + 1000) / 1000) * 1000);
|
||||||
|
|
||||||
assert_eq!(router.next_nonce(receipt.block_hash.unwrap().into()).await.unwrap(), 3);
|
assert_eq!(router.next_nonce(receipt.block_hash.unwrap().into()).await.unwrap(), 3);
|
||||||
}
|
}
|
||||||
@@ -310,7 +309,7 @@ async fn escape_hatch(
|
|||||||
let tx = ethereum_primitives::deterministically_sign(tx);
|
let tx = ethereum_primitives::deterministically_sign(tx);
|
||||||
let receipt = ethereum_test_primitives::publish_tx(provider, tx).await;
|
let receipt = ethereum_test_primitives::publish_tx(provider, tx).await;
|
||||||
assert!(receipt.status());
|
assert!(receipt.status());
|
||||||
assert_eq!(u128::from(Router::ESCAPE_HATCH_GAS), ((receipt.gas_used + 1000) / 1000) * 1000);
|
assert_eq!(Router::ESCAPE_HATCH_GAS, ((receipt.gas_used + 1000) / 1000) * 1000);
|
||||||
receipt
|
receipt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
85
processor/ethereum/router/src/tests/read_write.rs
Normal file
85
processor/ethereum/router/src/tests/read_write.rs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
use rand_core::{RngCore, OsRng};
|
||||||
|
|
||||||
|
use alloy_core::primitives::U256;
|
||||||
|
|
||||||
|
use crate::{Coin, InInstruction, Executed};
|
||||||
|
|
||||||
|
fn coins() -> [Coin; 2] {
|
||||||
|
[Coin::Ether, {
|
||||||
|
let mut erc20 = [0; 20];
|
||||||
|
OsRng.fill_bytes(&mut erc20);
|
||||||
|
Coin::Erc20(erc20.into())
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_coin_read_write() {
|
||||||
|
for coin in coins() {
|
||||||
|
let mut res = vec![];
|
||||||
|
coin.write(&mut res).unwrap();
|
||||||
|
assert_eq!(coin, Coin::read(&mut res.as_slice()).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_in_instruction_read_write() {
|
||||||
|
for coin in coins() {
|
||||||
|
let instruction = InInstruction {
|
||||||
|
id: (
|
||||||
|
{
|
||||||
|
let mut tx_id = [0; 32];
|
||||||
|
OsRng.fill_bytes(&mut tx_id);
|
||||||
|
tx_id
|
||||||
|
},
|
||||||
|
OsRng.next_u64(),
|
||||||
|
),
|
||||||
|
from: {
|
||||||
|
let mut from = [0; 20];
|
||||||
|
OsRng.fill_bytes(&mut from);
|
||||||
|
from
|
||||||
|
},
|
||||||
|
coin,
|
||||||
|
amount: U256::from_le_bytes({
|
||||||
|
let mut amount = [0; 32];
|
||||||
|
OsRng.fill_bytes(&mut amount);
|
||||||
|
amount
|
||||||
|
}),
|
||||||
|
data: {
|
||||||
|
let len = usize::try_from(OsRng.next_u64() % 65536).unwrap();
|
||||||
|
let mut data = vec![0; len];
|
||||||
|
OsRng.fill_bytes(&mut data);
|
||||||
|
data
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut buf = vec![];
|
||||||
|
instruction.write(&mut buf).unwrap();
|
||||||
|
assert_eq!(InInstruction::read(&mut buf.as_slice()).unwrap(), instruction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_executed_read_write() {
|
||||||
|
for executed in [
|
||||||
|
Executed::SetKey {
|
||||||
|
nonce: OsRng.next_u64(),
|
||||||
|
key: {
|
||||||
|
let mut key = [0; 32];
|
||||||
|
OsRng.fill_bytes(&mut key);
|
||||||
|
key
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Executed::Batch {
|
||||||
|
nonce: OsRng.next_u64(),
|
||||||
|
message_hash: {
|
||||||
|
let mut message_hash = [0; 32];
|
||||||
|
OsRng.fill_bytes(&mut message_hash);
|
||||||
|
message_hash
|
||||||
|
},
|
||||||
|
},
|
||||||
|
] {
|
||||||
|
let mut res = vec![];
|
||||||
|
executed.write(&mut res).unwrap();
|
||||||
|
assert_eq!(executed, Executed::read(&mut res.as_slice()).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user