Skip to content

Commit

Permalink
Merge branch 'main' into 4292-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
fab-10 authored Sep 18, 2024
2 parents 0079069 + 578104e commit 301fcda
Show file tree
Hide file tree
Showing 11 changed files with 776 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -43,7 +42,6 @@
import org.hyperledger.besu.cli.options.unstable.MetricsCLIOptions;
import org.hyperledger.besu.cli.options.unstable.NetworkingOptions;
import org.hyperledger.besu.cli.options.unstable.SynchronizerOptions;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.controller.BesuControllerBuilder;
Expand Down Expand Up @@ -256,10 +254,8 @@ public abstract class CommandTestAbstract {

@BeforeEach
public void initMocks() throws Exception {
// doReturn used because of generic BesuController
doReturn(mockControllerBuilder)
.when(mockControllerBuilderFactory)
.fromEthNetworkConfig(any(), any());
when(mockControllerBuilderFactory.fromEthNetworkConfig(any(), any()))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.synchronizerConfiguration(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.ethProtocolConfiguration(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.transactionPoolConfiguration(any()))
Expand Down Expand Up @@ -288,14 +284,12 @@ public void initMocks() throws Exception {
when(mockControllerBuilder.maxPeers(anyInt())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.maxRemotelyInitiatedPeers(anyInt()))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.besuComponent(any(BesuComponent.class)))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.besuComponent(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.cacheLastBlocks(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.genesisStateHashCacheEnabled(any()))
.thenReturn(mockControllerBuilder);

// doReturn used because of generic BesuController
doReturn(mockController).when(mockControllerBuilder).build();
when(mockControllerBuilder.build()).thenReturn(mockController);
lenient().when(mockController.getProtocolManager()).thenReturn(mockEthProtocolManager);
lenient().when(mockController.getProtocolSchedule()).thenReturn(mockProtocolSchedule);
lenient().when(mockController.getProtocolContext()).thenReturn(mockProtocolContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public Blockchain importAllBlocks() {
return blockchain;
}

public Blockchain importAllBlocks(
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode) {
importBlocks(blocks, headerValidationMode, ommerValidationMode);
return blockchain;
}

public void importFirstBlocks(final int count) {
importBlocks(blocks.subList(0, count));
}
Expand Down Expand Up @@ -126,6 +133,10 @@ public static BlockchainSetupUtil forUpgradedFork() {
return createForEthashChain(BlockTestUtil.getUpgradedForkResources(), DataStorageFormat.FOREST);
}

public static BlockchainSetupUtil forSnapTesting(final DataStorageFormat storageFormat) {
return createForEthashChain(BlockTestUtil.getSnapTestChainResources(), storageFormat);
}

public static BlockchainSetupUtil createForEthashChain(
final ChainResources chainResources, final DataStorageFormat storageFormat) {
return create(
Expand Down Expand Up @@ -241,14 +252,22 @@ public TransactionPool getTransactionPool() {
}

private void importBlocks(final List<Block> blocks) {
importBlocks(blocks, HeaderValidationMode.FULL, HeaderValidationMode.FULL);
}

private void importBlocks(
final List<Block> blocks,
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode) {
for (final Block block : blocks) {
if (block.getHeader().getNumber() == BlockHeader.GENESIS_BLOCK_NUMBER) {
continue;
}
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final BlockImportResult result =
blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL);
blockImporter.importBlock(
protocolContext, block, headerValidationMode, ommerValidationMode);
if (!result.isImported()) {
throw new IllegalStateException("Unable to import block " + block.getHeader().getNumber());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.ethereum.proof.WorldStateProofProvider;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.trie.CompactEncoding;
import org.hyperledger.besu.ethereum.trie.MerkleTrie;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
Expand Down Expand Up @@ -240,12 +241,9 @@ MessageData constructGetAccountRangeResponse(final MessageData message) {
stopWatch,
maxResponseBytes,
(pair) -> {
var rlpOutput = new BytesValueRLPOutput();
rlpOutput.startList();
rlpOutput.writeBytes(pair.getFirst());
rlpOutput.writeRLPBytes(pair.getSecond());
rlpOutput.endList();
return rlpOutput.encodedSize();
Bytes bytes =
AccountRangeMessage.toSlimAccount(RLP.input(pair.getSecond()));
return Hash.SIZE + bytes.size();
});

final Bytes32 endKeyBytes = range.endKeyHash();
Expand All @@ -257,11 +255,15 @@ MessageData constructGetAccountRangeResponse(final MessageData message) {
storage.streamFlatAccounts(range.startKeyHash(), shouldContinuePredicate);

if (accounts.isEmpty() && shouldContinuePredicate.shouldContinue.get()) {
var fromNextHash =
range.endKeyHash().compareTo(range.startKeyHash()) >= 0
? range.endKeyHash()
: range.startKeyHash();
// fetch next account after range, if it exists
LOGGER.debug(
"found no accounts in range, taking first value starting from {}",
asLogHash(range.endKeyHash()));
accounts = storage.streamFlatAccounts(range.endKeyHash(), UInt256.MAX_VALUE, 1L);
asLogHash(fromNextHash));
accounts = storage.streamFlatAccounts(fromNextHash, UInt256.MAX_VALUE, 1L);
}

final var worldStateProof =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.eth.messages.snap;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractSnapMessageData;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
Expand All @@ -28,6 +29,7 @@
import java.util.Optional;
import java.util.TreeMap;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import kotlin.collections.ArrayDeque;
import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -117,7 +119,8 @@ public AccountRangeData accountData(final boolean withRequestId) {
return ImmutableAccountRangeData.builder().accounts(accounts).proofs(proofs).build();
}

private Bytes toFullAccount(final RLPInput rlpInput) {
@VisibleForTesting
public static Bytes toFullAccount(final RLPInput rlpInput) {
final StateTrieAccountValue accountValue = StateTrieAccountValue.readFrom(rlpInput);

final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
Expand All @@ -131,6 +134,26 @@ private Bytes toFullAccount(final RLPInput rlpInput) {
return rlpOutput.encoded();
}

public static Bytes toSlimAccount(final RLPInput rlpInput) {
StateTrieAccountValue accountValue = StateTrieAccountValue.readFrom(rlpInput);
var rlpOutput = new BytesValueRLPOutput();
rlpOutput.startList();
rlpOutput.writeLongScalar(accountValue.getNonce());
rlpOutput.writeUInt256Scalar(accountValue.getBalance());
if (accountValue.getStorageRoot().equals(Hash.EMPTY_TRIE_HASH)) {
rlpOutput.writeNull();
} else {
rlpOutput.writeBytes(accountValue.getStorageRoot());
}
if (accountValue.getCodeHash().equals(Hash.EMPTY)) {
rlpOutput.writeNull();
} else {
rlpOutput.writeBytes(accountValue.getCodeHash());
}
rlpOutput.endList();
return rlpOutput.encoded();
}

@Value.Immutable
public interface AccountRangeData {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.math.BigInteger;
import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.immutables.value.Value;
Expand All @@ -48,12 +49,21 @@ public static GetAccountRangeMessage readFrom(final MessageData message) {

public static GetAccountRangeMessage create(
final Hash worldStateRootHash, final Bytes32 startKeyHash, final Bytes32 endKeyHash) {
return create(worldStateRootHash, startKeyHash, endKeyHash, SIZE_REQUEST);
}

@VisibleForTesting
public static GetAccountRangeMessage create(
final Hash worldStateRootHash,
final Bytes32 startKeyHash,
final Bytes32 endKeyHash,
final BigInteger sizeRequest) {
final BytesValueRLPOutput tmp = new BytesValueRLPOutput();
tmp.startList();
tmp.writeBytes(worldStateRootHash);
tmp.writeBytes(startKeyHash);
tmp.writeBytes(endKeyHash);
tmp.writeBigIntegerScalar(SIZE_REQUEST);
tmp.writeBigIntegerScalar(sizeRequest);
tmp.endList();
return new GetAccountRangeMessage(tmp.encoded());
}
Expand Down
Loading

0 comments on commit 301fcda

Please sign in to comment.