Skip to content

Testing setup improvements & fix for ganache second instance snapshot #486

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

Merged
merged 28 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c523b52
validator worker: 3 test cases for leader tick, 2 test cases for foll…
simzzz Jan 25, 2022
f90e07c
additional comment to an assert
simzzz Jan 25, 2022
bb4d964
Merge branch 'aip-61-adex-v5' into validator-worker-test-coverage
simzzz Jan 25, 2022
17c7d98
moved campaign creation to beginning of test
simzzz Jan 25, 2022
1028d5d
Merge branch 'aip-61-adex-v5' into validator-worker-test-coverage
simzzz Jan 25, 2022
e3fa968
moved validator worker tests within the full test
simzzz Jan 26, 2022
c5ef9da
Refactored tests, heartbeat can now be tested in validator worker tests
simzzz Jan 27, 2022
518ba27
commented out the last test case
simzzz Jan 28, 2022
48d30f5
Refactored validator tests
simzzz Feb 15, 2022
9bff9cf
Merge branch 'aip-61-adex-v5' into validator-worker-test-coverage
simzzz Feb 15, 2022
5c7bf06
merged aip-61 branch and fixed errors
simzzz Feb 15, 2022
91fc589
Merge branch 'aip-61-adex-v5' into validator-worker-test-coverage
simzzz Mar 24, 2022
c8aad10
Merge branch 'aip-61-adex-v5' into validator-worker-test-coverage
simzzz Mar 30, 2022
a4cc01b
comitting current progress
simzzz Apr 1, 2022
4bd90aa
update protocol-eth
elpiel Apr 6, 2022
1d1f4b2
scripts - ethereum - Update snapshot of Chain #1
elpiel Apr 11, 2022
08ab6c5
config - ganache - update Chain #1 config
elpiel Apr 11, 2022
93cc4c6
adapter - ethereum - use structs for the Contracts:
elpiel Apr 11, 2022
9120c04
deprecate sweeper
elpiel Apr 11, 2022
b901585
Config - ganache - increase propagation timeout
elpiel Apr 12, 2022
3f8460c
test_harness - start refactoring ticks tests
elpiel Apr 12, 2022
f90fa9f
Merge branch 'aip-61-adex-v5' into testing-setup-improvements
elpiel Apr 12, 2022
7d29b0d
Merge branch 'aip-61-adex-v5' into testing-setup-improvements
elpiel Apr 13, 2022
d731242
test_harness - check CAMPAIGN_1 states after ticks
elpiel Apr 13, 2022
b4304e8
validator_worker - SentryApi - improve test setup
elpiel Apr 13, 2022
a9c5da2
worker - Cargo - adapter - enable test-util feature
elpiel Apr 13, 2022
90bdde2
test_harness - fix failing test for LastApproved
elpiel Apr 14, 2022
6b366ce
test_harness - fix PR review comment
elpiel Apr 15, 2022
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
329 changes: 245 additions & 84 deletions adapter/src/ethereum/client.rs

Large diffs are not rendered by default.

310 changes: 196 additions & 114 deletions adapter/src/ethereum/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,105 +147,215 @@ pub fn get_test_channel(token_address: Address) -> Channel {
}
}

pub async fn mock_set_balance(
token_contract: &Contract<Http>,
from: [u8; 20],
address: [u8; 20],
amount: &BigNum,
) -> web3::contract::Result<H256> {
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");

token_contract
.call(
"setBalanceTo",
(H160(address), amount),
H160(from),
ContractOptions::default(),
)
.await
/// The Sweeper contract
///
/// Initialized and ready for calling contract with [`Web3<Http>`].
#[derive(Debug, Clone)]
pub struct Sweeper {
pub contract: Contract<Http>,
pub address: Address,
}

pub async fn outpace_deposit(
outpace_contract: &Contract<Http>,
channel: &Channel,
to: [u8; 20],
amount: &BigNum,
) -> web3::contract::Result<H256> {
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");

outpace_contract
.call(
"deposit",
(channel.tokenize(), H160(to), amount),
H160(to),
ContractOptions::with(|opt| {
impl Sweeper {
pub fn new(web3: &Web3<Http>, sweeper_address: Address) -> Self {
let sweeper_contract =
Contract::from_json(web3.eth(), H160(sweeper_address.to_bytes()), &SWEEPER_ABI)
.expect("Failed to init Sweeper contract from JSON ABI!");

Self {
address: sweeper_address,
contract: sweeper_contract,
}
}

/// Deploys the Sweeper contract from [`LEADER`]
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
let contract = Contract::deploy(web3.eth(), &SWEEPER_ABI)
.expect("Invalid ABI of Sweeper contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}),
)
.await
}))
.execute(*SWEEPER_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;

let sweeper_address = Address::from(contract.address().to_fixed_bytes());

Ok(Self {
contract,
address: sweeper_address,
})
}

pub async fn sweep(
&self,
outpace_address: [u8; 20],
channel: &Channel,
depositor: [u8; 20],
) -> web3::contract::Result<H256> {
let from_leader_account = H160(*LEADER.as_bytes());

self.contract
.call(
"sweep",
(
Token::Address(H160(outpace_address)),
channel.tokenize(),
Token::Array(vec![Token::Address(H160(depositor))]),
),
from_leader_account,
ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}),
)
.await
}
}
/// The Mocked token API contract
///
/// Used for mocking the tokens of an address.
///
/// Initialized and ready for calling contract with [`Web3<Http>`].
///
/// Mocked ABI: [`MOCK_TOKEN_ABI`]
///
/// Real ABI: [`crate::ethereum::ERC20_ABI`]
#[derive(Debug, Clone)]
pub struct Erc20Token {
pub web3: Web3<Http>,
pub info: TokenInfo,
pub contract: Contract<Http>,
}

