Beispiel #1
0
class BosonMicroBlock(HeliosMicroBlock):
    fields = [
        ('header', MicroBlockHeader),
        ('transactions', CountableList(BosonTransaction)),
        ('receive_transactions', CountableList(BosonReceiveTransaction)),
        ('reward_bundle', StakeRewardBundle),
    ]
Beispiel #2
0
class SpuriousDragonBlock(HomesteadBlock):
    transaction_class = SpuriousDragonTransaction
    fields = [
        ('header', BlockHeader),
        ('transactions', CountableList(transaction_class)),
        ('uncles', CountableList(BlockHeader))
    ]
Beispiel #3
0
class Block(Serializable):
    fields = [('header', BlockHeader),
              ('transaction_list', CountableList(Transaction)),
              ('uncles', CountableList(BlockHeader))]

    def __init__(self, header, transaction_list=None, uncles=None, **kwargs):
        super().__init__(header, transaction_list or [], uncles or [],
                         **kwargs)
Beispiel #4
0
class HeliosMicroBlock(BaseMicroBlock):
    fields = [
        ('header', MicroBlockHeader),
        ('transactions', CountableList(HeliosTestnetTransaction)),
        ('receive_transactions',
         CountableList(HeliosTestnetReceiveTransaction)),
        ('reward_bundle', StakeRewardBundle),
    ]
Beispiel #5
0
class BosonBlock(HeliosTestnetBlock):
    transaction_class = BosonTransaction
    receive_transaction_class = BosonReceiveTransaction
    header_class = BlockHeader
    reward_bundle_class = StakeRewardBundle
    receipt_class = Receipt

    fields = [
        ('header', header_class),
        ('transactions', CountableList(transaction_class)),
        ('receive_transactions', CountableList(receive_transaction_class)),
        ('reward_bundle', reward_bundle_class),
    ]
class StakeRewardType2(rlp.Serializable, metaclass=ABCMeta):
    proof_class = NodeStakingScore
    fields = [
        ('amount', big_endian_int),
        ('proof', CountableList(NodeStakingScore)),
    ]

    def __init__(self,
                 amount: int = 0,
                 proof: List[NodeStakingScore] = [],
                 **kwargs: Any) -> None:

        super(StakeRewardType2, self).__init__(amount, proof, **kwargs)

    # @property
    # def hash(self) -> bytes:
    #     return keccak(self.get_message_for_hash())
    #
    # def get_message_for_hash(self):
    #     return rlp.encode([self.amount, self.proof_root_hash])

    @property
    def proof_root_hash(self) -> bytes:
        root_hash, kv_nodes = make_trie_root_and_nodes(self.proof)
        return root_hash
def test_peek():
    assert rlp.peek(rlp.encode(b''), []) == b''
    nested = rlp.encode([0, 1, [2, 3]])
    assert rlp.peek(nested, [2, 0], big_endian_int) == 2
    for index in [3, [3], [0, 0], [2, 2], [2, 1, 0]]:
        with pytest.raises(IndexError):
            rlp.peek(nested, index)
    assert rlp.peek(nested, 2, CountableList(big_endian_int)) == (2, 3)
def test_evaluation_of_lazy_decode_with_list_sedes_and_invalid_value():
    sedes = CountableList(big_endian_int)
    value = [(), (1, 2), b'asdf', (3)]
    invalid_lazy = rlp.decode_lazy(rlp.encode(value), sedes)
    assert invalid_lazy[0] == value[0]
    assert invalid_lazy[1] == value[1]
    with pytest.raises(DeserializationError):
        invalid_lazy[2]
