diff --git a/applications/tari_app_grpc/proto/validator_node.proto b/applications/tari_app_grpc/proto/validator_node.proto index b12fdaff31..b15cb97005 100644 --- a/applications/tari_app_grpc/proto/validator_node.proto +++ b/applications/tari_app_grpc/proto/validator_node.proto @@ -101,6 +101,7 @@ message InvokeReadMethodRequest{ uint32 template_id = 2; string method = 3; bytes args = 4; + bytes sender = 5; } message InvokeReadMethodResponse { @@ -119,6 +120,7 @@ message InvokeMethodRequest{ uint32 template_id = 2; string method = 3; bytes args = 4; + bytes sender = 5; } message InvokeMethodResponse { diff --git a/applications/tari_collectibles/src-tauri/src/clients/validator_node_client.rs b/applications/tari_collectibles/src-tauri/src/clients/validator_node_client.rs index d5f72d1473..11523fcbd6 100644 --- a/applications/tari_collectibles/src-tauri/src/clients/validator_node_client.rs +++ b/applications/tari_collectibles/src-tauri/src/clients/validator_node_client.rs @@ -61,6 +61,7 @@ impl GrpcValidatorNodeClient { template_id, method, args, + sender: PublicKey::default().to_vec(), }; debug!(target: LOG_TARGET, "req {:?}", req); let response = self @@ -93,6 +94,7 @@ impl GrpcValidatorNodeClient { template_id, method, args, + sender: PublicKey::default().to_vec(), }; debug!(target: LOG_TARGET, "req {:?}", req); let response = self diff --git a/applications/tari_validator_node/proto/dan/common.proto b/applications/tari_validator_node/proto/dan/common.proto index 392a45b449..28b030c537 100644 --- a/applications/tari_validator_node/proto/dan/common.proto +++ b/applications/tari_validator_node/proto/dan/common.proto @@ -21,8 +21,7 @@ message Instruction { uint32 template_id = 1; string method = 2; bytes args = 3; - // bytes token_id = 5; - // bytes signature = 6; + bytes sender = 4; } message InstructionSet{ diff --git a/applications/tari_validator_node/proto/dan/validator_node.proto b/applications/tari_validator_node/proto/dan/validator_node.proto index 0344e0d1a6..c0386d6ff1 100644 --- a/applications/tari_validator_node/proto/dan/validator_node.proto +++ b/applications/tari_validator_node/proto/dan/validator_node.proto @@ -49,6 +49,7 @@ message InvokeReadMethodRequest{ uint32 template_id = 2; string method = 3; bytes args = 4; + bytes sender = 5; } message InvokeReadMethodResponse { @@ -61,6 +62,7 @@ message InvokeMethodRequest{ uint32 template_id = 2; string method = 3; bytes args = 4; + bytes sender = 5; } message InvokeMethodResponse { diff --git a/applications/tari_validator_node/src/contract_worker_manager.rs b/applications/tari_validator_node/src/contract_worker_manager.rs index c19e6b8845..7dcedab425 100644 --- a/applications/tari_validator_node/src/contract_worker_manager.rs +++ b/applications/tari_validator_node/src/contract_worker_manager.rs @@ -55,7 +55,7 @@ use tari_dan_core::{ workers::ConsensusWorker, DigitalAssetError, }; -use tari_dan_storage_sqlite::{SqliteDbFactory, SqliteGlobalDbBackendAdapter, SqliteStorageService}; +use tari_dan_storage_sqlite::{global::SqliteGlobalDbBackendAdapter, SqliteDbFactory, SqliteStorageService}; use tari_p2p::{comms_connector::SubscriptionFactory, tari_message::TariMessageType}; use tari_service_framework::ServiceHandles; use tari_shutdown::ShutdownSignal; diff --git a/applications/tari_validator_node/src/dan_node.rs b/applications/tari_validator_node/src/dan_node.rs index 8ee4393009..1c880958b7 100644 --- a/applications/tari_validator_node/src/dan_node.rs +++ b/applications/tari_validator_node/src/dan_node.rs @@ -25,7 +25,7 @@ use std::sync::Arc; use tari_common::exit_codes::{ExitCode, ExitError}; use tari_comms::NodeIdentity; use tari_dan_core::{services::MempoolServiceHandle, storage::global::GlobalDb}; -use tari_dan_storage_sqlite::{SqliteDbFactory, SqliteGlobalDbBackendAdapter}; +use tari_dan_storage_sqlite::{global::SqliteGlobalDbBackendAdapter, SqliteDbFactory}; use tari_p2p::comms_connector::SubscriptionFactory; use tari_service_framework::ServiceHandles; use tari_shutdown::ShutdownSignal; diff --git a/applications/tari_validator_node/src/default_service_specification.rs b/applications/tari_validator_node/src/default_service_specification.rs index c73eae1685..98573e6d0e 100644 --- a/applications/tari_validator_node/src/default_service_specification.rs +++ b/applications/tari_validator_node/src/default_service_specification.rs @@ -37,9 +37,9 @@ use tari_dan_core::{ }, }; use tari_dan_storage_sqlite::{ + global::SqliteGlobalDbBackendAdapter, SqliteChainBackendAdapter, SqliteDbFactory, - SqliteGlobalDbBackendAdapter, SqliteStateDbBackendAdapter, SqliteStorageService, }; diff --git a/applications/tari_validator_node/src/grpc/validator_node_grpc_server.rs b/applications/tari_validator_node/src/grpc/validator_node_grpc_server.rs index 1dc3450e90..bf4c9eb7dc 100644 --- a/applications/tari_validator_node/src/grpc/validator_node_grpc_server.rs +++ b/applications/tari_validator_node/src/grpc/validator_node_grpc_server.rs @@ -26,7 +26,7 @@ use std::{ use futures::channel::mpsc; use tari_app_grpc::tari_rpc::{self as rpc, TransactionOutput}; -use tari_common_types::types::{FixedHash, Signature}; +use tari_common_types::types::{FixedHash, PublicKey, Signature}; use tari_comms::NodeIdentity; use tari_crypto::tari_utilities::ByteArray; use tari_dan_core::{ @@ -188,6 +188,7 @@ impl rpc::validator_node_ .map_err(|_| Status::invalid_argument("invalid template_id"))?, request.method.clone(), request.args.clone(), + PublicKey::from_bytes(&request.sender).map_err(|_| Status::invalid_argument("invalid sender"))?, ) .await { @@ -238,7 +239,12 @@ impl rpc::validator_node_ .map_err(|e| Status::internal(format!("Could not create state db: {}", e)))? { let state_db_reader = state.reader(); - let instruction = Instruction::new(template_id, request.method, request.args); + let instruction = Instruction::new( + template_id, + request.method, + request.args, + PublicKey::from_bytes(&request.sender).map_err(|_| Status::invalid_argument("invalid sender"))?, + ); let response_bytes = self .asset_processor .invoke_read_method(&instruction, &state_db_reader) @@ -255,7 +261,13 @@ impl rpc::validator_node_ // Forward to proxy let response_bytes = self .asset_proxy - .invoke_read_method(&contract_id, template_id, request.method, request.args) + .invoke_read_method( + &contract_id, + template_id, + request.method, + request.args, + PublicKey::from_bytes(&request.sender).map_err(|_| Status::invalid_argument("invalid sender"))?, + ) .await .map_err(|err| Status::internal(format!("Error calling proxied method:{}", err)))?; // TODO: Populate authority diff --git a/applications/tari_validator_node/src/main.rs b/applications/tari_validator_node/src/main.rs index 996f471c2e..13ab37b864 100644 --- a/applications/tari_validator_node/src/main.rs +++ b/applications/tari_validator_node/src/main.rs @@ -54,7 +54,7 @@ use tari_dan_core::{ services::{ConcreteAssetProcessor, ConcreteAssetProxy, MempoolServiceHandle, ServiceSpecification}, storage::{global::GlobalDb, DbFactory}, }; -use tari_dan_storage_sqlite::{SqliteDbFactory, SqliteGlobalDbBackendAdapter}; +use tari_dan_storage_sqlite::{global::SqliteGlobalDbBackendAdapter, SqliteDbFactory}; use tari_p2p::comms_connector::SubscriptionFactory; use tari_service_framework::ServiceHandles; use tari_shutdown::{Shutdown, ShutdownSignal}; diff --git a/applications/tari_validator_node/src/p2p/proto/conversions.rs b/applications/tari_validator_node/src/p2p/proto/conversions.rs index 6aa67abf78..ec4b3f29db 100644 --- a/applications/tari_validator_node/src/p2p/proto/conversions.rs +++ b/applications/tari_validator_node/src/p2p/proto/conversions.rs @@ -22,6 +22,7 @@ use std::convert::{TryFrom, TryInto}; +use tari_common_types::types::PublicKey; use tari_crypto::tari_utilities::ByteArray; use tari_dan_core::{ models::{ @@ -117,6 +118,7 @@ impl From<&Instruction> for proto::common::Instruction { template_id: source.template_id() as u32, method: source.method().to_string(), args: Vec::from(source.args()), + sender: source.sender().to_vec(), } } } @@ -217,7 +219,12 @@ impl TryFrom for Instruction { fn try_from(value: proto::common::Instruction) -> Result { let template_id = TemplateId::try_from(value.template_id).map_err(|err| err.to_string())?; - Ok(Self::new(template_id, value.method, value.args)) + Ok(Self::new( + template_id, + value.method, + value.args, + PublicKey::from_bytes(&value.sender).map_err(|e| format!("Invalid public key:{}", e))?, + )) } } diff --git a/applications/tari_validator_node/src/p2p/rpc/service_impl.rs b/applications/tari_validator_node/src/p2p/rpc/service_impl.rs index 5920da1899..681a77f371 100644 --- a/applications/tari_validator_node/src/p2p/rpc/service_impl.rs +++ b/applications/tari_validator_node/src/p2p/rpc/service_impl.rs @@ -36,6 +36,9 @@ 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 crate::p2p::{proto::validator_node as proto, rpc::ValidatorNodeRpcService}; pub struct ValidatorNodeRpcServiceImpl { @@ -100,6 +103,8 @@ where .map_err(|_| RpcStatus::bad_request("Invalid template_id"))?, request.method, request.args, + PublicKey::from_bytes(&request.sender) + .map_err(|_| RpcStatus::bad_request("Invalid public key for sender"))?, ); let response_bytes = self .asset_processor @@ -124,11 +129,11 @@ where .map_err(|_| RpcStatus::bad_request("Invalid template_id"))?, request.method.clone(), request.args.clone(), - // TokenId(request.token_id.clone()), - // TODO: put signature in here - // ComSig::default() - // create_com_sig_from_bytes(&request.signature) - // .map_err(|err| Status::invalid_argument("signature was not a valid comsig"))?, + PublicKey::from_bytes(&request.sender).map_err(|_| RpcStatus::bad_request("invalid sender"))?, /* TokenId(request.token_id.clone()), + * TODO: put signature in here + * ComSig::default() + * create_com_sig_from_bytes(&request.signature) + * .map_err(|err| Status::invalid_argument("signature was not a valid comsig"))?, */ ); debug!(target: LOG_TARGET, "Submitting instruction {} to mempool", instruction); let mut mempool_service = self.mempool_service.clone(); diff --git a/applications/tari_validator_node/src/p2p/services/rpc_client.rs b/applications/tari_validator_node/src/p2p/services/rpc_client.rs index bd9f651308..4e1a0121dd 100644 --- a/applications/tari_validator_node/src/p2p/services/rpc_client.rs +++ b/applications/tari_validator_node/src/p2p/services/rpc_client.rs @@ -58,6 +58,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, ValidatorNodeClientError> { debug!( target: LOG_TARGET, @@ -70,6 +71,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient { template_id: template_id as u32, method, args, + sender: sender.to_vec(), }; let response = client.invoke_read_method(request).await?; @@ -86,6 +88,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, ValidatorNodeClientError> { debug!( target: LOG_TARGET, @@ -98,6 +101,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient { template_id: template_id as u32, method, args, + sender: sender.to_vec(), }; let response = client.invoke_method(request).await?; diff --git a/dan_layer/core/src/models/instruction.rs b/dan_layer/core/src/models/instruction.rs index 1d0e72ad00..3830fe7ca5 100644 --- a/dan_layer/core/src/models/instruction.rs +++ b/dan_layer/core/src/models/instruction.rs @@ -23,7 +23,7 @@ use std::fmt::{Display, Formatter}; use digest::Digest; -use tari_common_types::types::FixedHash; +use tari_common_types::types::{FixedHash, PublicKey}; use tari_crypto::common::Blake256; use tari_utilities::hex::Hex; @@ -34,6 +34,7 @@ pub struct Instruction { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, // from: TokenId, // signature: ComSig, hash: FixedHash, @@ -50,6 +51,7 @@ impl Instruction { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, // from: TokenId, // _signature: ComSig, ) -> Self { @@ -57,6 +59,7 @@ impl Instruction { template_id, method, args, + sender, // from, // TODO: this is obviously wrong // signature: ComSig::default(), @@ -78,6 +81,10 @@ impl Instruction { &self.args } + pub fn sender(&self) -> PublicKey { + self.sender.clone() + } + // // TODO: rename to avoid use of from // pub fn from_owner(&self) -> &TokenId { // &self.from diff --git a/dan_layer/core/src/services/asset_proxy.rs b/dan_layer/core/src/services/asset_proxy.rs index b91ff510d9..079affdf95 100644 --- a/dan_layer/core/src/services/asset_proxy.rs +++ b/dan_layer/core/src/services/asset_proxy.rs @@ -51,6 +51,7 @@ pub trait AssetProxy: Send + Sync { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result<(), DigitalAssetError>; async fn invoke_read_method( @@ -59,6 +60,7 @@ pub trait AssetProxy: Send + Sync { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, DigitalAssetError>; } @@ -100,10 +102,11 @@ impl> ConcreteAsse template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, DigitalAssetError> { let mut client = self.validator_node_client_factory.create_client(member); let resp = client - .invoke_read_method(&contract_id, template_id, method, args) + .invoke_read_method(&contract_id, template_id, method, args, sender) .await?; Ok(resp) } @@ -115,10 +118,13 @@ impl> ConcreteAsse template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, DigitalAssetError> { debug!(target: LOG_TARGET, "Forwarding '{}' instruction to {}", member, method); let mut client = self.validator_node_client_factory.create_client(member); - let resp = client.invoke_method(&contract_id, template_id, method, args).await?; + let resp = client + .invoke_method(&contract_id, template_id, method, args, sender) + .await?; Ok(resp) } @@ -130,6 +136,7 @@ impl> ConcreteAsse template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, DigitalAssetError> { let mut base_node_client = self.base_node_client.clone(); let tip = base_node_client.get_tip_info().await?; @@ -172,6 +179,7 @@ impl> ConcreteAsse template_id, method.clone(), args.clone(), + sender.clone(), )); } @@ -193,6 +201,7 @@ impl> ConcreteAsse template_id, method.clone(), args.clone(), + sender.clone(), )); } @@ -221,6 +230,7 @@ impl> AssetProxy template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result<(), DigitalAssetError> { // check if we are processing this asset if self.db_factory.get_state_db(contract_id)?.is_some() { @@ -228,17 +238,25 @@ impl> AssetProxy template_id, method.clone(), args.clone(), - // TokenId(request.token_id.clone()), - // TODO: put signature in here - // ComSig::default() - // create_com_sig_from_bytes(&request.signature) - // .map_err(|err| Status::invalid_argument("signature was not a valid comsig"))?, + sender.clone(), /* TokenId(request.token_id.clone()), + * TODO: put signature in here + * ComSig::default() + * create_com_sig_from_bytes(&request.signature) + * .map_err(|err| Status::invalid_argument("signature was not a valid + * comsig"))?, */ ); let mut mempool = self.mempool.clone(); mempool.submit_instruction(instruction).await } else { let _result = self - .forward_to_committee(*contract_id, InvokeType::InvokeMethod, template_id, method, args) + .forward_to_committee( + *contract_id, + InvokeType::InvokeMethod, + template_id, + method, + args, + sender, + ) .await?; Ok(()) } @@ -250,8 +268,16 @@ impl> AssetProxy template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, DigitalAssetError> { - self.forward_to_committee(*contract_id, InvokeType::InvokeReadMethod, template_id, method, args) - .await + self.forward_to_committee( + *contract_id, + InvokeType::InvokeReadMethod, + template_id, + method, + args, + sender, + ) + .await } } diff --git a/dan_layer/core/src/services/mocks/mod.rs b/dan_layer/core/src/services/mocks/mod.rs index a5f71d57b4..fbed3ea1c4 100644 --- a/dan_layer/core/src/services/mocks/mod.rs +++ b/dan_layer/core/src/services/mocks/mod.rs @@ -382,6 +382,7 @@ impl ValidatorNodeRpcClient for MockValidatorNodeClient { _template_id: TemplateId, _method: String, _args: Vec, + _sender: PublicKey, ) -> Result>, ValidatorNodeClientError> { Ok(None) } @@ -392,6 +393,7 @@ impl ValidatorNodeRpcClient for MockValidatorNodeClient { _template_id: TemplateId, _method: String, _args: Vec, + _sender: PublicKey, ) -> Result>, ValidatorNodeClientError> { Ok(None) } diff --git a/dan_layer/core/src/services/validator_node_rpc_client.rs b/dan_layer/core/src/services/validator_node_rpc_client.rs index c53e6aae53..b5f88c39f2 100644 --- a/dan_layer/core/src/services/validator_node_rpc_client.rs +++ b/dan_layer/core/src/services/validator_node_rpc_client.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use async_trait::async_trait; -use tari_common_types::types::FixedHash; +use tari_common_types::types::{FixedHash, PublicKey}; use tari_comms::{ connectivity::ConnectivityError, protocol::rpc::{RpcError, RpcStatus}, @@ -48,6 +48,7 @@ pub trait ValidatorNodeRpcClient: Send + Sync { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, ValidatorNodeClientError>; async fn invoke_method( @@ -56,6 +57,7 @@ pub trait ValidatorNodeRpcClient: Send + Sync { template_id: TemplateId, method: String, args: Vec, + sender: PublicKey, ) -> Result>, ValidatorNodeClientError>; async fn get_sidechain_blocks( diff --git a/dan_layer/core/src/templates/tip002_template.rs b/dan_layer/core/src/templates/tip002_template.rs index b94374cd7f..0c54aeb3ab 100644 --- a/dan_layer/core/src/templates/tip002_template.rs +++ b/dan_layer/core/src/templates/tip002_template.rs @@ -21,6 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use prost::Message; +use tari_common_types::types::PublicKey; use tari_core::transactions::transaction_components::TemplateParameter; use tari_dan_common_types::proto::tips::tip002; use tari_utilities::{hex::Hex, ByteArray}; @@ -36,6 +37,7 @@ pub fn initial_instructions(template_param: &TemplateParameter) -> InstructionSe TemplateId::Tip002, "init".to_string(), template_param.template_data.clone(), + PublicKey::default(), )]) } diff --git a/dan_layer/storage_sqlite/README.md b/dan_layer/storage_sqlite/README.md new file mode 100644 index 0000000000..06c3d5f598 --- /dev/null +++ b/dan_layer/storage_sqlite/README.md @@ -0,0 +1,12 @@ +To run migrations: + +``` +diesel migration run --database-url temp.sqlite --config-file .\diesel.toml +``` + +To run global migrations +To run migrations: + +``` +diesel migration run --database-url temp-glob.sqlite --config-file .\diesel-global.toml --migration-dir ./global_db_migrations +``` diff --git a/dan_layer/storage_sqlite/diesel-global.toml b/dan_layer/storage_sqlite/diesel-global.toml new file mode 100644 index 0000000000..26f6ef30b6 --- /dev/null +++ b/dan_layer/storage_sqlite/diesel-global.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/global/schema.rs" diff --git a/dan_layer/storage_sqlite/migrations/2021-11-02-185150_create_nodes/up.sql b/dan_layer/storage_sqlite/migrations/2021-11-02-185150_create_nodes/up.sql index 7a25906226..93eeb6ed58 100644 --- a/dan_layer/storage_sqlite/migrations/2021-11-02-185150_create_nodes/up.sql +++ b/dan_layer/storage_sqlite/migrations/2021-11-02-185150_create_nodes/up.sql @@ -14,6 +14,7 @@ create table instructions ( template_id int not null, method text not null, args blob not null, + sender blob not null, foreign key (node_id) references nodes(id) ); diff --git a/dan_layer/storage_sqlite/src/global/mod.rs b/dan_layer/storage_sqlite/src/global/mod.rs new file mode 100644 index 0000000000..f615a46d78 --- /dev/null +++ b/dan_layer/storage_sqlite/src/global/mod.rs @@ -0,0 +1,26 @@ +// 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. + +pub mod models; +pub mod schema; +mod sqlite_global_db_backend_adapter; +pub use sqlite_global_db_backend_adapter::SqliteGlobalDbBackendAdapter; diff --git a/dan_layer/storage_sqlite/src/models/metadata.rs b/dan_layer/storage_sqlite/src/global/models/metadata.rs similarity index 98% rename from dan_layer/storage_sqlite/src/models/metadata.rs rename to dan_layer/storage_sqlite/src/global/models/metadata.rs index ede758cf41..79075a6c3b 100644 --- a/dan_layer/storage_sqlite/src/models/metadata.rs +++ b/dan_layer/storage_sqlite/src/global/models/metadata.rs @@ -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::schema::*; +use crate::global::schema::*; #[derive(Queryable, Insertable, Identifiable)] #[table_name = "metadata"] diff --git a/dan_layer/storage_sqlite/src/global/models/mod.rs b/dan_layer/storage_sqlite/src/global/models/mod.rs new file mode 100644 index 0000000000..64826ec9ef --- /dev/null +++ b/dan_layer/storage_sqlite/src/global/models/mod.rs @@ -0,0 +1,23 @@ +// 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. + +pub mod metadata; diff --git a/dan_layer/storage_sqlite/src/global/schema.rs b/dan_layer/storage_sqlite/src/global/schema.rs new file mode 100644 index 0000000000..a17b0f45d5 --- /dev/null +++ b/dan_layer/storage_sqlite/src/global/schema.rs @@ -0,0 +1,28 @@ +// 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. + +table! { + metadata (key_name) { + key_name -> Binary, + value -> Binary, + } +} diff --git a/dan_layer/storage_sqlite/src/sqlite_global_db_backend_adapter.rs b/dan_layer/storage_sqlite/src/global/sqlite_global_db_backend_adapter.rs similarity index 95% rename from dan_layer/storage_sqlite/src/sqlite_global_db_backend_adapter.rs rename to dan_layer/storage_sqlite/src/global/sqlite_global_db_backend_adapter.rs index bf1574dba3..4f37c7a5a4 100644 --- a/dan_layer/storage_sqlite/src/sqlite_global_db_backend_adapter.rs +++ b/dan_layer/storage_sqlite/src/global/sqlite_global_db_backend_adapter.rs @@ -23,7 +23,7 @@ use diesel::{prelude::*, Connection, RunQueryDsl, SqliteConnection}; use tari_dan_core::storage::global::{GlobalDbBackendAdapter, GlobalDbMetadataKey}; -use crate::{error::SqliteStorageError, models::metadata::Metadata, SqliteTransaction}; +use crate::{error::SqliteStorageError, global::models::metadata::Metadata, SqliteTransaction}; #[derive(Clone)] pub struct SqliteGlobalDbBackendAdapter { @@ -60,7 +60,7 @@ impl GlobalDbBackendAdapter for SqliteGlobalDbBackendAdapter { } fn set_data(&self, key: GlobalDbMetadataKey, value: &[u8]) -> Result<(), Self::Error> { - use crate::schema::metadata; + use crate::global::schema::metadata; let tx = self.create_transaction()?; match self.get_data_with_connection(&key, &tx) { @@ -90,7 +90,7 @@ impl GlobalDbBackendAdapter for SqliteGlobalDbBackendAdapter { } fn get_data(&self, key: GlobalDbMetadataKey) -> Result>, Self::Error> { - use crate::schema::metadata::dsl; + use crate::global::schema::metadata::dsl; let connection = SqliteConnection::establish(self.database_url.as_str())?; let row: Option = dsl::metadata @@ -110,7 +110,7 @@ impl GlobalDbBackendAdapter for SqliteGlobalDbBackendAdapter { key: &GlobalDbMetadataKey, tx: &Self::BackendTransaction, ) -> Result>, Self::Error> { - use crate::schema::metadata::dsl; + use crate::global::schema::metadata::dsl; let row: Option = dsl::metadata .find(key.as_key_bytes()) diff --git a/dan_layer/storage_sqlite/src/lib.rs b/dan_layer/storage_sqlite/src/lib.rs index c2449fd418..472fe2d5f8 100644 --- a/dan_layer/storage_sqlite/src/lib.rs +++ b/dan_layer/storage_sqlite/src/lib.rs @@ -34,10 +34,9 @@ pub use sqlite_transaction::SqliteTransaction; mod sqlite_db_factory; pub use sqlite_db_factory::SqliteDbFactory; mod models; -mod sqlite_global_db_backend_adapter; mod sqlite_state_db_backend_adapter; -pub use sqlite_global_db_backend_adapter::SqliteGlobalDbBackendAdapter; pub use sqlite_state_db_backend_adapter::SqliteStateDbBackendAdapter; +pub mod global; mod sqlite_storage_service; pub use sqlite_storage_service::SqliteStorageService; diff --git a/dan_layer/storage_sqlite/src/models/instruction.rs b/dan_layer/storage_sqlite/src/models/instruction.rs index 8e6c6a1c2f..d9909b8c1f 100644 --- a/dan_layer/storage_sqlite/src/models/instruction.rs +++ b/dan_layer/storage_sqlite/src/models/instruction.rs @@ -23,6 +23,9 @@ use std::convert::{TryFrom, TryInto}; +use tari_common_types::types::PublicKey; +use tari_utilities::ByteArray; + use crate::{error::SqliteStorageError, schema::*}; #[derive(Debug, Identifiable, Queryable)] @@ -33,6 +36,7 @@ pub struct Instruction { pub template_id: i32, pub method: String, pub args: Vec, + pub sender: Vec, } impl TryFrom for tari_dan_core::models::Instruction { @@ -40,7 +44,12 @@ impl TryFrom for tari_dan_core::models::Instruction { fn try_from(instruction: Instruction) -> Result { let template_id = instruction.template_id.try_into()?; - Ok(Self::new(template_id, instruction.method, instruction.args)) + Ok(Self::new( + template_id, + instruction.method, + instruction.args, + PublicKey::from_bytes(&instruction.sender).expect("invalid public key"), + )) } } @@ -52,4 +61,5 @@ pub struct NewInstruction { pub template_id: i32, pub method: String, pub args: Vec, + pub sender: Vec, } diff --git a/dan_layer/storage_sqlite/src/models/mod.rs b/dan_layer/storage_sqlite/src/models/mod.rs index b31072f478..00613a6de0 100644 --- a/dan_layer/storage_sqlite/src/models/mod.rs +++ b/dan_layer/storage_sqlite/src/models/mod.rs @@ -22,7 +22,6 @@ pub mod instruction; pub mod locked_qc; -pub mod metadata; pub mod node; pub mod prepare_qc; pub mod state_key; diff --git a/dan_layer/storage_sqlite/src/schema.rs b/dan_layer/storage_sqlite/src/schema.rs index b6d81adf9f..0c2761b269 100644 --- a/dan_layer/storage_sqlite/src/schema.rs +++ b/dan_layer/storage_sqlite/src/schema.rs @@ -1,5 +1,24 @@ -// Copyright 2022 The Tari Project -// SPDX-License-Identifier: BSD-3-Clause +// 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. table! { instructions (id) { @@ -9,6 +28,7 @@ table! { template_id -> Integer, method -> Text, args -> Binary, + sender -> Binary, } } @@ -71,13 +91,6 @@ table! { } } -table! { - metadata (key_name) { - key_name -> Binary, - value -> Binary, - } -} - joinable!(instructions -> nodes (node_id)); allow_tables_to_appear_in_same_query!( diff --git a/dan_layer/storage_sqlite/src/sqlite_chain_backend_adapter.rs b/dan_layer/storage_sqlite/src/sqlite_chain_backend_adapter.rs index 7535f23166..7eebbbc743 100644 --- a/dan_layer/storage_sqlite/src/sqlite_chain_backend_adapter.rs +++ b/dan_layer/storage_sqlite/src/sqlite_chain_backend_adapter.rs @@ -28,6 +28,7 @@ use tari_dan_core::{ models::{HotStuffMessageType, QuorumCertificate, Signature, TariDanPayload, TreeNodeHash, ViewId}, storage::chain::{ChainDbBackendAdapter, DbInstruction, DbNode, DbQc}, }; +use tari_utilities::ByteArray; use crate::{ error::SqliteStorageError, @@ -425,6 +426,7 @@ impl ChainDbBackendAdapter for SqliteChainBackendAdapter { template_id: item.instruction.template_id() as i32, method: item.instruction.method().to_string(), args: Vec::from(item.instruction.args()), + sender: item.instruction.sender().to_vec(), }; diesel::insert_into(instructions::table) .values(new_instruction) diff --git a/dan_layer/storage_sqlite/src/sqlite_db_factory.rs b/dan_layer/storage_sqlite/src/sqlite_db_factory.rs index 4d627086a8..6c1c823eb6 100644 --- a/dan_layer/storage_sqlite/src/sqlite_db_factory.rs +++ b/dan_layer/storage_sqlite/src/sqlite_db_factory.rs @@ -31,7 +31,7 @@ use tari_utilities::hex::Hex; use crate::{ error::SqliteStorageError, - sqlite_global_db_backend_adapter::SqliteGlobalDbBackendAdapter, + global::SqliteGlobalDbBackendAdapter, sqlite_state_db_backend_adapter::SqliteStateDbBackendAdapter, SqliteChainBackendAdapter, };