From ef16082dbccadf7a2042946303847ab1a5cd10f0 Mon Sep 17 00:00:00 2001 From: chizor iwuh Date: Thu, 27 Apr 2023 23:06:52 +0100 Subject: [PATCH] update plugin, client --- plugin/src/builders/pool_rotation.rs | 90 ++++++++++++++++++- plugin/src/executors/tx.rs | 35 ++++++-- .../thread_lookup_tables_create.rs | 4 +- 3 files changed, 114 insertions(+), 15 deletions(-) diff --git a/plugin/src/builders/pool_rotation.rs b/plugin/src/builders/pool_rotation.rs index 85c2f504b..65abc4ca5 100644 --- a/plugin/src/builders/pool_rotation.rs +++ b/plugin/src/builders/pool_rotation.rs @@ -7,7 +7,8 @@ use anchor_lang::{ use clockwork_network_program::state::{Config, Pool, Registry, Snapshot, SnapshotFrame, Worker}; use log::info; use solana_client::nonblocking::rpc_client::RpcClient; -use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction}; +use solana_program::message::{VersionedMessage, v0}; +use solana_sdk::{signature::Keypair, signer::Signer, transaction::{VersionedTransaction}}; use crate::pool_position::PoolPosition; @@ -19,7 +20,7 @@ pub async fn build_pool_rotation_tx<'a>( snapshot: Snapshot, snapshot_frame: SnapshotFrame, worker_id: u64, -) -> Option { +) -> Option { info!("nonce: {:?} total_stake: {:?} current_position: {:?} stake_offset: {:?} stake_amount: {:?}", registry.nonce.checked_rem(snapshot.total_stake), snapshot.total_stake, @@ -80,8 +81,89 @@ pub async fn build_pool_rotation_tx<'a>( data: clockwork_network_program::instruction::PoolRotate {}.data(), }; + // let mut tx = Transaction::new_with_payer(&[ix.clone()], Some(&keypair.pubkey())); + // tx.sign(&[keypair], client.get_latest_blockhash().await.unwrap()); + // Build and sign tx. - let mut tx = Transaction::new_with_payer(&[ix.clone()], Some(&keypair.pubkey())); - tx.sign(&[keypair], client.get_latest_blockhash().await.unwrap()); + let blockhash = client.get_latest_blockhash().await.unwrap(); + + let tx = VersionedTransaction::try_new( + VersionedMessage::V0(v0::Message::try_compile( + &keypair.pubkey(), + &[ix.clone()], + &[], + blockhash, + ).expect("error compiling to v0 message")), + &[keypair], + ).expect("error creating new versioned transaction"); return Some(tx); } + +// pub async fn build_pool_rotation_tx<'a>( +// client: Arc, +// keypair: &Keypair, +// pool_position: PoolPosition, +// registry: Registry, +// snapshot: Snapshot, +// snapshot_frame: SnapshotFrame, +// worker_id: u64, +// ) -> Option { +// info!("nonce: {:?} total_stake: {:?} current_position: {:?} stake_offset: {:?} stake_amount: {:?}", +// registry.nonce.checked_rem(snapshot.total_stake), +// snapshot.total_stake, +// pool_position.current_position, +// snapshot_frame.stake_offset, +// snapshot_frame.stake_amount, +// ); + +// // Exit early if the rotator is not intialized +// if registry.nonce == 0 { +// return None; +// } + +// // Exit early the snapshot has no stake +// if snapshot.total_stake == 0 { +// return None; +// } + +// // Exit early if the worker is already in the pool. +// if pool_position.current_position.is_some() { +// return None; +// } + +// // Exit early if the snapshot frame is none or the worker has no delegated stake. +// if snapshot_frame.stake_amount.eq(&0) { +// return None; +// } + +// // Check if the rotation window is open for this worker. +// let is_rotation_window_open = match registry.nonce.checked_rem(snapshot.total_stake) { +// None => false, +// Some(sample) => { +// sample >= snapshot_frame.stake_offset +// && sample +// < snapshot_frame +// .stake_offset +// .checked_add(snapshot_frame.stake_amount) +// .unwrap() +// } +// }; +// if !is_rotation_window_open { +// return None; +// } + +// // Build rotation instruction to rotate the worker into pool 0. +// let snapshot_pubkey = Snapshot::pubkey(snapshot.id); +// let ix = clockwork_client::network::instruction::pool_rotate( +// Pool::pubkey(0), +// keypair.pubkey(), +// snapshot_pubkey, +// SnapshotFrame::pubkey(snapshot_pubkey, worker_id), +// Worker::pubkey(worker_id), +// ); + +// // Build and sign tx. +// let mut tx = Transaction::new_with_payer(&[ix.clone()], Some(&keypair.pubkey())); +// tx.sign(&[keypair], client.get_latest_blockhash().await.unwrap()); +// return Some(tx); +// } diff --git a/plugin/src/executors/tx.rs b/plugin/src/executors/tx.rs index eee836157..3ddcd45e7 100644 --- a/plugin/src/executors/tx.rs +++ b/plugin/src/executors/tx.rs @@ -25,7 +25,7 @@ use solana_program::pubkey::Pubkey; use solana_sdk::{ commitment_config::CommitmentConfig, signature::{Keypair, Signature}, - transaction::Transaction, + transaction::{VersionedTransaction}, }; use tokio::{runtime::Runtime, sync::RwLock}; @@ -423,7 +423,7 @@ impl TxExecutor { observed_slot: u64, due_slot: u64, thread_pubkey: Pubkey, - ) -> Option<(Pubkey, Transaction)> { + ) -> Option<(Pubkey, VersionedTransaction)> { let thread = match client.clone().get::(&thread_pubkey).await { Err(_err) => { self.increment_simulation_failure(thread_pubkey).await; @@ -433,8 +433,9 @@ impl TxExecutor { }; let lookup_tables_key = LookupTables::pubkey(thread.authority(), thread.pubkey()); - - let address_lookup_tables = match client.clone().get_lookup_tables(&lookup_tables_key).await { + + let address_lookup_tables = match client.clone().get_lookup_tables(&lookup_tables_key).await + { Err(_err) => { return None; } @@ -448,7 +449,7 @@ impl TxExecutor { thread, thread_pubkey, self.config.worker_id, - address_lookup_tables + address_lookup_tables, ) .await { @@ -484,7 +485,7 @@ impl TxExecutor { self: Arc, slot: u64, thread_pubkey: Pubkey, - tx: &Transaction, + tx: &VersionedTransaction, ) -> PluginResult<()> { let r_transaction_history = self.transaction_history.read().await; if let Some(metadata) = r_transaction_history.get(&thread_pubkey) { @@ -496,7 +497,10 @@ impl TxExecutor { Ok(()) } - async fn simulate_tx(self: Arc, tx: &Transaction) -> PluginResult { + async fn simulate_tx( + self: Arc, + tx: &VersionedTransaction, + ) -> PluginResult { TPU_CLIENT .get() .await @@ -525,14 +529,27 @@ impl TxExecutor { })? } - async fn submit_tx(self: Arc, tx: &Transaction) -> PluginResult { - if !TPU_CLIENT.get().await.send_transaction(tx).await { + async fn submit_tx( + self: Arc, + tx: &VersionedTransaction, + ) -> PluginResult { + let serialized_tx = serialize(&tx).unwrap(); + + if !TPU_CLIENT.get().await.send_wire_transaction(serialized_tx).await { return Err(GeyserPluginError::Custom( "Failed to send transaction".into(), )); } Ok(tx.clone()) } + // async fn submit_tx(self: Arc, tx: &Transaction) -> PluginResult { + // if !TPU_CLIENT.get().await.send_transaction(tx).await { + // return Err(GeyserPluginError::Custom( + // "Failed to send transaction".into(), + // )); + // } + // Ok(tx.clone()) + // } } impl Debug for TxExecutor { diff --git a/programs/thread/src/instructions/thread_lookup_tables_create.rs b/programs/thread/src/instructions/thread_lookup_tables_create.rs index 0731239fb..1a6565347 100644 --- a/programs/thread/src/instructions/thread_lookup_tables_create.rs +++ b/programs/thread/src/instructions/thread_lookup_tables_create.rs @@ -8,7 +8,7 @@ use anchor_lang::{ use crate::state::*; -/// Accounts required by the `thread_create` instruction. +/// Accounts required by the `thread_lookup_tables_create` instruction. #[derive(Accounts)] #[instruction(address_lookup_tables: Vec)] pub struct LookupTablesCreate<'info> { @@ -36,7 +36,7 @@ pub struct LookupTablesCreate<'info> { )] pub thread: Account<'info, Thread>, - /// The thread to be created. + /// The lookup_tables account to be created. #[account( init, seeds = [