def test_list_sedes():
    l1 = List()
    l2 = List((big_endian_int, big_endian_int))
    l3 = List((l1, l2, [[[]]]))

    l1.serialize([])
    l2.serialize((2, 3))
    l3.serialize([[], [5, 6], [[[]]]])

    with pytest.raises(SerializationError):
        l1.serialize([[]])
    with pytest.raises(SerializationError):
        l1.serialize([5])

    for d in ([], [1, 2, 3], [1, [2, 3], 4]):
        with pytest.raises(SerializationError):
            l2.serialize(d)
    for d in ([], [[], [], [[[]]]], [[], [5, 6], [[]]]):
        with pytest.raises(SerializationError):
            l3.serialize(d)

    c = CountableList(big_endian_int)
    assert l1.deserialize(c.serialize([])) == ()
    for s in (c.serialize(l) for l in [[1], [1, 2, 3], range(30), (4, 3)]):
        with pytest.raises(DeserializationError):
            l1.deserialize(s)

    valid = [(1, 2), (3, 4), (9, 8)]
    for s, v in ((c.serialize(v), v) for v in valid):
        assert l2.deserialize(s) == v
    invalid = [[], [1], [1, 2, 3]]
    for s in (c.serialize(i) for i in invalid):
        with pytest.raises(DeserializationError):
            l2.deserialize(s)
Beispiel #10
0
class Account(rlp.Serializable):
    """
    RLP object for accounts.
    """
    fields = [('nonce', big_endian_int), ('block_number', big_endian_int),
              ('receivable_transactions', CountableList(TransactionKey)),
              ('block_conflicts', CountableList(BlockConflictKey)),
              ('balance', big_endian_int), ('storage_root', trie_root),
              ('code_hash', hash32)]

    def __init__(self,
                 nonce: int = 0,
                 block_number: int = 0,
                 receivable_transactions=(),
                 block_conflicts=(),
                 balance: int = 0,
                 storage_root: bytes = BLANK_ROOT_HASH,
                 code_hash: bytes = EMPTY_SHA3,
                 **kwargs: Any) -> None:
        super(Account,
              self).__init__(nonce, block_number, receivable_transactions,
                             block_conflicts, balance, storage_root, code_hash,
                             **kwargs)
Beispiel #11
0
class BosonQueueBlock(BosonBlock, HeliosTestnetQueueBlock):
    transaction_class = HeliosTestnetTransaction
    receive_transaction_class = HeliosTestnetReceiveTransaction
    reward_bundle_class = StakeRewardBundle
    receipt_class = Receipt

    fields = [
        ('header', BlockHeader),
        ('transactions', CountableList(transaction_class)),
        ('receive_transactions', CountableList(receive_transaction_class)),
        ('reward_bundle', StakeRewardBundle),
    ]

    def as_complete_block(self, private_key, chain_id):
        # first lets sign the header
        """
        signs the header of the given block and changes it to a complete block
        doesnt validate the header before doing so
        """

        signed_header = self.header.get_signed(private_key, chain_id)

        return BosonBlock(signed_header, self.transactions,
                          self.receive_transactions, self.reward_bundle)
Beispiel #12
0
class Log(rlp.Serializable):
    fields = [
        ('address', address),
        ('topics', CountableList(int32)),
        ('data', binary)
    ]

    def __init__(self, address: bytes, topics: bytes, data: bytes) -> None:
        super(Log, self).__init__(address, topics, data)

    @property
    def bloomables(self):
        return (
            self.address,
        ) + tuple(
            int32.serialize(topic) for topic in self.topics
        )
class Receipt(rlp.Serializable):

    fields = [('status_code', binary), ('gas_used', big_endian_int),
              ('bloom', int256), ('logs', CountableList(Log))]

    def __init__(self,
                 status_code: bytes,
                 gas_used: int,
                 logs: Iterable[Log],
                 bloom: int = None) -> None:

        if bloom is None:
            bloomables = itertools.chain.from_iterable(log.bloomables
                                                       for log in logs)
            bloom = int(BloomFilter.from_iterable(bloomables))

        super(Receipt, self).__init__(
            status_code=status_code,
            gas_used=gas_used,
            bloom=bloom,
            logs=logs,
        )

        for log_idx, log in enumerate(self.logs):
            if log.address not in self.bloom_filter:
                raise ValidationError(
                    "The address from the log entry at position {0} is not "
                    "present in the provided bloom filter.".format(log_idx))
            for topic_idx, topic in enumerate(log.topics):
                if int32.serialize(topic) not in self.bloom_filter:
                    raise ValidationError(
                        "The topic at position {0} from the log entry at "
                        "position {1} is not present in the provided bloom "
                        "filter.".format(topic_idx, log_idx))

    @property
    def bloom_filter(self) -> BloomFilter:
        return BloomFilter(self.bloom)

    @bloom_filter.setter
    def bloom_filter(self, value: BloomFilter) -> None:
        self.bloom = int(value)
