Skip to content

Commit

Permalink
feat: add tari engine for flow and wasm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Jun 28, 2022
1 parent 6c116ac commit 470bd6a
Show file tree
Hide file tree
Showing 108 changed files with 2,405 additions and 1,339 deletions.
667 changes: 661 additions & 6 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions applications/tari_validator_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tari_dan_core = { path = "../../dan_layer/core" }
tari_dan_storage_sqlite = { path = "../../dan_layer/storage_sqlite" }
tari_dan_common_types = { path = "../../dan_layer/common_types" }
tari_common_types = { path = "../../base_layer/common_types" }
tari_dan_engine = { path = "../../dan_layer/engine"}

anyhow = "1.0.53"
async-trait = "0.1.50"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ impl ContractWorkerManager {
checkpoint_unique_id: vec![],
initial_state: Default::default(),
template_parameters: vec![],
wasm_modules: vec![],
wasm_functions: vec![],
flow_functions: vec![],
},
node_identity,
mempool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use tari_app_grpc::{
};
use tari_common_types::types::{FixedHash, PublicKey, Signature};
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_core::{models::StateRoot, services::WalletClient, DigitalAssetError};
use tari_dan_core::{services::WalletClient, DigitalAssetError};
use tari_dan_engine::state::models::StateRoot;

type Inner = grpc::wallet_client::WalletClient<tonic::transport::Channel>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ use tari_common_types::types::{FixedHash, PublicKey, Signature};
use tari_comms::NodeIdentity;
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_core::{
models::Instruction,
services::{AssetProcessor, AssetProxy, ServiceSpecification, WalletClient},
storage::DbFactory,
};
use tari_dan_engine::instructions::Instruction;
use tokio::{task, time};
use tonic::{Request, Response, Status};

Expand Down
42 changes: 21 additions & 21 deletions applications/tari_validator_node/src/p2p/proto/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ use std::convert::{TryFrom, TryInto};

