squeaknode/tests/core/test_interests.py
Jonathan Zernik 5198467889
Use new squeak protocol with schnorr and pubkey as identifier (#1870)
* Got all tests in core dir passing

* Got all tests in db dir passing

* Got tests for admin dir passing

* Got all unit tests passing

* Added migration for squeak table and profile table using new pubkey

* Re-start db migrations with new data-v2.db file

* Fix column type for profile public key

* Got all itests passing

* Log the public key in hex in create contact profile handle fn

* Rename rpc methods to use pubkey instead of address

* Got frontend mostly working with new profile pubkey

* Update frontend to use pubkey instead of address

* Change size of squeak profile detail item

* Update create contact profile dialog to use pubkey

* Update twitter account item to use pubkey

* Include label for pubkey in profile detail item

* Decrease size of profile detail item

* Update frontend build

* Improve logging of requests in handler
2021-12-20 09:28:38 -08:00

251 lines
7.6 KiB
Python

# MIT License
#
# Copyright (c) 2020 Jonathan Zernik
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from squeak.net import CInterested
from squeaknode.core.interests import get_differential_squeaks
from squeaknode.core.interests import squeak_matches_interest
from tests.utils import gen_random_hash
from tests.utils import gen_squeak
from tests.utils import gen_squeak_pubkeys
def test_squeak_matches_interest(private_key, public_key):
squeak = gen_squeak(private_key, 5678)
interest = CInterested(
pubkeys=(public_key,),
nMinBlockHeight=5000,
nMaxBlockHeight=6000,
)
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_empty_pubkeys(private_key, public_key):
squeak = gen_squeak(private_key, 5678)
interest = CInterested(
nMinBlockHeight=5000,
nMaxBlockHeight=6000,
)
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_above_block_range(private_key, public_key):
squeak = gen_squeak(private_key, 5678)
interest = CInterested(
pubkeys=(public_key,),
nMinBlockHeight=4000,
nMaxBlockHeight=5000,
)
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_below_block_range(private_key, public_key):
squeak = gen_squeak(private_key, 5678)
interest = CInterested(
pubkeys=(public_key,),
nMinBlockHeight=6000,
nMaxBlockHeight=7000,
)
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_pubkey_no_match(private_key, public_key):
squeak = gen_squeak(private_key, 5678)
other_pubkeys = tuple(gen_squeak_pubkeys(3))
interest = CInterested(
pubkeys=other_pubkeys,
nMinBlockHeight=5000,
nMaxBlockHeight=6000,
)
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_is_reply(private_key):
replyto_hash = gen_random_hash()
squeak = gen_squeak(private_key, 5678, replyto_hash=replyto_hash)
interest = CInterested(
hashReplySqk=replyto_hash,
)
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_is_not_reply(private_key):
replyto_hash = gen_random_hash()
squeak = gen_squeak(private_key, 5678, replyto_hash=replyto_hash)
other_replyto_hash = gen_random_hash()
interest = CInterested(
hashReplySqk=other_replyto_hash,
)
assert not squeak_matches_interest(squeak, interest)
def test_get_differential_squeaks():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=11,
nMaxBlockHeight=21,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert differential_results == [
CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=21,
nMaxBlockHeight=21,
)
]
def test_get_differential_squeaks_no_difference():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert differential_results == []
def test_get_differential_squeaks_new_pubkeys():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
additional_pubkeys = tuple(gen_squeak_pubkeys(3))
interest = CInterested(
pubkeys=tuple(list(squeak_pubkeys) + list(additional_pubkeys)),
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert len(differential_results) == 1
first_result = differential_results[0]
assert set(first_result.pubkeys) == set(additional_pubkeys)
assert first_result.nMinBlockHeight == 10
assert first_result.nMaxBlockHeight == 20
def test_get_differential_squeaks_default_min():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=-1,
nMaxBlockHeight=20,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert differential_results == [
CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=-1,
nMaxBlockHeight=9,
)
]
def test_get_differential_squeaks_default_max():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=-1,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert differential_results == [
CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=21,
nMaxBlockHeight=-1,
)
]
def test_get_differential_squeaks_new_pubkeys_and_min_max():
squeak_pubkeys = tuple(gen_squeak_pubkeys(3))
old_interest = CInterested(
pubkeys=squeak_pubkeys,
nMinBlockHeight=10,
nMaxBlockHeight=20,
)
new_pubkeys = tuple(gen_squeak_pubkeys(3))
interest = CInterested(
pubkeys=new_pubkeys,
nMinBlockHeight=-1,
nMaxBlockHeight=-1,
)
differential_results = list(
get_differential_squeaks(interest, old_interest))
assert len(differential_results) == 3
first_result = differential_results[0]
assert set(first_result.pubkeys) == set(squeak_pubkeys)
assert first_result.nMinBlockHeight == -1
assert first_result.nMaxBlockHeight == 9
second_result = differential_results[1]
assert set(second_result.pubkeys) == set(squeak_pubkeys)
assert second_result.nMinBlockHeight == 21
assert second_result.nMaxBlockHeight == -1
third_result = differential_results[2]
assert set(third_result.pubkeys) == set(new_pubkeys)
assert third_result.nMinBlockHeight == -1
assert third_result.nMaxBlockHeight == -1