ethereum.dao_fork.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.
Returns
sender : ethereum.fork_types.Address
The address of the account that signed the transaction.
def recover_sender(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 |
|
130 | Returns |
131 | ------- |
132 | sender : `ethereum.fork_types.Address` |
133 | The address of the account that signed the transaction. |
134 | """ |
135 | v, r, s = tx.v, tx.r, tx.s |
136 | if v != 27 and v != 28: |
137 | raise InvalidSignatureError("bad v") |
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 | public_key = secp256k1_recover(r, s, v - U256(27), signing_hash(tx)) |
144 | 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:
148 | """ |
---|---|
149 | Compute the hash of a transaction used in the signature. |
150 |
|
151 | The values that are used to compute the signing hash set the rules for a |
152 | transaction. For example, signing over the gas sets a limit for the |
153 | amount of money that is allowed to be pulled out of the sender's account. |
154 |
|
155 | Parameters |
156 | ---------- |
157 | tx : |
158 | Transaction of interest. |
159 |
|
160 | Returns |
161 | ------- |
162 | hash : `ethereum.crypto.hash.Hash32` |
163 | Hash of the transaction. |
164 | """ |
165 | return keccak256( |
166 | rlp.encode( |
167 | ( |
168 | tx.nonce, |
169 | tx.gas_price, |
170 | tx.gas, |
171 | tx.to, |
172 | tx.value, |
173 | tx.data, |
174 | ) |
175 | ) |
176 | ) |