Skip to content

Commit

Permalink
Merge pull request #493 from okp4/feat/law-stone-bindings-rework
Browse files Browse the repository at this point in the history
Feat/law stone bindings rework
  • Loading branch information
amimart authored Feb 23, 2024
2 parents 8495cd4 + 8deb368 commit 6722198
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 70 deletions.
8 changes: 3 additions & 5 deletions contracts/okp4-law-stone/examples/multiple-sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ Where:

- `{contract_name}`: Only informative, represents the corresponding smart contract name or type (e.g. `okp4-objectarium`);
- `{contract_address}`: The smart contract to query, concerning the `okp4-law-stone` it must be a `okp4-objectarium` contract;
- `{contract_query}`: The JSON query to perform on the targeted smart contract, URL encoded. In our case an `ObjectData` query, for example: `%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D`;
- `{contract_query}`: The JSON query to perform on the targeted smart contract, URL encoded. In our case an `ObjectData` query, for example: `%7B%22object_data%22%3A%7B%22id%22%3A%22b118d79b4a368028b34d564448e5f1082e098613434370f3c15d6a2bf9979dfc%22%7D%7D`;

## Instantiate

First the `template.pl` program must be stored on a `okp4-objectarium` and the `gov.pl` updated with the right URI in the `consult(File).` predicate, the URI should be in the form:

```bash
cosmwasm:okp4-objectarium:${STORAGE_ADDRESS}?query=%7B%22object_data%22%3A%7B%22id%22%3A%221a88ca1632c7323c0aa594000cda26ed9f48b36351c29c3d1e35e0a0474e862e%22%7D%7D
cosmwasm:okp4-objectarium:${STORAGE_ADDRESS}?query=%7B%22object_data%22%3A%7B%22id%22%3A%22b118d79b4a368028b34d564448e5f1082e098613434370f3c15d6a2bf9979dfc%22%7D%7D
```

The instantiate will take as parameters the base64 encoded program and the address of a `okp4-objectarium` contract, on which the program will be stored and pinned, the `template.pl` object will also be pinned to ensure all the needed resources stays available:
Expand All @@ -41,7 +41,6 @@ okp4d tx wasm instantiate $CODE_ID \
--from $ADDR \
--admin $ADMIN_ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"program\":\"$(cat gov.pl | base64)\", \"storage_address\": \"$STORAGE_ADDR\"}"
```

Expand All @@ -53,7 +52,7 @@ By using the `Ask` query we can provide Prolog predicates to be evaluated agains

```bash
okp4d query wasm contract-state smart $CONTRACT_ADDR \
"{\"ask\": {\"query\": \"can('change_governance', 'did:key:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').\"}}"
"{\"ask\": {\"query\": \"can('change_governance', 'did:example:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').\"}}"
```

## Break
Expand All @@ -68,6 +67,5 @@ By breaking the stone, you will not be able to query it anymore.
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
'"break_stone"'
```
4 changes: 2 additions & 2 deletions contracts/okp4-law-stone/examples/multiple-sources/gov.pl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
:- consult('cosmwasm:okp4-objectarium:${STORAGE_ADDRESS}?query=%7B%22object_data%22%3A%7B%22id%22%3A%221a88ca1632c7323c0aa594000cda26ed9f48b36351c29c3d1e35e0a0474e862e%22%7D%7D').
:- consult('cosmwasm:okp4-objectarium:${STORAGE_ADDRESS}?query=%7B%22object_data%22%3A%7B%22id%22%3A%22b118d79b4a368028b34d564448e5f1082e098613434370f3c15d6a2bf9979dfc%22%7D%7D').

admin_addr('okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').

allow_denom('uknow').
allow_did_method('key').
allow_did_method('example').
allow_addr(Addr) :- bech32_address(-('okp4', _), Addr).

min_exec_workflow_amount(1000000).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
valid_did(DID, Addr) :-
did_components(DID, did(Method, Addr, _, _, _)),
did_components(DID, did_components(Method, Addr, _, _, _)),
allow_did_method(Method),
allow_addr(Addr).

Expand Down
8 changes: 3 additions & 5 deletions contracts/okp4-law-stone/examples/single-source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ The spirit here is to provide a `okp4-law-stone` smart contract instance providi

You'll find in the [gov.pl](gov.pl) Prolog program some predicates defining the rules allowing to perform some typical Dataspaces actions.

The `can(Action, DID)` predicate will allow or not an action for a `did` (i.e. Decentralized Identifier), a `did` being expected to have the form: `did:key:${OKP4_ADDRESS}`. We can describe the action rules as follows:
The `can(Action, DID)` predicate will allow or not an action for a `did` (i.e. Decentralized Identifier), a `did` being expected to have the form: `did:example:${OKP4_ADDRESS}`. We can describe the action rules as follows:

