From 61f6b9d2603588eea8165252e54ae1a63effa87f Mon Sep 17 00:00:00 2001 From: laruh Date: Sun, 6 Oct 2024 15:58:20 +0700 Subject: [PATCH 1/3] .github workflows with linting and tests --- .github/actions/cargo-cache/action.yml | 20 +++++++++ .github/workflows/fmt-and-lint.yml | 60 ++++++++++++++++++++++++++ .github/workflows/test.yml | 35 +++++++++++++++ .gitignore | 4 +- 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 .github/actions/cargo-cache/action.yml create mode 100644 .github/workflows/fmt-and-lint.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/actions/cargo-cache/action.yml b/.github/actions/cargo-cache/action.yml new file mode 100644 index 0000000..54cf637 --- /dev/null +++ b/.github/actions/cargo-cache/action.yml @@ -0,0 +1,20 @@ +name: 'Set up cargo cache' +description: 'Sets up the cargo cache for the workflow' +runs: + using: 'composite' + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up cargo cache + uses: actions/cache@v3 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- \ No newline at end of file diff --git a/.github/workflows/fmt-and-lint.yml b/.github/workflows/fmt-and-lint.yml new file mode 100644 index 0000000..a5804a0 --- /dev/null +++ b/.github/workflows/fmt-and-lint.yml @@ -0,0 +1,60 @@ +name: Format and Lint +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + fmt-and-lint: + name: x86 Format and Lint Checks + timeout-minutes: 30 + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + components: clippy, rustfmt + override: true + profile: minimal + toolchain: nightly-2023-06-01 + + - name: Cargo cache + uses: ./.github/actions/cargo-cache + + - name: fmt check + # Format checks aren't OS dependent. + if: matrix.os == 'ubuntu-latest' + run: cargo fmt --all -- --check + + - name: Clippy lint + run: cargo clippy --all-targets --all-features -- --D warnings + + wasm-lint: + name: Wasm Lint Checks + timeout-minutes: 30 + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + components: clippy + override: true + profile: minimal + toolchain: nightly-2023-06-01 + target: wasm32-unknown-unknown + + - name: Cargo cache + uses: ./.github/actions/cargo-cache + + - name: Clippy lint + run: cargo clippy --target wasm32-unknown-unknown -- --D warnings \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..263f2bc --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,35 @@ +name: Test +on: + push: + branches-ignore: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + x86-64-unit-tests: + name: x86 Cargo unit tests + timeout-minutes: 30 + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + components: clippy, rustfmt + override: true + profile: minimal + toolchain: nightly-2023-06-01 + + - name: Cargo cache + uses: ./.github/actions/cargo-cache + + - name: Cargo test + run: cargo test --no-fail-fast \ No newline at end of file diff --git a/.gitignore b/.gitignore index 17e0f98..f7fafda 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ target/ -.vscode/ \ No newline at end of file +.vscode/ + +.idea \ No newline at end of file From 0f0ccf835c0f603d11b6796fc54f370336417138 Mon Sep 17 00:00:00 2001 From: laruh Date: Sun, 6 Oct 2024 16:45:44 +0700 Subject: [PATCH 2/3] fix clippy and fmt --- src/blake2b_internal.rs | 64 ++-- src/encoding.rs | 4 +- src/tests/encoding.rs | 7 +- src/tests/serde.rs | 764 ++++++++++++++++++------------------- src/tests/spend_policy.rs | 18 +- src/tests/transaction.rs | 14 +- src/transport/endpoints.rs | 2 +- src/types.rs | 2 +- src/types/atomic_swap.rs | 329 ++++++++-------- src/types/keypair.rs | 2 +- src/types/spend_policy.rs | 15 +- src/types/transaction.rs | 3 +- 12 files changed, 593 insertions(+), 631 deletions(-) diff --git a/src/blake2b_internal.rs b/src/blake2b_internal.rs index d878b89..2b3a9ff 100644 --- a/src/blake2b_internal.rs +++ b/src/blake2b_internal.rs @@ -155,7 +155,7 @@ fn hash_blake2b_pair(prefix: &[u8], leaf1: &[u8], leaf2: &[u8]) -> Hash256 { mod test { use super::*; - cross_target_tests!{ + cross_target_tests! { fn test_accumulator_new() { let default_accumulator = Accumulator::default(); @@ -167,51 +167,51 @@ mod test { } fn test_accumulator_root_default() { assert_eq!(Accumulator::default().root(), Hash256::default()) } - + fn test_accumulator_root() { let mut accumulator = Accumulator::default(); - + let timelock_leaf = timelock_leaf(0u64); accumulator.add_leaf(timelock_leaf); - + let pubkey = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); let pubkey_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey)); accumulator.add_leaf(pubkey_leaf); - + let sigs_required_leaf = sigs_required_leaf(1u64); accumulator.add_leaf(sigs_required_leaf); - + let expected = Hash256::try_from("h:72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d").unwrap(); assert_eq!(accumulator.root(), expected); } - + fn test_accumulator_add_leaf_standard_unlock_hash() { let mut accumulator = Accumulator::default(); - + let pubkey = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - + let pubkey_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey)); let timelock_leaf = timelock_leaf(0u64); let sigs_required_leaf = sigs_required_leaf(1u64); - + accumulator.add_leaf(timelock_leaf); accumulator.add_leaf(pubkey_leaf); accumulator.add_leaf(sigs_required_leaf); - + let root = accumulator.root(); let expected = Hash256::try_from("h:72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d").unwrap(); assert_eq!(root, expected) } - + fn test_accumulator_add_leaf_2of2_multisig_unlock_hash() { let mut accumulator = Accumulator::default(); - + let pubkey1 = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) @@ -220,26 +220,26 @@ mod test { &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - + let pubkey1_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey1)); let pubkey2_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey2)); - + let timelock_leaf = timelock_leaf(0u64); let sigs_required_leaf = sigs_required_leaf(2u64); - + accumulator.add_leaf(timelock_leaf); accumulator.add_leaf(pubkey1_leaf); accumulator.add_leaf(pubkey2_leaf); accumulator.add_leaf(sigs_required_leaf); - + let root = accumulator.root(); let expected = Hash256::try_from("h:1e94357817d236167e54970a8c08bbd41b37bfceeeb52f6c1ce6dd01d50ea1e7").unwrap(); assert_eq!(root, expected) } - + fn test_accumulator_add_leaf_1of2_multisig_unlock_hash() { let mut accumulator = Accumulator::default(); - + let pubkey1 = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) @@ -248,34 +248,34 @@ mod test { &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - + let pubkey1_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey1)); let pubkey2_leaf = public_key_leaf(&UnlockKey::Ed25519(pubkey2)); - + let timelock_leaf = timelock_leaf(0u64); let sigs_required_leaf = sigs_required_leaf(1u64); - + accumulator.add_leaf(timelock_leaf); accumulator.add_leaf(pubkey1_leaf); accumulator.add_leaf(pubkey2_leaf); accumulator.add_leaf(sigs_required_leaf); - + let root = accumulator.root(); let expected = Hash256::try_from("h:d7f84e3423da09d111a17f64290c8d05e1cbe4cab2b6bed49e3a4d2f659f0585").unwrap(); assert_eq!(root, expected) } - + fn test_standard_unlock_hash() { let pubkey = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - + let hash = standard_unlock_hash(&pubkey); let expected = Hash256::try_from("h:72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d").unwrap(); assert_eq!(hash, expected) } - + fn test_hash_blake2b_pair() { let left: [u8; 32] = hex::decode("cdcce3978a58ceb6c8480d218646db4eae85eb9ea9c2f5138fbacb4ce2c701e3") .unwrap() @@ -285,36 +285,36 @@ mod test { .unwrap() .try_into() .unwrap(); - + let hash = hash_blake2b_pair(&NODE_HASH_PREFIX, &left, &right); let expected = Hash256::try_from("h:72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d").unwrap(); assert_eq!(hash, expected) } - + fn test_timelock_leaf() { let hash = timelock_leaf(0); let expected = Hash256(STANDARD_TIMELOCK_BLAKE2B_HASH); assert_eq!(hash, expected) } - + fn test_sigs_required_leaf() { let hash = sigs_required_leaf(1u64); let expected = Hash256(STANDARD_SIGS_REQUIRED_BLAKE2B_HASH); assert_eq!(hash, expected) } - + fn test_hash_blake2b_single() { let hash = hash_blake2b_single(&hex::decode("006564323535313900000000000000000020000000000000000102030000000000000000000000000000000000000000000000000000000000").unwrap()); let expected = Hash256::try_from("h:21ce940603a2ee3a283685f6bfb4b122254894fd1ed3eb59434aadbf00c75d5b").unwrap(); assert_eq!(hash, expected) } - + fn test_public_key_leaf() { let pubkey = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - + let hash = public_key_leaf(&UnlockKey::Ed25519(pubkey)); let expected = Hash256::try_from("h:21ce940603a2ee3a283685f6bfb4b122254894fd1ed3eb59434aadbf00c75d5b").unwrap(); assert_eq!(hash, expected) diff --git a/src/encoding.rs b/src/encoding.rs index 3d97ca0..907c3e9 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -62,7 +62,7 @@ mod test { use super::*; use std::convert::TryFrom; - cross_target_tests!{ + cross_target_tests! { fn test_encoder_default_hash() { assert_eq!( Encoder::default().hash(), @@ -143,4 +143,4 @@ mod test { ); } } -} \ No newline at end of file +} diff --git a/src/tests/encoding.rs b/src/tests/encoding.rs index 126e50f..4e86280 100644 --- a/src/tests/encoding.rs +++ b/src/tests/encoding.rs @@ -1,4 +1,3 @@ - #[cfg(test)] mod test { use crate::blake2b_internal::standard_unlock_hash; @@ -6,8 +5,8 @@ mod test { use crate::types::{Address, Hash256, PublicKey, SpendPolicy, UnlockCondition}; use std::convert::TryFrom; use std::str::FromStr; - - cross_target_tests!{ + + cross_target_tests! { fn test_unlock_condition_unlock_hash_2of2_multisig() { let pubkey = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), @@ -172,4 +171,4 @@ mod test { assert_eq!(hash, expected); } } -} \ No newline at end of file +} diff --git a/src/tests/serde.rs b/src/tests/serde.rs index 899f3cc..b140ab9 100644 --- a/src/tests/serde.rs +++ b/src/tests/serde.rs @@ -1,5 +1,3 @@ - - // // FIXME reminder to populate the following tests // #[test] // #[ignore] @@ -104,52 +102,67 @@ mod test { }}; } // Ensure the original value matches the value after round-trip (serialize -> deserialize -> serialize) - use crate::types::{Address, Event, Hash256, UnlockKey, SiacoinElement, SiacoinOutput, StateElement, V2Transaction}; - + use crate::types::{Address, Event, Hash256, SiacoinElement, SiacoinOutput, StateElement, UnlockKey, V2Transaction}; + cross_target_tests! { - fn test_serde_address() { - test_serde!( - Address, - json!("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f") - ); - } - - fn test_serde_unlock_key() { - test_serde!( - UnlockKey, - json!("ed25519:0102030000000000000000000000000000000000000000000000000000000000") - ); - } - - fn test_serde_sia_hash() { - test_serde!( - Hash256, - json!("h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1") + fn test_serde_address() { + test_serde!( + Address, + json!("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f") + ); + } + + fn test_serde_unlock_key() { + test_serde!( + UnlockKey, + json!("ed25519:0102030000000000000000000000000000000000000000000000000000000000") + ); + } + + fn test_serde_sia_hash() { + test_serde!( + Hash256, + json!("h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1") + ); + } + + fn test_serde_siacoin_output() { + let j = json!({ + "value": "300000000000000000000000000000", + "address": "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" + }); + test_serde!(SiacoinOutput, j); + } + + fn test_serde_state_element() { + let j = json!({ + "id": "h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1", + "leafIndex": 21, + "merkleProof": null + }); + serde_json::from_value::(j).unwrap(); + } + + fn test_serde_siacoin_element() { + let j = json!( { + "id": "h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1", + "leafIndex": 21, + "merkleProof": ["h:8dfc4731c4ef4bf35f789893e72402a39c7ea63ba9e75565cb11000d0159959e"], + "siacoinOutput": { + "value": "300000000000000000000000000000", + "address": "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" + }, + "maturityHeight": 154 + } ); + serde_json::from_value::(j).unwrap(); } - - fn test_serde_siacoin_output() { - let j = json!({ - "value": "300000000000000000000000000000", - "address": "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" - }); - test_serde!(SiacoinOutput, j); - } - - fn test_serde_state_element() { - let j = json!({ - "id": "h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1", - "leafIndex": 21, - "merkleProof": null - }); - serde_json::from_value::(j).unwrap(); - } - - fn test_serde_siacoin_element() { + + fn test_serde_siacoin_element_null_merkle_proof() { let j = json!( { "id": "h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1", "leafIndex": 21, - "merkleProof": ["h:8dfc4731c4ef4bf35f789893e72402a39c7ea63ba9e75565cb11000d0159959e"], + "merkleProof": null, "siacoinOutput": { "value": "300000000000000000000000000000", "address": "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" @@ -159,312 +172,355 @@ mod test { ); serde_json::from_value::(j).unwrap(); } - - fn test_serde_siacoin_element_null_merkle_proof() { - let j = json!( { - "id": "h:dc07e5bf84fbda867a7ed7ca80c6d1d81db05cef16ff38f6ba80b6bf01e1ddb1", - "leafIndex": 21, - "merkleProof": null, - "siacoinOutput": { - "value": "300000000000000000000000000000", - "address": "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" - }, - "maturityHeight": 154 - } - ); - serde_json::from_value::(j).unwrap(); -} -fn test_serde_event_v2_contract_resolution_storage_proof() { - let j = json!( - { - "id": "h:a863dbc4f02efdfbf9f8d03e1aada090ede0a5752b71503787617d5f395c1335", - "index": { - "height": 201, - "id": "bid:e6e5282f107f2957844a93612e71003ec67238f32504b151e9e21fbb9224e8cf" - }, - "timestamp": "2024-07-18T19:04:16Z", - "maturityHeight": 345, - "type": "v2ContractResolution", - "data": { - "resolution": { - "parent": { - "id": "h:b30e0d25d4e414763378236b00a98cfbf9cd6a5e81540d1dcd40338ab6a5c636", - "leafIndex": 397, - "merkleProof": [ - "h:4d2a433de745231ff1eb0736ba68ffc3f8b1a976dbc3eca9649b5cf2dd5c2c44", - "h:e23fdf53d7c3c2bc7dc58660cb16e5b66dbf2e71c0a46c778af1c4d59a83cf63", - "h:0e63636af15d58fd9a87e21719899c2d518a948305e325929cbc4652d0fc3b38", - "h:37e5cee3bb2607e537209807b07dafef9658253080751b11858a9ae844364c0b", - "h:077252892fc0b8e687f14baf2ad3d2812539d05a293bfcabe8f0b884d8c91b01" - ], - "v2FileContract": { - "filesize": 0, - "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", - "proofHeight": 200, - "expirationHeight": 210, - "renterOutput": { - "value": "0", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" - }, - "hostOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + fn test_serde_event_v2_contract_resolution_storage_proof() { + let j = json!( + { + "id": "h:a863dbc4f02efdfbf9f8d03e1aada090ede0a5752b71503787617d5f395c1335", + "index": { + "height": 201, + "id": "bid:e6e5282f107f2957844a93612e71003ec67238f32504b151e9e21fbb9224e8cf" + }, + "timestamp": "2024-07-18T19:04:16Z", + "maturityHeight": 345, + "type": "v2ContractResolution", + "data": { + "resolution": { + "parent": { + "id": "h:b30e0d25d4e414763378236b00a98cfbf9cd6a5e81540d1dcd40338ab6a5c636", + "leafIndex": 397, + "merkleProof": [ + "h:4d2a433de745231ff1eb0736ba68ffc3f8b1a976dbc3eca9649b5cf2dd5c2c44", + "h:e23fdf53d7c3c2bc7dc58660cb16e5b66dbf2e71c0a46c778af1c4d59a83cf63", + "h:0e63636af15d58fd9a87e21719899c2d518a948305e325929cbc4652d0fc3b38", + "h:37e5cee3bb2607e537209807b07dafef9658253080751b11858a9ae844364c0b", + "h:077252892fc0b8e687f14baf2ad3d2812539d05a293bfcabe8f0b884d8c91b01" + ], + "v2FileContract": { + "filesize": 0, + "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", + "proofHeight": 200, + "expirationHeight": 210, + "renterOutput": { + "value": "0", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + }, + "hostOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "missedHostValue": "0", + "totalCollateral": "0", + "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "revisionNumber": 0, + "renterSignature": "sig:9d001e60633801956d1ce8b281b18a4b7da1249e8cb1e13b808f19c23e31c52596c303bd5efca278461877050412f1bec489037f101b7f41d3069906c60be30d", + "hostSignature": "sig:9d001e60633801956d1ce8b281b18a4b7da1249e8cb1e13b808f19c23e31c52596c303bd5efca278461877050412f1bec489037f101b7f41d3069906c60be30d" + } + }, + "type": "storageProof", + "resolution": { + "proofIndex": { + "id": "h:ee154b9b26af5a130d189c2467bd0157f24f4357478bfe5184243ab918c20290", + "leafIndex": 416, + "merkleProof": [], + "chainIndex": { + "height": 200, + "id": "bid:ee154b9b26af5a130d189c2467bd0157f24f4357478bfe5184243ab918c20290" + } }, - "missedHostValue": "0", - "totalCollateral": "0", - "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "revisionNumber": 0, - "renterSignature": "sig:9d001e60633801956d1ce8b281b18a4b7da1249e8cb1e13b808f19c23e31c52596c303bd5efca278461877050412f1bec489037f101b7f41d3069906c60be30d", - "hostSignature": "sig:9d001e60633801956d1ce8b281b18a4b7da1249e8cb1e13b808f19c23e31c52596c303bd5efca278461877050412f1bec489037f101b7f41d3069906c60be30d" + "leaf": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "proof": [] } }, - "type": "storageProof", - "resolution": { - "proofIndex": { - "id": "h:ee154b9b26af5a130d189c2467bd0157f24f4357478bfe5184243ab918c20290", - "leafIndex": 416, - "merkleProof": [], - "chainIndex": { - "height": 200, - "id": "bid:ee154b9b26af5a130d189c2467bd0157f24f4357478bfe5184243ab918c20290" - } + "siacoinElement": { + "id": "h:a863dbc4f02efdfbf9f8d03e1aada090ede0a5752b71503787617d5f395c1335", + "leafIndex": 418, + "merkleProof": null, + "siacoinOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" }, - "leaf": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "proof": [] - } - }, - "siacoinElement": { - "id": "h:a863dbc4f02efdfbf9f8d03e1aada090ede0a5752b71503787617d5f395c1335", - "leafIndex": 418, - "merkleProof": null, - "siacoinOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + "maturityHeight": 345 }, - "maturityHeight": 345 - }, - "missed": false + "missed": false + } } - } - ); - - let _event = serde_json::from_value::(j).unwrap(); - - // FIXME this should deserialize from a JSON object generated from walletd and recalcuate the txid to check encoding/serde -} + ); -fn test_serde_event_v2_contract_resolution_renewal() { - let j = json!( - { - "id": "h:debd3b8461d1aaa9011ba62d79c7ed7991eb0c60f9576880faadf2a8051aad54", - "index": { - "height": 203, - "id": "bid:bd04c08bb96203c7f24adf2d405cb1069c7da8573573011379a986be62fc2a29" - }, - "timestamp": "2024-07-18T19:04:16Z", - "maturityHeight": 347, - "type": "v2ContractResolution", - "data": { - "resolution": { - "parent": { - "id": "h:06b6349f4e76819aa36b7f1190d276b9ca97f0d5fc4564f153d6a36ed3c38033", - "leafIndex": 423, - "merkleProof": [ - "h:ba1427aad85e9985b61f262a2ea768a74f24af02d7e6c17f0cdb92559e7951ea", - "h:147817a1d32c3f068be5456d935bc6cddd6306fe5633b576d91260d43a82e6d8", - "h:f447a5360e1a7c4cab3062dd1699f56ea642b4f6cc6464fdfca0d1aa15fa436c", - "h:1cdf40c0a759931ff590496b953938fbe7315394ce3726b4e4c4b81fed3d5498" - ], - "v2FileContract": { - "filesize": 0, - "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", - "proofHeight": 211, - "expirationHeight": 221, - "renterOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + let _event = serde_json::from_value::(j).unwrap(); + + // FIXME this should deserialize from a JSON object generated from walletd and recalcuate the txid to check encoding/serde + } + + fn test_serde_event_v2_contract_resolution_renewal() { + let j = json!( + { + "id": "h:debd3b8461d1aaa9011ba62d79c7ed7991eb0c60f9576880faadf2a8051aad54", + "index": { + "height": 203, + "id": "bid:bd04c08bb96203c7f24adf2d405cb1069c7da8573573011379a986be62fc2a29" + }, + "timestamp": "2024-07-18T19:04:16Z", + "maturityHeight": 347, + "type": "v2ContractResolution", + "data": { + "resolution": { + "parent": { + "id": "h:06b6349f4e76819aa36b7f1190d276b9ca97f0d5fc4564f153d6a36ed3c38033", + "leafIndex": 423, + "merkleProof": [ + "h:ba1427aad85e9985b61f262a2ea768a74f24af02d7e6c17f0cdb92559e7951ea", + "h:147817a1d32c3f068be5456d935bc6cddd6306fe5633b576d91260d43a82e6d8", + "h:f447a5360e1a7c4cab3062dd1699f56ea642b4f6cc6464fdfca0d1aa15fa436c", + "h:1cdf40c0a759931ff590496b953938fbe7315394ce3726b4e4c4b81fed3d5498" + ], + "v2FileContract": { + "filesize": 0, + "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", + "proofHeight": 211, + "expirationHeight": 221, + "renterOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "hostOutput": { + "value": "0", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + }, + "missedHostValue": "0", + "totalCollateral": "0", + "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "revisionNumber": 0, + "renterSignature": "sig:7d6f0e5b799c689dca7b55b1ff8ad028c7285b777d6df0e68235bde5778802adfb87e80afaf5d6c9b9fa63cd0e433aaa7189e3fdf2c7bf374c0ca20858071f03", + "hostSignature": "sig:7d6f0e5b799c689dca7b55b1ff8ad028c7285b777d6df0e68235bde5778802adfb87e80afaf5d6c9b9fa63cd0e433aaa7189e3fdf2c7bf374c0ca20858071f03" + } + }, + "type": "renewal", + "resolution": { + "finalRevision": { + "filesize": 0, + "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", + "proofHeight": 211, + "expirationHeight": 221, + "renterOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "hostOutput": { + "value": "0", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + }, + "missedHostValue": "0", + "totalCollateral": "0", + "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "revisionNumber": 18446744073709551615u64, + "renterSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "hostSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }, - "hostOutput": { - "value": "0", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + "newContract": { + "filesize": 0, + "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", + "proofHeight": 221, + "expirationHeight": 231, + "renterOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "hostOutput": { + "value": "0", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + }, + "missedHostValue": "0", + "totalCollateral": "0", + "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "revisionNumber": 0, + "renterSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "hostSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }, - "missedHostValue": "0", - "totalCollateral": "0", - "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "revisionNumber": 0, - "renterSignature": "sig:7d6f0e5b799c689dca7b55b1ff8ad028c7285b777d6df0e68235bde5778802adfb87e80afaf5d6c9b9fa63cd0e433aaa7189e3fdf2c7bf374c0ca20858071f03", - "hostSignature": "sig:7d6f0e5b799c689dca7b55b1ff8ad028c7285b777d6df0e68235bde5778802adfb87e80afaf5d6c9b9fa63cd0e433aaa7189e3fdf2c7bf374c0ca20858071f03" + "renterRollover": "0", + "hostRollover": "0", + "renterSignature": "sig:54a4bb0247518f62b20bf141686e2c05858e91acd23ae5e42436d173e331aca92af344e8cb9b5da98f0bdef01c7b7d840cbe7e781b8f7acc7c33b0fa44c7ef08", + "hostSignature": "sig:54a4bb0247518f62b20bf141686e2c05858e91acd23ae5e42436d173e331aca92af344e8cb9b5da98f0bdef01c7b7d840cbe7e781b8f7acc7c33b0fa44c7ef08" } }, - "type": "renewal", + "siacoinElement": { + "id": "h:debd3b8461d1aaa9011ba62d79c7ed7991eb0c60f9576880faadf2a8051aad54", + "leafIndex": 427, + "merkleProof": null, + "siacoinOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "maturityHeight": 347 + }, + "missed": false + } + } + ); + + let _event = serde_json::from_value::(j).unwrap(); + + // FIXME this should deserialize from a JSON object generated from walletd and recalcuate the txid to check encoding/serde + } + + #[ignore] // FIXME Error("expected an empty map for expiration", line: 0, column: 0) + fn test_serde_event_v2_contract_resolution_expiration() { + let j = json!( + { + "id": "h:4c0170b9e82eacc2d14a13b974ce0c03560358276f135403bd060b53ce53be1c", + "index": { + "height": 190, + "id": "bid:730f554f8cd5e6bd855b21b8c53f59808f3aa7351093f44da7761181283e3c6b" + }, + "timestamp": "2024-07-18T19:04:16Z", + "maturityHeight": 334, + "type": "v2ContractResolution", + "data": { "resolution": { - "finalRevision": { - "filesize": 0, - "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", - "proofHeight": 211, - "expirationHeight": 221, - "renterOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" - }, - "hostOutput": { - "value": "0", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" - }, - "missedHostValue": "0", - "totalCollateral": "0", - "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "revisionNumber": 18446744073709551615u64, - "renterSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "hostSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "parent": { + "id": "h:34f6bb9b9ed58dedebce2f39d29a526ea3012e9ae005cfca6a5257761c5412f6", + "leafIndex": 351, + "merkleProof": [ + "h:e805430ecdd47bcaca574f78721c3b6a24f0a877110fc9fa7ab347fd231a9885", + "h:70782818a59e512d4995efd4ee94299e601496011b9c42b47eb0a3cd65aa89c9", + "h:42ab48d2ef2b54352d44ab2ef33c1a6d76589360c0dd556d703a452b7d3e4a2c", + "h:4af61bcae0a46d70f9b826b9bace336647389c38e6cb4c54356b9dd7fd6060aa", + "h:59d21dd10aa3def083106844e23ad7f6b93e309c80b24a03e2c9b6eba8acef33", + "h:f95c3f0fc4d632e5da8adcaa9249ea6b0c5fe66466a951871f5dc30a0c96b76d", + "h:3374baebf913a23e0b9811ae22e72f6cdf6999d332ccda4b4dbab87f58b2a574" + ], + "v2FileContract": { + "filesize": 0, + "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", + "proofHeight": 179, + "expirationHeight": 189, + "renterOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + }, + "hostOutput": { + "value": "0", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + }, + "missedHostValue": "0", + "totalCollateral": "0", + "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", + "revisionNumber": 0, + "renterSignature": "sig:c293b22c9feee5a081699ddbf83486704df855129c2bbe27c2dc56afcb7e68cd355785fa36954471c1e48691864b240969168422b1fd6396e18f720ebec50e00", + "hostSignature": "sig:c293b22c9feee5a081699ddbf83486704df855129c2bbe27c2dc56afcb7e68cd355785fa36954471c1e48691864b240969168422b1fd6396e18f720ebec50e00" + } }, - "newContract": { - "filesize": 0, - "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", - "proofHeight": 221, - "expirationHeight": 231, - "renterOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" - }, - "hostOutput": { - "value": "0", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" - }, - "missedHostValue": "0", - "totalCollateral": "0", - "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "revisionNumber": 0, - "renterSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "hostSignature": "sig:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "type": "expiration", + "resolution": {} + }, + "siacoinElement": { + "id": "h:4c0170b9e82eacc2d14a13b974ce0c03560358276f135403bd060b53ce53be1c", + "leafIndex": 391, + "merkleProof": null, + "siacoinOutput": { + "value": "10000000000000000000000000000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" }, - "renterRollover": "0", - "hostRollover": "0", - "renterSignature": "sig:54a4bb0247518f62b20bf141686e2c05858e91acd23ae5e42436d173e331aca92af344e8cb9b5da98f0bdef01c7b7d840cbe7e781b8f7acc7c33b0fa44c7ef08", - "hostSignature": "sig:54a4bb0247518f62b20bf141686e2c05858e91acd23ae5e42436d173e331aca92af344e8cb9b5da98f0bdef01c7b7d840cbe7e781b8f7acc7c33b0fa44c7ef08" - } - }, - "siacoinElement": { - "id": "h:debd3b8461d1aaa9011ba62d79c7ed7991eb0c60f9576880faadf2a8051aad54", - "leafIndex": 427, - "merkleProof": null, - "siacoinOutput": { - "value": "10000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + "maturityHeight": 334 }, - "maturityHeight": 347 - }, - "missed": false + "missed": true + } } - } - ); - - let _event = serde_json::from_value::(j).unwrap(); - - // FIXME this should deserialize from a JSON object generated from walletd and recalcuate the txid to check encoding/serde -} + ); -#[ignore] // FIXME Error("expected an empty map for expiration", line: 0, column: 0) -fn test_serde_event_v2_contract_resolution_expiration() { - let j = json!( - { - "id": "h:4c0170b9e82eacc2d14a13b974ce0c03560358276f135403bd060b53ce53be1c", - "index": { - "height": 190, - "id": "bid:730f554f8cd5e6bd855b21b8c53f59808f3aa7351093f44da7761181283e3c6b" - }, - "timestamp": "2024-07-18T19:04:16Z", - "maturityHeight": 334, - "type": "v2ContractResolution", - "data": { - "resolution": { - "parent": { - "id": "h:34f6bb9b9ed58dedebce2f39d29a526ea3012e9ae005cfca6a5257761c5412f6", - "leafIndex": 351, - "merkleProof": [ - "h:e805430ecdd47bcaca574f78721c3b6a24f0a877110fc9fa7ab347fd231a9885", - "h:70782818a59e512d4995efd4ee94299e601496011b9c42b47eb0a3cd65aa89c9", - "h:42ab48d2ef2b54352d44ab2ef33c1a6d76589360c0dd556d703a452b7d3e4a2c", - "h:4af61bcae0a46d70f9b826b9bace336647389c38e6cb4c54356b9dd7fd6060aa", - "h:59d21dd10aa3def083106844e23ad7f6b93e309c80b24a03e2c9b6eba8acef33", - "h:f95c3f0fc4d632e5da8adcaa9249ea6b0c5fe66466a951871f5dc30a0c96b76d", - "h:3374baebf913a23e0b9811ae22e72f6cdf6999d332ccda4b4dbab87f58b2a574" - ], - "v2FileContract": { - "filesize": 0, - "fileMerkleRoot": "h:0000000000000000000000000000000000000000000000000000000000000000", - "proofHeight": 179, - "expirationHeight": 189, - "renterOutput": { - "value": "10000000000000000000000000000", + let _event = serde_json::from_value::(j).unwrap(); + } + + fn test_serde_event_v2_transaction() { + let j = json!( + { + "id": "h:5900e475aace932c94bcc94cf296596ccff1d77d9aba52a079e9f429605671cd", + "index": { + "height": 203, + "id": "bid:bd04c08bb96203c7f24adf2d405cb1069c7da8573573011379a986be62fc2a29" + }, + "timestamp": "2024-07-18T19:04:16Z", + "maturityHeight": 203, + "type": "v2Transaction", + "data": { + "siacoinInputs": [ + { + "parent": { + "id": "h:78d58090bcdeaccf22abf99b6e0de25273e9eb82210359a16cefbd743a85fd50", + "leafIndex": 421, + "merkleProof": [ + "h:f26accb7c256e867a9ed62671ebe6c3eb34d085e5266f67073af2daa549f980d", + "h:d39e139147168c70da11c3f6db4fa54d35914ef67ba5654a75107da9c099ddda", + "h:f447a5360e1a7c4cab3062dd1699f56ea642b4f6cc6464fdfca0d1aa15fa436c" + ], + "siacoinOutput": { + "value": "256394172736732570239334030000", "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" }, - "hostOutput": { - "value": "0", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" + "maturityHeight": 0 + }, + "satisfiedPolicy": { + "policy": { + "type": "uc", + "policy": { + "timelock": 0, + "publicKeys": [ + "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc" + ], + "signaturesRequired": 1 + } }, - "missedHostValue": "0", - "totalCollateral": "0", - "renterPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "hostPublicKey": "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc", - "revisionNumber": 0, - "renterSignature": "sig:c293b22c9feee5a081699ddbf83486704df855129c2bbe27c2dc56afcb7e68cd355785fa36954471c1e48691864b240969168422b1fd6396e18f720ebec50e00", - "hostSignature": "sig:c293b22c9feee5a081699ddbf83486704df855129c2bbe27c2dc56afcb7e68cd355785fa36954471c1e48691864b240969168422b1fd6396e18f720ebec50e00" + "signatures": [ + "sig:c432fea5f147205e49235ddbd75c232fd8e9c7526b2b1575f70653ae2b3c0d0338c7fe710be338482060cf6ef2dea5e2319252fc28deaf70c77a2be60a533400" + ] } - }, - "type": "expiration", - "resolution": {} - }, - "siacoinElement": { - "id": "h:4c0170b9e82eacc2d14a13b974ce0c03560358276f135403bd060b53ce53be1c", - "leafIndex": 391, - "merkleProof": null, - "siacoinOutput": { - "value": "10000000000000000000000000000", + } + ], + "siacoinOutputs": [ + { + "value": "10400000000000000000000000000", "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" }, - "maturityHeight": 334 - }, - "missed": true + { + "value": "245994172736732570239334030000", + "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + } + ], + "minerFee": "0" + } } - } - ); - - let _event = serde_json::from_value::(j).unwrap(); -} + ); + test_serde!(Event, j); + } -fn test_serde_event_v2_transaction() { - let j = json!( - { - "id": "h:5900e475aace932c94bcc94cf296596ccff1d77d9aba52a079e9f429605671cd", - "index": { - "height": 203, - "id": "bid:bd04c08bb96203c7f24adf2d405cb1069c7da8573573011379a986be62fc2a29" - }, - "timestamp": "2024-07-18T19:04:16Z", - "maturityHeight": 203, - "type": "v2Transaction", - "data": { + fn test_v2_transaction_serde_basic_send() { + let j = json!( + { "siacoinInputs": [ { "parent": { - "id": "h:78d58090bcdeaccf22abf99b6e0de25273e9eb82210359a16cefbd743a85fd50", - "leafIndex": 421, + "id": "h:f59e395dc5cbe3217ee80eff60585ffc9802e7ca580d55297782d4a9b4e08589", + "leafIndex": 3, "merkleProof": [ - "h:f26accb7c256e867a9ed62671ebe6c3eb34d085e5266f67073af2daa549f980d", - "h:d39e139147168c70da11c3f6db4fa54d35914ef67ba5654a75107da9c099ddda", - "h:f447a5360e1a7c4cab3062dd1699f56ea642b4f6cc6464fdfca0d1aa15fa436c" + "h:ab0e1726444c50e2c0f7325eb65e5bd262a97aad2647d2816c39d97958d9588a", + "h:467e2be4d8482eca1f99440b6efd531ab556d10a8371a98a05b00cb284620cf0", + "h:64d5766fce1ff78a13a4a4744795ad49a8f8d187c01f9f46544810049643a74a", + "h:31d5151875152bc25d1df18ca6bbda1bef5b351e8d53c277791ecf416fcbb8a8", + "h:12a92a1ba87c7b38f3c4e264c399abfa28fb46274cfa429605a6409bd6d0a779", + "h:eda1d58a9282dbf6c3f1beb4d6c7bdc036d14a1cfee8ab1e94fabefa9bd63865", + "h:e03dee6e27220386c906f19fec711647353a5f6d76633a191cbc2f6dce239e89", + "h:e70fcf0129c500f7afb49f4f2bb82950462e952b7cdebb2ad0aa1561dc6ea8eb" ], "siacoinOutput": { - "value": "256394172736732570239334030000", + "value": "300000000000000000000000000000", "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" }, - "maturityHeight": 0 + "maturityHeight": 145 }, "satisfiedPolicy": { "policy": { @@ -478,87 +534,29 @@ fn test_serde_event_v2_transaction() { } }, "signatures": [ - "sig:c432fea5f147205e49235ddbd75c232fd8e9c7526b2b1575f70653ae2b3c0d0338c7fe710be338482060cf6ef2dea5e2319252fc28deaf70c77a2be60a533400" + "sig:f0a29ba576eb0dbc3438877ac1d3a6da4f3c4cbafd9030709c8a83c2fffa64f4dd080d37444261f023af3bd7a10a9597c33616267d5371bf2c0ade5e25e61903" ] } } ], "siacoinOutputs": [ { - "value": "10400000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" + "value": "1000000000000000000000000000", + "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" }, { - "value": "245994172736732570239334030000", + "value": "299000000000000000000000000000", "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" } ], "minerFee": "0" } - } - ); - test_serde!(Event, j); -} + ); + let tx = serde_json::from_value::(j).unwrap(); -fn test_v2_transaction_serde_basic_send() { - let j = json!( - { - "siacoinInputs": [ - { - "parent": { - "id": "h:f59e395dc5cbe3217ee80eff60585ffc9802e7ca580d55297782d4a9b4e08589", - "leafIndex": 3, - "merkleProof": [ - "h:ab0e1726444c50e2c0f7325eb65e5bd262a97aad2647d2816c39d97958d9588a", - "h:467e2be4d8482eca1f99440b6efd531ab556d10a8371a98a05b00cb284620cf0", - "h:64d5766fce1ff78a13a4a4744795ad49a8f8d187c01f9f46544810049643a74a", - "h:31d5151875152bc25d1df18ca6bbda1bef5b351e8d53c277791ecf416fcbb8a8", - "h:12a92a1ba87c7b38f3c4e264c399abfa28fb46274cfa429605a6409bd6d0a779", - "h:eda1d58a9282dbf6c3f1beb4d6c7bdc036d14a1cfee8ab1e94fabefa9bd63865", - "h:e03dee6e27220386c906f19fec711647353a5f6d76633a191cbc2f6dce239e89", - "h:e70fcf0129c500f7afb49f4f2bb82950462e952b7cdebb2ad0aa1561dc6ea8eb" - ], - "siacoinOutput": { - "value": "300000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" - }, - "maturityHeight": 145 - }, - "satisfiedPolicy": { - "policy": { - "type": "uc", - "policy": { - "timelock": 0, - "publicKeys": [ - "ed25519:cecc1507dc1ddd7295951c290888f095adb9044d1b73d696e6df065d683bd4fc" - ], - "signaturesRequired": 1 - } - }, - "signatures": [ - "sig:f0a29ba576eb0dbc3438877ac1d3a6da4f3c4cbafd9030709c8a83c2fffa64f4dd080d37444261f023af3bd7a10a9597c33616267d5371bf2c0ade5e25e61903" - ] - } - } - ], - "siacoinOutputs": [ - { - "value": "1000000000000000000000000000", - "address": "addr:000000000000000000000000000000000000000000000000000000000000000089eb0d6a8a69" - }, - { - "value": "299000000000000000000000000000", - "address": "addr:f7843ac265b037658b304468013da4fd0f304a1b73df0dc68c4273c867bfa38d01a7661a187f" - } - ], - "minerFee": "0" - } - ); - let tx = serde_json::from_value::(j).unwrap(); - - let j2 = serde_json::to_value(&tx).unwrap().to_string(); - let tx2 = serde_json::from_str::(&j2).unwrap(); - assert_eq!(tx, tx2); -} + let j2 = serde_json::to_value(&tx).unwrap().to_string(); + let tx2 = serde_json::from_str::(&j2).unwrap(); + assert_eq!(tx, tx2); + } + } } -} \ No newline at end of file diff --git a/src/tests/spend_policy.rs b/src/tests/spend_policy.rs index 5d658bc..fc8d6a3 100644 --- a/src/tests/spend_policy.rs +++ b/src/tests/spend_policy.rs @@ -1,5 +1,3 @@ - - #[cfg(test)] mod test { use crate::types::{Address, Hash256, PublicKey, SpendPolicy, UnlockCondition, UnlockKey}; @@ -15,7 +13,7 @@ mod test { } ); - let spend_policy_deser = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::Above(100); assert_eq!(spend_policy, spend_policy_deser); @@ -29,7 +27,7 @@ mod test { } ); - let spend_policy_deser = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::After(200); assert_eq!(spend_policy, spend_policy_deser); @@ -46,7 +44,7 @@ mod test { &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), ) .unwrap(); - let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::PublicKey(pubkey); assert_eq!(spend_policy, spend_policy_deser); @@ -60,7 +58,7 @@ mod test { } ); let hash = Hash256::try_from("h:0102030000000000000000000000000000000000000000000000000000000000").unwrap(); - let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::Hash(hash); assert_eq!(spend_policy, spend_policy_deser); @@ -75,7 +73,7 @@ mod test { ); let address = Address::from_str("addr:f72e84ee9e344e424a6764068ffd7fdce4b4e50609892c6801bc1ead79d3ae0d71791b277a3a").unwrap(); - let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::Opaque(address); assert_eq!(spend_policy, spend_policy_deser); @@ -125,7 +123,7 @@ mod test { } ); - let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap(); assert_eq!(spend_policy, spend_policy_deser); } @@ -155,10 +153,10 @@ mod test { signatures_required: 1, }; - let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap().into(); + let spend_policy_deser: SpendPolicy = serde_json::from_value::(j).unwrap(); let spend_policy = SpendPolicy::UnlockConditions(uc); assert_eq!(spend_policy, spend_policy_deser); } } -} \ No newline at end of file +} diff --git a/src/tests/transaction.rs b/src/tests/transaction.rs index 45829ce..8cc0f1d 100644 --- a/src/tests/transaction.rs +++ b/src/tests/transaction.rs @@ -1,14 +1,14 @@ #[cfg(test)] mod test { use crate::encoding::Encoder; - use crate::types::{Address, Hash256, Keypair, PublicKey, Signature, Attestation, Currency, CurrencyVersion, FileContractRevisionV2, SatisfiedPolicy, - SiacoinElement, SiacoinInputV1, SiacoinInputV2, SiacoinOutput, SiacoinOutputVersion, - StateElement, V2FileContract, V2FileContractElement, V2Transaction, SpendPolicy, - UnlockCondition}; + use crate::types::{Address, Attestation, Currency, CurrencyVersion, FileContractRevisionV2, Hash256, Keypair, + PublicKey, SatisfiedPolicy, SiacoinElement, SiacoinInputV1, SiacoinInputV2, SiacoinOutput, + SiacoinOutputVersion, Signature, SpendPolicy, StateElement, UnlockCondition, V2FileContract, + V2FileContractElement, V2Transaction}; use std::convert::TryFrom; use std::str::FromStr; - - cross_target_tests!{ + + cross_target_tests! { fn test_siacoin_input_encode() { let public_key = PublicKey::from_bytes( &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), @@ -711,4 +711,4 @@ mod test { assert_eq!(tx.siacoin_inputs[0].satisfied_policy.signatures[0], sig); } } -} \ No newline at end of file +} diff --git a/src/transport/endpoints.rs b/src/transport/endpoints.rs index dfbb8ee..1865b46 100644 --- a/src/transport/endpoints.rs +++ b/src/transport/endpoints.rs @@ -1,5 +1,5 @@ use crate::transport::client::{ApiClientError, Body, EndpointSchema, EndpointSchemaBuilder, SchemaMethod}; -use crate::types::{Address, BlockID, Event, Hash256, Currency, SiacoinElement, V1Transaction, V2Transaction}; +use crate::types::{Address, BlockID, Currency, Event, Hash256, SiacoinElement, V1Transaction, V2Transaction}; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use std::collections::HashMap; diff --git a/src/types.rs b/src/types.rs index 577272d..baa5d80 100644 --- a/src/types.rs +++ b/src/types.rs @@ -315,7 +315,7 @@ impl<'de> Deserialize<'de> for Event { }?; Ok(Event { - id: helper.id.into(), + id: helper.id, index: helper.index, timestamp: helper.timestamp, maturity_height: helper.maturity_height, diff --git a/src/types/atomic_swap.rs b/src/types/atomic_swap.rs index 06d0603..ad46684 100644 --- a/src/types/atomic_swap.rs +++ b/src/types/atomic_swap.rs @@ -3,22 +3,22 @@ use thiserror::Error; /* The full representation of the atomic swap contract is as follows: - SpendPolicy::Threshold { + SpendPolicy::Threshold { n: 1, - of: vec![ - SpendPolicy::Threshold { - n: 2, + of: vec![ + SpendPolicy::Threshold { + n: 2, of: vec![ SpendPolicy::Hash(), SpendPolicy::PublicKey() - ] + ] }, - SpendPolicy::Threshold { - n: 2, + SpendPolicy::Threshold { + n: 2, of: vec![ SpendPolicy::After(), SpendPolicy::PublicKey() - ] + ] }, ] } @@ -42,15 +42,15 @@ Alice can spend the UTXO by providing a signature, the secret and revealing the Alice can construct the following SatisfiedPolicy to spend the UTXO: SatisfiedPolicy { - policy: SpendPolicy::Threshold { + policy: SpendPolicy::Threshold { n: 1, - of: vec![ - SpendPolicy::Threshold { - n: 2, + of: vec![ + SpendPolicy::Threshold { + n: 2, of: vec![ SpendPolicy::Hash(), SpendPolicy::PublicKey() - ] + ] }, SpendPolicy::Opaque(), ] @@ -62,15 +62,15 @@ SatisfiedPolicy { Similarly, Bob can spend the UTXO with the following SatisfiedPolicy assuming he waits until the timestamp has passed: SatisfiedPolicy { - policy: SpendPolicy::Threshold { + policy: SpendPolicy::Threshold { n: 1, - of: vec![ - SpendPolicy::Threshold { - n: 2, + of: vec![ + SpendPolicy::Threshold { + n: 2, of: vec![ SpendPolicy::After(), SpendPolicy::PublicKey() - ] + ] }, SpendPolicy::Opaque(), ] @@ -103,9 +103,9 @@ pub enum AtomicSwapError { #[error("invalid atomic swap, invalid time component: {}", .0)] InvalidTimeComponent(ComponentError), #[error("invalid atomic swap, wrong n:{} policy: {:?}", n, policy)] - InvalidN{ policy: SpendPolicy, n : u8 }, + InvalidN { policy: SpendPolicy, n: u8 }, #[error("invalid atomic swap, wrong m:{} policy: {:?}", m, policy)] - InvalidM{ policy: SpendPolicy, m : usize }, + InvalidM { policy: SpendPolicy, m: usize }, #[error("invalid atomic swap, not a threshold: {:?}", .0)] InvalidSpendPolicyVariant(SpendPolicy), } @@ -119,39 +119,34 @@ pub enum AtomicSwapError { pub struct AtomicSwap(SpendPolicy); impl AtomicSwap { - pub fn new(policy: SpendPolicy) -> Result { - Self::is_valid(&policy).map(|_| Self(policy)) - } + pub fn new(policy: SpendPolicy) -> Result { Self::is_valid(&policy).map(|_| Self(policy)) } - pub fn address(&self) -> Address { - self.inner().address() - } + pub fn address(&self) -> Address { self.inner().address() } - pub fn opacify(&self) -> SpendPolicy { - self.inner().opacify() - } + pub fn opacify(&self) -> SpendPolicy { self.inner().opacify() } } impl IsValidatedPolicy for AtomicSwap { type Error = AtomicSwapError; type Inner = SpendPolicy; - fn inner(&self) -> &Self::Inner { - &self.0 - } + fn inner(&self) -> &Self::Inner { &self.0 } fn is_valid(policy: &Self::Inner) -> Result<(), Self::Error> { match policy { - SpendPolicy::Threshold { - n: 1, - of - } if of.len() == 2 => { + SpendPolicy::Threshold { n: 1, of } if of.len() == 2 => { HashLockPath::is_valid(&of[0]).map_err(AtomicSwapError::InvalidHashComponent)?; TimeLockPath::is_valid(&of[1]).map_err(AtomicSwapError::InvalidTimeComponent)?; Ok(()) }, - SpendPolicy::Threshold { n: 1, of } => Err(AtomicSwapError::InvalidM{ policy: policy.clone(), m: of.len() }), - SpendPolicy::Threshold { n, of: _ } => Err(AtomicSwapError::InvalidN{ policy: policy.clone(), n: n.clone() }), + SpendPolicy::Threshold { n: 1, of } => Err(AtomicSwapError::InvalidM { + policy: policy.clone(), + m: of.len(), + }), + SpendPolicy::Threshold { n, of: _ } => Err(AtomicSwapError::InvalidN { + policy: policy.clone(), + n: *n, + }), _ => Err(AtomicSwapError::InvalidSpendPolicyVariant(policy.clone())), } } @@ -164,29 +159,29 @@ pub enum ComponentError { #[error("invalid hash lock component, not a threshold: {:?}", .0)] HashLockInvalidSpendPolicyVariant(SpendPolicy), #[error("invalid hash lock component, wrong n:{} policy: {:?}", n, policy)] - HashLockInvalidN{ policy: SpendPolicy, n : u8 }, + HashLockInvalidN { policy: SpendPolicy, n: u8 }, #[error("invalid hash lock component, wrong m:{} policy: {:?}", m, policy)] - HashLockInvalidM{ policy: SpendPolicy, m : usize }, + HashLockInvalidM { policy: SpendPolicy, m: usize }, #[error("invalid time lock component, time lock path: {:?}", .0)] TimeLockInvalidThresholdStructure(SpendPolicy), #[error("invalid time lock component, not a threshold: {:?}", .0)] TimeLockInvalidSpendPolicyVariant(SpendPolicy), #[error("invalid time lock component, wrong n:{} policy: {:?}", n, policy)] - TimeLockInvalidN{ policy: SpendPolicy, n : u8 }, + TimeLockInvalidN { policy: SpendPolicy, n: u8 }, #[error("invalid time lock component, wrong m:{} policy: {:?}", m, policy)] - TimeLockInvalidM{ policy: SpendPolicy, m : usize }, + TimeLockInvalidM { policy: SpendPolicy, m: usize }, #[error("invalid component, not SpendPolicy::Opaque: {:?}", .0)] OpaqueInvalidSpendPolicyVariant(SpendPolicy), } /// The hash locked threshold path of the atomic swap contract. /// structure: -/// SpendPolicy::Threshold { -/// n: 2, +/// SpendPolicy::Threshold { +/// n: 2, /// of: vec![ /// SpendPolicy::Hash(sha256(secret)), /// SpendPolicy::PublicKey(public_key) -/// ] +/// ] /// } /// fulfillment conditions: /// - signature from participant's public key @@ -196,30 +191,29 @@ pub enum ComponentError { pub struct HashLockPath(SpendPolicy); impl HashLockPath { - pub fn new(policy: SpendPolicy) -> Result { - Self::is_valid(&policy).map(|_| Self(policy)) - } - + pub fn new(policy: SpendPolicy) -> Result { Self::is_valid(&policy).map(|_| Self(policy)) } } impl IsValidatedPolicy for HashLockPath { type Error = ComponentError; type Inner = SpendPolicy; - fn inner(&self) -> &Self::Inner { - &self.0 - } + fn inner(&self) -> &Self::Inner { &self.0 } fn is_valid(policy: &Self::Inner) -> Result<(), Self::Error> { match policy { - SpendPolicy::Threshold{ n: 2, of } if of.len() == 2 => { - match of.as_slice() { - [SpendPolicy::Hash(_), SpendPolicy::PublicKey(_)] => Ok(()), - _ => Err(ComponentError::HashLockInvalidThresholdStructure(policy.clone())), - } + SpendPolicy::Threshold { n: 2, of } if of.len() == 2 => match of.as_slice() { + [SpendPolicy::Hash(_), SpendPolicy::PublicKey(_)] => Ok(()), + _ => Err(ComponentError::HashLockInvalidThresholdStructure(policy.clone())), }, - SpendPolicy::Threshold{ n: 2, of } => Err(ComponentError::HashLockInvalidM{ policy: policy.clone(), m: of.len() }), - SpendPolicy::Threshold{ n, of: _ } => Err(ComponentError::HashLockInvalidN{ policy: policy.clone(), n: n.clone() }), + SpendPolicy::Threshold { n: 2, of } => Err(ComponentError::HashLockInvalidM { + policy: policy.clone(), + m: of.len(), + }), + SpendPolicy::Threshold { n, of: _ } => Err(ComponentError::HashLockInvalidN { + policy: policy.clone(), + n: *n, + }), _ => Err(ComponentError::HashLockInvalidSpendPolicyVariant(policy.clone())), } } @@ -227,12 +221,12 @@ impl IsValidatedPolicy for HashLockPath { /// The time based threshold path of the atomic swap contract. /// structure: -/// SpendPolicy::Threshold { -/// n: 2, +/// SpendPolicy::Threshold { +/// n: 2, /// of: vec![ /// SpendPolicy::After(timestamp), /// SpendPolicy::PublicKey(public_key) -/// ] +/// ] /// } /// fulfillment conditions: /// - signature from participant's public key @@ -241,29 +235,29 @@ impl IsValidatedPolicy for HashLockPath { pub struct TimeLockPath(SpendPolicy); impl TimeLockPath { - pub fn new(policy: SpendPolicy) -> Result { - Self::is_valid(&policy).map(|_| Self(policy)) - } + pub fn new(policy: SpendPolicy) -> Result { Self::is_valid(&policy).map(|_| Self(policy)) } } impl IsValidatedPolicy for TimeLockPath { type Error = ComponentError; type Inner = SpendPolicy; - fn inner(&self) -> &Self::Inner { - &self.0 - } + fn inner(&self) -> &Self::Inner { &self.0 } fn is_valid(policy: &Self::Inner) -> Result<(), Self::Error> { match policy { - SpendPolicy::Threshold{ n: 2, of } if of.len() == 2 => { - match of.as_slice() { - [SpendPolicy::After(_), SpendPolicy::PublicKey(_)] => Ok(()), - _ => Err(ComponentError::TimeLockInvalidThresholdStructure(policy.clone())), - } + SpendPolicy::Threshold { n: 2, of } if of.len() == 2 => match of.as_slice() { + [SpendPolicy::After(_), SpendPolicy::PublicKey(_)] => Ok(()), + _ => Err(ComponentError::TimeLockInvalidThresholdStructure(policy.clone())), }, - SpendPolicy::Threshold{ n: 2, of } => Err(ComponentError::TimeLockInvalidM{ policy: policy.clone(), m: of.len() }), - SpendPolicy::Threshold{ n, of: _ } => Err(ComponentError::TimeLockInvalidN{ policy: policy.clone(), n: n.clone() }), + SpendPolicy::Threshold { n: 2, of } => Err(ComponentError::TimeLockInvalidM { + policy: policy.clone(), + m: of.len(), + }), + SpendPolicy::Threshold { n, of: _ } => Err(ComponentError::TimeLockInvalidN { + policy: policy.clone(), + n: *n, + }), _ => Err(ComponentError::TimeLockInvalidSpendPolicyVariant(policy.clone())), } } @@ -273,18 +267,14 @@ impl IsValidatedPolicy for TimeLockPath { pub struct Opaque(SpendPolicy); impl Opaque { - pub fn new(policy: SpendPolicy) -> Result { - Self::is_valid(&policy).map(|_| Self(policy)) - } + pub fn new(policy: SpendPolicy) -> Result { Self::is_valid(&policy).map(|_| Self(policy)) } } impl IsValidatedPolicy for Opaque { type Error = ComponentError; type Inner = SpendPolicy; - fn inner(&self) -> &Self::Inner { - &self.0 - } + fn inner(&self) -> &Self::Inner { &self.0 } fn is_valid(policy: &Self::Inner) -> Result<(), Self::Error> { match policy { @@ -298,15 +288,15 @@ impl IsValidatedPolicy for Opaque { /// Used within the input that spends the locked UTXO. /// structure: /// SatisfiedPolicy { -/// policy: SpendPolicy::Threshold { +/// policy: SpendPolicy::Threshold { /// n: 1, -/// of: vec![ -/// SpendPolicy::Threshold { -/// n: 2, +/// of: vec![ +/// SpendPolicy::Threshold { +/// n: 2, /// of: vec![ /// SpendPolicy::Hash(sha256(secret)), /// SpendPolicy::PublicKey(public_key) -/// ] +/// ] /// }, /// SpendPolicy::Opaque(), /// ] @@ -319,10 +309,24 @@ pub struct AtomicSwapSuccess(SatisfiedPolicy); #[derive(Debug, Error)] enum AtomicSwapSuccessError { - #[error("invalid atomic swap success: {:?}, invalid signatures amount: {}", satisfied_policy, amount)] - InvalidSignaturesAmount { satisfied_policy: SatisfiedPolicy, amount: usize }, - #[error("invalid atomic swap success: {:?}, invalid preimages amount: {}", satisfied_policy, amount)] - InvalidPreimagesAmount { satisfied_policy: SatisfiedPolicy, amount: usize }, + #[error( + "invalid atomic swap success: {:?}, invalid signatures amount: {}", + satisfied_policy, + amount + )] + InvalidSignaturesAmount { + satisfied_policy: SatisfiedPolicy, + amount: usize, + }, + #[error( + "invalid atomic swap success: {:?}, invalid preimages amount: {}", + satisfied_policy, + amount + )] + InvalidPreimagesAmount { + satisfied_policy: SatisfiedPolicy, + amount: usize, + }, #[error("invalid atomic swap success: {:?}, invalid policy variant", satisfied_policy)] InvalidSpendPolicyVariant { satisfied_policy: SatisfiedPolicy }, // #[error("invalid atomic swap success: {0}, atomic swap error")] @@ -335,9 +339,7 @@ impl IsValidatedPolicy for AtomicSwapSuccess { type Error = AtomicSwapSuccessError; type Inner = SatisfiedPolicy; - fn inner(&self) -> &Self::Inner { - &self.0 - } + fn inner(&self) -> &Self::Inner { &self.0 } fn is_valid(satisfied_policy: &Self::Inner) -> Result<(), Self::Error> { if satisfied_policy.signatures.len() != 1 { @@ -355,12 +357,14 @@ impl IsValidatedPolicy for AtomicSwapSuccess { } match &satisfied_policy.policy { - SpendPolicy::Threshold{ n: 1, of } if of.len() == 2 => { + SpendPolicy::Threshold { n: 1, of } if of.len() == 2 => { HashLockPath::is_valid(&of[0]).map_err(|_| AtomicSwapSuccessError::PlaceHolder)?; Opaque::is_valid(&of[1]).map_err(|_| AtomicSwapSuccessError::PlaceHolder)?; Ok(()) }, - _ => Err(AtomicSwapSuccessError::InvalidSpendPolicyVariant{ satisfied_policy: satisfied_policy.clone()}), + _ => Err(AtomicSwapSuccessError::InvalidSpendPolicyVariant { + satisfied_policy: satisfied_policy.clone(), + }), } } } @@ -371,16 +375,13 @@ mod test { use crate::types::{Hash256, PublicKey}; fn pubkey0() -> PublicKey { - PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ).unwrap() + PublicKey::from_bytes(&hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap()) + .unwrap() } fn pubkey1() -> PublicKey { - PublicKey::from_bytes( - &hex::decode("06C87838297B7BB16AB23946C99DFDF77FF834E35DB07D71E9B1D2B01A11E96D").unwrap(), - ) - .unwrap() + PublicKey::from_bytes(&hex::decode("06C87838297B7BB16AB23946C99DFDF77FF834E35DB07D71E9B1D2B01A11E96D").unwrap()) + .unwrap() } fn valid_atomic_swap_spend_policy() -> SpendPolicy { @@ -389,46 +390,32 @@ mod test { of: vec![ SpendPolicy::Threshold { n: 2, - of: vec![ - SpendPolicy::Hash(Hash256::default()), - SpendPolicy::PublicKey(pubkey0()), - ], + of: vec![SpendPolicy::Hash(Hash256::default()), SpendPolicy::PublicKey(pubkey0())], }, SpendPolicy::Threshold { n: 2, - of: vec![ - SpendPolicy::After(0), - SpendPolicy::PublicKey(pubkey1()), - ], + of: vec![SpendPolicy::After(0), SpendPolicy::PublicKey(pubkey1())], }, ], } } - + fn valid_component_hash_lock() -> SpendPolicy { SpendPolicy::Threshold { n: 2, - of: vec![ - SpendPolicy::Hash(Hash256::default()), - SpendPolicy::PublicKey(pubkey0()), - ], + of: vec![SpendPolicy::Hash(Hash256::default()), SpendPolicy::PublicKey(pubkey0())], } } fn valid_component_time_lock() -> SpendPolicy { SpendPolicy::Threshold { n: 2, - of: vec![ - SpendPolicy::After(0), - SpendPolicy::PublicKey(pubkey1()), - ], + of: vec![SpendPolicy::After(0), SpendPolicy::PublicKey(pubkey1())], } } #[test] - fn test_atomic_swap_contract_valid() { - AtomicSwap::new(valid_atomic_swap_spend_policy()).unwrap(); - } + fn test_atomic_swap_contract_valid() { AtomicSwap::new(valid_atomic_swap_spend_policy()).unwrap(); } #[test] fn test_atomic_swap_contract_invalid_hash_lock_path() { @@ -437,7 +424,7 @@ mod test { of: vec![SpendPolicy::PublicKey(pubkey0()), valid_component_time_lock()], }; - match AtomicSwap::new(policy.clone()).unwrap_err() { + match AtomicSwap::new(policy).unwrap_err() { AtomicSwapError::InvalidHashComponent(ComponentError::HashLockInvalidSpendPolicyVariant(_)) => (), _ => panic!(), } @@ -450,7 +437,7 @@ mod test { of: vec![valid_component_hash_lock(), SpendPolicy::PublicKey(pubkey0())], }; - match AtomicSwap::new(policy.clone()).unwrap_err() { + match AtomicSwap::new(policy).unwrap_err() { AtomicSwapError::InvalidTimeComponent(ComponentError::TimeLockInvalidSpendPolicyVariant(_)) => (), _ => panic!(), } @@ -463,7 +450,7 @@ mod test { of: vec![valid_component_time_lock(), valid_component_hash_lock()], }; - match AtomicSwap::new(policy.clone()).unwrap_err() { + match AtomicSwap::new(policy).unwrap_err() { AtomicSwapError::InvalidHashComponent(ComponentError::HashLockInvalidThresholdStructure(_)) => (), _ => panic!(), } @@ -473,7 +460,7 @@ mod test { fn test_atomic_swap_contract_invalid_components_too_many() { let mut policy = valid_atomic_swap_spend_policy(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { of.push(SpendPolicy::PublicKey(pubkey0())); }, _ => unreachable!(), @@ -482,7 +469,7 @@ mod test { match AtomicSwap::new(policy.clone()) { Err(AtomicSwapError::InvalidM { policy: _, m }) => { assert_eq!(m, 3); - } + }, _ => panic!(), } } @@ -491,7 +478,7 @@ mod test { fn test_atomic_swap_contract_invalid_components_missing_time_lock_path() { let mut policy = valid_atomic_swap_spend_policy(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { let _ = of.pop().unwrap(); }, _ => unreachable!(), @@ -499,7 +486,7 @@ mod test { match AtomicSwap::new(policy.clone()) { Err(AtomicSwapError::InvalidM { policy: _, m }) => { assert_eq!(m, 1); - } + }, _ => panic!(), } } @@ -508,7 +495,7 @@ mod test { fn test_atomic_swap_contract_invalid_components_missing_hash_lock_path() { let mut policy = valid_atomic_swap_spend_policy(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { let _ = of.remove(0); }, _ => unreachable!(), @@ -516,7 +503,7 @@ mod test { match AtomicSwap::new(policy.clone()) { Err(AtomicSwapError::InvalidM { policy: _, m }) => { assert_eq!(m, 1); - } + }, _ => panic!(), } } @@ -525,15 +512,15 @@ mod test { fn test_atomic_swap_contract_invalid_components_missing_both_paths() { let mut policy = valid_atomic_swap_spend_policy(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { *of = vec![]; }, _ => unreachable!(), } - match AtomicSwap::new(policy.clone()) { + match AtomicSwap::new(policy) { Err(AtomicSwapError::InvalidM { policy: _, m }) => { assert_eq!(m, 0); - } + }, _ => panic!(), } } @@ -546,10 +533,10 @@ mod test { _ => unreachable!(), } - match AtomicSwap::new(policy.clone()) { + match AtomicSwap::new(policy) { Err(AtomicSwapError::InvalidN { policy: _, n }) => { assert_eq!(n, 10); - } + }, _ => panic!(), } } @@ -558,22 +545,20 @@ mod test { fn test_atomic_swap_contract_invalid_policy_variant() { let policy = SpendPolicy::PublicKey(pubkey0()); - match AtomicSwap::new(policy.clone()) { + match AtomicSwap::new(policy) { Err(AtomicSwapError::InvalidSpendPolicyVariant { .. }) => (), _ => panic!(), } } #[test] - fn test_atomic_swap_component_hash_lock_valid() { - HashLockPath::new(valid_component_hash_lock()).unwrap(); - } + fn test_atomic_swap_component_hash_lock_valid() { HashLockPath::new(valid_component_hash_lock()).unwrap(); } #[test] fn test_atomic_swap_component_hash_lock_invalid_threshold_structure() { let policy = SpendPolicy::Threshold { n: 2, - of: vec![SpendPolicy::PublicKey(pubkey0()) , SpendPolicy::PublicKey(pubkey0())], + of: vec![SpendPolicy::PublicKey(pubkey0()), SpendPolicy::PublicKey(pubkey0())], }; match HashLockPath::new(policy.clone()).unwrap_err() { @@ -586,7 +571,7 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_wrong_order() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { of.reverse(); }, _ => unreachable!(), @@ -602,14 +587,14 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_too_many() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { of.push(SpendPolicy::PublicKey(pubkey0())); }, _ => unreachable!(), } match HashLockPath::new(policy).unwrap_err() { - ComponentError::HashLockInvalidM{ policy: _, m } => assert_eq!(m, 3), + ComponentError::HashLockInvalidM { policy: _, m } => assert_eq!(m, 3), _ => panic!(), } } @@ -618,14 +603,12 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_missing_public_key() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![SpendPolicy::Hash(Hash256::default())] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![SpendPolicy::Hash(Hash256::default())], _ => unreachable!(), } match HashLockPath::new(policy).unwrap_err() { - ComponentError::HashLockInvalidM{ policy: _, m } => assert_eq!(m, 1), + ComponentError::HashLockInvalidM { policy: _, m } => assert_eq!(m, 1), _ => panic!(), } } @@ -634,14 +617,12 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_missing_hash() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![SpendPolicy::PublicKey(pubkey0())] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![SpendPolicy::PublicKey(pubkey0())], _ => unreachable!(), } match HashLockPath::new(policy).unwrap_err() { - ComponentError::HashLockInvalidM{ policy: _, m } => assert_eq!(m, 1), + ComponentError::HashLockInvalidM { policy: _, m } => assert_eq!(m, 1), _ => panic!(), } } @@ -650,14 +631,12 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_empty_threshold() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![], _ => unreachable!(), } match HashLockPath::new(policy).unwrap_err() { - ComponentError::HashLockInvalidM{ policy: _, m } => assert_eq!(m, 0), + ComponentError::HashLockInvalidM { policy: _, m } => assert_eq!(m, 0), _ => panic!(), } } @@ -666,14 +645,14 @@ mod test { fn test_atomic_swap_component_hash_lock_invalid_n() { let mut policy = valid_component_hash_lock(); match &mut policy { - SpendPolicy::Threshold { n, of:_ } => { + SpendPolicy::Threshold { n, of: _ } => { *n = 10; }, _ => unreachable!(), } match HashLockPath::new(policy).unwrap_err() { - ComponentError::HashLockInvalidN{ policy: _, n } => assert_eq!(n, 10), + ComponentError::HashLockInvalidN { policy: _, n } => assert_eq!(n, 10), _ => panic!(), } } @@ -689,15 +668,13 @@ mod test { } #[test] - fn test_atomic_swap_component_time_lock_valid() { - TimeLockPath::new(valid_component_time_lock()).unwrap(); - } + fn test_atomic_swap_component_time_lock_valid() { TimeLockPath::new(valid_component_time_lock()).unwrap(); } #[test] fn test_atomic_swap_component_time_lock_invalid_threshold_structure() { let policy = SpendPolicy::Threshold { n: 2, - of: vec![SpendPolicy::PublicKey(pubkey0()) , SpendPolicy::PublicKey(pubkey0())], + of: vec![SpendPolicy::PublicKey(pubkey0()), SpendPolicy::PublicKey(pubkey0())], }; match TimeLockPath::new(policy.clone()).unwrap_err() { @@ -710,7 +687,7 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_wrong_order() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { of.reverse(); }, _ => unreachable!(), @@ -726,14 +703,14 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_too_many() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { + SpendPolicy::Threshold { n: _, of } => { of.push(SpendPolicy::PublicKey(pubkey0())); }, _ => unreachable!(), } match TimeLockPath::new(policy).unwrap_err() { - ComponentError::TimeLockInvalidM{ policy: _, m } => assert_eq!(m, 3), + ComponentError::TimeLockInvalidM { policy: _, m } => assert_eq!(m, 3), _ => panic!(), } } @@ -742,14 +719,12 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_missing_public_key() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![SpendPolicy::After(0)] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![SpendPolicy::After(0)], _ => unreachable!(), } match TimeLockPath::new(policy).unwrap_err() { - ComponentError::TimeLockInvalidM{ policy: _, m } => assert_eq!(m, 1), + ComponentError::TimeLockInvalidM { policy: _, m } => assert_eq!(m, 1), _ => panic!(), } } @@ -758,14 +733,12 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_missing_time() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![SpendPolicy::PublicKey(pubkey1())] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![SpendPolicy::PublicKey(pubkey1())], _ => unreachable!(), } match TimeLockPath::new(policy).unwrap_err() { - ComponentError::TimeLockInvalidM{ policy: _, m } => assert_eq!(m, 1), + ComponentError::TimeLockInvalidM { policy: _, m } => assert_eq!(m, 1), _ => panic!(), } } @@ -774,14 +747,12 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_empty_threshold() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n:_, of } => { - *of = vec![] - }, + SpendPolicy::Threshold { n: _, of } => *of = vec![], _ => unreachable!(), } match TimeLockPath::new(policy).unwrap_err() { - ComponentError::TimeLockInvalidM{ policy: _, m } => assert_eq!(m, 0), + ComponentError::TimeLockInvalidM { policy: _, m } => assert_eq!(m, 0), _ => panic!(), } } @@ -790,14 +761,14 @@ mod test { fn test_atomic_swap_component_time_lock_invalid_n() { let mut policy = valid_component_time_lock(); match &mut policy { - SpendPolicy::Threshold { n, of:_ } => { + SpendPolicy::Threshold { n, of: _ } => { *n = 10; }, _ => unreachable!(), } match TimeLockPath::new(policy).unwrap_err() { - ComponentError::TimeLockInvalidN{ policy: _, n } => assert_eq!(n, 10), + ComponentError::TimeLockInvalidN { policy: _, n } => assert_eq!(n, 10), _ => panic!(), } } diff --git a/src/types/keypair.rs b/src/types/keypair.rs index 8fb6299..4324e23 100644 --- a/src/types/keypair.rs +++ b/src/types/keypair.rs @@ -30,7 +30,7 @@ impl Signer for Keypair { /// Sign a message with this keypair's secret key. fn try_sign(&self, message: &[u8]) -> Result { let expanded: ExpandedSecretKey = (&self.private.0).into(); - Ok(Signature::new(expanded.sign(&message, &self.public.0))) + Ok(Signature::new(expanded.sign(message, &self.public.0))) } } diff --git a/src/types/spend_policy.rs b/src/types/spend_policy.rs index 7650e28..fc657cd 100644 --- a/src/types/spend_policy.rs +++ b/src/types/spend_policy.rs @@ -1,6 +1,6 @@ use crate::blake2b_internal::{public_key_leaf, sigs_required_leaf, standard_unlock_hash, timelock_leaf, Accumulator}; use crate::encoding::{Encodable, Encoder}; -use crate::types::{Address, Hash256, PublicKey, Signature, Specifier, Preimage, SatisfiedPolicy}; +use crate::types::{Address, Hash256, Preimage, PublicKey, SatisfiedPolicy, Signature, Specifier}; use nom::bytes::complete::{take_until, take_while, take_while_m_n}; use nom::character::complete::char; use nom::combinator::all_consuming; @@ -180,29 +180,24 @@ impl SpendPolicy { pub fn atomic_swap(alice: PublicKey, bob: PublicKey, lock_time: u64, hash: Hash256) -> Self { let policy_after = SpendPolicy::After(lock_time); let policy_hash = SpendPolicy::Hash(hash); - + let policy_success = SpendPolicy::Threshold { n: 2, of: vec![SpendPolicy::PublicKey(alice), policy_hash], }; - + let policy_refund = SpendPolicy::Threshold { n: 2, of: vec![SpendPolicy::PublicKey(bob), policy_after], }; - + SpendPolicy::Threshold { n: 1, of: vec![policy_success, policy_refund], } } - pub fn atomic_swap_success( - alice: PublicKey, - bob: PublicKey, - lock_time: u64, - hash: Hash256, - ) -> Self { + pub fn atomic_swap_success(alice: PublicKey, bob: PublicKey, lock_time: u64, hash: Hash256) -> Self { match Self::atomic_swap(alice, bob, lock_time, hash) { SpendPolicy::Threshold { n, mut of } => { of[1] = of[1].opacify(); diff --git a/src/types/transaction.rs b/src/types/transaction.rs index 5372fde..e0d81e3 100644 --- a/src/types/transaction.rs +++ b/src/types/transaction.rs @@ -1,5 +1,6 @@ use crate::encoding::{Encodable, Encoder}; -use crate::types::{Address, ChainIndex, Hash256, Keypair, PublicKey, Signature, SpendPolicy, UnlockCondition, UnlockKey}; +use crate::types::{Address, ChainIndex, Hash256, Keypair, PublicKey, Signature, SpendPolicy, UnlockCondition, + UnlockKey}; use base64::{engine::general_purpose::STANDARD as base64, Engine as _}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_json::Value; From 349765c103674bbd3b78481fb49131b2d6ed04e6 Mon Sep 17 00:00:00 2001 From: laruh Date: Fri, 25 Oct 2024 20:37:15 +0700 Subject: [PATCH 3/3] wip clippy err --- src/transport/client/helpers.rs | 4 ++-- src/types/signature.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transport/client/helpers.rs b/src/transport/client/helpers.rs index 37ae54c..8438cec 100644 --- a/src/transport/client/helpers.rs +++ b/src/transport/client/helpers.rs @@ -82,10 +82,10 @@ pub trait ApiClientHelpers: ApiClient { if selected_amount < *total_amount { return Err(ApiClientHelpersError::SelectOutputs { available: selected_amount.into(), - required: total_amount.into(), + required: total_amount, }); } - let change = selected_amount as u128 - *total_amount; + let change = selected_amount - *total_amount; Ok((selected, change.into())) } diff --git a/src/types/signature.rs b/src/types/signature.rs index 3e6bf35..61ea86a 100644 --- a/src/types/signature.rs +++ b/src/types/signature.rs @@ -73,7 +73,7 @@ impl Signature { } pub fn verify(&self, message: &[u8], public_key: &PublicKey) -> Result<(), SignatureError> { - public_key.verify(message, &self) + public_key.verify(message, self) } }