mirror of
https://github.com/logos-co/nomos-specs.git
synced 2025-01-24 14:29:52 +00:00
2d3f463bb7
* feat(cryptarchia/epoch-schedule): switch to 334 schedule (from 433) * factor out common test config building code * feat(cryptarchia): test_leader uses common test config object * cryptarchia: update test_ledger_state_is_properly_updated_on_reorg * cryptarchia: update test_epoch_transition test * move to .tip() in tests instead of tip_id() * cryptarchia: wrap long comments * cryptarchia: move mk_block to test_common * cryptarchia: move mk_genesis_state to test_common * cryptarchia: refactor fork test to use mk_chain * cryptarchia: fork choice rules tests use mk_chain helper * cryptarchia: rename fork choice test suite to TestForkChoice * cryptarchia: config.s is always 3k/f or 3*base_period_length * cryptarchia: hardcode epoch schedule in specification * un-hard code epoch sched. params + provide a v0.0.1 spec for params
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
from unittest import TestCase
|
|
from itertools import repeat
|
|
import numpy as np
|
|
import hashlib
|
|
|
|
from copy import deepcopy
|
|
from cryptarchia.cryptarchia import (
|
|
maxvalid_bg,
|
|
Chain,
|
|
BlockHeader,
|
|
Slot,
|
|
Id,
|
|
MockLeaderProof,
|
|
Coin,
|
|
)
|
|
|
|
from .test_common import mk_chain
|
|
|
|
|
|
class TestForkChoice(TestCase):
|
|
def test_fork_choice_long_sparse_chain(self):
|
|
# The longest chain is not dense after the fork
|
|
short_coin, long_coin = Coin(sk=0, value=100), Coin(sk=1, value=100)
|
|
common, long_coin = mk_chain(parent=bytes(32), coin=long_coin, slots=range(50))
|
|
|
|
long_chain_sparse_ext, long_coin = mk_chain(
|
|
parent=common[-1].id(), coin=long_coin, slots=range(50, 100, 2)
|
|
)
|
|
|
|
short_chain_dense_ext, _ = mk_chain(
|
|
parent=common[-1].id(), coin=short_coin, slots=range(50, 100)
|
|
)
|
|
|
|
# add more blocks to the long chain to ensure the long chain is indeed longer
|
|
long_chain_further_ext, _ = mk_chain(
|
|
parent=long_chain_sparse_ext[-1].id(), coin=long_coin, slots=range(100, 126)
|
|
)
|
|
|
|
long_chain = deepcopy(common) + long_chain_sparse_ext + long_chain_further_ext
|
|
short_chain = deepcopy(common) + short_chain_dense_ext
|
|
assert len(long_chain) > len(short_chain)
|
|
|
|
# by setting a low k we trigger the density choice rule
|
|
k = 1
|
|
s = 50
|
|
|
|
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
|
|
|
|
# However, if we set k to the fork length, it will be accepted
|
|
k = long_chain.length()
|
|
assert maxvalid_bg(short_chain, [long_chain], k, s) == long_chain
|
|
|
|
def test_fork_choice_long_dense_chain(self):
|
|
# The longest chain is also the densest after the fork
|
|
short_coin, long_coin = Coin(sk=0, value=100), Coin(sk=1, value=100)
|
|
common, long_coin = mk_chain(
|
|
parent=bytes(32), coin=long_coin, slots=range(1, 50)
|
|
)
|
|
|
|
long_chain_dense_ext, _ = mk_chain(
|
|
parent=common[-1].id(), coin=long_coin, slots=range(50, 100)
|
|
)
|
|
short_chain_sparse_ext, _ = mk_chain(
|
|
parent=common[-1].id(), coin=short_coin, slots=range(50, 100, 2)
|
|
)
|
|
|
|
long_chain = deepcopy(common) + long_chain_dense_ext
|
|
short_chain = deepcopy(common) + short_chain_sparse_ext
|
|
|
|
k = 1
|
|
s = 50
|
|
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
|