def test_receive_collateral(init_environment): interface = make_interface(backend) from_address = interface.w3.eth.accounts[0] initial_balance = interface.w3.eth.getBalance(from_address) block_of_interest_index = proof.size - 1 collateral = pow(10, 17) res = submit_event_proof( interface, proof, block_of_interest_index, collateral=collateral, from_address=from_address, ) assert res["result"] == True after_submit = interface.w3.eth.getBalance(from_address) assert initial_balance - after_submit > collateral k = 6 for _ in range(k): finalize_event(interface, proof.headers[block_of_interest_index]) after_finalize = interface.w3.eth.getBalance(from_address) assert after_finalize > after_submit
def test_boi_in_small(init_environment): """ Block of interest contained in both chains --------+--x--> Ca | +--------> Cb """ block_of_interest_index = 0 interface = make_interface(backend) res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True res = submit_contesting_proof( interface, submit_proof, large_lca, large_contest_proof, block_of_interest_index, ) assert res["result"] == True
def init_environment(): """ This runs before every test """ global interface interface = make_interface("ganache") mainblocks = 100 fork_index = 50 forkblocks = 100 pt = ProofTool("../data/proofs/") ( proof_name, fork_name, lca, fork_header, fork_header_map, fork_interlink_map, ) = pt.create_proof_and_forkproof(mainblocks, fork_index, forkblocks) global proof proof = Proof() proof.set(pt.fetch_proof_by_name(proof_name)) global fork fork = Proof() fork.set( pt.fetch_proof_by_name(fork_name), header=fork_header, header_map=fork_header_map, interlink_map=fork_interlink_map, )
def test_dispute_invalid(init_environment): """ Try to dispute valid proof """ block_of_interest_index = 0 interface = make_interface(backend) invalid_index = int(proof.size / 2) invalid_proof = Proof() invalid_proof.set(change_interlink_hash(proof.proof, invalid_index)) res = submit_event_proof( interface, invalid_proof, block_of_interest_index, profile=True ) assert res["result"] == True res = dispute_existing_proof( interface, invalid_proof, block_of_interest_index, invalid_index, profile=True, ) assert res["result"] == True
def test_boi_in_common_submit_big(init_environment): """ Block of interest contained in both chains ----x---+-------> Ca | +----> Cb """ interface = make_interface(backend) block_of_interest_index = submit_proof.size - 1 res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True with pytest.raises(Exception) as ex: res = submit_contesting_proof( interface, submit_proof, small_lca, small_contest_proof, block_of_interest_index, ) assert extract_message_from_error(ex) == errors["boi in sub-chain"]
def test_revert_on_submit(init_environment): """ Submits a proof with block with changed interlink """ interface = make_interface(backend) res = validate_interlink(interface, changed_interlink_proof) assert res["result"] == False
def test_revert_on_missing_blocks_submit(init_environment): """ Submits a proof with a missing block """ interface = make_interface(backend) res = validate_interlink(interface, missing_blocks_proof) assert res["result"] == False
def test_replaced_block(init_environment): """ Submits a proof with a replaced block """ interface = make_interface(backend) res = validate_interlink(interface, replaced_blocks_proof) assert res["result"] == False
def finish_session(request): """ This runs after every test is finished """ yield # you can access the session from the injected 'request': session = request.session interface = make_interface("ganache") interface.end()
def finish_session(request): """ This runs after ever test """ yield # you can access the session from the injected 'request': session = request.session interface = make_interface(backend) interface.end()
def test_smaller_k(init_environment): """ Submit a proof with k=1 """ proof = Proof() proof.set(pt.fetch_proof("../data/proofs/proof_100_m15_k1.pkl")) interface = make_interface(backend) with pytest.raises(Exception) as ex: submit_event_proof(interface, proof, 0)
def test_insufficient_collateral(init_environment): """ Test contract call with insufficient collateral """ interface = make_interface(backend) collateral = pow(10, 17) - 1 with pytest.raises(Exception) as ex: submit_event_proof(interface, proof, proof.size - 1, collateral=collateral) assert extract_message_from_error(ex) == errors["ante up"]
def test_sufficient_collateral(init_environment): """ Test contract call with sufficient collateral """ interface = make_interface(backend) # Collateral defined in contract: # uint constant z = 100000000000000000; // 0.1 eth, 10^17 res = submit_event_proof(interface, proof, proof.size - 1, collateral=pow(10, 17)) assert res["result"] == True
def test_boi_out_of_index(init_environment): """ Block of interest contained in both chains ----------------> Ca x """ interface = make_interface(backend) block_of_interest_index = submit_proof.size with pytest.raises(Exception) as ex: res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert extract_message_from_error(ex) == errors["boi not exist"]
def test_dispute_valid(init_environment): """ Try to dispute valid proof """ block_of_interest_index = 0 interface = make_interface(backend) res = submit_event_proof( interface, proof, block_of_interest_index, profile=True ) assert res["result"] == True with pytest.raises(Exception) as ex: res = dispute_existing_proof( interface, proof, block_of_interest_index, 1, profile=True ) assert extract_message_from_error(ex) == errors["valid existing"]
def submit_over_contest(backend, mainchain_blocks, fork_point, additional_blocks): """ Display gas for submit and contest proofs Contest must succeed if we want to measure gas for both phases, so be sure contesting proof is bigger """ # compile and deploy smart contract interface = make_interface(backend) # retrieve submit and contest proof (submit_proof, contest_lca, contest_proof) = init_proofs(backend, mainchain_blocks, fork_point, additional_blocks) # we want to prove the existance of a block that exists only in submit chain # for convinience, we pick the tip of submit proof block_of_interest_index = 0 # start submit phase res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) # get the result. This includes the amount of gas used print("Submit proof gas:", res["gas"]) # be sure that worked assert res["result"], "submit proof failed" # start contest phase res = submit_contesting_proof( interface, submit_proof, contest_lca, contest_proof, block_of_interest_index, ) # get the result. This includes the amount of gas used print("Contest proof gas:", res["gas"]) # be sure that worked assert res["result"], "contest proof failed" # close interfase interface.end()
def init_environment(): """ This runs before every test """ backend = "ganache" global interface interface = make_interface(backend) global proof global headless_proof proof = Proof() headless_proof = Proof() original_proof = ProofTool("../data/proofs/").fetch_proof(100) proof.set(original_proof) _headless_proof = original_proof.copy() remove_genesis(_headless_proof) headless_proof.set(_headless_proof)
def run_nipopow(backend, proof): """ Make a call to verifier with proof. The block of interest is the last block of the chain. """ block_of_interest_index = 0 interface = make_interface(backend) _t = Timer() try: result = submit_event_proof(interface, proof, block_of_interest_index, profile=True) except Exception as ex: print(ex) result = {"result": extract_message_from_error(ex)} del _t interface.end() return result["result"]
def test_proof_exists(init_environment): """ Contest proof exists """ interface = make_interface(backend) block_of_interest_index = 0 res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True with pytest.raises(Exception) as ex: res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert extract_message_from_error(ex) == errors["period expired"]
def test_boi_out_of_index_contest(init_environment): """ Block of interest is in submit but not in contest proof """ interface = make_interface(backend) block_of_interest_index = submit_proof.size - 1 res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True with pytest.raises(Exception) as ex: res = submit_contesting_proof( interface, submit_proof, large_lca, large_contest_proof, submit_proof.size, # This is out of range ) assert extract_message_from_error(ex) == errors["boi not exist"]
def test_same_proofs(init_environment): """ Submit proof is the same as contest proof """ interface = make_interface(backend) block_of_interest_index = submit_proof.size - 1 res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True with pytest.raises(Exception) as ex: res = submit_contesting_proof( interface, submit_proof, 0, submit_proof, block_of_interest_index, ) assert extract_message_from_error(ex) == errors["boi in sub-chain"]
def test_wrong_lca(init_environment): """ Contest proof lies about lca """ interface = make_interface(backend) block_of_interest_index = 0 res = submit_event_proof( interface, submit_proof, block_of_interest_index, ) assert res["result"] == True with pytest.raises(Exception) as ex: res = submit_contesting_proof( interface, submit_proof, large_lca - 1, # this is wrong large_contest_proof, block_of_interest_index, ) assert extract_message_from_error(ex) == errors["wrong lca"]