Skip to content

Commit

Permalink
feat: add versioning to transaction(input,output,kernel) (#3709)
Browse files Browse the repository at this point in the history
Description
---
Add versioning to Transactions.

How Has This Been Tested?
---
Some cucumber tests.
  • Loading branch information
Cifko authored Jan 18, 2022
1 parent 520156e commit 08995c7
Show file tree
Hide file tree
Showing 41 changed files with 4,550 additions and 4,182 deletions.
8 changes: 8 additions & 0 deletions applications/tari_app_grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ message TransactionKernel {
Signature excess_sig = 7;
// The hash of the kernel, as it appears in the MMR
bytes hash = 8;
// Version
uint32 version = 9;
}

// A transaction input.
Expand All @@ -173,6 +175,8 @@ message TransactionInput {
bytes output_hash = 9;
// Covenant
bytes covenant = 10;
// Version
uint32 version = 11;
}

// Output for a transaction, defining the new ownership of coins that are being transferred. The commitment is a
Expand All @@ -196,6 +200,8 @@ message TransactionOutput {
ComSignature metadata_signature = 7;
// Covenant
bytes covenant = 8;
// Version
uint32 version = 9;
}

// Options for UTXO's
Expand All @@ -217,6 +223,8 @@ message OutputFeatures {
MintNonFungibleFeatures mint_non_fungible = 7;

SideChainCheckpointFeatures sidechain_checkpoint = 8;
// Version
uint32 version = 9;
}

message AssetOutputFeatures {
Expand Down
21 changes: 13 additions & 8 deletions applications/tari_app_grpc/src/conversions/output_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use tari_core::transactions::transaction::{
AssetOutputFeatures,
MintNonFungibleFeatures,
OutputFeatures,
OutputFeaturesVersion,
OutputFlags,
SideChainCheckpointFeatures,
TemplateParameter,
Expand All @@ -50,17 +51,20 @@ impl TryFrom<grpc::OutputFeatures> for OutputFeatures {
Some(PublicKey::from_bytes(features.parent_public_key.as_bytes()).map_err(|err| format!("{:?}", err))?)
};

Ok(Self {
flags: OutputFlags::from_bits(features.flags as u8)
Ok(OutputFeatures::new(
OutputFeaturesVersion::try_from(
u8::try_from(features.version).map_err(|_| "Invalid version: overflowed u8")?,
)?,
OutputFlags::from_bits(features.flags as u8)
.ok_or_else(|| "Invalid or unrecognised output flags".to_string())?,
maturity: features.maturity,
metadata: features.metadata,
features.maturity,
features.metadata,
unique_id,
parent_public_key,
asset: features.asset.map(|a| a.try_into()).transpose()?,
mint_non_fungible: features.mint_non_fungible.map(|m| m.try_into()).transpose()?,
sidechain_checkpoint: features.sidechain_checkpoint.map(|m| m.try_into()).transpose()?,
})
features.asset.map(|a| a.try_into()).transpose()?,
features.mint_non_fungible.map(|m| m.try_into()).transpose()?,
features.sidechain_checkpoint.map(|m| m.try_into()).transpose()?,
))
}
}

Expand All @@ -78,6 +82,7 @@ impl From<OutputFeatures> for grpc::OutputFeatures {
asset: features.asset.map(|a| a.into()),
mint_non_fungible: features.mint_non_fungible.map(|m| m.into()),
sidechain_checkpoint: features.sidechain_checkpoint.map(|m| m.into()),
version: features.version as u32,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use tari_common_types::types::{Commitment, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::transaction::TransactionInput,
transactions::transaction::{TransactionInput, TransactionInputVersion},
};
use tari_crypto::{
script::{ExecutionStack, TariScript},
Expand Down Expand Up @@ -58,6 +58,9 @@ impl TryFrom<grpc::TransactionInput> for TransactionInput {
let covenant = Covenant::consensus_decode(&mut input.covenant.as_slice()).map_err(|err| err.to_string())?;

Ok(TransactionInput::new_with_output_data(
TransactionInputVersion::try_from(
u8::try_from(input.version).map_err(|_| "Invalid version: overflowed u8")?,
)?,
features,
commitment,
TariScript::from_bytes(input.script.as_slice()).map_err(|err| format!("{:?}", err))?,
Expand Down Expand Up @@ -127,6 +130,7 @@ impl TryFrom<TransactionInput> for grpc::TransactionInput {
.covenant()
.map_err(|_| "Non-compact Transaction input should contain covenant".to_string())?
.to_consensus_bytes(),
version: input.version as u32,
})
}
}
Expand Down
16 changes: 10 additions & 6 deletions applications/tari_app_grpc/src/conversions/transaction_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::convert::{TryFrom, TryInto};
use tari_common_types::types::Commitment;
use tari_core::transactions::{
tari_amount::MicroTari,
transaction::{KernelFeatures, TransactionKernel},
transaction::{KernelFeatures, TransactionKernel, TransactionKernelVersion},
};
use tari_crypto::tari_utilities::{ByteArray, Hashable};

Expand All @@ -44,14 +44,17 @@ impl TryFrom<grpc::TransactionKernel> for TransactionKernel {
.try_into()
.map_err(|_| "excess_sig could not be converted".to_string())?;

Ok(Self {
features: KernelFeatures::from_bits(kernel.features as u8)
Ok(Self::new(
TransactionKernelVersion::try_from(
u8::try_from(kernel.version).map_err(|_| "Invalid version: overflowed u8")?,
)?,
KernelFeatures::from_bits(kernel.features as u8)
.ok_or_else(|| "Invalid or unrecognised kernel feature flag".to_string())?,
MicroTari::from(kernel.fee),
kernel.lock_height,
excess,
excess_sig,
fee: MicroTari::from(kernel.fee),
lock_height: kernel.lock_height,
})
))
}
}

Expand All @@ -69,6 +72,7 @@ impl From<TransactionKernel> for grpc::TransactionKernel {
signature: Vec::from(kernel.excess_sig.get_signature().as_bytes()),
}),
hash,
version: kernel.version as u32,
}
}
}
12 changes: 8 additions & 4 deletions applications/tari_app_grpc/src/conversions/transaction_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use tari_common_types::types::{BulletRangeProof, Commitment, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::transaction::TransactionOutput,
transactions::transaction::{TransactionOutput, TransactionOutputVersion},
};
use tari_crypto::script::TariScript;
use tari_utilities::{ByteArray, Hashable};
Expand Down Expand Up @@ -56,15 +56,18 @@ impl TryFrom<grpc::TransactionOutput> for TransactionOutput {
.try_into()
.map_err(|_| "Metadata signature could not be converted".to_string())?;
let covenant = Covenant::consensus_decode(&mut output.covenant.as_slice()).map_err(|err| err.to_string())?;
Ok(Self {
Ok(Self::new(
TransactionOutputVersion::try_from(
u8::try_from(output.version).map_err(|_| "Invalid version: overflowed u8")?,
)?,
features,
commitment,
proof: BulletRangeProof(output.range_proof),
BulletRangeProof(output.range_proof),
script,
sender_offset_public_key,
metadata_signature,
covenant,
})
))
}
}

