ethereum.forks.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.

Transaction

Atomic operation performed on the block chain.

24
@slotted_freezable
25
@dataclass
class Transaction:

nonce

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

31
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

36
    gas_price: Uint

gas

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

41
    gas: Uint

to

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

46
    to: Bytes0 | Address

value

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

52
    value: U256

data

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

57
    data: Bytes

v

The recovery id of the signature.

63
    v: U256

r

The first part of the signature.

68
    r: U256

s

The second part of the signature.

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

  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:
111
    """
112
    Calculates the gas that is charged before execution is started.
113
114
    The intrinsic cost of the transaction is charged before execution has
115
    begun. Functions/operations in the EVM cost money to execute so this
116
    intrinsic cost is for the operations that need to be paid for as part of
117
    the transaction. Data transfer, for example, is part of this intrinsic
118
    cost. It costs ether to send data over the wire and that ether is
119
    accounted for in the intrinsic cost calculated in this function. This
120
    intrinsic cost must be calculated and paid for before execution in order
121
    for all operations to be implemented.
122
123
    The intrinsic cost includes:
124
    1. Base cost (`TX_BASE`)
125
    2. Cost for data (zero and non-zero bytes)
126
127
    This function takes a transaction as a parameter and returns the intrinsic
128
    gas cost of the transaction.
129
    """
130
    from .vm.gas import GasCosts
131
132
    num_zeros = Uint(tx.data.count(0))
133
    num_non_zeros = ulen(tx.data) - num_zeros
134
    data_cost = (
135
        num_zeros * GasCosts.TX_DATA_PER_ZERO
136
        + num_non_zeros * GasCosts.TX_DATA_PER_NON_ZERO
137
    )
138
139
    return GasCosts.TX_BASE + 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:
143
    """
144
    Extracts the sender address from a transaction.
145
146
    The v, r, and s values are the three parts that make up the signature
147
    of a transaction. In order to recover the sender of a transaction the two
148
    components needed are the signature (``v``, ``r``, and ``s``) and the
149
    signing hash of the transaction. The sender's public key can be obtained
150
    with these two values and therefore the sender address can be retrieved.
151
152
    This function takes a transaction as a parameter and returns
153
    the address of the sender of the transaction. It raises an
154
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
155
    """
156
    v, r, s = tx.v, tx.r, tx.s
157
    if v != 27 and v != 28:
158
        raise InvalidSignatureError("bad v")
159
    if U256(0) >= r or r >= SECP256K1N:
160
        raise InvalidSignatureError("bad r")
161
    if U256(0) >= s or s >= SECP256K1N:
162
        raise InvalidSignatureError("bad s")
163
164
    public_key = secp256k1_recover(r, s, v - U256(27), signing_hash(tx))
165
    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:
169
    """
170
    Compute the hash of a transaction used in the signature.
171
172
    The values that are used to compute the signing hash set the rules for a
173
    transaction. For example, signing over the gas sets a limit for the
174
    amount of money that is allowed to be pulled out of the sender's account.
175
176
    This function takes a transaction as a parameter and returns the
177
    signing hash of the transaction.
178
    """
179
    return keccak256(
180
        rlp.encode(
181
            (
182
                tx.nonce,
183
                tx.gas_price,
184
                tx.gas,
185
                tx.to,
186
                tx.value,
187
                tx.data,
188
            )
189
        )
190
    )

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:
194
    """
195
    Compute the hash of a transaction.
196
197
    This function takes a transaction as a parameter and returns the
198
    hash of the transaction.
199
    """
200
    return keccak256(rlp.encode(tx))