Search in sources :

Example 1 with Blockchain

use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.

the class SStoreOperationTest method createMessageFrame.

private MessageFrame createMessageFrame(final Address address, final Gas initialGas, final Gas remainingGas) {
    final Blockchain blockchain = mock(Blockchain.class);
    final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
    final WorldUpdater worldStateUpdater = worldStateArchive.getMutable().updater();
    final BlockHeader blockHeader = new BlockHeaderTestFixture().buildHeader();
    final MessageFrame frame = new MessageFrameTestFixture().address(address).worldUpdater(worldStateUpdater).blockHeader(blockHeader).blockchain(blockchain).initialGas(initialGas).build();
    worldStateUpdater.getOrCreate(address).getMutable().setBalance(Wei.of(1));
    worldStateUpdater.commit();
    frame.setGasRemaining(remainingGas);
    return frame;
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) MessageFrameTestFixture(org.hyperledger.besu.ethereum.core.MessageFrameTestFixture) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with Blockchain

use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.

the class TransitionBesuControllerBuilder method initTransitionWatcher.

private void initTransitionWatcher(final ProtocolContext protocolContext, final TransitionCoordinator composedCoordinator) {
    PostMergeContext postMergeContext = protocolContext.getConsensusContext(PostMergeContext.class);
    postMergeContext.observeNewIsPostMergeState((isPoS, difficultyStoppedAt) -> {
        if (isPoS) {
            // if we transitioned to post-merge, stop and disable any mining
            composedCoordinator.getPreMergeObject().disable();
            composedCoordinator.getPreMergeObject().stop();
            // set the blockchoiceRule to never reorg, rely on forkchoiceUpdated instead
            protocolContext.getBlockchain().setBlockChoiceRule((newBlockHeader, currentBlockHeader) -> -1);
        } else if (composedCoordinator.isMiningBeforeMerge()) {
            // if our merge state is set to mine pre-merge and we are mining, start mining
            composedCoordinator.getPreMergeObject().enable();
            composedCoordinator.getPreMergeObject().start();
        }
    });
    // initialize our merge context merge status before we would start either
    Blockchain blockchain = protocolContext.getBlockchain();
    blockchain.getTotalDifficultyByHash(blockchain.getChainHeadHash()).ifPresent(postMergeContext::setIsPostMerge);
}
Also used : PostMergeContext(org.hyperledger.besu.consensus.merge.PostMergeContext) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain)

Example 3 with Blockchain

use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.

the class PrivacyBlockProcessorTest method mustPerformRehydration.

@Test
public void mustPerformRehydration() {
    final BlockDataGenerator blockDataGenerator = new BlockDataGenerator();
    final Blockchain blockchain = mock(Blockchain.class);
    final MutableWorldState mutableWorldState = mock(MutableWorldState.class);
    when(mutableWorldState.updater()).thenReturn(mock(WorldUpdater.class));
    final Block firstBlock = blockDataGenerator.block(BlockDataGenerator.BlockOptions.create().addTransaction(PrivateTransactionDataFixture.privateMarkerTransactionOnchain()));
    final Block secondBlock = blockDataGenerator.block(BlockDataGenerator.BlockOptions.create().addTransaction(PrivateTransactionDataFixture.privateMarkerTransactionOnchainAdd()));
    when(enclave.receive(any())).thenReturn(PrivateTransactionDataFixture.generateAddToGroupReceiveResponse(PrivateTransactionDataFixture.privateTransactionBesu(), PrivateTransactionDataFixture.privateMarkerTransactionOnchain()));
    when(blockchain.getTransactionLocation(any())).thenReturn(Optional.of(new TransactionLocation(firstBlock.getHash(), 0)));
    when(blockchain.getBlockByHash(any())).thenReturn(Optional.of(firstBlock));
    when(blockchain.getBlockHeader(any())).thenReturn(Optional.of(firstBlock.getHeader()));
    final ProtocolSpec protocolSpec = mockProtocolSpec();
    when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
    when(publicWorldStateArchive.getMutable(any(), any())).thenReturn(Optional.of(mutableWorldState));
    final MutableWorldState mockPrivateStateArchive = mockPrivateStateArchive();
    when(privateWorldStateArchive.getMutable(any(), any())).thenReturn(Optional.of(mockPrivateStateArchive));
    final PrivacyGroupHeadBlockMap expected = new PrivacyGroupHeadBlockMap(Collections.singletonMap(VALID_BASE64_ENCLAVE_KEY, firstBlock.getHash()));
    privateStateStorage.updater().putPrivacyGroupHeadBlockMap(firstBlock.getHash(), expected).putPrivateBlockMetadata(firstBlock.getHash(), VALID_BASE64_ENCLAVE_KEY, PrivateBlockMetadata.empty()).commit();
    privacyBlockProcessor.processBlock(blockchain, mutableWorldState, secondBlock);
    verify(blockProcessor).processBlock(eq(blockchain), eq(mutableWorldState), eq(secondBlock.getHeader()), eq(secondBlock.getBody().getTransactions()), eq(secondBlock.getBody().getOmmers()), any());
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) PrivacyGroupHeadBlockMap(org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap) Test(org.junit.Test)

