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

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

Transaction

Atomic operation performed on the block chain.

25
@slotted_freezable
26
@dataclass
class Transaction:

nonce

32
    nonce: U256

gas_price

33
    gas_price: Uint

gas

34
    gas: Uint

to

35
    to: Union[Bytes0, Address]

value

36
    value: U256

data

37
    data: Bytes

v

38
    v: U256

r

39
    r: U256

s

40
    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:
44
    """
45
    Verifies a transaction.
46
47
    The gas in a transaction gets used to pay for the intrinsic cost of
48
    operations, therefore if there is insufficient gas then it would not
49
    be possible to execute a transaction and it will be declared invalid.
50
51
    Additionally, the nonce of a transaction must not equal or exceed the
52
    limit defined in `EIP-2681 <https://eips.ethereum.org/EIPS/eip-2681>`_.
53
    In practice, defining the limit as ``2**64-1`` has no impact because
54
    sending ``2**64-1`` transactions is improbable. It's not strictly
55
    impossible though, ``2**64-1`` transactions is the entire capacity of the
56
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
57
58
    Parameters
59
    ----------
60
    tx :
61
        Transaction to validate.
62
63
    Returns
64
    -------
65
    verified : `bool`
66
        True if the transaction can be executed, or False otherwise.
67
    """
68
    if calculate_intrinsic_cost(tx) > Uint(tx.gas):
69
        return False
70
    if tx.nonce >= U256(U64.MAX_VALUE):
71
        return False
72
    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:
76
    """
77
    Calculates the gas that is charged before execution is started.
78
79
    The intrinsic cost of the transaction is charged before execution has
80
    begun. Functions/operations in the EVM cost money to execute so this
81
    intrinsic cost is for the operations that need to be paid for as part of
82
    the transaction. Data transfer, for example, is part of this intrinsic
83
    cost. It costs ether to send data over the wire and that ether is
84
    accounted for in the intrinsic cost calculated in this function. This
85
    intrinsic cost must be calculated and paid for before execution in order
86
    for all operations to be implemented.
87
88
    Parameters
89
    ----------
90
    tx :
91
        Transaction to compute the intrinsic cost of.
92
93
    Returns
94
    -------
95
    verified : `ethereum.base_types.Uint`
96
        The intrinsic cost of the transaction.
97
    """
98
    data_cost = 0
99
100
    for byte in tx.data:
101
        if byte == 0:
102
            data_cost += TX_DATA_COST_PER_ZERO
103
        else:
104
            data_cost += TX_DATA_COST_PER_NON_ZERO
105
106
    return Uint(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.

Parameters

tx : Transaction of interest.

Returns

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

def recover_sender(tx: Transaction) -> Address:
110
    """
111
    Extracts the sender address from a transaction.
112
113
    The v, r, and s values are the three parts that make up the signature
114
    of a transaction. In order to recover the sender of a transaction the two
115
    components needed are the signature (``v``, ``r``, and ``s``) and the
116
    signing hash of the transaction. The sender's public key can be obtained
117
    with these two values and therefore the sender address can be retrieved.
118
119
    Parameters
120
    ----------
121
    tx :
122
        Transaction of interest.
123
124
    Returns
125
    -------
126
    sender : `ethereum.fork_types.Address`
127
        The address of the account that signed the transaction.
128
    """
129
    v, r, s = tx.v, tx.r, tx.s
130
    if v != 27 and v != 28:
131
        raise InvalidSignatureError("bad v")
132
    if U256(0) >= r or r >= SECP256K1N:
133
        raise InvalidSignatureError("bad r")
134
    if U256(0) >= s or s >= SECP256K1N:
135
        raise InvalidSignatureError("bad s")
136
137
    public_key = secp256k1_recover(r, s, v - U256(27), signing_hash(tx))
138
    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.

Parameters

tx : Transaction of interest.

Returns

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

def signing_hash(tx: Transaction) -> Hash32:
142
    """
143
    Compute the hash of a transaction used in the signature.
144
145
    The values that are used to compute the signing hash set the rules for a
146
    transaction. For example, signing over the gas sets a limit for the
147
    amount of money that is allowed to be pulled out of the sender's account.
148
149
    Parameters
150
    ----------
151
    tx :
152
        Transaction of interest.
153
154
    Returns
155
    -------
156
    hash : `ethereum.crypto.hash.Hash32`
157
        Hash of the transaction.
158
    """
159
    return keccak256(
160
        rlp.encode(
161
            (
162
                tx.nonce,
163
                tx.gas_price,
164
                tx.gas,
165
                tx.to,
166
                tx.value,
167
                tx.data,
168
            )
169
        )
170
    )