Search in sources :

Example 1 with Trace

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();
}
Also used : Trace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace) MiningBeneficiaryCalculator(org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) ArrayList(java.util.ArrayList) Wei(org.hyperledger.besu.datatypes.Wei) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with Trace

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());
}
Also used : TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Trace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace) Bytes(org.apache.tuweni.bytes.Bytes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Test(org.junit.Test)

Example 3 with Trace

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);
}
Also used : Trace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace) ClassicBlockProcessor(org.hyperledger.besu.ethereum.mainnet.ClassicBlockProcessor) AbstractBlockProcessor(org.hyperledger.besu.ethereum.mainnet.AbstractBlockProcessor) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 4 with Trace

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);
}
Also used : Trace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace) MainnetBlockProcessor(org.hyperledger.besu.ethereum.mainnet.MainnetBlockProcessor) AbstractBlockProcessor(org.hyperledger.besu.ethereum.mainnet.AbstractBlockProcessor) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

Trace (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 Test (org.junit.Test)3 AbstractBlockProcessor (org.hyperledger.besu.ethereum.mainnet.AbstractBlockProcessor)2 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Wei (org.hyperledger.besu.datatypes.Wei)1 TransactionTrace (org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace)1 ClassicBlockProcessor (org.hyperledger.besu.ethereum.mainnet.ClassicBlockProcessor)1 MainnetBlockProcessor (org.hyperledger.besu.ethereum.mainnet.MainnetBlockProcessor)1 MiningBeneficiaryCalculator (org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator)1 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)1