use tari_common_types::types::PublicKey;
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_core::{
models::{
CheckpointData,
HotStuffMessage,
HotStuffMessageType,
HotStuffTreeNode,
Instruction,
InstructionSet,
KeyValue,
Node,
QuorumCertificate,
SideChainBlock,
Signature,
StateOpLogEntry,
StateRoot,
TariDanPayload,
TemplateId,
TreeNodeHash,
ViewId,
use tari_dan_common_types::TemplateId;
use tari_dan_core::models::{
CheckpointData,
HotStuffMessage,
HotStuffMessageType,
HotStuffTreeNode,
InstructionSet,
Node,
QuorumCertificate,
SideChainBlock,
Signature,
TariDanPayload,
TreeNodeHash,
ViewId,
};
use tari_dan_engine::{
instructions::Instruction,
state::{
models::{KeyValue, StateOpLogEntry, StateRoot},
DbStateOpLogEntry,
},
storage::state::DbStateOpLogEntry,
};

use crate::p2p::proto;
Expand Down Expand Up @@ -218,7 +218,7 @@ impl TryFrom<proto::common::Instruction> for Instruction {
type Error = String;

fn try_from(value: proto::common::Instruction) -> Result<Self, Self::Error> {
let template_id = TemplateId::try_from(value.template_id).map_err(|err| err.to_string())?;
let template_id = TemplateId::try_from(value.template_id)?;
Ok(Self::new(
template_id,
value.method,
Expand Down
6 changes: 4 additions & 2 deletions applications/tari_validator_node/src/p2p/rpc/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ use tari_comms::{
utils,
};
use tari_dan_core::{
models::{Instruction, TreeNodeHash},
models::TreeNodeHash,
services::{AssetProcessor, MempoolService},
storage::{state::StateDbUnitOfWorkReader, DbFactory},
storage::DbFactory,
};
use tari_dan_engine::state::StateDbUnitOfWorkReader;
use tokio::{sync::mpsc, task};

const LOG_TARGET: &str = "vn::p2p::rpc";

use tari_common_types::types::PublicKey;
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_engine::instructions::Instruction;

use crate::p2p::{proto::validator_node as proto, rpc::ValidatorNodeRpcService};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ use tari_common_types::types::{FixedHash, PublicKey};
use tari_comms::PeerConnection;
use tari_comms_dht::DhtRequester;
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_common_types::TemplateId;
use tari_dan_core::{
models::{Node, SchemaState, SideChainBlock, StateOpLogEntry, TemplateId, TreeNodeHash},
models::{Node, SideChainBlock, TreeNodeHash},
services::{ValidatorNodeClientError, ValidatorNodeClientFactory, ValidatorNodeRpcClient},
};
use tari_dan_engine::state::models::{SchemaState, StateOpLogEntry};
use tokio_stream::StreamExt;

use crate::p2p::{proto::validator_node as proto, rpc};
Expand Down
4 changes: 4 additions & 0 deletions dan_layer/common_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
// SPDX-License-Identifier: BSD-3-Clause

pub mod proto;
pub mod storage;

mod template_id;
pub use template_id::TemplateId;
6 changes: 6 additions & 0 deletions dan_layer/common_types/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

mod unit_of_work_tracker;

pub use unit_of_work_tracker::UnitOfWorkTracker;
47 changes: 47 additions & 0 deletions dan_layer/common_types/src/storage/unit_of_work_tracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
RwLock,
RwLockReadGuard,
RwLockWriteGuard,
};

#[derive(Debug)]
pub struct UnitOfWorkTracker<TItem> {
item: Arc<RwLock<TItem>>,
is_dirty: Arc<AtomicBool>,
}

impl<TItem> Clone for UnitOfWorkTracker<TItem> {
fn clone(&self) -> Self {
Self {
item: self.item.clone(),
is_dirty: self.is_dirty.clone(),
}
}
}

impl<TItem> UnitOfWorkTracker<TItem> {
pub fn new(item: TItem, is_dirty: bool) -> Self {
Self {
item: Arc::new(RwLock::new(item)),
is_dirty: Arc::new(AtomicBool::new(is_dirty)),
}
}

pub fn get(&self) -> RwLockReadGuard<TItem> {
self.item.read().unwrap()
}

pub fn get_mut(&self) -> RwLockWriteGuard<TItem> {
self.is_dirty.store(true, Ordering::SeqCst);
self.item.write().unwrap()
}

pub fn is_dirty(&self) -> bool {
self.is_dirty.load(Ordering::SeqCst)
}
}
70 changes: 70 additions & 0 deletions dan_layer/common_types/src/template_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::{
convert::TryFrom,
fmt::{Display, Formatter},
str::FromStr,
};

#[derive(Copy, Clone, Debug)]
pub enum TemplateId {
Tip002 = 2,
Tip003 = 3,
Tip004 = 4,
Tip721 = 721,
EditableMetadata = 20,
}

impl FromStr for TemplateId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Tip002" => Ok(TemplateId::Tip002),
"Tip003" => Ok(TemplateId::Tip003),
"Tip004" => Ok(TemplateId::Tip004),
"Tip721" => Ok(TemplateId::Tip721),
"EditableMetadata" => Ok(TemplateId::EditableMetadata),
_ => {
println!("Unrecognised template");
Err(format!("Unrecognised template ID '{}'", s))
},
}
}
}

impl TryFrom<u32> for TemplateId {
type Error = String;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
2 => Ok(TemplateId::Tip002),
3 => Ok(TemplateId::Tip003),
4 => Ok(TemplateId::Tip004),
721 => Ok(TemplateId::Tip721),
_ => Err(format!("Unknown value: {}", value)),
}
}
}

impl TryFrom<i32> for TemplateId {
type Error = String;

fn try_from(value: i32) -> Result<Self, Self::Error> {
u32::try_from(value)
.map_err(|_| {
format!(
"Could not convert to TemplateId because it was not a valid u32:{}",
value
)
})?
.try_into()
}
}

impl Display for TemplateId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
4 changes: 1 addition & 3 deletions dan_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tari_core = {path = "../../base_layer/core"}
tari_dan_common_types = {path = "../common_types"}
tari_common_types = {path = "../../base_layer/common_types"}
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.4" }
tari_dan_engine = { path = "../engine"}