Example 4 with Blockchain

use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.

the class PrivacyBlockProcessorTest method mustCopyPreviousPrivacyGroupBlockHeadMap.

@Test
public void mustCopyPreviousPrivacyGroupBlockHeadMap() {
    final BlockDataGenerator blockDataGenerator = new BlockDataGenerator();
    final Blockchain blockchain = mock(Blockchain.class);
    final MutableWorldState mutableWorldState = mock(MutableWorldState.class);
    final PrivacyGroupHeadBlockMap expected = new PrivacyGroupHeadBlockMap(Collections.singletonMap(Bytes32.ZERO, Hash.EMPTY));
    final Block firstBlock = blockDataGenerator.block();
    final Block secondBlock = blockDataGenerator.block(BlockDataGenerator.BlockOptions.create().setParentHash(firstBlock.getHash()));
    privacyBlockProcessor.processBlock(blockchain, mutableWorldState, firstBlock);
    privateStateStorage.updater().putPrivacyGroupHeadBlockMap(firstBlock.getHash(), expected).commit();
    privacyBlockProcessor.processBlock(blockchain, mutableWorldState, secondBlock);
    assertThat(privateStateStorage.getPrivacyGroupHeadBlockMap(secondBlock.getHash())).contains(expected);
    verify(blockProcessor).processBlock(eq(blockchain), eq(mutableWorldState), eq(firstBlock.getHeader()), eq(firstBlock.getBody().getTransactions()), eq(firstBlock.getBody().getOmmers()), any());
    verify(blockProcessor).processBlock(eq(blockchain), eq(mutableWorldState), eq(secondBlock.getHeader()), eq(secondBlock.getBody().getTransactions()), eq(secondBlock.getBody().getOmmers()), any());
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) PrivacyGroupHeadBlockMap(org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap) Test(org.junit.Test)

Example 5 with Blockchain

use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.

the class GoQuorumBlockProcessorTest method noAccountCreatedWhenBlockRewardIsZeroAndSkipped.

@Test
public void noAccountCreatedWhenBlockRewardIsZeroAndSkipped() {
    final Blockchain blockchain = new ReferenceTestBlockchain();
    final GoQuorumBlockProcessor blockProcessor = new GoQuorumBlockProcessor(transactionProcessor, transactionReceiptFactory, Wei.ZERO, BlockHeader::getCoinbase, true, Optional.of(goQuorumPrivacyParameters));
    final MutableWorldState worldState = ReferenceTestWorldState.create(emptyMap());
    final Hash initialHash = worldState.rootHash();
    final BlockHeader emptyBlockHeader = new BlockHeaderTestFixture().transactionsRoot(Hash.EMPTY_LIST_HASH).ommersHash(Hash.EMPTY_LIST_HASH).buildHeader();
    blockProcessor.processBlock(blockchain, worldState, emptyBlockHeader, emptyList(), emptyList());
    // An empty block with 0 reward should not change the world state
    assertThat(worldState.rootHash()).isEqualTo(initialHash);
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Hash(org.hyperledger.besu.datatypes.Hash) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain) Test(org.junit.Test)

Aggregations

Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)72 Test (org.junit.Test)45 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)42 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)36 Optional (java.util.Optional)31 Hash (org.hyperledger.besu.datatypes.Hash)31 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)31 List (java.util.List)30 Block (org.hyperledger.besu.ethereum.core.Block)30 Bytes (org.apache.tuweni.bytes.Bytes)27 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)26 Transaction (org.hyperledger.besu.ethereum.core.Transaction)25 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)24 InMemoryKeyValueStorageProvider.createInMemoryBlockchain (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain)24 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)23 ArrayList (java.util.ArrayList)22 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)22 CompletableFuture (java.util.concurrent.CompletableFuture)21 Wei (org.hyperledger.besu.datatypes.Wei)21 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)21