From f55e9b40e62ff9837b2a8b77c8b86a8fecac5515 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Wed, 10 May 2023 01:45:42 -0400 Subject: [PATCH] Have coordinator publish batches to Substrate --- coordinator/src/main.rs | 39 +++++++++++++++---- substrate/client/src/serai/in_instructions.rs | 2 +- substrate/client/src/serai/mod.rs | 4 +- .../client/tests/common/in_instructions.rs | 3 +- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index d6a4559d..9d3c3377 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -429,10 +429,11 @@ pub async fn handle_processors( ); // TODO: Also check the other KeyGenId fields - // TODO: Is this safe? + // TODO: Publish an unsigned TX with a Musig signature here, instead of on-chain voting + // That removes the need for this nonce let Ok(nonce) = serai.get_nonce(&substrate_signer.address()).await else { - log::error!("couldn't connect to Serai node to get nonce"); - todo!(); // TODO + log::error!("couldn't get nonce from Serai node"); + todo!() }; let tx = serai @@ -459,7 +460,7 @@ pub async fn handle_processors( log::info!("voted on key pair for {:?} in TX {}", id.set, hex::encode(hash)) } Err(e) => { - log::error!("couldn't connect to Serai node to publish TX: {:?}", e); + log::error!("couldn't connect to Serai node to publish vote TX: {:?}", e); todo!(); // TODO } } @@ -537,9 +538,33 @@ pub async fn handle_processors( })) } }, - ProcessorMessage::Substrate(msg) => match msg { - // TODO - processor_messages::substrate::ProcessorMessage::Update { .. } => todo!(), + ProcessorMessage::Substrate(inner_msg) => match inner_msg { + processor_messages::substrate::ProcessorMessage::Update { key: _, batch } => { + assert_eq!( + batch.batch.network, msg.network, + "processor sent us a batch for a different network than it was for", + ); + // TODO: Check this key's key pair's substrate key is authorized to publish batches + // TODO: Check the batch ID is an atomic increment + + match serai.publish(&serai.execute_batch(batch.clone())).await { + Ok(hash) => { + log::info!( + "executed batch {:?} {} (block {}) in TX {}", + batch.batch.network, + batch.batch.id, + hex::encode(batch.batch.block), + hex::encode(hash), + ) + } + Err(e) => { + log::error!("couldn't connect to Serai node to publish batch TX: {:?}", e); + todo!(); // TODO + } + } + + None + } }, }; diff --git a/substrate/client/src/serai/in_instructions.rs b/substrate/client/src/serai/in_instructions.rs index fadd3fe0..2c4b2c97 100644 --- a/substrate/client/src/serai/in_instructions.rs +++ b/substrate/client/src/serai/in_instructions.rs @@ -33,7 +33,7 @@ impl Serai { .await } - pub fn execute_batch(&self, batch: SignedBatch) -> Result { + pub fn execute_batch(&self, batch: SignedBatch) -> Encoded { self.unsigned::(&in_instructions::Call::::execute_batch { batch }) } } diff --git a/substrate/client/src/serai/mod.rs b/substrate/client/src/serai/mod.rs index 559d7860..13ee01ad 100644 --- a/substrate/client/src/serai/mod.rs +++ b/substrate/client/src/serai/mod.rs @@ -269,7 +269,7 @@ impl Serai { .map_err(SeraiError::RpcError) } - fn unsigned(&self, call: &C) -> Result { + fn unsigned(&self, call: &C) -> Encoded { // TODO: Should Serai purge the old transaction code AND set this to 0/1? const TRANSACTION_VERSION: u8 = 4; @@ -284,7 +284,7 @@ impl Serai { // Prefix the length let mut complete_bytes = scale::Compact(u32::try_from(bytes.len()).unwrap()).encode(); complete_bytes.extend(bytes); - Ok(Encoded(complete_bytes)) + Encoded(complete_bytes) } pub fn sign>( diff --git a/substrate/client/tests/common/in_instructions.rs b/substrate/client/tests/common/in_instructions.rs index fde6dc98..7563b96c 100644 --- a/substrate/client/tests/common/in_instructions.rs +++ b/substrate/client/tests/common/in_instructions.rs @@ -31,8 +31,7 @@ pub async fn provide_batch(batch: Batch) -> [u8; 32] { let block = publish_tx( &serai - .execute_batch(SignedBatch { batch: batch.clone(), signature: pair.sign(&batch.encode()) }) - .unwrap(), + .execute_batch(SignedBatch { batch: batch.clone(), signature: pair.sign(&batch.encode()) }), ) .await;