Skip to content

Commit

Permalink
split oc3 in 2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
toblich committed Jan 7, 2025
1 parent 9b6aa75 commit 5ce75ed
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use anchor_lang::prelude::*;
use anchor_spl::token_interface;

use crate::{
v1::ocr3::ocr3_set, AcceptOwnership, AddBillingTokenConfig, AddChainSelector,
BillingTokenConfig, CcipRouterError, DestChainAdded, DestChainConfig, DestChainConfigUpdated,
DestChainState, FeeTokenAdded, FeeTokenDisabled, FeeTokenEnabled, FeeTokenRemoved,
Ocr3ConfigInfo, OcrPluginType, RemoveBillingTokenConfig, SetOcrConfig, SetTokenBillingConfig,
SourceChainAdded, SourceChainConfig, SourceChainConfigUpdated, SourceChainState,
TimestampedPackedU224, TokenBilling, TransferOwnership, UpdateBillingTokenConfig,
UpdateConfigCCIPRouter, UpdateDestChainSelectorConfig, UpdateSourceChainSelectorConfig,
WithdrawBilledFunds, FEE_BILLING_SIGNER_SEEDS,
AcceptOwnership, AddBillingTokenConfig, AddChainSelector, BillingTokenConfig, CcipRouterError,
DestChainAdded, DestChainConfig, DestChainConfigUpdated, DestChainState, FeeTokenAdded,
FeeTokenDisabled, FeeTokenEnabled, FeeTokenRemoved, Ocr3ConfigInfo, OcrPluginType,
RemoveBillingTokenConfig, SetOcrConfig, SetTokenBillingConfig, SourceChainAdded,
SourceChainConfig, SourceChainConfigUpdated, SourceChainState, TimestampedPackedU224,
TokenBilling, TransferOwnership, UpdateBillingTokenConfig, UpdateConfigCCIPRouter,
UpdateDestChainSelectorConfig, UpdateSourceChainSelectorConfig, WithdrawBilledFunds,
FEE_BILLING_SIGNER_SEEDS,
};

use super::fee_quoter::do_billing_transfer;
use super::ocr3base::ocr3_set;

pub fn transfer_ownership(ctx: Context<TransferOwnership>, proposed_owner: Pubkey) -> Result<()> {
let mut config = ctx.accounts.config.load_mut()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub mod token_admin_registry;
mod fee_quoter;
mod merkle;
mod messages;
mod ocr3;
mod ocr3base;
mod ocr3impl;
mod pools;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar;
use anchor_lang::solana_program::{keccak, secp256k1_recover::*};

use crate::{CommitInput, ExecutionReportSingleChain, Ocr3Config, Ocr3ConfigInfo};
use crate::{Ocr3Config, Ocr3ConfigInfo};

#[constant]
pub const MAX_ORACLES: usize = 16; // can set a maximum of 16 transmitters + 16 signers simultaneously in a single set config tx
Expand Down Expand Up @@ -113,6 +113,11 @@ fn clear_transmitters(ocr3_config: &mut Ocr3Config) {
ocr3_config.transmitters = [[0; 32]; MAX_TRANSMITTERS]
}

pub trait Ocr3Report {
fn hash(&self, ctx: &ReportContext) -> [u8; 32];
fn len(&self) -> usize;
}

pub fn ocr3_transmit<R: Ocr3Report>(
ocr3_config: &Ocr3Config,
instruction_sysvar: &AccountInfo<'_>,
Expand Down Expand Up @@ -226,52 +231,6 @@ fn assign_oracles<const A: usize>(oracles: &mut [[u8; A]], location: &mut [[u8;
Ok(())
}

pub trait Ocr3Report {
fn hash(&self, ctx: &ReportContext) -> [u8; 32];
fn len(&self) -> usize;
}

pub struct Ocr3ReportForCommit<'a>(pub &'a CommitInput);

impl Ocr3Report for Ocr3ReportForCommit<'_> {
fn hash(&self, ctx: &ReportContext) -> [u8; 32] {
use anchor_lang::solana_program::hash;
let mut buffer: Vec<u8> = Vec::new();
self.0.serialize(&mut buffer).unwrap();
let report_len = self.len() as u16; // u16 > max tx size, u8 may have overflow
hash::hashv(&[&report_len.to_le_bytes(), &buffer, &ctx.as_bytes()]).to_bytes()
}

fn len(&self) -> usize {
4 + (32 + 28) * self.0.price_updates.token_price_updates.len() + // token_price_updates
4 + (8 + 28) * self.0.price_updates.gas_price_updates.len() + // gas_price_updates
self.0.merkle_root.len()
// + 4 + 65 * self.rmn_signatures.len()
}
}

pub struct Ocr3ReportForExecutionReportSingleChain<'a>(pub &'a ExecutionReportSingleChain);

impl Ocr3Report for Ocr3ReportForExecutionReportSingleChain<'_> {
fn hash(&self, _: &ReportContext) -> [u8; 32] {
[0; 32] // not needed, this report is not hashed for signing
}
fn len(&self) -> usize {
let offchain_token_data_len = self
.0
.offchain_token_data
.iter()
.fold(0, |acc, e| acc + 4 + e.len());

8 // source chain selector
+ self.0.message.len() // ccip message
+ 4 + offchain_token_data_len// offchain_token_data
+ 32 // root
+ 4 + self.0.proofs.len() * 32 // count + proofs
+ 4 + self.0.token_indexes.len() // token_indexes
}
}

