From 205da3fd3872c6bebaa10be9740928276376e3b4 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 12 Apr 2025 09:57:29 -0400 Subject: [PATCH] Update the Ethereum processor to the Router messages including their on-chain address This only updates the syntax. It does not yet actually route the address as necessary. --- .../ethereum/src/primitives/transaction.rs | 57 +++++++++++++------ processor/ethereum/src/publisher.rs | 6 +- processor/ethereum/src/scheduler.rs | 2 + 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/processor/ethereum/src/primitives/transaction.rs b/processor/ethereum/src/primitives/transaction.rs index a698fdb4..3bee4c59 100644 --- a/processor/ethereum/src/primitives/transaction.rs +++ b/processor/ethereum/src/primitives/transaction.rs @@ -3,7 +3,7 @@ use std::io; use ciphersuite::Secp256k1; use frost::dkg::ThresholdKeys; -use alloy_core::primitives::U256; +use alloy_core::primitives::{U256, Address as EthereumAddress}; use serai_client::networks::ethereum::Address; @@ -17,8 +17,20 @@ use crate::{output::OutputId, machine::ClonableTransctionMachine}; #[derive(Clone, PartialEq, Debug)] pub(crate) enum Action { - SetKey { chain_id: U256, nonce: u64, key: PublicKey }, - Batch { chain_id: U256, nonce: u64, coin: Coin, fee: U256, outs: Vec<(Address, U256)> }, + SetKey { + chain_id: U256, + router_address: EthereumAddress, + nonce: u64, + key: PublicKey, + }, + Batch { + chain_id: U256, + router_address: EthereumAddress, + nonce: u64, + coin: Coin, + fee: U256, + outs: Vec<(Address, U256)>, + }, } #[derive(Clone, PartialEq, Eq, Debug)] @@ -33,25 +45,28 @@ impl Action { pub(crate) fn message(&self) -> Vec { match self { - Action::SetKey { chain_id, nonce, key } => { - Router::update_serai_key_message(*chain_id, *nonce, key) + Action::SetKey { chain_id, router_address, nonce, key } => { + Router::update_serai_key_message(*chain_id, *router_address, *nonce, key) + } + Action::Batch { chain_id, router_address, nonce, coin, fee, outs } => { + Router::execute_message( + *chain_id, + *router_address, + *nonce, + *coin, + *fee, + OutInstructions::from(outs.as_ref()), + ) } - Action::Batch { chain_id, nonce, coin, fee, outs } => Router::execute_message( - *chain_id, - *nonce, - *coin, - *fee, - OutInstructions::from(outs.as_ref()), - ), } } pub(crate) fn eventuality(&self) -> Eventuality { Eventuality(match self { - Self::SetKey { chain_id: _, nonce, key } => { + Self::SetKey { chain_id: _, router_address: _, nonce, key } => { Executed::NextSeraiKeySet { nonce: *nonce, key: key.eth_repr() } } - Self::Batch { chain_id: _, nonce, .. } => { + Self::Batch { chain_id: _, router_address: _, nonce, .. } => { Executed::Batch { nonce: *nonce, message_hash: keccak256(self.message()), results: vec![] } } }) @@ -89,6 +104,10 @@ impl SignableTransaction for Action { reader.read_exact(&mut chain_id)?; let chain_id = U256::from_be_bytes(chain_id); + let mut router_address = [0; 20]; + reader.read_exact(&mut router_address)?; + let router_address = EthereumAddress::from(router_address); + let mut nonce = [0; 8]; reader.read_exact(&mut nonce)?; let nonce = u64::from_le_bytes(nonce); @@ -100,7 +119,7 @@ impl SignableTransaction for Action { let key = PublicKey::from_eth_repr(key).ok_or_else(|| io::Error::other("invalid key in Action"))?; - Action::SetKey { chain_id, nonce, key } + Action::SetKey { chain_id, router_address, nonce, key } } 1 => { let coin = borsh::from_reader(reader)?; @@ -123,22 +142,24 @@ impl SignableTransaction for Action { outs.push((address, amount)); } - Action::Batch { chain_id, nonce, coin, fee, outs } + Action::Batch { chain_id, router_address, nonce, coin, fee, outs } } _ => unreachable!(), }) } fn write(&self, writer: &mut impl io::Write) -> io::Result<()> { match self { - Self::SetKey { chain_id, nonce, key } => { + Self::SetKey { chain_id, router_address, nonce, key } => { writer.write_all(&[0])?; writer.write_all(&chain_id.to_be_bytes::<32>())?; + writer.write_all(router_address.as_slice())?; writer.write_all(&nonce.to_le_bytes())?; writer.write_all(&key.eth_repr()) } - Self::Batch { chain_id, nonce, coin, fee, outs } => { + Self::Batch { chain_id, router_address, nonce, coin, fee, outs } => { writer.write_all(&[1])?; writer.write_all(&chain_id.to_be_bytes::<32>())?; + writer.write_all(router_address.as_slice())?; writer.write_all(&nonce.to_le_bytes())?; borsh::BorshSerialize::serialize(coin, writer)?; writer.write_all(&fee.as_le_bytes())?; diff --git a/processor/ethereum/src/publisher.rs b/processor/ethereum/src/publisher.rs index afeb2603..65dab004 100644 --- a/processor/ethereum/src/publisher.rs +++ b/processor/ethereum/src/publisher.rs @@ -87,8 +87,10 @@ impl signers::TransactionPublisher for TransactionPublisher< let nonce = tx.0.nonce(); // Convert from an Action (an internal representation of a signable event) to a TxLegacy let tx = match tx.0 { - Action::SetKey { chain_id: _, nonce: _, key } => router.update_serai_key(&key, &tx.1), - Action::Batch { chain_id: _, nonce: _, coin, fee, outs } => { + Action::SetKey { chain_id: _, router_address: _, nonce: _, key } => { + router.update_serai_key(&key, &tx.1) + } + Action::Batch { chain_id: _, router_address: _, nonce: _, coin, fee, outs } => { router.execute(coin, fee, OutInstructions::from(outs.as_ref()), &tx.1) } }; diff --git a/processor/ethereum/src/scheduler.rs b/processor/ethereum/src/scheduler.rs index 207792ec..07bf185d 100644 --- a/processor/ethereum/src/scheduler.rs +++ b/processor/ethereum/src/scheduler.rs @@ -50,6 +50,7 @@ impl smart_contract_scheduler::SmartContract> for SmartContract { ) -> (Self::SignableTransaction, EventualityFor>) { let action = Action::SetKey { chain_id: self.chain_id, + router_address: if true { todo!("TODO") } else { Default::default() }, nonce, key: PublicKey::new(new_key).expect("rotating to an invald key"), }; @@ -139,6 +140,7 @@ impl smart_contract_scheduler::SmartContract> for SmartContract { res.push(Action::Batch { chain_id: self.chain_id, + router_address: if true { todo!("TODO") } else { Default::default() }, nonce, coin: coin_to_ethereum_coin(coin), fee: U256::try_from(total_gas).unwrap() * fee_per_gas,