Skip to content

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Aug 18, 2022
1 parent 0101f55 commit c9ae047
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 925 deletions.
102 changes: 100 additions & 2 deletions applications/tari_app_grpc/src/conversions/sidechain_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
// 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 std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};

use tari_core::transactions::transaction_components::{CodeTemplateRegistration, SideChainFeatures};
use tari_common_types::types::{PublicKey, Signature};
use tari_core::{
consensus::MaxSizeString,
transactions::transaction_components::{BuildInfo, CodeTemplateRegistration, SideChainFeatures, TemplateType},
};
use tari_utilities::ByteArray;

use crate::tari_rpc as grpc;

Expand All @@ -46,3 +51,96 @@ impl TryFrom<grpc::SideChainFeatures> for SideChainFeatures {
Ok(Self { template_registration })
}
}

// -------------------------------- TemplateRegistration -------------------------------- //
impl TryFrom<grpc::TemplateRegistration> for CodeTemplateRegistration {
type Error = String;

fn try_from(value: grpc::TemplateRegistration) -> Result<Self, Self::Error> {
Ok(Self {
author_public_key: PublicKey::from_bytes(&value.author_public_key).map_err(|e| e.to_string())?,
author_signature: value
.author_signature
.map(Signature::try_from)
.ok_or("author_signature not provided")??,
template_name: MaxSizeString::try_from(value.template_name).map_err(|e| e.to_string())?,
template_version: value
.template_version
.try_into()
.map_err(|_| "Invalid template version")?,
template_type: value
.template_type
.map(TryFrom::try_from)
.ok_or("Template type not provided")??,
build_info: value
.build_info
.map(TryFrom::try_from)
.ok_or("Build info not provided")??,
binary_sha: value.binary_sha.try_into().map_err(|_| "Invalid commit sha")?,
binary_url: MaxSizeString::try_from(value.binary_url).map_err(|e| e.to_string())?,
})
}
}

impl From<CodeTemplateRegistration> for grpc::TemplateRegistration {
fn from(value: CodeTemplateRegistration) -> Self {
Self {
author_public_key: value.author_public_key.to_vec(),
author_signature: Some(value.author_signature.into()),
template_name: value.template_name.to_string(),
template_version: u32::from(value.template_version),
template_type: Some(value.template_type.into()),
build_info: Some(value.build_info.into()),
binary_sha: value.binary_sha.to_vec(),
binary_url: value.binary_url.to_string(),
}
}
}

// -------------------------------- TemplateType -------------------------------- //
impl TryFrom<grpc::TemplateType> for TemplateType {
type Error = String;

fn try_from(value: grpc::TemplateType) -> Result<Self, Self::Error> {
let template_type = value.template_type.ok_or("Template type not provided")?;
match template_type {
grpc::template_type::TemplateType::Wasm(wasm) => Ok(TemplateType::Wasm {
abi_version: wasm.abi_version.try_into().map_err(|_| "abi_version overflowed")?,
}),
}
}
}

impl From<TemplateType> for grpc::TemplateType {
fn from(value: TemplateType) -> Self {
match value {
TemplateType::Wasm { abi_version } => Self {
template_type: Some(grpc::template_type::TemplateType::Wasm(grpc::WasmInfo {
abi_version: abi_version.into(),
})),
},
}
}
}

// -------------------------------- BuildInfo -------------------------------- //

impl TryFrom<grpc::BuildInfo> for BuildInfo {
type Error = String;

fn try_from(value: grpc::BuildInfo) -> Result<Self, Self::Error> {
Ok(Self {
repo_url: value.repo_url.try_into().map_err(|_| "Invalid repo url")?,
commit_hash: value.commit_hash.try_into().map_err(|_| "Invalid commit hash")?,
})
}
}

