Example #1
0
def init_environment():
    """
    This runs before every test
    """

    global backend
    global submit_proof
    global small_contest_proof
    global small_lca
    global large_contest_proof
    global large_lca

    backend = "ganache"

    pt = ProofTool()
    pt.fetch_proof(1000)
    (
        submit_proof_name,
        small_contest_proof_name,
        small_lca,
        small_header,
        small_header_map,
        small_interlink_map,
    ) = pt.create_proof_and_forkproof(1000, 200, 100)
    (
        submit_proof_name,
        large_contest_proof_name,
        large_lca,
        large_header,
        large_header_map,
        large_interlink_map,
    ) = pt.create_proof_and_forkproof(1000, 100, 200)

    submit_proof = Proof()
    submit_proof.set(pt.fetch_proof(submit_proof_name))

    small_contest_proof = Proof()
    small_contest_proof.set(
        pt.fetch_proof(small_contest_proof_name),
        header=small_header,
        header_map=small_header_map,
        interlink_map=small_interlink_map,
    )

    large_contest_proof = Proof()
    large_contest_proof.set(
        pt.fetch_proof(large_contest_proof_name),
        header=large_header,
        header_map=large_header_map,
        interlink_map=large_interlink_map,
    )
def main():
    """
    Test Proof
    """

    proof_tool = ProofTool()
    (
        proof_name,
        fork_proof_name,
        lca,
        fork_header,
        fork_header_map,
        fork_interlink_map,
    ) = proof_tool.create_proof_and_forkproof(100, 10, 20)

    my_proof = Proof()
    my_proof.set(proof_tool.fetch_proof(proof_name))
    p = my_proof.best_level_subproof

    my_fork_proof = Proof()
    my_fork_proof.set(
        proof_tool.fetch_proof(fork_proof_name),
        header=fork_header,
        header_map=fork_header_map,
        interlink_map=fork_interlink_map,
    )
    pp = my_fork_proof.best_level_subproof

    my_proof_tip = Proof()
    my_proof_tip.set(my_proof.proof[:lca])

    print(blockchain_utils.Hash(my_fork_proof.proof[-1][0]).hex())
    print(blockchain_utils.Hash(pp[-1][0]).hex())

    print("Best proof has length", len(my_fork_proof.best_level_subproof))
Example #3
0
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,
    )
Example #4
0
def main():
    """
    Create, edit and print chains and proofs
    """

    parser = argparse.ArgumentParser(
        description="Prints the contents of a NiPoPoW")
    parser.add_argument("--blocks",
                        required=True,
                        type=int,
                        help="Number of blocks")
    parser.add_argument("--output",
                        default="proof.pkl",
                        type=str,
                        help="Name of exported proof")
    args = parser.parse_args()
    blocks = args.blocks
    output = args.output
    if output.find(".pkl") == -1:
        output += ".pkl"

    # Create blockchain
    header, headers_map, interlink_map = create_blockchain(blocks=blocks)
    print_headers(headers_map)
    print_interlinks(headers_map, interlink_map)

    # Create proof
    proof = make_proof(header, headers_map, interlink_map)
    print_proof(proof, headers_map)

    ### Start spoiling proof

    # remove_genesis(proof)
    # proof = change_interlink_hash(proof, 0)
    # proof = skip_blocks(proof, -2)
    # proof = replace_block(proof, headers_map, interlink_map, int(len(proof)/2))
    # print_proof(proof, headers_map)
    # verify_proof(Hash(proof[0][0]), proof)

    ### Stop spoiling proof

    proof_tool = ProofTool("../../data/proofs/")
    p, f, lca = proof_tool.create_proof_and_forkproof(blocks, forkindex,
                                                      forkblocks)
    print(p, f, lca)

    fixed_fork_proof = proof_tool.fetch_proof(f)
    verify_proof(Hash(fixed_fork_proof[0][0]), fixed_fork_proof)
Example #5
0
def main():
    """
    Test for backends
    """

    proof_tool = ProofTool("../data/proofs/")
    proof_tool.fetch_proof(80)
    (submit_n, contest_n, _, _, _,
     _) = proof_tool.create_proof_and_forkproof(80, 10, 25)

    proof = Proof()
    proof.set(proof_tool.fetch_proof_by_name(submit_n))

    c_proof = Proof()
    c_proof.set(proof_tool.fetch_proof_by_name(contest_n))

    interface = ContractInterface(contract={
        "path": "../OldContract.sol",
        "ctor": []
    },
                                  backend="geth")

    result = interface.call(
        "submitEventProof",
        function_args=[proof.headers, proof.siblings, proof.headers[-1]],
        value=pow(10, 17),
    )
    sum = 0
    print(result)
    for e in result["events"]:
        print(e[0], "\t", e[1])
        sum += e[1]
    print("Sum:", sum)

    result = interface.call(
        "submitContestingProof",
        function_args=[c_proof.headers, c_proof.siblings, c_proof.headers[-1]],
    )

    interface.end()

    sum = 0
    print(result)
    for e in result["events"]:
        print(e[0], "\t", e[1])
        sum += e[1]
    print("Sum:", sum)
Example #6
0
def init_proofs(backend, mainchain_blocks, fork_point, additional_blocks):
    """
    create proofs for two contesting chains

    mainchain_blocks
    +-------------+
    v             v

    --------+----->  Chain A
            |
       +->  +-------->  Chain B
       |
       |    ^        ^
       |    |        |
       |    +--------+
       |          |
    fork_point    additional_blocks

    """

    pt = ProofTool()
    pt.fetch_proof(mainchain_blocks)
    (
        submit_proof_name,
        contest_proof_name,
        contest_lca,
        contest_header,
        contest_header_map,
        contest_interlink_map,
    ) = pt.create_proof_and_forkproof(mainchain_blocks, fork_point,
                                      additional_blocks)

    submit_proof = Proof()
    submit_proof.set(pt.fetch_proof(submit_proof_name))

    contest_proof = Proof()
    contest_proof.set(
        pt.fetch_proof(contest_proof_name),
        header=contest_header,
        header_map=contest_header_map,
        interlink_map=contest_interlink_map,
    )

    return (submit_proof, contest_lca, contest_proof)
Example #7
0
def init_environment():
    """
    This runs before every test
    """

    global backend
    global interface

    backend = "ganache"
    interface = ContractInterface(
        {
            "path": "../contractNipopow.sol",
            "ctor": [genesis, m, k]
        },
        backend=backend,
    )

    global submit_proof
    global contest_proof

    pt = ProofTool()
    pt.fetch_proof(20)
    (
        submit_proof_name,
        contest_proof_name,
        contest_lca,
        contest_header,
        contest_header_map,
        contest_interlink_map,
    ) = pt.create_proof_and_forkproof(100, 50, 50)

    submit_proof = Proof()
    submit_proof.set(pt.fetch_proof(submit_proof_name))

    contest_proof = Proof()
    contest_proof.set(
        pt.fetch_proof(contest_proof_name),
        header=contest_header,
        header_map=contest_header_map,
        interlink_map=contest_interlink_map,
    )