Skip to content

Commit

Permalink
Use V1 analysis for enclosed manifests (#256)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip

* wip

* bump version
  • Loading branch information
GhenadieVP authored Nov 7, 2024
1 parent e5e07b5 commit 82af570
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.46"
version = "1.1.47"
edition = "2021"
build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.46"
version = "1.1.47"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl GatewayClient {
intent.manifest,
intent.header.start_epoch_inclusive,
signer_public_keys,
intent.header.notary_public_key,
Some(intent.header.notary_public_key),
intent.header.nonce,
);
self.transaction_preview(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl TransactionPreviewRequest {
manifest: TransactionManifest,
start_epoch_inclusive: Epoch,
signer_public_keys: impl IntoIterator<Item = PublicKey>,
notary_public_key: PublicKey,
notary_public_key: Option<PublicKey>,
nonce: Nonce,
) -> Self {
let signer_public_keys = signer_public_keys
Expand All @@ -28,7 +28,7 @@ impl TransactionPreviewRequest {
start_epoch_inclusive,
)
.into(),
notary_public_key: Some(GWPublicKey::from(notary_public_key)),
notary_public_key: notary_public_key.map(GWPublicKey::from),
notary_is_signatory: signer_public_keys.is_empty(),
tip_percentage: 0,
nonce: nonce.into(),
Expand Down Expand Up @@ -56,7 +56,7 @@ mod tests {
intent.clone().manifest,
intent.header.start_epoch_inclusive,
keys.clone(),
intent.header.notary_public_key,
Some(intent.header.notary_public_key),
intent.header.nonce,
);
assert_eq!(sut.flags, flags);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::RwLockWriteGuard;

use radix_engine_toolkit::functions::transaction_v2::subintent_manifest;
use radix_transactions::manifest::BlobProvider;

use crate::prelude::*;

Expand Down Expand Up @@ -54,22 +55,15 @@ impl SargonOS {
);
}

// Get the transaction preview
let transaction_preview = self
.get_transaction_preview(
// Get the execution summary
let execution_summary = self
.get_transaction_execution_summary(
gateway_client,
transaction_manifest.clone(),
nonce,
notary_public_key,
Some(notary_public_key),
)
.await?;
let engine_toolkit_receipt = transaction_preview
.radix_engine_toolkit_receipt
.ok_or(CommonError::FailedToExtractTransactionReceiptBytes)?;

// Analyze the manifest
let execution_summary =
transaction_manifest.execution_summary(engine_toolkit_receipt)?;

Ok(TransactionToReview {
transaction_manifest,
Expand All @@ -94,7 +88,7 @@ impl SargonOS {
let subintent_manifest = SubintentManifest::new(
instructions,
network_id,
blobs,
blobs.clone(),
ChildIntents::default(),
)?;

Expand All @@ -114,16 +108,52 @@ impl SargonOS {

let pre_auth_to_review = match subintent_manifest.as_enclosed() {
Some(manifest) => {
// TODO: perform the actual analysis
// let gateway_client = GatewayClient::new(
// self.clients.http_client.driver.clone(),
// network_id,
// );
// let response = self.get_subintent_preview(manifest, gateway_client, nonce).await?;
let mut instructions = manifest.instructions;
/// The ASSERT_WORKTOP_IS_EMPTY instruction, as it is a V2 instruction, is not allowed in the V1 manifest.
instructions.instructions.remove(0);
let manifest_v2 = TransactionManifestV2::with_instructions_and_blobs_and_children(
instructions,
blobs.clone(),
ChildIntents::default(),
);

let manifest_string = manifest_v2.manifest_string();
let network_definition = network_id.network_definition();
let scrypto_manifest_v1: ScryptoTransactionManifest =
scrypto_compile_manifest(
&manifest_string,
&network_definition,
BlobProvider::new_with_blobs(
blobs
.blobs()
.into_iter()
.map(|b| b.0.bytes)
.collect(),
),
)
.map_err(|e| {
CommonError::from_scrypto_compile_error(e, network_id)
})?;

let manifest_v1: TransactionManifest =
(scrypto_manifest_v1, network_id).try_into()?;

let gateway_client = GatewayClient::new(
self.clients.http_client.driver.clone(),
network_id,
);
let execution_summary = self
.get_transaction_execution_summary(
gateway_client,
manifest_v1.into(),
nonce,
None,
)
.await?;

PreAuthToReview::Enclosed(PreAuthEnclosedManifest {
manifest: subintent_manifest,
summary: ExecutionSummary::sample_stokenet(),
summary: execution_summary,
})
}
None => PreAuthToReview::Open(PreAuthOpenManifest {
Expand Down Expand Up @@ -157,21 +187,21 @@ impl SargonOS {
.collect::<IndexSet<PublicKey>>())
}

async fn get_transaction_preview(
async fn get_transaction_execution_summary(
&self,
gateway_client: GatewayClient,
manifest: TransactionManifest,
nonce: Nonce,
notary_public_key: PublicKey,
) -> Result<TransactionPreviewResponse> {
notary_public_key: Option<PublicKey>,
) -> Result<ExecutionSummary> {
let signer_public_keys =
self.extract_signer_public_keys(manifest.summary()?)?;

// Getting the current ledger epoch
let epoch = gateway_client.current_epoch().await?;

let request = TransactionPreviewRequest::new_transaction_analysis_v1(
manifest,
manifest.clone(),
epoch,
signer_public_keys,
notary_public_key,
Expand All @@ -183,7 +213,12 @@ impl SargonOS {
if response.receipt.status != TransactionReceiptStatus::Succeeded {
return Err(Self::map_failed_transaction_preview(response));
};
Ok(response)

let engine_toolkit_receipt = response
.radix_engine_toolkit_receipt
.ok_or(CommonError::FailedToExtractTransactionReceiptBytes)?;

manifest.execution_summary(engine_toolkit_receipt)
}

#[cfg(not(tarpaulin_include))] // TBD
Expand Down Expand Up @@ -687,39 +722,40 @@ mod transaction_preview_analysis_tests {
)
}

#[actix_rt::test]
async fn analyse_open_enclosed_auth_preview() {
let os = prepare_os(MockNetworkingDriver::new_always_failing()).await;

let scrypto_subintent_manifest =
ScryptoSubintentManifestV2Builder::new_subintent_v2()
.assert_worktop_is_empty()
.drop_all_proofs()
.yield_to_parent(())
.build();

let subintent_manifest: SubintentManifest =
(scrypto_subintent_manifest, NetworkID::Mainnet)
.try_into()
.unwrap();

let result = os
.analyse_pre_auth_preview(
subintent_manifest.manifest_string(),
Blobs::default(),
Nonce::sample(),
)
.await
.unwrap();

assert_eq!(
result,
PreAuthToReview::Enclosed(PreAuthEnclosedManifest {
manifest: subintent_manifest.clone(),
summary: ExecutionSummary::sample_stokenet(),
})
)
}
// Disabled temporary as v1 analysis is used for now.
// #[actix_rt::test]
// async fn analyse_open_enclosed_auth_preview() {
// let os = prepare_os(MockNetworkingDriver::new_always_failing()).await;

// let scrypto_subintent_manifest =
// ScryptoSubintentManifestV2Builder::new_subintent_v2()
// .assert_worktop_is_empty()
// .drop_all_proofs()
// .yield_to_parent(())
// .build();

// let subintent_manifest: SubintentManifest =
// (scrypto_subintent_manifest, NetworkID::Mainnet)
// .try_into()
// .unwrap();

// let result = os
// .analyse_pre_auth_preview(
// subintent_manifest.manifest_string(),
// Blobs::default(),
// Nonce::sample(),
// )
// .await
// .unwrap();

// assert_eq!(
// result,
// PreAuthToReview::Enclosed(PreAuthEnclosedManifest {
// manifest: subintent_manifest.clone(),
// summary: ExecutionSummary::sample_stokenet(),
// })
// )
// }

async fn prepare_os(
mock_networking_driver: MockNetworkingDriver,
Expand Down

0 comments on commit 82af570

Please sign in to comment.