pub async fn sweeper_sweep(
sweeper_contract: &Contract<Http>,
outpace_address: [u8; 20],
channel: &Channel,
depositor: [u8; 20],
) -> web3::contract::Result<H256> {
let from_leader_account = H160(*LEADER.as_bytes());

sweeper_contract
.call(
"sweep",
(
Token::Address(H160(outpace_address)),
channel.tokenize(),
Token::Array(vec![Token::Address(H160(depositor))]),
),
from_leader_account,
ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}),
impl Erc20Token {
/// Presumes a default TokenInfo
pub fn new(web3: &Web3<Http>, token_info: TokenInfo) -> Self {
let token_contract = Contract::from_json(
web3.eth(),
token_info.address.as_bytes().into(),
&MOCK_TOKEN_ABI,
)
.await
}
.expect("Failed to init Outpace contract from JSON ABI!");

/// Deploys the Sweeper contract from [`LEADER`]
pub async fn deploy_sweeper_contract(
web3: &Web3<Http>,
) -> web3::contract::Result<(Address, Contract<Http>)> {
let sweeper_contract = Contract::deploy(web3.eth(), &SWEEPER_ABI)
.expect("Invalid ABI of Sweeper contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}))
.execute(*SWEEPER_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;
Self {
web3: web3.clone(),
info: token_info,
contract: token_contract,
}
}

/// Deploys the Mock Token contract from [`LEADER`]
pub async fn deploy(web3: &Web3<Http>, min_token_units: u64) -> web3::contract::Result<Self> {
let token_contract = Contract::deploy(web3.eth(), &MOCK_TOKEN_ABI)
.expect("Invalid ABI of Mock Token contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1_i32.into());
opt.gas = Some(6_721_975_i32.into());
}))
.execute(*MOCK_TOKEN_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;

let token_address = Address::from(token_contract.address().to_fixed_bytes());

let token_info = TokenInfo {
min_token_units_for_deposit: BigNum::from(min_token_units),
precision: NonZeroU8::new(18).expect("should create NonZeroU8"),
// 0.000_001
min_validator_fee: BigNum::from(1_000_000_000_000),
address: token_address,
};

Ok(Self {
web3: web3.clone(),
info: token_info,
contract: token_contract,
})
}

let sweeper_address = Address::from(sweeper_contract.address().to_fixed_bytes());
/// Set Mocked token balance
pub async fn set_balance(
&self,
from: [u8; 20],
address: [u8; 20],
amount: &BigNum,
) -> web3::contract::Result<H256> {
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");

self.contract
.call(
"setBalanceTo",
(H160(address), amount),
H160(from),
ContractOptions::default(),
)
.await
}
}

Ok((sweeper_address, sweeper_contract))
/// The Outpace contract
///
/// Initialized and ready for calling contract with [`Web3<Http>`].
#[derive(Debug, Clone)]
pub struct Outpace {
pub contract: Contract<Http>,
pub address: Address,
}

/// Deploys the Outpace contract from [`LEADER`]
pub async fn deploy_outpace_contract(
web3: &Web3<Http>,
) -> web3::contract::Result<(Address, Contract<Http>)> {
let outpace_contract = Contract::deploy(web3.eth(), &OUTPACE_ABI)
.expect("Invalid ABI of Outpace contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}))
.execute(*OUTPACE_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;
let outpace_address = Address::from(outpace_contract.address().to_fixed_bytes());
impl Outpace {
pub fn new(web3: &Web3<Http>, outpace_address: Address) -> Self {
let outpace_contract =
Contract::from_json(web3.eth(), outpace_address.as_bytes().into(), &OUTPACE_ABI)
.expect("Failed to init Outpace contract from JSON ABI!");

Self {
address: outpace_address,
contract: outpace_contract,
}
}

/// Deploys the Outpace contract from [`LEADER`]
pub async fn deploy(web3: &Web3<Http>) -> web3::contract::Result<Self> {
let outpace_contract = Contract::deploy(web3.eth(), &OUTPACE_ABI)
.expect("Invalid ABI of Outpace contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}))
.execute(*OUTPACE_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;

let outpace_address = Address::from(outpace_contract.address().to_fixed_bytes());

Ok(Self {
address: outpace_address,
contract: outpace_contract,
})
}

Ok((outpace_address, outpace_contract))
pub async fn deposit(
&self,
channel: &Channel,
to: [u8; 20],
amount: &BigNum,
) -> web3::contract::Result<H256> {
let amount = U256::from_dec_str(&amount.to_string()).expect("Should create U256");

self.contract
.call(
"deposit",
(channel.tokenize(), H160(to), amount),
H160(to),
ContractOptions::with(|opt| {
opt.gas_price = Some(1.into());
opt.gas = Some(6_721_975.into());
}),
)
.await
}
}

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

Ok((identity_address, identity_contract))
}

/// Deploys the Mock Token contract from [`LEADER`]
pub async fn deploy_token_contract(
web3: &Web3<Http>,
min_token_units: u64,
) -> web3::contract::Result<(TokenInfo, Address, Contract<Http>)> {
let token_contract = Contract::deploy(web3.eth(), &MOCK_TOKEN_ABI)
.expect("Invalid ABI of Mock Token contract")
.confirmations(0)
.options(ContractOptions::with(|opt| {
opt.gas_price = Some(1_i32.into());
opt.gas = Some(6_721_975_i32.into());
}))
.execute(*MOCK_TOKEN_BYTECODE, (), H160(LEADER.to_bytes()))
.await?;

let token_address = Address::from(token_contract.address().to_fixed_bytes());

let token_info = TokenInfo {
min_token_units_for_deposit: BigNum::from(min_token_units),
precision: NonZeroU8::new(18).expect("should create NonZeroU8"),
// 0.000_001
min_validator_fee: BigNum::from(1_000_000_000_000),
address: token_address,
};

Ok((token_info, token_address, token_contract))
}
1 change: 1 addition & 0 deletions adapter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(deprecated)]

pub use {
self::adapter::{
Expand Down
14 changes: 8 additions & 6 deletions docs/config/ganache.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ analytics_maxtime = 20000
heartbeat_time = 30000
health_threshold_promilles = 950
health_unsignable_promilles = 750
propagation_timeout = 1000
# 2 seconds
# TODO: Check the POST /validator-message route performance, more than 1 second for timeout is a lot!
propagation_timeout = 2000

fetch_timeout = 5000
all_campaigns_timeout = 5000
Expand Down Expand Up @@ -47,12 +49,12 @@ global_min_impression_price = '1000000'
chain_id = 1
rpc = 'http://localhost:8545'
# Ganache Snapshot address
outpace = '0x671b54359be2fa82aB05A5336EB8134F782B88Cd'
outpace = '0x26CBc2eAAe377f6Ac4b73a982CD1125eF4CEC96f'
# Ganache Snapshot address
sweeper = '0xE37BA12D23e755138f7Ea8391551B773Cb2D8F5F'
sweeper = '0x3550A85877002f79db46B01672b5F86f1037cB7c'

[chain."Ganache #1".token."Mocked TOKEN 2"]
address = '0x704D76a8c31FbAafE2B7895b13A763c7487FC0D8' # checked
[chain."Ganache #1".token."Mocked TOKEN 1"]
address = '0x12a28f2bfBFfDf5842657235cC058242f40fDEa6' # checked
precision = 18
# Minimum token units for the Create2 deposits to count
# 1 * 10^18 = 1.0000 TOKEN
Expand All @@ -70,7 +72,7 @@ outpace = '0xAbc27d46a458E2e49DaBfEf45ca74dEDBAc3DD06'
# Ganache Snapshot address
sweeper = '0x7dD57C0324284102A757153E18F2Cb1ACdB7d2bD'

[chain."Ganache #1337".token."Mocked TOKEN"]
[chain."Ganache #1337".token."Mocked TOKEN 1337"]
address = '0x2bcaf6968aec8a3b5126fbfab5fd419da6e8ad8e' # checked
precision = 18
# Minimum token units for the Create2 deposits to count
Expand Down
1 change: 1 addition & 0 deletions primitives/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct Chain {
/// The OUTPACE contract address on this Chain
pub outpace: Address,
/// The Sweeper contract address on this Chain
#[deprecated = "we no longer need the sweeper contract for deposits"]
pub sweeper: Address,
}

Expand Down
1 change: 1 addition & 0 deletions primitives/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl ChainInfo {
/// Precision can differ for the same token from one [`Chain`] to another.
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
pub struct TokenInfo {
#[deprecated = "we no longer need the sweeper contract & create2 addresses for deposits"]
pub min_token_units_for_deposit: BigNum,
pub min_validator_fee: BigNum,
pub precision: NonZeroU8,
Expand Down
Loading