- `change_governance`: Only the did admin can do it: `did:key:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6`;
- `change_governance`: Only the did admin can do it: `did:example:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6`;
- `exec_workflow`: Only a valid DID having a minimum spendable of `1000000uknow`;
- `create_dataset` Only a valid DID having a minimum spendable of `10000uknow`;
- `create_service` Only a valid DID having a minimum spendable of `100000uknow`;
Expand All @@ -25,7 +25,6 @@ okp4d tx wasm instantiate $CODE_ID \
--from $ADDR \
--admin $ADMIN_ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"program\":\"$(cat gov.pl | base64)\", \"storage_address\": \"$STORAGE_ADDR\"}"
```

Expand All @@ -37,7 +36,7 @@ By using the `Ask` query we can provide Prolog predicates to be evaluated agains

```bash
okp4d query wasm contract-state smart $CONTRACT_ADDR \
"{\"ask\": {\"query\": \"can('change_governance', 'did:key:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').\"}}"
"{\"ask\": {\"query\": \"can('change_governance', 'did:example:okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').\"}}"
```

## Break
Expand All @@ -52,6 +51,5 @@ By breaking the stone, you will not be able to query it anymore.
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
'"break_stone"'
```
4 changes: 2 additions & 2 deletions contracts/okp4-law-stone/examples/single-source/gov.pl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
admin_addr('okp41p8u47en82gmzfm259y6z93r9qe63l25dfwwng6').

allow_denom('uknow').
allow_did_method('key').
allow_did_method('example').
allow_addr(Addr) :- bech32_address(-('okp4', _), Addr).

valid_did(DID, Addr) :-
did_components(DID, did(Method, Addr, _, _, _)),
did_components(DID, did_components(Method, Addr, _, _, _)),
allow_did_method(Method),
allow_addr(Addr).

