// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
// This file is either (a) original to Subzero Labs, Inc. or (b) derived from the Anza codebase and modified by Subzero Labs, Inc.

use rialo_s_clock::Clock;
use rialo_s_epoch_schedule::EpochSchedule;
use rialo_s_instruction::error::InstructionError;
use rialo_s_pubkey::Pubkey;
use rialo_s_rent::Rent;
#[cfg(any(test, feature = "dev-context-only-utils"))]
use rialo_s_sdk_ids::sysvar;
#[allow(deprecated)]
use rialo_s_sysvar::recent_blockhashes::RecentBlockhashes;
use rialo_s_sysvar::Sysvar;
use rialo_s_sysvar_id::SysvarId;
use rialo_s_transaction_context::{IndexOfAccount, InstructionContext, TransactionContext};
use rialo_s_type_overrides::sync::Arc;
use serde::de::DeserializeOwned;

use crate::invoke_context::InvokeContext;

#[cfg(feature = "frozen-abi")]
impl ::rialo_frozen_abi::abi_example::AbiExample for SysvarCache {
    fn example() -> Self {
        // SysvarCache is not Serialize so just rely on Default.
        SysvarCache::default()
    }
}

#[derive(Default, Clone, Debug)]
pub struct SysvarCache {
    // full account data as provided by bank, including any trailing zero bytes
    clock: Option<Vec<u8>>,
    epoch_schedule: Option<Vec<u8>>,
    rent: Option<Vec<u8>>,

    #[allow(deprecated)]
    recent_blockhashes: Option<RecentBlockhashes>,
}

#[cfg(any(test, feature = "dev-context-only-utils"))]
const RECENT_BLOCKHASHES_ID: Pubkey =
    Pubkey::from_str_const("SysvarRecentB1ockHashes11111111111111111111");

impl SysvarCache {
    /// Overwrite a sysvar. For testing purposes only.
    #[allow(deprecated)]
    #[cfg(any(test, feature = "dev-context-only-utils"))]
    pub fn set_sysvar_for_tests<T: Sysvar + SysvarId>(&mut self, sysvar: &T) {
        let data = bincode::serialize(sysvar).expect("Failed to serialize sysvar.");
        let sysvar_id = T::id();
        match sysvar_id {
            sysvar::clock::ID => {
                self.clock = Some(data);
            }
            sysvar::epoch_schedule::ID => {
                self.epoch_schedule = Some(data);
            }
            RECENT_BLOCKHASHES_ID => {
                let recent_blockhashes: RecentBlockhashes = bincode::deserialize(&data)
                    .expect("Failed to deserialize RecentBlockhashes sysvar.");
                self.recent_blockhashes = Some(recent_blockhashes);
            }
            sysvar::rent::ID => {
                self.rent = Some(data);
            }
            _ => panic!("Unrecognized Sysvar ID: {sysvar_id}"),
        }
    }

    // this is exposed for SyscallGetSysvar and should not otherwise be used
    pub fn sysvar_id_to_buffer(&self, sysvar_id: &Pubkey) -> &Option<Vec<u8>> {
        if Clock::check_id(sysvar_id) {
            &self.clock
        } else if EpochSchedule::check_id(sysvar_id) {
            &self.epoch_schedule
        } else if Rent::check_id(sysvar_id) {
            &self.rent
        } else {
            &None
        }
    }

    // most if not all of the obj getter functions can be removed once builtins transition to bpf
    // the Arc<T> wrapper is to preserve the existing public interface
    fn get_sysvar_obj<T: DeserializeOwned>(
        &self,
        sysvar_id: &Pubkey,
    ) -> Result<Arc<T>, InstructionError> {
        if let Some(ref sysvar_buf) = self.sysvar_id_to_buffer(sysvar_id) {
            bincode::deserialize(sysvar_buf)
                .map(Arc::new)
                .map_err(|_| InstructionError::UnsupportedSysvar)
        } else {
            Err(InstructionError::UnsupportedSysvar)
        }
    }

    pub fn get_clock(&self) -> Result<Arc<Clock>, InstructionError> {
        self.get_sysvar_obj(&Clock::id())
    }

    pub fn get_epoch_schedule(&self) -> Result<Arc<EpochSchedule>, InstructionError> {
        self.get_sysvar_obj(&EpochSchedule::id())
    }

    pub fn get_rent(&self) -> Result<Arc<Rent>, InstructionError> {
        self.get_sysvar_obj(&Rent::id())
    }

    #[deprecated]
    #[allow(deprecated)]
    pub fn get_recent_blockhashes(&self) -> Result<Arc<RecentBlockhashes>, InstructionError> {
        self.recent_blockhashes
            .clone()
            .ok_or(InstructionError::UnsupportedSysvar)
            .map(Arc::new)
    }