impl From<BuildInfo> for grpc::BuildInfo {
fn from(value: BuildInfo) -> Self {
Self {
repo_url: value.repo_url.into_string(),
commit_hash: value.commit_hash.into_vec(),
}
}
}
29 changes: 23 additions & 6 deletions applications/tari_console_wallet/src/grpc/wallet_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@ use tari_common_types::{
};
use tari_comms::{multiaddr::Multiaddr, types::CommsPublicKey, CommsNode};
use tari_core::transactions::{
tari_amount::MicroTari,
transaction_components::{CodeTemplateRegistration, OutputFeatures, UnblindedOutput},
tari_amount::{MicroTari, T},
transaction_components::{
CodeTemplateRegistration,
OutputFeatures,
OutputType,
SideChainFeatures,
UnblindedOutput,
},
};
use tari_utilities::{hex::Hex, ByteArray, Hashable};
use tari_wallet::{
connectivity_service::{OnlineStatus, WalletConnectivityInterface},
output_manager_service::handle::OutputManagerHandle,
output_manager_service::{handle::OutputManagerHandle, UtxoSelectionCriteria},
transaction_service::{
handle::TransactionServiceHandle,
storage::models::{self, WalletTransaction},
Expand Down Expand Up @@ -922,7 +928,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
&self,
request: Request<CreateTemplateRegistrationRequest>,
) -> Result<Response<CreateTemplateRegistrationResponse>, Status> {
let mut asset_manager = self.wallet.asset_manager.clone();
let mut output_manager = self.wallet.output_manager_service.clone();
let mut transaction_service = self.wallet.transaction_service.clone();
let message = request.into_inner();

Expand All @@ -932,11 +938,22 @@ impl wallet_server::Wallet for WalletGrpcServer {
.ok_or_else(|| Status::invalid_argument("template_registration is empty"))?,
)
.map_err(|e| Status::invalid_argument(format!("template_registration is invalid: {}", e)))?;
let fee_per_gram = message.fee_per_gram;

let message = format!("Template registration {}", template_registration.template_name);
let output = output_manager
.create_output_with_features(1 * T, OutputFeatures {
output_type: OutputType::CodeTemplateRegistration,
sidechain_features: Some(Box::new(SideChainFeatures {
template_registration: Some(template_registration),
})),
..Default::default()
})
.await
.map_err(|e| Status::internal(e.to_string()))?;

let (tx_id, transaction) = asset_manager
.create_code_template_registration(template_registration)
let (tx_id, transaction) = output_manager
.create_send_to_self_with_output(vec![output], fee_per_gram.into(), UtxoSelectionCriteria::default())
.await
.map_err(|e| Status::internal(e.to_string()))?;

Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/covenants/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ mod test {
fn it_returns_true_if_eq() {
let output = create_outputs(1, UtxoTestParams {
features: OutputFeatures {
sidechain_features: Some(Box::new(SideChainFeatures {})),
sidechain_features: Some(Box::new(SideChainFeatures::default())),
..Default::default()
},
script: script![Drop Nop],
Expand All @@ -377,7 +377,7 @@ mod test {
.is_eq(&output, &output.features.output_type)
.unwrap());
assert!(OutputField::FeaturesSideChainFeatures
.is_eq(&output, &SideChainFeatures {})
.is_eq(&output, &SideChainFeatures::default())
.unwrap());
assert!(OutputField::FeaturesSideChainFeatures
.is_eq(&output, output.features.sidechain_features.as_ref().unwrap())
Expand All @@ -394,7 +394,7 @@ mod test {
fn it_returns_false_if_not_eq() {
let output = create_outputs(1, UtxoTestParams {
features: OutputFeatures {
sidechain_features: Some(Box::new(SideChainFeatures {})),
sidechain_features: Some(Box::new(SideChainFeatures::default())),
..Default::default()
},
script: script![Drop Nop],
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/covenants/filters/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ mod test {
let input = create_input();
let (mut context, outputs) = setup_filter_test(&covenant, &input, 0, |outputs| {
outputs[5].features.maturity = 42;
outputs[5].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[5].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
outputs[7].features.maturity = 42;
outputs[7].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[7].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
// Does not have maturity = 42
outputs[8].features.maturity = 123;
outputs[8].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[8].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
});

let mut output_set = OutputSet::new(&outputs);
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/fields_hashed_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod test {
fn it_filters_outputs_with_fields_that_hash_to_given_hash() {
let features = OutputFeatures {
maturity: 42,
sidechain_features: Some(Box::new(SideChainFeatures {})),
sidechain_features: Some(Box::new(SideChainFeatures::default())),
..Default::default()
};
let hashed = Challenge::new().chain(features.to_consensus_bytes()).finalize();
Expand Down
8 changes: 4 additions & 4 deletions base_layer/core/src/covenants/filters/fields_preserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ mod test {
let covenant = covenant!(fields_preserved(@fields(@field::features_maturity, @field::features_flags)));
let mut input = create_input();
input.set_maturity(42).unwrap();
input.features_mut().unwrap().sidechain_features = Some(Box::new(SideChainFeatures {}));
input.features_mut().unwrap().sidechain_features = Some(Box::new(SideChainFeatures::default()));
input.features_mut().unwrap().output_type = OutputType::Standard;
let (mut context, outputs) = setup_filter_test(&covenant, &input, 0, |outputs| {
outputs[5].features.maturity = 42;
outputs[5].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[5].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
outputs[5].features.output_type = OutputType::Standard;
outputs[7].features.maturity = 42;
outputs[7].features.output_type = OutputType::Standard;
outputs[7].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[7].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
outputs[8].features.maturity = 42;
outputs[8].features.sidechain_features = Some(Box::new(SideChainFeatures {}));
outputs[8].features.sidechain_features = Some(Box::new(SideChainFeatures::default()));
outputs[8].features.output_type = OutputType::Coinbase;
});
let mut output_set = OutputSet::new(&outputs);
Expand Down
74 changes: 40 additions & 34 deletions base_layer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,47 @@ pub mod large_ints {
}

pub use large_ints::{U256, U512};
use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher};
use tari_mmr::{pruned_hashset::PrunedHashSet, Hash, MerkleMountainRange, MutableMmr};

hash_domain!(
KernelMmrHashDomain,
"com.tari.tari_project.base_layer.core.kernel_mmr",
1
);
pub type KernelMmrHasherBlake256 = DomainSeparatedHasher<Blake256, KernelMmrHashDomain>;
pub type KernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, Vec<Hash>>;
pub type PrunedKernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, PrunedHashSet>;
#[cfg(feature = "base_node")]
mod mmr_hashing {
use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher};
use tari_mmr::{pruned_hashset::PrunedHashSet, Hash, MerkleMountainRange, MutableMmr};

hash_domain!(
KernelMmrHashDomain,
"com.tari.tari_project.base_layer.core.kernel_mmr",
1
);
pub type KernelMmrHasherBlake256 = DomainSeparatedHasher<Blake256, KernelMmrHashDomain>;
pub type KernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, Vec<Hash>>;
pub type PrunedKernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
WitnessMmrHashDomain,
"com.tari.tari_project.base_layer.core.witness_mmr",
1
);
pub type WitnessMmrHasherBlake256 = DomainSeparatedHasher<Blake256, WitnessMmrHashDomain>;
pub type WitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, Vec<Hash>>;
pub type PrunedWitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, PrunedHashSet>;
hash_domain!(
WitnessMmrHashDomain,
"com.tari.tari_project.base_layer.core.witness_mmr",
1
);
pub type WitnessMmrHasherBlake256 = DomainSeparatedHasher<Blake256, WitnessMmrHashDomain>;
pub type WitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, Vec<Hash>>;
pub type PrunedWitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
OutputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type OutputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, OutputMmrHashDomain>;
pub type MutableOutputMmr = MutableMmr<OutputMmrHasherBlake256, Vec<Hash>>;
pub type PrunedOutputMmr = MerkleMountainRange<OutputMmrHasherBlake256, PrunedHashSet>;
pub type MutablePrunedOutputMmr = MutableMmr<OutputMmrHasherBlake256, PrunedHashSet>;
hash_domain!(
OutputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type OutputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, OutputMmrHashDomain>;
pub type MutableOutputMmr = MutableMmr<OutputMmrHasherBlake256, Vec<Hash>>;
pub type PrunedOutputMmr = MerkleMountainRange<OutputMmrHasherBlake256, PrunedHashSet>;
pub type MutablePrunedOutputMmr = MutableMmr<OutputMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
InputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type InputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, InputMmrHashDomain>;
pub type PrunedInputMmr = MerkleMountainRange<InputMmrHasherBlake256, PrunedHashSet>;
hash_domain!(
InputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type InputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, InputMmrHashDomain>;
pub type PrunedInputMmr = MerkleMountainRange<InputMmrHasherBlake256, PrunedHashSet>;
}
#[cfg(feature = "base_node")]
pub use mmr_hashing::*;
Loading

0 comments on commit c9ae047

Please sign in to comment.