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

feature: Add BasicSborAssertion and ScryptoSborAssertion macros #1848

Merged

Conversation

dhedey
Copy link
Contributor

@dhedey dhedey commented Jul 2, 2024

Summary

These macros output tests which check for a fixed type schema, or a backwards compatible type schema.

See sbor-assert.rs in the tests to see how the macros are used, and see transaction_receipt.rs and system_structure.rs for how these are used in practice.

What's rather cool is that Rust Analyzer / the VS Code extension spots the test and magically offers a "Run Test" button, which makes this much nicer / more magic than I was expecting :).

Screenshot 2024-07-03 at 18 17 57

Details

You can start with #[sbor_assert(generate("FILE:<relative_file_path.txt>")] which will output a test which panics and gives you the current schema.

You can then do this as new versions get added:

#[sbor_assert(backwards_compatible(
    version1 = "FILE:mytype_schema_v1.txt",
    version2 = "FILE:mytype_schema_v2.txt",
))]

I've written rust docs for e.g. ScryptoSborAssertion, and the error messages aim to help you get the syntax right. Just start with adding #[derive(ScryptoSborAssertion)] and the error messages should guide you.

I also had to add a little extension to the define_single_versioned! / define_multi_versioned! macros to be able to support adding attributes to only the outer / inner types. It's not the nicest, but it's functional.

Testing

See sbor-assert.rs. Failing tests aren't included - because they're a bit painful to do.

But they've been manually tested, and most of the underlying logic has already been tested as part of the schema_comparisons.rs tests.

Update Recommendations

We can now start using these macros across scrypto and node.

These macros output tests which check for a fixed type schema,
or a backwards compatible type schema.
@dhedey dhedey changed the title feature: Add BasicSborAssertion and ScryptoSborAssertion macros feature: Add BasicSborAssertion and ScryptoSborAssertion macros Jul 2, 2024
Copy link

github-actions bot commented Jul 2, 2024

Docker tags
docker.io/radixdlt/private-scrypto-builder:bb47750079

Copy link

github-actions bot commented Jul 2, 2024

Benchmark for bb47750

