diff --git a/cryptarchia/cryptarchia.py b/cryptarchia/cryptarchia.py index daaa95a..14d7c4e 100644 --- a/cryptarchia/cryptarchia.py +++ b/cryptarchia/cryptarchia.py @@ -25,6 +25,9 @@ class Hash(bytes): h.update(d) return super().__new__(cls, h.digest()) + def __deepcopy__(self, memo): + return self + @dataclass(frozen=True) class Epoch: @@ -138,10 +141,16 @@ class Slot: @dataclass -class Coin: - sk: int +class Note: value: int - nonce: bytes = bytes(32) + sk: int # TODO: rename to nf_sk + nonce: Hash = Hash(b"nonce") + unit: Hash = Hash(b"NMO") + state: Hash = Hash(b"state") + zone_id: Hash = Hash(b"ZoneID") + + def __post_init__(self): + assert 0 <= self.value <= 2**64 @property def pk(self) -> int: @@ -153,31 +162,24 @@ class Coin: def encode_pk(self) -> bytes: return int.to_bytes(self.pk, length=32, byteorder="big") - def evolve(self) -> "Coin": + def evolve(self) -> "Note": evolved_nonce = Hash(b"coin-evolve", self.encode_sk(), self.nonce) - return Coin(nonce=evolved_nonce, sk=self.sk, value=self.value) + return Note(nonce=evolved_nonce, sk=self.sk, value=self.value) - def commitment(self) -> Id: - # TODO: mocked until CL is understood + def commitment(self) -> Hash: value_bytes = int.to_bytes(self.value, length=32, byteorder="big") - - h = sha256() - h.update(b"coin-commitment") - h.update(self.nonce) - h.update(self.encode_pk()) - h.update(value_bytes) - return h.digest() + return Hash( + b"NOMOS_NOTE_CM", + self.state, + value_bytes, + self.unit, + self.nonce, + self.encode_pk(), + self.zone_id, + ) def nullifier(self) -> Id: - # TODO: mocked until CL is understood - value_bytes = int.to_bytes(self.value, length=32, byteorder="big") - - h = sha256() - h.update(b"coin-nullifier") - h.update(self.nonce) - h.update(self.encode_pk()) - h.update(value_bytes) - return h.digest() + return Hash(b"NOMOS_NOTE_NF", self.commitment(), self.encode_sk()) @dataclass @@ -189,13 +191,11 @@ class MockLeaderProof: parent: Id @staticmethod - def new(coin: Coin, slot: Slot, parent: Id): - evolved_coin = coin.evolve() - + def new(note: Note, slot: Slot, parent: Id): return MockLeaderProof( - commitment=coin.commitment(), - nullifier=coin.nullifier(), - evolved_commitment=evolved_coin.commitment(), + commitment=note.commitment(), + nullifier=note.nullifier(), + evolved_commitment=note.evolve().commitment(), slot=slot, parent=parent, ) @@ -281,7 +281,7 @@ class LedgerState: # set of commitments eligible to lead commitments_lead: set[Id] = field(default_factory=set) - # set of nullified coins + # set of nullified notes nullifiers: set[Id] = field(default_factory=set) # -- Stake Relativization State @@ -348,7 +348,7 @@ class EpochState: inferred_total_active_stake: int def verify_eligible_to_lead_due_to_age(self, commitment: Id) -> bool: - # A coin is eligible to lead if it was committed to before the the stake + # A note is eligible to lead if it was committed to before the the stake # distribution snapshot was taken or it was produced by a leader proof # since the snapshot was taken. # @@ -360,7 +360,7 @@ class EpochState: def total_active_stake(self) -> int: """ Returns the inferred total stake participating in consensus. - Total active stake is used to reletivize a coin's value in leadership proofs. + Total active stake is used to reletivize a note's value in leadership proofs. """ return self.inferred_total_active_stake @@ -432,7 +432,7 @@ class Follower: slot: Slot, parent: Id, proof: MockLeaderProof, - # coins are old enough if their commitment is in the stake distribution snapshot + # notes are old enough if their commitment is in the stake distribution snapshot epoch_state: EpochState, # nullifiers (and commitments) are checked against the current state. # For now, we assume proof parent state and current state are identical. @@ -642,13 +642,13 @@ class MOCK_LEADER_VRF: ORDER = 2**256 @classmethod - def vrf(cls, coin: Coin, epoch_nonce: bytes, slot: Slot) -> int: + def vrf(cls, note: Note, epoch_nonce: bytes, slot: Slot) -> int: ticket = Hash( b"LEAD", epoch_nonce, slot.encode(), - coin.commitment(), - coin.encode_sk(), + note.commitment(), + note.encode_sk(), ) return int.from_bytes(ticket) @@ -660,18 +660,18 @@ class MOCK_LEADER_VRF: @dataclass class Leader: config: Config - coin: Coin + note: Note def try_prove_slot_leader( self, epoch: EpochState, slot: Slot, parent: Id ) -> MockLeaderProof | None: if self._is_slot_leader(epoch, slot): - return MockLeaderProof.new(self.coin, slot, parent) + return MockLeaderProof.new(self.note, slot, parent) def _is_slot_leader(self, epoch: EpochState, slot: Slot): - relative_stake = self.coin.value / epoch.total_active_stake() + relative_stake = self.note.value / epoch.total_active_stake() - r = MOCK_LEADER_VRF.vrf(self.coin, epoch.nonce(), slot) + r = MOCK_LEADER_VRF.vrf(self.note, epoch.nonce(), slot) return r < MOCK_LEADER_VRF.ORDER * phi( self.config.active_slot_coeff, relative_stake diff --git a/cryptarchia/test_common.py b/cryptarchia/test_common.py index cd35090..1d63e0f 100644 --- a/cryptarchia/test_common.py +++ b/cryptarchia/test_common.py @@ -1,7 +1,7 @@ from .cryptarchia import ( Config, Slot, - Coin, + Note, BlockHeader, LedgerState, MockLeaderProof, @@ -11,9 +11,9 @@ from .cryptarchia import ( class TestNode: - def __init__(self, config: Config, genesis: LedgerState, coin: Coin): + def __init__(self, config: Config, genesis: LedgerState, note: Note): self.config = config - self.leader = Leader(coin=coin, config=config) + self.leader = Leader(note=note, config=config) self.follower = Follower(genesis, config) def epoch_state(self, slot: Slot): @@ -25,7 +25,7 @@ class TestNode: parent = self.follower.tip_id() epoch_state = self.epoch_state(slot) if leader_proof := self.leader.try_prove_slot_leader(epoch_state, slot, parent): - self.leader.coin = self.leader.coin.evolve() + self.leader.note = self.leader.note.evolve() return BlockHeader( parent=parent, slot=slot, @@ -40,15 +40,15 @@ class TestNode: self.follower.on_block(block) -def mk_config(initial_stake_distribution: list[Coin]) -> Config: - initial_inferred_total_stake = sum(c.value for c in initial_stake_distribution) +def mk_config(initial_stake_distribution: list[Note]) -> Config: + initial_inferred_total_stake = sum(n.value for n in initial_stake_distribution) return Config.cryptarchia_v0_0_1(initial_inferred_total_stake).replace( k=1, active_slot_coeff=0.5, ) -def mk_genesis_state(initial_stake_distribution: list[Coin]) -> LedgerState: +def mk_genesis_state(initial_stake_distribution: list[Note]) -> LedgerState: return LedgerState( block=BlockHeader( slot=Slot(0), @@ -56,18 +56,18 @@ def mk_genesis_state(initial_stake_distribution: list[Coin]) -> LedgerState: content_size=0, content_id=bytes(32), leader_proof=MockLeaderProof.new( - Coin(sk=0, value=0), Slot(0), parent=bytes(32) + Note(sk=0, value=0), Slot(0), parent=bytes(32) ), ), nonce=bytes(32), - commitments_spend={c.commitment() for c in initial_stake_distribution}, - commitments_lead={c.commitment() for c in initial_stake_distribution}, + commitments_spend={n.commitment() for n in initial_stake_distribution}, + commitments_lead={n.commitment() for n in initial_stake_distribution}, nullifiers=set(), ) def mk_block( - parent: BlockHeader, slot: int, coin: Coin, content=bytes(32), orphaned_proofs=[] + parent: BlockHeader, slot: int, note: Note, content=bytes(32), orphaned_proofs=[] ) -> BlockHeader: assert type(parent) == BlockHeader, type(parent) assert type(slot) == int, type(slot) @@ -78,19 +78,19 @@ def mk_block( parent=parent.id(), content_size=len(content), content_id=sha256(content).digest(), - leader_proof=MockLeaderProof.new(coin, Slot(slot), parent=parent.id()), + leader_proof=MockLeaderProof.new(note, Slot(slot), parent=parent.id()), orphaned_proofs=orphaned_proofs, ) def mk_chain( - parent: BlockHeader, coin: Coin, slots: list[int] -) -> tuple[list[BlockHeader], Coin]: + parent: BlockHeader, note: Note, slots: list[int] +) -> tuple[list[BlockHeader], Note]: assert type(parent) == BlockHeader chain = [] for s in slots: - block = mk_block(parent=parent, slot=s, coin=coin) + block = mk_block(parent=parent, slot=s, note=note) chain.append(block) parent = block - coin = coin.evolve() - return chain, coin + note = note.evolve() + return chain, note diff --git a/cryptarchia/test_fork_choice.py b/cryptarchia/test_fork_choice.py index 24b295d..d0c95d0 100644 --- a/cryptarchia/test_fork_choice.py +++ b/cryptarchia/test_fork_choice.py @@ -4,7 +4,7 @@ from copy import deepcopy from cryptarchia.cryptarchia import ( maxvalid_bg, Slot, - Coin, + Note, Follower, common_prefix_depth, LedgerState, @@ -21,16 +21,16 @@ class TestForkChoice(TestCase): # \ # 4 - 5 - coin = Coin(sk=1, value=100) + note = Note(sk=1, value=100) b0 = mk_genesis_state([]).block - b1 = mk_block(b0, 1, coin) - b2 = mk_block(b1, 2, coin) - b3 = mk_block(b2, 3, coin) - b4 = mk_block(b0, 1, coin, content=b"b4") - b5 = mk_block(b4, 2, coin) - b6 = mk_block(b2, 3, coin, content=b"b6") - b7 = mk_block(b6, 4, coin) + b1 = mk_block(b0, 1, note) + b2 = mk_block(b1, 2, note) + b3 = mk_block(b2, 3, note) + b4 = mk_block(b0, 1, note, content=b"b4") + b5 = mk_block(b4, 2, note) + b6 = mk_block(b2, 3, note, content=b"b6") + b7 = mk_block(b6, 4, note) states = { b.id(): LedgerState(block=b) for b in [b0, b1, b2, b3, b4, b5, b6, b7] @@ -173,20 +173,20 @@ class TestForkChoice(TestCase): # The longest chain is not dense after the fork genesis = mk_genesis_state([]).block - short_coin, long_coin = Coin(sk=0, value=100), Coin(sk=1, value=100) - common, long_coin = mk_chain(parent=genesis, coin=long_coin, slots=range(50)) + short_note, long_note = Note(sk=0, value=100), Note(sk=1, value=100) + common, long_note = mk_chain(parent=genesis, note=long_note, slots=range(50)) - long_chain_sparse_ext, long_coin = mk_chain( - parent=common[-1], coin=long_coin, slots=range(50, 100, 2) + long_chain_sparse_ext, long_note = mk_chain( + parent=common[-1], note=long_note, slots=range(50, 100, 2) ) short_chain_dense_ext, _ = mk_chain( - parent=common[-1], coin=short_coin, slots=range(50, 100) + parent=common[-1], note=short_note, 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], coin=long_coin, slots=range(100, 126) + parent=long_chain_sparse_ext[-1], note=long_note, slots=range(100, 126) ) long_chain = deepcopy(common) + long_chain_sparse_ext + long_chain_further_ext @@ -213,18 +213,18 @@ class TestForkChoice(TestCase): 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( + short_note, long_note = Note(sk=0, value=100), Note(sk=1, value=100) + common, long_note = mk_chain( parent=mk_genesis_state([]).block, - coin=long_coin, + note=long_note, slots=range(1, 50), ) long_chain_dense_ext, _ = mk_chain( - parent=common[-1], coin=long_coin, slots=range(50, 100) + parent=common[-1], note=long_note, slots=range(50, 100) ) short_chain_sparse_ext, _ = mk_chain( - parent=common[-1], coin=short_coin, slots=range(50, 100, 2) + parent=common[-1], note=short_note, slots=range(50, 100, 2) ) long_chain = deepcopy(common) + long_chain_dense_ext @@ -240,13 +240,13 @@ class TestForkChoice(TestCase): ) def test_fork_choice_integration(self): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - coins = [c_a, c_b] - config = mk_config(coins) - genesis = mk_genesis_state(coins) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + notes = [n_a, n_b] + config = mk_config(notes) + genesis = mk_genesis_state(notes) follower = Follower(genesis, config) - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() follower.on_block(b1) @@ -262,8 +262,8 @@ class TestForkChoice(TestCase): # b3 # - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_b = mk_block(b1, 2, c_b), c_b.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_b = mk_block(b1, 2, n_b), n_b.evolve() follower.on_block(b2) follower.on_block(b3) @@ -280,7 +280,7 @@ class TestForkChoice(TestCase): # b3 - b4 == tip # - b4, c_b = mk_block(b3, 3, c_b), c_a.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_a.evolve() follower.on_block(b4) assert follower.tip_id() == b4.id() diff --git a/cryptarchia/test_leader.py b/cryptarchia/test_leader.py index 08d662a..e2c3dd5 100644 --- a/cryptarchia/test_leader.py +++ b/cryptarchia/test_leader.py @@ -2,7 +2,7 @@ from unittest import TestCase import numpy as np -from .cryptarchia import Leader, EpochState, LedgerState, Coin, phi, Slot +from .cryptarchia import Leader, EpochState, LedgerState, Note, phi, Slot from .test_common import mk_config @@ -14,11 +14,11 @@ class TestLeader(TestCase): inferred_total_active_stake=1000, ) - coin = Coin(sk=0, value=10) + note = Note(sk=0, value=10) f = 0.05 l = Leader( - config=mk_config([coin]).replace(active_slot_coeff=f), - coin=coin, + config=mk_config([note]).replace(active_slot_coeff=f), + note=note, ) # We'll use the Margin of Error equation to decide how many samples we need. diff --git a/cryptarchia/test_ledger_state_update.py b/cryptarchia/test_ledger_state_update.py index d66abf0..79a8ef9 100644 --- a/cryptarchia/test_ledger_state_update.py +++ b/cryptarchia/test_ledger_state_update.py @@ -1,7 +1,7 @@ from unittest import TestCase from .cryptarchia import ( - Coin, + Note, Follower, InvalidLeaderProof, MissingOrphanProof, @@ -13,12 +13,12 @@ from .test_common import mk_block, mk_config, mk_genesis_state class TestLedgerStateUpdate(TestCase): def test_on_block_idempotent(self): - leader_coin = Coin(sk=0, value=100) - genesis = mk_genesis_state([leader_coin]) + leader_note = Note(sk=0, value=100) + genesis = mk_genesis_state([leader_note]) - follower = Follower(genesis, mk_config([leader_coin])) + follower = Follower(genesis, mk_config([leader_note])) - block = mk_block(slot=0, parent=genesis.block, coin=leader_coin) + block = mk_block(slot=0, parent=genesis.block, note=leader_note) follower.on_block(block) # Follower should have accepted the block @@ -33,47 +33,47 @@ class TestLedgerStateUpdate(TestCase): assert len(follower.ledger_state) == 2 assert len(follower.forks) == 0 - def test_ledger_state_prevents_coin_reuse(self): - leader_coin = Coin(sk=0, value=100) - genesis = mk_genesis_state([leader_coin]) + def test_ledger_state_prevents_note_reuse(self): + leader_note = Note(sk=0, value=100) + genesis = mk_genesis_state([leader_note]) - follower = Follower(genesis, mk_config([leader_coin])) + follower = Follower(genesis, mk_config([leader_note])) - block = mk_block(slot=0, parent=genesis.block, coin=leader_coin) + block = mk_block(slot=0, parent=genesis.block, note=leader_note) follower.on_block(block) # Follower should have accepted the block assert len(list(iter_chain(follower.tip_id(), follower.ledger_state))) == 2 assert follower.tip() == block - # Follower should have updated their ledger state to mark the leader coin as spent - assert follower.tip_state().verify_unspent(leader_coin.nullifier()) == False + # Follower should have updated their ledger state to mark the leader note as spent + assert follower.tip_state().verify_unspent(leader_note.nullifier()) == False - reuse_coin_block = mk_block(slot=1, parent=block, coin=leader_coin) + reuse_note_block = mk_block(slot=1, parent=block, note=leader_note) with self.assertRaises(InvalidLeaderProof): - follower.on_block(reuse_coin_block) + follower.on_block(reuse_note_block) # Follower should *not* have accepted the block assert len(list(iter_chain(follower.tip_id(), follower.ledger_state))) == 2 assert follower.tip() == block def test_ledger_state_is_properly_updated_on_reorg(self): - coin = [Coin(sk=0, value=100), Coin(sk=1, value=100), Coin(sk=2, value=100)] + note = [Note(sk=0, value=100), Note(sk=1, value=100), Note(sk=2, value=100)] - genesis = mk_genesis_state(coin) + genesis = mk_genesis_state(note) - follower = Follower(genesis, mk_config(coin)) + follower = Follower(genesis, mk_config(note)) - # 1) coin[0] & coin[1] both concurrently win slot 0 + # 1) note[0] & note[1] both concurrently win slot 0 - block_1 = mk_block(parent=genesis.block, slot=0, coin=coin[0]) - block_2 = mk_block(parent=genesis.block, slot=0, coin=coin[1]) + block_1 = mk_block(parent=genesis.block, slot=0, note=note[0]) + block_2 = mk_block(parent=genesis.block, slot=0, note=note[1]) # 2) follower sees block 1 first follower.on_block(block_1) assert follower.tip() == block_1 - assert not follower.tip_state().verify_unspent(coin[0].nullifier()) + assert not follower.tip_state().verify_unspent(note[0].nullifier()) # 3) then sees block 2, but sticks with block_1 as the tip @@ -81,46 +81,46 @@ class TestLedgerStateUpdate(TestCase): assert follower.tip() == block_1 assert len(follower.forks) == 1, f"{len(follower.forks)}" - # 4) then coin[2] wins slot 1 and chooses to extend from block_2 + # 4) then note[2] wins slot 1 and chooses to extend from block_2 - block_3 = mk_block(parent=block_2, slot=1, coin=coin[2]) + block_3 = mk_block(parent=block_2, slot=1, note=note[2]) follower.on_block(block_3) # the follower should have switched over to the block_2 fork assert follower.tip() == block_3 - # and the original coin[0] should now be removed from the spent pool - assert follower.tip_state().verify_unspent(coin[0].nullifier()) + # and the original note[0] should now be removed from the spent pool + assert follower.tip_state().verify_unspent(note[0].nullifier()) def test_fork_creation(self): - coins = [Coin(sk=i, value=100) for i in range(7)] - genesis = mk_genesis_state(coins) + notes = [Note(sk=i, value=100) for i in range(7)] + genesis = mk_genesis_state(notes) - follower = Follower(genesis, mk_config(coins)) + follower = Follower(genesis, mk_config(notes)) - # coin_0 & coin_1 both concurrently win slot 0 based on the genesis block + # note_0 & note_1 both concurrently win slot 0 based on the genesis block # Both blocks are accepted, and a fork is created "from the genesis block" - block_1 = mk_block(parent=genesis.block, slot=0, coin=coins[0]) - block_2 = mk_block(parent=genesis.block, slot=0, coin=coins[1]) + block_1 = mk_block(parent=genesis.block, slot=0, note=notes[0]) + block_2 = mk_block(parent=genesis.block, slot=0, note=notes[1]) follower.on_block(block_1) follower.on_block(block_2) assert follower.tip() == block_1 assert len(follower.forks) == 1, f"{len(follower.forks)}" assert follower.forks[0] == block_2.id() - # coin_2 wins slot 1 and chooses to extend from block_1 - # coin_3 also wins slot 1 and but chooses to extend from block_2 + # note_2 wins slot 1 and chooses to extend from block_1 + # note_3 also wins slot 1 and but chooses to extend from block_2 # Both blocks are accepted. Both the local chain and the fork grow. No fork is newly created. - block_3 = mk_block(parent=block_1, slot=1, coin=coins[2]) - block_4 = mk_block(parent=block_2, slot=1, coin=coins[3]) + block_3 = mk_block(parent=block_1, slot=1, note=notes[2]) + block_4 = mk_block(parent=block_2, slot=1, note=notes[3]) follower.on_block(block_3) follower.on_block(block_4) assert follower.tip() == block_3 assert len(follower.forks) == 1, f"{len(follower.forks)}" assert follower.forks[0] == block_4.id() - # coin_4 wins slot 1 and but chooses to extend from block_2 as well + # note_4 wins slot 1 and but chooses to extend from block_2 as well # The block is accepted. A new fork is created "from the block_2". - block_5 = mk_block(parent=block_2, slot=1, coin=coins[4]) + block_5 = mk_block(parent=block_2, slot=1, note=notes[4]) follower.on_block(block_5) assert follower.tip() == block_3 assert len(follower.forks) == 2, f"{len(follower.forks)}" @@ -129,8 +129,8 @@ class TestLedgerStateUpdate(TestCase): # A block based on an unknown parent is not accepted. # Nothing changes from the local chain and forks. - unknown_block = mk_block(parent=block_5, slot=2, coin=coins[5]) - block_6 = mk_block(parent=unknown_block, slot=2, coin=coins[6]) + unknown_block = mk_block(parent=block_5, slot=2, note=notes[5]) + block_6 = mk_block(parent=unknown_block, slot=2, note=notes[6]) with self.assertRaises(ParentNotFound): follower.on_block(block_6) assert follower.tip() == block_3 @@ -139,9 +139,9 @@ class TestLedgerStateUpdate(TestCase): assert follower.forks[1] == block_5.id() def test_epoch_transition(self): - leader_coins = [Coin(sk=i, value=100) for i in range(4)] - genesis = mk_genesis_state(leader_coins) - config = mk_config(leader_coins) + leader_notes = [Note(sk=i, value=100) for i in range(4)] + genesis = mk_genesis_state(leader_notes) + config = mk_config(leader_notes) follower = Follower(genesis, config) @@ -150,19 +150,19 @@ class TestLedgerStateUpdate(TestCase): # ---- EPOCH 0 ---- - block_1 = mk_block(slot=0, parent=genesis.block, coin=leader_coins[0]) + block_1 = mk_block(slot=0, parent=genesis.block, note=leader_notes[0]) follower.on_block(block_1) assert follower.tip() == block_1 assert follower.tip().slot.epoch(config).epoch == 0 - block_2 = mk_block(slot=19, parent=block_1, coin=leader_coins[1]) + block_2 = mk_block(slot=19, parent=block_1, note=leader_notes[1]) follower.on_block(block_2) assert follower.tip() == block_2 assert follower.tip().slot.epoch(config).epoch == 0 # ---- EPOCH 1 ---- - block_3 = mk_block(slot=20, parent=block_2, coin=leader_coins[2]) + block_3 = mk_block(slot=20, parent=block_2, note=leader_notes[2]) follower.on_block(block_3) assert follower.tip() == block_3 assert follower.tip().slot.epoch(config).epoch == 1 @@ -171,48 +171,48 @@ class TestLedgerStateUpdate(TestCase): # when trying to propose a block for epoch 2, the stake distribution snapshot should be taken # at the end of epoch 0, i.e. slot 9 - # To ensure this is the case, we add a new coin just to the state associated with that slot, + # To ensure this is the case, we add a new note just to the state associated with that slot, # so that the new block can be accepted only if that is the snapshot used # first, verify that if we don't change the state, the block is not accepted - block_4 = mk_block(slot=40, parent=block_3, coin=Coin(sk=4, value=100)) + block_4 = mk_block(slot=40, parent=block_3, note=Note(sk=4, value=100)) with self.assertRaises(InvalidLeaderProof): follower.on_block(block_4) assert follower.tip() == block_3 - # then we add the coin to "spendable commitments" associated with slot 9 + # then we add the note to "spendable commitments" associated with slot 9 follower.ledger_state[block_2.id()].commitments_spend.add( - Coin(sk=4, value=100).commitment() + Note(sk=4, value=100).commitment() ) follower.on_block(block_4) assert follower.tip() == block_4 assert follower.tip().slot.epoch(config).epoch == 2 - def test_evolved_coin_is_eligible_for_leadership(self): - coin = Coin(sk=0, value=100) + def test_evolved_note_is_eligible_for_leadership(self): + note = Note(sk=0, value=100) - genesis = mk_genesis_state([coin]) + genesis = mk_genesis_state([note]) - follower = Follower(genesis, mk_config([coin])) + follower = Follower(genesis, mk_config([note])) - # coin wins the first slot - block_1 = mk_block(slot=0, parent=genesis.block, coin=coin) + # note wins the first slot + block_1 = mk_block(slot=0, parent=genesis.block, note=note) follower.on_block(block_1) assert follower.tip() == block_1 - # coin can't be reused to win following slots: - block_2_reuse = mk_block(slot=1, parent=block_1, coin=coin) + # note can't be reused to win following slots: + block_2_reuse = mk_block(slot=1, parent=block_1, note=note) with self.assertRaises(InvalidLeaderProof): follower.on_block(block_2_reuse) assert follower.tip() == block_1 - # but the evolved coin is eligible - block_2_evolve = mk_block(slot=1, parent=block_1, coin=coin.evolve()) + # but the evolved note is eligible + block_2_evolve = mk_block(slot=1, parent=block_1, note=note.evolve()) follower.on_block(block_2_evolve) assert follower.tip() == block_2_evolve - def test_new_coins_becoming_eligible_after_stake_distribution_stabilizes(self): - coin = Coin(sk=0, value=100) - config = mk_config([coin]) - genesis = mk_genesis_state([coin]) + def test_new_notes_becoming_eligible_after_stake_distribution_stabilizes(self): + note = Note(sk=0, value=100) + config = mk_config([note]) + genesis = mk_genesis_state([note]) follower = Follower(genesis, config) # We assume an epoch length of 20 slots in this test. @@ -220,78 +220,78 @@ class TestLedgerStateUpdate(TestCase): # ---- EPOCH 0 ---- - block_0_0 = mk_block(slot=0, parent=genesis.block, coin=coin) + block_0_0 = mk_block(slot=0, parent=genesis.block, note=note) follower.on_block(block_0_0) assert follower.tip() == block_0_0 - # mint a new coin to be used for leader elections in upcoming epochs - coin_new = Coin(sk=1, value=10) + # mint a new note to be used for leader elections in upcoming epochs + note_new = Note(sk=1, value=10) follower.ledger_state[block_0_0.id()].commitments_spend.add( - coin_new.commitment() + note_new.commitment() ) - # the new coin is not yet eligible for elections - block_0_1_attempt = mk_block(slot=1, parent=block_0_0, coin=coin_new) + # the new note is not yet eligible for elections + block_0_1_attempt = mk_block(slot=1, parent=block_0_0, note=note_new) with self.assertRaises(InvalidLeaderProof): follower.on_block(block_0_1_attempt) assert follower.tip() == block_0_0 - # whereas the evolved coin from genesis can be spent immediately - block_0_1 = mk_block(slot=1, parent=block_0_0, coin=coin.evolve()) + # whereas the evolved note from genesis can be spent immediately + block_0_1 = mk_block(slot=1, parent=block_0_0, note=note.evolve()) follower.on_block(block_0_1) assert follower.tip() == block_0_1 # ---- EPOCH 1 ---- - # The newly minted coin is still not eligible in the following epoch since the + # The newly minted note is still not eligible in the following epoch since the # stake distribution snapshot is taken at the beginning of the previous epoch - block_1_0 = mk_block(slot=20, parent=block_0_1, coin=coin_new) + block_1_0 = mk_block(slot=20, parent=block_0_1, note=note_new) with self.assertRaises(InvalidLeaderProof): follower.on_block(block_1_0) assert follower.tip() == block_0_1 # ---- EPOCH 2 ---- - # The coin is finally eligible 2 epochs after it was first minted + # The note is finally eligible 2 epochs after it was first minted - block_2_0 = mk_block(slot=40, parent=block_0_1, coin=coin_new) + block_2_0 = mk_block(slot=40, parent=block_0_1, note=note_new) follower.on_block(block_2_0) assert follower.tip() == block_2_0 - # And now the minted coin can freely use the evolved coin for subsequent blocks - block_2_1 = mk_block(slot=40, parent=block_2_0, coin=coin_new.evolve()) + # And now the minted note can freely use the evolved note for subsequent blocks + block_2_1 = mk_block(slot=40, parent=block_2_0, note=note_new.evolve()) follower.on_block(block_2_1) assert follower.tip() == block_2_1 def test_orphaned_proofs(self): - coin, coin_orphan = Coin(sk=0, value=100), Coin(sk=1, value=100) - genesis = mk_genesis_state([coin, coin_orphan]) + note, note_orphan = Note(sk=0, value=100), Note(sk=1, value=100) + genesis = mk_genesis_state([note, note_orphan]) - follower = Follower(genesis, mk_config([coin, coin_orphan])) + follower = Follower(genesis, mk_config([note, note_orphan])) - block_0_0 = mk_block(slot=0, parent=genesis.block, coin=coin) + block_0_0 = mk_block(slot=0, parent=genesis.block, note=note) follower.on_block(block_0_0) assert follower.tip() == block_0_0 - coin_new = coin.evolve() - coin_new_new = coin_new.evolve() - block_0_1 = mk_block(slot=1, parent=block_0_0, coin=coin_new_new) + note_new = note.evolve() + note_new_new = note_new.evolve() + block_0_1 = mk_block(slot=1, parent=block_0_0, note=note_new_new) with self.assertRaises(InvalidLeaderProof): follower.on_block(block_0_1) - # the coin evolved twice should not be accepted as it is not in the lead commitments + # the note evolved twice should not be accepted as it is not in the lead commitments assert follower.tip() == block_0_0 # An orphaned proof will not be accepted until a node first sees the corresponding block. # - # Also, notice that the block is using the evolved orphan coin which is not present on the main + # Also, notice that the block is using the evolved orphan note which is not present on the main # branch. The evolved orphan commitment is added from the orphan prior to validating the block # header as part of orphan importing process - orphan = mk_block(parent=genesis.block, slot=0, coin=coin_orphan) + orphan = mk_block(parent=genesis.block, slot=0, note=note_orphan) block_0_1 = mk_block( slot=1, parent=block_0_0, - coin=coin_orphan.evolve(), + note=note_orphan.evolve(), orphaned_proofs=[orphan], ) with self.assertRaises(MissingOrphanProof): diff --git a/cryptarchia/test_orphaned_proofs.py b/cryptarchia/test_orphaned_proofs.py index 0114464..00c2a20 100644 --- a/cryptarchia/test_orphaned_proofs.py +++ b/cryptarchia/test_orphaned_proofs.py @@ -1,14 +1,14 @@ from unittest import TestCase -from cryptarchia.cryptarchia import Coin, Follower +from cryptarchia.cryptarchia import Note, Follower from .test_common import mk_config, mk_genesis_state, mk_block class TestOrphanedProofs(TestCase): def test_simple_orphan_import(self): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - coins = [c_a, c_b] + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + coins = [n_a, n_b] config = mk_config(coins) genesis = mk_genesis_state(coins) follower = Follower(genesis, config) @@ -22,9 +22,9 @@ class TestOrphanedProofs(TestCase): # b3 # - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_b = mk_block(b1, 2, c_b), c_b.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_b = mk_block(b1, 2, n_b), n_b.evolve() for b in [b1, b2, b3]: follower.on_block(b) @@ -41,7 +41,7 @@ class TestOrphanedProofs(TestCase): # \ / # b3 # - b4, c_a = mk_block(b2, 3, c_a, orphaned_proofs=[b3]), c_a.evolve() + b4, n_a = mk_block(b2, 3, n_a, orphaned_proofs=[b3]), n_a.evolve() follower.on_block(b4) assert follower.tip() == b4 @@ -49,8 +49,8 @@ class TestOrphanedProofs(TestCase): assert follower.unimported_orphans() == [] def test_orphan_proof_import_from_long_running_fork(self): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - coins = [c_a, c_b] + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + coins = [n_a, n_b] config = mk_config(coins) genesis = mk_genesis_state(coins) follower = Follower(genesis, config) @@ -64,13 +64,13 @@ class TestOrphanedProofs(TestCase): # b4 - b5 # - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_a = mk_block(b2, 3, c_a), c_a.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_a = mk_block(b2, 3, n_a), n_a.evolve() - b4, c_b = mk_block(b1, 2, c_b), c_b.evolve() - b5, c_b = mk_block(b4, 3, c_b), c_b.evolve() + b4, n_b = mk_block(b1, 2, n_b), n_b.evolve() + b5, n_b = mk_block(b4, 3, n_b), n_b.evolve() for b in [b1, b2, b3, b4, b5]: follower.on_block(b) @@ -87,15 +87,15 @@ class TestOrphanedProofs(TestCase): # \ / / # b4 - b5 - b6, c_a = mk_block(b3, 4, c_a, orphaned_proofs=[b4, b5]), c_a.evolve() + b6, n_a = mk_block(b3, 4, n_a, orphaned_proofs=[b4, b5]), n_a.evolve() follower.on_block(b6) assert follower.tip() == b6 assert [f for f in follower.forks] == [b5.id()] def test_orphan_proof_import_from_fork_without_direct_shared_parent(self): - coins = [Coin(sk=i, value=10) for i in range(2)] - c_a, c_b = coins + coins = [Note(sk=i, value=10) for i in range(2)] + n_a, n_b = coins config = mk_config(coins) genesis = mk_genesis_state(coins) follower = Follower(genesis, config) @@ -108,15 +108,15 @@ class TestOrphanedProofs(TestCase): # \ # b5 - b6 - b7 - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_a = mk_block(b2, 3, c_a), c_a.evolve() - b4, c_a = mk_block(b3, 4, c_a), c_a.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_a = mk_block(b2, 3, n_a), n_a.evolve() + b4, n_a = mk_block(b3, 4, n_a), n_a.evolve() - b5, c_b = mk_block(b1, 2, c_b), c_b.evolve() - b6, c_b = mk_block(b5, 3, c_b), c_b.evolve() - b7, c_b = mk_block(b6, 4, c_b), c_b.evolve() + b5, n_b = mk_block(b1, 2, n_b), n_b.evolve() + b6, n_b = mk_block(b5, 3, n_b), n_b.evolve() + b7, n_b = mk_block(b6, 4, n_b), n_b.evolve() for b in [b1, b2, b3, b4, b5, b6, b7]: follower.on_block(b) @@ -136,7 +136,7 @@ class TestOrphanedProofs(TestCase): # Earlier implementations of orphan proof validation failed to # validate b7 as an orphan here. - b8, c_a = mk_block(b4, 5, c_a, orphaned_proofs=[b5, b6, b7]), c_a.evolve() + b8, n_a = mk_block(b4, 5, n_a, orphaned_proofs=[b5, b6, b7]), n_a.evolve() follower.on_block(b8) assert follower.tip() == b8 @@ -166,21 +166,21 @@ class TestOrphanedProofs(TestCase): # -- b6 # - coins = [Coin(sk=i, value=10) for i in range(3)] - c_a, c_b, c_c = coins + coins = [Note(sk=i, value=10) for i in range(3)] + n_a, n_b, n_c = coins config = mk_config(coins) genesis = mk_genesis_state(coins) follower = Follower(genesis, config) - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_a = mk_block(b2, 3, c_a), c_a.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_a = mk_block(b2, 3, n_a), n_a.evolve() - b4, c_b = mk_block(b1, 2, c_b), c_b.evolve() - b5, c_b = mk_block(b4, 3, c_b), c_b.evolve() + b4, n_b = mk_block(b1, 2, n_b), n_b.evolve() + b5, n_b = mk_block(b4, 3, n_b), n_b.evolve() - b6, c_c = mk_block(b4, 3, c_c), c_c.evolve() + b6, n_c = mk_block(b4, 3, n_c), n_c.evolve() for b in [b1, b2, b3, b4, b5, b6]: follower.on_block(b) @@ -189,7 +189,7 @@ class TestOrphanedProofs(TestCase): assert [f for f in follower.forks] == [b5.id(), b6.id()] assert follower.unimported_orphans() == [b4, b5, b6] - b7, c_a = mk_block(b3, 4, c_a, orphaned_proofs=[b4, b5, b6]), c_a.evolve() + b7, n_a = mk_block(b3, 4, n_a, orphaned_proofs=[b4, b5, b6]), n_a.evolve() follower.on_block(b7) assert follower.tip() == b7 @@ -215,23 +215,23 @@ class TestOrphanedProofs(TestCase): # \ / \_ / # -b5(a)-----\-b7(a, o=b5) - coins = [Coin(sk=i, value=10) for i in range(2)] - c_a, c_b = coins + coins = [Note(sk=i, value=10) for i in range(2)] + n_a, n_b = coins config = mk_config(coins) genesis = mk_genesis_state(coins) follower = Follower(genesis, config) - b1, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 2, c_a), c_a.evolve() - b3, c_a = mk_block(b2, 3, c_a), c_a.evolve() + b1, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 2, n_a), n_a.evolve() + b3, n_a = mk_block(b2, 3, n_a), n_a.evolve() - b4, c_b = mk_block(b3, 4, c_b), c_b.evolve() - b5, c_a = mk_block(b3, 4, c_a), c_a.evolve() + b4, n_b = mk_block(b3, 4, n_b), n_b.evolve() + b5, n_a = mk_block(b3, 4, n_a), n_a.evolve() - b6, c_b = mk_block(b4, 5, c_b, orphaned_proofs=[b5]), c_b.evolve() - b7, c_a = mk_block(b4, 5, c_a, orphaned_proofs=[b5]), c_a.evolve() + b6, n_b = mk_block(b4, 5, n_b, orphaned_proofs=[b5]), n_b.evolve() + b7, n_a = mk_block(b4, 5, n_a, orphaned_proofs=[b5]), n_a.evolve() - b8, c_b = mk_block(b6, 6, c_b, orphaned_proofs=[b7]), c_b.evolve() + b8, n_b = mk_block(b6, 6, n_b, orphaned_proofs=[b7]), n_b.evolve() for b in [b1, b2, b3, b4, b5]: follower.on_block(b) diff --git a/cryptarchia/test_stake_relativization.py b/cryptarchia/test_stake_relativization.py index cfa9473..3a58f76 100644 --- a/cryptarchia/test_stake_relativization.py +++ b/cryptarchia/test_stake_relativization.py @@ -3,17 +3,17 @@ import itertools import numpy as np -from .cryptarchia import Config, Coin, Slot +from .cryptarchia import Config, Note, Slot from .test_common import mk_config, mk_genesis_state, mk_block, TestNode, Follower class TestStakeRelativization(TestCase): def test_ledger_leader_counting(self): - coins = [Coin(sk=i, value=10) for i in range(2)] - c_a, c_b = coins + notes = [Note(sk=i, value=10) for i in range(2)] + n_a, n_b = notes - config = mk_config(coins) - genesis = mk_genesis_state(coins) + config = mk_config(notes) + genesis = mk_genesis_state(notes) follower = Follower(genesis, config) @@ -21,31 +21,31 @@ class TestStakeRelativization(TestCase): assert follower.tip_state().leader_count == 0 # after a block, 1 leader has been observed - b1 = mk_block(genesis.block, slot=1, coin=c_a) + b1 = mk_block(genesis.block, slot=1, note=n_a) follower.on_block(b1) assert follower.tip_state().leader_count == 1 # on fork, tip state is not updated - orphan = mk_block(genesis.block, slot=1, coin=c_b) + orphan = mk_block(genesis.block, slot=1, note=n_b) follower.on_block(orphan) assert follower.tip_state().block == b1 assert follower.tip_state().leader_count == 1 # after orphan is adopted, leader count should jumpy by 2 (each orphan counts as a leader) - b2 = mk_block(b1, slot=2, coin=c_a.evolve(), orphaned_proofs=[orphan]) + b2 = mk_block(b1, slot=2, note=n_a.evolve(), orphaned_proofs=[orphan]) follower.on_block(b2) assert follower.tip_state().block == b2 assert follower.tip_state().leader_count == 3 def test_inference_on_empty_genesis_epoch(self): - coin = Coin(sk=0, value=10) - config = mk_config([coin]).replace( + note = Note(sk=0, value=10) + config = mk_config([note]).replace( initial_total_active_stake=20, total_active_stake_learning_rate=0.5, active_slot_coeff=0.5, ) - genesis = mk_genesis_state([coin]) - node = TestNode(config, genesis, coin) + genesis = mk_genesis_state([note]) + node = TestNode(config, genesis, note) # -- epoch 0 -- @@ -77,12 +77,12 @@ class TestStakeRelativization(TestCase): np.random.seed(seed) stake = np.array((np.random.pareto(10, N) + 1) * 1000, dtype=np.int64) - coins = [Coin(sk=i, value=int(s)) for i, s in enumerate(stake)] + notes = [Note(sk=i, value=int(s)) for i, s in enumerate(stake)] config = Config.cryptarchia_v0_0_1(stake.sum() * 2).replace(k=40) - genesis = mk_genesis_state(coins) + genesis = mk_genesis_state(notes) - nodes = [TestNode(config, genesis, c) for c in coins] + nodes = [TestNode(config, genesis, n) for n in notes] T = config.epoch_length * EPOCHS slot_leaders = np.zeros(T, dtype=np.int32) diff --git a/cryptarchia/test_sync.py b/cryptarchia/test_sync.py index 101e8e0..043c298 100644 --- a/cryptarchia/test_sync.py +++ b/cryptarchia/test_sync.py @@ -1,6 +1,6 @@ from unittest import TestCase -from cryptarchia.cryptarchia import BlockHeader, Coin, Follower +from cryptarchia.cryptarchia import BlockHeader, Note, Follower from cryptarchia.sync import InvalidBlockTree, sync from cryptarchia.test_common import mk_block, mk_config, mk_genesis_state @@ -9,14 +9,14 @@ class TestSync(TestCase): def test_sync_single_chain_from_genesis(self): # Prepare a peer with a single chain: # b0 - b1 - b2 - b3 - coin = Coin(sk=0, value=10) - config = mk_config([coin]) - genesis = mk_genesis_state([coin]) + note = Note(sk=0, value=10) + config = mk_config([note]) + genesis = mk_genesis_state([note]) peer = Follower(genesis, config) - b0, coin = mk_block(genesis.block, 1, coin), coin.evolve() - b1, coin = mk_block(b0, 2, coin), coin.evolve() - b2, coin = mk_block(b1, 3, coin), coin.evolve() - b3, coin = mk_block(b2, 4, coin), coin.evolve() + b0, note = mk_block(genesis.block, 1, note), note.evolve() + b1, note = mk_block(b0, 2, note), note.evolve() + b2, note = mk_block(b1, 3, note), note.evolve() + b3, note = mk_block(b2, 4, note), note.evolve() for b in [b0, b1, b2, b3]: peer.on_block(b) self.assertEqual(peer.tip(), b3) @@ -32,14 +32,14 @@ class TestSync(TestCase): def test_sync_single_chain_from_middle(self): # Prepare a peer with a single chain: # b0 - b1 - b2 - b3 - coin = Coin(sk=0, value=10) - config = mk_config([coin]) - genesis = mk_genesis_state([coin]) + note = Note(sk=0, value=10) + config = mk_config([note]) + genesis = mk_genesis_state([note]) peer = Follower(genesis, config) - b0, coin = mk_block(genesis.block, 1, coin), coin.evolve() - b1, coin = mk_block(b0, 2, coin), coin.evolve() - b2, coin = mk_block(b1, 3, coin), coin.evolve() - b3, coin = mk_block(b2, 4, coin), coin.evolve() + b0, note = mk_block(genesis.block, 1, note), note.evolve() + b1, note = mk_block(b0, 2, note), note.evolve() + b2, note = mk_block(b1, 3, note), note.evolve() + b3, note = mk_block(b2, 4, note), note.evolve() for b in [b0, b1, b2, b3]: peer.on_block(b) self.assertEqual(peer.tip(), b3) @@ -61,16 +61,16 @@ class TestSync(TestCase): # b0 - b1 - b2 - b5 == tip # \ # b3 - b4 - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) @@ -88,16 +88,16 @@ class TestSync(TestCase): # b0 - b1 - b2 - b5 == tip # \ # b3 - b4 - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) @@ -121,16 +121,16 @@ class TestSync(TestCase): # b0 - b1 - b2 - b5 == tip # \ # b3 - b4 - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) @@ -156,15 +156,15 @@ class TestSync(TestCase): # Peer-1: b0 - b1 - b2 # \ # Peer-2: b3 - b4 - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() peer0 = Follower(genesis, config) for b in [b0, b1, b2, b5]: peer0.on_block(b) @@ -200,23 +200,23 @@ class TestSync(TestCase): # b0 - b1 - b2 - b3 - (invalid_b4) - (invalid_b5) # # First, build a valid chain (b0 ~ b3): - coin = Coin(sk=0, value=10) - config = mk_config([coin]) - genesis = mk_genesis_state([coin]) + note = Note(sk=0, value=10) + config = mk_config([note]) + genesis = mk_genesis_state([note]) peer = Follower(genesis, config) - b0, coin = mk_block(genesis.block, 1, coin), coin.evolve() - b1, coin = mk_block(b0, 2, coin), coin.evolve() - b2, coin = mk_block(b1, 3, coin), coin.evolve() - b3, coin = mk_block(b2, 4, coin), coin.evolve() + b0, note = mk_block(genesis.block, 1, note), note.evolve() + b1, note = mk_block(b0, 2, note), note.evolve() + b2, note = mk_block(b1, 3, note), note.evolve() + b3, note = mk_block(b2, 4, note), note.evolve() for b in [b0, b1, b2, b3]: peer.on_block(b) self.assertEqual(peer.tip(), b3) self.assertEqual(peer.forks, []) # And deliberately, add invalid blocks (b4 ~ b5): - fake_coin = Coin(sk=1, value=10) - b4, fake_coin = mk_block(b3, 5, fake_coin), fake_coin.evolve() - b5, fake_coin = mk_block(b4, 6, fake_coin), fake_coin.evolve() + fake_note = Note(sk=1, value=10) + b4, fake_note = mk_block(b3, 5, fake_note), fake_note.evolve() + b5, fake_note = mk_block(b4, 6, fake_note), fake_note.evolve() apply_invalid_block_to_ledger_state(peer, b4) apply_invalid_block_to_ledger_state(peer, b5) # the tip shouldn't be changed. @@ -239,25 +239,25 @@ class TestSync(TestCase): # b2 - (invalid_b6) - (invalid_b7) # # First, build a valid chain (b0 ~ b5): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b3, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b4, c_a = mk_block(b3, 4, c_a), c_a.evolve() - b5, c_a = mk_block(b4, 5, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b3, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b4, n_a = mk_block(b3, 4, n_a), n_a.evolve() + b5, n_a = mk_block(b4, 5, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) self.assertEqual(peer.forks, [b2.id()]) # And deliberately, add invalid blocks (b6 ~ b7): - fake_coin = Coin(sk=2, value=10) - b6, fake_coin = mk_block(b2, 3, fake_coin), fake_coin.evolve() - b7, fake_coin = mk_block(b6, 4, fake_coin), fake_coin.evolve() + fake_note = Note(sk=2, value=10) + b6, fake_note = mk_block(b2, 3, fake_note), fake_note.evolve() + b7, fake_note = mk_block(b6, 4, fake_note), fake_note.evolve() apply_invalid_block_to_ledger_state(peer, b6) apply_invalid_block_to_ledger_state(peer, b7) # the tip shouldn't be changed. @@ -287,14 +287,14 @@ class TestSyncFromCheckpoint(TestCase): # b0 - b1 - b2 - b3 # || # checkpoint - coin = Coin(sk=0, value=10) - config = mk_config([coin]) - genesis = mk_genesis_state([coin]) + note = Note(sk=0, value=10) + config = mk_config([note]) + genesis = mk_genesis_state([note]) peer = Follower(genesis, config) - b0, coin = mk_block(genesis.block, 1, coin), coin.evolve() - b1, coin = mk_block(b0, 2, coin), coin.evolve() - b2, coin = mk_block(b1, 3, coin), coin.evolve() - b3, coin = mk_block(b2, 4, coin), coin.evolve() + b0, note = mk_block(genesis.block, 1, note), note.evolve() + b1, note = mk_block(b0, 2, note), note.evolve() + b2, note = mk_block(b1, 3, note), note.evolve() + b3, note = mk_block(b2, 4, note), note.evolve() for b in [b0, b1, b2, b3]: peer.on_block(b) self.assertEqual(peer.tip(), b3) @@ -324,16 +324,16 @@ class TestSyncFromCheckpoint(TestCase): # b0 - b1 - b2 - b5 == tip # \ # b3 - b4 - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) @@ -363,15 +363,15 @@ class TestSyncFromCheckpoint(TestCase): # Peer1: b3 - b4 # || # checkpoint - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b3, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b4, c_b = mk_block(b3, 3, c_b), c_b.evolve() - b5, c_a = mk_block(b2, 4, c_a), c_a.evolve() + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b3, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b4, n_b = mk_block(b3, 3, n_b), n_b.evolve() + b5, n_a = mk_block(b2, 4, n_a), n_a.evolve() peer0 = Follower(genesis, config) for b in [b0, b1, b2, b5]: peer0.on_block(b) @@ -407,25 +407,25 @@ class TestSyncFromCheckpoint(TestCase): # b2 - (invalid_b6) - (invalid_b7) # # First, build a valid chain (b0 ~ b5): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b3, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b4, c_a = mk_block(b3, 4, c_a), c_a.evolve() - b5, c_a = mk_block(b4, 5, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b3, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b4, n_a = mk_block(b3, 4, n_a), n_a.evolve() + b5, n_a = mk_block(b4, 5, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) self.assertEqual(peer.forks, [b2.id()]) # And deliberately, add invalid blocks (b6 ~ b7): - fake_coin = Coin(sk=2, value=10) - b6, fake_coin = mk_block(b2, 3, fake_coin), fake_coin.evolve() - b7, fake_coin = mk_block(b6, 4, fake_coin), fake_coin.evolve() + fake_note = Note(sk=2, value=10) + b6, fake_note = mk_block(b2, 3, fake_note), fake_note.evolve() + b7, fake_note = mk_block(b6, 4, fake_note), fake_note.evolve() apply_invalid_block_to_ledger_state(peer, b6) apply_invalid_block_to_ledger_state(peer, b7) # the tip shouldn't be changed. @@ -457,25 +457,25 @@ class TestSyncFromCheckpoint(TestCase): # b2 - (invalid_b6) - (invalid_b7) # # First, build a valid chain (b0 ~ b5): - c_a, c_b = Coin(sk=0, value=10), Coin(sk=1, value=10) - config = mk_config([c_a, c_b]) - genesis = mk_genesis_state([c_a, c_b]) + n_a, n_b = Note(sk=0, value=10), Note(sk=1, value=10) + config = mk_config([n_a, n_b]) + genesis = mk_genesis_state([n_a, n_b]) peer = Follower(genesis, config) - b0, c_a = mk_block(genesis.block, 1, c_a), c_a.evolve() - b1, c_a = mk_block(b0, 2, c_a), c_a.evolve() - b2, c_b = mk_block(b0, 2, c_b), c_b.evolve() - b3, c_a = mk_block(b1, 3, c_a), c_a.evolve() - b4, c_a = mk_block(b3, 4, c_a), c_a.evolve() - b5, c_a = mk_block(b4, 5, c_a), c_a.evolve() + b0, n_a = mk_block(genesis.block, 1, n_a), n_a.evolve() + b1, n_a = mk_block(b0, 2, n_a), n_a.evolve() + b2, n_b = mk_block(b0, 2, n_b), n_b.evolve() + b3, n_a = mk_block(b1, 3, n_a), n_a.evolve() + b4, n_a = mk_block(b3, 4, n_a), n_a.evolve() + b5, n_a = mk_block(b4, 5, n_a), n_a.evolve() for b in [b0, b1, b2, b3, b4, b5]: peer.on_block(b) self.assertEqual(peer.tip(), b5) self.assertEqual(peer.forks, [b2.id()]) # And deliberately, add invalid blocks (b6 ~ b7): - fake_coin = Coin(sk=2, value=10) - b6, fake_coin = mk_block(b2, 3, fake_coin), fake_coin.evolve() - b7, fake_coin = mk_block(b6, 4, fake_coin), fake_coin.evolve() + fake_note = Note(sk=2, value=10) + b6, fake_note = mk_block(b2, 3, fake_note), fake_note.evolve() + b7, fake_note = mk_block(b6, 4, fake_note), fake_note.evolve() apply_invalid_block_to_ledger_state(peer, b6) apply_invalid_block_to_ledger_state(peer, b7) # the tip shouldn't be changed.