ethereum.spurious_dragon.transactionsethereum.byzantium.transactions

Transactions are atomic units of work created externally to Ethereum and submitted to be executed. If Ethereum is viewed as a state machine, transactions are the events that move between states.

TX_BASE_COST

Base cost of a transaction in gas units. This is the minimum amount of gas required to execute a transaction.

20
TX_BASE_COST = Uint(21000)

TX_DATA_COST_PER_NON_ZERO

Gas cost per non-zero byte in the transaction data.

26
TX_DATA_COST_PER_NON_ZERO = Uint(68)

TX_DATA_COST_PER_ZERO

Gas cost per zero byte in the transaction data.

31
TX_DATA_COST_PER_ZERO = Uint(4)

TX_CREATE_COST

Additional gas cost for creating a new contract.

36
TX_CREATE_COST = Uint(32000)

Transaction

Atomic operation performed on the block chain.

42
@slotted_freezable
43
@dataclass
class Transaction:

nonce

49
    nonce: U256

gas_price

54
    gas_price: Uint

gas

59
    gas: Uint

to

64
    to: Union[Bytes0, Address]

value

70
    value: U256

data

75
    data: Bytes

v

81
    v: U256

r

86
    r: U256

s

91
    s: U256

validate_transaction

Verifies a transaction.

The gas in a transaction gets used to pay for the intrinsic cost of operations, therefore if there is insufficient gas then it would not be possible to execute a transaction and it will be declared invalid.

Additionally, the nonce of a transaction must not equal or exceed the limit defined in EIP-2681. In practice, defining the limit as 2**64-1 has no impact because sending 2**64-1 transactions is improbable. It's not strictly impossible though, 2**64-1 transactions is the entire capacity of the Ethereum blockchain at 2022 gas limits for a little over 22 years.

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction after validation. It throws an InvalidTransaction exception if the transaction is invalid.

def validate_transaction(tx: Transaction) -> Uint:
98
    """
99
    Verifies a transaction.
100
101
    The gas in a transaction gets used to pay for the intrinsic cost of
102
    operations, therefore if there is insufficient gas then it would not
103
    be possible to execute a transaction and it will be declared invalid.
104
105
    Additionally, the nonce of a transaction must not equal or exceed the
106
    limit defined in [EIP-2681].
107
    In practice, defining the limit as ``2**64-1`` has no impact because
108
    sending ``2**64-1`` transactions is improbable. It's not strictly
109
    impossible though, ``2**64-1`` transactions is the entire capacity of the
110
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
111
112
    This function takes a transaction as a parameter and returns the intrinsic
113
    gas cost of the transaction after validation. It throws an
114
    `InvalidTransaction` exception if the transaction is invalid.
115
116
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
117
    """
118
    intrinsic_gas = calculate_intrinsic_cost(tx)
119
    if intrinsic_gas > tx.gas:
120
        raise InvalidTransaction("Insufficient gas")
121
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
122
        raise InvalidTransaction("Nonce too high")
123
    return intrinsic_gas

calculate_intrinsic_cost

Calculates the gas that is charged before execution is started.

The intrinsic cost of the transaction is charged before execution has begun. Functions/operations in the EVM cost money to execute so this intrinsic cost is for the operations that need to be paid for as part of the transaction. Data transfer, for example, is part of this intrinsic cost. It costs ether to send data over the wire and that ether is accounted for in the intrinsic cost calculated in this function. This intrinsic cost must be calculated and paid for before execution in order for all operations to be implemented.

The intrinsic cost includes:

  1. Base cost (TX_BASE_COST)

  2. Cost for data (zero and non-zero bytes)

  3. Cost for contract creation (if applicable)

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction.

def calculate_intrinsic_cost(tx: Transaction) -> Uint:
127
    """
128
    Calculates the gas that is charged before execution is started.
129
130
    The intrinsic cost of the transaction is charged before execution has
131
    begun. Functions/operations in the EVM cost money to execute so this
132
    intrinsic cost is for the operations that need to be paid for as part of
133
    the transaction. Data transfer, for example, is part of this intrinsic
134
    cost. It costs ether to send data over the wire and that ether is
135
    accounted for in the intrinsic cost calculated in this function. This
136
    intrinsic cost must be calculated and paid for before execution in order
137
    for all operations to be implemented.
138
139
    The intrinsic cost includes:
140
    1. Base cost (`TX_BASE_COST`)
141
    2. Cost for data (zero and non-zero bytes)
142
    3. Cost for contract creation (if applicable)
143
144
    This function takes a transaction as a parameter and returns the intrinsic
145
    gas cost of the transaction.
146
    """
