Пример #1
0
def explain_default_size():
    """
    Show the size of the serialized object with defaults
    Note: ValidatorRecord omitted (has no defaults)
    """
    attestation_record = AttestationRecord()
    block = Block()
    crosslink_record = CrosslinkRecord()
    shard_and_committee = ShardAndCommittee()
    crystallized_state = CrystallizedState()

    attestation_record_bytes = serialize(attestation_record, type(attestation_record))
    block_bytes = serialize(block, type(block))
    crosslink_record_bytes = serialize(crosslink_record, type(crosslink_record))
    shard_and_committee_bytes = serialize(shard_and_committee, type(shard_and_committee))
    crystallized_state_bytes = serialize(crystallized_state, type(crystallized_state))

    if (verbose):
        print('{} | {}'.format(len(attestation_record_bytes), attestation_record_bytes))
        print('{} | {}'.format(len(block_bytes), block_bytes))
        print('{} | {}'.format(len(shard_and_committee_bytes), shard_and_committee_bytes))
        print('{} | {}'.format(len(crosslink_record_bytes), crosslink_record_bytes))
        print('{} | {}'.format(len(crystallized_state_bytes), crystallized_state_bytes))

    return {
        'attestationRecord': len(attestation_record_bytes),
        'block': len(block_bytes),
        'shardAndCommittee': len(shard_and_committee_bytes),
        'crosslinkRecord': len(crosslink_record_bytes),
        'validatorRecord': "N/A",
        'crystallizedState': len(crystallized_state_bytes),
    }
Пример #2
0
def test_state_transition_integration(genesis_crystallized_state,
                                      genesis_active_state,
                                      genesis_block,
                                      num_validators,
                                      epoch_length,
                                      mock_make_child):
    c = genesis_crystallized_state
    a = genesis_active_state
    block = genesis_block
    print('Generated genesis state')
    print('Crystallized state length:', len(serialize(genesis_crystallized_state)))
    print('Active state length:', len(serialize(genesis_active_state)))
    print('Block size:', len(serialize(genesis_block)))

    block2, c2, a2 = mock_make_child((c, a), block, 0, 0.8, [])
    t = time.time()
    assert compute_state_transition((c, a), block, block2)
    print("Normal block (basic attestation only) processed in %.4f sec" % (time.time() - t))
    print('Verified a block!')
    block3, c3, a3 = mock_make_child((c2, a2), block2, 0, 0.8, [(0, 0.75)])
    print('Verified a block with a committee!')
    while a3.height % epoch_length > 0:
        block3, c3, a3 = mock_make_child((c3, a3), block3, 0, 0.8, [(a3.height, 0.6 + 0.02 * a3.height)])
        print('Height: %d' % a3.height)
    print('FFG bitmask:', bin(int.from_bytes(a3.ffg_voter_bitfield, 'big')))
    block4, c4, a4 = mock_make_child((c3, a3), block3, 1, 0.55, [])
    t = time.time()
    assert compute_state_transition((c3, a3), block3, block4)
    print("Epoch transition processed in %.4f sec" % (time.time() - t))
Пример #3
0
    def mock_make_child(parent_state,
                        parent,
                        slot_number,
                        attestations=None):
        if attestations is None:
            attestations = []

        crystallized_state, active_state = parent_state
        block = Block(
            parent_hash=parent.hash,
            slot_number=slot_number,
            randao_reveal=blake(str(random.random()).encode('utf-8')),
            attestations=attestations,
            pow_chain_ref=b'\x00'*32,
            active_state_root=b'\x00'*32,
            crystallized_state_root=b'\x00'*32
        )
        print('Generated preliminary block header')

        new_crystallized_state, new_active_state = compute_state_transition(
            (crystallized_state, active_state),
            parent,
            block,
            config=config
        )
        print('Calculated state transition')

        if crystallized_state == new_crystallized_state:
            block.crystallized_state_root = parent.crystallized_state_root
        else:
            block.crystallized_state_root = blake(serialize(crystallized_state))

        block.active_state_root = blake(serialize(active_state))

        return block, new_crystallized_state, new_active_state
