Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get mutable methods of the worldtstate provider #8113

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public BesuController build() {
worldStateHealerSupplier::get);

if (maybeStoredGenesisBlockHash.isEmpty()) {
genesisState.writeStateTo(worldStateArchive.getMutable());
genesisState.writeStateTo(worldStateArchive.getWorldState());
}

transactionSimulator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.ethereum.transaction.BlockStateCall;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockOverrides;
Expand Down Expand Up @@ -135,8 +136,13 @@ private BlockHeader getBlockHeader(final long blockNumber) {
}

private MutableWorldState getWorldState(final BlockHeader header, final boolean isPersisting) {
final WorldStateQueryParams worldStateQueryParams =
WorldStateQueryParams.newBuilder()
.withBlockHeader(header)
.withShouldWorldStateUpdateHead(isPersisting)
.build();
return worldStateArchive
.getMutable(header, isPersisting)
.getWorldState(worldStateQueryParams)
.orElseThrow(
() ->
new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.services;

import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withBlockHeaderAndUpdateNodeHead;

import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
Expand All @@ -25,7 +27,7 @@
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.common.provider.DiffBasedWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.common.storage.DiffBasedWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.data.BlockBody;
Expand Down Expand Up @@ -109,7 +111,9 @@ public boolean setHeadUnsafe(final BlockHeader blockHeader, final BlockBody bloc

final MutableBlockchain blockchain = protocolContext.getBlockchain();

if (worldStateArchive.flatMap(archive -> archive.getMutable(coreHeader, true)).isPresent()) {
if (worldStateArchive
.flatMap(archive -> archive.getWorldState(withBlockHeaderAndUpdateNodeHead(coreHeader)))
.isPresent()) {
if (coreHeader.getParentHash().equals(blockchain.getChainHeadHash())) {
LOG.atDebug()
.setMessage(
Expand Down Expand Up @@ -142,7 +146,7 @@ public void disableWorldStateTrie() {
// TODO maybe find a best way in the future to delete and disable trie
worldStateArchive.ifPresent(
archive -> {
archive.getDefaultWorldStateConfig().setTrieDisabled(true);
archive.getWorldStateSharedSpec().setTrieDisabled(true);
final DiffBasedWorldStateKeyValueStorage worldStateStorage =
archive.getWorldStateKeyValueStorage();
final Optional<Hash> worldStateBlockHash = worldStateStorage.getWorldStateBlockHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -59,6 +58,7 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.VariablesKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.data.AddedBlockContext;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void setUp() {
.when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any()))
.thenReturn(ValidationResult.valid());
lenient()
.when(mockWorldStateArchive.getMutable(any(), anyBoolean()))
.when(mockWorldStateArchive.getWorldState(any(WorldStateQueryParams.class)))
.thenReturn(Optional.of(mockWorldState));

blockBroadcaster = new BlockBroadcaster(mockEthContext, 10 * ByteUnits.MEGABYTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void shouldRetrieveStateUpdatePostTracingForOneBlock() {
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");

final long persistedNonceForAccount =
worldStateArchive.getMutable().get(addressToVerify).getNonce();
worldStateArchive.getWorldState().get(addressToVerify).getNonce();

final long blockNumber = 2;

Expand All @@ -109,7 +109,7 @@ void shouldRetrieveStateUpdatePostTracingForOneBlock() {
},
opTracer);

assertThat(worldStateArchive.getMutable().get(addressToVerify).getNonce())
assertThat(worldStateArchive.getWorldState().get(addressToVerify).getNonce())
.isEqualTo(persistedNonceForAccount);

final Block tracedBlock = blockchain.getBlockByNumber(blockNumber).get();
Expand Down Expand Up @@ -139,7 +139,7 @@ void shouldRetrieveStateUpdatePostTracingForAllBlocks() {
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");

final long persistedNonceForAccount =
worldStateArchive.getMutable().get(addressToVerify).getNonce();
worldStateArchive.getWorldState().get(addressToVerify).getNonce();

final long startBlock = 1;
final long endBlock = 32;
Expand All @@ -157,7 +157,7 @@ void shouldRetrieveStateUpdatePostTracingForAllBlocks() {
},
opTracer);

assertThat(worldStateArchive.getMutable().get(addressToVerify).getNonce())
assertThat(worldStateArchive.getWorldState().get(addressToVerify).getNonce())
.isEqualTo(persistedNonceForAccount);

LongStream.rangeClosed(startBlock, endBlock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static java.util.stream.Collectors.joining;
import static org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator.ForkchoiceResult.Status.INVALID;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withBlockHeaderAndUpdateNodeHead;

import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.consensus.merge.PayloadWrapper;
Expand Down Expand Up @@ -679,7 +680,7 @@ private boolean moveWorldStateTo(final BlockHeader newHead) {
Optional<MutableWorldState> newWorldState =
protocolContext
.getWorldStateArchive()
.getMutable(newHead.getStateRoot(), newHead.getHash());
.getWorldState(withBlockHeaderAndUpdateNodeHead(newHead));

newWorldState.ifPresentOrElse(
mutableWorldState ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void setUp() {

protocolContext =
new ProtocolContext(blockchain, worldStateArchive, mergeContext, badBlockManager);
var mutable = worldStateArchive.getMutable();
var mutable = worldStateArchive.getWorldState();
genesisState.writeStateTo(mutable);
mutable.persist(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class MergeReorgTest implements MergeGenesisConfigHelper {

@BeforeEach
public void setUp() {
var mutable = worldStateArchive.getMutable();
var mutable = worldStateArchive.getWorldState();
genesisState.writeStateTo(mutable);
mutable.persist(null);
mergeContext.setTerminalTotalDifficulty(Difficulty.of(1001));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public TestContext build() {
createInMemoryBlockchain(
genesisState.getBlock(),
BftBlockHeaderFunctions.forOnchainBlock(BFT_EXTRA_DATA_ENCODER));
genesisState.writeStateTo(worldStateArchive.getMutable());
genesisState.writeStateTo(worldStateArchive.getWorldState());
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
this.importer = importer;
this.blockchain = createInMemoryBlockchain(importer.getGenesisBlock());
this.stateArchive = createInMemoryWorldStateArchive();
this.importer.getGenesisState().writeStateTo(stateArchive.getMutable());
this.importer.getGenesisState().writeStateTo(stateArchive.getWorldState());
this.context =
new ProtocolContext(
blockchain, stateArchive, mock(ConsensusContext.class), new BadBlockManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void setUpOnce() throws Exception {
MutableBlockchain chain =
InMemoryKeyValueStorageProvider.createInMemoryBlockchain(importer.getGenesisBlock());
WorldStateArchive state = InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive();
importer.getGenesisState().writeStateTo(state.getMutable());
importer.getGenesisState().writeStateTo(state.getWorldState());
ProtocolContext context =
new ProtocolContext(chain, state, mock(ConsensusContext.class), new BadBlockManager());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType.UNKNOWN_BLOCK;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withBlockHeaderAndUpdateNodeHead;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
Expand All @@ -29,7 +30,7 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.trie.diffbased.common.DiffBasedWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.common.provider.DiffBasedWorldStateProvider;

import java.util.Optional;

Expand Down Expand Up @@ -137,14 +138,14 @@ private boolean rollIncrementally(
interimHead.ifPresent(
it -> {
blockchain.rewindToBlock(it.getBlockHash());
archive.getMutable(it.getStateRoot(), it.getBlockHash());
archive.getWorldState(withBlockHeaderAndUpdateNodeHead(it));
LOG.info("incrementally rolled worldstate to {}", it.toLogString());
});
currentHead = interimHead;

} else {
blockchain.rewindToBlock(target.getBlockHash());
archive.getMutable(target.getStateRoot(), target.getBlockHash());
archive.getWorldState(withBlockHeaderAndUpdateNodeHead(target));
currentHead = Optional.of(target);
LOG.info("finished rolling worldstate to {}", target.toLogString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.privateProcessor;

import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withStateRootAndBlockHashAndUpdateNodeHead;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
Expand Down Expand Up @@ -49,11 +51,12 @@ public static <TRACE> Optional<TRACE> processTracing(
final MutableWorldState disposablePrivateState =
privacyParameters
.getPrivateWorldStateArchive()
.getMutable(
privacyController
.getStateRootByBlockNumber(privacyGroupId, enclaveKey, blockNumber)
.get(),
parentHash)
.getWorldState(
withStateRootAndBlockHashAndUpdateNodeHead(
privacyController
.getStateRootByBlockNumber(privacyGroupId, enclaveKey, blockNumber)
.get(),
parentHash))
.get();

return blockchainQueries.getAndMapWorldState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher.BLOCKS_PER_BLOOM_CACHE;
import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator.calculateExcessBlobGasForParent;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withBlockHeaderAndNoUpdateNodeHead;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -1002,7 +1003,10 @@ public <U> Optional<U> getAndMapWorldState(
.getBlockHeader(blockHash)
.flatMap(
blockHeader -> {
try (var ws = worldStateArchive.getMutable(blockHeader, false).orElse(null)) {
try (var ws =
worldStateArchive
.getWorldState(withBlockHeaderAndNoUpdateNodeHead(blockHeader))
.orElse(null)) {
if (ws != null) {
return mapper.apply(ws);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ private void backupLeaves() throws IOException {
header.get().getStateRoot(),
Function.identity(),
Function.identity());

accountTrie.visitLeafs(this::visitAccount);
backupStatus.currentAccount = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void assertOnlyChainHeadMovesWorldParameterAbsent(final String blockParam

// assert the chain moved, and the worldstate did not
assertThat(newChainTip).isEqualTo(blockOne);
assertThat(archive.getMutable().rootHash()).isEqualTo(chainTip.getStateRoot());
assertThat(archive.getWorldState().rootHash()).isEqualTo(chainTip.getStateRoot());
}

@ParameterizedTest
Expand All @@ -116,7 +116,7 @@ public void assertOnlyChainHeadMoves(final String blockParam) {

// assert the chain moved, and the worldstate did not
assertThat(newChainTip).isEqualTo(blockOne);
assertThat(archive.getMutable().rootHash()).isEqualTo(chainTip.getStateRoot());
assertThat(archive.getWorldState().rootHash()).isEqualTo(chainTip.getStateRoot());
}

@ParameterizedTest
Expand All @@ -142,7 +142,7 @@ public void assertBothChainHeadAndWorldStatByNumber(final String blockParam) {

// assert both the chain and worldstate moved to block one
assertThat(newChainTip).isEqualTo(blockOne);
assertThat(archive.getMutable().rootHash()).isEqualTo(blockOne.getStateRoot());
assertThat(archive.getWorldState().rootHash()).isEqualTo(blockOne.getStateRoot());
}

@Test
Expand All @@ -167,7 +167,7 @@ public void assertNotFound() {

// assert neither the chain nor the worldstate moved
assertThat(newChainTip).isEqualTo(chainTip);
assertThat(archive.getMutable().rootHash()).isEqualTo(chainTip.getStateRoot());
assertThat(archive.getWorldState().rootHash()).isEqualTo(chainTip.getStateRoot());
}

private JsonRpcRequestContext debugSetHead(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.hyperledger.besu.ethereum.core.BlockHeaderBuilder.createPending;
import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator.calculateExcessBlobGasForParent;
import static org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams.withBlockHeaderAndNoUpdateNodeHead;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.BlobGas;
Expand Down Expand Up @@ -404,7 +405,7 @@ private MutableWorldState duplicateWorldStateAtParent(final BlockHeader parentHe
final Hash parentStateRoot = parentHeader.getStateRoot();
return protocolContext
.getWorldStateArchive()
.getMutable(parentHeader, false)
.getWorldState(withBlockHeaderAndNoUpdateNodeHead(parentHeader))
.orElseThrow(
() -> {
LOG.info("Unable to create block because world state is not available");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.VariablesKeyValueStorage;
import org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason;
import org.hyperledger.besu.ethereum.trie.diffbased.common.provider.WorldStateQueryParams;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.gascalculator.LondonGasCalculator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
Expand Down Expand Up @@ -182,7 +183,7 @@ public void setup() {
worldStateUpdater.createAccount(sender, 0, Wei.of(1_000_000_000L));
worldStateUpdater.commit();

when(protocolContext.getWorldStateArchive().getMutable(any(), anyBoolean()))
when(protocolContext.getWorldStateArchive().getWorldState(any(WorldStateQueryParams.class)))
.thenReturn(Optional.of(worldState));
when(ethContext.getEthPeers().subscribeConnect(any())).thenReturn(1L);
when(ethScheduler.scheduleBlockCreationTask(any(Runnable.class)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void rewardBeneficiary_zeroReward_skipZeroRewardsFalse() {
ethScheduler);

final MutableWorldState mutableWorldState =
executionContextTestFixture.getStateArchive().getMutable();
executionContextTestFixture.getStateArchive().getWorldState();
assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNull();

final ProcessableBlockHeader header =
Expand Down Expand Up @@ -327,7 +327,7 @@ void rewardBeneficiary_zeroReward_skipZeroRewardsTrue() {
ethScheduler);

final MutableWorldState mutableWorldState =
executionContextTestFixture.getStateArchive().getMutable();
executionContextTestFixture.getStateArchive().getWorldState();
assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNull();

final ProcessableBlockHeader header =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EntriesFromIntegrationTest {
@Test
@SuppressWarnings("MathAbsoluteRandom")
public void shouldCollectStateEntries() {
final MutableWorldState worldState = createInMemoryWorldStateArchive().getMutable();
final MutableWorldState worldState = createInMemoryWorldStateArchive().getWorldState();
final WorldUpdater updater = worldState.updater();
MutableAccount account = updater.getOrCreate(Address.fromHexString("0x56"));
final Map<Bytes32, AccountStorageEntry> expectedValues = new TreeMap<>();
Expand Down
Loading
Loading