Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zeegomo committed Jan 31, 2024
1 parent 72c75ca commit 2776b39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
17 changes: 9 additions & 8 deletions cryptarchia/cryptarchia.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ class BlockHeader:
# The following code is to be considered as a reference implementation, mostly to be used for testing.
def id(self) -> Id:
# version byte
bytes = bytearray(b"\x01")
h = blake2b(digest_size=32)
h.update(b"\x01")
# header type
bytes += b"\x00"
h.update(b"\x00")
# content size
bytes += int.to_bytes(self.content_size, length=4, byteorder="big")
h.update(int.to_bytes(self.content_size, length=4, byteorder="big"))
# content id
assert len(self.content_id) == 32
bytes += self.content_id
h.update(self.content_id)
# slot
bytes += int.to_bytes(self.slot.absolute_slot, length=8, byteorder="big")
h.update(int.to_bytes(self.slot.absolute_slot, length=8, byteorder="big"))
# parent
assert len(self.content_id) == 32
bytes += self.parent
return blake2b(bytes, digest_size=32).digest()
assert len(self.parent) == 32
h.update(self.parent)
return h.digest()


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion cryptarchia/messages.abnf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ CONTENT = *OCTET
U32 = 4OCTET ; unsigned integer 32 bit (BE)
U64 = 8OCTET ; unsigned integer 32 bit (BE)
HEADER-ID = 32OCTET
CONTENT-ID = 32OCTET
CONTENT-ID = 32OCTET
36 changes: 17 additions & 19 deletions cryptarchia/test_fork_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
from cryptarchia.cryptarchia import maxvalid_bg, Chain, BlockHeader, Slot, Id


def make_block(parent_id: Id, slot: Slot, block_id: Id) -> BlockHeader:
def make_block(parent_id: Id, slot: Slot, content: bytes) -> BlockHeader:
assert len(parent_id) == 32
assert len(block_id) == 32
return BlockHeader(parent=parent_id, content_size=1, slot=slot, content_id=block_id)
content_id = hashlib.sha256(content).digest()[:32]
return BlockHeader(
parent=parent_id, content_size=1, slot=slot, content_id=content_id
)


class TestLeader(TestCase):
def test_fork_choice_long_sparse_chain(self):
# The longest chain is not dense after the fork
common = [
make_block(bytes(32), Slot(i), bytes(repeat(i, 32))) for i in range(1, 50)
]
common = [make_block(bytes(32), Slot(i), bytes(i)) for i in range(1, 50)]
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
long_id = hashlib.sha256(f"{slot}-long".encode()).digest()[:32]
short_id = hashlib.sha256(f"{slot}-short".encode()).digest()[:32]
long_content = f"{slot}-long".encode()
short_content = f"{slot}-short".encode()
if slot % 2 == 0:
long_chain.append(make_block(bytes(32), Slot(slot), long_id))
short_chain.append(make_block(bytes(32), Slot(slot), short_id))
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
short_chain.append(make_block(bytes(32), Slot(slot), short_content))
# add more blocks to the long chain
for slot in range(100, 200):
long_id = hashlib.sha256(f"{slot}-long".encode()).digest()[:32]
long_chain.append(make_block(bytes(32), Slot(slot), long_id))
long_content = f"{slot}-long".encode()
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
assert len(long_chain) > len(short_chain)
# by setting a low k we trigger the density choice rule
k = 1
Expand All @@ -49,18 +49,16 @@ def test_fork_choice_long_sparse_chain(self):

def test_fork_choice_long_dense_chain(self):
# The longest chain is also the densest after the fork
common = [
make_block(bytes(32), Slot(i), bytes(repeat(i, 32))) for i in range(1, 50)
]
common = [make_block(bytes(32), Slot(i), bytes(i)) for i in range(1, 50)]
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
long_id = hashlib.sha256(f"{slot}-long".encode()).digest()[:32]
short_id = hashlib.sha256(f"{slot}-short".encode()).digest()[:32]
long_chain.append(make_block(bytes(32), Slot(slot), long_id))
long_content = f"{slot}-long".encode()
short_content = f"{slot}-short".encode()
long_chain.append(make_block(bytes(32), Slot(slot), long_content))
if slot % 2 == 0:
short_chain.append(make_block(bytes(32), Slot(slot), short_id))
short_chain.append(make_block(bytes(32), Slot(slot), short_content))
k = 1
s = 50
assert maxvalid_bg(Chain(short_chain), [Chain(long_chain)], k, s) == Chain(
Expand Down

0 comments on commit 2776b39

Please sign in to comment.