Skip to content

Commit

Permalink
feature: Introduce Test Transaction builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Sep 15, 2024
1 parent 53a84c6 commit 769c596
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 446 deletions.
4 changes: 4 additions & 0 deletions radix-common/src/crypto/public_key_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub trait HasPublicKeyHash {
type TypedPublicKeyHash: IsPublicKeyHash;

fn get_hash(&self) -> Self::TypedPublicKeyHash;

fn signature_proof(&self) -> NonFungibleGlobalId {
NonFungibleGlobalId::from_public_key_hash(self.get_hash())
}
}

pub trait IsPublicKeyHash: Copy {
Expand Down
2 changes: 2 additions & 0 deletions radix-common/src/types/non_fungible_global_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ pub trait FromPublicKey: Sized {
}

impl FromPublicKey for NonFungibleGlobalId {
/// Prefer using the `signature` function or the `signature_proof()` method.
fn from_public_key<P: HasPublicKeyHash>(public_key: &P) -> Self {
Self::from_public_key_hash(public_key.get_hash())
}

/// Prefer using the `signature` function or the `signature_proof()` method.
fn from_public_key_hash<P: IsPublicKeyHash>(public_key_hash: P) -> Self {
match public_key_hash.into_enum() {
PublicKeyHash::Secp256k1(public_key_hash) => NonFungibleGlobalId::new(
Expand Down
34 changes: 16 additions & 18 deletions radix-engine-tests/tests/protocol/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use radix_common::prelude::*;
use radix_engine::errors::*;
use radix_engine::updates::ProtocolVersion;
use scrypto_test::prelude::*;

#[test]
Expand All @@ -12,21 +9,22 @@ fn bottlenose_protocol_should_not_support_v2_transactions() {
let (public_key, _, account) = ledger.new_allocated_account();

// Act
let intents = (0..2)
.into_iter()
.map(|_| {
let manifest = ManifestBuilder::new_v2()
.lock_standard_test_fee(account)
.build();
(
manifest,
ledger.next_transaction_nonce(),
vec![],
btreeset![NonFungibleGlobalId::from_public_key(&public_key)],
)
})
.collect();
let receipt = ledger.execute_test_transaction(TestTransaction::new_v2_from_nonce(intents));
let mut builder = TestTransaction::new_v2_builder(ledger.next_transaction_nonce());
let child_one = builder.add_subintent(
ManifestBuilder::new_subintent_v2()
.yield_to_parent(())
.build(),
[],
);
let transaction = builder.finish_with_root_intent(
ManifestBuilder::new_v2()
.use_child("child", child_one)
.lock_standard_test_fee(account)
.yield_to_child("child", ())
.build(),
[public_key.signature_proof()],
);
let receipt = ledger.execute_test_transaction(transaction);

// Assert
receipt.expect_specific_rejection(|e| matches!(e, RejectionReason::TransactionNotYetSupported));
Expand Down
200 changes: 76 additions & 124 deletions radix-engine-tests/tests/system/subintent_auth.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
use radix_common::constants::TRANSACTION_PROCESSOR_PACKAGE;
use radix_common::prelude::*;
use radix_engine::errors::{RuntimeError, SystemModuleError};
use radix_engine::system::system_modules::auth::AuthError;
use radix_engine_interface::blueprints::transaction_processor::{
TRANSACTION_PROCESSOR_BLUEPRINT, TRANSACTION_PROCESSOR_RUN_IDENT,
};
use radix_engine_interface::macros::dec;
use radix_transactions::builder::{ManifestBuilder, ResolvableArguments};
use radix_transactions::manifest::YieldToChild;
use radix_transactions::model::{InstructionV1, ManifestNamedIntentIndex, TestTransaction};
use scrypto_test::ledger_simulator::LedgerSimulatorBuilder;
use scrypto_test::prelude::*;

#[test]
fn should_not_be_able_to_use_root_auth_in_subintent() {
Expand All @@ -18,40 +7,27 @@ fn should_not_be_able_to_use_root_auth_in_subintent() {
let (public_key, _, account) = ledger.new_allocated_account();

// Act
let intents = vec![
{
let manifest = ManifestBuilder::new_v2()
.lock_standard_test_fee(account)
.add_instruction_advanced(YieldToChild {
child_index: ManifestNamedIntentIndex(0),
args: ().resolve(),
})
.0
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![1],
btreeset![NonFungibleGlobalId::from_public_key(&public_key)],
)
},
{
let manifest = ManifestBuilder::new_v2()
.withdraw_from_account(account, XRD, dec!(10))
.deposit_entire_worktop(account)
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![],
btreeset!(),
)
},
];

let receipt = ledger.execute_test_transaction(TestTransaction::new_v2_from_nonce(intents));
let mut builder = TestTransaction::new_v2_builder(ledger.next_transaction_nonce());

let child = builder.add_subintent(
ManifestBuilder::new_subintent_v2()
.withdraw_from_account(account, XRD, dec!(10))
.deposit_entire_worktop(account)
.yield_to_parent(())
.build(),
[],
);

let transaction = builder.finish_with_root_intent(
ManifestBuilder::new_v2()
.use_child("child", child)
.lock_standard_test_fee(account)
.yield_to_child("child", ())
.build(),
[public_key.signature_proof()],
);

let receipt = ledger.execute_test_transaction(transaction);

// Assert
receipt.expect_specific_failure(|e| {
Expand All @@ -70,40 +46,28 @@ fn should_be_able_to_use_separate_auth_in_subintent() {
let (public_key2, _, account2) = ledger.new_allocated_account();

// Act
let intents = vec![
{
let manifest = ManifestBuilder::new_v2()
.lock_standard_test_fee(account)
.add_instruction_advanced(YieldToChild {
child_index: ManifestNamedIntentIndex(0),
args: ().resolve(),
})
.0
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![1],
btreeset![NonFungibleGlobalId::from_public_key(&public_key)],
)
},
{
let manifest = ManifestBuilder::new_v2()
.withdraw_from_account(account2, XRD, dec!(10))
.deposit_entire_worktop(account2)
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![],
btreeset![NonFungibleGlobalId::from_public_key(&public_key2)],
)
},
];

let receipt = ledger.execute_test_transaction(TestTransaction::new_v2_from_nonce(intents));
let mut builder = TestTransaction::new_v2_builder(ledger.next_transaction_nonce());

let child = builder.add_subintent(
ManifestBuilder::new_subintent_v2()
.withdraw_from_account(account2, XRD, dec!(10))
.deposit_entire_worktop(account2)
// TODO-CUTTLEFISH: Fix the test / behaviour so this line can be uncommented
//.yield_to_parent(())
.build(),
[public_key2.signature_proof()],
);

let transaction = builder.finish_with_root_intent(
ManifestBuilder::new_v2()
.use_child("child", child)
.lock_standard_test_fee(account)
.yield_to_child("child", ())
.build(),
[public_key.signature_proof()],
);

let receipt = ledger.execute_test_transaction(transaction);

// Assert
receipt.expect_commit_success();
Expand All @@ -124,51 +88,39 @@ fn should_not_be_able_to_call_tx_processor_in_subintent() {
let (public_key, _, account) = ledger.new_allocated_account();

// Act
let intents = vec![
{
let manifest = ManifestBuilder::new_v2()
.lock_standard_test_fee(account)
.add_instruction_advanced(YieldToChild {
child_index: ManifestNamedIntentIndex(0),
args: ().resolve(),
})
.0
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![1],
btreeset![NonFungibleGlobalId::from_public_key(&public_key)],
let mut builder = TestTransaction::new_v2_builder(ledger.next_transaction_nonce());

let instructions: Vec<InstructionV1> = Vec::new();
let manifest_encoded_instructions = manifest_encode(&instructions).unwrap();

let child = builder.add_subintent(
ManifestBuilder::new_subintent_v2()
.call_function(
TRANSACTION_PROCESSOR_PACKAGE,
TRANSACTION_PROCESSOR_BLUEPRINT,
TRANSACTION_PROCESSOR_RUN_IDENT,
ManifestTransactionProcessorRunInput {
manifest_encoded_instructions,
global_address_reservations: vec![],
references: vec![],
blobs: index_map_new(),
},
)
},
{
let instructions: Vec<InstructionV1> = Vec::new();
let manifest_encoded_instructions = manifest_encode(&instructions).unwrap();
let manifest = ManifestBuilder::new_v2()
.call_function(
TRANSACTION_PROCESSOR_PACKAGE,
TRANSACTION_PROCESSOR_BLUEPRINT,
TRANSACTION_PROCESSOR_RUN_IDENT,
ManifestTransactionProcessorRunInput {
manifest_encoded_instructions,
global_address_reservations: vec![],
references: vec![],
blobs: index_map_new(),
},
)
.build();

(
manifest,
ledger.next_transaction_nonce(),
vec![],
btreeset![],
)
},
];

let receipt = ledger.execute_test_transaction(TestTransaction::new_v2_from_nonce(intents));
.yield_to_parent(())
.build(),
[],
);

let transaction = builder.finish_with_root_intent(
ManifestBuilder::new_v2()
.use_child("child", child)
.lock_standard_test_fee(account)
.yield_to_child("child", ())
.build(),
[public_key.signature_proof()],
);

let receipt = ledger.execute_test_transaction(transaction);

// Assert
receipt.expect_specific_failure(|e| {
Expand Down
Loading

0 comments on commit 769c596

Please sign in to comment.