Пример #4
0
    def mock_make_child(parent_state,
                        parent,
                        skips,
                        attester_share=0.8,
                        crosslink_shards=[]):
        crystallized_state, active_state = parent_state
        block, proposer = make_unfinished_block(parent_state, parent, skips,
                                                attester_share,
                                                crosslink_shards)
        print('Generated preliminary block header')

        new_crystallized_state, new_active_state = compute_state_transition(
            (crystallized_state, active_state),
            parent,
            block,
            verify_sig=False,
            config=config)
        print('Calculated state transition')

        if crystallized_state == new_crystallized_state:
            block.state_hash = blake(parent.state_hash[:32] +
                                     blake(serialize(new_active_state)))
        else:
            block.state_hash = blake(
                blake(serialize(new_crystallized_state)) +
                blake(serialize(new_active_state)))
        # Main signature
        block.sign(
            keymap[crystallized_state.active_validators[proposer].pubkey])
        print('Signed')

        return block, new_crystallized_state, new_active_state
Пример #5
0
 def mock_make_child(parent_state, parent, skips, attester_share=0.8, crosslink_shards=[]):
     crystallized_state, active_state = parent_state
     parent_attestation = serialize(parent)
     indices, main_signer = get_attesters_and_signer(crystallized_state, active_state, skips)
     print('Selected indices: %r' % indices)
     print('Selected main signer: %d' % main_signer)
     # Randomly pick indices to include
     bitfield = [1 if random.random() < attester_share else 0 for i in indices]
     # Attestations
     sigs = [bls.sign(parent_attestation, keymap[crystallized_state.active_validators[indices[i]].pubkey])
             for i in range(len(indices)) if bitfield[i]]
     attestation_aggregate_sig = bls.aggregate_sigs(sigs)
     print('Aggregated sig')
     attestation_bitmask = bytearray((len(bitfield)-1) // 8 + 1)
     for i, b in enumerate(bitfield):
         attestation_bitmask[i//8] ^= (128 >> (i % 8)) * b
     print('Aggregate bitmask:', bin(int.from_bytes(attestation_bitmask, 'big')))
     # Randomly pick indices to include for crosslinks
     shard_aggregate_votes = []
     for shard, crosslinker_share in crosslink_shards:
         print('Making crosslink in shard %d' % shard)
         indices = get_shard_attesters(crystallized_state, shard)
         print('Indices: %r' % indices)
         bitfield = [1 if random.random() < crosslinker_share else 0 for i in indices]
         bitmask = bytearray((len(bitfield)+7) // 8)
         for i, b in enumerate(bitfield):
             bitmask[i//8] ^= (128 >> (i % 8)) * b
         print('Bitmask:', bin(int.from_bytes(bitmask, 'big')))
         shard_block_hash = blake(bytes([shard]))
         crosslink_attestation_hash = get_crosslink_aggvote_msg(shard, shard_block_hash, crystallized_state)
         sigs = [bls.sign(crosslink_attestation_hash, keymap[crystallized_state.active_validators[indices[i]].pubkey])
                 for i in range(len(indices)) if bitfield[i]]
         v = AggregateVote(shard_id=shard,
                           shard_block_hash=shard_block_hash,
                           signer_bitmask=bitmask,
                           aggregate_sig=list(bls.aggregate_sigs(sigs)))
         shard_aggregate_votes.append(v)
     print('Added %d shard aggregate votes' % len(crosslink_shards))
     # State calculations
     o = Block(parent_hash=blake(parent_attestation),
               skip_count=skips,
               randao_reveal=blake(str(random.random()).encode('utf-8')),
               attestation_bitmask=attestation_bitmask,
               attestation_aggregate_sig=list(attestation_aggregate_sig),
               shard_aggregate_votes=shard_aggregate_votes,
               main_chain_ref=b'\x00'*32,
               state_hash=b'\x00'*64)
     print('Generated preliminary block header')
     new_crystallized_state, new_active_state = \
         compute_state_transition((crystallized_state, active_state), parent, o, verify_sig=False)
     print('Calculated state transition')
     if crystallized_state == new_crystallized_state:
         o.state_hash = blake(parent.state_hash[:32] + blake(serialize(new_active_state)))
     else:
         o.state_hash = blake(blake(serialize(new_crystallized_state)) + blake(serialize(new_active_state)))
     # Main signature
     o.sign(keymap[crystallized_state.active_validators[main_signer].pubkey])
     print('Signed')
     return o, new_crystallized_state, new_active_state
Пример #6
0
def genesis_block(genesis_active_state, genesis_crystallized_state):
    active_state_root = blake(serialize(genesis_active_state))
    crystallized_state_root = blake(serialize(genesis_crystallized_state))
    
    return get_genesis_block(
        active_state_root=active_state_root,
        crystallized_state_root=crystallized_state_root,
    )
Пример #7
0
def test_state_transition_integration(genesis_crystallized_state,
                                      genesis_active_state,
                                      genesis_block,
                                      mock_make_child,
                                      mock_make_attestations,
                                      config):
    c = genesis_crystallized_state
    a = genesis_active_state
    block = genesis_block
    print('Generated genesis state')
    print('Crystallized state length:', len(serialize(genesis_crystallized_state)))
    print('Active state length:', len(serialize(genesis_active_state)))
    print('Block size:', len(serialize(genesis_block)))

    attestations_of_genesis = mock_make_attestations(
        (c, a),
        block,
        attester_share=0.8
    )

    block2, c2, a2 = mock_make_child((c, a), block, 1, attestations_of_genesis)
    assert block2.slot_number == 1
    assert len(block2.attestations) == len(attestations_of_genesis)
    assert block2.crystallized_state_root == block.crystallized_state_root
    assert block2.active_state_root != b'\x00'*32

    t = time.time()
    assert compute_state_transition((c, a), block, block2, config=config)
    print(
        "Normal block with %s attestations of size %s processed in %.4f sec" %
        (
            len(attestations_of_genesis),
            len(c.indices_for_slots[attestations_of_genesis[0].slot][0].committee),
            (time.time() - t))
        )
    print('Verified a block!')

    attestations_of_2 = mock_make_attestations(
        (c2, a2),
        block2,
        attester_share=0.8
    )

    cycle_transition_slot = c2.last_state_recalc + config['cycle_length']

    block3, c3, a3 = mock_make_child(
        (c2, a2),
        block2,
        cycle_transition_slot,
        attestations_of_2
    )

    t = time.time()
    assert compute_state_transition((c2, a2), block2, block3, config=config)
    print("Cycle transition processed in %.4f sec" % (time.time() - t))
Пример #8
0
def test_active_state_serialization():
    s = ActiveState()
    ds = deserialize(serialize(s, type(s)), type(s))
    assert eq(s, ds)

    s = ActiveState(partial_crosslinks=[
        PartialCrosslinkRecord(shard_id=42,
                               shard_block_hash=b'\x55' * 32,
                               voter_bitmask=b'31337dawg')
    ],
                    height=555,
                    randao=b'\x88' * 32,
                    balance_deltas=[5, 7, 9, 579] + [3] * 333)
    ds = deserialize(serialize(s, type(s)), type(s))
    assert eq(s, ds)
Пример #9
0
def test_get_genesis_block(genesis_active_state, genesis_crystallized_state):
    active_state_root = blake(serialize(genesis_active_state))
    crystallized_state_root = blake(serialize(genesis_crystallized_state))

    block = get_genesis_block(
        active_state_root=active_state_root,
        crystallized_state_root=crystallized_state_root,
    )

    assert block.parent_hash == ZERO_HASH32
    assert block.slot_number == 0
    assert block.randao_reveal == ZERO_HASH32
    assert block.num_attestations == 0
    assert block.pow_chain_ref == ZERO_HASH32
    assert block.active_state_root == active_state_root
    assert block.crystallized_state_root == crystallized_state_root
Пример #10
0
def get_hashes_to_sign(
        active_state: 'ActiveState',
        block: 'Block',
        config: Dict[str, Any] = DEFAULT_CONFIG) -> List[Hash32]:
    cycle_length = config['cycle_length']

    hashes = get_hashes_from_active_state(
        active_state,
        block,
        from_slot=block.slot_number - cycle_length + 1,
        to_slot=block.slot_number - 1,
        config=config,
    ) + [blake(serialize(block))]

    return hashes
Пример #11
0
def _compute_new_active_state(crystallized_state,
                              active_state,
                              parent_block,
                              block,
                              verify_sig=True,
                              config=DEFAULT_CONFIG):
    # Determine who the attesters and the main signer are
    attestation_indices, proposer = get_attesters_and_proposer(
        crystallized_state, active_state, block.skip_count, config)

    # Verify attestations
    attesters = process_attestations(crystallized_state.active_validators,
                                     attestation_indices,
                                     block.attestation_bitfield,
                                     serialize(parent_block),
                                     block.attestation_aggregate_sig)

    # Verify main signature
    if verify_sig:
        assert block.verify(
            crystallized_state.active_validators[proposer].pubkey)
        print('Verified main sig')

    # Update crosslink records
    new_crosslink_records, new_ffg_bitfield, total_new_voters = update_ffg_and_crosslink_progress(
        crystallized_state, active_state.partial_crosslinks,
        active_state.ffg_voter_bitfield, block.shard_aggregate_votes, config)

    # track the reward for the block proposer
    proposer = RecentProposerRecord(index=proposer,
                                    balance_delta=len(attesters) +
                                    total_new_voters)

    # TODO: verify randao reveal from validator's hash preimage
    updated_randao = (int.from_bytes(active_state.randao, 'big')
                      ^ int.from_bytes(block.randao_reveal, 'big')).to_bytes(
                          32, 'big')

    return ActiveState(
        height=active_state.height + 1,
        randao=updated_randao,
        total_skip_count=active_state.total_skip_count + block.skip_count,
        partial_crosslinks=new_crosslink_records,
        ffg_voter_bitfield=new_ffg_bitfield,
        recent_attesters=active_state.recent_attesters + attesters,
        recent_proposers=active_state.recent_proposers + [proposer])
Пример #12
0
def _compute_new_active_state(crystallized_state,
                              active_state,
                              parent_block,
                              block,
                              verify_sig=True):
    # Determine who the attesters and the main signer are
    attestation_indices, main_signer = get_attesters_and_signer(
        crystallized_state, active_state, block.skip_count)

    # Verify attestations
    balance_deltas = process_attestations(crystallized_state.active_validators,
                                          attestation_indices,
                                          block.attestation_bitfield,
                                          serialize(parent_block),
                                          block.attestation_aggregate_sig)

    # Reward main signer
    balance_deltas.append((main_signer << 24) + len(balance_deltas))

    # Verify main signature
    if verify_sig:
        assert block.verify(
            crystallized_state.active_validators[main_signer].pubkey)
        print('Verified main sig')

    # Update crosslink records
    new_crosslink_records, new_ffg_bitfield, total_new_voters = update_ffg_and_crosslink_progress(
        crystallized_state, active_state.partial_crosslinks,
        active_state.ffg_voter_bitfield, block.shard_aggregate_votes)
    balance_deltas.append((main_signer << 24) + total_new_voters)

    # TODO: verify randao reveal from validator's hash preimage
    updated_randao = (int.from_bytes(active_state.randao, 'big')
                      ^ int.from_bytes(block.randao_reveal, 'big')).to_bytes(
                          32, 'big')

    return ActiveState(
        height=active_state.height + 1,
        randao=updated_randao,
        total_skip_count=active_state.total_skip_count + block.skip_count,
        partial_crosslinks=new_crosslink_records,
        ffg_voter_bitfield=new_ffg_bitfield,
        balance_deltas=active_state.balance_deltas + balance_deltas)
Пример #13
0
def test_initialize_new_cycle(
        genesis_crystallized_state, genesis_active_state, genesis_block,
        last_state_recalc, last_justified_slot, justified_streak,
        last_finalized_slot, crystallized_state_total_deposits,
        block_vote_cache_total_deposits, result_last_state_recalc,
        result_justified_streak, result_last_finalized_slot, config):
    # Fill the parent_crystallized_state with parematers
    parent_crystallized_state = genesis_crystallized_state
    parent_crystallized_state.last_state_recalc = last_state_recalc
    parent_crystallized_state.last_justified_slot = last_justified_slot
    parent_crystallized_state.justified_streak = justified_streak
    parent_crystallized_state.last_finalized_slot = last_finalized_slot
    parent_crystallized_state.total_deposits = crystallized_state_total_deposits

    parent_active_state = genesis_active_state

    parent_block = genesis_block
    block = copy.deepcopy(genesis_block)
    block.slot_number = 258
    block.parent_hash = blake(serialize(parent_block))

    active_state = fill_recent_block_hashes(parent_active_state, parent_block,
                                            block)
    # Fill the total_voter_deposits to simulate the different committee results
    active_state.block_vote_cache[block.parent_hash] = {
        'voter_indices': set(),
        'total_voter_deposits': block_vote_cache_total_deposits,
    }

    crystallized_state, active_state = initialize_new_cycle(
        parent_crystallized_state,
        active_state,
        block,
        config=config,
    )
    assert crystallized_state.last_state_recalc == result_last_state_recalc
    assert crystallized_state.justified_streak == result_justified_streak
    assert crystallized_state.last_finalized_slot == result_last_finalized_slot
Пример #14
0
 def hash(self):
     return blake(serialize(self))
Пример #15
0
 def verify(self, pub):
     zig = self.sig
     self.sig = [0, 0]
     o = verify(serialize(self), pub, tuple(zig))
     self.sig = zig
     return o
Пример #16
0
 def sign(self, key):
     self.sig = [0, 0]
     self.sig = list(sign(serialize(self), key))
Пример #17
0
    def make_unfinished_block(parent_state,
                              parent,
                              skips,
                              attester_share=0.8,
                              crosslink_shards_and_shares=None):
        if crosslink_shards_and_shares is None:
            crosslink_shards_and_shares = []

        crystallized_state, active_state = parent_state
        parent_attestation = serialize(parent)
        indices, proposer = get_attesters_and_proposer(
            crystallized_state,
            active_state,
            skips,
            config
        )

        print('Selected indices: %r' % indices)
        print('Selected block proposer: %d' % proposer)

        # Randomly pick indices to include
        is_attesting = [random.random() < attester_share for _ in indices]
        # Attestations
        sigs = [
            bls.sign(
                parent_attestation,
                keymap[crystallized_state.active_validators[indices[i]].pubkey]
            )
            for i, attesting in enumerate(is_attesting) if attesting
        ]
        attestation_aggregate_sig = bls.aggregate_sigs(sigs)
        print('Aggregated sig')

        attestation_bitfield = get_empty_bitfield(len(indices))
        for i, attesting in enumerate(is_attesting):
            if attesting:
                attestation_bitfield = set_voted(attestation_bitfield, i)
        print('Aggregate bitfield:', bin(int.from_bytes(attestation_bitfield, 'big')))

        # Randomly pick indices to include for crosslinks
        shard_aggregate_votes = []

        # The shards that are selected to be crosslinking
        crosslink_shards = get_crosslink_shards(crystallized_state, config=config)

        for shard, crosslinker_share in crosslink_shards_and_shares:
            # Check if this shard is in the crosslink shards list
            assert shard in crosslink_shards

            print('Making crosslink in shard %d' % shard)
            indices = get_crosslink_notaries(crystallized_state, shard, crosslink_shards=crosslink_shards, config=config)
            print('Indices: %r' % indices)
            is_notarizing = [random.random() < attester_share for _ in indices]
            notary_bitfield = get_empty_bitfield(len(indices))
            for i, notarizing in enumerate(is_notarizing):
                if notarizing:
                    notary_bitfield = set_voted(notary_bitfield, i)
            print('Bitfield:', bin(int.from_bytes(notary_bitfield, 'big')))
            shard_block_hash = blake(bytes([shard]))
            crosslink_attestation_hash = get_crosslink_aggvote_msg(
                shard,
                shard_block_hash,
                crystallized_state
            )
            sigs = [
                bls.sign(
                    crosslink_attestation_hash,
                    keymap[crystallized_state.active_validators[indices[i]].pubkey]
                )
                for i, notarizing in enumerate(is_notarizing) if notarizing
            ]
            v = AggregateVote(
                shard_id=shard,
                shard_block_hash=shard_block_hash,
                notary_bitfield=notary_bitfield,
                aggregate_sig=list(bls.aggregate_sigs(sigs))
            )
            shard_aggregate_votes.append(v)
        print('Added %d shard aggregate votes' % len(crosslink_shards_and_shares))

        block = Block(
            parent_hash=blake(parent_attestation),
            skip_count=skips,
            randao_reveal=blake(str(random.random()).encode('utf-8')),
            attestation_bitfield=attestation_bitfield,
            attestation_aggregate_sig=list(attestation_aggregate_sig),
            shard_aggregate_votes=shard_aggregate_votes,
            main_chain_ref=b'\x00'*32,
            state_hash=b'\x00'*64
        )
        return block, proposer
Пример #18
0
def explain_maxval_size():
    """
    Show the size of the object when using maximum values
    """

    # Attestation Record
    # TODO replace oblique hash loop with correct size
    obl_hashes = []
    for i in range(0, 64):
        obl_hashes.append(helpers.MAX_BYTES)

    attestation_record = AttestationRecord(
        slot=helpers.MAX_I64,
        shard_id=helpers.MAX_I16,
        oblique_parent_hashes=obl_hashes,
        shard_block_hash=helpers.MAX_BYTES,
        attester_bitfield=helpers.MAX_BYTES,
        aggregate_sig=[helpers.MAX_256, helpers.MAX_256]
    )

    # Blocks
    # TODO: provide realistic number for attestations
    attestations = []
    for i in range(0, helpers.MAX_ATTESTATIONS):
        attestations.append(attestation_record)

    block = Block(
        parent_hash=helpers.MAX_BYTES,
        slot_number=helpers.MAX_I64,
        randao_reveal=helpers.MAX_BYTES,
        attestations=attestations,
        pow_chain_ref=helpers.MAX_BYTES,
        active_state_root=helpers.MAX_BYTES,
        crystallized_state_root=helpers.MAX_BYTES,
    )

    # Crosslink Record
    crosslink_record = CrosslinkRecord(
        hash=helpers.MAX_BYTES,
        dynasty=helpers.MAX_I64,
    )

    # Validator Record
    validator_record = ValidatorRecord(
        pubkey=helpers.MAX_256,
        withdrawal_shard=helpers.MAX_I16,
        withdrawal_address=helpers.ADDRESS_BYTES,
        randao_commitment=helpers.MAX_BYTES,
        balance=helpers.MAX_I64,
        start_dynasty=helpers.MAX_I64,
        end_dynasty=helpers.MAX_I64,
    )

    # Shard and Committee
    # TODO: replace placeholder
    committees = []
    for i in range(0, helpers.PLACEHOLDER):
        committees.append(helpers.MAX_I16)

    shard_and_committee = ShardAndCommittee(
        shard_id=helpers.MAX_I16,
        committee=committees,
    )

    # Crystallized State
    validatorlist = []
    for i in range(0, helpers.MAX_VALIDATORS):
        validatorlist.append(validator_record)

    # TODO: replace placeholder
    crosslinklist = []
    for i in range(0, helpers.PLACEHOLDER):
        crosslinklist.append(crosslink_record)

    # TODO: replace placeholder
    indices_heights = []
    for i in range(0, helpers.PLACEHOLDER):
        tmp = []
        for j in range(0, 10):
            tmp.append(shard_and_committee)
        indices_heights.append(tmp)

    crystallized_state = CrystallizedState(
        validators=validatorlist,
        indices_for_heights=indices_heights,
        last_justified_slot=helpers.MAX_I64,
        justified_streak=helpers.MAX_I64,
        last_finalized_slot=helpers.MAX_I64,
        current_dynasty=helpers.MAX_I64,
        crosslinking_start_shard=helpers.MAX_I16,
        crosslink_records=crosslinklist,
        total_deposits=helpers.MAX_256,
        dynasty_seed=helpers.MAX_BYTES,
        dynasty_seed_last_reset=helpers.MAX_I64,
        last_state_recalc=helpers.MAX_I64,
    )

    attestation_record_bytes = serialize(attestation_record, type(attestation_record))
    block_bytes = serialize(block, type(block))
    crosslink_record_bytes = serialize(crosslink_record, type(crosslink_record))
    validator_record_bytes = serialize(validator_record, type(validator_record))
    shard_and_committee_bytes = serialize(shard_and_committee, type(shard_and_committee))
    crystallized_state_bytes = serialize(crystallized_state, type(crystallized_state))

    if (verbose):
        print('{} | {}'.format(len(attestation_record_bytes), attestation_record_bytes))
        print('{} | {}'.format(len(block_bytes), block_bytes))
        print('{} | {}'.format(len(shard_and_committee_bytes), shard_and_committee_bytes))
        print('{} | {}'.format(len(crosslink_record_bytes), crosslink_record_bytes))
        print('{} | {}'.format(len(validator_record_bytes), validator_record_bytes))
        print('{} | {}'.format(len(crystallized_state_bytes), crystallized_state_bytes))

    return {
        'attestationRecord': len(attestation_record_bytes),
        'block': len(block_bytes),
        'shardAndCommittee': len(shard_and_committee_bytes),
        'crosslinkRecord': len(crosslink_record_bytes),
        'validatorRecord': len(validator_record_bytes),
        'crystallizedState': len(crystallized_state_bytes),
    }
Пример #19
0
def test_failed_serialization(value, typ):
    with pytest.raises(Exception):
        serialize(value, typ)
Пример #20
0
def state_hash(crystallized_state, active_state):
    return blake(serialize(crystallized_state)) + blake(
        serialize(active_state))
Пример #21
0
    def make_unfinished_block(parent_state,
                              parent,
                              skips,
                              attester_share=0.8,
                              crosslink_shards_and_shares=None):
        if crosslink_shards_and_shares is None:
            crosslink_shards_and_shares = []

        crystallized_state, active_state = parent_state
        parent_attestation = serialize(parent)
        indices, proposer = get_attesters_and_proposer(crystallized_state,
                                                       active_state, skips,
                                                       config)

        print('Selected indices: %r' % indices)
        print('Selected block proposer: %d' % proposer)

        # Randomly pick indices to include
        bitfield = [
            1 if random.random() < attester_share else 0 for i in indices
        ]
        # Attestations
        sigs = [
            bls.sign(
                parent_attestation, keymap[
                    crystallized_state.active_validators[indices[i]].pubkey])
            for i in range(len(indices)) if bitfield[i]
        ]
        attestation_aggregate_sig = bls.aggregate_sigs(sigs)
        print('Aggregated sig')

        attestation_bitfield = bytearray((len(bitfield) - 1) // 8 + 1)
        for i, b in enumerate(bitfield):
            attestation_bitfield[i // 8] ^= (128 >> (i % 8)) * b
        print('Aggregate bitfield:',
              bin(int.from_bytes(attestation_bitfield, 'big')))

        # Randomly pick indices to include for crosslinks
        shard_aggregate_votes = []

        # The shards that are selected to be crosslinking
        crosslink_shards = get_crosslink_shards(crystallized_state,
                                                config=config)

        for shard, crosslinker_share in crosslink_shards_and_shares:
            # Check if this shard is in the crosslink shards list
            assert shard in crosslink_shards

            print('Making crosslink in shard %d' % shard)
            indices = get_crosslink_notaries(crystallized_state,
                                             shard,
                                             crosslink_shards=crosslink_shards,
                                             config=config)
            print('Indices: %r' % indices)
            bitfield = [
                1 if random.random() < crosslinker_share else 0
                for i in indices
            ]
            bitmask = bytearray((len(bitfield) + 7) // 8)
            for i, b in enumerate(bitfield):
                bitmask[i // 8] ^= (128 >> (i % 8)) * b
            print('Bitmask:', bin(int.from_bytes(bitmask, 'big')))
            shard_block_hash = blake(bytes([shard]))
            crosslink_attestation_hash = get_crosslink_aggvote_msg(
                shard, shard_block_hash, crystallized_state)
            sigs = [
                bls.sign(
                    crosslink_attestation_hash,
                    keymap[crystallized_state.active_validators[
                        indices[i]].pubkey]) for i in range(len(indices))
                if bitfield[i]
            ]
            v = AggregateVote(shard_id=shard,
                              shard_block_hash=shard_block_hash,
                              signer_bitmask=bitmask,
                              aggregate_sig=list(bls.aggregate_sigs(sigs)))
            shard_aggregate_votes.append(v)
        print('Added %d shard aggregate votes' %
              len(crosslink_shards_and_shares))

        block = Block(
            parent_hash=blake(parent_attestation),
            skip_count=skips,
            randao_reveal=blake(str(random.random()).encode('utf-8')),
            attestation_bitfield=attestation_bitfield,
            attestation_aggregate_sig=list(attestation_aggregate_sig),
            shard_aggregate_votes=shard_aggregate_votes,
            main_chain_ref=b'\x00' * 32,
            state_hash=b'\x00' * 64)
        return block, proposer
Пример #22
0
def test_basic_serialization(value, typ, data):
    assert serialize(value, typ) == data
    assert deserialize(data, typ) == value