Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cw law stone spec #143

Merged
merged 7 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"running": false,
"brotli": false,
"gzip": false
},
{
"path": "target/wasm32-unknown-unknown/release/cw_law_stone.wasm",
"running": false,
"brotli": false,
"gzip": false
}
]
16 changes: 16 additions & 0 deletions Cargo.lock

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

54 changes: 54 additions & 0 deletions contracts/cw-law-stone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
authors = ["OKP4"]
edition = "2021"
name = "cw-law-stone"
version = "0.2.0"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
codegen-units = 1
debug = false
debug-assertions = false
incremental = false
lto = true
opt-level = 3
overflow-checks = true
panic = 'abort'
rpath = false

[dependencies]
cosmwasm-schema.workspace = true
cosmwasm-std.workspace = true
cosmwasm-storage.workspace = true
cw-storage-plus.workspace = true
cw2.workspace = true
schemars.workspace = true
serde.workspace = true
thiserror.workspace = true
logic-bindings = { version = "0.2", path = "../../packages/logic-bindings" }

[dev-dependencies]
cw-multi-test.workspace = true

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []

[package.metadata.scripts]
optimize = """docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.12.10
"""
12 changes: 12 additions & 0 deletions contracts/cw-law-stone/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tasks.generate_schema]
args = ["run", "--bin", "schema"]
command = "cargo"

[tasks.schema]
dependencies = ["generate_schema"]
script = '''
SCHEMA=$(find schema -type f -maxdepth 1 -name '*.json' -print0)
TITLE=$(jq -r .contract_name $SCHEMA)
jq --arg description "$(cat README.md)" '. + {description: $description}' $SCHEMA > $SCHEMA.tmp && mv $SCHEMA.tmp $SCHEMA
jq --arg title $TITLE '. + {title: $title}' $SCHEMA > $SCHEMA.tmp && mv $SCHEMA.tmp $SCHEMA
'''
13 changes: 13 additions & 0 deletions contracts/cw-law-stone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CW Law Stone

## Overview

The `cw-law-stone` smart contract aims to provide GaaS (i.e. Governance as a Service) in any [Cosmos blockchains](https://cosmos.network/) using the [CosmWasm](https://cosmwasm.com/) framework and the [Logic](https://docs.okp4.network/modules/next/logic) OKP4 module.

This contract is built around a Prolog program describing the law by rules and facts. The law stone is immutable, this means it can only been questioned, there is no update mechanisms.

The `cw-law-stone` responsibility is to guarantee the availability of its rules in order to question them, but not to ensure the rules application.

To ensure reliability over time, the associated Prolog program is stored and pinned in a `cw-storage` contract. Moreover, all the eventual loaded files must be stored in a `cw-storage` contract as well, allowing the contract to pin them.

To be able to free the underlying resources (i.e. objects in `cw-storage`) if not used anymore, the contract admin can break the stone.
11 changes: 11 additions & 0 deletions contracts/cw-law-stone/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_schema::write_api;

use cw_law_stone::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}
39 changes: 39 additions & 0 deletions contracts/cw-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::ContractError::NotImplemented;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult};
use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:law-stone";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut<'_>,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Err(NotImplemented {})
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut<'_>,
_env: Env,
_info: MessageInfo,
_msg: ExecuteMsg,
) -> Result<Response, ContractError> {
Err(NotImplemented {})
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(_deps: Deps<'_>, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
Err(StdError::generic_err("Not implemented"))
}
11 changes: 11 additions & 0 deletions contracts/cw-law-stone/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Not implemented")]
NotImplemented {},
}
17 changes: 17 additions & 0 deletions contracts/cw-law-stone/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![forbid(unsafe_code)]
#![deny(
warnings,
rust_2018_idioms,
trivial_casts,
trivial_numeric_casts,
unused_lifetimes,
unused_import_braces,
unused_qualifications,
unused_qualifications
)]

pub mod contract;
mod error;
pub mod msg;

