Skip to content

Commit ec7827a

Browse files
committed
Merge branch 'aip-61-adex-v5' into leader-follower-ticks-and-heartbeat-tests
2 parents 05933dc + 852e864 commit ec7827a

File tree

194 files changed

+3181
-879
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+3181
-879
lines changed

adapter/src/ethereum/client.rs

Lines changed: 245 additions & 84 deletions
Large diffs are not rendered by default.

adapter/src/ethereum/test_util.rs

Lines changed: 208 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use web3::{
1111
use primitives::{
1212
channel::{Channel, Nonce},
1313
config::{ChainInfo, TokenInfo, GANACHE_CONFIG},
14-
test_util::{ADVERTISER, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, LEADER, PUBLISHER},
14+
test_util::{
15+
ADVERTISER, ADVERTISER_2, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, LEADER, PUBLISHER,
16+
},
1517
Address, BigNum, Chain, ValidatorId,
1618
};
1719

@@ -83,6 +85,15 @@ pub static KEYSTORES: Lazy<HashMap<Address, Options>> = Lazy::new(|| {
8385
*GUARDIAN_2,
8486
keystore_options(&format!("{}_keystore.json", *GUARDIAN_2), "ganache6"),
8587
),
88+
// Address 7
89+
// (
90+
// *PUBLISHER_2,
91+
// keystore_options(&format!("{}_keystore.json", *PUBLISHER_2), "ganache7"),
92+
// ),
93+
(
94+
*ADVERTISER_2,
95+
keystore_options(&format!("{}_keystore.json", *ADVERTISER_2), "ganache8"),
96+
),
8697
]
8798
.into_iter()
8899
.collect()
@@ -147,105 +158,215 @@ pub fn get_test_channel(token_address: Address) -> Channel {
147158
}
148159
}
149160

150-
pub async fn mock_set_balance(
151-
token_contract: &Contract<Http>,
152-
from: [u8; 20],
153-
address: [u8; 20],
154-
amount: &BigNum,
155-
) -> web3::contract::Result<H256> {
156-
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
157-
158-
token_contract
159-
.call(
160-
"setBalanceTo",
161-
(H160(address), amount),
162-
H160(from),
163-
ContractOptions::default(),
164-
)
165-
.await
161+
/// The Sweeper contract
162+
///
163+
/// Initialized and ready for calling contract with [`Web3<Http>`].
164+
#[derive(Debug, Clone)]
165+
pub struct Sweeper {
166+
pub contract: Contract<Http>,
167+
pub address: Address,
166168
}
167169

168-
pub async fn outpace_deposit(
169-
outpace_contract: &Contract<Http>,
170-
channel: &Channel,
171-
to: [u8; 20],
172-
amount: &BigNum,
173-
) -> web3::contract::Result<H256> {
174-
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
175-
176-
outpace_contract
177-
.call(
178-
"deposit",
179-
(channel.tokenize(), H160(to), amount),
180-
H160(to),
181-
ContractOptions::with(|opt| {
170+
impl Sweeper {
171+
pub fn new(web3: &Web3<Http>, sweeper_address: Address) -> Self {
172+
let sweeper_contract =
173+
Contract::from_json(web3.eth(), H160(sweeper_address.to_bytes()), &SWEEPER_ABI)
174+
.expect("Failed to init Sweeper contract from JSON ABI!");
175+
176+
Self {
177+
address: sweeper_address,
178+
contract: sweeper_contract,
179+
}
180+
}
181+
182+
/// Deploys the Sweeper contract from [`LEADER`]
183+
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
184+
let contract = Contract::deploy(web3.eth(), &SWEEPER_ABI)
185+
.expect("Invalid ABI of Sweeper contract")
186+
.confirmations(0)
187+
.options(ContractOptions::with(|opt| {
182188
opt.gas_price = Some(1.into());
183189
opt.gas = Some(6_721_975.into());
184-
}),
185-
)
186-
.await
190+
}))
191+
.execute(*SWEEPER_BYTECODE, (), H160(LEADER.to_bytes()))
192+
.await?;
193+
194+
let sweeper_address = Address::from(contract.address().to_fixed_bytes());
195+
196+
Ok(Self {
197+
contract,
198+
address: sweeper_address,
199+
})
200+
}
201+
202+
pub async fn sweep(
203+
&self,
204+
outpace_address: [u8; 20],
205+
channel: &Channel,
206+
depositor: [u8; 20],
207+
) -> web3::contract::Result<H256> {
208+
let from_leader_account = H160(*LEADER.as_bytes());
209+
210+
self.contract
211+
.call(
212+
"sweep",
213+
(
214+
Token::Address(H160(outpace_address)),
215+
channel.tokenize(),
216+
Token::Array(vec![Token::Address(H160(depositor))]),
217+
),
218+
from_leader_account,
219+
ContractOptions::with(|opt| {
220+
opt.gas_price = Some(1.into());
221+
opt.gas = Some(6_721_975.into());
222+
}),
223+
)
224+
.await
225+
}
226+
}
227+
/// The Mocked token API contract
228+
///
229+
/// Used for mocking the tokens of an address.
230+
///
231+
/// Initialized and ready for calling contract with [`Web3<Http>`].
232+
///
233+
/// Mocked ABI: [`MOCK_TOKEN_ABI`]
234+
///
235+
/// Real ABI: [`crate::ethereum::ERC20_ABI`]
236+
#[derive(Debug, Clone)]
237+
pub struct Erc20Token {
238+
pub web3: Web3<Http>,
239+
pub info: TokenInfo,
240+
pub contract: Contract<Http>,
187241
}
188242

189-
pub async fn sweeper_sweep(
190-
sweeper_contract: &Contract<Http>,
191-
outpace_address: [u8; 20],
192-
channel: &Channel,
193-
depositor: [u8; 20],
194-
) -> web3::contract::Result<H256> {
195-
let from_leader_account = H160(*LEADER.as_bytes());
196-
197-
sweeper_contract
198-
.call(
199-
"sweep",
200-
(
201-
Token::Address(H160(outpace_address)),
202-
channel.tokenize(),
203-
Token::Array(vec![Token::Address(H160(depositor))]),
204-
),
205-
from_leader_account,
206-
ContractOptions::with(|opt| {
207-
opt.gas_price = Some(1.into());
208-
opt.gas = Some(6_721_975.into());
209-
}),
243+
impl Erc20Token {
244+
/// Presumes a default TokenInfo
245+
pub fn new(web3: &Web3<Http>, token_info: TokenInfo) -> Self {
246+
let token_contract = Contract::from_json(
247+
web3.eth(),
248+
token_info.address.as_bytes().into(),
249+
&MOCK_TOKEN_ABI,
210250
)
211-
.await
212-
}
251+
.expect("Failed to init Outpace contract from JSON ABI!");
213252

214-
/// Deploys the Sweeper contract from [`LEADER`]
215-
pub async fn deploy_sweeper_contract(
216-
web3: &Web3<Http>,
217-
) -> web3::contract::Result<(Address, Contract<Http>)> {
218-
let sweeper_contract = Contract::deploy(web3.eth(), &SWEEPER_ABI)
219-
.expect("Invalid ABI of Sweeper contract")
220-
.confirmations(0)
221-
.options(ContractOptions::with(|opt| {
222-
opt.gas_price = Some(1.into());
223-
opt.gas = Some(6_721_975.into());
224-
}))
225-
.execute(*SWEEPER_BYTECODE, (), H160(LEADER.to_bytes()))
226-
.await?;
253+
Self {
254+
web3: web3.clone(),
255+
info: token_info,
256+
contract: token_contract,
257+
}
258+
}
259+
260+
/// Deploys the Mock Token contract from [`LEADER`]
261+
pub async fn deploy(web3: &Web3<Http>, min_token_units: u64) -> web3::contract::Result<Self> {
262+
let token_contract = Contract::deploy(web3.eth(), &MOCK_TOKEN_ABI)
263+
.expect("Invalid ABI of Mock Token contract")
264+
.confirmations(0)
265+
.options(ContractOptions::with(|opt| {
266+
opt.gas_price = Some(1_i32.into());
267+
opt.gas = Some(6_721_975_i32.into());
268+
}))
269+
.execute(*MOCK_TOKEN_BYTECODE, (), H160(LEADER.to_bytes()))
270+
.await?;
271+
272+
let token_address = Address::from(token_contract.address().to_fixed_bytes());
273+
274+
let token_info = TokenInfo {
275+
min_token_units_for_deposit: BigNum::from(min_token_units),
276+
precision: NonZeroU8::new(18).expect("should create NonZeroU8"),
277+
// 0.000_001
278+
min_validator_fee: BigNum::from(1_000_000_000_000),
279+
address: token_address,
280+
};
281+
282+
Ok(Self {
283+
web3: web3.clone(),
284+
info: token_info,
285+
contract: token_contract,
286+
})
287+
}
227288

228-
let sweeper_address = Address::from(sweeper_contract.address().to_fixed_bytes());
289+
/// Set Mocked token balance
290+
pub async fn set_balance(
291+
&self,
292+
from: [u8; 20],
293+
address: [u8; 20],
294+
amount: &BigNum,
295+
) -> web3::contract::Result<H256> {
296+
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
297+
298+
self.contract
299+
.call(
300+
"setBalanceTo",
301+
(H160(address), amount),
302+
H160(from),
303+
ContractOptions::default(),
304+
)
305+
.await
306+
}
307+
}
229308

230-
Ok((sweeper_address, sweeper_contract))
309+
/// The Outpace contract
310+
///
311+
/// Initialized and ready for calling contract with [`Web3<Http>`].
312+
#[derive(Debug, Clone)]
313+
pub struct Outpace {
314+
pub contract: Contract<Http>,
315+
pub address: Address,
231316
}
232317

233-
/// Deploys the Outpace contract from [`LEADER`]
234-
pub async fn deploy_outpace_contract(
235-
web3: &Web3<Http>,
236-
) -> web3::contract::Result<(Address, Contract<Http>)> {
237-
let outpace_contract = Contract::deploy(web3.eth(), &OUTPACE_ABI)
238-
.expect("Invalid ABI of Outpace contract")
239-
.confirmations(0)
240-
.options(ContractOptions::with(|opt| {
241-
opt.gas_price = Some(1.into());
242-
opt.gas = Some(6_721_975.into());
243-
}))
244-
.execute(*OUTPACE_BYTECODE, (), H160(LEADER.to_bytes()))
245-
.await?;
246-
let outpace_address = Address::from(outpace_contract.address().to_fixed_bytes());
318+
impl Outpace {
319+
pub fn new(web3: &Web3<Http>, outpace_address: Address) -> Self {
320+
let outpace_contract =
321+
Contract::from_json(web3.eth(), outpace_address.as_bytes().into(), &OUTPACE_ABI)
322+
.expect("Failed to init Outpace contract from JSON ABI!");
247323

248-
Ok((outpace_address, outpace_contract))
324+
Self {
325+
address: outpace_address,
326+
contract: outpace_contract,
327+
}
328+
}
329+
330+
/// Deploys the Outpace contract from [`LEADER`]
331+
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
332+
let outpace_contract = Contract::deploy(web3.eth(), &OUTPACE_ABI)
333+
.expect("Invalid ABI of Outpace contract")
334+
.confirmations(0)
335+
.options(ContractOptions::with(|opt| {
336+
opt.gas_price = Some(1.into());
337+
opt.gas = Some(6_721_975.into());
338+
}))
339+
.execute(*OUTPACE_BYTECODE, (), H160(LEADER.to_bytes()))
340+
.await?;
341+
342+
let outpace_address = Address::from(outpace_contract.address().to_fixed_bytes());
343+
344+
Ok(Self {
345+
address: outpace_address,
346+
contract: outpace_contract,
347+
})
348+
}
349+
350+
pub async fn deposit(
351+
&self,
352+
channel: &Channel,
353+
to: [u8; 20],
354+
amount: &BigNum,
355+
) -> web3::contract::Result<H256> {
356+
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
357+
358+
self.contract
359+
.call(
360+
"deposit",
361+
(channel.tokenize(), H160(to), amount),
362+
H160(to),
363+
ContractOptions::with(|opt| {
364+
opt.gas_price = Some(1.into());
365+
opt.gas = Some(6_721_975.into());
366+
}),
367+
)
368+
.await
369+
}
249370
}
250371

251372
/// Deploys the Identity contract for the give `for_address`
@@ -278,31 +399,3 @@ pub async fn deploy_identity_contract(
278399

279400
Ok((identity_address, identity_contract))
280401
}
281-
282-
/// Deploys the Mock Token contract from [`LEADER`]
283-
pub async fn deploy_token_contract(
284-
web3: &Web3<Http>,
285-
min_token_units: u64,
286-
) -> web3::contract::Result<(TokenInfo, Address, Contract<Http>)> {
287-
let token_contract = Contract::deploy(web3.eth(), &MOCK_TOKEN_ABI)
288-
.expect("Invalid ABI of Mock Token contract")
289-
.confirmations(0)
290-
.options(ContractOptions::with(|opt| {
291-
opt.gas_price = Some(1_i32.into());
292-
opt.gas = Some(6_721_975_i32.into());
293-
}))
294-
.execute(*MOCK_TOKEN_BYTECODE, (), H160(LEADER.to_bytes()))
295-
.await?;
296-
297-
let token_address = Address::from(token_contract.address().to_fixed_bytes());
298-
299-
let token_info = TokenInfo {
300-
min_token_units_for_deposit: BigNum::from(min_token_units),
301-
precision: NonZeroU8::new(18).expect("should create NonZeroU8"),
302-
// 0.000_001
303-
min_validator_fee: BigNum::from(1_000_000_000_000),
304-
address: token_address,
305-
};
306-
307-
Ok((token_info, token_address, token_contract))
308-
}

adapter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![deny(rust_2018_idioms)]
22
#![deny(clippy::all)]
33
#![cfg_attr(docsrs, feature(doc_cfg))]
4+
#![allow(deprecated)]
45

56
pub use {
67
self::adapter::{

adview-manager/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ fn get_unit_html(
162162
) -> String {
163163
// replace all `"` quotes with a single quote `'`
164164
// these values are used inside `onclick` & `onload` html attributes
165-
let on_load = on_load.replace("\"", "'");
166-
let on_click = on_click.replace("\"", "'");
165+
let on_load = on_load.replace('\"', "'");
166+
let on_click = on_click.replace('\"', "'");
167167
let image_url = normalize_url(&ad_unit.media_url);
168168

169169
let element_html = if is_video(ad_unit) {
@@ -460,7 +460,7 @@ impl Manager {
460460
// Apply targeting, now with adView.* variables, and sort the resulting ad units
461461
let mut units_with_price = m_campaigns
462462
.iter()
463-
.map(|m_campaign| {
463+
.flat_map(|m_campaign| {
464464
// since we are in a Iterator.map(), we can't use async, so we block
465465
if block_on(self.is_campaign_sticky(m_campaign.campaign.id)) {
466466
return vec![];
@@ -498,7 +498,6 @@ impl Manager {
498498
.map(|uwp| (uwp.clone(), campaign_id))
499499
.collect()
500500
})
501-
.flatten()
502501
.filter(|x| !(self.options.disabled_video && is_video(&x.0.unit)))
503502
.collect::<Vec<_>>();
504503

0 commit comments

Comments
 (0)