Skip to content

Commit

Permalink
refactor: clean up duplicate memory code in package
Browse files Browse the repository at this point in the history
Decouples package from wasm module loading
  • Loading branch information
Stan Bondi authored and sdbondi committed Jul 29, 2022
1 parent d560eea commit f286222
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 300 deletions.
3 changes: 1 addition & 2 deletions dan_layer/common_types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use tari_common_types::types::FixedHash;

// This is to avoid adding borsh as a dependency in common types (and therefore every application).
// Either this becomes the standard Hash type for the dan layer, or Borsh becomes the standard serialization format for
// Tari.
// TODO: Either this becomes the standard Hash type for the dan layer, or add borsh support to FixedHash.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Hash(FixedHash);

Expand Down
27 changes: 8 additions & 19 deletions dan_layer/engine/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,19 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use digest::Digest;
use rand::rngs::OsRng;
use tari_common_types::types::{PrivateKey, PublicKey};
use tari_crypto::{
hash::blake2::Blake256,
hashing::{DomainSeparatedHasher, DomainSeparation},
keys::PublicKey as PublicKeyT,
};
use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher, keys::PublicKey as PublicKeyT};

pub fn create_key_pair() -> (PrivateKey, PublicKey) {
PublicKey::random_keypair(&mut OsRng)
}
hash_domain!(TariEngineHashDomain, "tari.dan.engine", 0);

pub struct TariEngineDomainSeparation;
pub type TariEngineHasher = DomainSeparatedHasher<Blake256, TariEngineHashDomain>;

impl DomainSeparation for TariEngineDomainSeparation {
fn version() -> u8 {
0
}

fn domain() -> &'static str {
"tari.dan.engine"
}
pub fn hasher(label: &'static str) -> impl Digest<OutputSize = digest::consts::U32> {
TariEngineHasher::new(label)
}

pub fn domain_separated_hasher(label: &'static str) -> DomainSeparatedHasher<Blake256, TariEngineDomainSeparation> {
DomainSeparatedHasher::new(label)
pub fn create_key_pair() -> (PrivateKey, PublicKey) {
PublicKey::random_keypair(&mut OsRng)
}
2 changes: 1 addition & 1 deletion dan_layer/engine/src/instruction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{package::PackageId, wasm::WasmExecutionError};
use crate::{packager::PackageId, wasm::WasmExecutionError};

#[derive(Debug, thiserror::Error)]
pub enum InstructionError {
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use processor::InstructionProcessor;

mod signature;

use crate::{instruction::signature::InstructionSignature, package::PackageId};
use crate::{instruction::signature::InstructionSignature, packager::PackageId};

#[derive(Debug, Clone)]
pub enum Instruction {
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/instruction/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{collections::HashMap, sync::Arc};

use crate::{
instruction::{error::InstructionError, Instruction, InstructionSet},
package::{Package, PackageId},
packager::{Package, PackageId},
runtime::{Runtime, RuntimeInterface},
traits::Invokable,
wasm::{ExecutionResult, Process},
Expand Down
3 changes: 1 addition & 2 deletions dan_layer/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ pub mod models;
pub mod state;
pub mod wasm;

pub mod compile;
pub mod crypto;
pub mod instruction;
pub mod package;
pub mod packager;
pub mod runtime;
pub mod traits;

Expand Down
219 changes: 0 additions & 219 deletions dan_layer/engine/src/package.rs

This file was deleted.

29 changes: 29 additions & 0 deletions dan_layer/engine/src/packager/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::wasm::WasmExecutionError;

#[derive(Debug, thiserror::Error)]
pub enum PackageError {
#[error(transparent)]
WasmModuleError(#[from] WasmExecutionError),
}
30 changes: 30 additions & 0 deletions dan_layer/engine/src/packager/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

mod error;
pub use error::PackageError;

mod package;
pub use package::{Package, PackageBuilder, PackageId};

mod module_loader;
pub use module_loader::PackageModuleLoader;
30 changes: 30 additions & 0 deletions dan_layer/engine/src/packager/module_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::packager::PackageError;

pub trait PackageModuleLoader {
type Loaded;
type Error: Into<PackageError>;

fn load_module(&self) -> Result<Self::Loaded, Self::Error>;
}
Loading

0 comments on commit f286222

Please sign in to comment.