From f98da64e70057455c037f5db0d8840d6c5995a80 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Tue, 3 Oct 2023 09:30:21 +0200 Subject: [PATCH 1/7] Divided int tests into citadel and blockchain --- integration-tests/tests/blockchain/mod.rs | 8 ++++++++ .../tests/{ => blockchain}/retrieve_txs.rs | 0 .../tests/{ => blockchain}/stake_add_owner.rs | 0 integration-tests/tests/{ => citadel}/int_test_lp.rs | 0 .../tests/{ => citadel}/int_test_user.rs | 0 .../tests/{ => citadel}/issue_license.rs | 0 integration-tests/tests/citadel/mod.rs | 12 ++++++++++++ integration-tests/tests/{ => citadel}/queries.rs | 0 .../tests/{ => citadel}/retrieve_requests.rs | 0 .../tests/{ => citadel}/send_request.rs | 0 integration-tests/tests/tests.rs | 2 ++ 11 files changed, 22 insertions(+) create mode 100644 integration-tests/tests/blockchain/mod.rs rename integration-tests/tests/{ => blockchain}/retrieve_txs.rs (100%) rename integration-tests/tests/{ => blockchain}/stake_add_owner.rs (100%) rename integration-tests/tests/{ => citadel}/int_test_lp.rs (100%) rename integration-tests/tests/{ => citadel}/int_test_user.rs (100%) rename integration-tests/tests/{ => citadel}/issue_license.rs (100%) create mode 100644 integration-tests/tests/citadel/mod.rs rename integration-tests/tests/{ => citadel}/queries.rs (100%) rename integration-tests/tests/{ => citadel}/retrieve_requests.rs (100%) rename integration-tests/tests/{ => citadel}/send_request.rs (100%) diff --git a/integration-tests/tests/blockchain/mod.rs b/integration-tests/tests/blockchain/mod.rs new file mode 100644 index 0000000..2b6fa07 --- /dev/null +++ b/integration-tests/tests/blockchain/mod.rs @@ -0,0 +1,8 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +mod retrieve_txs; +mod stake_add_owner; diff --git a/integration-tests/tests/retrieve_txs.rs b/integration-tests/tests/blockchain/retrieve_txs.rs similarity index 100% rename from integration-tests/tests/retrieve_txs.rs rename to integration-tests/tests/blockchain/retrieve_txs.rs diff --git a/integration-tests/tests/stake_add_owner.rs b/integration-tests/tests/blockchain/stake_add_owner.rs similarity index 100% rename from integration-tests/tests/stake_add_owner.rs rename to integration-tests/tests/blockchain/stake_add_owner.rs diff --git a/integration-tests/tests/int_test_lp.rs b/integration-tests/tests/citadel/int_test_lp.rs similarity index 100% rename from integration-tests/tests/int_test_lp.rs rename to integration-tests/tests/citadel/int_test_lp.rs diff --git a/integration-tests/tests/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs similarity index 100% rename from integration-tests/tests/int_test_user.rs rename to integration-tests/tests/citadel/int_test_user.rs diff --git a/integration-tests/tests/issue_license.rs b/integration-tests/tests/citadel/issue_license.rs similarity index 100% rename from integration-tests/tests/issue_license.rs rename to integration-tests/tests/citadel/issue_license.rs diff --git a/integration-tests/tests/citadel/mod.rs b/integration-tests/tests/citadel/mod.rs new file mode 100644 index 0000000..c397d77 --- /dev/null +++ b/integration-tests/tests/citadel/mod.rs @@ -0,0 +1,12 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +mod int_test_user; +mod issue_license; +mod queries; +mod send_request; +mod retrieve_requests; +mod int_test_lp; diff --git a/integration-tests/tests/queries.rs b/integration-tests/tests/citadel/queries.rs similarity index 100% rename from integration-tests/tests/queries.rs rename to integration-tests/tests/citadel/queries.rs diff --git a/integration-tests/tests/retrieve_requests.rs b/integration-tests/tests/citadel/retrieve_requests.rs similarity index 100% rename from integration-tests/tests/retrieve_requests.rs rename to integration-tests/tests/citadel/retrieve_requests.rs diff --git a/integration-tests/tests/send_request.rs b/integration-tests/tests/citadel/send_request.rs similarity index 100% rename from integration-tests/tests/send_request.rs rename to integration-tests/tests/citadel/send_request.rs diff --git a/integration-tests/tests/tests.rs b/integration-tests/tests/tests.rs index df6010c..d85a2dc 100644 --- a/integration-tests/tests/tests.rs +++ b/integration-tests/tests/tests.rs @@ -5,3 +5,5 @@ // Copyright (c) DUSK NETWORK. All rights reserved. mod websocket; +mod citadel; +mod blockchain; From 9aa6bfe447f61dedeaff19fbecdeb6c843bf18d4 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Tue, 3 Oct 2023 11:10:25 +0200 Subject: [PATCH 2/7] Moved citadel request related code to separate submodule --- moat-core/src/blockchain_requests/mod.rs | 4 ---- moat-core/src/citadel_requests/mod.rs | 11 +++++++++++ .../request_creator.rs | 0 .../request_scanner.rs | 2 +- moat-core/src/lib.rs | 7 +++++-- 5 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 moat-core/src/citadel_requests/mod.rs rename moat-core/src/{blockchain_requests => citadel_requests}/request_creator.rs (100%) rename moat-core/src/{blockchain_requests => citadel_requests}/request_scanner.rs (96%) diff --git a/moat-core/src/blockchain_requests/mod.rs b/moat-core/src/blockchain_requests/mod.rs index 977c339..21f9d8d 100644 --- a/moat-core/src/blockchain_requests/mod.rs +++ b/moat-core/src/blockchain_requests/mod.rs @@ -7,11 +7,7 @@ mod payload_extractor; mod payload_retriever; mod payload_sender; -mod request_creator; -mod request_scanner; pub use payload_extractor::PayloadExtractor; pub use payload_retriever::PayloadRetriever; pub use payload_sender::PayloadSender; -pub use request_creator::RequestCreator; -pub use request_scanner::RequestScanner; diff --git a/moat-core/src/citadel_requests/mod.rs b/moat-core/src/citadel_requests/mod.rs new file mode 100644 index 0000000..889eb0f --- /dev/null +++ b/moat-core/src/citadel_requests/mod.rs @@ -0,0 +1,11 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +mod request_creator; +mod request_scanner; + +pub use request_creator::RequestCreator; +pub use request_scanner::RequestScanner; diff --git a/moat-core/src/blockchain_requests/request_creator.rs b/moat-core/src/citadel_requests/request_creator.rs similarity index 100% rename from moat-core/src/blockchain_requests/request_creator.rs rename to moat-core/src/citadel_requests/request_creator.rs diff --git a/moat-core/src/blockchain_requests/request_scanner.rs b/moat-core/src/citadel_requests/request_scanner.rs similarity index 96% rename from moat-core/src/blockchain_requests/request_scanner.rs rename to moat-core/src/citadel_requests/request_scanner.rs index 441cf77..51aed1d 100644 --- a/moat-core/src/blockchain_requests/request_scanner.rs +++ b/moat-core/src/citadel_requests/request_scanner.rs @@ -19,7 +19,7 @@ impl RequestScanner { let mut requests = Vec::new(); for tx in &txs.transactions { if let Ok(request) = - PayloadExtractor::payload_from_tx::(tx) + PayloadExtractor::payload_from_tx::(tx) { requests.push(request) } diff --git a/moat-core/src/lib.rs b/moat-core/src/lib.rs index ce2c189..154fb26 100644 --- a/moat-core/src/lib.rs +++ b/moat-core/src/lib.rs @@ -13,6 +13,7 @@ mod bc_types; mod blockchain_requests; +mod citadel_requests; mod circuit; mod citadel_queries; mod citadel_types; @@ -23,8 +24,10 @@ mod tx_retrievals; pub use bc_types::*; pub use blockchain_requests::{ - PayloadExtractor, PayloadRetriever, PayloadSender, RequestCreator, - RequestScanner, + PayloadExtractor, PayloadRetriever, PayloadSender, +}; +pub use citadel_requests::{ + RequestCreator, RequestScanner }; pub use circuit::*; pub use citadel_queries::{ From b5da2e3653f72b7bb5c98f4b97368fd7f056e0f9 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Tue, 3 Oct 2023 12:47:00 +0200 Subject: [PATCH 3/7] Reintroduced request sender, cleaned BlsScalar depenencies --- README.md | 18 +++++++++ integration-tests/Cargo.toml | 2 +- .../tests/citadel/int_test_user.rs | 2 +- integration-tests/tests/citadel/mod.rs | 4 +- .../tests/citadel/send_request.rs | 12 +++--- integration-tests/tests/tests.rs | 4 +- integration-tests/tests/websocket/mod.rs | 1 + .../tests/websocket/ws_queries.rs | 3 +- .../tests/websocket/ws_server.rs | 2 +- license-provider/Cargo.toml | 1 + license-provider/src/license_issuer.rs | 3 +- moat-cli/src/main.rs | 9 +---- .../src/blockchain_requests/payload_sender.rs | 2 +- .../src/citadel_queries/citadel_types.rs | 2 +- moat-core/src/citadel_requests/mod.rs | 2 + .../src/citadel_requests/request_creator.rs | 5 +++ .../src/citadel_requests/request_scanner.rs | 2 +- .../src/citadel_requests/request_sender.rs | 37 +++++++++++++++++++ moat-core/src/lib.rs | 6 +-- 19 files changed, 87 insertions(+), 30 deletions(-) create mode 100644 moat-core/src/citadel_requests/request_sender.rs diff --git a/README.md b/README.md index bfb965e..3832471 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,21 @@ This is a low-level crate which provides wallet (Blockchain) connectivity for fu Users of moat-core do not need to be aware of this crate, yet for maintainers and extenders, the crate provides a convenient low level interface between the higher-level moat-core library and the blockchain. Note that this crate deals only with contract method calling, it does not deal with contract queries. + +## moat-core + +### creating license requests +Class: RequestCreator +Methods: create, create_from_hex_args +Both methods allow for creation of a request, given user's secret spend key and license provider's public spend key. +The request can then be sent to license provider, off-chain or on-chain. + +### sending license requests +Class: RequestSender +Methods: send_request +Sends a request to blockchain, by calling a "noop" method of the license contract. + +### scanning license requests +Class: +Methods: scan_last_blocks, scan_block_range +Scans requests in a collection of blocks, given either as a number of last blocks, or as a range of blocks. diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 6a2de53..e84f1a3 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -14,7 +14,7 @@ phoenix-core = { version = "0.20.0-rc.0", features = ["alloc"] } poseidon-merkle = { version = "0.2.1-rc.0", features = ["rkyv-impl"] } dusk-pki = { version = "0.12", default-features = false, features = ["rkyv-impl"] } dusk-plonk = { version = "0.14", default-features = false, features = ["rkyv-impl", "alloc"] } -dusk-jubjub = { version = "0.12", default-features = false } +dusk-bls12_381 = "0.11" wallet-accessor = { path = "../wallet-accessor" } rkyv = { version = "=0.7.39" } tokio = { version = "1.15", features = ["rt-multi-thread", "time", "fs", "macros"] } diff --git a/integration-tests/tests/citadel/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs index b3a1cda..1be932a 100644 --- a/integration-tests/tests/citadel/int_test_user.rs +++ b/integration-tests/tests/citadel/int_test_user.rs @@ -21,8 +21,8 @@ use bytecheck::CheckBytes; use bytes::Bytes; +use dusk_bls12_381::BlsScalar; use dusk_bytes::{DeserializableSlice, Serializable}; -use dusk_jubjub::BlsScalar; use dusk_pki::{PublicSpendKey, SecretSpendKey}; use dusk_plonk::prelude::*; use dusk_wallet::{RuskHttpClient, WalletPath}; diff --git a/integration-tests/tests/citadel/mod.rs b/integration-tests/tests/citadel/mod.rs index c397d77..b3330b1 100644 --- a/integration-tests/tests/citadel/mod.rs +++ b/integration-tests/tests/citadel/mod.rs @@ -4,9 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +mod int_test_lp; mod int_test_user; mod issue_license; mod queries; -mod send_request; mod retrieve_requests; -mod int_test_lp; +mod send_request; diff --git a/integration-tests/tests/citadel/send_request.rs b/integration-tests/tests/citadel/send_request.rs index a708a40..a108cd7 100644 --- a/integration-tests/tests/citadel/send_request.rs +++ b/integration-tests/tests/citadel/send_request.rs @@ -4,11 +4,11 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use dusk_wallet::{RuskHttpClient, WalletPath}; use moat_core::{ - Error, PayloadRetriever, PayloadSender, RequestCreator, RequestJson, - LICENSE_CONTRACT_ID, MAX_REQUEST_SIZE, NOOP_METHOD_NAME, + Error, PayloadRetriever, RequestCreator, RequestJson, RequestSender, + MAX_REQUEST_SIZE, }; use moat_core::{JsonLoader, TxAwaiter}; use rand::rngs::StdRng; @@ -59,15 +59,13 @@ async fn send_request() -> Result<(), Error> { PathBuf::from(WALLET_PATH).as_path().join("wallet.dat"), ); - let tx_id = PayloadSender::send_to_contract_method( - (request, 0u64, BlsScalar::one()), + let tx_id = RequestSender::send_request( + request, &config, &wallet_path, &PwdHash(PWD_HASH.to_string()), GAS_LIMIT, GAS_PRICE, - LICENSE_CONTRACT_ID, - NOOP_METHOD_NAME, ) .await?; let client = RuskHttpClient::new(config.rusk_address); diff --git a/integration-tests/tests/tests.rs b/integration-tests/tests/tests.rs index d85a2dc..8fc8fab 100644 --- a/integration-tests/tests/tests.rs +++ b/integration-tests/tests/tests.rs @@ -4,6 +4,6 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -mod websocket; -mod citadel; mod blockchain; +mod citadel; +mod websocket; diff --git a/integration-tests/tests/websocket/mod.rs b/integration-tests/tests/websocket/mod.rs index 1a99aac..a6fa734 100644 --- a/integration-tests/tests/websocket/mod.rs +++ b/integration-tests/tests/websocket/mod.rs @@ -6,6 +6,7 @@ pub mod ws_client; pub mod ws_common; +pub mod ws_queries; pub mod ws_server; pub mod ws_test; diff --git a/integration-tests/tests/websocket/ws_queries.rs b/integration-tests/tests/websocket/ws_queries.rs index 1663c2e..4106c0a 100644 --- a/integration-tests/tests/websocket/ws_queries.rs +++ b/integration-tests/tests/websocket/ws_queries.rs @@ -4,8 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use crate::websocket::ws_license_contract_mock_multi_server; +use dusk_bls12_381::BlsScalar; use moat_core::{CitadelInquirerWs, Error, LicenseSession, LicenseSessionId}; -use dusk_jubjub::BlsScalar; const TEST_DURATION_SECONDS: u64 = 4; const PORT: u32 = 9126; diff --git a/integration-tests/tests/websocket/ws_server.rs b/integration-tests/tests/websocket/ws_server.rs index 61f6284..3178299 100644 --- a/integration-tests/tests/websocket/ws_server.rs +++ b/integration-tests/tests/websocket/ws_server.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use crate::websocket::ws_common::*; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use futures_util::{SinkExt, StreamExt}; use moat_core::{Error, LicenseSession, MAX_RESPONSE_SIZE}; use tokio::net::{TcpListener, TcpStream}; diff --git a/license-provider/Cargo.toml b/license-provider/Cargo.toml index b222fbc..56a05bd 100644 --- a/license-provider/Cargo.toml +++ b/license-provider/Cargo.toml @@ -10,6 +10,7 @@ moat-core={ path = "../moat-core" } wallet-accessor = { path = "../wallet-accessor" } dusk-poseidon = { version = "0.30", default-features = false, features = ["rkyv-impl", "alloc"] } dusk-jubjub = { version = "0.12", default-features = false } +dusk-bls12_381 = "0.11" dusk-pki = { version = "0.12", default-features = false, features = ["rkyv-impl"] } rusk-abi = { version = "0.10.0-piecrust.0.6", default-features = false } rkyv = { version = "=0.7.39" } diff --git a/license-provider/src/license_issuer.rs b/license-provider/src/license_issuer.rs index fa07cb2..ee2ca2e 100644 --- a/license-provider/src/license_issuer.rs +++ b/license-provider/src/license_issuer.rs @@ -4,7 +4,8 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_jubjub::{BlsScalar, JubJubAffine, JubJubScalar}; +use dusk_bls12_381::BlsScalar; +use dusk_jubjub::{JubJubAffine, JubJubScalar}; use dusk_pki::SecretSpendKey; use dusk_poseidon::sponge; use dusk_wallet::{RuskHttpClient, WalletPath}; diff --git a/moat-cli/src/main.rs b/moat-cli/src/main.rs index cddc91a..d2501a9 100644 --- a/moat-cli/src/main.rs +++ b/moat-cli/src/main.rs @@ -60,10 +60,7 @@ use crate::args::Args; use clap::Parser; use dusk_wallet::WalletPath; -use moat_core::{ - JsonLoader, PayloadSender, RequestCreator, RequestJson, - LICENSE_CONTRACT_ID, NOOP_METHOD_NAME, -}; +use moat_core::{JsonLoader, RequestCreator, RequestJson, RequestSender}; use rand::rngs::StdRng; use rand::SeedableRng; use std::error::Error; @@ -107,15 +104,13 @@ async fn main() -> Result<(), Box> { PwdHash(pwd_hash) }; - PayloadSender::send_to_contract_method( + RequestSender::send_request( request, &blockchain_access_config, &wallet_path, &psw, gas_limit, gas_price, - LICENSE_CONTRACT_ID, - NOOP_METHOD_NAME, ) .await?; diff --git a/moat-core/src/blockchain_requests/payload_sender.rs b/moat-core/src/blockchain_requests/payload_sender.rs index 6f6be2b..8fd423d 100644 --- a/moat-core/src/blockchain_requests/payload_sender.rs +++ b/moat-core/src/blockchain_requests/payload_sender.rs @@ -6,7 +6,7 @@ use crate::error::Error; use crate::MAX_CALL_SIZE; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use dusk_wallet::WalletPath; use phoenix_core::transaction::ModuleId; use rkyv::ser::serializers::AllocSerializer; diff --git a/moat-core/src/citadel_queries/citadel_types.rs b/moat-core/src/citadel_queries/citadel_types.rs index ec84d55..daa4820 100644 --- a/moat-core/src/citadel_queries/citadel_types.rs +++ b/moat-core/src/citadel_queries/citadel_types.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use bytecheck::CheckBytes; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use rkyv::{Archive, Deserialize, Serialize}; /// License Session Id diff --git a/moat-core/src/citadel_requests/mod.rs b/moat-core/src/citadel_requests/mod.rs index 889eb0f..1c0cb1c 100644 --- a/moat-core/src/citadel_requests/mod.rs +++ b/moat-core/src/citadel_requests/mod.rs @@ -6,6 +6,8 @@ mod request_creator; mod request_scanner; +mod request_sender; pub use request_creator::RequestCreator; pub use request_scanner::RequestScanner; +pub use request_sender::RequestSender; diff --git a/moat-core/src/citadel_requests/request_creator.rs b/moat-core/src/citadel_requests/request_creator.rs index ba0a9d0..1a69449 100644 --- a/moat-core/src/citadel_requests/request_creator.rs +++ b/moat-core/src/citadel_requests/request_creator.rs @@ -15,6 +15,8 @@ use zk_citadel::license::Request; pub struct RequestCreator; impl RequestCreator { + /// creates request from user's secret spend key + /// and license provider's public spend key pub fn create( ssk_user: &SecretSpendKey, psk_lp: &PublicSpendKey, @@ -31,6 +33,9 @@ impl RequestCreator { Ok(request) } + /// creates request from user's secret spend key + /// and license provider's public spend key + /// parameters are expected to be hexadecimal strings pub fn create_from_hex_args< R: RngCore + CryptoRng, S: AsRef, diff --git a/moat-core/src/citadel_requests/request_scanner.rs b/moat-core/src/citadel_requests/request_scanner.rs index 51aed1d..441cf77 100644 --- a/moat-core/src/citadel_requests/request_scanner.rs +++ b/moat-core/src/citadel_requests/request_scanner.rs @@ -19,7 +19,7 @@ impl RequestScanner { let mut requests = Vec::new(); for tx in &txs.transactions { if let Ok(request) = - PayloadExtractor::payload_from_tx::(tx) + PayloadExtractor::payload_from_tx::(tx) { requests.push(request) } diff --git a/moat-core/src/citadel_requests/request_sender.rs b/moat-core/src/citadel_requests/request_sender.rs new file mode 100644 index 0000000..5b575d6 --- /dev/null +++ b/moat-core/src/citadel_requests/request_sender.rs @@ -0,0 +1,37 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +use crate::{Error, PayloadSender, LICENSE_CONTRACT_ID, NOOP_METHOD_NAME}; +use dusk_bls12_381::BlsScalar; +use dusk_wallet::WalletPath; +use wallet_accessor::{BlockchainAccessConfig, Password}; +use zk_citadel::license::Request; + +pub struct RequestSender; + +impl RequestSender { + pub async fn send_request( + request: Request, + config: &BlockchainAccessConfig, + wallet_path: &WalletPath, + password: &Password, + gas_limit: u64, + gas_price: u64, + ) -> Result { + let tx_id = PayloadSender::send_to_contract_method( + (request, 0u64, BlsScalar::one()), // todo: explain this, + &config, + &wallet_path, + password, + gas_limit, + gas_price, + LICENSE_CONTRACT_ID, + NOOP_METHOD_NAME, + ) + .await?; + Ok(tx_id) + } +} diff --git a/moat-core/src/lib.rs b/moat-core/src/lib.rs index 154fb26..d0841cc 100644 --- a/moat-core/src/lib.rs +++ b/moat-core/src/lib.rs @@ -13,9 +13,9 @@ mod bc_types; mod blockchain_requests; -mod citadel_requests; mod circuit; mod citadel_queries; +mod citadel_requests; mod citadel_types; mod contract_queries; mod error; @@ -26,13 +26,11 @@ pub use bc_types::*; pub use blockchain_requests::{ PayloadExtractor, PayloadRetriever, PayloadSender, }; -pub use citadel_requests::{ - RequestCreator, RequestScanner -}; pub use circuit::*; pub use citadel_queries::{ CitadelInquirer, CitadelInquirerWs, LicenseSession, LicenseSessionId, }; +pub use citadel_requests::{RequestCreator, RequestScanner, RequestSender}; pub use citadel_types::*; pub use contract_queries::{ block::*, ContractInquirer, ContractInquirerWs, StreamAux, From 00d8c34dc303d3d1a9dc0e1b378c5830fa2b1cfe Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Wed, 4 Oct 2023 13:57:04 +0200 Subject: [PATCH 4/7] Introduced blockchain queries --- README.md | 75 +++++++++++++++++-- .../tests/blockchain/retrieve_txs.rs | 10 +-- .../bc_inquirer.rs | 0 .../mod.rs | 4 +- .../tx_awaiter.rs | 2 + .../tx_inquirer.rs} | 14 ++-- .../blockchain_requests/payload_retriever.rs | 4 +- .../src/citadel_requests/request_scanner.rs | 6 +- moat-core/src/lib.rs | 4 +- 9 files changed, 88 insertions(+), 31 deletions(-) rename moat-core/src/{tx_retrievals => blockchain_queries}/bc_inquirer.rs (100%) rename moat-core/src/{tx_retrievals => blockchain_queries}/mod.rs (87%) rename moat-core/src/{tx_retrievals => blockchain_queries}/tx_awaiter.rs (98%) rename moat-core/src/{tx_retrievals/tx_retriever.rs => blockchain_queries/tx_inquirer.rs} (92%) diff --git a/README.md b/README.md index 3832471..b5aa01b 100644 --- a/README.md +++ b/README.md @@ -100,18 +100,77 @@ Note that this crate deals only with contract method calling, it does not deal w ## moat-core -### creating license requests +### citadel requests + Class: RequestCreator -Methods: create, create_from_hex_args +Methods: + create, + create_from_hex_args Both methods allow for creation of a request, given user's secret spend key and license provider's public spend key. The request can then be sent to license provider, off-chain or on-chain. -### sending license requests Class: RequestSender Methods: send_request -Sends a request to blockchain, by calling a "noop" method of the license contract. +Submits the request into blockchain. + +Class: RequestScanner +Methods: + scan_transactions, + scan_last_blocks, + scan_block_range +Methods scan requests in a given collection of transactions, +in a given range of blocks, +or in a given number of most recent blocks. + +### citadel queries + +Class: CitadelInquirer +Methods: + get_licenses, + get_merkle_opening, + get_session, + get_info + +### blockchain requests + +Class: PayloadExtractor +Methods: payload_from_tx +Extracts payload from transaction, if present (i.e., it is a contract calling transaction) + +Class: PayloadRetriever +Methods: retrieve_payload +Retrieves payload of a given transaction in a blockchain, on the condition that the transaction is found and contains a payload (i.e., it is a contract calling transaction) + +Class: PayloadSender +Methods: send_to_contract_method +Calls a given method of a given contract, passing to it a payload. + +### contract queries + +Class: ContractInquirer +Methods: + query_contract, + query_contract_with_feeder +query_contract: accepts generic argument A, contract id and method name and returns generic value R +query_contract_with_feeder: accepts generic argument A, contract id and method name and returns a Stream of type Bytes + +### blockchain queries + +Class: BcInquirer +Methods: + gql_query, + block_height + +Class: TxAwaiter +Methods: + wait_for, + wait_for_tx +Waits for transaction identified by a given transaction id to be confirmed on the blockchain. + +Class: TxInquirer +Methods: + txs_from_block, + txs_from_block_range, + txs_from_last_n_blocks, + retrieve_tx -### scanning license requests -Class: -Methods: scan_last_blocks, scan_block_range -Scans requests in a collection of blocks, given either as a number of last blocks, or as a range of blocks. diff --git a/integration-tests/tests/blockchain/retrieve_txs.rs b/integration-tests/tests/blockchain/retrieve_txs.rs index 71b13a1..4598c84 100644 --- a/integration-tests/tests/blockchain/retrieve_txs.rs +++ b/integration-tests/tests/blockchain/retrieve_txs.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_wallet::RuskHttpClient; -use moat_core::{Error, TxRetriever}; +use moat_core::{Error, TxInquirer}; use toml_base_config::BaseConfig; use tracing::trace; use wallet_accessor::BlockchainAccessConfig; @@ -22,7 +22,7 @@ async fn retrieve_txs_from_block() -> Result<(), Error> { const BLOCK_HEIGHT: u64 = 110; - let txs = TxRetriever::txs_from_block(&client, BLOCK_HEIGHT).await?; + let txs = TxInquirer::txs_from_block(&client, BLOCK_HEIGHT).await?; trace!("transactions retrieved={}", txs.transactions.len()); @@ -42,7 +42,7 @@ async fn retrieve_txs_from_block_range() -> Result<(), Error> { const BLOCK_HEIGHT_BEG: u64 = 1; const BLOCK_HEIGHT_END: u64 = 1000; - let (txs, top_block) = TxRetriever::txs_from_block_range( + let (txs, top_block) = TxInquirer::txs_from_block_range( &client, BLOCK_HEIGHT_BEG, BLOCK_HEIGHT_END, @@ -66,7 +66,7 @@ async fn retrieve_txs_from_last_n_blocks() -> Result<(), Error> { let client = RuskHttpClient::new(cfg.rusk_address); const N: usize = 10000; - let txs = TxRetriever::txs_from_last_n_blocks(&client, N).await?; + let txs = TxInquirer::txs_from_last_n_blocks(&client, N).await?; trace!("transactions={}", txs.transactions.len()); @@ -86,7 +86,7 @@ async fn retrieve_tx_by_id() -> Result<(), Error> { let client = RuskHttpClient::new(config.rusk_address); - let (tx, height) = TxRetriever::retrieve_tx(TXID, &client).await?; + let (tx, height) = TxInquirer::retrieve_tx(TXID, &client).await?; trace!("tx={:?}, block_height={}", tx, height); diff --git a/moat-core/src/tx_retrievals/bc_inquirer.rs b/moat-core/src/blockchain_queries/bc_inquirer.rs similarity index 100% rename from moat-core/src/tx_retrievals/bc_inquirer.rs rename to moat-core/src/blockchain_queries/bc_inquirer.rs diff --git a/moat-core/src/tx_retrievals/mod.rs b/moat-core/src/blockchain_queries/mod.rs similarity index 87% rename from moat-core/src/tx_retrievals/mod.rs rename to moat-core/src/blockchain_queries/mod.rs index 3b989d1..a6ff26f 100644 --- a/moat-core/src/tx_retrievals/mod.rs +++ b/moat-core/src/blockchain_queries/mod.rs @@ -6,8 +6,8 @@ mod bc_inquirer; mod tx_awaiter; -mod tx_retriever; +mod tx_inquirer; pub use bc_inquirer::BcInquirer; pub use tx_awaiter::TxAwaiter; -pub use tx_retriever::TxRetriever; +pub use tx_inquirer::TxInquirer; diff --git a/moat-core/src/tx_retrievals/tx_awaiter.rs b/moat-core/src/blockchain_queries/tx_awaiter.rs similarity index 98% rename from moat-core/src/tx_retrievals/tx_awaiter.rs rename to moat-core/src/blockchain_queries/tx_awaiter.rs index 8998077..c53a46f 100644 --- a/moat-core/src/tx_retrievals/tx_awaiter.rs +++ b/moat-core/src/blockchain_queries/tx_awaiter.rs @@ -55,6 +55,7 @@ impl TxAwaiter { } pub async fn wait_for( + // todo: rename client: &RuskHttpClient, tx_id: BlsScalar, ) -> Result<(), Error> { @@ -63,6 +64,7 @@ impl TxAwaiter { } async fn wait_for_tx( + // todo: rename client: &RuskHttpClient, tx_id: impl AsRef, ) -> Result<(), Error> { diff --git a/moat-core/src/tx_retrievals/tx_retriever.rs b/moat-core/src/blockchain_queries/tx_inquirer.rs similarity index 92% rename from moat-core/src/tx_retrievals/tx_retriever.rs rename to moat-core/src/blockchain_queries/tx_inquirer.rs index 1afa3a7..739ec2d 100644 --- a/moat-core/src/tx_retrievals/tx_retriever.rs +++ b/moat-core/src/blockchain_queries/tx_inquirer.rs @@ -10,20 +10,16 @@ use crate::Error::TransactionNotFound; use crate::{BcInquirer, QueryResult}; use dusk_wallet::RuskHttpClient; -pub struct TxRetriever; +pub struct TxInquirer; -impl TxRetriever { +impl TxInquirer { pub async fn txs_from_block( client: &RuskHttpClient, block_height: u64, ) -> Result { - TxRetriever::txs_from_block_range( - client, - block_height, - block_height + 1, - ) - .await - .map(|(txs, _)| txs) + TxInquirer::txs_from_block_range(client, block_height, block_height + 1) + .await + .map(|(txs, _)| txs) } // range retrieval seems to have a limit of 10k diff --git a/moat-core/src/blockchain_requests/payload_retriever.rs b/moat-core/src/blockchain_requests/payload_retriever.rs index bd816b5..f20bc55 100644 --- a/moat-core/src/blockchain_requests/payload_retriever.rs +++ b/moat-core/src/blockchain_requests/payload_retriever.rs @@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Infallible}; use crate::blockchain_requests::PayloadExtractor; use crate::error::Error; -use crate::TxRetriever; +use crate::TxInquirer; pub struct PayloadRetriever; @@ -27,7 +27,7 @@ impl PayloadRetriever { + for<'b> CheckBytes>, S: AsRef, { - let (tx, _) = TxRetriever::retrieve_tx(txid.as_ref(), client).await?; + let (tx, _) = TxInquirer::retrieve_tx(txid.as_ref(), client).await?; PayloadExtractor::payload_from_tx(&tx) } } diff --git a/moat-core/src/citadel_requests/request_scanner.rs b/moat-core/src/citadel_requests/request_scanner.rs index 441cf77..b2d2d06 100644 --- a/moat-core/src/citadel_requests/request_scanner.rs +++ b/moat-core/src/citadel_requests/request_scanner.rs @@ -6,7 +6,7 @@ use crate::blockchain_requests::PayloadExtractor; use crate::error::Error; -use crate::{Transactions, TxRetriever}; +use crate::{Transactions, TxInquirer}; use dusk_wallet::RuskHttpClient; use wallet_accessor::BlockchainAccessConfig; use zk_citadel::license::Request; @@ -34,7 +34,7 @@ impl RequestScanner { ) -> Result, Error> { let client = RuskHttpClient::new(cfg.rusk_address.clone()); let txs = - TxRetriever::txs_from_last_n_blocks(&client, last_n_blocks).await?; + TxInquirer::txs_from_last_n_blocks(&client, last_n_blocks).await?; let requests = RequestScanner::scan_transactions(txs); Ok(requests) } @@ -47,7 +47,7 @@ impl RequestScanner { ) -> Result<(Vec, u64), Error> { let client = RuskHttpClient::new(cfg.rusk_address.clone()); let (txs, top) = - TxRetriever::txs_from_block_range(&client, height_beg, height_end) + TxInquirer::txs_from_block_range(&client, height_beg, height_end) .await?; let requests = RequestScanner::scan_transactions(txs); Ok((requests, top)) diff --git a/moat-core/src/lib.rs b/moat-core/src/lib.rs index d0841cc..32076ca 100644 --- a/moat-core/src/lib.rs +++ b/moat-core/src/lib.rs @@ -12,6 +12,7 @@ //! subproject. mod bc_types; +mod blockchain_queries; mod blockchain_requests; mod circuit; mod citadel_queries; @@ -20,9 +21,9 @@ mod citadel_types; mod contract_queries; mod error; mod json_loader; -mod tx_retrievals; pub use bc_types::*; +pub use blockchain_queries::{BcInquirer, TxAwaiter, TxInquirer}; pub use blockchain_requests::{ PayloadExtractor, PayloadRetriever, PayloadSender, }; @@ -37,4 +38,3 @@ pub use contract_queries::{ }; pub use error::Error; pub use json_loader::JsonLoader; -pub use tx_retrievals::{BcInquirer, TxAwaiter, TxRetriever}; From a64a07b6b90063c76e765ebda59ab765f1114c42 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Wed, 4 Oct 2023 16:45:30 +0200 Subject: [PATCH 5/7] Added submitting request to blockchain in the round trip test --- .../tests/citadel/int_test_user.rs | 36 +++++++++++++------ .../src/blockchain_queries/tx_awaiter.rs | 2 -- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/integration-tests/tests/citadel/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs index 1be932a..ac7fef9 100644 --- a/integration-tests/tests/citadel/int_test_user.rs +++ b/integration-tests/tests/citadel/int_test_user.rs @@ -30,8 +30,9 @@ use license_provider::{LicenseIssuer, ReferenceLP}; use moat_core::Error::InvalidQueryResponse; use moat_core::{ BcInquirer, CitadelInquirer, Error, JsonLoader, LicenseCircuit, - LicenseSessionId, PayloadSender, RequestCreator, RequestJson, StreamAux, - TxAwaiter, ARITY, DEPTH, LICENSE_CONTRACT_ID, USE_LICENSE_METHOD_NAME, + LicenseSessionId, PayloadRetriever, PayloadSender, RequestCreator, + RequestJson, RequestSender, StreamAux, TxAwaiter, ARITY, DEPTH, + LICENSE_CONTRACT_ID, USE_LICENSE_METHOD_NAME, }; use poseidon_merkle::Opening; use rand::rngs::StdRng; @@ -270,20 +271,37 @@ async fn user_round_trip() -> Result<(), Error> { let client = RuskHttpClient::new(blockchain_config.rusk_address.clone()); // create request - let request_json: RequestJson = RequestJson::from_file(request_path)?; + let ssk_user_bytes = hex::decode(request_json.user_ssk.clone())?; + let ssk_user = SecretSpendKey::from_slice(ssk_user_bytes.as_slice())?; let request = RequestCreator::create_from_hex_args( - request_json.user_ssk.clone(), + request_json.user_ssk, request_json.provider_psk, rng, )?; - let ssk_user_bytes = hex::decode(request_json.user_ssk)?; - let ssk_user = SecretSpendKey::from_slice(ssk_user_bytes.as_slice())?; + // as a User, submit request to blockchain + info!("submitting request to blockchain (as a User)"); + let tx_id = RequestSender::send_request( + request, + &blockchain_config, + &wallet_path, + &PwdHash(PWD_HASH.to_string()), + GAS_LIMIT, + GAS_PRICE, + ) + .await?; + TxAwaiter::wait_for(&client, tx_id).await?; - // as a LP, call issue license, wait for tx to confirm + // as a LP, retrieve request from blockchain + info!("retrieving request from blockchain (as an LP)"); + let tx_id = format!("{:X}", tx_id); + let (request, _, _): (Request, u64, BlsScalar) = + // let request: Request = + PayloadRetriever::retrieve_payload(tx_id, &client).await?; + // as a LP, call issue license, wait for tx to confirm show_state(&client, "before issue_license").await?; info!("calling issue_license (as an LP)"); let issue_license_txid = issue_license( @@ -300,7 +318,6 @@ async fn user_round_trip() -> Result<(), Error> { info!("end_height={}", end_height); // as a User, call get_licenses, obtain license and pos - let start_height = if end_height > BLOCK_RANGE { end_height - BLOCK_RANGE } else { @@ -319,13 +336,11 @@ async fn user_round_trip() -> Result<(), Error> { .expect("owned license found"); // as a User, call get_merkle_opening, obtain opening - info!("calling get_merkle_opening (as a user)"); let opening = CitadelInquirer::get_merkle_opening(&client, pos).await?; assert!(opening.is_some()); // as a User, compute proof, call use_license, wait for tx to confirm - show_state(&client, "before use_license").await?; info!("calling use_license (as a user)"); let session_id = prove_and_send_use_license( @@ -345,7 +360,6 @@ async fn user_round_trip() -> Result<(), Error> { let session_id = LicenseSessionId { id: session_id }; // as an SP, call get_session - info!("calling get_session (as an SP)"); let session = CitadelInquirer::get_session(&client, session_id).await?; assert!(session.is_some()); diff --git a/moat-core/src/blockchain_queries/tx_awaiter.rs b/moat-core/src/blockchain_queries/tx_awaiter.rs index c53a46f..8998077 100644 --- a/moat-core/src/blockchain_queries/tx_awaiter.rs +++ b/moat-core/src/blockchain_queries/tx_awaiter.rs @@ -55,7 +55,6 @@ impl TxAwaiter { } pub async fn wait_for( - // todo: rename client: &RuskHttpClient, tx_id: BlsScalar, ) -> Result<(), Error> { @@ -64,7 +63,6 @@ impl TxAwaiter { } async fn wait_for_tx( - // todo: rename client: &RuskHttpClient, tx_id: impl AsRef, ) -> Result<(), Error> { From 7d4e9664c4fef59eafb726c8788c6474a471c6c1 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Wed, 4 Oct 2023 16:53:56 +0200 Subject: [PATCH 6/7] Eliminated sending triple when submitting request --- README.md | 12 +++++++----- .../tests/blockchain/stake_add_owner.rs | 2 +- integration-tests/tests/citadel/int_test_lp.rs | 8 ++++---- integration-tests/tests/citadel/int_test_user.rs | 7 +++---- integration-tests/tests/citadel/issue_license.rs | 2 +- integration-tests/tests/citadel/send_request.rs | 5 ++--- license-provider/src/license_issuer.rs | 2 +- license-provider/src/reference_lp.rs | 2 +- license-provider/tests/test_lp.rs | 2 +- .../mod.rs | 0 .../payload_extractor.rs | 0 .../payload_retriever.rs | 2 +- .../payload_sender.rs | 2 +- moat-core/src/citadel_requests/request_scanner.rs | 2 +- moat-core/src/citadel_requests/request_sender.rs | 4 ++-- moat-core/src/lib.rs | 6 +++--- 16 files changed, 29 insertions(+), 29 deletions(-) rename moat-core/src/{blockchain_requests => blockchain_payloads}/mod.rs (100%) rename moat-core/src/{blockchain_requests => blockchain_payloads}/payload_extractor.rs (100%) rename moat-core/src/{blockchain_requests => blockchain_payloads}/payload_retriever.rs (95%) rename moat-core/src/{blockchain_requests => blockchain_payloads}/payload_sender.rs (96%) diff --git a/README.md b/README.md index b5aa01b..319d06e 100644 --- a/README.md +++ b/README.md @@ -131,19 +131,21 @@ Methods: get_session, get_info -### blockchain requests +### blockchain payloads Class: PayloadExtractor Methods: payload_from_tx -Extracts payload from transaction, if present (i.e., it is a contract calling transaction) +Extracts payload from transaction, errors if payload of a given type is not present +in the transaction or a give transaction is not a contract calling transaction. Class: PayloadRetriever Methods: retrieve_payload -Retrieves payload of a given transaction in a blockchain, on the condition that the transaction is found and contains a payload (i.e., it is a contract calling transaction) +Retrieves payload of a given transaction in a blockchain, errors if the transaction +is not found or it doesn't contain a payload (e.g., given transaction is not a contract calling transaction) Class: PayloadSender -Methods: send_to_contract_method -Calls a given method of a given contract, passing to it a payload. +Methods: execute_contract_method +Executes a given method of a given contract, passing to it the given payload as an argument. ### contract queries diff --git a/integration-tests/tests/blockchain/stake_add_owner.rs b/integration-tests/tests/blockchain/stake_add_owner.rs index bf68ec7..f1df0de 100644 --- a/integration-tests/tests/blockchain/stake_add_owner.rs +++ b/integration-tests/tests/blockchain/stake_add_owner.rs @@ -43,7 +43,7 @@ async fn stake_add_owner() -> Result<(), Error> { PathBuf::from(WALLET_PATH).as_path().join("wallet.dat"), ); - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( request_json.provider_psk, &blockchain_config, &wallet_path, diff --git a/integration-tests/tests/citadel/int_test_lp.rs b/integration-tests/tests/citadel/int_test_lp.rs index 8a84797..20474b3 100644 --- a/integration-tests/tests/citadel/int_test_lp.rs +++ b/integration-tests/tests/citadel/int_test_lp.rs @@ -20,7 +20,7 @@ async fn lp_scan() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp = ReferenceLP::init(&lp_config_path)?; + let mut reference_lp = ReferenceLP::create(&lp_config_path)?; reference_lp.scan(&blockchain_config).await?; Ok(()) @@ -37,7 +37,7 @@ async fn lp_scan_last_blocks() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp = ReferenceLP::init(&lp_config_path)?; + let mut reference_lp = ReferenceLP::create(&lp_config_path)?; let (_total, _owned) = reference_lp .scan_last_blocks(10000, &blockchain_config) @@ -58,8 +58,8 @@ async fn lp_scan_2_lps() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp1 = ReferenceLP::init(&lp1_config_path)?; - let mut reference_lp2 = ReferenceLP::init(&lp2_config_path)?; + let mut reference_lp1 = ReferenceLP::create(&lp1_config_path)?; + let mut reference_lp2 = ReferenceLP::create(&lp2_config_path)?; let (_, _lp1_count) = reference_lp1.scan(&blockchain_config).await?; let (_, _lp2_count) = reference_lp2.scan(&blockchain_config).await?; Ok(()) diff --git a/integration-tests/tests/citadel/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs index ac7fef9..f4f5fd3 100644 --- a/integration-tests/tests/citadel/int_test_user.rs +++ b/integration-tests/tests/citadel/int_test_user.rs @@ -147,7 +147,7 @@ async fn prove_and_send_use_license( public_inputs, }; - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( use_license_arg, &blockchain_config, &wallet_path, @@ -259,7 +259,7 @@ async fn user_round_trip() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp2.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; @@ -297,8 +297,7 @@ async fn user_round_trip() -> Result<(), Error> { // as a LP, retrieve request from blockchain info!("retrieving request from blockchain (as an LP)"); let tx_id = format!("{:X}", tx_id); - let (request, _, _): (Request, u64, BlsScalar) = - // let request: Request = + let request: Request = PayloadRetriever::retrieve_payload(tx_id, &client).await?; // as a LP, call issue license, wait for tx to confirm diff --git a/integration-tests/tests/citadel/issue_license.rs b/integration-tests/tests/citadel/issue_license.rs index c3e2161..eb4e027 100644 --- a/integration-tests/tests/citadel/issue_license.rs +++ b/integration-tests/tests/citadel/issue_license.rs @@ -31,7 +31,7 @@ async fn issue_license() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp2.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let request_json: RequestJson = RequestJson::from_file(request_path)?; diff --git a/integration-tests/tests/citadel/send_request.rs b/integration-tests/tests/citadel/send_request.rs index a108cd7..06d7429 100644 --- a/integration-tests/tests/citadel/send_request.rs +++ b/integration-tests/tests/citadel/send_request.rs @@ -4,7 +4,6 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_bls12_381::BlsScalar; use dusk_wallet::{RuskHttpClient, WalletPath}; use moat_core::{ Error, PayloadRetriever, RequestCreator, RequestJson, RequestSender, @@ -73,7 +72,7 @@ async fn send_request() -> Result<(), Error> { let tx_id_hex = format!("{:x}", tx_id); - let (retrieved_request, _, _) = + let retrieved_request = get_request_from_blockchain(tx_id_hex, &client).await?; assert_eq!( request_vec, @@ -89,7 +88,7 @@ async fn send_request() -> Result<(), Error> { async fn get_request_from_blockchain>( tx_id: S, client: &RuskHttpClient, -) -> Result<(Request, u64, BlsScalar), Error> { +) -> Result { const NUM_RETRIES: i32 = 30; for i in 0..NUM_RETRIES { let result = diff --git a/license-provider/src/license_issuer.rs b/license-provider/src/license_issuer.rs index ee2ca2e..2b6a01b 100644 --- a/license-provider/src/license_issuer.rs +++ b/license-provider/src/license_issuer.rs @@ -63,7 +63,7 @@ impl LicenseIssuer { "sending issue license with license blob size={}", tuple.0.len() ); - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( tuple, &self.config, &self.wallet_path, diff --git a/license-provider/src/reference_lp.rs b/license-provider/src/reference_lp.rs index 3aff0c7..edaa722 100644 --- a/license-provider/src/reference_lp.rs +++ b/license-provider/src/reference_lp.rs @@ -45,7 +45,7 @@ impl ReferenceLP { } } - pub fn init>(lp_config_path: P) -> Result { + pub fn create>(lp_config_path: P) -> Result { let lp_config: LPConfig = LPConfig::from_file(lp_config_path)?; let psk_bytes = hex::decode(lp_config.psk_lp)?; let ssk_bytes = hex::decode(lp_config.ssk_lp)?; diff --git a/license-provider/tests/test_lp.rs b/license-provider/tests/test_lp.rs index b09d006..88f5887 100644 --- a/license-provider/tests/test_lp.rs +++ b/license-provider/tests/test_lp.rs @@ -11,7 +11,7 @@ use moat_core::{Error, JsonLoader, RequestScanner, Transactions}; fn lp_filter_requests() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let txs_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/tx/transactions.json"); diff --git a/moat-core/src/blockchain_requests/mod.rs b/moat-core/src/blockchain_payloads/mod.rs similarity index 100% rename from moat-core/src/blockchain_requests/mod.rs rename to moat-core/src/blockchain_payloads/mod.rs diff --git a/moat-core/src/blockchain_requests/payload_extractor.rs b/moat-core/src/blockchain_payloads/payload_extractor.rs similarity index 100% rename from moat-core/src/blockchain_requests/payload_extractor.rs rename to moat-core/src/blockchain_payloads/payload_extractor.rs diff --git a/moat-core/src/blockchain_requests/payload_retriever.rs b/moat-core/src/blockchain_payloads/payload_retriever.rs similarity index 95% rename from moat-core/src/blockchain_requests/payload_retriever.rs rename to moat-core/src/blockchain_payloads/payload_retriever.rs index f20bc55..738a712 100644 --- a/moat-core/src/blockchain_requests/payload_retriever.rs +++ b/moat-core/src/blockchain_payloads/payload_retriever.rs @@ -9,7 +9,7 @@ use dusk_wallet::RuskHttpClient; use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize, Infallible}; -use crate::blockchain_requests::PayloadExtractor; +use crate::blockchain_payloads::PayloadExtractor; use crate::error::Error; use crate::TxInquirer; diff --git a/moat-core/src/blockchain_requests/payload_sender.rs b/moat-core/src/blockchain_payloads/payload_sender.rs similarity index 96% rename from moat-core/src/blockchain_requests/payload_sender.rs rename to moat-core/src/blockchain_payloads/payload_sender.rs index 8fd423d..e151d48 100644 --- a/moat-core/src/blockchain_requests/payload_sender.rs +++ b/moat-core/src/blockchain_payloads/payload_sender.rs @@ -17,7 +17,7 @@ pub struct PayloadSender; impl PayloadSender { /// Sends payload to a given method #[allow(clippy::too_many_arguments)] - pub async fn send_to_contract_method( + pub async fn execute_contract_method( payload: P, cfg: &BlockchainAccessConfig, wallet_path: &WalletPath, diff --git a/moat-core/src/citadel_requests/request_scanner.rs b/moat-core/src/citadel_requests/request_scanner.rs index b2d2d06..67f94df 100644 --- a/moat-core/src/citadel_requests/request_scanner.rs +++ b/moat-core/src/citadel_requests/request_scanner.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::blockchain_requests::PayloadExtractor; +use crate::blockchain_payloads::PayloadExtractor; use crate::error::Error; use crate::{Transactions, TxInquirer}; use dusk_wallet::RuskHttpClient; diff --git a/moat-core/src/citadel_requests/request_sender.rs b/moat-core/src/citadel_requests/request_sender.rs index 5b575d6..c3b4d8f 100644 --- a/moat-core/src/citadel_requests/request_sender.rs +++ b/moat-core/src/citadel_requests/request_sender.rs @@ -21,8 +21,8 @@ impl RequestSender { gas_limit: u64, gas_price: u64, ) -> Result { - let tx_id = PayloadSender::send_to_contract_method( - (request, 0u64, BlsScalar::one()), // todo: explain this, + let tx_id = PayloadSender::execute_contract_method( + request, &config, &wallet_path, password, diff --git a/moat-core/src/lib.rs b/moat-core/src/lib.rs index 32076ca..1d94737 100644 --- a/moat-core/src/lib.rs +++ b/moat-core/src/lib.rs @@ -12,8 +12,8 @@ //! subproject. mod bc_types; +mod blockchain_payloads; mod blockchain_queries; -mod blockchain_requests; mod circuit; mod citadel_queries; mod citadel_requests; @@ -23,10 +23,10 @@ mod error; mod json_loader; pub use bc_types::*; -pub use blockchain_queries::{BcInquirer, TxAwaiter, TxInquirer}; -pub use blockchain_requests::{ +pub use blockchain_payloads::{ PayloadExtractor, PayloadRetriever, PayloadSender, }; +pub use blockchain_queries::{BcInquirer, TxAwaiter, TxInquirer}; pub use circuit::*; pub use citadel_queries::{ CitadelInquirer, CitadelInquirerWs, LicenseSession, LicenseSessionId, From c5175362fb10bc8b40f05bd31123d10876112e60 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Thu, 5 Oct 2023 15:22:11 +0200 Subject: [PATCH 7/7] Made challenge in test dependent on the number of sessions --- README.md | 28 ++++++++------ .../tests/citadel/int_test_user.rs | 15 ++++++-- .../{queries.rs => license_queries.rs} | 37 ++++++++++++++++++- integration-tests/tests/citadel/mod.rs | 2 +- .../src/citadel_requests/request_sender.rs | 4 +- 5 files changed, 66 insertions(+), 20 deletions(-) rename integration-tests/tests/citadel/{queries.rs => license_queries.rs} (58%) diff --git a/README.md b/README.md index 319d06e..535c347 100644 --- a/README.md +++ b/README.md @@ -112,15 +112,15 @@ The request can then be sent to license provider, off-chain or on-chain. Class: RequestSender Methods: send_request Submits the request into blockchain. +It does so by calling a dummy contract method with request as an argument. Class: RequestScanner Methods: scan_transactions, scan_last_blocks, scan_block_range -Methods scan requests in a given collection of transactions, -in a given range of blocks, -or in a given number of most recent blocks. +Scan requests in a given collection of transactions, +contained in a given range of blocks or in a given number of most recent blocks. ### citadel queries @@ -130,22 +130,24 @@ Methods: get_merkle_opening, get_session, get_info +Execute citadel-specific query methods of the license contract method. ### blockchain payloads Class: PayloadExtractor Methods: payload_from_tx -Extracts payload from transaction, errors if payload of a given type is not present -in the transaction or a give transaction is not a contract calling transaction. +Extracts a payload from the given transaction, +errors if payload of a given type is not present or the transaction is not a contract calling transaction. Class: PayloadRetriever Methods: retrieve_payload -Retrieves payload of a given transaction in a blockchain, errors if the transaction -is not found or it doesn't contain a payload (e.g., given transaction is not a contract calling transaction) +Retrieves payload of a given transaction id, +errors if transaction is not found, or it does not contain a payload +(for example, given transaction is not a contract calling transaction) Class: PayloadSender Methods: execute_contract_method -Executes a given method of a given contract, passing to it the given payload as an argument. +Executes given method of a given contract (identified by a contract id), passing to it the payload as an argument. ### contract queries @@ -153,8 +155,8 @@ Class: ContractInquirer Methods: query_contract, query_contract_with_feeder -query_contract: accepts generic argument A, contract id and method name and returns generic value R -query_contract_with_feeder: accepts generic argument A, contract id and method name and returns a Stream of type Bytes +query_contract - accepts a generic argument, contract id and contract query method name, returns a generic value result +query_contract_with_feeder - accepts a generic argument, contract id and method name, returns result as a Stream of bytes ### blockchain queries @@ -162,12 +164,14 @@ Class: BcInquirer Methods: gql_query, block_height +gql_query - executes a GQL query and returns result as a vector of bytes +block_height - returns the current block height as u64 Class: TxAwaiter Methods: wait_for, wait_for_tx -Waits for transaction identified by a given transaction id to be confirmed on the blockchain. +Waits for a transaction identified by transaction id to be confirmed on the blockchain. Class: TxInquirer Methods: @@ -175,4 +179,4 @@ Methods: txs_from_block_range, txs_from_last_n_blocks, retrieve_tx - +Retrieve transaction identified by transaction id, or transactions contained in a given block, or a collection of blocks. diff --git a/integration-tests/tests/citadel/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs index f4f5fd3..6d09d0e 100644 --- a/integration-tests/tests/citadel/int_test_user.rs +++ b/integration-tests/tests/citadel/int_test_user.rs @@ -70,15 +70,14 @@ fn compute_citadel_parameters( psk_lp: PublicSpendKey, lic: &License, merkle_proof: Opening<(), DEPTH, ARITY>, + challenge: &JubJubScalar, ) -> (CitadelProverParameters, SessionCookie) { - const CHALLENGE: u64 = 20221127u64; - let c = JubJubScalar::from(CHALLENGE); let (cpp, sc) = CitadelProverParameters::compute_parameters( &ssk, &lic, &psk_lp, &psk_lp, - &c, + challenge, rng, merkle_proof, ); @@ -121,6 +120,7 @@ async fn prove_and_send_use_license( license: &License, opening: Opening<(), DEPTH, ARITY>, rng: &mut StdRng, + challenge: &JubJubScalar, ) -> Result { let (cpp, sc) = compute_citadel_parameters( rng, @@ -128,6 +128,7 @@ async fn prove_and_send_use_license( reference_lp.psk_lp, license, opening, + &challenge, ); let circuit = LicenseCircuit::new(&cpp, &sc); @@ -336,11 +337,16 @@ async fn user_round_trip() -> Result<(), Error> { // as a User, call get_merkle_opening, obtain opening info!("calling get_merkle_opening (as a user)"); - let opening = CitadelInquirer::get_merkle_opening(&client, pos).await?; + let opening = + CitadelInquirer::get_merkle_opening(&client, pos.clone()).await?; assert!(opening.is_some()); // as a User, compute proof, call use_license, wait for tx to confirm show_state(&client, "before use_license").await?; + // for test purposes we make challenge dependent on the number of sessions, + // so that it is different every time we run the test + let (_, _, num_sessions) = CitadelInquirer::get_info(&client).await?; + let challenge = JubJubScalar::from(num_sessions as u64 + 1); info!("calling use_license (as a user)"); let session_id = prove_and_send_use_license( &client, @@ -353,6 +359,7 @@ async fn user_round_trip() -> Result<(), Error> { &license, opening.unwrap(), rng, + &challenge, ) .await?; show_state(&client, "after use_license").await?; diff --git a/integration-tests/tests/citadel/queries.rs b/integration-tests/tests/citadel/license_queries.rs similarity index 58% rename from integration-tests/tests/citadel/queries.rs rename to integration-tests/tests/citadel/license_queries.rs index 9d2df31..ba403d4 100644 --- a/integration-tests/tests/citadel/queries.rs +++ b/integration-tests/tests/citadel/license_queries.rs @@ -4,8 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use dusk_bls12_381::BlsScalar; use dusk_wallet::RuskHttpClient; -use moat_core::{CitadelInquirer, Error, StreamAux}; +use moat_core::{CitadelInquirer, Error, LicenseSessionId, StreamAux}; use toml_base_config::BaseConfig; use tracing::trace; use wallet_accessor::BlockchainAccessConfig; @@ -44,3 +45,37 @@ async fn call_get_merkle_opening() -> Result<(), Error> { trace!("response={:?}", response); Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +#[cfg_attr(not(feature = "int_tests"), ignore)] +async fn call_get_session() -> Result<(), Error> { + let config_path = + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/config.toml"); + let config = BlockchainAccessConfig::load_path(config_path)?; + + let client = RuskHttpClient::new(config.rusk_address); + + let response = CitadelInquirer::get_session( + &client, + LicenseSessionId { + id: BlsScalar::one(), + }, + ) + .await?; + trace!("response={:?}", response); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +#[cfg_attr(not(feature = "int_tests"), ignore)] +async fn call_get_info() -> Result<(), Error> { + let config_path = + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/config.toml"); + let config = BlockchainAccessConfig::load_path(config_path)?; + + let client = RuskHttpClient::new(config.rusk_address); + + let response = CitadelInquirer::get_info(&client).await?; + trace!("response={:?}", response); + Ok(()) +} diff --git a/integration-tests/tests/citadel/mod.rs b/integration-tests/tests/citadel/mod.rs index b3330b1..16df4dc 100644 --- a/integration-tests/tests/citadel/mod.rs +++ b/integration-tests/tests/citadel/mod.rs @@ -7,6 +7,6 @@ mod int_test_lp; mod int_test_user; mod issue_license; -mod queries; +mod license_queries; mod retrieve_requests; mod send_request; diff --git a/moat-core/src/citadel_requests/request_sender.rs b/moat-core/src/citadel_requests/request_sender.rs index c3b4d8f..17834a4 100644 --- a/moat-core/src/citadel_requests/request_sender.rs +++ b/moat-core/src/citadel_requests/request_sender.rs @@ -23,8 +23,8 @@ impl RequestSender { ) -> Result { let tx_id = PayloadSender::execute_contract_method( request, - &config, - &wallet_path, + config, + wallet_path, password, gas_limit, gas_price,