def test_rlp_encode_fails() -> None: test_cases = [ 123, [b"hello", Uint(255), [b"how", [b"are", [b"you", [123]]]]], ] for raw_data in test_cases: with pytest.raises(TypeError): rlp.encode(cast(RLP, raw_data))
def run_test(path: str) -> None: base = ( "tests/fixtures/" "LegacyTests/Constantinople/BlockchainTests/GeneralStateTests/" ) test = load_test(base + path) genesis_header = json_to_header(test.get("genesisBlockHeader")) genesis = Block( genesis_header, [], [], ) assert rlp_hash(genesis_header) == hex2bytes( test["genesisBlockHeader"]["hash"] ) assert rlp.encode(cast(rlp.RLP, genesis)) == hex2bytes( test.get("genesisRLP") ) pre_state = json_to_state(test.get("pre")) expected_post_state = json_to_state(test.get("postState")) chain = BlockChain( blocks=[genesis], state=pre_state, ) block_obj = None for block in test.get("blocks"): header = json_to_header(block.get("blockHeader")) txs: List[Transaction] = [ json_to_tx(tx_json) for tx_json in block.get("transactions") ] ommers: List[Header] = [ json_to_header(ommer_json) for ommer_json in block.get("uncleHeaders") ] assert rlp_hash(header) == hex2bytes(block["blockHeader"]["hash"]) block_obj = Block(header, txs, ommers) assert rlp.encode(cast(rlp.RLP, block_obj)) == hex2bytes(block["rlp"]) state_transition(chain, block_obj) last_block_hash = rlp_hash(chain.blocks[-1].header) assert last_block_hash == hex2bytes(test["lastblockhash"]) assert chain.state == expected_post_state
def test_roundtrip_encoding_and_decoding() -> None: test_cases = [ b"", b"h", b"hello how are you doing today?", Uint(35).to_be_bytes(), Uint(255).to_be_bytes(), [], [ b"hello", [b"how", [b"are", b"you", [b"doing", [Uint(255).to_be_bytes()]]]], ], ] for raw_data in test_cases: assert rlp.decode(rlp.encode(cast(RLP, raw_data))) == raw_data
def test_rlp_encode_successfully() -> None: test_cases = [ (b"", bytearray([0x80])), (b"\x83" * 55, bytearray([0xB7]) + bytearray(b"\x83" * 55)), (Uint(0), b"\x80"), (Uint(255), b"\x81\xff"), ([], bytearray([0xC0])), ( [b"hello"] * 5 + [Uint(35)] * 5, # type: ignore bytearray([0xE3]) + bytearray(b"\x85hello\x85hello\x85hello\x85hello\x85hello#####"), ), ( [b"hello", Uint(255), [b"how", [b"are", b"you", [b"doing"]]]], bytearray( b"\xdd\x85hello\x81\xff\xd4\x83how\xcf\x83are\x83you\xc6\x85doing" ), ), ] for (raw_data, expected_encoding) in test_cases: assert rlp.encode(cast(RLP, raw_data)) == expected_encoding
def test_rlp_encode_multi_char_str() -> None: assert rlp.encode("hello") == b"\x85hello"
def test_rlp_encode_one_char_str() -> None: assert rlp.encode("h") == b"h"
def test_rlp_encode_empty_str() -> None: assert rlp.encode("") == b"\x80"
def test_rlp_encode_uint256_byte_max() -> None: assert rlp.encode(U256(255)) == b"\x81\xff"
def test_rlp_encode_uint256_0() -> None: assert rlp.encode(U256(0)) == b"\x80"
def test_rlp_encode_uint_0() -> None: assert rlp.encode(Uint(0)) == b"\x80"
def test_ethtest_fixtures_for_successfull_rlp_decoding( raw_data: Bytes, encoded_data: Bytes) -> None: decoded_data = rlp.decode(encoded_data) assert rlp.encode(decoded_data) == encoded_data
def test_ethtest_fixtures_for_rlp_encoding( raw_data: RLP, expected_encoded_data: Bytes) -> None: assert rlp.encode(raw_data) == expected_encoded_data