Skip to content

Commit cf1aa4f

Browse files
authored
Merge pull request #422 from okp4/feat/dataverse-instantiate
Feat/dataverse instantiate
2 parents 9e1b882 + 89a55ee commit cf1aa4f

File tree

10 files changed

+291
-23
lines changed

10 files changed

+291
-23
lines changed

.size-limit.json

+6
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"running": false,
1717
"brotli": false,
1818
"gzip": false
19+
},
20+
{
21+
"path": "target/wasm32-unknown-unknown/release/okp4_dataverse.wasm",
22+
"running": false,
23+
"brotli": false,
24+
"gzip": false
1925
}
2026
]

Cargo.lock

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ members = ["contracts/*", "packages/*"]
33

44
[workspace.dependencies]
55
cosmwasm-schema = "1.5.0"
6-
cosmwasm-std = "1.5.0"
6+
cosmwasm-std = { version = "1.5.0", features = ["cosmwasm_1_2"] }
77
cosmwasm-storage = "1.4.1"
88
cw-multi-test = "0.15.1"
99
cw-storage-plus = "1.2.0"
1010
cw-utils = "1.0.2"
1111
cw2 = "1.1.1"
12+
okp4-cognitarium = { path = "contracts/okp4-cognitarium", features = [
13+
"library",
14+
] }
1215
okp4-logic-bindings = { path = "packages/okp4-logic-bindings" }
13-
okp4-objectarium = { path = "contracts/okp4-objectarium" }
16+
okp4-objectarium = { path = "contracts/okp4-objectarium", features = [
17+
"library",
18+
] }
1419
okp4-objectarium-client = { path = "packages/okp4-objectarium-client" }
1520
schemars = "0.8.16"
1621
serde = { version = "1.0.192", default-features = false, features = ["derive"] }

Makefile.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ rustup target add wasm32-unknown-unknown
5959

