mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
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.
This commit is contained in:
@@ -3,7 +3,7 @@ use std::io;
|
|||||||
use ciphersuite::Secp256k1;
|
use ciphersuite::Secp256k1;
|
||||||
use frost::dkg::ThresholdKeys;
|
use frost::dkg::ThresholdKeys;
|
||||||
|
|
||||||
use alloy_core::primitives::U256;
|
use alloy_core::primitives::{U256, Address as EthereumAddress};
|
||||||
|
|
||||||
use serai_client::networks::ethereum::Address;
|
use serai_client::networks::ethereum::Address;
|
||||||
|
|
||||||
@@ -17,8 +17,20 @@ use crate::{output::OutputId, machine::ClonableTransctionMachine};
|
|||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub(crate) enum Action {
|
pub(crate) enum Action {
|
||||||
SetKey { chain_id: U256, nonce: u64, key: PublicKey },
|
SetKey {
|
||||||
Batch { chain_id: U256, nonce: u64, coin: Coin, fee: U256, outs: Vec<(Address, U256)> },
|
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)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
@@ -33,25 +45,28 @@ impl Action {
|
|||||||
|
|
||||||
pub(crate) fn message(&self) -> Vec<u8> {
|
pub(crate) fn message(&self) -> Vec<u8> {
|
||||||
match self {
|
match self {
|
||||||
Action::SetKey { chain_id, nonce, key } => {
|
Action::SetKey { chain_id, router_address, nonce, key } => {
|
||||||
Router::update_serai_key_message(*chain_id, *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 {
|
pub(crate) fn eventuality(&self) -> Eventuality {
|
||||||
Eventuality(match self {
|
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() }
|
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![] }
|
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)?;
|
reader.read_exact(&mut chain_id)?;
|
||||||
let chain_id = U256::from_be_bytes(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];
|
let mut nonce = [0; 8];
|
||||||
reader.read_exact(&mut nonce)?;
|
reader.read_exact(&mut nonce)?;
|
||||||
let nonce = u64::from_le_bytes(nonce);
|
let nonce = u64::from_le_bytes(nonce);
|
||||||
@@ -100,7 +119,7 @@ impl SignableTransaction for Action {
|
|||||||
let key =
|
let key =
|
||||||
PublicKey::from_eth_repr(key).ok_or_else(|| io::Error::other("invalid key in Action"))?;
|
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 => {
|
1 => {
|
||||||
let coin = borsh::from_reader(reader)?;
|
let coin = borsh::from_reader(reader)?;
|
||||||
@@ -123,22 +142,24 @@ impl SignableTransaction for Action {
|
|||||||
|
|
||||||
outs.push((address, amount));
|
outs.push((address, amount));
|
||||||
}
|
}
|
||||||
Action::Batch { chain_id, nonce, coin, fee, outs }
|
Action::Batch { chain_id, router_address, nonce, coin, fee, outs }
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::SetKey { chain_id, nonce, key } => {
|
Self::SetKey { chain_id, router_address, nonce, key } => {
|
||||||
writer.write_all(&[0])?;
|
writer.write_all(&[0])?;
|
||||||
writer.write_all(&chain_id.to_be_bytes::<32>())?;
|
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(&nonce.to_le_bytes())?;
|
||||||
writer.write_all(&key.eth_repr())
|
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(&[1])?;
|
||||||
writer.write_all(&chain_id.to_be_bytes::<32>())?;
|
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(&nonce.to_le_bytes())?;
|
||||||
borsh::BorshSerialize::serialize(coin, writer)?;
|
borsh::BorshSerialize::serialize(coin, writer)?;
|
||||||
writer.write_all(&fee.as_le_bytes())?;
|
writer.write_all(&fee.as_le_bytes())?;
|
||||||
|
|||||||
@@ -87,8 +87,10 @@ impl<D: Db> signers::TransactionPublisher<Transaction> for TransactionPublisher<
|
|||||||
let nonce = tx.0.nonce();
|
let nonce = tx.0.nonce();
|
||||||
// Convert from an Action (an internal representation of a signable event) to a TxLegacy
|
// Convert from an Action (an internal representation of a signable event) to a TxLegacy
|
||||||
let tx = match tx.0 {
|
let tx = match tx.0 {
|
||||||
Action::SetKey { chain_id: _, nonce: _, key } => router.update_serai_key(&key, &tx.1),
|
Action::SetKey { chain_id: _, router_address: _, nonce: _, key } => {
|
||||||
Action::Batch { chain_id: _, nonce: _, coin, fee, outs } => {
|
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)
|
router.execute(coin, fee, OutInstructions::from(outs.as_ref()), &tx.1)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ impl<D: Db> smart_contract_scheduler::SmartContract<Rpc<D>> for SmartContract {
|
|||||||
) -> (Self::SignableTransaction, EventualityFor<Rpc<D>>) {
|
) -> (Self::SignableTransaction, EventualityFor<Rpc<D>>) {
|
||||||
let action = Action::SetKey {
|
let action = Action::SetKey {
|
||||||
chain_id: self.chain_id,
|
chain_id: self.chain_id,
|
||||||
|
router_address: if true { todo!("TODO") } else { Default::default() },
|
||||||
nonce,
|
nonce,
|
||||||
key: PublicKey::new(new_key).expect("rotating to an invald key"),
|
key: PublicKey::new(new_key).expect("rotating to an invald key"),
|
||||||
};
|
};
|
||||||
@@ -139,6 +140,7 @@ impl<D: Db> smart_contract_scheduler::SmartContract<Rpc<D>> for SmartContract {
|
|||||||
|
|
||||||
res.push(Action::Batch {
|
res.push(Action::Batch {
|
||||||
chain_id: self.chain_id,
|
chain_id: self.chain_id,
|
||||||
|
router_address: if true { todo!("TODO") } else { Default::default() },
|
||||||
nonce,
|
nonce,
|
||||||
coin: coin_to_ethereum_coin(coin),
|
coin: coin_to_ethereum_coin(coin),
|
||||||
fee: U256::try_from(total_gas).unwrap() * fee_per_gas,
|
fee: U256::try_from(total_gas).unwrap() * fee_per_gas,
|
||||||
|
|||||||
Reference in New Issue
Block a user