Skip to content

Commit 6a5150d

Browse files
committed
adview-manager - use primitives::IPFS & impl randomized_sort_pos
1 parent 6886452 commit 6a5150d

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

adview-manager/src/lib.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ use adex_primitives::{
55
supermarket::units_for_slot,
66
supermarket::units_for_slot::response::{AdUnit, Campaign},
77
targeting::{self, input, Value},
8-
BigNum, ChannelId, SpecValidators,
8+
BigNum, ChannelId, SpecValidators, IPFS,
99
};
1010
use async_std::{sync::RwLock, task::block_on};
1111
use chrono::{DateTime, Utc};
1212
use lazy_static::lazy_static;
13+
use num_integer::Integer;
1314
use rand::Rng;
1415
use reqwest::StatusCode;
1516
use serde::{Deserialize, Serialize};
1617
use slog::{error, Logger};
17-
use std::{cmp::Ordering, collections::VecDeque, convert::TryFrom, sync::Arc};
18+
use std::{
19+
cmp::Ordering,
20+
collections::VecDeque,
21+
convert::TryFrom,
22+
ops::{Add, Mul},
23+
sync::Arc,
24+
};
1825
use thiserror::Error;
1926
use units_for_slot::response::UnitsWithPrice;
2027
use url::Url;
@@ -44,7 +51,7 @@ pub struct Options {
4451
// Defaulted via defaultOpts
4552
#[serde(rename = "marketURL")]
4653
pub market_url: String,
47-
pub market_slot: String,
54+
pub market_slot: IPFS,
4855
pub publisher_addr: String,
4956
// All passed tokens must be of the same price and decimals, so that the amounts can be accurately compared
5057
pub whitelisted_tokens: Vec<String>,
@@ -66,9 +73,9 @@ impl Options {
6673
#[derive(Debug, Clone)]
6774
pub struct HistoryEntry {
6875
time: DateTime<Utc>,
69-
unit_id: String,
76+
unit_id: IPFS,
7077
campaign_id: ChannelId,
71-
slot_id: String,
78+
slot_id: IPFS,
7279
}
7380

7481
#[derive(Serialize)]
@@ -77,8 +84,8 @@ struct Event {
7784
#[serde(rename = "type")]
7885
event_type: String,
7986
publisher: String,
80-
ad_unit: String,
81-
ad_slot: String,
87+
ad_unit: IPFS,
88+
ad_slot: IPFS,
8289
#[serde(rename = "ref")]
8390
referrer: String,
8491
}
@@ -149,16 +156,15 @@ fn is_video(ad_unit: &AdUnit) -> bool {
149156
ad_unit.media_mime.split('/').next() == Some("video")
150157
}
151158

152-
// @TODO: IMPL
153-
// function randomizedSortPos(unit: Unit, seed: BN): BN {
154-
// // using base32 is technically wrong (IDs are in base 58), but it works well enough for this purpose
155-
// // kind of a LCG PRNG but without the state; using GCC's constraints as seen on stack overflow
156-
// // takes around ~700ms for 100k iterations, yields very decent distribution (e.g. 724ms 50070, 728ms 49936)
157-
// return new BN(unit.id, 32).mul(seed).add(new BN(12345)).mod(new BN(0x80000000))
158-
// }
159-
fn randomized_sort_pos(_ad_unit: &AdUnit, _seed: BigNum) -> BigNum {
160-
// todo!("Implement the randomized_sort_pos() function!")
161-
BigNum::from(10)
159+
/// Does not copy the JS impl, instead it generates the BigNum from the IPFS CID bytes instead
160+
fn randomized_sort_pos(ad_unit: &AdUnit, seed: BigNum) -> BigNum {
161+
let bytes = ad_unit.id.0.to_bytes();
162+
163+
let unit_id = BigNum::from_bytes_be(&bytes);
164+
165+
let x: BigNum = unit_id.mul(seed).add(BigNum::from(12345));
166+
167+
x.mod_floor(&BigNum::from(0x80000000))
162168
}
163169

164170
fn get_unit_html(
@@ -605,10 +611,11 @@ pub struct StickyAdUnit {
605611
#[cfg(test)]
606612
mod test {
607613
use super::*;
614+
use adex_primitives::util::tests::prep_db::DUMMY_IPFS;
608615

609616
fn get_ad_unit(media_mime: &str) -> AdUnit {
610617
AdUnit {
611-
id: "".to_string(),
618+
id: DUMMY_IPFS[0].clone(),
612619
media_url: "".to_string(),
613620
media_mime: media_mime.to_string(),
614621
target_url: "".to_string(),
@@ -633,4 +640,24 @@ mod test {
633640
// Non-IPFS case
634641
assert_eq!("http://123".to_string(), normalize_url("http://123"));
635642
}
643+
644+
mod randomized_sort_pos {
645+
646+
use super::*;
647+
648+
#[test]
649+
fn test_randomized_position() {
650+
let ad_unit = AdUnit {
651+
id: DUMMY_IPFS[0].clone(),
652+
media_url: "ipfs://QmWWQSuPMS6aXCbZKpEjPHPUZN2NjB3YrhJTHsV4X3vb2t".to_string(),
653+
media_mime: "image/jpeg".to_string(),
654+
target_url: "https://google.com".to_string(),
655+
};
656+
657+
let result = randomized_sort_pos(&ad_unit, 5.into());
658+
659+
// The seed is responsible for generating different results since the AdUnit IPFS can be the same
660+
assert_eq!(BigNum::from(177_349_401), result);
661+
}
662+
}
636663
}

primitives/src/big_num.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl BigNum {
4444
pub fn to_str_radix(&self, radix: u32) -> String {
4545
self.0.to_str_radix(radix)
4646
}
47+
48+
pub fn from_bytes_be(buf: &[u8]) -> Self {
49+
Self(BigUint::from_bytes_be(buf))
50+
}
4751
}
4852

4953
impl fmt::Debug for BigNum {

0 commit comments

Comments
 (0)