|
34 | 34 | import org.hyperledger.besu.ethereum.vm.BlockHashLookup; |
35 | 35 | import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup; |
36 | 36 | import org.hyperledger.besu.evm.gascalculator.GasCalculator; |
| 37 | +import org.hyperledger.besu.evm.account.EvmAccount; |
| 38 | +import org.hyperledger.besu.evm.gascalculator.GasCalculator; |
37 | 39 | import org.hyperledger.besu.evm.worldstate.WorldUpdater; |
38 | 40 | import org.hyperledger.besu.plugin.data.TransactionSelectionResult; |
39 | 41 | import org.hyperledger.besu.plugin.data.TransactionType; |
40 | 42 | import org.hyperledger.besu.plugin.services.txselection.TransactionSelector; |
41 | 43 | import org.hyperledger.besu.plugin.services.txselection.TransactionSelectorFactory; |
42 | 44 |
|
43 | 45 | import java.util.ArrayList; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.HashMap; |
44 | 48 | import java.util.EnumMap; |
45 | 49 | import java.util.List; |
46 | 50 | import java.util.Map; |
|
50 | 54 | import java.util.function.Function; |
51 | 55 | import java.util.function.Supplier; |
52 | 56 | import java.util.stream.Collectors; |
| 57 | +import java.util.stream.Stream; |
53 | 58 |
|
54 | 59 | import com.google.common.collect.Lists; |
55 | 60 | import org.slf4j.Logger; |
|
76 | 81 | * not cleared between executions of buildTransactionListForBlock(). |
77 | 82 | */ |
78 | 83 | public class BlockTransactionSelector { |
79 | | - public static class TransactionValidationResult { |
80 | | - private final Transaction transaction; |
81 | | - private final ValidationResult<TransactionInvalidReason> validationResult; |
82 | | - |
83 | | - public TransactionValidationResult( |
84 | | - final Transaction transaction, |
85 | | - final ValidationResult<TransactionInvalidReason> validationResult) { |
86 | | - this.transaction = transaction; |
87 | | - this.validationResult = validationResult; |
88 | | - } |
89 | | - |
90 | | - public Transaction getTransaction() { |
91 | | - return transaction; |
92 | | - } |
93 | | - |
94 | | - public ValidationResult<TransactionInvalidReason> getValidationResult() { |
95 | | - return validationResult; |
96 | | - } |
97 | | - |
98 | | - @Override |
99 | | - public boolean equals(final Object o) { |
100 | | - if (this == o) { |
101 | | - return true; |
102 | | - } |
103 | | - if (o == null || getClass() != o.getClass()) { |
104 | | - return false; |
105 | | - } |
106 | | - TransactionValidationResult that = (TransactionValidationResult) o; |
107 | | - return Objects.equals(transaction, that.transaction) |
108 | | - && Objects.equals(validationResult, that.validationResult); |
109 | | - } |
110 | | - |
111 | | - @Override |
112 | | - public int hashCode() { |
113 | | - return Objects.hash(transaction, validationResult); |
114 | | - } |
115 | | - } |
116 | | - |
117 | | - public static class TransactionSelectionResults { |
118 | | - private final List<Transaction> transactions = Lists.newArrayList(); |
119 | | - private final Map<TransactionType, List<Transaction>> transactionsByType = |
120 | | - new EnumMap<>(TransactionType.class); |
121 | | - private final List<TransactionReceipt> receipts = Lists.newArrayList(); |
122 | | - private final List<TransactionValidationResult> invalidTransactions = Lists.newArrayList(); |
123 | | - private long cumulativeGasUsed = 0; |
124 | | - private long cumulativeDataGasUsed = 0; |
125 | | - |
126 | | - private void update( |
127 | | - final Transaction transaction, |
128 | | - final TransactionReceipt receipt, |
129 | | - final long gasUsed, |
130 | | - final long dataGasUsed) { |
131 | | - transactions.add(transaction); |
132 | | - transactionsByType |
133 | | - .computeIfAbsent(transaction.getType(), type -> new ArrayList<>()) |
134 | | - .add(transaction); |
135 | | - receipts.add(receipt); |
136 | | - cumulativeGasUsed += gasUsed; |
137 | | - cumulativeDataGasUsed += dataGasUsed; |
138 | | - LOG.atTrace() |
139 | | - .setMessage( |
140 | | - "New selected transaction {}, total transactions {}, cumulative gas used {}, cumulative data gas used {}") |
141 | | - .addArgument(transaction::toTraceLog) |
142 | | - .addArgument(transactions::size) |
143 | | - .addArgument(cumulativeGasUsed) |
144 | | - .addArgument(cumulativeDataGasUsed) |
145 | | - .log(); |
146 | | - } |
147 | | - |
148 | | - private void updateWithInvalidTransaction( |
149 | | - final Transaction transaction, |
150 | | - final ValidationResult<TransactionInvalidReason> validationResult) { |
151 | | - invalidTransactions.add(new TransactionValidationResult(transaction, validationResult)); |
152 | | - } |
153 | | - |
154 | | - public List<Transaction> getTransactions() { |
155 | | - return transactions; |
156 | | - } |
157 | | - |
158 | | - public List<Transaction> getTransactionsByType(final TransactionType type) { |
159 | | - return transactionsByType.getOrDefault(type, List.of()); |
160 | | - } |
161 | | - |
162 | | - public List<TransactionReceipt> getReceipts() { |
163 | | - return receipts; |
164 | | - } |
165 | | - |
166 | | - public long getCumulativeGasUsed() { |
167 | | - return cumulativeGasUsed; |
168 | | - } |
169 | | - |
170 | | - public long getCumulativeDataGasUsed() { |
171 | | - return cumulativeDataGasUsed; |
172 | | - } |
173 | | - |
174 | | - public List<TransactionValidationResult> getInvalidTransactions() { |
175 | | - return invalidTransactions; |
176 | | - } |
177 | | - |
178 | | - @Override |
179 | | - public boolean equals(final Object o) { |
180 | | - if (this == o) { |
181 | | - return true; |
182 | | - } |
183 | | - if (o == null || getClass() != o.getClass()) { |
184 | | - return false; |
185 | | - } |
186 | | - TransactionSelectionResults that = (TransactionSelectionResults) o; |
187 | | - return cumulativeGasUsed == that.cumulativeGasUsed |
188 | | - && cumulativeDataGasUsed == that.cumulativeDataGasUsed |
189 | | - && transactions.equals(that.transactions) |
190 | | - && receipts.equals(that.receipts) |
191 | | - && invalidTransactions.equals(that.invalidTransactions); |
192 | | - } |
193 | | - |
194 | | - @Override |
195 | | - public int hashCode() { |
196 | | - return Objects.hash( |
197 | | - transactions, receipts, invalidTransactions, cumulativeGasUsed, cumulativeDataGasUsed); |
198 | | - } |
199 | | - |
200 | | - public String toTraceLog() { |
201 | | - return "cumulativeGasUsed=" |
202 | | - + cumulativeGasUsed |
203 | | - + ", cumulativeDataGasUsed=" |
204 | | - + cumulativeDataGasUsed |
205 | | - + ", transactions=" |
206 | | - + transactions.stream().map(Transaction::toTraceLog).collect(Collectors.joining("; ")); |
207 | | - } |
208 | | - } |
209 | 84 |
|
210 | 85 | private static final Logger LOG = LoggerFactory.getLogger(BlockTransactionSelector.class); |
211 | 86 |
|
@@ -407,7 +282,13 @@ private List<org.hyperledger.besu.plugin.data.Log> getLogs(final List<Log> logs) |
407 | 282 |
|
408 | 283 | private boolean transactionDataPriceBelowMin(final Transaction transaction) { |
409 | 284 | if (transaction.getType().supportsBlob()) { |
410 | | - if (transaction.getMaxFeePerDataGas().orElseThrow().lessThan(dataGasPrice)) { |
| 285 | + if (transaction |
| 286 | + .getMaxFeePerDataGas() |
| 287 | + .orElseThrow() |
| 288 | + .lessThan( |
| 289 | + feeMarket |
| 290 | + .getTransactionPriceCalculator() |
| 291 | + .dataPrice(transaction, processableBlockHeader))) { |
411 | 292 | return true; |
412 | 293 | } |
413 | 294 | } |
@@ -477,7 +358,6 @@ private void updateTransactionResultTracking( |
477 | 358 | final Transaction transaction, final TransactionProcessingResult result) { |
478 | 359 |
|
479 | 360 | final long gasUsedByTransaction = transaction.getGasLimit() - result.getGasRemaining(); |
480 | | - |
481 | 361 | final long cumulativeGasUsed = |
482 | 362 | transactionSelectionResult.getCumulativeGasUsed() + gasUsedByTransaction; |
483 | 363 |
|
@@ -525,4 +405,140 @@ private boolean blockOccupancyAboveThreshold() { |
525 | 405 |
|
526 | 406 | return occupancyRatio >= minBlockOccupancyRatio; |
527 | 407 | } |
| 408 | + |
| 409 | + public static class TransactionValidationResult { |
| 410 | + private final Transaction transaction; |
| 411 | + private final ValidationResult<TransactionInvalidReason> validationResult; |
| 412 | + |
| 413 | + public TransactionValidationResult( |
| 414 | + final Transaction transaction, |
| 415 | + final ValidationResult<TransactionInvalidReason> validationResult) { |
| 416 | + this.transaction = transaction; |
| 417 | + this.validationResult = validationResult; |
| 418 | + } |
| 419 | + |
| 420 | + public Transaction getTransaction() { |
| 421 | + return transaction; |
| 422 | + } |
| 423 | + |
| 424 | + public ValidationResult<TransactionInvalidReason> getValidationResult() { |
| 425 | + return validationResult; |
| 426 | + } |
| 427 | + |
| 428 | + @Override |
| 429 | + public boolean equals(final Object o) { |
| 430 | + if (this == o) { |
| 431 | + return true; |
| 432 | + } |
| 433 | + if (o == null || getClass() != o.getClass()) { |
| 434 | + return false; |
| 435 | + } |
| 436 | + TransactionValidationResult that = (TransactionValidationResult) o; |
| 437 | + return Objects.equals(transaction, that.transaction) |
| 438 | + && Objects.equals(validationResult, that.validationResult); |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + public int hashCode() { |
| 443 | + return Objects.hash(transaction, validationResult); |
| 444 | + } |
| 445 | + } |
| 446 | + |
| 447 | + public static class TransactionSelectionResults { |
| 448 | + |
| 449 | + private final Map<TransactionType, List<Transaction>> transactionsByType = new HashMap<>(); |
| 450 | + private final List<TransactionReceipt> receipts = Lists.newArrayList(); |
| 451 | + private final List<TransactionValidationResult> invalidTransactions = Lists.newArrayList(); |
| 452 | + private long cumulativeGasUsed = 0; |
| 453 | + private long cumulativeDataGasUsed = 0; |
| 454 | + |
| 455 | + private void update( |
| 456 | + final Transaction transaction, |
| 457 | + final TransactionReceipt receipt, |
| 458 | + final long gasUsed, |
| 459 | + final long dataGasUsed) { |
| 460 | + transactionsByType |
| 461 | + .computeIfAbsent(transaction.getType(), type -> new ArrayList<>()) |
| 462 | + .add(transaction); |
| 463 | + receipts.add(receipt); |
| 464 | + cumulativeGasUsed += gasUsed; |
| 465 | + cumulativeDataGasUsed += dataGasUsed; |
| 466 | + traceLambda( |
| 467 | + LOG, |
| 468 | + "New selected transaction {}, total transactions {}, cumulative gas used {}, cumulative data gas used {}", |
| 469 | + transaction::toTraceLog, |
| 470 | + () -> transactionsByType.values().stream().mapToInt(List::size).sum(), |
| 471 | + () -> cumulativeGasUsed, |
| 472 | + () -> cumulativeDataGasUsed); |
| 473 | + } |
| 474 | + |
| 475 | + private void updateWithInvalidTransaction( |
| 476 | + final Transaction transaction, |
| 477 | + final ValidationResult<TransactionInvalidReason> validationResult) { |
| 478 | + invalidTransactions.add(new TransactionValidationResult(transaction, validationResult)); |
| 479 | + } |
| 480 | + |
| 481 | + public List<Transaction> getTransactions() { |
| 482 | + return streamAllTransactions().collect(Collectors.toList()); |
| 483 | + } |
| 484 | + |
| 485 | + public List<Transaction> getTransactionsByType(final TransactionType type) { |
| 486 | + return transactionsByType.getOrDefault(type, List.of()); |
| 487 | + } |
| 488 | + |
| 489 | + public List<TransactionReceipt> getReceipts() { |
| 490 | + return receipts; |
| 491 | + } |
| 492 | + |
| 493 | + public long getCumulativeGasUsed() { |
| 494 | + return cumulativeGasUsed; |
| 495 | + } |
| 496 | + |
| 497 | + public long getCumulativeDataGasUsed() { |
| 498 | + return cumulativeDataGasUsed; |
| 499 | + } |
| 500 | + |
| 501 | + public List<TransactionValidationResult> getInvalidTransactions() { |
| 502 | + return invalidTransactions; |
| 503 | + } |
| 504 | + |
| 505 | + private Stream<Transaction> streamAllTransactions() { |
| 506 | + return transactionsByType.values().stream().flatMap(List::stream); |
| 507 | + } |
| 508 | + |
| 509 | + @Override |
| 510 | + public boolean equals(final Object o) { |
| 511 | + if (this == o) { |
| 512 | + return true; |
| 513 | + } |
| 514 | + if (o == null || getClass() != o.getClass()) { |
| 515 | + return false; |
| 516 | + } |
| 517 | + TransactionSelectionResults that = (TransactionSelectionResults) o; |
| 518 | + return cumulativeGasUsed == that.cumulativeGasUsed |
| 519 | + && cumulativeDataGasUsed == that.cumulativeDataGasUsed |
| 520 | + && transactionsByType.equals(that.transactionsByType) |
| 521 | + && receipts.equals(that.receipts) |
| 522 | + && invalidTransactions.equals(that.invalidTransactions); |
| 523 | + } |
| 524 | + |
| 525 | + @Override |
| 526 | + public int hashCode() { |
| 527 | + return Objects.hash( |
| 528 | + transactionsByType, |
| 529 | + receipts, |
| 530 | + invalidTransactions, |
| 531 | + cumulativeGasUsed, |
| 532 | + cumulativeDataGasUsed); |
| 533 | + } |
| 534 | + |
| 535 | + public String toTraceLog() { |
| 536 | + return "cumulativeGasUsed=" |
| 537 | + + cumulativeGasUsed |
| 538 | + + ", cumulativeDataGasUsed=" |
| 539 | + + cumulativeDataGasUsed |
| 540 | + + ", transactions=" |
| 541 | + + streamAllTransactions().map(Transaction::toTraceLog).collect(Collectors.joining("; ")); |
| 542 | + } |
| 543 | + } |
528 | 544 | } |
0 commit comments