mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-19 13:18:26 +02:00
Delete interests module (#2073)
This commit is contained in:
parent
5997d16031
commit
964477ceb0
2 changed files with 0 additions and 326 deletions
|
|
@ -1,75 +0,0 @@
|
|||
# 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 typing import Iterable
|
||||
|
||||
from squeak.core import CSqueak
|
||||
from squeak.net import CInterested
|
||||
|
||||
|
||||
EMPTY_HASH = b'\x00' * 32
|
||||
|
||||
|
||||
def squeak_matches_interest(squeak: CSqueak, interest: CInterested) -> bool:
|
||||
if len(interest.pubkeys) > 0 \
|
||||
and squeak.GetPubKey() not in interest.pubkeys:
|
||||
return False
|
||||
if interest.nMinBlockHeight != -1 \
|
||||
and squeak.nBlockHeight < interest.nMinBlockHeight:
|
||||
return False
|
||||
if interest.nMaxBlockHeight != -1 \
|
||||
and squeak.nBlockHeight > interest.nMaxBlockHeight:
|
||||
return False
|
||||
if interest.hashReplySqk != EMPTY_HASH \
|
||||
and squeak.hashReplySqk != interest.hashReplySqk:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_differential_squeaks(
|
||||
interest: CInterested,
|
||||
old_interest: CInterested,
|
||||
) -> Iterable[CInterested]:
|
||||
# Get new squeaks below the current min_block
|
||||
if interest.nMinBlockHeight < old_interest.nMinBlockHeight:
|
||||
yield CInterested(
|
||||
pubkeys=old_interest.pubkeys,
|
||||
nMinBlockHeight=interest.nMinBlockHeight,
|
||||
nMaxBlockHeight=old_interest.nMinBlockHeight - 1,
|
||||
)
|
||||
# Get new squeaks above the current max_block
|
||||
if (interest.nMaxBlockHeight == -1 and old_interest.nMaxBlockHeight != -1) or \
|
||||
interest.nMaxBlockHeight > old_interest.nMaxBlockHeight:
|
||||
yield CInterested(
|
||||
pubkeys=old_interest.pubkeys,
|
||||
nMinBlockHeight=old_interest.nMaxBlockHeight + 1,
|
||||
nMaxBlockHeight=interest.nMaxBlockHeight,
|
||||
)
|
||||
# Get new squeaks for new pubkeys
|
||||
follow_pubkeys = set(interest.pubkeys)
|
||||
old_follow_pubkeys = set(old_interest.pubkeys)
|
||||
new_pubkeys = tuple(follow_pubkeys - old_follow_pubkeys)
|
||||
if len(new_pubkeys) > 0:
|
||||
yield CInterested(
|
||||
pubkeys=new_pubkeys,
|
||||
nMinBlockHeight=interest.nMinBlockHeight,
|
||||
nMaxBlockHeight=interest.nMaxBlockHeight,
|
||||
)
|
||||
|
|
@ -1,251 +0,0 @@
|
|||
# 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue