ethereum.forks.tangerine_whistle.transactionsethereum.forks.spurious_dragon.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.

GAS_TX_BASE

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

24
GAS_TX_BASE = Uint(21000)

GAS_TX_DATA_PER_NON_ZERO

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

30
GAS_TX_DATA_PER_NON_ZERO = Uint(68)

GAS_TX_DATA_PER_ZERO

Gas cost per zero byte in the transaction data.

35
GAS_TX_DATA_PER_ZERO = Uint(4)

GAS_TX_CREATE

Additional gas cost for creating a new contract.

40
GAS_TX_CREATE = Uint(32000)

Transaction

Atomic operation performed on the block chain.

46
@slotted_freezable
47
@dataclass
class Transaction:

nonce

A scalar value equal to the number of transactions sent by the sender.

53
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

58
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

63
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

68
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

74
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

79
    data: Bytes

v

The recovery id of the signature.

85
    v: U256

r

The first part of the signature.

90
    r: U256

s

The second part of the signature.

95
    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 InsufficientTransactionGasError exception if the transaction does not provide enough gas to cover the intrinsic cost, and a NonceOverflowError exception if the nonce is greater than 2**64 - 2.

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

  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:
133
    """
134
    Calculates the gas that is charged before execution is started.
135
136
    The intrinsic cost of the transaction is charged before execution has
137
    begun. Functions/operations in the EVM cost money to execute so this
138
    intrinsic cost is for the operations that need to be paid for as part of
139
    the transaction. Data transfer, for example, is part of this intrinsic
140
    cost. It costs ether to send data over the wire and that ether is
141
    accounted for in the intrinsic cost calculated in this function. This
142
    intrinsic cost must be calculated and paid for before execution in order
143
    for all operations to be implemented.
144
145
    The intrinsic cost includes:
146
    1. Base cost (`GAS_TX_BASE`)
147
    2. Cost for data (zero and non-zero bytes)
148
    3. Cost for contract creation (if applicable)
149
150
    This function takes a transaction as a parameter and returns the intrinsic
151
    gas cost of the transaction.
152
    """
153
    num_zeros = Uint(tx.data.count(0))
154
    num_non_zeros = ulen(tx.data) - num_zeros
155
    data_cost = (
156
        num_zeros * GAS_TX_DATA_PER_ZERO
157
        + num_non_zeros * GAS_TX_DATA_PER_NON_ZERO
158
    )
159
160
    if tx.to == Bytes0(b""):
161
        create_cost = GAS_TX_CREATE
162
    else:
163
        create_cost = Uint(0)
164
165
    return GAS_TX_BASE + 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 a transaction as a parameter and returnsThis 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:
169
    """
170
    Extracts the sender address from a transaction.
171
172
    The v, r, and s values are the three parts that make up the signature
173
    of a transaction. In order to recover the sender of a transaction the two
174
    components needed are the signature (``v``, ``r``, and ``s``) and the
175
    signing hash of the transaction. The sender's public key can be obtained
176
    with these two values and therefore the sender address can be retrieved.
177
178
    This function takes a transaction as a parameter and returns
178
    This function takes chain_id and a transaction as parameters and returns
179
    the address of the sender of the transaction. It raises an
180
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
181
    """
182
    v, r, s = tx.v, tx.r, tx.s
183
    if v != 27 and v != 28:
184
        raise InvalidSignatureError("bad v")
183
    if U256(0) >= r or r >= SECP256K1N:
184
        raise InvalidSignatureError("bad r")
185
    if U256(0) >= s or s > SECP256K1N // U256(2):
186
        raise InvalidSignatureError("bad s")
187
190
    public_key = secp256k1_recover(r, s, v - U256(27), signing_hash(tx))
188
    if v == 27 or v == 28:
189
        public_key = secp256k1_recover(
190
            r, s, v - U256(27), signing_hash_pre155(tx)
191
        )
192
    else:
193
        chain_id_x2 = U256(chain_id) * U256(2)
194
        if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
195
            raise InvalidSignatureError("bad v")
196
        public_key = secp256k1_recover(
197
            r, s, v - U256(35) - chain_id_x2, signing_hash_155(tx, chain_id)
198
        )
199
200
    return Address(keccak256(public_key)[12:32])

signing_hash

Compute the hash of a transaction used in the signature.

The values that are used to compute the signing hash set the rules for a transaction. For example, signing over the gas sets a limit for the amount of money that is allowed to be pulled out of the sender's account.

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

def signing_hash(tx: Transaction) -> Hash32:
195
    """
196
    Compute the hash of a transaction used in the signature.
197
198
    The values that are used to compute the signing hash set the rules for a
199
    transaction. For example, signing over the gas sets a limit for the
200
    amount of money that is allowed to be pulled out of the sender's account.
201
202
    This function takes a transaction as a parameter and returns the
203
    signing hash of the transaction.
204
    """
205
    return keccak256(
206
        rlp.encode(
207
            (
208
                tx.nonce,
209
                tx.gas_price,
210
                tx.gas,
211
                tx.to,
212
                tx.value,
213
                tx.data,
214
            )
215
        )
216
    )

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:
204
    """
205
    Compute the hash of a transaction used in a legacy (pre [EIP-155])
206
    signature.
207
208
    This function takes a transaction as a parameter and returns the
209
    signing hash of the transaction.
210
211
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
212
    """
213
    return keccak256(
214
        rlp.encode(
215
            (
216
                tx.nonce,
217
                tx.gas_price,
218
                tx.gas,
219
                tx.to,
220
                tx.value,
221
                tx.data,
222
            )
223
        )
224
    )

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:
228
    """
229
    Compute the hash of a transaction used in a [EIP-155] signature.
230
231
    This function takes a transaction and chain ID as parameters and returns
232
    the hash of the transaction used in a [EIP-155] signature.
233
234
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
235
    """
236
    return keccak256(
237
        rlp.encode(
238
            (
239
                tx.nonce,
240
                tx.gas_price,
241
                tx.gas,
242
                tx.to,
243
                tx.value,
244
                tx.data,
245
                chain_id,
246
                Uint(0),
247
                Uint(0),
248
            )
249
        )
250
    )

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:
254
    """
255
    Compute the hash of a transaction.
256
257
    This function takes a transaction as a parameter and returns the
258
    hash of the transaction.
259
    """
260
    return keccak256(rlp.encode(tx))