Beispiel #14
0
class HeliosTestnetBlock(BaseBlock):
    transaction_class = HeliosTestnetTransaction
    receive_transaction_class = HeliosTestnetReceiveTransaction
    header_class = BlockHeader
    reward_bundle_class = StakeRewardBundle
    receipt_class = Receipt

    fields = [
        ('header', header_class),
        ('transactions', CountableList(transaction_class)),
        ('receive_transactions', CountableList(receive_transaction_class)),
        ('reward_bundle', reward_bundle_class),
    ]

    bloom_filter = None

    def __init__(self,
                 header,
                 transactions=None,
                 receive_transactions=None,
                 reward_bundle=None):
        if transactions is None:
            transactions = []

        if receive_transactions is None:
            receive_transactions = []

        if reward_bundle is None:
            reward_bundle = StakeRewardBundle()

        self.bloom_filter = BloomFilter(header.bloom)

        super(HeliosTestnetBlock, self).__init__(
            header=header,
            transactions=transactions,
            receive_transactions=receive_transactions,
            reward_bundle=reward_bundle,
        )

    #
    # Helpers
    #
    @property
    def number(self):
        return self.header.block_number

    @property
    def hash(self):
        return self.header.hash

    #
    # Transaction class for this block class
    #
    @classmethod
    def get_transaction_class(cls):
        # TODO:Remove
        return cls.transaction_class

    #
    # Receive transaction class for this block class
    #
    @classmethod
    def get_receive_transaction_class(cls):
        #TODO:Remove
        return cls.receive_transaction_class

    #
    # Reward bundle class for this block class
    #
    @classmethod
    def get_reward_bundle_class(cls):
        # TODO:Remove
        return cls.reward_bundle_class

    #
    # Receipts API
    #
    def get_receipts(self, chaindb):
        return chaindb.get_receipts(self.header, Receipt)

    #
    # Header API
    #
    @classmethod
    def from_header(cls, header, chaindb: 'BaseChainDB'):
        """
        Returns the block denoted by the given block header.
        """
        #TODO:Remove. It is dirty to have to pass the chaindb into here.
        transactions = chaindb.get_block_transactions(header,
                                                      cls.transaction_class)
        receive_transactions = chaindb.get_block_receive_transactions(
            header, cls.receive_transaction_class)
        reward_bundle = chaindb.get_reward_bundle(header.reward_hash,
                                                  cls.reward_bundle_class)

        return cls(header=header,
                   transactions=transactions,
                   receive_transactions=receive_transactions,
                   reward_bundle=reward_bundle)

    #
    # Microblock API
    #
    @classmethod
    def from_micro_block(cls, micro_block: HeliosMicroBlock):
        header = cls.header_class.from_micro_header(micro_block.header)
        return cls(
            header=header,
            transactions=micro_block.transactions,
            receive_transactions=micro_block.receive_transactions,
            reward_bundle=micro_block.reward_bundle,
        )
