Ejemplo n.º 1
0
def test_query_excludes_outdated_matching_records(enr_db):
    private_key_a = PrivateKeyFactory().to_bytes()

    enr_a_0 = ENRFactory(
        sequence_number=0,
        private_key=private_key_a,
        custom_kv_pairs={b"test": b"value-A"},
    )
    enr_a_7 = ENRFactory(sequence_number=7, private_key=private_key_a)

    private_key_b = PrivateKeyFactory().to_bytes()

    enr_b_1 = ENRFactory(
        sequence_number=1,
        private_key=private_key_b,
        custom_kv_pairs={b"test": b"value-A"},
    )

    enr_db.set_enr(enr_a_0)
    enr_db.set_enr(enr_a_7)

    enr_db.set_enr(enr_b_1)

    enr_results = tuple(enr_db.query(KeyExists(b"test")))
    assert len(enr_results) == 1

    enr = enr_results[0]
    assert enr == enr_b_1
Ejemplo n.º 2
0
def test_enr_manager_handles_existing_enr_in_database(enr_db):
    private_key = PrivateKeyFactory()
    enr = ENRFactory(private_key=private_key.to_bytes(), sequence_number=10)
    enr_db.set_enr(enr)

    enr_manager = ENRManager(private_key, enr_db)
    assert enr_manager.enr == enr
Ejemplo n.º 3
0
def test_enr_manager_update_api(enr_db):
    enr_manager = ENRManager(PrivateKeyFactory(), enr_db)
    assert b"unicorns" not in enr_manager.enr
    base_enr = enr_manager.enr

    enr_manager.update((b"unicorns", b"rainbows"))
    enr_a = enr_manager.enr
    assert enr_a.sequence_number == base_enr.sequence_number + 1
    assert enr_manager.enr == enr_db.get_enr(enr_manager.enr.node_id)

    assert enr_manager.enr[b"unicorns"] == b"rainbows"

    enr_manager.update((b"unicorns", b"cupcakes"))
    enr_b = enr_manager.enr
    assert enr_b.sequence_number == enr_a.sequence_number + 1
    assert enr_manager.enr == enr_db.get_enr(enr_manager.enr.node_id)

    assert enr_manager.enr[b"unicorns"] == b"cupcakes"

    with pytest.raises(ValidationError):
        enr_manager.update((b"dup", b"a"), (b"dup", b"b"))

    enr_manager.update((b"unicorns", None))
    enr_c = enr_manager.enr
    assert enr_manager.enr == enr_db.get_enr(enr_manager.enr.node_id)

    with pytest.raises(KeyError):
        enr_c[b"unicorns"]
Ejemplo n.º 4
0
def test_enr_db_raises_DuplicateRecord(enr_db):
    private_key = PrivateKeyFactory().to_bytes()

    enr_a = ENRFactory(
        private_key=private_key,
        sequence_number=1,
        custom_kv_pairs={b"custom": b"enr-a"},
    )
    enr_b = ENRFactory(
        private_key=private_key,
        sequence_number=1,
        custom_kv_pairs={b"custom": b"enr-b"},
    )

    assert enr_a.node_id == enr_b.node_id
    assert enr_a.sequence_number == enr_b.sequence_number

    assert enr_a != enr_b

    # set it the first time.
    enr_db.set_enr(enr_a)

    with pytest.raises(DuplicateRecord):
        enr_db.set_enr(enr_b, raise_on_error=True)

    # without the flag it should silently ignore the error
    enr_db.set_enr(enr_b)
Ejemplo n.º 5
0
def test_enr_manager_updates_existing_enr(enr_db):
    private_key = PrivateKeyFactory()
    base_enr = ENRFactory(
        private_key=private_key.to_bytes(),
        sequence_number=0,
        custom_kv_pairs={b"unicorns": b"rainbows"},
    )
    assert base_enr[b"unicorns"] == b"rainbows"
    enr_db.set_enr(base_enr)

    enr_manager = ENRManager(
        private_key,
        enr_db,
        kv_pairs={b"unicorns": b"cupcakes"},
    )
    assert enr_manager.enr != base_enr
    assert enr_manager.enr.sequence_number == base_enr.sequence_number + 1
    assert enr_manager.enr[b"unicorns"] == b"cupcakes"
    assert enr_manager.enr == enr_db.get_enr(enr_manager.enr.node_id)
Ejemplo n.º 6
0
def test_get_and_set_enr(enr_db):
    private_key = PrivateKeyFactory().to_bytes()
    db = enr_db
    enr = ENRFactory(private_key=private_key)

    with pytest.raises(KeyError):
        db.get_enr(enr.node_id)

    db.set_enr(enr)
    assert db.get_enr(enr.node_id) == enr
Ejemplo n.º 7
0
def test_query_only_returns_latest_record(enr_db):
    private_key_a = PrivateKeyFactory().to_bytes()

    enr_a_0 = ENRFactory(sequence_number=0, private_key=private_key_a)
    enr_a_7 = ENRFactory(sequence_number=7, private_key=private_key_a)

    private_key_b = PrivateKeyFactory().to_bytes()

    enr_b_1 = ENRFactory(sequence_number=1, private_key=private_key_b)
    enr_b_9 = ENRFactory(sequence_number=9, private_key=private_key_b)

    enr_db.set_enr(enr_a_0)
    enr_db.set_enr(enr_a_7)

    enr_db.set_enr(enr_b_1)
    enr_db.set_enr(enr_b_9)

    enr_results = set(enr_db.query())
    assert len(enr_results) == 2

    assert enr_a_7 in enr_results
    assert enr_b_9 in enr_results
Ejemplo n.º 8
0
def test_enr_manager_creates_enr_if_not_present(enr_db):
    enr_manager = ENRManager(PrivateKeyFactory(), enr_db)
    assert enr_manager.enr
    assert enr_db.get_enr(enr_manager.enr.node_id) == enr_manager.enr
Ejemplo n.º 9
0
def enr_manager():
    enr_db = QueryableENRDB(sqlite3.connect(":memory:"))
    return ENRManager(PrivateKeyFactory(), enr_db)