Skip to content

Commit

Permalink
fix(cognitarium): manage default values for store limits
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Apr 24, 2023
1 parent fcb0835 commit 148fdef
Show file tree
Hide file tree
Showing 56 changed files with 573 additions and 708 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions contracts/okp4-cognitarium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cw2.workspace = true
schemars.workspace = true
serde.workspace = true
thiserror.workspace = true
derive_builder = "0.12.0"

[dev-dependencies]
base64 = "0.21.0"
Expand Down
22 changes: 11 additions & 11 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
#[cfg(test)]
mod tests {
use super::*;
use crate::msg::StoreLimits;
use crate::msg::StoreLimitsInput;
use crate::state;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::Uint128;
Expand All @@ -59,12 +59,12 @@ mod tests {
let mut deps = mock_dependencies();

let msg = InstantiateMsg {
limits: StoreLimits {
limits: StoreLimitsInput {
max_triple_count: Some(Uint128::from(1u128)),
max_byte_size: Some(Uint128::from(2u128)),
max_triple_byte_size: Some(Uint128::from(3u128)),
max_query_limit: Some(Uint128::from(4u128)),
max_query_variable_count: Some(Uint128::from(5u128)),
max_query_limit: Some(4),
max_query_variable_count: Some(5),
max_insert_data_byte_size: Some(Uint128::from(6u128)),
max_insert_data_triple_count: Some(Uint128::from(7u128)),
},
Expand All @@ -79,13 +79,13 @@ mod tests {
assert_eq!(
store.limits,
state::StoreLimits {
max_triple_count: Some(Uint128::from(1u128)),
max_byte_size: Some(Uint128::from(2u128)),
max_triple_byte_size: Some(Uint128::from(3u128)),
max_query_limit: Some(Uint128::from(4u128)),
max_query_variable_count: Some(Uint128::from(5u128)),
max_insert_data_byte_size: Some(Uint128::from(6u128)),
max_insert_data_triple_count: Some(Uint128::from(7u128)),
max_triple_count: Uint128::from(1u128),
max_byte_size: Uint128::from(2u128),
max_triple_byte_size: Uint128::from(3u128),
max_query_limit: 4,
max_query_variable_count: 5,
max_insert_data_byte_size: Uint128::from(6u128),
max_insert_data_triple_count: Uint128::from(7u128),
}
);
}
Expand Down
100 changes: 87 additions & 13 deletions contracts/okp4-cognitarium/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Binary, Uint128};
use derive_builder::Builder;
use std::collections::BTreeMap;

/// Instantiate message
#[cw_serde]
pub struct InstantiateMsg {
/// Limitations regarding store usage.
pub limits: StoreLimits,
pub limits: StoreLimitsInput,
}

/// Execute messages
Expand Down Expand Up @@ -43,38 +44,78 @@ pub enum QueryMsg {
},
}

/// # StoreLimits
/// Contains limitations regarding store usages.
/// # StoreLimitsInput
/// Contains requested limitations regarding store usages.
#[cw_serde]
pub struct StoreLimits {
#[derive(Default, Builder)]
#[builder(default, setter(into, strip_option))]
pub struct StoreLimitsInput {
/// The maximum number of triples the store can contains.
/// If `None`, there is no limit on the number of triples.
/// If `None`, the default value of [Uint128::MAX] is used, which can be considered as no limit.
pub max_triple_count: Option<Uint128>,
/// The maximum number of bytes the store can contains.
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
/// including the size of data types and language tags if any.
/// If `None`, there is no limit on the number of bytes.
/// If `None`, the default value of [Uint128::MAX] is used, which can be considered as no limit.
pub max_byte_size: Option<Uint128>,
/// The maximum number of bytes the store can contains for a single triple.
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
/// including the size of data types and language tags if any. The limit is used to prevent
/// storing very large triples, especially literals.
/// If `None`, there is no limit on the number of bytes.
/// If `None`, the default value of [Uint128::MAX] is used, which can be considered as no limit.
pub max_triple_byte_size: Option<Uint128>,
/// The maximum limit of a query, i.e. the maximum number of triples returned by a select query.
/// If `None`, there is no limit on the number of triples returned.
pub max_query_limit: Option<Uint128>,
/// If `None`, the default value of 30 is used.
pub max_query_limit: Option<u32>,
/// The maximum number of variables a query can select.
/// If `None`, there is no limit on the number of variables.
pub max_query_variable_count: Option<Uint128>,
/// If `None`, the default value of 30 is used.
pub max_query_variable_count: Option<u32>,
/// The maximum number of bytes an insert data query can contains.
/// If `None`, there is no limit on the number of bytes.
/// If `None`, the default value of [Uint128::MAX] is used, which can be considered as no limit.
pub max_insert_data_byte_size: Option<Uint128>,
/// The maximum number of triples an insert data query can contains (after parsing).
/// If `None`, there is no limit on the number of triples.
/// If `None`, the default value of [Uint128::MAX] is used, which can be considered as no limit.
pub max_insert_data_triple_count: Option<Uint128>,
}

impl StoreLimitsInput {
const DEFAULT_MAX_TRIPLE_COUNT: Uint128 = Uint128::MAX;
const DEFAULT_MAX_BYTE_SIZE: Uint128 = Uint128::MAX;
const DEFAULT_MAX_TRIPLE_BYTE_SIZE: Uint128 = Uint128::MAX;
const DEFAULT_MAX_QUERY_LIMIT: u32 = 30;
const DEFAULT_MAX_QUERY_VARIABLE_COUNT: u32 = 30;
const DEFAULT_MAX_INSERT_DATA_BYTE_SIZE: Uint128 = Uint128::MAX;
const DEFAULT_MAX_INSERT_DATA_TRIPLE_COUNT: Uint128 = Uint128::MAX;

pub fn max_triple_count_or_default(&self) -> Uint128 {
self.max_triple_count
.unwrap_or(Self::DEFAULT_MAX_TRIPLE_COUNT)
}
pub fn max_byte_size_or_default(&self) -> Uint128 {
self.max_byte_size.unwrap_or(Self::DEFAULT_MAX_BYTE_SIZE)
}
pub fn max_triple_byte_size_or_default(&self) -> Uint128 {
self.max_triple_byte_size
.unwrap_or(Self::DEFAULT_MAX_TRIPLE_BYTE_SIZE)
}
pub fn max_query_limit_or_default(&self) -> u32 {
self.max_query_limit
.unwrap_or(Self::DEFAULT_MAX_QUERY_LIMIT)
}
pub fn max_query_variable_count_or_default(&self) -> u32 {
self.max_query_variable_count
.unwrap_or(Self::DEFAULT_MAX_QUERY_VARIABLE_COUNT)
}
pub fn max_insert_data_byte_size_or_default(&self) -> Uint128 {
self.max_insert_data_byte_size
.unwrap_or(Self::DEFAULT_MAX_INSERT_DATA_BYTE_SIZE)
}
pub fn max_insert_data_triple_count_or_default(&self) -> Uint128 {
self.max_insert_data_triple_count
.unwrap_or(Self::DEFAULT_MAX_INSERT_DATA_TRIPLE_COUNT)
}
}

/// # DataInput
/// Represents the input data for the [ExecuteMsg::InsertData] message as RDF triples in a specific format.
#[cw_serde]
Expand Down Expand Up @@ -106,6 +147,39 @@ pub struct StoreResponse {
pub stat: StoreStat,
}

/// # StoreLimits
/// Contains limitations regarding store usages.
#[cw_serde]
#[derive(Default, Builder)]
#[builder(default, setter(into, strip_option))]
pub struct StoreLimits {
/// The maximum number of triples the store can contains.
pub max_triple_count: Uint128,

/// The maximum number of bytes the store can contains.
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
/// including the size of data types and language tags if any.
pub max_byte_size: Uint128,

/// The maximum number of bytes the store can contains for a single triple.
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
/// including the size of data types and language tags if any. The limit is used to prevent
/// storing very large triples, especially literals.
pub max_triple_byte_size: Uint128,

/// The maximum limit of a query, i.e. the maximum number of triples returned by a select query.
pub max_query_limit: u32,

/// The maximum number of variables a query can select.
pub max_query_variable_count: u32,

/// The maximum number of bytes an insert data query can contains.
pub max_insert_data_byte_size: Uint128,

/// The maximum number of triples an insert data query can contains (after parsing).
pub max_insert_data_triple_count: Uint128,
}

/// # StoreStat
///
/// Contains usage information about the triple store.
Expand Down
32 changes: 23 additions & 9 deletions contracts/okp4-cognitarium/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,32 @@ pub struct Store {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct StoreLimits {
pub max_triple_count: Option<Uint128>,
pub max_byte_size: Option<Uint128>,
pub max_triple_byte_size: Option<Uint128>,
pub max_query_limit: Option<Uint128>,
pub max_query_variable_count: Option<Uint128>,
pub max_insert_data_byte_size: Option<Uint128>,
pub max_insert_data_triple_count: Option<Uint128>,
pub max_triple_count: Uint128,
pub max_byte_size: Uint128,
pub max_triple_byte_size: Uint128,
pub max_query_limit: u32,
pub max_query_variable_count: u32,
pub max_insert_data_byte_size: Uint128,
pub max_insert_data_triple_count: Uint128,
}

impl From<msg::StoreLimits> for StoreLimits {
fn from(value: msg::StoreLimits) -> Self {
impl From<msg::StoreLimitsInput> for StoreLimits {
fn from(value: msg::StoreLimitsInput) -> Self {
StoreLimits {
max_triple_count: value.max_triple_count_or_default(),
max_byte_size: value.max_byte_size_or_default(),
max_triple_byte_size: value.max_triple_byte_size_or_default(),
max_query_limit: value.max_query_limit_or_default(),
max_query_variable_count: value.max_query_variable_count_or_default(),
max_insert_data_byte_size: value.max_insert_data_byte_size_or_default(),
max_insert_data_triple_count: value.max_insert_data_triple_count_or_default(),
}
}
}

impl From<StoreLimits> for msg::StoreLimits {
fn from(value: StoreLimits) -> Self {
msg::StoreLimits {
max_triple_count: value.max_triple_count,
max_byte_size: value.max_byte_size,
max_triple_byte_size: value.max_triple_byte_size,
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@

* [Simple](./okp4-cognitarium-querymsg-definitions-wherecondition-oneof-simple.md "Represents a simple condition")`undefined#/query/definitions/WhereCondition/oneOf/0`

* [StoreLimits](./okp4-cognitarium-instantiatemsg-definitions-storelimits.md "Contains limitations regarding store usages")`undefined#/instantiate/definitions/StoreLimits`

* [StoreLimits](./okp4-cognitarium-responses-storeresponse-definitions-storelimits.md "Contains limitations regarding store usages")`undefined#/responses/store/definitions/StoreLimits`

* [StoreLimitsInput](./okp4-cognitarium-instantiatemsg-definitions-storelimitsinput.md "Contains requested limitations regarding store usages")`undefined#/instantiate/definitions/StoreLimitsInput`

* [StoreObject](./okp4-objectarium-executemsg-oneof-storeobject.md "StoreObject store an object to the bucket and make the sender the owner of the object")`undefined#/execute/oneOf/0`

* [StoreResponse](./okp4-cognitarium-responses-storeresponse.md "Contains information related to triple store")`undefined#/responses/store`
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 148fdef

Please sign in to comment.