Beispiel #15
0
class FrontierBlock(BaseBlock):
    transaction_class = FrontierTransaction
    fields = [('header', BlockHeader),
              ('transactions', CountableList(transaction_class)),
              ('uncles', CountableList(BlockHeader))]

    bloom_filter = None

    def __init__(self, header, transactions=None, uncles=None):
        if transactions is None:
            transactions = []
        if uncles is None:
            uncles = []

        self.bloom_filter = BloomFilter(header.bloom)

        super(FrontierBlock, self).__init__(
            header=header,
            transactions=transactions,
            uncles=uncles,
        )
        # TODO: should perform block validation at this point?

    #
    # Helpers
    #
    @property
    def number(self):
        return self.header.block_number

    @property
    def hash(self):
        return self.header.hash

    #
    # Transaction class for this block class
    #
    @classmethod
    def get_transaction_class(cls):
        return cls.transaction_class

    #
    # Receipts API
    #
    def get_receipts(self, chaindb):
        return chaindb.get_receipts(self.header, Receipt)

    #
    # Header API
    #
    @classmethod
    def from_header(cls, header, chaindb):
        """
        Returns the block denoted by the given block header.
        """
        if header.uncles_hash == EMPTY_UNCLE_HASH:
            uncles = []  # type: List[bytes]
        else:
            uncles = chaindb.get_block_uncles(header.uncles_hash)

        transactions = chaindb.get_block_transactions(
            header, cls.get_transaction_class())

        return cls(
            header=header,
            transactions=transactions,
            uncles=uncles,
        )

    #
    # Execution API
    #
    def add_uncle(self, uncle):
        self.uncles.append(uncle)
        self.header.uncles_hash = keccak(rlp.encode(self.uncles))
        return self
Beispiel #16
0
class HeliosTestnetQueueBlock(HeliosTestnetBlock, BaseQueueBlock):
    transaction_class = HeliosTestnetTransaction
    receive_transaction_class = HeliosTestnetReceiveTransaction
    reward_bundle_class = StakeRewardBundle
    receipt_class = Receipt

    fields = [
        ('header', BlockHeader),
        ('transactions', CountableList(transaction_class)),
        ('receive_transactions', CountableList(receive_transaction_class)),
        ('reward_bundle', StakeRewardBundle),
    ]
    #
    # Header API
    #
    @classmethod
    def from_header(cls, header):
        """
        Returns the block denoted by the given block header.
        """
        #        if not isinstance(header, UnsignedBlockHeader):
        #            header = header.create_unsigned_block_header_from_self()
        #
        #creating a new queueblock means it has no transactions yet
        transactions = []
        receive_transactions = []

        return cls(
            header=header.copy(gas_used=0, v=0, r=0, s=0),
            transactions=transactions,
            receive_transactions=receive_transactions,
            reward_bundle=cls.reward_bundle_class(),
        )

    @classmethod
    def make_genesis_block(cls, chain_address: Address):
        genesis_header = BlockHeader(
            chain_address=chain_address,
            account_hash=constants.GENESIS_ACCOUNT_HASH,
            extra_data=constants.GENESIS_EXTRA_DATA,
            gas_limit=constants.GENESIS_GAS_LIMIT,
            gas_used=0,
            bloom=0,
            block_number=0,
            parent_hash=constants.GENESIS_PARENT_HASH,
            receipt_root=constants.BLANK_ROOT_HASH,
            timestamp=int(time.time()),
            transaction_root=constants.BLANK_ROOT_HASH,
            receive_transaction_root=constants.BLANK_ROOT_HASH,
        )
        return cls.from_header(genesis_header)

    def as_complete_block(self, private_key, chain_id):
        #first lets sign the header
        """
        signs the header of the given block and changes it to a complete block
        doesnt validate the header before doing so
        """

        signed_header = self.header.get_signed(private_key, chain_id)

        return HeliosTestnetBlock(signed_header, self.transactions,
                                  self.receive_transactions,
                                  self.reward_bundle)
class Message(rlp.Serializable):

    fields = [('field1', binary), ('field2', binary),
              ('field3', CountableList(binary, max_length=100))]
Beispiel #18
0
class ByzantiumBlock(SpuriousDragonBlock):
    transaction_class = ByzantiumTransaction
    fields = [('header', BlockHeader),
              ('transactions', CountableList(transaction_class)),
              ('uncles', CountableList(BlockHeader))]
Beispiel #19
0
class HomesteadBlock(FrontierBlock):
    transaction_class = HomesteadTransaction
    fields = [('header', BlockHeader),
              ('transactions', CountableList(transaction_class)),
              ('uncles', CountableList(BlockHeader))]