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

feat: add sender to instructions #4234

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions applications/tari_app_grpc/proto/validator_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ message InvokeReadMethodRequest{
uint32 template_id = 2;
string method = 3;
bytes args = 4;
bytes sender = 5;
}

message InvokeReadMethodResponse {
Expand All @@ -119,6 +120,7 @@ message InvokeMethodRequest{
uint32 template_id = 2;
string method = 3;
bytes args = 4;
bytes sender = 5;
}

message InvokeMethodResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl GrpcValidatorNodeClient {
template_id,
method,
args,
sender: PublicKey::default().to_vec(),
};
debug!(target: LOG_TARGET, "req {:?}", req);
let response = self
Expand Down Expand Up @@ -93,6 +94,7 @@ impl GrpcValidatorNodeClient {
template_id,
method,
args,
sender: PublicKey::default().to_vec(),
};
debug!(target: LOG_TARGET, "req {:?}", req);
let response = self
Expand Down
3 changes: 1 addition & 2 deletions applications/tari_validator_node/proto/dan/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ message InvokeReadMethodRequest{
uint32 template_id = 2;
string method = 3;
bytes args = 4;
bytes sender = 5;
}

message InvokeReadMethodResponse {
Expand All @@ -61,6 +62,7 @@ message InvokeMethodRequest{
uint32 template_id = 2;
string method = 3;
bytes args = 4;
bytes sender = 5;
}

message InvokeMethodResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_validator_node/src/dan_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use tari_dan_core::{
},
};
use tari_dan_storage_sqlite::{
global::SqliteGlobalDbBackendAdapter,
SqliteChainBackendAdapter,
SqliteDbFactory,
SqliteGlobalDbBackendAdapter,
SqliteStateDbBackendAdapter,
SqliteStorageService,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -188,6 +188,7 @@ impl<TServiceSpecification: ServiceSpecification + 'static> 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
{
Expand Down Expand Up @@ -238,7 +239,12 @@ impl<TServiceSpecification: ServiceSpecification + 'static> 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)
Expand All @@ -255,7 +261,13 @@ impl<TServiceSpecification: ServiceSpecification + 'static> 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
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_validator_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -217,7 +219,12 @@ impl TryFrom<proto::common::Instruction> for Instruction {

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())?;
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))?,
))
}
}

Expand Down
15 changes: 10 additions & 5 deletions applications/tari_validator_node/src/p2p/rpc/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TMempoolService, TDbFactory: DbFactory, TAssetProcessor> {
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient {
template_id: TemplateId,
method: String,
args: Vec<u8>,
sender: PublicKey,
) -> Result<Option<Vec<u8>>, ValidatorNodeClientError> {
debug!(
target: LOG_TARGET,
Expand All @@ -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?;

Expand All @@ -86,6 +88,7 @@ impl ValidatorNodeRpcClient for TariCommsValidatorNodeRpcClient {
template_id: TemplateId,
method: String,
args: Vec<u8>,
sender: PublicKey,
) -> Result<Option<Vec<u8>>, ValidatorNodeClientError> {
debug!(
target: LOG_TARGET,
Expand All @@ -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?;

Expand Down
9 changes: 8 additions & 1 deletion dan_layer/core/src/models/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,6 +34,7 @@ pub struct Instruction {
template_id: TemplateId,
method: String,
args: Vec<u8>,
sender: PublicKey,
// from: TokenId,
// signature: ComSig,
hash: FixedHash,
Expand All @@ -50,13 +51,15 @@ impl Instruction {
template_id: TemplateId,
method: String,
args: Vec<u8>,
sender: PublicKey,
// from: TokenId,
// _signature: ComSig,
) -> Self {
let mut s = Self {
template_id,
method,
args,
sender,
// from,
// TODO: this is obviously wrong
// signature: ComSig::default(),
Expand All @@ -78,6 +81,10 @@ impl Instruction {
&self.args
}

pub fn sender(&self) -> PublicKey {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this naming convention usually means a reference to the item is returned.
Suggest either to_sender_public_key or return a reference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I thought it was Copy for some reason....

self.sender.clone()
}

// // TODO: rename to avoid use of from
// pub fn from_owner(&self) -> &TokenId {
// &self.from
Expand Down
Loading