#[error_code]
pub enum Ocr3Error {
#[msg("Invalid config: F must be positive")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anchor_lang::AnchorSerialize;

use crate::{CommitInput, ExecutionReportSingleChain};

use super::ocr3base::{Ocr3Report, ReportContext};

pub struct Ocr3ReportForCommit<'a>(pub &'a CommitInput);

impl Ocr3Report for Ocr3ReportForCommit<'_> {
fn hash(&self, ctx: &ReportContext) -> [u8; 32] {
use anchor_lang::solana_program::hash;
let mut buffer: Vec<u8> = Vec::new();
self.0.serialize(&mut buffer).unwrap();
let report_len = self.len() as u16; // u16 > max tx size, u8 may have overflow
hash::hashv(&[&report_len.to_le_bytes(), &buffer, &ctx.as_bytes()]).to_bytes()
}

fn len(&self) -> usize {
4 + (32 + 28) * self.0.price_updates.token_price_updates.len() + // token_price_updates
4 + (8 + 28) * self.0.price_updates.gas_price_updates.len() + // gas_price_updates
self.0.merkle_root.len()
// + 4 + 65 * self.rmn_signatures.len()
}
}

pub struct Ocr3ReportForExecutionReportSingleChain<'a>(pub &'a ExecutionReportSingleChain);

impl Ocr3Report for Ocr3ReportForExecutionReportSingleChain<'_> {
fn hash(&self, _: &ReportContext) -> [u8; 32] {
[0; 32] // not needed, this report is not hashed for signing
}
fn len(&self) -> usize {
let offchain_token_data_len = self
.0
.offchain_token_data
.iter()
.fold(0, |acc, e| acc + 4 + e.len());

8 // source chain selector
+ self.0.message.len() // ccip message
+ 4 + offchain_token_data_len// offchain_token_data
+ 32 // root
+ 4 + self.0.proofs.len() * 32 // count + proofs
+ 4 + self.0.token_indexes.len() // token_indexes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use solana_program::{instruction::Instruction, program::invoke_signed};

use super::merkle::{calculate_merkle_root, MerkleError};
use super::messages::{ReleaseOrMintInV1, ReleaseOrMintOutV1};
use super::ocr3::{ocr3_transmit, Ocr3ReportForExecutionReportSingleChain, ReportContext};
use super::ocr3base::{ocr3_transmit, ReportContext};
use super::ocr3impl::{Ocr3ReportForCommit, Ocr3ReportForExecutionReportSingleChain};
use super::pools::{
calculate_token_pool_account_indices, get_balance, interact_with_pool,
validate_and_parse_token_accounts, CCIP_POOL_V1_RET_BYTES,
};

use crate::v1::ocr3::Ocr3ReportForCommit;
use crate::{
Any2SolanaMessage, BillingTokenConfigWrapper, CcipRouterError, CommitInput, CommitReport,
CommitReportAccepted, CommitReportContext, DestChain, ExecuteReportContext,
Expand Down

0 comments on commit 5ce75ed

Please sign in to comment.