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

refactor: fix db structure #3616

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions applications/tari_app_grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
],
&["proto"],
)?;

Ok(())
}
15 changes: 14 additions & 1 deletion applications/tari_app_grpc/proto/validator_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ service ValidatorNode {
rpc GetMetadata(GetMetadataRequest) returns (GetMetadataResponse);
rpc GetTokenData(GetTokenDataRequest) returns (GetTokenDataResponse);
rpc ExecuteInstruction(ExecuteInstructionRequest) returns (ExecuteInstructionResponse);
rpc InvokeReadMethod(InvokeReadMethodRequest) returns (InvokeReadMethodResponse);
}

message GetMetadataRequest {
Expand Down Expand Up @@ -57,11 +58,23 @@ message ExecuteInstructionRequest{
bytes asset_public_key = 1;
uint32 template_id = 2;
string method = 3;
repeated bytes args = 4;
bytes args = 4;
// bytes token_id = 5;
// bytes signature = 6;
}

message ExecuteInstructionResponse {
string status = 1;
optional bytes result = 2;
}

message InvokeReadMethodRequest{
bytes asset_public_key = 1;
uint32 template_id = 2;
string method = 3;
bytes args = 4;
}

message InvokeReadMethodResponse {
optional bytes result = 1;
}
3 changes: 3 additions & 0 deletions applications/tari_collectibles/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", branch
tari_key_manager = { path = "../../../base_layer/key_manager" }
tari_mmr = { path = "../../../base_layer/mmr"}
tari_utilities = "*"
tari_dan_common_types = { path = "../../../dan_layer/common_types"}

blake2 = "^0.9.0"
futures = "0.3.17"
Expand All @@ -33,6 +34,8 @@ diesel = { version = "1.4.8", features = ["sqlite"] }
diesel_migrations = "1.4.0"
thiserror = "1.0.30"
uuid = { version = "0.8.2", features = ["serde"] }
prost = "0.8"
prost-types = "0.8"

[features]
default = [ "custom-protocol" ]
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
-- Your SQL goes here
create table assets (
id blob not null primary key,
asset_public_key blob not null unique,
name text,
description text,
image text,
committee_length integer not null,
committee_pub_keys blob not null
);

create table wallets (
id blob not null primary key,
name text,
cipher_seed blob not null unique
);
);

create table asset_wallets (
id blob not null primary key,
asset_id blob not null references assets(id),
wallet_id blob not null references wallets(id)
);

create table addresses (
id blob not null primary key,
asset_wallet_id blob not null references asset_wallets (id),
name text,
public_key blob not null,
key_manager_path TEXT not null
);

create table tip002_address (
id blob not null primary key,
address_id blob not null references addresses(id),
balance bigint not null,
at_height bigint
);

create table issued_assets (
id blob not null primary key,
wallet_id blob not null references wallets (id),
public_key blob not null,
key_manager_path text not null,
is_draft boolean not null
);

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create table key_indices (
id blob not null primary key,
branch_seed text not null unique,
last_index integer not null
last_index BigInt not null
);
22 changes: 19 additions & 3 deletions applications/tari_collectibles/src-tauri/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

use crate::{
clients::{BaseNodeClient, GrpcValidatorNodeClient, WalletClient},
error::CollectiblesError,
providers::{mocks::MockKeyManagerProvider, KeyManagerProvider},
settings::Settings,
storage::{
sqlite::{SqliteCollectiblesStorage, SqliteDbFactory},
Expand All @@ -36,6 +38,7 @@ use tari_key_manager::{
key_manager::{DerivedKey as DerivedKeyGeneric, KeyManager as GenericKeyManager},
};
use tauri::async_runtime::RwLock;
use uuid::Uuid;

type KeyDigest = Blake256;
pub type DerivedKey = DerivedKeyGeneric<PrivateKey>;
Expand All @@ -45,6 +48,7 @@ pub struct AppState {
config: Settings,
db_factory: SqliteDbFactory,
asset_key_manager: KeyManager,
current_wallet_id: Option<Uuid>,
}

#[derive(Clone)]
Expand All @@ -60,6 +64,7 @@ impl ConcurrentAppState {
db_factory: SqliteDbFactory::new(settings.data_dir.as_path()),
config: settings,
asset_key_manager: KeyManager::new(),
current_wallet_id: None,
})),
}
}
Expand Down Expand Up @@ -90,7 +95,7 @@ impl ConcurrentAppState {
WalletClient::new(self.inner.read().await.config.wallet_grpc_address.clone())
}

