use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace in project besu by hyperledger.
the class RewardTraceGenerator method generateFromBlock.
/**
* Generates a stream of reward {@link Trace} from the passed {@link Block} data.
*
* @param protocolSchedule the {@link ProtocolSchedule} to use
* @param block the current {@link Block} to use
* @return a stream of generated reward traces {@link Trace}
*/
public static Stream<Trace> generateFromBlock(final ProtocolSchedule protocolSchedule, final Block block) {
final List<Trace> flatTraces = new ArrayList<>();
final BlockHeader blockHeader = block.getHeader();
final List<BlockHeader> ommers = block.getBody().getOmmers();
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber());
final Wei blockReward = protocolSpec.getBlockReward();
final MiningBeneficiaryCalculator miningBeneficiaryCalculator = protocolSpec.getMiningBeneficiaryCalculator();
final Wei coinbaseReward = protocolSpec.getBlockProcessor().getCoinbaseReward(blockReward, blockHeader.getNumber(), ommers.size());
// add uncle reward traces
ommers.forEach(ommerBlockHeader -> {
final Wei ommerReward = protocolSpec.getBlockProcessor().getOmmerReward(blockReward, blockHeader.getNumber(), ommerBlockHeader.getNumber());
final Action.Builder uncleActionBuilder = Action.builder().author(miningBeneficiaryCalculator.calculateBeneficiary(ommerBlockHeader).toHexString()).rewardType(UNCLE_LABEL).value(ommerReward.toShortHexString());
flatTraces.add(RewardTrace.builder().actionBuilder(uncleActionBuilder).blockHash(block.getHash().toHexString()).blockNumber(blockHeader.getNumber()).type(REWARD_LABEL).build());
});
// add block reward trace
final Action.Builder blockActionBuilder = Action.builder().author(miningBeneficiaryCalculator.calculateBeneficiary(blockHeader).toHexString()).rewardType(BLOCK_LABEL).value(coinbaseReward.toShortHexString());
flatTraces.add(0, RewardTrace.builder().actionBuilder(blockActionBuilder).blockHash(block.getHash().toHexString()).blockNumber(blockHeader.getNumber()).type(REWARD_LABEL).build());
return flatTraces.stream();
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace in project besu by hyperledger.
the class FlatTraceGeneratorTest method testGenerateFromTransactionTraceWithRevertReason.
@Test
public void testGenerateFromTransactionTraceWithRevertReason() {
final Bytes revertReason = Bytes.random(32);
Mockito.when(transaction.getSender()).thenReturn(Address.ZERO);
Mockito.when(transactionProcessingResult.getRevertReason()).thenReturn(Optional.of(revertReason));
final TransactionTrace transactionTrace = new TransactionTrace(transaction, transactionProcessingResult, Collections.emptyList());
final Stream<Trace> traceStream = FlatTraceGenerator.generateFromTransactionTrace(null, transactionTrace, null, new AtomicInteger());
final List<Trace> traces = traceStream.collect(Collectors.toList());
Assertions.assertThat(traces.isEmpty()).isFalse();
Assertions.assertThat(traces.get(0)).isNotNull();
Assertions.assertThat(traces.get(0) instanceof FlatTrace).isTrue();
Assertions.assertThat(((FlatTrace) traces.get(0)).getRevertReason()).isEqualTo(revertReason.toHexString());
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace in project besu by hyperledger.
the class RewardTraceGeneratorTest method assertThatTraceGeneratorReturnValidRewardsForClassicBlockProcessor.
@Test
public void assertThatTraceGeneratorReturnValidRewardsForClassicBlockProcessor() {
final AbstractBlockProcessor.TransactionReceiptFactory transactionReceiptFactory = mock(AbstractBlockProcessor.TransactionReceiptFactory.class);
final ClassicBlockProcessor blockProcessor = new ClassicBlockProcessor(transactionProcessor, transactionReceiptFactory, blockReward, BlockHeader::getCoinbase, true, eraRounds);
when(protocolSpec.getBlockProcessor()).thenReturn(blockProcessor);
final Stream<Trace> traceStream = RewardTraceGenerator.generateFromBlock(protocolSchedule, block);
final Action.Builder actionBlockReward = Action.builder().rewardType("block").author(blockBeneficiary.toHexString()).value(blockProcessor.getCoinbaseReward(blockReward, block.getHeader().getNumber(), 1).toShortHexString());
final Trace blocReward = new RewardTrace.Builder().blockHash(block.getHash().toHexString()).blockNumber(block.getHeader().getNumber()).actionBuilder(actionBlockReward).type("reward").build();
// calculate reward with ClassicBlockProcessor
final Action.Builder actionOmmerReward = Action.builder().rewardType("uncle").author(ommerBeneficiary.toHexString()).value(blockProcessor.getOmmerReward(blockReward, block.getHeader().getNumber(), ommerHeader.getNumber()).toShortHexString());
final Trace ommerReward = new RewardTrace.Builder().blockHash(block.getHash().toHexString()).blockNumber(block.getHeader().getNumber()).actionBuilder(actionOmmerReward).type("reward").build();
final List<Trace> traces = traceStream.collect(Collectors.toList());
// check block reward
assertThat(traces.get(0)).usingRecursiveComparison().isEqualTo(blocReward);
// check ommer reward
assertThat(traces.get(1)).usingRecursiveComparison().isEqualTo(ommerReward);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace in project besu by hyperledger.
the class RewardTraceGeneratorTest method assertThatTraceGeneratorReturnValidRewardsForMainnetBlockProcessor.
@Test
public void assertThatTraceGeneratorReturnValidRewardsForMainnetBlockProcessor() {
final AbstractBlockProcessor.TransactionReceiptFactory transactionReceiptFactory = mock(AbstractBlockProcessor.TransactionReceiptFactory.class);
final MainnetBlockProcessor blockProcessor = new MainnetBlockProcessor(transactionProcessor, transactionReceiptFactory, blockReward, BlockHeader::getCoinbase, true, Optional.empty());
when(protocolSpec.getBlockProcessor()).thenReturn(blockProcessor);
final Stream<Trace> traceStream = RewardTraceGenerator.generateFromBlock(protocolSchedule, block);
final Action.Builder actionBlockReward = Action.builder().rewardType("block").author(blockBeneficiary.toHexString()).value(blockProcessor.getCoinbaseReward(blockReward, block.getHeader().getNumber(), 1).toShortHexString());
final Trace blocReward = new RewardTrace.Builder().blockHash(block.getHash().toHexString()).blockNumber(block.getHeader().getNumber()).actionBuilder(actionBlockReward).type("reward").build();
// calculate reward with MainnetBlockProcessor
final Action.Builder actionOmmerReward = Action.builder().rewardType("uncle").author(ommerBeneficiary.toHexString()).value(blockProcessor.getOmmerReward(blockReward, block.getHeader().getNumber(), ommerHeader.getNumber()).toShortHexString());
final Trace ommerReward = new RewardTrace.Builder().blockHash(block.getHash().toHexString()).blockNumber(block.getHeader().getNumber()).actionBuilder(actionOmmerReward).type("reward").build();
final List<Trace> traces = traceStream.collect(Collectors.toList());
// check block reward
assertThat(traces.get(0)).usingRecursiveComparison().isEqualTo(blocReward);
// check ommer reward
assertThat(traces.get(1)).usingRecursiveComparison().isEqualTo(ommerReward);
}
Aggregations