2024-01-29 13:29:56 +00:00
|
|
|
from unittest import TestCase
|
2024-01-31 22:09:03 +00:00
|
|
|
from itertools import repeat
|
2024-01-29 13:29:56 +00:00
|
|
|
import numpy as np
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from copy import deepcopy
|
2024-02-01 09:56:49 +00:00
|
|
|
from cryptarchia.cryptarchia import (
|
|
|
|
maxvalid_bg,
|
|
|
|
Chain,
|
|
|
|
BlockHeader,
|
|
|
|
Slot,
|
|
|
|
Id,
|
|
|
|
MockLeaderProof,
|
2024-02-01 10:53:59 +00:00
|
|
|
Coin,
|
2024-02-01 09:56:49 +00:00
|
|
|
)
|
2024-01-29 13:29:56 +00:00
|
|
|
|
|
|
|
|
2024-01-31 22:09:03 +00:00
|
|
|
def make_block(parent_id: Id, slot: Slot, content: bytes) -> BlockHeader:
|
|
|
|
assert len(parent_id) == 32
|
|
|
|
content_id = hashlib.sha256(content).digest()
|
|
|
|
return BlockHeader(
|
2024-02-01 09:56:49 +00:00
|
|
|
parent=parent_id,
|
|
|
|
content_size=1,
|
|
|
|
slot=slot,
|
|
|
|
content_id=content_id,
|
2024-02-09 14:12:12 +00:00
|
|
|
leader_proof=MockLeaderProof.new(
|
|
|
|
Coin(sk=0, value=10), slot=slot, parent=parent_id
|
|
|
|
),
|
2024-01-31 22:09:03 +00:00
|
|
|
)
|
2024-01-29 13:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLeader(TestCase):
|
|
|
|
def test_fork_choice_long_sparse_chain(self):
|
|
|
|
# The longest chain is not dense after the fork
|
2024-01-31 22:09:03 +00:00
|
|
|
common = [make_block(bytes(32), Slot(i), bytes(i)) for i in range(1, 50)]
|
2024-01-29 13:29:56 +00:00
|
|
|
long_chain = deepcopy(common)
|
|
|
|
short_chain = deepcopy(common)
|
|
|
|
|
|
|
|
for slot in range(50, 100):
|
|
|
|
# make arbitrary ids for the different chain so that the blocks appear to be different
|
2024-01-31 22:09:03 +00:00
|
|
|
long_content = f"{slot}-long".encode()
|
|
|
|
short_content = f"{slot}-short".encode()
|
2024-01-29 13:29:56 +00:00
|
|
|
if slot % 2 == 0:
|
2024-01-31 22:09:03 +00:00
|
|
|
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
|
|
|
|
short_chain.append(make_block(bytes(32), Slot(slot), short_content))
|
2024-01-29 13:29:56 +00:00
|
|
|
# add more blocks to the long chain
|
|
|
|
for slot in range(100, 200):
|
2024-01-31 22:09:03 +00:00
|
|
|
long_content = f"{slot}-long".encode()
|
|
|
|
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
|
2024-01-29 13:29:56 +00:00
|
|
|
assert len(long_chain) > len(short_chain)
|
|
|
|
# by setting a low k we trigger the density choice rule
|
|
|
|
k = 1
|
|
|
|
s = 50
|
2024-02-09 14:12:12 +00:00
|
|
|
short_chain = Chain(short_chain, genesis=bytes(32))
|
|
|
|
long_chain = Chain(long_chain, genesis=bytes(32))
|
|
|
|
assert (
|
|
|
|
maxvalid_bg(
|
|
|
|
short_chain,
|
|
|
|
[long_chain],
|
|
|
|
k,
|
|
|
|
s,
|
|
|
|
)
|
|
|
|
== short_chain
|
2024-01-29 13:29:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# However, if we set k to the fork length, it will be accepted
|
2024-02-09 14:12:12 +00:00
|
|
|
k = long_chain.length()
|
|
|
|
assert (
|
|
|
|
maxvalid_bg(
|
|
|
|
short_chain,
|
|
|
|
[long_chain],
|
|
|
|
k,
|
|
|
|
s,
|
|
|
|
)
|
|
|
|
== long_chain
|
2024-01-29 13:29:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_fork_choice_long_dense_chain(self):
|
|
|
|
# The longest chain is also the densest after the fork
|
2024-01-31 22:09:03 +00:00
|
|
|
common = [make_block(bytes(32), Slot(i), bytes(i)) for i in range(1, 50)]
|
2024-01-29 13:29:56 +00:00
|
|
|
long_chain = deepcopy(common)
|
|
|
|
short_chain = deepcopy(common)
|
|
|
|
for slot in range(50, 100):
|
|
|
|
# make arbitrary ids for the different chain so that the blocks appear to be different
|
2024-01-31 22:09:03 +00:00
|
|
|
long_content = f"{slot}-long".encode()
|
|
|
|
short_content = f"{slot}-short".encode()
|
|
|
|
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
|
2024-01-29 13:29:56 +00:00
|
|
|
if slot % 2 == 0:
|
2024-01-31 22:09:03 +00:00
|
|
|
short_chain.append(make_block(bytes(32), Slot(slot), short_content))
|
2024-01-29 13:29:56 +00:00
|
|
|
k = 1
|
|
|
|
s = 50
|
2024-02-09 14:12:12 +00:00
|
|
|
short_chain = Chain(short_chain, genesis=bytes(32))
|
|
|
|
long_chain = Chain(long_chain, genesis=bytes(32))
|
|
|
|
assert (
|
|
|
|
maxvalid_bg(
|
|
|
|
short_chain,
|
|
|
|
[long_chain],
|
|
|
|
k,
|
|
|
|
s,
|
|
|
|
)
|
|
|
|
== long_chain
|
2024-01-29 13:29:56 +00:00
|
|
|
)
|