ethereum.frontier.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)

Transaction

Atomic operation performed on the block chain.

37
@slotted_freezable
38
@dataclass
class Transaction:

nonce

44
    nonce: U256

gas_price

49
    gas_price: Uint

gas

54
    gas: Uint

to

59
    to: Union[Bytes0, Address]

value

65
    value: U256

data

70
    data: Bytes

v

76
    v: U256

r

81
    r: U256

s

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

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

def calculate_intrinsic_cost(tx: Transaction) -> Uint:
122
    """
123
    Calculates the gas that is charged before execution is started.
124
125
    The intrinsic cost of the transaction is charged before execution has
126
    begun. Functions/operations in the EVM cost money to execute so this
127
    intrinsic cost is for the operations that need to be paid for as part of
128
    the transaction. Data transfer, for example, is part of this intrinsic
129
    cost. It costs ether to send data over the wire and that ether is
130
    accounted for in the intrinsic cost calculated in this function. This
131
    intrinsic cost must be calculated and paid for before execution in order
132
    for all operations to be implemented.
133
134
    The intrinsic cost includes:
135
    1. Base cost (`TX_BASE_COST`)
136
    2. Cost for data (zero and non-zero bytes)
137
138
    This function takes a transaction as a parameter and returns the intrinsic
139
    gas cost of the transaction.
140
    """
141
    data_cost = Uint(0)
142
143
    for byte in tx.data:
144
        if byte == 0:
145
            data_cost += TX_DATA_COST_PER_ZERO
146
        else:
147
            data_cost += TX_DATA_COST_PER_NON_ZERO
148
149
    return TX_BASE_COST + data_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 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(tx: Transaction) -> Address:
153
    """
154
    Extracts the sender address from a transaction.
155
156
    The v, r, and s values are the three parts that make up the signature
157
    of a transaction. In order to recover the sender of a transaction the two
158
    components needed are the signature (``v``, ``r``, and ``s``) and the
159
    signing hash of the transaction. The sender's public key can be obtained
160
    with these two values and therefore the sender address can be retrieved.
161
162
    This function takes a transaction as a parameter and returns
163
    the address of the sender of the transaction. It raises an
164
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
165
    """
166
    v, r, s = tx.v, tx.r, tx.s
167
    if v != 27 and v != 28:
168
        raise InvalidSignatureError("bad v")
169
    if U256(0) >= r or r >= SECP256K1N:
170
        raise InvalidSignatureError("bad r")
171
    if U256(0) >= s or s >= SECP256K1N:
172
        raise InvalidSignatureError("bad s")
173
174
    public_key = secp256k1_recover(r, s, v - U256(27), signing_hash(tx))
175
    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:
179
    """
180
    Compute the hash of a transaction used in the signature.
181
182
    The values that are used to compute the signing hash set the rules for a
183
    transaction. For example, signing over the gas sets a limit for the
184
    amount of money that is allowed to be pulled out of the sender's account.
185
186
    This function takes a transaction as a parameter and returns the
187
    signing hash of the transaction.
188
    """
189
    return keccak256(
190
        rlp.encode(
191
            (
192
                tx.nonce,
193
                tx.gas_price,
194
                tx.gas,
195
                tx.to,
196
                tx.value,
197
                tx.data,
198
            )
199
        )
200
    )

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:
204
    """
205
    Compute the hash of a transaction.
206
207
    This function takes a transaction as a parameter and returns the
208
    hash of the transaction.
209
    """
210
    return keccak256(rlp.encode(tx))