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

20
TX_BASE_COST = 21000

TX_DATA_COST_PER_NON_ZERO

21
TX_DATA_COST_PER_NON_ZERO = 68

TX_DATA_COST_PER_ZERO

22
TX_DATA_COST_PER_ZERO = 4

TX_CREATE_COST

23
TX_CREATE_COST = 32000

Transaction

Atomic operation performed on the block chain.

26
@slotted_freezable
27
@dataclass
class Transaction:

nonce

33
    nonce: U256

gas_price

34
    gas_price: Uint

gas

35
    gas: Uint

to

36
    to: Union[Bytes0, Address]

value

37
    value: U256

data

38
    data: Bytes

v

39
    v: U256

r

40
    r: U256

s

41
    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 <https://eips.ethereum.org/EIPS/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.

Parameters

tx : Transaction to validate.

Returns

verified : bool True if the transaction can be executed, or False otherwise.

def validate_transaction(tx: Transaction) -> bool:
45
    """
46
    Verifies a transaction.
47
48
    The gas in a transaction gets used to pay for the intrinsic cost of
49
    operations, therefore if there is insufficient gas then it would not
50
    be possible to execute a transaction and it will be declared invalid.
51
52
    Additionally, the nonce of a transaction must not equal or exceed the
53
    limit defined in `EIP-2681 <https://eips.ethereum.org/EIPS/eip-2681>`_.
54
    In practice, defining the limit as ``2**64-1`` has no impact because
55
    sending ``2**64-1`` transactions is improbable. It's not strictly
56
    impossible though, ``2**64-1`` transactions is the entire capacity of the
57
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
58
59
    Parameters
60
    ----------
61
    tx :
62
        Transaction to validate.
63
64
    Returns
65
    -------
66
    verified : `bool`
67
        True if the transaction can be executed, or False otherwise.
68
    """
69
    if calculate_intrinsic_cost(tx) > Uint(tx.gas):
70
        return False
71
    if tx.nonce >= U256(U64.MAX_VALUE):
72
        return False
73
    return True

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.

Parameters

tx : Transaction to compute the intrinsic cost of.

Returns

verified : ethereum.base_types.Uint The intrinsic cost of the transaction.

def calculate_intrinsic_cost(tx: Transaction) -> Uint:
77
    """
78
    Calculates the gas that is charged before execution is started.
79
80
    The intrinsic cost of the transaction is charged before execution has
81
    begun. Functions/operations in the EVM cost money to execute so this
82
    intrinsic cost is for the operations that need to be paid for as part of
83
    the transaction. Data transfer, for example, is part of this intrinsic
84
    cost. It costs ether to send data over the wire and that ether is
85
    accounted for in the intrinsic cost calculated in this function. This
86
    intrinsic cost must be calculated and paid for before execution in order
87
    for all operations to be implemented.
88
89
    Parameters
90
    ----------
91
    tx :
92
        Transaction to compute the intrinsic cost of.
93
94
    Returns
95
    -------
96
    verified : `ethereum.base_types.Uint`
97
        The intrinsic cost of the transaction.
98
    """
99
    data_cost = 0
100
101
    for byte in tx.data:
102
        if byte == 0:
103
            data_cost += TX_DATA_COST_PER_ZERO
104
        else:
105
            data_cost += TX_DATA_COST_PER_NON_ZERO
106
107
    if tx.to == Bytes0(b""):
108
        create_cost = TX_CREATE_COST
109
    else:
110
        create_cost = 0
111
112
    return Uint(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.

Parameters

tx : Transaction of interest. chain_id : ID of the executing chain.

Returns

sender : ethereum.fork_types.Address The address of the account that signed the transaction.

def recover_sender(chain_id: U64, ​​tx: Transaction) -> Address:
116
    """
117
    Extracts the sender address from a transaction.
118
119
    The v, r, and s values are the three parts that make up the signature
120
    of a transaction. In order to recover the sender of a transaction the two
121
    components needed are the signature (``v``, ``r``, and ``s``) and the
122
    signing hash of the transaction. The sender's public key can be obtained
123
    with these two values and therefore the sender address can be retrieved.
124
125
    Parameters
126
    ----------
127
    tx :
128
        Transaction of interest.
129
    chain_id :
130
        ID of the executing chain.
131
132
    Returns
133
    -------
134
    sender : `ethereum.fork_types.Address`
135
        The address of the account that signed the transaction.
136
    """
137
    v, r, s = tx.v, tx.r, tx.s
138
    if U256(0) >= r or r >= SECP256K1N:
139
        raise InvalidSignatureError("bad r")
140
    if U256(0) >= s or s > SECP256K1N // U256(2):
141
        raise InvalidSignatureError("bad s")
142
143
    if v == 27 or v == 28:
144
        public_key = secp256k1_recover(
145
            r, s, v - U256(27), signing_hash_pre155(tx)
146
        )
147
    else:
148
        chain_id_x2 = U256(chain_id) * U256(2)
149
        if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
150
            raise InvalidSignatureError("bad v")
151
        public_key = secp256k1_recover(
152
            r, s, v - U256(35) - chain_id_x2, signing_hash_155(tx, chain_id)
153
        )
154
    return Address(keccak256(public_key)[12:32])

signing_hash_pre155

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

Parameters

tx : Transaction of interest.

Returns

hash : ethereum.crypto.hash.Hash32 Hash of the transaction.

def signing_hash_pre155(tx: Transaction) -> Hash32:
158
    """
159
    Compute the hash of a transaction used in a legacy (pre EIP 155) signature.
160
161
    Parameters
162
    ----------
163
    tx :
164
        Transaction of interest.
165
166
    Returns
167
    -------
168
    hash : `ethereum.crypto.hash.Hash32`
169
        Hash of the transaction.
170
    """
171
    return keccak256(
172
        rlp.encode(
173
            (
174
                tx.nonce,
175
                tx.gas_price,
176
                tx.gas,
177
                tx.to,
178
                tx.value,
179
                tx.data,
180
            )
181
        )
182
    )

signing_hash_155

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

Parameters

tx : Transaction of interest. chain_id : The id of the current chain.

Returns

hash : ethereum.crypto.hash.Hash32 Hash of the transaction.

def signing_hash_155(tx: Transaction, ​​chain_id: U64) -> Hash32:
186
    """
187
    Compute the hash of a transaction used in a EIP 155 signature.
188
189
    Parameters
190
    ----------
191
    tx :
192
        Transaction of interest.
193
    chain_id :
194
        The id of the current chain.
195
196
    Returns
197
    -------
198
    hash : `ethereum.crypto.hash.Hash32`
199
        Hash of the transaction.
200
    """
201
    return keccak256(
202
        rlp.encode(
203
            (
204
                tx.nonce,
205
                tx.gas_price,
206
                tx.gas,
207
                tx.to,
208
                tx.value,
209
                tx.data,
210
                chain_id,
211
                Uint(0),
212
                Uint(0),
213
            )
214
        )
215
    )