anyhow = "1.0.53"
async-trait = "0.1.50"
Expand All @@ -39,9 +40,6 @@ tokio = { version="1.10", features = ["macros", "time"]}
tokio-stream = { version = "0.1.7", features = ["sync"] }
tonic = "0.6.2"

# saving of patricia tree
patricia_tree = { version = "0.3.0", features = ["binary-format"] }
bytecodec = { version = "0.4.14", features = ["bincode_codec"] }
serde_json = "1.0.64"

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions dan_layer/core/src/digital_assets_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use prost::DecodeError;
use tari_comms_dht::outbound::DhtOutboundError;
use tari_crypto::ristretto::RistrettoPublicKey;
use tari_dan_engine::state::error::StateStorageError;
use thiserror::Error;
use tokio::sync::mpsc::error::SendError;

Expand Down Expand Up @@ -105,6 +106,8 @@ pub enum DigitalAssetError {
SendError(#[from] Box<SendError<(RistrettoPublicKey, HotStuffMessage<TariDanPayload>)>>),
#[error("Invalid committee public key hex")]
InvalidCommitteePublicKeyHex,
#[error("State storage error:{0}")]
StateStorageError(#[from] StateStorageError),
}

impl From<lmdb_zero::Error> for DigitalAssetError {
Expand Down
34 changes: 11 additions & 23 deletions dan_layer/core/src/models/asset_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
use serde::{self, Deserialize, Serialize};
use tari_common_types::types::FixedHash;
use tari_core::transactions::transaction_components::TemplateParameter;
use tari_dan_engine::{
function_definitions::{FlowFunctionDefinition, WasmFunctionDefinition},
state::models::SchemaState,
wasm::WasmModuleDefinition,
};

use crate::helpers::deserialize_from_hex;

Expand All @@ -40,6 +45,9 @@ pub struct AssetDefinition {
pub checkpoint_unique_id: Vec<u8>,
pub initial_state: InitialState,
pub template_parameters: Vec<TemplateParameter>,
pub wasm_modules: Vec<WasmModuleDefinition>,
pub wasm_functions: Vec<WasmFunctionDefinition>,
pub flow_functions: Vec<FlowFunctionDefinition>,
}

impl Default for AssetDefinition {
Expand All @@ -52,6 +60,9 @@ impl Default for AssetDefinition {
phase_timeout: 30,
initial_state: Default::default(),
template_parameters: vec![],
wasm_modules: vec![],
wasm_functions: vec![],
flow_functions: vec![],
}
}
}
Expand All @@ -66,26 +77,3 @@ impl AssetDefinition {
pub struct InitialState {
pub schemas: Vec<SchemaState>,
}

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct SchemaState {
pub name: String,
pub items: Vec<KeyValue>,
}

impl SchemaState {
pub fn new(name: String, items: Vec<KeyValue>) -> Self {
Self { name, items }
}

pub fn push_key_value(&mut self, key_value: KeyValue) -> &mut Self {
self.items.push(key_value);
self
}
}

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct KeyValue {
pub key: Vec<u8>,
pub value: Vec<u8>,
}
3 changes: 2 additions & 1 deletion dan_layer/core/src/models/hot_stuff_tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

use digest::{Digest, FixedOutput};
use tari_crypto::common::Blake256;
use tari_dan_engine::state::models::StateRoot;

use crate::models::{Payload, StateRoot, TreeNodeHash};
use crate::models::{Payload, TreeNodeHash};

#[derive(Debug, Clone)]
pub struct HotStuffTreeNode<TPayload: Payload> {
Expand Down
3 changes: 2 additions & 1 deletion dan_layer/core/src/models/instruction_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use std::{convert::TryFrom, hash::Hash, iter::FromIterator};

use tari_common_types::types::FixedHash;
use tari_crypto::common::Blake256;
use tari_dan_engine::instructions::Instruction;
use tari_mmr::MerkleMountainRange;

use crate::models::{ConsensusHash, Instruction};
use crate::models::ConsensusHash;

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct InstructionSetHash(FixedHash);
Expand Down
Loading

0 comments on commit 470bd6a

Please sign in to comment.