Expand Down
14 changes: 5 additions & 9 deletions contracts/okp4-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ mod tests {
use okp4_logic_bindings::testing::mock::mock_dependencies_with_logic_handler;
use okp4_logic_bindings::uri::CosmwasmUri;
use okp4_logic_bindings::{
Answer, AskResponse, LogicCustomQuery, Result as LogicResult, Substitution, Term,
Answer, AskResponse, LogicCustomQuery, Result as LogicResult, Substitution,
};
use okp4_objectarium::msg::PageInfo;
use std::collections::VecDeque;
Expand Down Expand Up @@ -300,15 +300,13 @@ mod tests {
gas_used: 1000,
answer: Some(Answer {
success: true,
error: None,
has_more: false,
variables: vec!["Files".to_string()],
results: vec![LogicResult {
substitutions: vec![Substitution {
variable: "Files".to_string(),
term: Term {
name: deps_name,
arguments: vec![],
},
expression: deps_name,
}],
}],
}),
Expand Down Expand Up @@ -462,15 +460,13 @@ mod tests {
gas_used: 1000,
answer: Some(Answer {
success: true,
error: None,
has_more: false,
variables: vec!["Foo".to_string()],
results: vec![LogicResult {
substitutions: vec![Substitution {
variable: "Foo".to_string(),
term: Term {
name: "bar".to_string(),
arguments: vec![],
},
expression: "bar".to_string(),
}],
}],
}),
Expand Down
11 changes: 4 additions & 7 deletions contracts/okp4-law-stone/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ pub fn ask_response_to_objects(
.flat_map(|r: &okp4_logic_bindings::Result| r.substitutions.clone())
.filter(|s| s.variable == variable)
.map(|s: Substitution| {
s.term
.parse()
s.parse_expression()
.map_err(|e| ContractError::LogicAskResponse(LogicAskResponseError::Parse(e)))
.and_then(term_as_vec)
})
Expand All @@ -72,7 +71,7 @@ pub fn ask_response_to_objects(
mod tests {
use super::*;
use okp4_logic_bindings::error::TermParseError;
use okp4_logic_bindings::{Answer, Term};
use okp4_logic_bindings::Answer;

#[test]
fn logic_to_objects() {
Expand Down Expand Up @@ -105,15 +104,13 @@ mod tests {
results: vec![okp4_logic_bindings::Result {
substitutions: vec![Substitution {
variable: "X".to_string(),
term: Term {
name: case.0,
arguments: vec![],
}
expression: case.0,
}]
}],
has_more: false,
success: true,
variables: vec![],
error: None,
}),
height: 1,
gas_used: 1,
Expand Down
5 changes: 0 additions & 5 deletions contracts/okp4-objectarium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ okp4d tx wasm instantiate $CODE_ID \
--from $ADDR \
--admin $ADMIN_ADDR \
--gas 1000000 \
--broadcast-mode block \
'{"bucket":"my-bucket"}'
```

Expand All @@ -75,7 +74,6 @@ We can store an object by providing its data in base64 encoded, we can pin the s
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"store_object\":{\"data\": \"$(cat my-data | base64)\",\"pin\":true}}"
```

Expand All @@ -87,13 +85,11 @@ With the following commands we can pin and unpin existing objects:
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"pin_object\":{\"id\": \"$OBJECT_ID\"}}"

okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"unpin_object\":{\"id\": \"$OBJECT_ID\"}}"
```

Expand All @@ -103,7 +99,6 @@ And if an object is not pinned, or pinned by the sender of transaction, we can r
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"forget_object\":{\"id\": \"$OBJECT_ID\"}}"
```

Expand Down
18 changes: 6 additions & 12 deletions docs/okp4-law-stone.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ This is only needed as serde-json-\{core,wasm\} has a horrible encoding for Vec&

| property | description |
| ----------- | -------------------------------------------------- |
| `error` | **string\|null**. |
| `has_more` | _(Required.) _ **boolean**. |
| `results` | _(Required.) _ **Array<[Result](#result)>**. |
| `success` | _(Required.) _ **boolean**. |
Expand All @@ -120,18 +121,11 @@ A string containing Base64-encoded data.

### Substitution

| property | description |
| ---------- | -------------------------- |
| `term` | _(Required.) _ **object**. |
| `variable` | _(Required.) _ **string**. |

### Term

| property | description |
| ----------- | ---------------------------------------------- |
| `arguments` | _(Required.) _ **Array<[Term](#term)>**. |
| `name` | _(Required.) _ **string**. |
| property | description |
| ------------ | -------------------------- |
| `expression` | _(Required.) _ **string**. |
| `variable` | _(Required.) _ **string**. |

---

_Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-law-stone.json` (`175af2aa8dc69420`)_
_Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-law-stone.json` (`b4af0e49e5a861ed`)_
7 changes: 1 addition & 6 deletions docs/okp4-objectarium.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ okp4d tx wasm instantiate $CODE_ID \
--from $ADDR \
--admin $ADMIN_ADDR \
--gas 1000000 \
--broadcast-mode block \
'{"bucket":"my-bucket"}'
```

Expand All @@ -75,7 +74,6 @@ We can store an object by providing its data in base64 encoded, we can pin the s
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"store_object\":{\"data\": \"$(cat my-data | base64)\",\"pin\":true}}"
```

Expand All @@ -87,13 +85,11 @@ With the following commands we can pin and unpin existing objects:
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"pin_object\":{\"id\": \"$OBJECT_ID\"}}"

okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"unpin_object\":{\"id\": \"$OBJECT_ID\"}}"
```

Expand All @@ -103,7 +99,6 @@ And if an object is not pinned, or pinned by the sender of transaction, we can r
okp4d tx wasm execute $CONTRACT_ADDR \
--from $ADDR \
--gas 1000000 \
--broadcast-mode block \
"{\"forget_object\":{\"id\": \"$OBJECT_ID\"}}"
```

Expand Down Expand Up @@ -516,4 +511,4 @@ A string containing a 128-bit integer in decimal representation.

---

_Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-objectarium.json` (`2b01e2531b94fc0e`)_
_Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-objectarium.json` (`37a6830a8446a597`)_
2 changes: 1 addition & 1 deletion packages/okp4-logic-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod query;
mod term_parser;
pub mod uri;

pub use query::{Answer, AskResponse, LogicCustomQuery, Result, Substitution, Term};
pub use query::{Answer, AskResponse, LogicCustomQuery, Result, Substitution};
pub use term_parser::TermValue;

// Exposed for testing only
Expand Down
24 changes: 9 additions & 15 deletions packages/okp4-logic-bindings/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct AskResponse {
#[serde(rename_all = "snake_case")]
pub struct Answer {
pub success: bool,
pub error: Option<String>,
pub has_more: bool,
pub variables: Vec<String>,
pub results: Vec<Result>,
Expand All @@ -39,19 +40,12 @@ pub struct Result {
#[serde(rename_all = "snake_case")]
pub struct Substitution {
pub variable: String,
pub term: Term,
pub expression: String,
}

#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct Term {
pub name: String,
pub arguments: Vec<Term>,
}

impl Term {
pub fn parse(self) -> std::result::Result<TermValue, TermParseError> {
from_str(self.name.as_str())
impl Substitution {
pub fn parse_expression(self) -> std::result::Result<TermValue, TermParseError> {
from_str(self.expression.as_str())
}
}

Expand All @@ -62,11 +56,11 @@ mod tests {
#[test]
fn term_parse() {
assert_eq!(
Term {
name: "'hello'".to_string(),
arguments: vec![],
Substitution {
variable: "X".to_string(),
expression: "'hello'".to_string(),
}
.parse(),
.parse_expression(),
Ok(TermValue::Value("hello".to_string()))
);
}
Expand Down

0 comments on commit 6722198

Please sign in to comment.