Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring, clean-up, constants #18

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ futures-core = "0.3"
reqwest = "0.11"

[features]
int_tests = []
exp_tests = []
vol_tests = []
int_tests = [] #integration tests - require a running Rusk node/cluster
exp_tests = [] #expensive tests - integration tests which take longer time to run
vol_tests = [] #volatile tests - integration tests which require constants' update before running
5 changes: 2 additions & 3 deletions integration-tests/tests/int_test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async fn issue_license(
/// Calculates and verified proof, sends proof along with public parameters
/// as arguments to the license contract's use_license method.
/// Awaits for confirmation of the contract-calling transaction.
async fn use_license(
async fn prove_and_send_use_license(
client: &RuskHttpClient,
blockchain_config: &BlockchainAccessConfig,
wallet_path: &WalletPath,
Expand Down Expand Up @@ -305,7 +305,7 @@ async fn user_round_trip() -> Result<(), Error> {

show_state(&client, "before use_license").await?;
info!("calling use_license (as a user)");
let session_id = use_license(
let session_id = prove_and_send_use_license(
&client,
&blockchain_config,
&wallet_path,
Expand Down Expand Up @@ -333,6 +333,5 @@ async fn user_round_trip() -> Result<(), Error> {
);

// if we try to call use_license again, it will be rejected
// (currently, it panics giving Trap(UnreachableCodeReached))
Ok(())
}
10 changes: 3 additions & 7 deletions integration-tests/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
use dusk_wallet::RuskHttpClient;
use moat_core::{CitadelInquirer, Error, StreamAux};
use toml_base_config::BaseConfig;
use tracing::debug;
use tracing::trace;
use wallet_accessor::BlockchainAccessConfig;

#[allow(dead_code)]
const MAX_CALL_SIZE: usize = 65536;

#[tokio::test(flavor = "multi_thread")]
#[cfg_attr(not(feature = "int_tests"), ignore)]
async fn call_get_licenses() -> Result<(), Error> {
Expand All @@ -28,7 +25,7 @@ async fn call_get_licenses() -> Result<(), Error> {

const ITEM_LEN: usize = CitadelInquirer::GET_LICENSES_ITEM_LEN;
let response = StreamAux::collect_all::<(u64, Vec<u8>), ITEM_LEN>(stream)?;
debug!("response={:?}", response);
trace!("response={:?}", response);
Ok(())
}

Expand All @@ -44,7 +41,6 @@ async fn call_get_merkle_opening() -> Result<(), Error> {
let pos = 0u64;

let response = CitadelInquirer::get_merkle_opening(&client, pos).await?;

debug!("response={:?}", response);
trace!("response={:?}", response);
Ok(())
}
2 changes: 1 addition & 1 deletion integration-tests/tests/retrieve_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn scan_requests_in_last_blocks() -> Result<(), Error> {

let requests = RequestScanner::scan_last_blocks(LAST_BLOCKS, &cfg).await?;
trace!(
"there were {} requests found in last n={} blocks",
"there were {} requests found in last {} blocks",
requests.len(),
LAST_BLOCKS
);
Expand Down
2 changes: 0 additions & 2 deletions integration-tests/tests/retrieve_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ async fn retrieve_txs_from_block_range() -> Result<(), Error> {
)
.await?;

assert!(top_block > 0);

trace!("transactions retrieved={}", txs.transactions.len());
trace!("current top block={}", top_block);

Expand Down
7 changes: 5 additions & 2 deletions integration-tests/tests/send_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use dusk_jubjub::BlsScalar;
use dusk_wallet::{RuskHttpClient, WalletPath};
use moat_core::{
Error, PayloadRetriever, PayloadSender, RequestCreator, RequestJson,
MAX_REQUEST_SIZE,
};
use moat_core::{JsonLoader, TxAwaiter};
use rand::rngs::StdRng;
Expand Down Expand Up @@ -48,7 +49,9 @@ async fn send_request() -> Result<(), Error> {
request_json.provider_psk,
rng,
)?;
let request_vec = rkyv::to_bytes::<_, 8192>(&request).unwrap().to_vec();
let request_vec = rkyv::to_bytes::<_, MAX_REQUEST_SIZE>(&request)
.unwrap()
.to_vec();

let config = BlockchainAccessConfig::load_path(config_path)?;

Expand All @@ -74,7 +77,7 @@ async fn send_request() -> Result<(), Error> {
get_request_from_blockchain(tx_id_hex, &client).await?;
assert_eq!(
request_vec,
rkyv::to_bytes::<_, 8192>(&retrieved_request)
rkyv::to_bytes::<_, MAX_REQUEST_SIZE>(&retrieved_request)
.unwrap()
.to_vec(),
"requests not equal"
Expand Down
14 changes: 4 additions & 10 deletions integration-tests/tests/stake_add_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_wallet::{RuskHttpClient, WalletPath};
use moat_core::{Error, JsonLoader, PayloadSender, RequestJson, TxAwaiter};
use phoenix_core::transaction::ModuleId;
use moat_core::{
Error, JsonLoader, PayloadSender, RequestJson, TxAwaiter,
ADD_OWNER_METHOD_NAME, STAKE_CONTRACT_ID,
};
use std::path::PathBuf;
use toml_base_config::BaseConfig;
use wallet_accessor::BlockchainAccessConfig;
use wallet_accessor::Password::PwdHash;

const STAKE_CONTRACT_ID: ModuleId = {
let mut bytes = [0u8; 32];
bytes[0] = 0x02;
bytes
};

const ADD_OWNER_METHOD_NAME: &str = "add_owner";

const WALLET_PATH: &str = concat!(env!("HOME"), "/.dusk/rusk-wallet");
const PWD_HASH: &str =
"7f2611ba158b6dcea4a69c229c303358c5e04493abeadee106a4bfa464d55787";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use moat_core::{CitadelInquirerWs, Error, LicenseSession, LicenseSessionId};
pub mod websocket;
use crate::websocket::ws_license_contract_mock_multi_server;
use dusk_jubjub::BlsScalar;

const TEST_DURATION_SECONDS: u64 = 4;
Expand Down
7 changes: 3 additions & 4 deletions integration-tests/tests/websocket/ws_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::websocket::ws_common::*;
use dusk_jubjub::BlsScalar;
use futures_util::{SinkExt, StreamExt};
use moat_core::{Error, LicenseSession};
use moat_core::{Error, LicenseSession, MAX_RESPONSE_SIZE};
use tokio::net::{TcpListener, TcpStream};
use tokio_tungstenite::tungstenite::Message;
use tracing::trace;
Expand All @@ -33,7 +33,6 @@ pub async fn ws_license_contract_mock_server(
Ok(())
}

// todo: remove this redundant server implementation
#[allow(dead_code)]
pub async fn ws_license_contract_mock_multi_server(
seconds: u64,
Expand Down Expand Up @@ -101,13 +100,13 @@ async fn accept_connection(stream: TcpStream) {
let response_data: Option<LicenseSession> = Some(LicenseSession {
public_inputs: vec![BlsScalar::zero()],
});
rkyv::to_bytes::<_, 8192>(&response_data)
rkyv::to_bytes::<_, MAX_RESPONSE_SIZE>(&response_data)
.expect("Data should serialize correctly")
.to_vec()
}
"get_licenses" => {
let response_data = vec![vec![1u8], vec![2u8]];
rkyv::to_bytes::<_, 8192>(&response_data)
rkyv::to_bytes::<_, MAX_RESPONSE_SIZE>(&response_data)
.expect("Data should serialize correctly")
.to_vec()
}
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/websocket/ws_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TEST_DURATION_SECONDS: u64 = 4;
const PORT: u32 = 9125;

#[tokio::test]
#[cfg_attr(not(feature = "int_tests"), ignore)]
#[ignore]
async fn ws_license_contract_mock_call() -> Result<(), Error> {
trace!("test driver - spawning ws license contract mock server");
tokio::spawn(ws_license_contract_mock_server(TEST_DURATION_SECONDS, PORT));
Expand Down
9 changes: 3 additions & 6 deletions license-provider/src/license_issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dusk_jubjub::{BlsScalar, JubJubAffine, JubJubScalar};
use dusk_pki::SecretSpendKey;
use dusk_poseidon::sponge;
use dusk_wallet::{RuskHttpClient, WalletPath};
use moat_core::{Error, PayloadSender, TxAwaiter};
use moat_core::{Error, PayloadSender, TxAwaiter, MAX_LICENSE_SIZE};
use rand::{CryptoRng, RngCore};
use tracing::trace;
use wallet_accessor::{BlockchainAccessConfig, Password};
Expand All @@ -22,11 +22,8 @@ pub struct LicenseIssuer {
gas_price: u64,
}

// todo: explain how are user attributes going to be passed in here from the
// user
const USER_ATTRIBUTES: u64 = 1 << 17; //0x9b308734u64;
const USER_ATTRIBUTES: u64 = 1 << 17;

#[allow(dead_code)]
impl LicenseIssuer {
pub fn new(
config: BlockchainAccessConfig,
Expand All @@ -52,7 +49,7 @@ impl LicenseIssuer {
) -> Result<BlsScalar, Error> {
let attr = JubJubScalar::from(USER_ATTRIBUTES);
let license = License::new(&attr, ssk_lp, request, rng);
let license_blob = rkyv::to_bytes::<_, 8192>(&license)
let license_blob = rkyv::to_bytes::<_, MAX_LICENSE_SIZE>(&license)
.expect("License should serialize correctly")
.to_vec();
let lpk = JubJubAffine::from(license.lsa.pk_r().as_ref());
Expand Down
4 changes: 2 additions & 2 deletions license-provider/src/reference_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use blake3::OUT_LEN;
use dusk_bytes::DeserializableSlice;
use dusk_pki::{PublicSpendKey, SecretSpendKey, ViewKey};
use moat_core::{Error, JsonLoader, RequestScanner};
use moat_core::{Error, JsonLoader, RequestScanner, MAX_REQUEST_SIZE};
use std::collections::BTreeSet;
use std::path::Path;
use wallet_accessor::BlockchainAccessConfig;
Expand Down Expand Up @@ -140,7 +140,7 @@ impl ReferenceLP {

fn hash_request(request: &Request) -> [u8; OUT_LEN] {
*blake3::hash(
rkyv::to_bytes::<_, 4096>(request)
rkyv::to_bytes::<_, MAX_REQUEST_SIZE>(request)
.expect("Request should serialize correctly")
.as_slice(),
)
Expand Down
16 changes: 4 additions & 12 deletions moat-core/src/blockchain_requests/payload_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::error::Error;
use crate::{
ISSUE_LICENSE_METHOD_NAME, LICENSE_CONTRACT_ID, MAX_CALL_SIZE,
NOOP_METHOD_NAME, USE_LICENSE_METHOD_NAME,
};
use dusk_jubjub::BlsScalar;
use dusk_wallet::WalletPath;
use phoenix_core::transaction::ModuleId;
Expand All @@ -13,18 +17,6 @@ use wallet_accessor::{BlockchainAccessConfig, Password, WalletAccessor};

pub struct PayloadSender;

const LICENSE_CONTRACT_ID: ModuleId = {
let mut bytes = [0u8; 32];
bytes[0] = 0x03;
bytes
};

const NOOP_METHOD_NAME: &str = "noop";
const ISSUE_LICENSE_METHOD_NAME: &str = "issue_license";
const USE_LICENSE_METHOD_NAME: &str = "use_license";

const MAX_CALL_SIZE: usize = 65536;

impl PayloadSender {
/// Sends a given payload to the noop method
pub async fn send_noop<P>(
Expand Down
20 changes: 6 additions & 14 deletions moat-core/src/contract_queries/citadel_inquirer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@

use crate::contract_queries::block::Block;
use crate::error::Error;
use crate::{ContractInquirer, LicenseSession, LicenseSessionId, ARITY, DEPTH};
use crate::{
ContractInquirer, LicenseSession, LicenseSessionId, ARITY, DEPTH,
GET_INFO_METHOD_NAME, GET_LICENSES_METHOD_NAME,
GET_MERKLE_OPENING_METHOD_NAME, GET_SESSION_METHOD_NAME,
LICENSE_CONTRACT_ID,
};
use bytes::Bytes;
use dusk_wallet::RuskHttpClient;
use phoenix_core::transaction::ModuleId;
use poseidon_merkle::Opening;
use std::ops::Range;
use zk_citadel::license::License;

// todo: refactor such consts to some common location
const LICENSE_CONTRACT_ID: ModuleId = {
let mut bytes = [0u8; 32];
bytes[0] = 0x03;
bytes
};

const GET_LICENSES_METHOD_NAME: &str = "get_licenses";
const GET_MERKLE_OPENING_METHOD_NAME: &str = "get_merkle_opening";
const GET_SESSION_METHOD_NAME: &str = "get_session";
const GET_INFO_METHOD_NAME: &str = "get_info";

pub struct CitadelInquirer {}

impl CitadelInquirer {
Expand Down
16 changes: 4 additions & 12 deletions moat-core/src/contract_queries/citadel_inquirer_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::error::Error;
use crate::{ContractInquirerWs, LicenseSession, LicenseSessionId};
use phoenix_core::transaction::ModuleId;
use std::ops::Range;

// todo: refactor such consts to some common location
const LICENSE_CONTRACT_ID: ModuleId = {
let mut bytes = [0u8; 32];
bytes[0] = 0x03;
bytes
use crate::{
ContractInquirerWs, LicenseSession, LicenseSessionId,
GET_LICENSES_METHOD_NAME, GET_SESSION_METHOD_NAME, LICENSE_CONTRACT_ID,
};

const GET_LICENSES_METHOD_NAME: &str = "get_licenses";
const GET_SESSION_METHOD_NAME: &str = "get_session";
use std::ops::Range;

pub struct CitadelInquirerWs {}

Expand Down
6 changes: 1 addition & 5 deletions moat-core/src/contract_queries/contract_inquirer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
use crate::contract_queries::block::Block;
use crate::error::Error;
use crate::Error::InvalidQueryResponse;
use crate::MAX_CALL_SIZE;
use bytecheck::CheckBytes;
use bytes::Bytes;
use dusk_wallet::{RuskHttpClient, RuskRequest};
use phoenix_core::transaction::ModuleId;
use rkyv::validation::validators::DefaultValidator;
use rkyv::{check_archived_root, Archive, Deserialize, Infallible};

#[allow(dead_code)]
pub struct ContractInquirer {}

#[allow(dead_code)]
const MAX_CALL_SIZE: usize = 65536;

#[allow(dead_code)]
impl ContractInquirer {
pub async fn query_contract<A, R>(
client: &RuskHttpClient,
Expand Down
8 changes: 2 additions & 6 deletions moat-core/src/contract_queries/contract_inquirer_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use crate::contract_queries::ws_types::{ExecutionRequest, ExecutionResponse};
use crate::error::Error;
use crate::Error::{InvalidQueryResponse, WebSocketStreamClosed};
use crate::MAX_CALL_SIZE;
use bytecheck::CheckBytes;
use futures_util::{SinkExt, StreamExt};
use phoenix_core::transaction::ModuleId;
Expand All @@ -17,13 +18,8 @@ use tokio::net::TcpStream;
use tokio_tungstenite::client_async;
use tokio_tungstenite::tungstenite::Message;

#[allow(dead_code)]
pub struct ContractInquirerWs {}

#[allow(dead_code)]
const MAX_CALL_SIZE: usize = 65536;

#[allow(dead_code)]
impl ContractInquirerWs {
pub async fn query_contract<A, R>(
url: impl AsRef<str>,
Expand All @@ -40,7 +36,7 @@ impl ContractInquirerWs {
{
let stream = TcpStream::connect(url.as_ref()).await?;

let url = format!("ws://{}", url.as_ref()); // todo: find a more elegant way // /01/stream
let url = format!("ws://{}", url.as_ref());

let (mut ws_stream, _) = client_async(url, stream).await?;

Expand Down
Loading