pub use crate::error::ContractError;
52 changes: 52 additions & 0 deletions contracts/cw-law-stone/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Binary;
#[allow(unused_imports)]
use logic_bindings::AskResponse;

/// Instantiate message
#[cw_serde]
pub struct InstantiateMsg {
/// The Prolog program carrying law rules and facts.
pub program: Binary,

/// The `cw-storage` contract address on which to store the law program.
pub storage_address: String,
}

/// Execute messages
#[cw_serde]
pub enum ExecuteMsg {
/// # BreakStone
/// Break the stone making this contract unusable, by clearing all the related resources:
/// - Unpin all the pinned objects on `cw-storage` contracts, if any.
/// - Forget the main program (i.e. or at least unpin it).
/// Only the contract admin is authorized to break it, if any.
ccamel marked this conversation as resolved.
Show resolved Hide resolved
/// If already broken, this is a no-op.
BreakStone,
}

/// Query messages
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// # Ask
/// If not broken, ask the logic module the provided query with the law program loaded.
#[returns(AskResponse)]
Ask { query: String },

/// # Program
/// If not broken, returns the law program location information.
#[returns(ProgramResponse)]
Program,
amimart marked this conversation as resolved.
Show resolved Hide resolved
}

/// # ProgramResponse
/// ProgramResponse carry elements to locate the program in a `cw-storage` contract.
#[cw_serde]
pub struct ProgramResponse {
/// The program object id in the `cw-storage` contract.
pub object_id: String,

/// The `cw-storage` contract address on which the law program is stored.
pub storage_address: String,
}
28 changes: 28 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Top-level Schemas

* [cw-law-stone](./cw-law-stone.md "CW Law StoneOverviewThe cw-law-stone smart contract aims to provide GaaS (i") – `-`

* [cw-logic-sample](./cw-logic-sample.md "CW Logic SampleSample contract to query the OKP4 logic module") – `-`

* [cw-storage](./cw-storage.md "CW StorageOverviewThe cw-storage smart contract enables the storage of arbitrary objects in any Cosmos blockchains using the CosmWasm framework") – `-`
Expand All @@ -12,8 +14,12 @@

### Objects

* [Ask](./cw-law-stone-querymsg-oneof-ask.md "If not broken, ask the logic module the provided query with the law program loaded") – `undefined#/query/oneOf/0`

* [Ask](./cw-logic-sample-querymsg-oneof-ask.md "Ask returns the evaluation of the query using the program context through the logic module") – `undefined#/query/oneOf/0`

* [AskResponse](./cw-law-stone-responses-askresponse.md) – `undefined#/responses/ask`

* [AskResponse](./cw-logic-sample-responses-askresponse.md) – `undefined#/responses/ask`

* [Bucket](./cw-storage-querymsg-oneof-bucket.md "Bucket returns the bucket information") – `undefined#/query/oneOf/0`
Expand All @@ -28,6 +34,8 @@

* [Increment](./cw-template-executemsg-oneof-increment.md "Execute an increment message") – `undefined#/execute/oneOf/0`

* [InstantiateMsg](./cw-law-stone-instantiatemsg.md "Instantiate message") – `undefined#/instantiate`

* [InstantiateMsg](./cw-logic-sample-instantiatemsg.md "Instantiate messages") – `undefined#/instantiate`

* [InstantiateMsg](./cw-storage-instantiatemsg.md "Instantiate messages") – `undefined#/instantiate`
Expand Down Expand Up @@ -56,12 +64,24 @@

* [PinObject](./cw-storage-executemsg-oneof-pinobject.md "PinObject pins the object in the bucket for the considered sender") – `undefined#/execute/oneOf/2`

* [ProgramResponse](./cw-law-stone-responses-programresponse.md "ProgramResponse carry elements to locate the program in a cw-storage contract") – `undefined#/responses/program`

* [Reset](./cw-template-executemsg-oneof-reset.md "Reset counter to the specified value") – `undefined#/execute/oneOf/1`

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

