Skip to content

Commit

Permalink
Implement all queries on State that are required for the Coeus node.
Browse files Browse the repository at this point in the history
- also introduced the name Bundle for an array of operations that are signed separately in a transaction
- also fixed metadata in packages
  • Loading branch information
wigy-opensource-developer committed Nov 3, 2020
1 parent 0f38f59 commit 3b9a0e5
Show file tree
Hide file tree
Showing 35 changed files with 354 additions and 218 deletions.
2 changes: 1 addition & 1 deletion coeus-core-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "iop-coeus-core-ffi"
version = "0.0.6"
authors = ["IoP Ventures LLC <info@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
authors = ["IOP Ventures LLC <dev@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
edition = "2018"
license = "LGPL-3.0-or-later"

Expand Down
4 changes: 2 additions & 2 deletions coeus-core-wasm/.package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internet-of-people/coeus-core-wasm",
"collaborators": [
"IoP Ventures LLC <info@iop-ventures.com>",
"IOP Ventures LLC <dev@iop-ventures.com>",
"Rache Bartmoss <[email protected]>",
"wigy <[email protected]>"
],
Expand All @@ -19,4 +19,4 @@
"module": "browser/iop_coeus_core_wasm.js",
"types": "iop_coeus_core_wasm.d.ts",
"sideEffects": "false"
}
}
2 changes: 1 addition & 1 deletion coeus-core-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "iop-coeus-core-wasm"
version = "0.0.6"
authors = ["IoP Ventures LLC <info@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
authors = ["IOP Ventures LLC <dev@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
edition = "2018"
license = "LGPL-3.0-or-later"

Expand Down
2 changes: 1 addition & 1 deletion coeus-core-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub use signed::*;
pub use state::*;
pub use tx::*;

use serde::Serialize;
use wasm_bindgen::prelude::*;

use iop_coeus_core::*;
use iop_hydra_proto::txtype::coeus::CoeusAsset;
use iop_keyvault_wasm::*;
25 changes: 0 additions & 25 deletions coeus-core-wasm/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,3 @@ impl Wraps<UserOperation> for JsUserOperation {
&self.inner
}
}

#[wasm_bindgen(js_name = SystemOperation)]
pub struct JsSystemOperation {
inner: SystemOperation,
}

#[wasm_bindgen(js_class = SystemOperation)]
impl JsSystemOperation {
#[wasm_bindgen(js_name = startBlock)]
pub fn start_block(height: BlockHeight) -> JsSystemOperation {
SystemOperation::start_block(height).into()
}
}

impl From<SystemOperation> for JsSystemOperation {
fn from(inner: SystemOperation) -> Self {
Self { inner }
}
}

impl Wraps<SystemOperation> for JsSystemOperation {
fn inner(&self) -> &SystemOperation {
&self.inner
}
}
58 changes: 29 additions & 29 deletions coeus-core-wasm/src/signed.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
use super::*;

#[wasm_bindgen(js_name = NoncedOperationsBuilder)]
pub struct JsNoncedOperationsBuilder {
#[wasm_bindgen(js_name = NoncedBundleBuilder)]
pub struct JsNoncedBundleBuilder {
operations: Vec<UserOperation>,
}

#[wasm_bindgen(js_class = NoncedOperationsBuilder)]
impl JsNoncedOperationsBuilder {
#[wasm_bindgen(js_class = NoncedBundleBuilder)]
impl JsNoncedBundleBuilder {
#[wasm_bindgen(constructor)]
pub fn new() -> JsNoncedOperationsBuilder {
pub fn new() -> JsNoncedBundleBuilder {
Self { operations: Default::default() }
}

// TODO Alternative design would be to just have a build() that takes a JS array of
// JsOperations, and we make sure each item is of correct type and convert it to a Rust Vec
pub fn add(mut self, user_operation: &JsUserOperation) -> JsNoncedOperationsBuilder {
pub fn add(mut self, user_operation: &JsUserOperation) -> JsNoncedBundleBuilder {
self.operations.push(user_operation.inner().to_owned());
self
}

pub fn build(self, nonce: Nonce) -> JsNoncedOperations {
NoncedOperations::new(self.operations, nonce).into()
pub fn build(self, nonce: Nonce) -> JsNoncedBundle {
NoncedBundle::new(self.operations, nonce).into()
}
}

#[wasm_bindgen(js_name = NoncedOperations)]
pub struct JsNoncedOperations {
inner: NoncedOperations,
#[wasm_bindgen(js_name = NoncedBundle)]
pub struct JsNoncedBundle {
inner: NoncedBundle,
}

#[wasm_bindgen(js_class = NoncedOperations)]
impl JsNoncedOperations {
#[wasm_bindgen(js_class = NoncedBundle)]
impl JsNoncedBundle {
pub fn price(&self, state: &JsState) -> JsPrice {
let _state = state.inner();
self.inner.get_price().into()
}

pub fn sign(self, sk: &JsMPrivateKey) -> Result<JsSignedOperations, JsValue> {
pub fn sign(self, sk: &JsMPrivateKey) -> Result<JsSignedBundle, JsValue> {
let signed = self.inner.sign(sk.inner()).map_err_to_js()?;
Ok(signed.into())
}
Expand All @@ -46,28 +46,28 @@ impl JsNoncedOperations {
}
}

impl From<NoncedOperations> for JsNoncedOperations {
fn from(inner: NoncedOperations) -> Self {
impl From<NoncedBundle> for JsNoncedBundle {
fn from(inner: NoncedBundle) -> Self {
Self { inner }
}
}

impl Wraps<NoncedOperations> for JsNoncedOperations {
fn inner(&self) -> &NoncedOperations {
impl Wraps<NoncedBundle> for JsNoncedBundle {
fn inner(&self) -> &NoncedBundle {
&self.inner
}
}

#[wasm_bindgen(js_name = SignedOperations)]
pub struct JsSignedOperations {
inner: SignedOperations,
#[wasm_bindgen(js_name = SignedBundle)]
pub struct JsSignedBundle {
inner: SignedBundle,
}

#[wasm_bindgen(js_class = SignedOperations)]
impl JsSignedOperations {
#[wasm_bindgen(js_class = SignedBundle)]
impl JsSignedBundle {
#[wasm_bindgen(constructor)]
pub fn new(data: &JsValue) -> Result<JsSignedOperations, JsValue> {
let signed_ops: SignedOperations = data.into_serde().map_err_to_js()?;
pub fn new(data: &JsValue) -> Result<JsSignedBundle, JsValue> {
let signed_ops: SignedBundle = data.into_serde().map_err_to_js()?;
Ok(signed_ops.into())
}

Expand All @@ -80,14 +80,14 @@ impl JsSignedOperations {
}
}

impl From<SignedOperations> for JsSignedOperations {
fn from(inner: SignedOperations) -> Self {
impl From<SignedBundle> for JsSignedBundle {
fn from(inner: SignedBundle) -> Self {
Self { inner }
}
}

impl Wraps<SignedOperations> for JsSignedOperations {
fn inner(&self) -> &SignedOperations {
impl Wraps<SignedBundle> for JsSignedBundle {
fn inner(&self) -> &SignedBundle {
&self.inner
}
}
78 changes: 63 additions & 15 deletions coeus-core-wasm/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::*;

#[wasm_bindgen(js_name = State)]
#[wasm_bindgen(js_name = CoeusState)]
pub struct JsState {
inner: State,
}

#[wasm_bindgen(js_class = State)]
#[wasm_bindgen(js_class = CoeusState)]
impl JsState {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<JsState, JsValue> {
Expand All @@ -19,33 +19,81 @@ impl JsState {
JsValue::from_serde(data).map_err_to_js()
}

#[wasm_bindgen(js_name = applySignedOperations)]
pub fn apply_signed_operations(
&mut self, ops: &JsSignedOperations,
) -> Result<Version, JsValue> {
self.inner.apply_signed_operations(ops.inner().to_owned()).map_err_to_js()
#[wasm_bindgen(js_name = getMetadata)]
pub fn get_metadata(&self, name: &JsDomainName) -> Result<JsValue, JsValue> {
let domain = self.inner.domain(name.inner()).map_err_to_js()?;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct Metadata<'a> {
owner: &'a Principal,
subtree_policies: &'a SubtreePolicies,
registration_policy: &'a RegistrationPolicy,
expires_at_height: BlockHeight,
}

let metadata = Metadata {
owner: domain.owner(),
subtree_policies: domain.subtree_policies(),
registration_policy: domain.registration_policy(),
expires_at_height: domain.expires_at_height(),
};

JsValue::from_serde(&metadata).map_err_to_js()
}

#[wasm_bindgen(js_name = getChildren)]
pub fn get_children(&self, name: &JsDomainName) -> Result<JsValue, JsValue> {
let domain = self.inner.domain(name.inner()).map_err_to_js()?;
JsValue::from_serde(&domain.child_names()).map_err_to_js()
}

#[wasm_bindgen(js_name = applySystemOperation)]
pub fn apply_system_operation(&mut self, op: &JsSystemOperation) -> Result<Version, JsValue> {
self.inner.apply_operations(vec![op.inner().to_owned()]).map_err_to_js()
#[wasm_bindgen(js_name = lastNonce)]
pub fn last_nonce(&self, pk: &JsMPublicKey) -> u64 {
return self.inner.nonce(pk.inner());
}

#[wasm_bindgen(js_name = applyTransaction)]
pub fn apply_transaction(&mut self, txid: &str, asset: &JsCoeusAsset) -> Result<(), JsValue> {
self.inner.apply_transaction(txid, asset.inner().to_owned()).map_err_to_js()
}

#[wasm_bindgen(js_name = revertTransaction)]
pub fn revert_transaction(&mut self, txid: &str, asset: &JsCoeusAsset) -> Result<(), JsValue> {
self.inner.revert_transaction(txid, asset.inner().to_owned()).map_err_to_js()
}

#[wasm_bindgen(js_name = blockApplying)]
pub fn block_applying(&mut self, height: BlockHeight) -> Result<(), JsValue> {
self.inner.block_applying(height).map_err_to_js()
}

#[wasm_bindgen(js_name = blockReverted)]
pub fn block_reverted(&mut self, height: BlockHeight) -> Result<(), JsValue> {
self.inner.block_reverted(height).map_err_to_js()
}

#[wasm_bindgen(getter = corrupted)]
pub fn is_corrupted(&self) -> bool {
self.inner.is_corrupted()
}

#[wasm_bindgen(getter)]
pub fn version(&self) -> Version {
self.inner.version()
}

#[wasm_bindgen(js_name = undoLastOperation)]
pub fn undo_last_operation(&mut self, to_version: Version) -> Result<(), JsValue> {
self.inner.undo_last_operation(to_version).map_err_to_js()
}

#[wasm_bindgen(getter = lastSeenHeight)]
pub fn last_seen_height(&self) -> BlockHeight {
self.inner.last_seen_height()
}

#[wasm_bindgen(js_name = getTxnStatus)]
pub fn get_txn_status(&self, txid: &str) -> Result<bool, JsValue> {
let status = self.inner.get_txn_status(txid).map_err_to_js()?;
Ok(status.success)
}

// #[wasm_bindgen(js_name = toString)]
// pub fn stringify(&self) -> String {
// self.inner.to_string()
Expand Down
2 changes: 1 addition & 1 deletion coeus-core-wasm/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl JsCoeusTxBuilder {

// TODO support multiple signed operations
pub fn build(
&self, ops: &JsSignedOperations, sender_pubkey: &JsSecpPublicKey, nonce: u64,
&self, ops: &JsSignedBundle, sender_pubkey: &JsSecpPublicKey, nonce: u64,
) -> Result<JsValue, JsValue> {
let common_fields = CommonTransactionFields {
network: self.network,
Expand Down
2 changes: 1 addition & 1 deletion coeus-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "iop-coeus-core"
version = "0.0.6"
authors = ["IoP Ventures LLC <info@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
authors = ["IOP Ventures LLC <dev@iop-ventures.com>", "Rache Bartmoss <[email protected]>", "wigy <[email protected]>"]
edition = "2018"
license = "LGPL-3.0-or-later"

Expand Down
32 changes: 32 additions & 0 deletions coeus-core/src/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::*;

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoeusAsset {
pub bundles: Vec<SignedBundle>,
}

// TODO work out ecosystem for pricing model
impl CoeusAsset {
const FEE_BYTES_OFFSET: u64 = 0;
//const FLAKES_PER_BYTES: u64 = 3000;

pub fn fee(&self) -> u64 {
let price =
self.bundles.iter().fold(Price::fee(Self::FEE_BYTES_OFFSET), |mut price, bundle| {
price += bundle.get_price();
price
});
price.fee
}

pub fn to_bytes(&self) -> Result<Vec<u8>> {
let json_val = serde_json::to_value(self)?;
let json_str = canonical_json(&json_val)?;
Ok(json_str.into_bytes())
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
Ok(serde_json::from_slice(bytes)?)
}
}
6 changes: 3 additions & 3 deletions coeus-core/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Domain {
&self.subtree_policies
}

pub(crate) fn registration_policy(&self) -> &RegistrationPolicy {
pub fn registration_policy(&self) -> &RegistrationPolicy {
&self.registration_policy
}

Expand All @@ -120,7 +120,7 @@ impl Domain {
subtree_policies: SubtreePolicies::new().with_schema(Self::json_schema_draft6()),
registration_policy: RegistrationPolicy::any(),
data: json!({}),
expires_at_height: BlockHeight::max_value(),
expires_at_height: BlockHeight::MAX,
};
let mut root = Self {
name: name(&[]),
Expand All @@ -130,7 +130,7 @@ impl Domain {
subtree_policies: SubtreePolicies::new().with_expiration(2 * ExpirationPolicy::YEAR),
registration_policy: Default::default(),
data: json!({}),
expires_at_height: BlockHeight::max_value(),
expires_at_height: BlockHeight::MAX,
};
root.insert_or_replace_child(schema).unwrap();
root
Expand Down
3 changes: 3 additions & 0 deletions coeus-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod asset;
mod domain;
mod domain_name;
mod operations;
Expand All @@ -7,6 +8,7 @@ mod principal;
mod signed;
mod state;

pub use asset::*;
pub use domain::*;
pub use domain_name::*;
pub use operations::*;
Expand Down Expand Up @@ -34,6 +36,7 @@ use iop_keyvault::{
};
#[cfg(feature = "did")]
use iop_morpheus_core::data::did::Did;
use json_digest::canonical_json;

// TODO move all blockchain-related types to hydra-proto after adding typetags to Asset and TransactionType.
/// State identifier of a decentralized ledger, usually represented as a sequence number of blocks.
Expand Down
Loading

0 comments on commit 3b9a0e5

Please sign in to comment.