    pub fn fill_missing_entries<F: FnMut(&Pubkey, &mut dyn FnMut(&[u8]))>(
        &mut self,
        mut get_account_data: F,
    ) {
        if self.clock.is_none() {
            get_account_data(&Clock::id(), &mut |data: &[u8]| {
                if bincode::deserialize::<Clock>(data).is_ok() {
                    self.clock = Some(data.to_vec());
                }
            });
        }

        if self.epoch_schedule.is_none() {
            get_account_data(&EpochSchedule::id(), &mut |data: &[u8]| {
                if bincode::deserialize::<EpochSchedule>(data).is_ok() {
                    self.epoch_schedule = Some(data.to_vec());
                }
            });
        }

        if self.rent.is_none() {
            get_account_data(&Rent::id(), &mut |data: &[u8]| {
                if bincode::deserialize::<Rent>(data).is_ok() {
                    self.rent = Some(data.to_vec());
                }
            });
        }

        #[allow(deprecated)]
        if self.recent_blockhashes.is_none() {
            get_account_data(&RecentBlockhashes::id(), &mut |data: &[u8]| {
                if let Ok(recent_blockhashes) = bincode::deserialize(data) {
                    self.recent_blockhashes = Some(recent_blockhashes);
                }
            });
        }
    }

    pub fn reset(&mut self) {
        *self = Self::default();
    }
}

/// These methods facilitate a transition from fetching sysvars from keyed
/// accounts to fetching from the sysvar cache without breaking consensus. In
/// order to keep consistent behavior, they continue to enforce the same checks
/// as `rialo_s_sdk::keyed_account::from_keyed_account` despite dynamically
/// loading them instead of deserializing from account data.
pub mod get_sysvar_with_account_check {
    use super::*;

    fn check_sysvar_account<S: Sysvar>(
        transaction_context: &TransactionContext,
        instruction_context: &InstructionContext,
        instruction_account_index: IndexOfAccount,
    ) -> Result<(), InstructionError> {
        let index_in_transaction = instruction_context
            .get_index_of_instruction_account_in_transaction(instruction_account_index)?;
        if !S::check_id(transaction_context.get_key_of_account_at_index(index_in_transaction)?) {
            return Err(InstructionError::InvalidArgument);
        }
        Ok(())
    }

    pub fn clock(
        invoke_context: &InvokeContext<'_, '_>,
        instruction_context: &InstructionContext,
        instruction_account_index: IndexOfAccount,
    ) -> Result<Arc<Clock>, InstructionError> {
        check_sysvar_account::<Clock>(
            invoke_context.transaction_context,
            instruction_context,
            instruction_account_index,
        )?;
        invoke_context.get_sysvar_cache().get_clock()
    }

    pub fn rent(
        invoke_context: &InvokeContext<'_, '_>,
        instruction_context: &InstructionContext,
        instruction_account_index: IndexOfAccount,
    ) -> Result<Arc<Rent>, InstructionError> {
        check_sysvar_account::<Rent>(
            invoke_context.transaction_context,
            instruction_context,
            instruction_account_index,
        )?;
        invoke_context.get_sysvar_cache().get_rent()
    }

    #[allow(deprecated)]
    pub fn recent_blockhashes(
        invoke_context: &InvokeContext<'_, '_>,
        instruction_context: &InstructionContext,
        instruction_account_index: IndexOfAccount,
    ) -> Result<Arc<RecentBlockhashes>, InstructionError> {
        check_sysvar_account::<RecentBlockhashes>(
            invoke_context.transaction_context,
            instruction_context,
            instruction_account_index,
        )?;
        invoke_context.get_sysvar_cache().get_recent_blockhashes()
    }
}

#[cfg(test)]
mod tests {
    use test_case::test_case;

    use super::*;

    // sysvar cache provides the full account data of a sysvar
    // the setters MUST NOT be changed to serialize an object representation
    // it is required that the syscall be able to access the full buffer as it exists onchain
    // this is meant to cover the cases:
    // * account data is larger than struct sysvar
    // * vector sysvar has fewer than its maximum entries
    // if at any point the data is roundtripped through bincode, the vector will shrink
    #[test_case(Clock::default(); "clock")]
    #[test_case(EpochSchedule::default(); "epoch_schedule")]
    #[test_case(Rent::default(); "rent")]
    fn test_sysvar_cache_preserves_bytes<T: Sysvar>(_: T) {
        let id = T::id();
        let size = T::size_of().saturating_mul(2);
        let in_buf = vec![0; size];

        let mut sysvar_cache = SysvarCache::default();
        sysvar_cache.fill_missing_entries(|pubkey, callback| {
            if *pubkey == id {
                callback(&in_buf)
            }
        });
        let sysvar_cache = sysvar_cache;

        let out_buf = sysvar_cache.sysvar_id_to_buffer(&id).clone().unwrap();

        assert_eq!(out_buf, in_buf);
    }
}
