From 0fa21911f0240d064065af030138dffeffc80cae Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 1 Feb 2023 18:18:50 +0530 Subject: [PATCH] feat: submit and store benchmark type --- .../20230201101934_survey_bench_type.sql | 18 ++++++ src/api/v1/bench.rs | 57 +++++++++++++++---- templates/bench/index.ts | 4 +- templates/bench/prove.ts | 16 +++++- templates/bench/service-worker.ts | 2 +- templates/bench/types.ts | 6 ++ 6 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 migrations/20230201101934_survey_bench_type.sql diff --git a/migrations/20230201101934_survey_bench_type.sql b/migrations/20230201101934_survey_bench_type.sql new file mode 100644 index 0000000..1db5532 --- /dev/null +++ b/migrations/20230201101934_survey_bench_type.sql @@ -0,0 +1,18 @@ +CREATE TABLE IF NOT EXISTS survey_bench_type ( + name VARCHAR(30) UNIQUE NOT NULL, + ID SERIAL PRIMARY KEY NOT NULL +); + +INSERT INTO survey_bench_type (name) VALUES ('wasm'); +INSERT INTO survey_bench_type (name) VALUES ('js'); + + +CREATE OR REPLACE FUNCTION id_in_survey_bench_type(iname varchar) +RETURNS int LANGUAGE SQL AS $$ + SELECT ID FROM survey_bench_type WHERE name = name; +$$; + + +ALTER TABLE survey_responses + ADD COLUMN submission_bench_type_id INTEGER references survey_bench_type(ID) NOT NULL + DEFAULT id_in_survey_bench_type('wasm'); diff --git a/src/api/v1/bench.rs b/src/api/v1/bench.rs index f989ee4..429ed30 100644 --- a/src/api/v1/bench.rs +++ b/src/api/v1/bench.rs @@ -169,6 +169,28 @@ pub struct Submission { pub device_software_recognised: String, pub threads: i32, pub benches: Vec, + pub submission_type: SubmissionType, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum SubmissionType { + Wasm, + Js, +} + +impl ToString for SubmissionType { + fn to_string(&self) -> String { + let s = serde_json::to_string(&self).unwrap(); + (&s[1..(s.len() - 1)]).to_string() + } +} + +impl FromStr for SubmissionType { + type Err = serde_json::Error; + fn from_str(s: &str) -> Result { + serde_json::from_str(&format!("\"{}\"", s)) + } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -177,12 +199,11 @@ pub struct SubmissionProof { pub proof: String, } -fn is_session_authenticated(r: &HttpRequest, mut pl: &mut Payload) -> bool { +fn is_session_authenticated(r: &HttpRequest, pl: &mut Payload) -> bool { use actix_web::FromRequest; matches!( Session::from_request(r, pl).into_inner().map(|x| { let val = x.get::(SURVEY_USER_ID); - println!("{:#?}", val); val }), Ok(Ok(Some(_))) @@ -222,20 +243,25 @@ async fn submit( let resp_id = sqlx::query_as!( ID, "INSERT INTO survey_responses ( - user_id, - campaign_id, - device_user_provided, - device_software_recognised, - threads, - submitted_at - ) VALUES ($1, $2, $3, $4, $5, $6) + user_id, + campaign_id, + device_user_provided, + device_software_recognised, + threads, + submitted_at, + submission_bench_type_id + ) VALUES ( + $1, $2, $3, $4, $5, $6, + (SELECT ID FROM survey_bench_type WHERE name = $7) + ) RETURNING ID;", &user_id, &campaign_id, &payload.device_user_provided, &payload.device_software_recognised, &payload.threads, - &now + &now, + &payload.submission_type.to_string(), ) .fetch_one(&data.db) .await?; @@ -314,3 +340,14 @@ async fn fetch(data: AppData, path: web::Path) -> ServiceResult { const ADV = document.getElementById("advance"); @@ -67,12 +68,13 @@ export const index = () => { }; const submitBench = async () => { + const submission_type = await get_bench_type(); const payload: Submission = { device_user_provided: deviceName, threads: window.navigator.hardwareConcurrency, device_software_recognised: window.navigator.userAgent, - benches: res, + submission_type, }; const resp = await fetch( diff --git a/templates/bench/prove.ts b/templates/bench/prove.ts index cbc7c7b..35ce58a 100644 --- a/templates/bench/prove.ts +++ b/templates/bench/prove.ts @@ -10,7 +10,18 @@ */ import * as p from "@mcaptcha/pow_sha256-polyfill"; -import { PoWConfig } from "./types"; +import { PoWConfig, SubmissionType } from "./types"; + +export const get_bench_type = async (): Promise => { + console.log(`Wasm support says ${WasmSupported}`); + let submission_type: SubmissionType; + if (WasmSupported) { + submission_type = SubmissionType.wasm; + } else { + submission_type = SubmissionType.js; + } + return submission_type; +}; /** * proove work @@ -21,8 +32,9 @@ const prove = async (config: PoWConfig): Promise => { console.log(`Wasm support says ${WasmSupported}`); let duration: number; if (WasmSupported) { - const wasm = require("@mcaptcha/pow-wasm"); + const wasm = await require("@mcaptcha/pow-wasm"); const t0 = performance.now(); + console.log(JSON.stringify(wasm)) wasm.gen_pow(config.salt, config.string, config.difficulty_factor); const t1 = performance.now(); duration = t1 - t0; diff --git a/templates/bench/service-worker.ts b/templates/bench/service-worker.ts index 954f0ec..a1810df 100644 --- a/templates/bench/service-worker.ts +++ b/templates/bench/service-worker.ts @@ -32,7 +32,7 @@ onmessage = async (event) => { salt: SALT, }; - let duration = await prove(config); + const duration = await prove(config); const msg: Bench = { difficulty: difficulty_factor, diff --git a/templates/bench/types.ts b/templates/bench/types.ts index f2152fd..41f4e0f 100644 --- a/templates/bench/types.ts +++ b/templates/bench/types.ts @@ -25,6 +25,7 @@ export type Submission = { device_software_recognised: String; threads: number; benches: Array; + submission_type: SubmissionType; }; export type SubmissionProof = { @@ -41,3 +42,8 @@ export type PoWConfig = { difficulty_factor: number; salt: string; }; + +export enum SubmissionType { + wasm = "wasm", + js = "js", +}