|
| 1 | +use dlp_api::{ |
| 2 | + error::DlpError, |
| 3 | + v2::{ |
| 4 | + pda::{OPERATOR_BOND_SEED, PROTOCOL_CONFIG_SEED}, |
| 5 | + OperatorBond, ProtocolConfig, RegisterOperatorArgs, |
| 6 | + OPERATOR_STATUS_ACTIVE, |
| 7 | + }, |
| 8 | +}; |
| 9 | +use solana_sdk_ids::system_program; |
| 10 | +use solana_system_interface::instruction as system_instruction; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + processor::utils::{ |
| 14 | + loaders::{ |
| 15 | + load_initialized_pda, load_program, load_signer, |
| 16 | + load_uninitialized_pda, |
| 17 | + }, |
| 18 | + pda::create_pda, |
| 19 | + }, |
| 20 | + solana_program::{ |
| 21 | + account_info::AccountInfo, entrypoint::ProgramResult, program::invoke, |
| 22 | + program_error::ProgramError, pubkey::Pubkey, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +/// Register one operator for v2 commitments. |
| 27 | +/// |
| 28 | +/// Accounts: |
| 29 | +/// 0: `[signer, writable]` operator identity and stake payer |
| 30 | +/// 1: `[signer]` protocol authority that admits the operator |
| 31 | +/// 2: `[writable]` OperatorBond PDA |
| 32 | +/// 3: `[]` ProtocolConfig PDA |
| 33 | +/// 4: `[]` system program |
| 34 | +pub fn process_register_operator( |
| 35 | + _program_id: &Pubkey, |
| 36 | + accounts: &[AccountInfo], |
| 37 | + data: &[u8], |
| 38 | +) -> ProgramResult { |
| 39 | + let args = RegisterOperatorArgs::try_from_bytes(data)?; |
| 40 | + |
| 41 | + let [operator, authority, operator_bond, protocol_config, system_program] = |
| 42 | + accounts |
| 43 | + else { |
| 44 | + return Err(ProgramError::NotEnoughAccountKeys); |
| 45 | + }; |
| 46 | + |
| 47 | + load_signer(operator, "operator")?; |
| 48 | + load_signer(authority, "authority")?; |
| 49 | + load_program(system_program, system_program::id(), "system program")?; |
| 50 | + |
| 51 | + load_initialized_pda( |
| 52 | + protocol_config, |
| 53 | + &[PROTOCOL_CONFIG_SEED], |
| 54 | + &crate::id(), |
| 55 | + false, |
| 56 | + "protocol config", |
| 57 | + )?; |
| 58 | + |
| 59 | + let protocol_config_data = protocol_config.try_borrow_data()?; |
| 60 | + let protocol_config_state = |
| 61 | + ProtocolConfig::try_from_bytes_with_discriminator( |
| 62 | + protocol_config_data.as_ref(), |
| 63 | + )?; |
| 64 | + |
| 65 | + if protocol_config_state.authority != *authority.key { |
| 66 | + return Err(DlpError::InvalidAuthority.into()); |
| 67 | + } |
| 68 | + |
| 69 | + if *operator.key == Pubkey::default() |
| 70 | + || args.amount_lamports < protocol_config_state.min_operator_bond |
| 71 | + { |
| 72 | + return Err(ProgramError::InvalidInstructionData); |
| 73 | + } |
| 74 | + |
| 75 | + let operator_bond_bump = load_uninitialized_pda( |
| 76 | + operator_bond, |
| 77 | + &[OPERATOR_BOND_SEED, operator.key.as_ref()], |
| 78 | + &crate::id(), |
| 79 | + true, |
| 80 | + "operator bond", |
| 81 | + )?; |
| 82 | + |
| 83 | + create_pda( |
| 84 | + operator_bond, |
| 85 | + &crate::id(), |
| 86 | + OperatorBond::SPACE, |
| 87 | + &[OPERATOR_BOND_SEED, operator.key.as_ref()], |
| 88 | + operator_bond_bump, |
| 89 | + system_program, |
| 90 | + operator, |
| 91 | + )?; |
| 92 | + |
| 93 | + invoke( |
| 94 | + &system_instruction::transfer( |
| 95 | + operator.key, |
| 96 | + operator_bond.key, |
| 97 | + args.amount_lamports, |
| 98 | + ), |
| 99 | + &[ |
| 100 | + operator.clone(), |
| 101 | + operator_bond.clone(), |
| 102 | + system_program.clone(), |
| 103 | + ], |
| 104 | + )?; |
| 105 | + |
| 106 | + let operator_bond_state = OperatorBond { |
| 107 | + operator_identity: *operator.key, |
| 108 | + stake_lamports: args.amount_lamports, |
| 109 | + locked_lamports: 0, |
| 110 | + status: OPERATOR_STATUS_ACTIVE, |
| 111 | + withdraw_requested_slot: None, |
| 112 | + }; |
| 113 | + |
| 114 | + let mut operator_bond_data = operator_bond.try_borrow_mut_data()?; |
| 115 | + operator_bond_state |
| 116 | + .to_bytes_with_discriminator(operator_bond_data.as_mut())?; |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
0 commit comments