Esempio n. 1
0
    def __init__(self, validators: List[Validator]):
        assert len(validators) > 0, "At least one validator is required."
        self.validators = validators
        self.genesis = Message.genesis(r.choice(self.validators), validators)

        # Add genesis message to all validators
        for validator in self.validators:
            res = validator.add_message(self.genesis)
            assert res.is_ok(), res.value
 def create_message(self, rn: int) -> Optional[Message]:
     justification: Justification = self.state.justification()
     head: Block = Estimator.estimate(self.state, justification)
     if not self == head.active_validators[rn]:
         return None
     return Message(
         self,
         Block(head.height + 1, head.hash,
               head.active_validators),  # Block creation
         justification,
         self.state.current_slot())
Esempio n. 3
0
    def __init__(self, validators: List[Validator]):
        assert len(validators) > 0, "At least one validator is required."
        self.validators = validators

        # FIXME: Genesis block should be in states by default
        self.genesis = Message.genesis(r.choice(self.validators))
        self.genesis.estimate.active_validators = validators

        # Add genesis message to all validators
        for validator in self.validators:
            res = validator.add_message(self.genesis)
            assert res.is_ok(), res.value
Esempio n. 4
0
 def justify_message(self, message: Message) -> Result[Error, bool]:
     message.receiver_slot = self.ticker.current()
     self.store.add(message)
     return Ok(True)
Esempio n. 5
0
 def create_message(self) -> Message:
     sender: Validator = self
     estimate: Block = self.create_estimate()
     justification: Justification = self.state.justification()
     current_slot: int = self.state.current_slot()
     return Message(sender, estimate, justification, current_slot)
Esempio n. 6
0
def test_store():
    ticker = Ticker()
    validator = Validator("v0", 1.0, ticker)
    genesis = Message.genesis(validator)
    store = Store()
    store.add(genesis)
    b1 = Block(0, genesis.estimate.hash)
    child = Message(validator, b1,
                    Justification(store.latest_message_hashes()), 0)
    store.add(child)

    message_history = {validator: [genesis.hash, child.hash]}
    assert store.message_history == message_history

    messages = {genesis.hash: genesis, child.hash: child}
    assert store.messages == messages

    children = {genesis.hash: [child.hash]}
    assert store.children == children

    parent = {child.hash: genesis.hash}
    assert store.parent == parent

    block_to_message_in_hash = {
        genesis.estimate.hash: genesis.hash,
        child.estimate.hash: child.hash
    }
    assert store.block_to_message_in_hash == block_to_message_in_hash

    assert store.genesis == genesis

    assert store.message(child.hash) == child
    assert store.message(genesis.hash) == genesis
    assert store.message(000) is None

    latest_message_hashes = {validator: child.hash}
    assert store.latest_message_hashes() == latest_message_hashes

    latest_messages = {validator: child}
    assert store.latest_messages() == latest_messages

    assert store.parent_message(child) == genesis
    assert store.parent_message(genesis) is None

    assert store.children_messages(child) == []
    assert store.children_messages(genesis) == [child]

    assert store.parent_block(genesis.estimate) is None
    assert store.parent_block(child.estimate) == genesis.estimate

    assert store.children_blocks(child.estimate) == []
    assert store.children_blocks(genesis.estimate) == [child.estimate]

    assert store.has_children_blocks(genesis.estimate)
    assert not store.has_children_blocks(child.estimate)

    assert store.justified(genesis)
    assert store.justified(child)
    assert not store.justified(000)

    assert store.genesis_block() == genesis.estimate
Esempio n. 7
0
 def finalize_message(self, message: Message) -> Result[Error, bool]:
     # FIXME: Rename
     message.receiver_slot = self.ticker.current()
     self.store.add(message)
     return Ok(True)