* [UnpinObject](./cw-storage-executemsg-oneof-unpinobject.md "UnpinObject unpins the object in the bucket for the considered sender") – `undefined#/execute/oneOf/3`

* [Untitled object in cw-law-stone](./cw-law-stone-querymsg-oneof-ask-properties-ask.md) – `undefined#/query/oneOf/0/properties/ask`

* [Untitled object in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-answer.md) – `undefined#/responses/ask/definitions/Answer`

* [Untitled object in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-result.md) – `undefined#/responses/ask/definitions/Result`

* [Untitled object in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-substitution.md) – `undefined#/responses/ask/definitions/Substitution`

* [Untitled object in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-term.md) – `undefined#/responses/ask/definitions/Term`

* [Untitled object in cw-logic-sample](./cw-logic-sample-querymsg-oneof-ask-properties-ask.md) – `undefined#/query/oneOf/0/properties/ask`

* [Untitled object in cw-logic-sample](./cw-logic-sample-responses-askresponse-definitions-answer.md) – `undefined#/responses/ask/definitions/Answer`
Expand Down Expand Up @@ -102,6 +122,14 @@

### Arrays

* [Untitled array in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-answer-properties-results.md) – `undefined#/responses/ask/definitions/Answer/properties/results`

* [Untitled array in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-answer-properties-variables.md) – `undefined#/responses/ask/definitions/Answer/properties/variables`

* [Untitled array in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-result-properties-substitutions.md) – `undefined#/responses/ask/definitions/Result/properties/substitutions`

* [Untitled array in cw-law-stone](./cw-law-stone-responses-askresponse-definitions-term-properties-arguments.md) – `undefined#/responses/ask/definitions/Term/properties/arguments`

* [Untitled array in cw-logic-sample](./cw-logic-sample-responses-askresponse-definitions-answer-properties-results.md) – `undefined#/responses/ask/definitions/Answer/properties/results`

* [Untitled array in cw-logic-sample](./cw-logic-sample-responses-askresponse-definitions-answer-properties-variables.md) – `undefined#/responses/ask/definitions/Answer/properties/variables`
Expand Down
23 changes: 23 additions & 0 deletions docs/cw-law-stone-executemsg-oneof-breakstone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# BreakStone Schema

```txt
undefined#/execute/oneOf/0
```

Break the stone making this contract unusable, by clearing all the related resources: - Unpin all the pinned objects on `cw-storage` contracts, if any. - Forget the main program (i.e. or at least unpin it). Only the contract admin is authorized to break it, if any. If already broken, this is a no-op.

| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :--------------------------------------------------------------------- |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [cw-law-stone.json\*](schema/cw-law-stone.json "open original schema") |

## 0 Type

`string` ([BreakStone](cw-law-stone-executemsg-oneof-breakstone.md))

## 0 Constraints

**enum**: the value of this property must be equal to one of the following values:

| Value | Explanation |
| :-------------- | :---------- |
| `"break_stone"` | |
19 changes: 19 additions & 0 deletions docs/cw-law-stone-executemsg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ExecuteMsg Schema

```txt
undefined#/execute
```

Execute messages

| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :--------------------------------------------------------------------- |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [cw-law-stone.json\*](schema/cw-law-stone.json "open original schema") |

## execute Type

merged type ([ExecuteMsg](cw-law-stone-executemsg.md))

one (and only one) of

* [BreakStone](cw-law-stone-executemsg-oneof-breakstone.md "check type definition")
17 changes: 17 additions & 0 deletions docs/cw-law-stone-instantiatemsg-definitions-binary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Untitled string in cw-law-stone Schema

```txt
undefined#/instantiate/definitions/Binary
```

Binary is a wrapper around <code>Vec&lt;u8&gt;</code> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.

This is only needed as serde-json-{core,wasm} has a horrible encoding for <code>Vec&lt;u8&gt;</code>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.

| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :--------------------------------------------------------------------- |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [cw-law-stone.json\*](schema/cw-law-stone.json "open original schema") |

## Binary Type

`string`
Loading