Expand All @@ -84,6 +87,7 @@ impl From<TransactionOutput> for grpc::TransactionOutput {
signature_v: Vec::from(output.metadata_signature.v().as_bytes()),
}),
covenant: output.covenant.to_consensus_bytes(),
version: output.version as u32,
}
}
}
14 changes: 9 additions & 5 deletions applications/tari_app_grpc/src/conversions/unblinded_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use tari_common_types::types::{PrivateKey, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::{tari_amount::MicroTari, transaction::UnblindedOutput},
transactions::{
tari_amount::MicroTari,
transaction::{TransactionOutputVersion, UnblindedOutput},
},
};
use tari_crypto::script::{ExecutionStack, TariScript};
use tari_utilities::ByteArray;
Expand Down Expand Up @@ -85,17 +88,18 @@ impl TryFrom<grpc::UnblindedOutput> for UnblindedOutput {

let covenant = Covenant::consensus_decode(&mut output.covenant.as_slice()).map_err(|err| err.to_string())?;

Ok(Self {
value: MicroTari::from(output.value),
Ok(Self::new(
TransactionOutputVersion::try_from(0u8)?,
MicroTari::from(output.value),
spending_key,
features,
script,
input_data,
script_private_key,
sender_offset_public_key,
metadata_signature,
script_lock_height: output.script_lock_height,
output.script_lock_height,
covenant,
})
))
}
}
8 changes: 1 addition & 7 deletions applications/test_faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ async fn write_keys(mut rx: mpsc::Receiver<(TransactionOutput, PrivateKey, Micro
}
let (pk, sig) = test_helpers::create_random_signature_from_s_key(key_sum, 0.into(), 0);
let excess = Commitment::from_public_key(&pk);
let kernel = TransactionKernel {
features: KernelFeatures::empty(),
fee: MicroTari::from(0),
lock_height: 0,
excess,
excess_sig: sig,
};
let kernel = TransactionKernel::new_current_version(KernelFeatures::empty(), MicroTari::from(0), 0, excess, sig);
let kernel = serde_json::to_string(&kernel).unwrap();
let _ = utxo_file.write_all(format!("{}\n", kernel).as_bytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ where B: BlockchainBackend + 'static
Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn start_streaming(
&self,
tx: &mut mpsc::Sender<Result<SyncUtxosByBlockResponse, RpcStatus>>,
Expand Down
Loading

0 comments on commit 08995c7

Please sign in to comment.