6060
[tasks.wasm]
6161
args = [
62+
"hack",
6263
"build",
6364
"--release",
6465
"--lib",
@@ -67,7 +68,7 @@ args = [
6768
"--locked",
6869
]
6970
command = "cargo"
70-
dependencies = ["install-wasm"]
71+
dependencies = ["install-wasm", "install-cargo-hack"]
7172
env = { RUSTFLAGS = "-C link-arg=-s" }
7273

7374
[tasks.schema]
@@ -537,6 +538,9 @@ if ! [ -x "$(command -v ffizer)" ]; then
537538
fi
538539
'''
539540

541+
[tasks.install-cargo-hack]
542+
install_crate = { crate_name = "cargo-hack" }
543+
540544
[config]
541545
default_to_workspace = false
542546
min_version = "0.36.3"

contracts/okp4-dataverse/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ cw-storage-plus.workspace = true
3535
cw-utils.workspace = true
3636
cw2.workspace = true
3737
itertools = "0.12.0"
38-
okp4-logic-bindings.workspace = true
39-
okp4-objectarium-client.workspace = true
40-
okp4-objectarium.workspace = true
38+
okp4-cognitarium.workspace = true
4139
schemars.workspace = true
4240
serde.workspace = true
4341
thiserror.workspace = true

contracts/okp4-dataverse/src/contract.rs

+115-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#[cfg(not(feature = "library"))]
22
use cosmwasm_std::entry_point;
3-
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult};
3+
use cosmwasm_std::{
4+
instantiate2_address, to_json_binary, Binary, CodeInfoResponse, Deps, DepsMut, Env,
5+
MessageInfo, Response, StdError, StdResult, WasmMsg,
6+
};
47
use cw2::set_contract_version;
58

69
use crate::error::ContractError;
710
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
11+
use crate::state::{Dataverse, DATAVERSE};
812

913
// version info for migration info
1014
const CONTRACT_NAME: &str = concat!("crates.io:", env!("CARGO_PKG_NAME"));
@@ -13,13 +17,50 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
1317
#[cfg_attr(not(feature = "library"), entry_point)]
1418
pub fn instantiate(
1519
deps: DepsMut<'_>,
16-
_env: Env,
20+
env: Env,
1721
_info: MessageInfo,
18-
_msg: InstantiateMsg,
22+
msg: InstantiateMsg,
1923
) -> Result<Response, ContractError> {
2024
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
2125

22-
Err(StdError::generic_err("Not implemented").into())
26+
let creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
27+
let CodeInfoResponse { checksum, .. } = deps
28+
.querier
29+
.query_wasm_code_info(msg.triplestore_config.code_id.u64())?;
30+
let salt = Binary::from(msg.name.as_bytes());
31+
32+
let _triplestore_address = instantiate2_address(&checksum, &creator, &salt)?;
33+
34+
// Necessary stuff for testing purposes, see: https://github.com/CosmWasm/cosmwasm/issues/1648
35+
let triplestore_address = {
36+
#[cfg(not(test))]
37+
{
38+
deps.api.addr_humanize(&_triplestore_address)?
39+
}
40+
#[cfg(test)]
41+
cosmwasm_std::Addr::unchecked("predicted address")
42+
};
43+
44+
DATAVERSE.save(
45+
deps.storage,
46+
&Dataverse {
47+
name: msg.name.clone(),
48+
triplestore_address: triplestore_address.clone(),
49+
},
50+
)?;
51+
52+
Ok(Response::new()
53+
.add_attribute("triplestore_address", triplestore_address.to_string())
54+
.add_message(WasmMsg::Instantiate2 {
55+
admin: Some(env.contract.address.to_string()),
56+
code_id: msg.triplestore_config.code_id.u64(),
57+
label: format!("{}_triplestore", msg.name),
58+
msg: to_json_binary(&okp4_cognitarium::msg::InstantiateMsg {
59+
limits: msg.triplestore_config.limits.into(),
60+
})?,
61+
funds: vec![],
62+
salt,
63+
}))
2364
}
2465

2566
#[cfg_attr(not(feature = "library"), entry_point)]
@@ -42,4 +83,73 @@ pub fn query(_deps: Deps<'_>, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
4283
pub mod query {}
4384

4485
#[cfg(test)]
45-
mod tests {}
86+
mod tests {
87+
use super::*;
88+
use crate::msg::{TripleStoreConfig, TripleStoreLimitsInput};
89+
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
90+
use cosmwasm_std::{
91+
Addr, Attribute, ContractResult, HexBinary, SubMsg, SystemError, SystemResult, Uint128,
92+
Uint64, WasmQuery,
93+
};
94+
95+
#[test]
96+
fn proper_instantiate() {
97+
let mut deps = mock_dependencies();
98+
deps.querier.update_wasm(|query| match query {
99+
WasmQuery::CodeInfo { code_id, .. } => {
100+
let resp = CodeInfoResponse::new(
101+
code_id.clone(),
102+
"creator".to_string(),
103+
HexBinary::from_hex(
104+
"3B94AAF0B7D804B5B458DED0D20CACF95D2A1C8DF78ED3C89B61291760454AEC",
105+
)
106+
.unwrap(),
107+
);
108+
SystemResult::Ok(ContractResult::Ok(to_json_binary(&resp).unwrap()))
109+
}
110+
_ => SystemResult::Err(SystemError::Unknown {}),
111+
});
112+
113+
let store_limits = TripleStoreLimitsInput {
114+
max_byte_size: Some(Uint128::from(50000u128)),
115+
..Default::default()
116+
};
117+
118+
let msg = InstantiateMsg {
119+
name: "my-dataverse".to_string(),
120+
triplestore_config: TripleStoreConfig {
121+
code_id: Uint64::from(17u64),
122+
limits: store_limits.clone(),
123+
},
124+
};
125+
126+
let env = mock_env();
127+
let res = instantiate(deps.as_mut(), env.clone(), mock_info("creator", &[]), msg).unwrap();
128+
129+
assert_eq!(
130+
res.attributes,
131+
vec![Attribute::new("triplestore_address", "predicted address")]
132+
);
133+
assert_eq!(
134+
res.messages,
135+
vec![SubMsg::new(WasmMsg::Instantiate2 {
136+
admin: Some(env.contract.address.to_string()),
137+
code_id: 17,
138+
label: "my-dataverse_triplestore".to_string(),
139+
msg: to_json_binary(&okp4_cognitarium::msg::InstantiateMsg {
140+
limits: store_limits.into(),
141+
})
142+
.unwrap(),
143+
funds: vec![],
144+
salt: Binary::from("my-dataverse".as_bytes()),
145+
})]
146+
);
147+
assert_eq!(
148+
DATAVERSE.load(&deps.storage).unwrap(),
149+
Dataverse {
150+
name: "my-dataverse".to_string(),
151+
triplestore_address: Addr::unchecked("predicted address"),
152+
}
153+
)
154+
}
155+
}

contracts/okp4-dataverse/src/error.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use cosmwasm_std::StdError;
1+
use cosmwasm_std::{Instantiate2AddressError, StdError};
22
use thiserror::Error;
33

44
#[derive(Error, Debug, PartialEq)]
55
pub enum ContractError {
66
#[error("{0}")]
77
Std(#[from] StdError),
8+
9+
#[error("{0}")]
10+
Instantiate2Address(#[from] Instantiate2AddressError),
811
}

contracts/okp4-dataverse/src/msg.rs

+81-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use cosmwasm_schema::{cw_serde, QueryResponses};
2-
use cosmwasm_std::Binary;
2+
use cosmwasm_std::{Binary, Uint128, Uint64};
33

44
/// `InstantiateMsg` is used to initialize a new instance of the dataverse.
55
#[cw_serde]
66
pub struct InstantiateMsg {
77
/// A unique name to identify the dataverse instance.
88
pub name: String,
9+
10+
/// The configuration used to instantiate the triple store.
11+
pub triplestore_config: TripleStoreConfig,
912
}
1013

1114
/// `ExecuteMsg` defines the set of possible actions that can be performed on the dataverse.
@@ -55,19 +58,19 @@ pub enum ExecuteMsg {
5558
/// Preconditions:
5659
/// - The identity must be unique within the dataverse.
5760
identity: Did,
58-
/// The URI that identifies the dataset.
61+
/// The URI that identifies the resource.
5962
/// This URI makes sense only in the context of the service that provides the resource.
6063
///
6164
/// Preconditions:
6265
/// - The URI must be unique within the dataverse.
6366
identifier: Uri,
64-
/// The URI of the service, already registered in the dataverse, that provides the dataset.
67+
/// The URI of the service, already registered in the dataverse, that provides the resource.
6568
///
6669
/// Preconditions:
6770
/// - The Service must be registered in the dataverse before the resource can be registered.
6871
provided_by: Uri,
6972
/// The URI of the entity responsible for registering and managing the resource in the dataverse (i.e. on the blockchain).
70-
/// It's an optional field, if not provided the dataset is registered by the entity that invokes the transaction.
73+
/// It's an optional field, if not provided the resource is registered by the entity that invokes the transaction.
7174
registrar: Option<Did>,
7275
},
7376

@@ -120,6 +123,80 @@ pub enum ExecuteMsg {
120123
},
121124
}
122125

126+
/// # TripleStoreConfig
127+
/// `TripleStoreConfig` represents the configuration related to the management of the triple store.
128+
#[cw_serde]
129+
pub struct TripleStoreConfig {
130+
/// The code id that will be used to instantiate the triple store contract in which
131+
/// to store dataverse semantic data. It must implement the cognitarium interface.
132+
pub code_id: Uint64,
133+
134+
/// Limitations regarding triple store usage.
135+
pub limits: TripleStoreLimitsInput,
136+
}
137+
138+
/// # TripleStoreLimitsInput
139+
/// Contains requested limitations regarding store usages.
140+
#[cw_serde]
141+
#[derive(Default)]
142+
pub struct TripleStoreLimitsInput {
143+
/// The maximum number of triples the store can contain.
144+
/// Default to [Uint128::MAX] if not set, which can be considered as no limit.
145+
pub max_triple_count: Option<Uint128>,
146+
/// The maximum number of bytes the store can contain.
147+
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
148+
/// including the size of data types and language tags if any.
149+
/// Default to [Uint128::MAX] if not set, which can be considered as no limit.
150+
pub max_byte_size: Option<Uint128>,
151+
/// The maximum number of bytes the store can contain for a single triple.
152+
/// The size of a triple is counted as the sum of the size of its subject, predicate and object,
153+
/// including the size of data types and language tags if any. The limit is used to prevent
154+
/// storing very large triples, especially literals.
155+
/// Default to [Uint128::MAX] if not set, which can be considered as no limit.
156+
pub max_triple_byte_size: Option<Uint128>,
157+
/// The maximum limit of a query, i.e. the maximum number of triples returned by a select query.
158+
/// Default to 30 if not set.
159+
pub max_query_limit: Option<u32>,
160+
/// The maximum number of variables a query can select.
161+
/// Default to 30 if not set.
162+
pub max_query_variable_count: Option<u32>,
163+
/// The maximum number of bytes an insert data query can contain.
164+
/// Default to [Uint128::MAX] if not set, which can be considered as no limit.
165+
pub max_insert_data_byte_size: Option<Uint128>,
166+
/// The maximum number of triples an insert data query can contain (after parsing).
167+
/// Default to [Uint128::MAX] if not set, which can be considered as no limit.
168+
pub max_insert_data_triple_count: Option<Uint128>,
169+
}
170+
171+
impl From<TripleStoreLimitsInput> for okp4_cognitarium::msg::StoreLimitsInput {
172+
fn from(value: TripleStoreLimitsInput) -> Self {
173+
let mut limits = okp4_cognitarium::msg::StoreLimitsInput::default();
174+
if let Some(max_triple_count) = value.max_triple_count {
175+
limits.max_triple_count = max_triple_count;
176+
}
177+
if let Some(max_byte_size) = value.max_byte_size {
178+
limits.max_byte_size = max_byte_size;
179+
}
180+
if let Some(max_triple_byte_size) = value.max_triple_byte_size {
181+
limits.max_triple_byte_size = max_triple_byte_size;
182+
}
183+
if let Some(max_query_limit) = value.max_query_limit {
184+
limits.max_query_limit = max_query_limit;
185+
}
186+
if let Some(max_query_variable_count) = value.max_query_variable_count {
187+
limits.max_query_variable_count = max_query_variable_count;
188+
}
189+
if let Some(max_insert_data_byte_size) = value.max_insert_data_byte_size {
190+
limits.max_insert_data_byte_size = max_insert_data_byte_size;
191+
}
192+
if let Some(max_insert_data_triple_count) = value.max_insert_data_triple_count {
193+
limits.max_insert_data_triple_count = max_insert_data_triple_count;
194+
}
195+
196+
limits
197+
}
198+
}
199+
123200
/// # RdfFormat
124201
/// `RdfFormat` represents the various serialization formats for RDF (Resource Description Framework) data.
125202
#[cw_serde]

contracts/okp4-dataverse/src/state.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
use cosmwasm_std::Addr;
2+
use cw_storage_plus::Item;
3+
use serde::{Deserialize, Serialize};
14

5+
pub const DATAVERSE: Item<'_, Dataverse> = Item::new("dataverse");
6+
7+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
8+
pub struct Dataverse {
9+
pub name: String,
10+
pub triplestore_address: Addr,
11+
}

0 commit comments

Comments
 (0)