pub async fn connect_base_node_client(&self) -> Result<BaseNodeClient, String> {
pub async fn connect_base_node_client(&self) -> Result<BaseNodeClient, CollectiblesError> {
let lock = self.inner.read().await;
let client =
BaseNodeClient::connect(format!("http://{}", lock.config.base_node_grpc_address)).await?;
Expand All @@ -99,8 +104,7 @@ impl ConcurrentAppState {

pub async fn connect_validator_node_client(
&self,
_public_key: PublicKey,
) -> Result<GrpcValidatorNodeClient, String> {
) -> Result<GrpcValidatorNodeClient, CollectiblesError> {
// todo: convert this GRPC to tari comms
let lock = self.inner.read().await;
let client = GrpcValidatorNodeClient::connect(format!(
Expand All @@ -114,4 +118,16 @@ impl ConcurrentAppState {
pub async fn create_db(&self) -> Result<SqliteCollectiblesStorage, StorageError> {
self.inner.read().await.db_factory.create_db()
}

pub async fn key_manager(&self) -> impl KeyManagerProvider {
MockKeyManagerProvider {}
}

pub async fn current_wallet_id(&self) -> Option<Uuid> {
self.inner.read().await.current_wallet_id
}

pub async fn set_current_wallet_id(&self, wallet_id: Uuid) {
self.inner.write().await.current_wallet_id = Some(wallet_id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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::error::CollectiblesError;
use futures::StreamExt;
use tari_app_grpc::tari_rpc as grpc;
use tari_common_types::types::PublicKey;
Expand All @@ -30,14 +31,13 @@ pub struct BaseNodeClient {
}

impl BaseNodeClient {
pub async fn connect(endpoint: String) -> Result<Self, String> {
pub async fn connect(endpoint: String) -> Result<Self, CollectiblesError> {
let client = grpc::base_node_client::BaseNodeClient::connect(endpoint.clone())
.await
.map_err(|err| {
format!(
"No connection to wallet. Is it running with grpc on '{}' ? Error: {}",
endpoint, err
)
.map_err(|err| CollectiblesError::ClientConnectionError {
client: "wallet",
address: endpoint,
error: err.to_string(),
})?;

Ok(Self { client })
Expand All @@ -47,18 +47,24 @@ impl BaseNodeClient {
&mut self,
offset: u64,
count: u64,
) -> Result<Vec<grpc::ListAssetRegistrationsResponse>, String> {
) -> Result<Vec<grpc::ListAssetRegistrationsResponse>, CollectiblesError> {
let client = self.client_mut();
let request = grpc::ListAssetRegistrationsRequest { offset, count };
let mut stream = client
.list_asset_registrations(request)
.await
.map(|response| response.into_inner())
.map_err(|s| format!("Could not get register assets: {}", s))?;
.map_err(|source| CollectiblesError::ClientRequestError {
request: "list_asset_registrations".to_string(),
source,
})?;

let mut assets = vec![];
while let Some(result) = stream.next().await {
let asset = result.map_err(|err| err.to_string())?;
let asset = result.map_err(|source| CollectiblesError::ClientRequestError {
request: "list_asset_registrations".to_string(),
source,
})?;
assets.push(asset);
}

Expand All @@ -68,7 +74,7 @@ impl BaseNodeClient {
pub async fn get_asset_metadata(
&mut self,
asset_public_key: &PublicKey,
) -> Result<grpc::GetAssetMetadataResponse, String> {
) -> Result<grpc::GetAssetMetadataResponse, CollectiblesError> {
let client = self.client_mut();
let request = grpc::GetAssetMetadataRequest {
asset_public_key: Vec::from(asset_public_key.as_bytes()),
Expand All @@ -78,7 +84,10 @@ impl BaseNodeClient {
.get_asset_metadata(request)
.await
.map(|response| response.into_inner())
.map_err(|s| format!("Could not get asset metadata: {}", s))?;
.map_err(|s| CollectiblesError::ClientRequestError {
request: "get_asset_metadata".to_string(),
source: s,
})?;
dbg!(&response);
Ok(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
// 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::error::CollectiblesError;
use tari_app_grpc::tari_rpc as grpc;
use tari_common_types::types::PublicKey;
use tari_utilities::ByteArray;

pub trait ValidatorNodeClient {}

Expand All @@ -28,17 +31,43 @@ pub struct GrpcValidatorNodeClient {
}

impl GrpcValidatorNodeClient {
pub async fn connect(endpoint: String) -> Result<Self, String> {
pub async fn connect(endpoint: String) -> Result<Self, CollectiblesError> {
let s = Self {
client: grpc::validator_node_client::ValidatorNodeClient::connect(endpoint.clone())
.await
.map_err(|e| {
format!(
"No connection to validator node. Is it running with gRPC on '{}'? Error: {}",
endpoint, e
)
.map_err(|e| CollectiblesError::ClientConnectionError {
client: "validator_node",
address: endpoint,
error: e.to_string(),
})?,
};
Ok(s)
}

pub async fn invoke_read_method(
&mut self,
asset_public_key: PublicKey,
template_id: u32,
method: String,
args: Vec<u8>,
) -> Result<Option<Vec<u8>>, CollectiblesError> {
let req = grpc::InvokeReadMethodRequest {
asset_public_key: Vec::from(asset_public_key.as_bytes()),
template_id,
method,
args,
};
dbg!(&req);
let response = self
.client
.invoke_read_method(req)
.await
.map(|resp| resp.into_inner())
.map_err(|e| CollectiblesError::ClientRequestError {
source: e,
request: "invoke_read_method".to_string(),
})?;
dbg!(&response);
Ok(response.result)
}
}
Loading