use async_graphql::InputObject;
use linera_base::data_types::{Amount, ArithmeticError, Resources};
use serde::{Deserialize, Serialize};
#[derive(Eq, PartialEq, Hash, Clone, Debug, Serialize, Deserialize, InputObject)]
pub struct ResourceControlPolicy {
pub block: Amount,
pub fuel_unit: Amount,
pub read_operation: Amount,
pub write_operation: Amount,
pub byte_read: Amount,
pub byte_written: Amount,
pub byte_stored: Amount,
pub operation: Amount,
pub operation_byte: Amount,
pub message: Amount,
pub message_byte: Amount,
pub maximum_bytes_read_per_block: u64,
pub maximum_bytes_written_per_block: u64,
}
impl Default for ResourceControlPolicy {
fn default() -> Self {
Self {
block: Amount::default(),
fuel_unit: Amount::default(),
read_operation: Amount::default(),
write_operation: Amount::default(),
byte_read: Amount::default(),
byte_written: Amount::default(),
byte_stored: Amount::default(),
operation: Amount::default(),
operation_byte: Amount::default(),
message: Amount::default(),
message_byte: Amount::default(),
maximum_bytes_read_per_block: u64::MAX,
maximum_bytes_written_per_block: u64::MAX,
}
}
}
impl ResourceControlPolicy {
pub fn block_price(&self) -> Amount {
self.block
}
pub fn total_price(&self, resources: &Resources) -> Result<Amount, ArithmeticError> {
let mut amount = Amount::ZERO;
amount.try_add_assign(self.fuel_price(resources.fuel)?)?;
amount.try_add_assign(self.read_operations_price(resources.read_operations)?)?;
amount.try_add_assign(self.write_operations_price(resources.write_operations)?)?;
amount.try_add_assign(self.bytes_read_price(resources.bytes_to_read as u64)?)?;
amount.try_add_assign(self.bytes_written_price(resources.bytes_to_write as u64)?)?;
amount.try_add_assign(self.message.try_mul(resources.messages as u128)?)?;
amount.try_add_assign(self.message_bytes_price(resources.message_size as u64)?)?;
amount.try_add_assign(self.bytes_stored_price(resources.storage_size_delta as u64)?)?;
Ok(amount)
}
pub(crate) fn operation_bytes_price(&self, size: u64) -> Result<Amount, ArithmeticError> {
self.operation_byte.try_mul(size as u128)
}
pub(crate) fn message_bytes_price(&self, size: u64) -> Result<Amount, ArithmeticError> {
self.message_byte.try_mul(size as u128)
}
pub(crate) fn read_operations_price(&self, count: u32) -> Result<Amount, ArithmeticError> {
self.read_operation.try_mul(count as u128)
}
pub(crate) fn write_operations_price(&self, count: u32) -> Result<Amount, ArithmeticError> {
self.write_operation.try_mul(count as u128)
}
pub(crate) fn bytes_read_price(&self, count: u64) -> Result<Amount, ArithmeticError> {
self.byte_read.try_mul(count as u128)
}
pub(crate) fn bytes_written_price(&self, count: u64) -> Result<Amount, ArithmeticError> {
self.byte_written.try_mul(count as u128)
}
#[allow(dead_code)]
pub(crate) fn bytes_stored_price(&self, count: u64) -> Result<Amount, ArithmeticError> {
self.byte_stored.try_mul(count as u128)
}
pub(crate) fn fuel_price(&self, fuel: u64) -> Result<Amount, ArithmeticError> {
self.fuel_unit.try_mul(u128::from(fuel))
}
pub(crate) fn remaining_fuel(&self, balance: Amount) -> u64 {
u64::try_from(balance.saturating_div(self.fuel_unit)).unwrap_or(u64::MAX)
}
}
#[cfg(with_testing)]
impl ResourceControlPolicy {
pub fn only_fuel() -> Self {
Self {
fuel_unit: Amount::from_micros(1),
..Self::default()
}
}
pub fn fuel_and_block() -> Self {
Self {
block: Amount::from_millis(1),
fuel_unit: Amount::from_micros(1),
..Self::default()
}
}
pub fn all_categories() -> Self {
Self {
block: Amount::from_millis(1),
fuel_unit: Amount::from_nanos(1),
byte_read: Amount::from_attos(100),
byte_written: Amount::from_attos(1_000),
operation: Amount::from_attos(10),
operation_byte: Amount::from_attos(1),
message: Amount::from_attos(10),
message_byte: Amount::from_attos(1),
..Self::default()
}
}
pub fn devnet() -> Self {
Self {
block: Amount::from_millis(1),
fuel_unit: Amount::from_nanos(10),
byte_read: Amount::from_nanos(10),
byte_written: Amount::from_nanos(100),
read_operation: Amount::from_micros(10),
write_operation: Amount::from_micros(20),
byte_stored: Amount::from_nanos(10),
message_byte: Amount::from_nanos(100),
operation_byte: Amount::from_nanos(10),
operation: Amount::from_micros(10),
message: Amount::from_micros(10),
maximum_bytes_read_per_block: 100_000_000,
maximum_bytes_written_per_block: 10_000_000,
}
}
}