Smash serai-client so the processors don't need the entire lib to access their specific code

We prior controlled this with feature flags. It's just better to define their
own crates.
This commit is contained in:
Luke Parker
2025-11-04 19:02:37 -05:00
parent 1b499edfe1
commit 973287d0a1
40 changed files with 232 additions and 79 deletions

View File

@@ -0,0 +1,28 @@
[package]
name = "serai-client-ethereum"
version = "0.1.0"
description = "Ethereum client library for the Serai network"
license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/client/ethereum"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
keywords = ["serai"]
edition = "2021"
rust-version = "1.85"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints]
workspace = true
[dependencies]
std-shims = { path = "../../../common/std-shims", default-features = false, features = ["alloc"] }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
borsh = { version = "1", default-features = false, features = ["derive"] }
serai-primitives = { path = "../../primitives", default-features = false, version = "0.1" }
[features]
std = ["std-shims/std", "borsh/std", "serai-primitives/std"]

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2025 Luke Parker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,132 @@
#![no_std]
use core::str::FromStr;
use std_shims::{vec::Vec, io::Read};
use borsh::{BorshSerialize, BorshDeserialize};
use serai_primitives::address::ExternalAddress;
/// THe maximum amount of gas an address is allowed to specify as its gas limit.
///
/// Payments to an address with a gas limit which exceed this value will be dropped entirely.
pub const ADDRESS_GAS_LIMIT: u32 = 950_000;
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub struct ContractDeployment {
/// The gas limit to use for this contract's execution.
///
/// This MUST be less than the Serai gas limit. The cost of it, and the associated costs with
/// making this transaction, will be deducted from the amount transferred.
gas_limit: u32,
/// The initialization code of the contract to deploy.
///
/// This contract will be deployed (executing the initialization code). No further calls will
/// be made.
code: Vec<u8>,
}
/// A contract to deploy, enabling executing arbitrary code.
impl ContractDeployment {
pub fn new(gas_limit: u32, code: Vec<u8>) -> Option<Self> {
// Check the gas limit is less the address gas limit
if gas_limit > ADDRESS_GAS_LIMIT {
None?;
}
// The max address length, minus the type byte, minus the size of the gas
const MAX_CODE_LEN: usize =
(ExternalAddress::MAX_LEN as usize) - (1 + core::mem::size_of::<u32>());
if code.len() > MAX_CODE_LEN {
None?;
}
Some(Self { gas_limit, code })
}
pub fn gas_limit(&self) -> u32 {
self.gas_limit
}
pub fn code(&self) -> &[u8] {
&self.code
}
}
/// A representation of an Ethereum address.
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub enum Address {
/// A traditional address.
Address([u8; 20]),
/// A contract to deploy, enabling executing arbitrary code.
Contract(ContractDeployment),
}
impl From<[u8; 20]> for Address {
fn from(address: [u8; 20]) -> Self {
Address::Address(address)
}
}
impl TryFrom<ExternalAddress> for Address {
type Error = ();
fn try_from(data: ExternalAddress) -> Result<Address, ()> {
let mut kind = [0xff];
let mut reader: &[u8] = data.as_ref();
reader.read_exact(&mut kind).map_err(|_| ())?;
Ok(match kind[0] {
0 => {
let mut address = [0xff; 20];
reader.read_exact(&mut address).map_err(|_| ())?;
Address::Address(address)
}
1 => {
let mut gas_limit = [0xff; 4];
reader.read_exact(&mut gas_limit).map_err(|_| ())?;
Address::Contract(ContractDeployment {
gas_limit: {
let gas_limit = u32::from_le_bytes(gas_limit);
if gas_limit > ADDRESS_GAS_LIMIT {
Err(())?;
}
gas_limit
},
// The code is whatever's left since the ExternalAddress is a delimited container of
// appropriately bounded length
code: reader.to_vec(),
})
}
_ => Err(())?,
})
}
}
impl From<Address> for ExternalAddress {
fn from(address: Address) -> ExternalAddress {
let mut res = Vec::with_capacity(1 + 20);
match address {
Address::Address(address) => {
res.push(0);
res.extend(&address);
}
Address::Contract(ContractDeployment { gas_limit, code }) => {
res.push(1);
res.extend(&gas_limit.to_le_bytes());
res.extend(&code);
}
}
// We only construct addresses whose code is small enough this can safely be constructed
ExternalAddress::try_from(res).unwrap()
}
}
impl FromStr for Address {
type Err = ();
fn from_str(str: &str) -> Result<Address, ()> {
let Some(address) = str.strip_prefix("0x") else { Err(())? };
if address.len() != 40 {
Err(())?
};
Ok(Address::Address(
hex::decode(address.to_lowercase()).map_err(|_| ())?.try_into().map_err(|_| ())?,
))
}
}