def test_missing_genesis_submit(init_environment): """ Calls the submit_event_proof of the contracts with a proof without genesis """ block_of_interest_index = 0 with pytest.raises(Exception) as e: submit_event_proof(interface, headless_proof, block_of_interest_index) assert extract_message_from_error(e) == errors["genesis"]
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_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_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 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_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_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_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 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_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"]
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_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"]