Search in sources :

Example 1 with MiningBeneficiaryCalculator

use of org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator 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 MiningBeneficiaryCalculator

use of org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator in project besu by hyperledger.

the class PrivateStorageMigrationTest method setUp.

@Before
public void setUp() {
    final KeyValueStorage kvStorage = new InMemoryKeyValueStorage();
    privateStateStorage = new PrivateStateKeyValueStorage(kvStorage);
    privateStateRootResolver = new PrivateStateRootResolver(privateStateStorage);
    lenient().when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
    lenient().when(protocolSpec.getTransactionProcessor()).thenReturn(transactionProcessor);
    lenient().when(protocolSpec.getTransactionReceiptFactory()).thenReturn(transactionReceiptFactory);
    lenient().when(protocolSpec.getBlockReward()).thenReturn(Wei.ZERO);
    lenient().when(protocolSpec.getMiningBeneficiaryCalculator()).thenReturn(miningBeneficiaryCalculator);
    lenient().when(protocolSpec.isSkipZeroBlockRewards()).thenReturn(false);
    migration = new PrivateStorageMigration(blockchain, PRIVACY_ADDRESS, protocolSchedule, publicWorldStateArchive, privateStateStorage, privateStateRootResolver, legacyPrivateStateStorage, (protocolSpec) -> privateMigrationBlockProcessor);
}
Also used : PrivateStateRootResolver(org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver) Arrays(java.util.Arrays) PrivateTransactionMetadata(org.hyperledger.besu.ethereum.privacy.storage.PrivateTransactionMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) DEFAULT_PRIVACY(org.hyperledger.besu.ethereum.core.PrivacyParameters.DEFAULT_PRIVACY) Map(java.util.Map) BigInteger(java.math.BigInteger) Block(org.hyperledger.besu.ethereum.core.Block) Bytes32(org.apache.tuweni.bytes.Bytes32) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) List(java.util.List) PrivateBlockMetadata(org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Optional(java.util.Optional) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) TransactionType(org.hyperledger.besu.plugin.data.TransactionType) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Hash(org.hyperledger.besu.datatypes.Hash) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EMPTY_ROOT_HASH(org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver.EMPTY_ROOT_HASH) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Bytes(org.apache.tuweni.bytes.Bytes) Mockito.lenient(org.mockito.Mockito.lenient) Address(org.hyperledger.besu.datatypes.Address) ArrayList(java.util.ArrayList) TransactionReceiptFactory(org.hyperledger.besu.ethereum.mainnet.AbstractBlockProcessor.TransactionReceiptFactory) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) SCHEMA_VERSION_1_0_0(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage.SCHEMA_VERSION_1_0_0) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SCHEMA_VERSION_1_4_0(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage.SCHEMA_VERSION_1_4_0) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) Wei(org.hyperledger.besu.datatypes.Wei) Before(org.junit.Before) PrivateTransactionDataFixture.privateMarkerTransaction(org.hyperledger.besu.ethereum.core.PrivateTransactionDataFixture.privateMarkerTransaction) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) MainnetTransactionProcessor(org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor) PrivateStateStorage(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateStorage) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) PrivateStateKeyValueStorage(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage) Mockito.verify(org.mockito.Mockito.verify) LegacyPrivateStateStorage(org.hyperledger.besu.ethereum.privacy.storage.LegacyPrivateStateStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) MiningBeneficiaryCalculator(org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator) Transaction(org.hyperledger.besu.ethereum.core.Transaction) PrivacyGroupHeadBlockMap(org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) PrivateStateKeyValueStorage(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) PrivateStateRootResolver(org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver) PrivateStateKeyValueStorage(org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage) Before(org.junit.Before)

Aggregations

ArrayList (java.util.ArrayList)2 Wei (org.hyperledger.besu.datatypes.Wei)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 MiningBeneficiaryCalculator (org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator)2 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)2 BigInteger (java.math.BigInteger)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 SignatureAlgorithmFactory (org.hyperledger.besu.crypto.SignatureAlgorithmFactory)1 Address (org.hyperledger.besu.datatypes.Address)1 Hash (org.hyperledger.besu.datatypes.Hash)1 Trace (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace)1 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)1 Block (org.hyperledger.besu.ethereum.core.Block)1