def test_withdraw( deposit_contract: Any, deposit_token_contract: Any, owner: Account, accounts: Accounts, chain: Chain, web3: Web3, ) -> None: withdrawal_delay = 10 withdrawal_delay_data = encode_data(withdrawal_delay) deposit_token_contract.send(deposit_contract, 100, withdrawal_delay_data) recipient = accounts[-1] chain.mine(withdrawal_delay) with brownie.reverts(): deposit_contract.withdraw(recipient, {"from": owner}) tx = deposit_contract.requestWithdrawal({"from": owner}) check_deposit_changed_event(tx, 100, withdrawal_delay, tx.block_number) assert deposit_contract.getWithdrawalRequestedBlock( owner) == tx.block_number withdraw_block_number = tx.block_number + withdrawal_delay with brownie.reverts(): deposit_contract.withdraw(recipient, {"from": owner}) mine_until(withdraw_block_number - 2, chain) with brownie.reverts(): deposit_contract.withdraw(recipient, {"from": owner}) assert web3.eth.block_number == withdraw_block_number - 1 # one block mined by sending tx tx = deposit_contract.withdraw(recipient, {"from": owner}) check_deposit_changed_event(tx, 0, 0, 0, withdrawn=True) check_deposit(deposit_contract, owner, 0, 0, 0) assert deposit_token_contract.balanceOf(recipient) == 100
def benchmark_execute_cipher_batch( config_contract: Any, batcher_contract: Any, executor_contract: Any, chain: Chain, owner: Account, keypers: Accounts, config_change_heads_up_blocks: int, ) -> None: batch_sizes = [0, 1, 10, 100] thresholds = [1, 10, 100] chain.snapshot() for batch_size in batch_sizes: for threshold in thresholds: chain.revert() tx = execute_cipher_batch( config_contract=config_contract, batcher_contract=batcher_contract, executor_contract=executor_contract, chain=chain, owner=owner, keypers=keypers, config_change_heads_up_blocks=config_change_heads_up_blocks, threshold=threshold, batch_size=batch_size, tx_size=100, ) print(f"Batch size: {batch_size:>3d} " f"Threshold: {threshold:>3d} " f"Gas: {tx.gas_used / 1000:.1f}k")
def my_own_session_run_at_beginning(_Ebb): global ebb global chain cfg.IS_BROWNIE_TEST = True config.w3 = web3 ebb = Contract.eblocbroker.eBlocBroker = config.ebb = _Ebb if not config.chain: config.chain = Chain() chain = config.chain
def my_own_session_run_at_beginning(_Ebb): global Ebb # noqa global chain # noqa global ebb # noqa config.Ebb = Ebb = Contract.Contract(is_brownie=True) config.ebb = _Ebb Contract.eblocbroker.eBlocBroker = _Ebb ebb = _Ebb Ebb.w3 = web3 if not config.chain: config.chain = Chain() chain = config.chain
def test_recover_unsold_tokens_should_transfer_all_tokens_after_exparation( executor, dao_agent, ldo_token): chain = Chain() expiration_delay = executor.offer_expires_at() - chain.time() chain.sleep(expiration_delay + 3600) chain.mine() executor_balance = ldo_token.balanceOf(executor) dao_agent_balance = ldo_token.balanceOf(dao_agent) executor.recover_unsold_tokens() assert ldo_token.balanceOf(executor) == 0 assert ldo_token.balanceOf( dao_agent) == dao_agent_balance + executor_balance
def test_cipher_execution_skips_after_timeout( executor_contract: Any, config_contract: Any, chain: Chain, config_change_heads_up_blocks: int, owner: Account, keypers: List[Account], ) -> None: config = make_batch_config( start_batch_index=0, start_block_number=chain.height + config_change_heads_up_blocks + 20, batch_span=10, execution_timeout=20, keypers=keypers, ) schedule_config(config_contract, config, owner=owner) batch_index = 0 forbidden_from_block = (config.start_block_number + (batch_index + 1) * config.batch_span + config.execution_timeout) mine_until( forbidden_from_block - 2, chain, ) chain.snapshot() # test that skipCipherExecution still fails with brownie.reverts( "ExecutorContract: execution timeout not reached yet"): executor_contract.skipCipherExecution(batch_index) # test that we could still executeCipherBatch for block forbidden_from_block -1 chain.revert() tx = executor_contract.executeCipherBatch(batch_index, ZERO_HASH32, [], 0, {"from": keypers[0]}) assert tx.events["BatchExecuted"] # test that calling executeCipherBatch after the timeout results in skipping the cipher # execution chain.revert() mine_until( forbidden_from_block - 1, chain, ) tx = executor_contract.executeCipherBatch(batch_index, ZERO_HASH32, [], 0, {"from": keypers[0]}) assert len(tx.events) == 1 assert tx.events["CipherExecutionSkipped"]["numExecutionHalfSteps"] == 1
def test_purchase_not_allowed_after_expiration_via_transfer( accounts, executor, helpers): chain = Chain() purchaser = accounts.at(accounts[0], force=True) purchase_ldo_amount = LDO_ALLOCATIONS[0] eth_cost = purchase_ldo_amount * ETH_TO_LDO_RATE_PRECISION // ETH_TO_LDO_RATE allocation = executor.get_allocation(purchaser) assert allocation[0] == purchase_ldo_amount assert allocation[1] == eth_cost helpers.fund_with_eth(purchaser, eth_cost) expiration_delay = executor.offer_expires_at() - chain.time() chain.sleep(expiration_delay + 3600) chain.mine() with reverts("offer expired"): purchaser.transfer(to=executor, amount=eth_cost, gas_limit=400_000)
import socket import time import warnings from typing import Any, Callable, Dict, Tuple, Union from urllib.parse import urlparse import psutil from brownie._singleton import _Singleton from brownie.exceptions import RPCConnectionError, RPCProcessError from brownie.network.state import Chain from brownie.network.web3 import web3 from . import ganache, geth, hardhat chain = Chain() ATTACH_BACKENDS = { "ethereumjs testrpc": ganache, "geth": geth, "hardhat": hardhat } LAUNCH_BACKENDS = { "ganache": ganache, "ethnode": geth, "geth": geth, "npx hardhat": hardhat, }
def mine_until(block_number: int, chain: Chain) -> None: current_block_number = chain.height assert current_block_number <= block_number blocks_to_mine = block_number - current_block_number chain.mine(blocks_to_mine)