|
| 1 | +from typing import List as PyList |
| 2 | +from rlp import decode |
| 3 | +from rlp_types import * |
| 4 | +from ssz_types import * |
| 5 | + |
| 6 | +def upgrade_rlp_transaction_to_ssz(pre_bytes: bytes, |
| 7 | + chain_id: ChainId) -> SignedTransaction: |
| 8 | + type_ = pre_bytes[0] |
| 9 | + |
| 10 | + if type_ == 0x03: # EIP-4844 |
| 11 | + pre = decode(pre_bytes[1:], Eip4844SignedTransaction) |
| 12 | + assert pre.chain_id == chain_id |
| 13 | + |
| 14 | + assert pre.signature_y_parity in (0, 1) |
| 15 | + ecdsa_signature = ecdsa_pack_signature( |
| 16 | + pre.signature_y_parity != 0, |
| 17 | + pre.signature_r, |
| 18 | + pre.signature_s, |
| 19 | + ) |
| 20 | + from_ = ecdsa_recover_from_address(ecdsa_signature, compute_eip4844_sig_hash(pre)) |
| 21 | + |
| 22 | + return SignedTransaction( |
| 23 | + payload=PartialContainer[TransactionPayload, MAX_TRANSACTION_PAYLOAD_FIELDS]( |
| 24 | + nonce=pre.nonce, |
| 25 | + max_fee_per_gas=pre.max_fee_per_gas, |
| 26 | + gas=pre.gas_limit, |
| 27 | + to=ExecutionAddress(pre.destination), |
| 28 | + value=pre.amount, |
| 29 | + input_=pre.data, |
| 30 | + access_list=[AccessTuple( |
| 31 | + address=access_tuple[0], |
| 32 | + storage_keys=access_tuple[1] |
| 33 | + ) for access_tuple in pre.access_list], |
| 34 | + max_priority_fee_per_gas=pre.max_priority_fee_per_gas, |
| 35 | + max_fee_per_blob_gas=pre.max_fee_per_blob_gas, |
| 36 | + blob_versioned_hashes=pre.blob_versioned_hashes, |
| 37 | + ), |
| 38 | + signature=PartialContainer[TransactionSignature, MAX_TRANSACTION_SIGNATURE_FIELDS]( |
| 39 | + from_=from_, |
| 40 | + ecdsa_signature=ecdsa_signature, |
| 41 | + type_=TRANSACTION_TYPE_EIP4844, |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + if type_ == 0x02: # EIP-1559 |
| 46 | + pre = decode(pre_bytes[1:], Eip1559SignedTransaction) |
| 47 | + assert pre.chain_id == chain_id |
| 48 | + |
| 49 | + assert pre.signature_y_parity in (0, 1) |
| 50 | + ecdsa_signature = ecdsa_pack_signature( |
| 51 | + pre.signature_y_parity != 0, |
| 52 | + pre.signature_r, |
| 53 | + pre.signature_s, |
| 54 | + ) |
| 55 | + from_ = ecdsa_recover_from_address(ecdsa_signature, compute_eip1559_sig_hash(pre)) |
| 56 | + |
| 57 | + return SignedTransaction( |
| 58 | + payload=PartialContainer[TransactionPayload, MAX_TRANSACTION_PAYLOAD_FIELDS]( |
| 59 | + nonce=pre.nonce, |
| 60 | + max_fee_per_gas=pre.max_fee_per_gas, |
| 61 | + gas=pre.gas_limit, |
| 62 | + to=ExecutionAddress(pre.destination) if len(pre.destination) > 0 else None, |
| 63 | + value=pre.amount, |
| 64 | + input_=pre.data, |
| 65 | + access_list=[AccessTuple( |
| 66 | + address=access_tuple[0], |
| 67 | + storage_keys=access_tuple[1] |
| 68 | + ) for access_tuple in pre.access_list], |
| 69 | + max_priority_fee_per_gas=pre.max_priority_fee_per_gas, |
| 70 | + ), |
| 71 | + signature=PartialContainer[TransactionSignature, MAX_TRANSACTION_SIGNATURE_FIELDS]( |
| 72 | + from_=from_, |
| 73 | + ecdsa_signature=ecdsa_signature, |
| 74 | + type_=TRANSACTION_TYPE_EIP1559, |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + if type_ == 0x01: # EIP-2930 |
| 79 | + pre = decode(pre_bytes[1:], Eip2930SignedTransaction) |
| 80 | + assert pre.chainId == chain_id |
| 81 | + |
| 82 | + assert pre.signatureYParity in (0, 1) |
| 83 | + ecdsa_signature = ecdsa_pack_signature( |
| 84 | + pre.signatureYParity != 0, |
| 85 | + pre.signatureR, |
| 86 | + pre.signatureS |
| 87 | + ) |
| 88 | + from_ = ecdsa_recover_from_address(ecdsa_signature, compute_eip2930_sig_hash(pre)) |
| 89 | + |
| 90 | + return SignedTransaction( |
| 91 | + payload=PartialContainer[TransactionPayload, MAX_TRANSACTION_PAYLOAD_FIELDS]( |
| 92 | + nonce=pre.nonce, |
| 93 | + max_fee_per_gas=pre.gasPrice, |
| 94 | + gas=pre.gasLimit, |
| 95 | + to=ExecutionAddress(pre.to) if len(pre.to) > 0 else None, |
| 96 | + value=pre.value, |
| 97 | + input_=pre.data, |
| 98 | + access_list=[AccessTuple( |
| 99 | + address=access_tuple[0], |
| 100 | + storage_keys=access_tuple[1] |
| 101 | + ) for access_tuple in pre.accessList], |
| 102 | + ), |
| 103 | + signature=PartialContainer[TransactionSignature, MAX_TRANSACTION_SIGNATURE_FIELDS]( |
| 104 | + from_=from_, |
| 105 | + ecdsa_signature=ecdsa_signature, |
| 106 | + type_=TRANSACTION_TYPE_EIP2930, |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
| 110 | + if 0xc0 <= type_ <= 0xfe: # Legacy |
| 111 | + pre = decode(pre_bytes, LegacySignedTransaction) |
| 112 | + |
| 113 | + if pre.v not in (27, 28): # EIP-155 |
| 114 | + assert pre.v in (2 * chain_id + 35, 2 * chain_id + 36) |
| 115 | + ecdsa_signature = ecdsa_pack_signature( |
| 116 | + (pre.v & 0x1) == 0, |
| 117 | + pre.r, |
| 118 | + pre.s, |
| 119 | + ) |
| 120 | + from_ = ecdsa_recover_from_address(ecdsa_signature, compute_legacy_sig_hash(pre)) |
| 121 | + |
| 122 | + return SignedTransaction( |
| 123 | + payload=PartialContainer[TransactionPayload, MAX_TRANSACTION_PAYLOAD_FIELDS]( |
| 124 | + nonce=pre.nonce, |
| 125 | + max_fee_per_gas=pre.gasprice, |
| 126 | + gas=pre.startgas, |
| 127 | + to=ExecutionAddress(pre.to) if len(pre.to) > 0 else None, |
| 128 | + value=pre.value, |
| 129 | + input_=pre.data, |
| 130 | + ), |
| 131 | + signature=PartialContainer[TransactionSignature, MAX_TRANSACTION_SIGNATURE_FIELDS]( |
| 132 | + from_=from_, |
| 133 | + ecdsa_signature=ecdsa_signature, |
| 134 | + type_=TRANSACTION_TYPE_LEGACY if (pre.v not in (27, 28)) else None, |
| 135 | + ), |
| 136 | + ) |
| 137 | + |
| 138 | + assert False |
| 139 | + |
| 140 | +class ContractAddressData(Serializable): |
| 141 | + fields = ( |
| 142 | + ('from_', Binary(20, 20)), |
| 143 | + ('nonce', big_endian_int), |
| 144 | + ) |
| 145 | + |
| 146 | +def compute_contract_address(from_: ExecutionAddress, |
| 147 | + nonce: uint64) -> ExecutionAddress: |
| 148 | + return ExecutionAddress(keccak(encode(ContractAddressData( |
| 149 | + from_=from_, |
| 150 | + nonce=nonce, |
| 151 | + )))[12:32]) |
| 152 | + |
| 153 | +def upgrade_rlp_receipt_to_ssz(pre_bytes: bytes, |
| 154 | + prev_cumulative_gas_used: uint64, |
| 155 | + transaction: SignedTransaction) -> Receipt: |
| 156 | + type_ = pre_bytes[0] |
| 157 | + |
| 158 | + if type_ in (0x03, 0x02, 0x01): # EIP-4844, EIP-1559, EIP-2930 |
| 159 | + pre = decode(pre_bytes[1:], RlpReceipt) |
| 160 | + elif 0xc0 <= type_ <= 0xfe: # Legacy |
| 161 | + pre = decode(pre_bytes, RlpReceipt) |
| 162 | + else: |
| 163 | + assert False |
| 164 | + |
| 165 | + if len(pre.post_state_or_status) == 32: |
| 166 | + root = pre.post_state_or_status |
| 167 | + status = None |
| 168 | + else: |
| 169 | + root = None |
| 170 | + status = len(pre.post_state_or_status) > 0 and pre.post_state_or_status[0] != 0 |
| 171 | + |
| 172 | + return Receipt( |
| 173 | + root=root, |
| 174 | + gas_used=pre.cumulative_gas_used - prev_cumulative_gas_used, |
| 175 | + contract_address=compute_contract_address( |
| 176 | + transaction.signature.from_, |
| 177 | + transaction.payload.nonce, |
| 178 | + ) if transaction.payload.to is None else None, |
| 179 | + logs_bloom=pre.logs_bloom, |
| 180 | + logs=[Log( |
| 181 | + address=log[0], |
| 182 | + topics=log[1], |
| 183 | + data=log[2], |
| 184 | + ) for log in pre.logs], |
| 185 | + status=status, |
| 186 | + ) |
| 187 | + |
| 188 | +def upgrade_rlp_receipts_to_ssz(pre_bytes_list: PyList[bytes], |
| 189 | + chain_id: ChainId, |
| 190 | + transactions: PyList[SignedTransaction]) -> PyList[Receipt]: |
| 191 | + receipts = [] |
| 192 | + cumulative_gas_used = 0 |
| 193 | + for i, pre_bytes in enumerate(pre_bytes_list): |
| 194 | + receipt = upgrade_rlp_receipt_to_ssz(pre_bytes, cumulative_gas_used, transactions[i]) |
| 195 | + cumulative_gas_used += receipt.gas_used |
| 196 | + receipts.append(receipt) |
| 197 | + return receipts |
0 commit comments