147
    data_cost = Uint(0)
148
149
    for byte in tx.data:
150
        if byte == 0:
151
            data_cost += TX_DATA_COST_PER_ZERO
152
        else:
153
            data_cost += TX_DATA_COST_PER_NON_ZERO
154
155
    if tx.to == Bytes0(b""):
156
        create_cost = TX_CREATE_COST
157
    else:
158
        create_cost = Uint(0)
159
160
    return TX_BASE_COST + data_cost + create_cost

recover_sender

Extracts the sender address from a transaction.

The v, r, and s values are the three parts that make up the signature of a transaction. In order to recover the sender of a transaction the two components needed are the signature (v, r, and s) and the signing hash of the transaction. The sender's public key can be obtained with these two values and therefore the sender address can be retrieved.

This function takes chain_id and a transaction as parameters and returns the address of the sender of the transaction. It raises an InvalidSignatureError if the signature values (r, s, v) are invalid.

def recover_sender(chain_id: U64, ​​tx: Transaction) -> Address:
164
    """
165
    Extracts the sender address from a transaction.
166
167
    The v, r, and s values are the three parts that make up the signature
168
    of a transaction. In order to recover the sender of a transaction the two
169
    components needed are the signature (``v``, ``r``, and ``s``) and the
170
    signing hash of the transaction. The sender's public key can be obtained
171
    with these two values and therefore the sender address can be retrieved.
172
173
    This function takes chain_id and a transaction as parameters and returns
174
    the address of the sender of the transaction. It raises an
175
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
176
    """
177
    v, r, s = tx.v, tx.r, tx.s
178
    if U256(0) >= r or r >= SECP256K1N:
179
        raise InvalidSignatureError("bad r")
180
    if U256(0) >= s or s > SECP256K1N // U256(2):
181
        raise InvalidSignatureError("bad s")
182
183
    if v == 27 or v == 28:
184
        public_key = secp256k1_recover(
185
            r, s, v - U256(27), signing_hash_pre155(tx)
186
        )
187
    else:
188
        chain_id_x2 = U256(chain_id) * U256(2)
189
        if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
190
            raise InvalidSignatureError("bad v")
191
        public_key = secp256k1_recover(
192
            r, s, v - U256(35) - chain_id_x2, signing_hash_155(tx, chain_id)
193
        )
194
195
    return Address(keccak256(public_key)[12:32])

signing_hash_pre155

Compute the hash of a transaction used in a legacy (pre EIP-155) signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction.

def signing_hash_pre155(tx: Transaction) -> Hash32:
199
    """
200
    Compute the hash of a transaction used in a legacy (pre [EIP-155])
201
    signature.
202
203
    This function takes a transaction as a parameter and returns the
204
    signing hash of the transaction.
205
206
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
207
    """
208
    return keccak256(
209
        rlp.encode(
210
            (
211
                tx.nonce,
212
                tx.gas_price,
213
                tx.gas,
214
                tx.to,
215
                tx.value,
216
                tx.data,
217
            )
218
        )
219
    )

signing_hash_155

Compute the hash of a transaction used in a EIP-155 signature.

This function takes a transaction and chain ID as parameters and returns the hash of the transaction used in a EIP-155 signature.

def signing_hash_155(tx: Transaction, ​​chain_id: U64) -> Hash32:
223
    """
224
    Compute the hash of a transaction used in a [EIP-155] signature.
225
226
    This function takes a transaction and chain ID as parameters and returns
227
    the hash of the transaction used in a [EIP-155] signature.
228
229
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
230
    """
231
    return keccak256(
232
        rlp.encode(
233
            (
234
                tx.nonce,
235
                tx.gas_price,
236
                tx.gas,
237
                tx.to,
238
                tx.value,
239
                tx.data,
240
                chain_id,
241
                Uint(0),
242
                Uint(0),
243
            )
244
        )
245
    )

get_transaction_hash

Compute the hash of a transaction.

This function takes a transaction as a parameter and returns the hash of the transaction.

def get_transaction_hash(tx: Transaction) -> Hash32:
249
    """
250
    Compute the hash of a transaction.
251
252
    This function takes a transaction as a parameter and returns the
253
    hash of the transaction.
254
    """
255
    return keccak256(rlp.encode(tx))