Click to view benchmark
Test Base PR %
costing::bench_prepare_wasm 66.0±0.38ms 66.8±0.45ms +1.21%
costing::decode_sbor 11.1±0.02µs 11.0±0.06µs -0.90%
costing::decode_sbor_bytes 30.0±0.12µs 29.8±0.17µs -0.67%
costing::deserialize_wasm 1303.9±3.23µs 1292.9±3.18µs -0.84%
costing::instantiate_flash_loan 4.2±0.80ms 3.9±0.63ms -7.14%
costing::instantiate_radiswap 5.7±0.07ms 5.7±0.08ms 0.00%
costing::spin_loop 21.4±0.07ms 21.5±0.04ms +0.47%
costing::validate_sbor_payload 32.2±0.08µs 31.9±0.03µs -0.93%
costing::validate_sbor_payload_bytes 257.7±0.47ns 244.0±0.41ns -5.32%
costing::validate_secp256k1 76.3±0.21µs 76.3±0.22µs 0.00%
costing::validate_wasm 36.3±0.05ms 37.1±0.03ms +2.20%
decimal::add/0 8.4±0.00ns 8.4±0.00ns 0.00%
decimal::add/rust-native 9.9±0.01ns 9.8±0.01ns -1.01%
decimal::add/wasmer 119.0±0.63ns 114.9±0.12ns -3.45%
decimal::add/wasmer-call-native 451.8±0.33ns 449.0±0.27ns -0.62%
decimal::add/wasmi 581.0±0.88ns 576.4±1.04ns -0.79%
decimal::add/wasmi-call-native 5.3±0.02µs 5.3±0.02µs 0.00%
decimal::div/0 191.6±0.17ns 191.4±0.26ns -0.10%
decimal::from_string/0 153.6±0.58ns 153.5±0.17ns -0.07%
decimal::mul/0 142.2±0.23ns 142.8±0.15ns +0.42%
decimal::mul/rust-native 138.3±0.11ns 138.5±0.20ns +0.14%
decimal::mul/wasmer 1517.4±1.81ns 1557.7±1.05ns +2.66%
decimal::mul/wasmer-call-native 574.5±0.32ns 577.7±0.54ns +0.56%
decimal::mul/wasmi 42.1±0.10µs 41.2±0.19µs -2.14%
decimal::mul/wasmi-call-native 5.4±0.01µs 5.4±0.01µs 0.00%
decimal::pow/0 657.1±0.61ns 654.4±0.74ns -0.41%
decimal::pow/rust-native 630.6±0.56ns 634.5±0.48ns +0.62%
decimal::pow/wasmer 6.6±0.01µs 6.6±0.01µs 0.00%
decimal::pow/wasmer-call-native 1022.0±0.68ns 1022.4±0.50ns +0.04%
decimal::pow/wasmi 195.5±0.49µs 195.4±0.62µs -0.05%
decimal::pow/wasmi-call-native 5.4±0.01µs 5.4±0.01µs 0.00%
decimal::root/0 7.9±0.01µs 7.7±0.01µs -2.53%
decimal::sub/0 8.5±0.01ns 8.5±0.01ns 0.00%
decimal::to_string/0 443.0±0.26ns 440.6±0.44ns -0.54%
precise_decimal::add/0 9.4±0.15ns 9.2±0.03ns -2.13%
precise_decimal::add/rust-native 11.4±0.01ns 11.4±0.00ns 0.00%
precise_decimal::add/wasmer 127.9±0.19ns 124.7±0.41ns -2.50%
precise_decimal::add/wasmer-call-native 514.1±0.54ns 518.5±0.93ns +0.86%
precise_decimal::add/wasmi 732.5±1.23ns 962.9±0.63ns +31.45%
precise_decimal::add/wasmi-call-native 6.8±0.05µs 7.2±0.01µs +5.88%
precise_decimal::div/0 302.5±0.41ns 313.4±0.15ns +3.60%
precise_decimal::from_string/0 199.0±0.28ns 199.1±0.11ns +0.05%
precise_decimal::mul/0 344.5±0.69ns 356.1±0.24ns +3.37%
precise_decimal::mul/rust-native 302.6±0.65ns 313.8±4.95ns +3.70%
precise_decimal::mul/wasmer 3.4±0.00µs 3.4±0.00µs 0.00%
precise_decimal::mul/wasmer-call-native 828.6±1.07ns 815.0±2.74ns -1.64%
precise_decimal::mul/wasmi 104.7±0.21µs 104.6±0.31µs -0.10%
precise_decimal::mul/wasmi-call-native 7.2±0.03µs 7.1±0.03µs -1.39%
precise_decimal::pow/0 1880.5±6.73ns 1888.8±5.77ns +0.44%
precise_decimal::pow/rust-native 1499.9±5.09ns 1483.1±1.62ns -1.12%
precise_decimal::pow/wasmer 16.0±0.01µs 16.0±0.01µs 0.00%
precise_decimal::pow/wasmer-call-native 2.1±0.00µs 2.1±0.00µs 0.00%
precise_decimal::pow/wasmi 510.0±0.86µs 508.4±0.74µs -0.31%
precise_decimal::pow/wasmi-call-native 13.0±0.04µs 13.1±0.03µs +0.77%
precise_decimal::root/0 56.5±0.02µs 56.7±0.01µs +0.35%
precise_decimal::sub/0 9.6±0.14ns 9.4±0.03ns -2.08%
precise_decimal::to_string/0 732.6±1.56ns 735.7±2.78ns +0.42%
schema::validate_payload 351.7±0.44µs 368.9±0.51µs +4.89%
transaction::radiswap 5.5±0.02ms 5.5±0.03ms 0.00%
transaction::transfer 1803.1±8.54µs 1821.3±8.24µs +1.01%
transaction_processing::prepare 2.5±0.00ms 2.5±0.00ms 0.00%
transaction_processing::prepare_and_decompile 6.3±0.01ms 6.4±0.01ms +1.59%
transaction_processing::prepare_and_decompile_and_recompile 24.8±1.24ms 30.3±2.31ms +22.18%
transaction_validation::validate_manifest 42.3±0.45µs 42.3±0.05µs 0.00%
transaction_validation::verify_bls_2KB 1042.4±38.36µs 1182.2±178.19µs +13.41%
transaction_validation::verify_bls_32B 1041.6±36.85µs 1033.3±88.26µs -0.80%
transaction_validation::verify_ecdsa 74.3±0.06µs 74.2±0.05µs -0.13%
transaction_validation::verify_ed25519 54.6±0.09µs 54.8±0.50µs +0.37%

Copy link
Member

@iamyulong iamyulong left a comment

Choose a reason for hiding this comment

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

LGTM

@dhedey dhedey merged commit 0b009e8 into feature/schema-comparisons Aug 7, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants