Skip to content

Commit fe5fdc6

Browse files
authored
Merge pull request #486 from AmbireTech/testing-setup-improvements
Testing setup improvements & fix for ganache second instance snapshot
2 parents 6b1c3fa + 6b366ce commit fe5fdc6

File tree

176 files changed

+1082
-499
lines changed

Some content is hidden

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

176 files changed

+1082
-499
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: 196 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -147,105 +147,215 @@ pub fn get_test_channel(token_address: Address) -> Channel {
147147
}
148148
}
149149

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
150+
/// The Sweeper contract
151+
///
152+
/// Initialized and ready for calling contract with [`Web3<Http>`].
153+
#[derive(Debug, Clone)]
154+
pub struct Sweeper {
155+
pub contract: Contract<Http>,
156+
pub address: Address,
166157
}
167158

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| {
159+
impl Sweeper {
160+
pub fn new(web3: &Web3<Http>, sweeper_address: Address) -> Self {
161+
let sweeper_contract =
162+
Contract::from_json(web3.eth(), H160(sweeper_address.to_bytes()), &SWEEPER_ABI)
163+
.expect("Failed to init Sweeper contract from JSON ABI!");
164+
165+
Self {
166+
address: sweeper_address,
167+
contract: sweeper_contract,
168+
}
169+
}
170+
171+
/// Deploys the Sweeper contract from [`LEADER`]
172+
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
173+
let contract = Contract::deploy(web3.eth(), &SWEEPER_ABI)
174+
.expect("Invalid ABI of Sweeper contract")
175+
.confirmations(0)
176+
.options(ContractOptions::with(|opt| {
182177
opt.gas_price = Some(1.into());
183178
opt.gas = Some(6_721_975.into());
184-
}),
185-
)
186-
.await
179+
}))
180+
.execute(*SWEEPER_BYTECODE, (), H160(LEADER.to_bytes()))
181+
.await?;
182+
183+
let sweeper_address = Address::from(contract.address().to_fixed_bytes());
184+
185+
Ok(Self {
186+
contract,
187+
address: sweeper_address,
188+
})
189+
}
190+
191+
pub async fn sweep(
192+
&self,
193+
outpace_address: [u8; 20],
194+
channel: &Channel,
195+
depositor: [u8; 20],
196+
) -> web3::contract::Result<H256> {
197+
let from_leader_account = H160(*LEADER.as_bytes());
198+
199+
self.contract
200+
.call(
201+
"sweep",
202+
(
203+
Token::Address(H160(outpace_address)),
204+
channel.tokenize(),
205+
Token::Array(vec![Token::Address(H160(depositor))]),
206+
),
207+
from_leader_account,
208+
ContractOptions::with(|opt| {
209+
opt.gas_price = Some(1.into());
210+
opt.gas = Some(6_721_975.into());
211+
}),
212+
)
213+
.await
214+
}
215+
}
216+
/// The Mocked token API contract
217+
///
218+
/// Used for mocking the tokens of an address.
219+
///
220+
/// Initialized and ready for calling contract with [`Web3<Http>`].
221+
///
222+
/// Mocked ABI: [`MOCK_TOKEN_ABI`]
223+
///
224+
/// Real ABI: [`crate::ethereum::ERC20_ABI`]
225+
#[derive(Debug, Clone)]
226+
pub struct Erc20Token {
227+
pub web3: Web3<Http>,
228+
pub info: TokenInfo,
229+
pub contract: Contract<Http>,
187230
}
188231

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-
}),
232+
impl Erc20Token {
233+
/// Presumes a default TokenInfo
234+
pub fn new(web3: &Web3<Http>, token_info: TokenInfo) -> Self {
235+
let token_contract = Contract::from_json(
236+
web3.eth(),
237+
token_info.address.as_bytes().into(),
238+
&MOCK_TOKEN_ABI,
210239
)
211-
.await
212-
}
240+
.expect("Failed to init Outpace contract from JSON ABI!");
213241

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?;
242+
Self {
243+
web3: web3.clone(),
244+
info: token_info,
245+
contract: token_contract,
246+
}
247+
}
248+
249+
/// Deploys the Mock Token contract from [`LEADER`]
250+
pub async fn deploy(web3: &Web3<Http>, min_token_units: u64) -> web3::contract::Result<Self> {
251+
let token_contract = Contract::deploy(web3.eth(), &MOCK_TOKEN_ABI)
252+
.expect("Invalid ABI of Mock Token contract")
253+
.confirmations(0)
254+
.options(ContractOptions::with(|opt| {
255+
opt.gas_price = Some(1_i32.into());
256+
opt.gas = Some(6_721_975_i32.into());
257+
}))
258+
.execute(*MOCK_TOKEN_BYTECODE, (), H160(LEADER.to_bytes()))
259+
.await?;
260+
261+
let token_address = Address::from(token_contract.address().to_fixed_bytes());
262+
263+
let token_info = TokenInfo {
264+
min_token_units_for_deposit: BigNum::from(min_token_units),
265+
precision: NonZeroU8::new(18).expect("should create NonZeroU8"),
266+
// 0.000_001
267+
min_validator_fee: BigNum::from(1_000_000_000_000),
268+
address: token_address,
269+
};
270+
271+
Ok(Self {
272+
web3: web3.clone(),
273+
info: token_info,
274+
contract: token_contract,
275+
})
276+
}
227277

228-
let sweeper_address = Address::from(sweeper_contract.address().to_fixed_bytes());
278+
/// Set Mocked token balance
279+
pub async fn set_balance(
280+
&self,
281+
from: [u8; 20],
282+
address: [u8; 20],
283+
amount: &BigNum,
284+
) -> web3::contract::Result<H256> {
285+
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
286+
287+
self.contract
288+
.call(
289+
"setBalanceTo",
290+
(H160(address), amount),
291+
H160(from),
292+
ContractOptions::default(),
293+
)
294+
.await
295+
}
296+
}
229297

230-
Ok((sweeper_address, sweeper_contract))
298+
/// The Outpace contract
299+
///
300+
/// Initialized and ready for calling contract with [`Web3<Http>`].
301+
#[derive(Debug, Clone)]
302+
pub struct Outpace {
303+
pub contract: Contract<Http>,
304+
pub address: Address,
231305
}
232306

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());
307+
impl Outpace {
308+
pub fn new(web3: &Web3<Http>, outpace_address: Address) -> Self {
309+
let outpace_contract =
310+
Contract::from_json(web3.eth(), outpace_address.as_bytes().into(), &OUTPACE_ABI)
311+
.expect("Failed to init Outpace contract from JSON ABI!");
312+
313+
Self {
314+
address: outpace_address,
315+
contract: outpace_contract,
316+
}
317+
}
318+
319+
/// Deploys the Outpace contract from [`LEADER`]
320+
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
321+
let outpace_contract = Contract::deploy(web3.eth(), &OUTPACE_ABI)
322+
.expect("Invalid ABI of Outpace contract")
323+
.confirmations(0)
324+
.options(ContractOptions::with(|opt| {
325+
opt.gas_price = Some(1.into());
326+
opt.gas = Some(6_721_975.into());
327+
}))
328+
.execute(*OUTPACE_BYTECODE, (), H160(LEADER.to_bytes()))
329+
.await?;
330+
331+
let outpace_address = Address::from(outpace_contract.address().to_fixed_bytes());
332+
333+
Ok(Self {
334+
address: outpace_address,
335+
contract: outpace_contract,
336+
})
337+
}
247338

248-
Ok((outpace_address, outpace_contract))
339+
pub async fn deposit(
340+
&self,
341+
channel: &Channel,
342+
to: [u8; 20],
343+
amount: &BigNum,
344+
) -> web3::contract::Result<H256> {
345+
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");
346+
347+
self.contract
348+
.call(
349+
"deposit",
350+
(channel.tokenize(), H160(to), amount),
351+
H160(to),
352+
ContractOptions::with(|opt| {
353+
opt.gas_price = Some(1.into());
354+
opt.gas = Some(6_721_975.into());
355+
}),
356+
)
357+
.await
358+
}
249359
}
250360

251361
/// Deploys the Identity contract for the give `for_address`
@@ -278,31 +388,3 @@ pub async fn deploy_identity_contract(
278388

279389
Ok((identity_address, identity_contract))
280390
}
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::{

docs/config/ganache.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ analytics_maxtime = 20000
1414
heartbeat_time = 30000
1515
health_threshold_promilles = 950
1616
health_unsignable_promilles = 750
17-
propagation_timeout = 1000
17+
# 2 seconds
18+
# TODO: Check the POST /validator-message route performance, more than 1 second for timeout is a lot!
19+
propagation_timeout = 2000
1820

1921
fetch_timeout = 5000
2022
all_campaigns_timeout = 5000
@@ -47,12 +49,12 @@ global_min_impression_price = '1000000'
4749
chain_id = 1
4850
rpc = 'http://localhost:8545'
4951
# Ganache Snapshot address
50-
outpace = '0x671b54359be2fa82aB05A5336EB8134F782B88Cd'
52+
outpace = '0x26CBc2eAAe377f6Ac4b73a982CD1125eF4CEC96f'
5153
# Ganache Snapshot address
52-
sweeper = '0xE37BA12D23e755138f7Ea8391551B773Cb2D8F5F'
54+
sweeper = '0x3550A85877002f79db46B01672b5F86f1037cB7c'
5355

54-
[chain."Ganache #1".token."Mocked TOKEN 2"]
55-
address = '0x704D76a8c31FbAafE2B7895b13A763c7487FC0D8' # checked
56+
[chain."Ganache #1".token."Mocked TOKEN 1"]
57+
address = '0x12a28f2bfBFfDf5842657235cC058242f40fDEa6' # checked
5658
precision = 18
5759
# Minimum token units for the Create2 deposits to count
5860
# 1 * 10^18 = 1.0000 TOKEN
@@ -70,7 +72,7 @@ outpace = '0xAbc27d46a458E2e49DaBfEf45ca74dEDBAc3DD06'
7072
# Ganache Snapshot address
7173
sweeper = '0x7dD57C0324284102A757153E18F2Cb1ACdB7d2bD'
7274

73-
[chain."Ganache #1337".token."Mocked TOKEN"]
75+
[chain."Ganache #1337".token."Mocked TOKEN 1337"]
7476
address = '0x2bcaf6968aec8a3b5126fbfab5fd419da6e8ad8e' # checked
7577
precision = 18
7678
# Minimum token units for the Create2 deposits to count

primitives/src/chain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct Chain {
4848
/// The OUTPACE contract address on this Chain
4949
pub outpace: Address,
5050
/// The Sweeper contract address on this Chain
51+
#[deprecated = "we no longer need the sweeper contract for deposits"]
5152
pub sweeper: Address,
5253
}
5354

primitives/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl ChainInfo {
164164
/// Precision can differ for the same token from one [`Chain`] to another.
165165
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
166166
pub struct TokenInfo {
167+
#[deprecated = "we no longer need the sweeper contract & create2 addresses for deposits"]
167168
pub min_token_units_for_deposit: BigNum,
168169
pub min_validator_fee: BigNum,
169170
pub precision: NonZeroU8,

0 commit comments

Comments
 (0)