use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use super::{Address, FalconSignature};
pub type Gas = u64;
pub type GasUsed = u64;
pub type FeeQuanta = u128;
pub const MIN_GAS_LIMIT: Gas = 21_000;
pub const MAX_TX_SIZE: usize = 128 * 1024;
pub const MAX_CALLDATA: usize = 64 * 1024;
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
#[borsh(use_discriminant = true)]
#[serde(rename_all = "snake_case")]
#[repr(u8)]
pub enum TxType {
Standard = 0x00,
Deploy = 0x01,
StakeDeposit = 0x03,
StakeWithdraw = 0x04,
Slash = 0x05,
ClaimReward = 0x06,
ClaimAirdrop = 0x07,
SweepAirdrop = 0x08,
MultisigTx = 0x09,
RotateMultisig = 0x0A,
EmergencyPause = 0x0B,
EmergencyResume = 0x0C,
RegisterPubkey = 0x0D,
Unjail = 0x0E,
RotateValidatorKeys = 0x0F,
DisputeSlash = 0x10,
Commit = 0x11,
Reveal = 0x12,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum FeePayer {
Sender = 0x00,
GasTank = 0x01,
Paymaster(Address) = 0x02,
}
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum AccessType {
Read = 0x00,
ReadWrite = 0x01,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct AccessEntry {
pub address: Address,
pub storage_keys: Vec<[u8; 32]>,
pub access_type: AccessType,
}
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct Tx {
pub from: Address,
pub to: Address,
#[serde(deserialize_with = "crate::types::serde_hex::deserialize_u128_hex_or_int")]
pub value: u128,
pub data: Vec<u8>,
#[serde(deserialize_with = "crate::types::serde_hex::deserialize_u64_hex_or_int")]
pub gas_limit: Gas,
#[serde(deserialize_with = "crate::types::serde_hex::deserialize_u64_hex_or_int")]
pub nonce: u64,
pub signature: FalconSignature,
pub fee_payer: FeePayer,
pub access_list: Vec<AccessEntry>,
#[serde(
default,
deserialize_with = "crate::types::serde_hex::deserialize_opt_u64_hex_or_int"
)]
pub deadline: Option<u64>,
#[serde(deserialize_with = "crate::types::serde_hex::deserialize_u64_hex_or_int")]
pub chain_id: u64,
pub tx_type: TxType,
}
impl Tx {
#[allow(clippy::too_many_arguments)]
#[must_use]
pub fn unsigned(
from: Address,
to: Address,
value: u128,
data: Vec<u8>,
gas_limit: Gas,
nonce: u64,
fee_payer: FeePayer,
access_list: Vec<AccessEntry>,
deadline: Option<u64>,
chain_id: u64,
tx_type: TxType,
) -> Self {
Self {
from,
to,
value,
data,
gas_limit,
nonce,
signature: FalconSignature::new(Vec::new()),
fee_payer,
access_list,
deadline,
chain_id,
tx_type,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct CallPayload {
pub function: String,
pub calldata: Vec<u8>,
}
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct DeployData {
pub name: String,
pub wasm_bytes: Vec<u8>,
pub contract_type: super::abi::ContractType,
pub init_calldata: Vec<u8>,
}
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct CommitPayload {
pub commitment: [u8; 32],
pub value_ceiling: u128,
}
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct RevealPayload {
pub commitment: [u8; 32],
pub nonce: [u8; 32],
pub inner_tx: Vec<u8>,
}
impl FeePayer {
#[cfg(test)]
#[allow(dead_code)]
fn discriminant_tag(&self) -> u8 {
match self {
FeePayer::Sender => 0x00,
FeePayer::GasTank => 0x01,
FeePayer::Paymaster(_) => 0x02,
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::types::FalconPubkey;
fn minimal_tx() -> Tx {
Tx::unsigned(
Address::ZERO,
Address::ZERO,
0,
vec![],
21_000,
0,
FeePayer::Sender,
vec![],
None,
1,
TxType::Standard,
)
}
#[test]
fn tx_type_tags_match_spec() {
assert_eq!(TxType::Standard as u8, 0x00);
assert_eq!(TxType::Deploy as u8, 0x01);
assert_eq!(TxType::StakeDeposit as u8, 0x03);
assert_eq!(TxType::RegisterPubkey as u8, 0x0D);
assert_eq!(TxType::DisputeSlash as u8, 0x10);
assert_eq!(TxType::Commit as u8, 0x11);
assert_eq!(TxType::Reveal as u8, 0x12);
}
#[test]
fn commit_reveal_payloads_borsh_round_trip() {
let commit = CommitPayload {
commitment: [0xAB; 32],
value_ceiling: 5_000_000_000,
};
let bytes = borsh::to_vec(&commit).unwrap();
assert_eq!(borsh::from_slice::<CommitPayload>(&bytes).unwrap(), commit);
let reveal = RevealPayload {
commitment: [0xAB; 32],
nonce: [0x5A; 32],
inner_tx: vec![1, 2, 3, 4],
};
let bytes = borsh::to_vec(&reveal).unwrap();
assert_eq!(borsh::from_slice::<RevealPayload>(&bytes).unwrap(), reveal);
}
#[test]
fn fee_payer_tags_match_spec() {
assert_eq!(FeePayer::Sender.discriminant_tag(), 0x00);
assert_eq!(FeePayer::GasTank.discriminant_tag(), 0x01);
let addr = Address::new([0x42; 32]);
assert_eq!(FeePayer::Paymaster(addr).discriminant_tag(), 0x02);
}
#[test]
fn access_type_tags_match_spec() {
assert_eq!(AccessType::Read as u8, 0x00);
assert_eq!(AccessType::ReadWrite as u8, 0x01);
}
#[test]
fn tx_borsh_round_trip() {
let tx = minimal_tx();
let bytes = borsh::to_vec(&tx).unwrap();
let decoded: Tx = borsh::from_slice(&bytes).unwrap();
assert_eq!(tx, decoded);
}
#[test]
fn tx_borsh_handles_full_fields() {
let tx = Tx::unsigned(
Address::new([0x01; 32]),
Address::new([0x02; 32]),
10_000,
vec![1, 2, 3, 4],
100_000,
7,
FeePayer::Paymaster(Address::new([0x03; 32])),
vec![AccessEntry {
address: Address::new([0x04; 32]),
storage_keys: vec![[0u8; 32], [0xFF; 32]],
access_type: AccessType::ReadWrite,
}],
Some(500),
1,
TxType::Standard,
);
let bytes = borsh::to_vec(&tx).unwrap();
let decoded: Tx = borsh::from_slice(&bytes).unwrap();
assert_eq!(tx, decoded);
}
#[test]
fn tx_json_decodes_engine_wire_shape() {
let json = serde_json::json!({
"from": vec![0xAAu8; 32],
"to": vec![0xBBu8; 32],
"value": "0xf4240",
"data": [],
"gas_limit": "0x186a0",
"nonce": "0x4",
"signature": vec![0u8; 666],
"fee_payer": "Sender",
"access_list": [],
"deadline": serde_json::Value::Null,
"chain_id": "0x7a69",
"tx_type": "standard",
});
let decoded: Tx = serde_json::from_value(json).expect("engine wire shape decodes");
assert_eq!(decoded.value, 0xf_4240);
assert_eq!(decoded.gas_limit, 0x1_86a0);
assert_eq!(decoded.nonce, 4);
assert_eq!(decoded.chain_id, 0x7a69);
assert_eq!(decoded.tx_type, TxType::Standard);
assert_eq!(decoded.deadline, None);
assert_eq!(decoded.fee_payer, FeePayer::Sender);
}
#[test]
fn deploy_tx_uses_zero_recipient() {
let pk = FalconPubkey::new([0x55; crate::types::FALCON_PUBKEY_LEN]);
let from = Address::from_pubkey(&pk);
let tx = Tx::unsigned(
from,
Address::ZERO,
0,
b"wasm_bytecode_here".to_vec(),
5_000_000,
0,
FeePayer::Sender,
vec![],
None,
1,
TxType::Deploy,
);
assert!(tx.to.is_zero());
assert_eq!(tx.tx_type, TxType::Deploy);
}
}