Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin - Versioned Tx #263

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions plugin/src/builders/pool_rotation.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::sync::Arc;

use anchor_lang::{
solana_program::instruction::Instruction,
InstructionData, ToAccountMetas
};
use anchor_lang::{solana_program::instruction::Instruction, InstructionData, ToAccountMetas};
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_geyser_plugin_interface::geyser_plugin_interface::GeyserPluginError;
use solana_program::message::{VersionedMessage, v0};
use solana_sdk::{signature::Keypair, signer::Signer, transaction::VersionedTransaction};

use crate::pool_position::PoolPosition;

Expand All @@ -19,7 +18,7 @@ pub async fn build_pool_rotation_tx<'a>(
snapshot: Snapshot,
snapshot_frame: SnapshotFrame,
worker_id: u64,
) -> Option<Transaction> {
) -> Option<VersionedTransaction> {
info!("nonce: {:?} total_stake: {:?} current_position: {:?} stake_offset: {:?} stake_amount: {:?}",
registry.nonce.checked_rem(snapshot.total_stake),
snapshot.total_stake,
Expand Down Expand Up @@ -81,7 +80,23 @@ pub async fn build_pool_rotation_tx<'a>(
};

// 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);
let blockhash = client.get_latest_blockhash().await.unwrap();

let tx = match v0::Message::try_compile(
&keypair.pubkey(),
&[ix.clone()],
&[],
blockhash,
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to compile to v0 message ").into())),
Ok(message) => match VersionedTransaction::try_new(
VersionedMessage::V0(message),
&[keypair]
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to create versioned transaction ").into())),
Ok(tx) => Ok(tx)
}

};
return tx.ok();
}
54 changes: 44 additions & 10 deletions plugin/src/builders/thread_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use anchor_lang::{InstructionData, ToAccountMetas};
use clockwork_thread_program::state::{VersionedThread, Trigger};
use clockwork_network_program::state::Worker;
use bincode::serialize;
use clockwork_utils::thread::PAYER_PUBKEY;
use log::info;
use solana_account_decoder::UiAccountEncoding;
Expand All @@ -16,12 +17,16 @@ use solana_geyser_plugin_interface::geyser_plugin_interface::{
};
use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
pubkey::Pubkey, address_lookup_table_account::AddressLookupTableAccount,
};
use solana_sdk::{
account::Account, commitment_config::CommitmentConfig,
compute_budget::ComputeBudgetInstruction, signature::Keypair, signer::Signer,
transaction::Transaction,
account::Account,
commitment_config::CommitmentConfig,
compute_budget::ComputeBudgetInstruction,
message::{v0, VersionedMessage},
signature::Keypair,
signer::Signer,
transaction::VersionedTransaction,
};

/// Max byte size of a serialized transaction.
Expand All @@ -40,7 +45,8 @@ pub async fn build_thread_exec_tx(
thread: VersionedThread,
thread_pubkey: Pubkey,
worker_id: u64,
) -> PluginResult<Option<Transaction>> {
address_lookup_tables: Vec<AddressLookupTableAccount>
) -> PluginResult<Option<VersionedTransaction>> {
// Grab the thread and relevant data.
let now = std::time::Instant::now();
let blockhash = client.get_latest_blockhash().await.unwrap();
Expand Down Expand Up @@ -73,11 +79,24 @@ pub async fn build_thread_exec_tx(
let mut successful_ixs: Vec<Instruction> = vec![];
let mut units_consumed: Option<u64> = None;
loop {
let mut sim_tx = Transaction::new_with_payer(&ixs, Some(&signatory_pubkey));
sim_tx.sign(&[payer], blockhash);
let sim_tx = match v0::Message::try_compile(
&signatory_pubkey,
&ixs,
&address_lookup_tables,
blockhash,
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to compile to v0 message ").into())),
Ok(message) => match VersionedTransaction::try_new(
VersionedMessage::V0(message),
&[payer]
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to create versioned transaction ").into())),
Ok(tx) => Ok(tx)
}
}?;

// Exit early if the transaction exceeds the size limit.
if sim_tx.message_data().len() > TRANSACTION_MESSAGE_SIZE_LIMIT {
if serialize(&sim_tx).unwrap().len() > TRANSACTION_MESSAGE_SIZE_LIMIT {
break;
}

Expand Down Expand Up @@ -199,8 +218,23 @@ pub async fn build_thread_exec_tx(
}

// Build and return the signed transaction.
let mut tx = Transaction::new_with_payer(&successful_ixs, Some(&signatory_pubkey));
tx.sign(&[payer], blockhash);
let tx = match v0::Message::try_compile(
&signatory_pubkey,
&ixs,
&address_lookup_tables,
blockhash,
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to compile to v0 message ").into())),
Ok(message) => match VersionedTransaction::try_new(
VersionedMessage::V0(message),
&[payer]
) {
Err(_) => Err(GeyserPluginError::Custom(format!("Failed to create versioned transaction ").into())),
Ok(tx) => Ok(tx)
}

}?;

info!(
"slot: {:?} thread: {:?} sim_duration: {:?} instruction_count: {:?} compute_units: {:?} tx_sig: {:?}",
slot,
Expand Down
21 changes: 15 additions & 6 deletions plugin/src/executors/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -428,7 +428,7 @@ impl TxExecutor {
observed_slot: u64,
due_slot: u64,
thread_pubkey: Pubkey,
) -> Option<(Pubkey, Transaction, u64)> {
) -> Option<(Pubkey, VersionedTransaction, u64)> {
let thread = match client.clone().get::<VersionedThread>(&thread_pubkey).await {
Err(_err) => {
self.increment_simulation_failure(thread_pubkey).await;
Expand All @@ -455,6 +455,7 @@ impl TxExecutor {
thread,
thread_pubkey,
self.config.worker_id,
vec![],
)
.await
{
Expand Down Expand Up @@ -490,7 +491,7 @@ impl TxExecutor {
self: Arc<Self>,
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) {
Expand All @@ -502,7 +503,10 @@ impl TxExecutor {
Ok(())
}

async fn simulate_tx(self: Arc<Self>, tx: &Transaction) -> PluginResult<Transaction> {
async fn simulate_tx(
self: Arc<Self>,
tx: &VersionedTransaction,
) -> PluginResult<VersionedTransaction> {
TPU_CLIENT
.get()
.await
Expand Down Expand Up @@ -531,8 +535,13 @@ impl TxExecutor {
})?
}

async fn submit_tx(self: Arc<Self>, tx: &Transaction) -> PluginResult<Transaction> {
if !TPU_CLIENT.get().await.send_transaction(tx).await {
async fn submit_tx(
self: Arc<Self>,
tx: &VersionedTransaction,
) -> PluginResult<VersionedTransaction> {
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(),
));
Expand Down