2020-02-26 01:13:26 +03:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-02-27 05:23:03 +03:00
|
|
|
from src.util.merkle_set import MerkleSet, confirm_included_already_hashed
|
2020-06-27 12:19:42 +03:00
|
|
|
from tests.setup_nodes import test_constants, bt
|
2020-07-06 21:46:52 +03:00
|
|
|
from src.util.wallet_tools import WalletTool
|
2020-02-26 01:13:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def event_loop():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
yield loop
|
|
|
|
|
|
|
|
|
|
|
|
class TestMerkleSet:
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_basics(self):
|
2020-06-29 21:46:27 +03:00
|
|
|
WalletTool()
|
2020-02-26 01:13:26 +03:00
|
|
|
|
|
|
|
num_blocks = 10
|
2020-09-16 21:11:01 +03:00
|
|
|
blocks = bt.get_consecutive_blocks(
|
|
|
|
test_constants,
|
|
|
|
num_blocks,
|
|
|
|
[],
|
|
|
|
10,
|
|
|
|
)
|
2020-02-26 01:13:26 +03:00
|
|
|
|
2020-02-26 03:15:23 +03:00
|
|
|
merkle_set = MerkleSet()
|
2020-02-26 23:03:25 +03:00
|
|
|
merkle_set_reverse = MerkleSet()
|
|
|
|
|
|
|
|
for block in reversed(blocks):
|
2020-06-23 12:50:56 +03:00
|
|
|
merkle_set_reverse.add_already_hashed(block.get_coinbase().name())
|
2020-02-26 23:03:25 +03:00
|
|
|
|
2020-02-26 01:13:26 +03:00
|
|
|
for block in blocks:
|
2020-06-23 12:50:56 +03:00
|
|
|
merkle_set.add_already_hashed(block.get_coinbase().name())
|
2020-02-26 01:13:26 +03:00
|
|
|
|
|
|
|
for block in blocks:
|
2020-02-27 05:23:03 +03:00
|
|
|
result, proof = merkle_set.is_included_already_hashed(
|
2020-06-23 12:50:56 +03:00
|
|
|
block.get_coinbase().name()
|
2020-02-27 05:23:03 +03:00
|
|
|
)
|
2020-02-26 01:13:26 +03:00
|
|
|
assert result is True
|
2020-02-27 05:23:03 +03:00
|
|
|
result_fee, proof_fee = merkle_set.is_included_already_hashed(
|
2020-06-23 12:50:56 +03:00
|
|
|
block.get_fees_coin().name()
|
2020-02-27 05:23:03 +03:00
|
|
|
)
|
2020-02-26 01:13:26 +03:00
|
|
|
assert result_fee is False
|
2020-02-27 05:23:03 +03:00
|
|
|
validate_proof = confirm_included_already_hashed(
|
2020-06-23 12:50:56 +03:00
|
|
|
merkle_set.get_root(), block.get_coinbase().name(), proof
|
2020-02-27 05:23:03 +03:00
|
|
|
)
|
|
|
|
validate_proof_fee = confirm_included_already_hashed(
|
2020-06-23 12:50:56 +03:00
|
|
|
merkle_set.get_root(), block.get_fees_coin().name(), proof_fee
|
2020-02-27 05:23:03 +03:00
|
|
|
)
|
2020-02-26 01:13:26 +03:00
|
|
|
assert validate_proof is True
|
|
|
|
assert validate_proof_fee is False
|
2020-02-26 23:03:25 +03:00
|
|
|
|
|
|
|
# Test if order of adding items change the outcome
|
|
|
|
assert merkle_set.get_root() == merkle_set_reverse.get_root()
|