Skip to content

Commit

Permalink
[PIE-1809] Add import chain json utility (PegaSysEng#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaxter authored Aug 12, 2019
1 parent 5a54246 commit 6998e65
Show file tree
Hide file tree
Showing 46 changed files with 1,753 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface GenesisConfigOptions {

boolean isClique();

String getConsensusEngine();

IbftConfigOptions getIbftLegacyConfigOptions();

CliqueConfigOptions getCliqueConfigOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.common.collect.ImmutableMap;

public class JsonGenesisConfigOptions implements GenesisConfigOptions {

private static final String ETHASH_CONFIG_KEY = "ethash";
private static final String IBFT_LEGACY_CONFIG_KEY = "ibft";
private static final String IBFT2_CONFIG_KEY = "ibft2";
Expand All @@ -39,6 +38,21 @@ public static JsonGenesisConfigOptions fromJsonObject(final ObjectNode configRoo
this.configRoot = isNull(maybeConfig) ? JsonUtil.createEmptyObjectNode() : maybeConfig;
}

@Override
public String getConsensusEngine() {
if (isEthHash()) {
return ETHASH_CONFIG_KEY;
} else if (isIbft2()) {
return IBFT2_CONFIG_KEY;
} else if (isIbftLegacy()) {
return IBFT_LEGACY_CONFIG_KEY;
} else if (isClique()) {
return CLIQUE_CONFIG_KEY;
} else {
return "unknown";
}
}

@Override
public boolean isEthHash() {
return configRoot.has(ETHASH_CONFIG_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions {
private OptionalInt contractSizeLimit = OptionalInt.empty();
private OptionalInt stackSizeLimit = OptionalInt.empty();

@Override
public String getConsensusEngine() {
return "ethash";
}

@Override
public boolean isEthHash() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GenesisConfigOptionsTest {
public void shouldUseEthHashWhenEthHashInConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("ethash", emptyMap()));
assertThat(config.isEthHash()).isTrue();
assertThat(config.getConsensusEngine()).isEqualTo("ethash");
}

@Test
Expand All @@ -41,6 +42,7 @@ public void shouldUseIbftLegacyWhenIbftInConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("ibft", emptyMap()));
assertThat(config.isIbftLegacy()).isTrue();
assertThat(config.getIbftLegacyConfigOptions()).isNotSameAs(IbftConfigOptions.DEFAULT);
assertThat(config.getConsensusEngine()).isEqualTo("ibft");
}

@Test
Expand All @@ -50,11 +52,20 @@ public void shouldNotUseIbftLegacyIfIbftNotPresent() {
assertThat(config.getIbftLegacyConfigOptions()).isSameAs(IbftConfigOptions.DEFAULT);
}

@Test
public void shouldUseIbft2WhenIbft2InConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("ibft2", emptyMap()));
assertThat(config.isIbftLegacy()).isFalse();
assertThat(config.isIbft2()).isTrue();
assertThat(config.getConsensusEngine()).isEqualTo("ibft2");
}

@Test
public void shouldUseCliqueWhenCliqueInConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("clique", emptyMap()));
assertThat(config.isClique()).isTrue();
assertThat(config.getCliqueConfigOptions()).isNotSameAs(CliqueConfigOptions.DEFAULT);
assertThat(config.getConsensusEngine()).isEqualTo("clique");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.util.Subscribers;

import java.util.function.Function;

public class CliqueBlockMiner extends BlockMiner<CliqueContext, CliqueBlockCreator> {

private final Address localAddress;

public CliqueBlockMiner(
final CliqueBlockCreator blockCreator,
final Function<BlockHeader, CliqueBlockCreator> blockCreator,
final ProtocolSchedule<CliqueContext> protocolSchedule,
final ProtocolContext<CliqueContext> protocolContext,
final Subscribers<MinedBlockObserver> observers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -68,32 +69,42 @@ public CliqueMinerExecutor(
@Override
public CliqueBlockMiner startAsyncMining(
final Subscribers<MinedBlockObserver> observers, final BlockHeader parentHeader) {
final CliqueBlockCreator blockCreator =
new CliqueBlockCreator(
localAddress, // TOOD(tmm): This can be removed (used for voting not coinbase).
this::calculateExtraData,
pendingTransactions,
protocolContext,
protocolSchedule,
(gasLimit) -> gasLimit,
nodeKeys,
minTransactionGasPrice,
parentHeader,
epochManager);

final CliqueBlockMiner currentRunningMiner =
new CliqueBlockMiner(
blockCreator,
protocolSchedule,
protocolContext,
observers,
blockScheduler,
parentHeader,
localAddress);
final CliqueBlockMiner currentRunningMiner = createMiner(observers, parentHeader);
executorService.execute(currentRunningMiner);
return currentRunningMiner;
}

@Override
public CliqueBlockMiner createMiner(final BlockHeader parentHeader) {
return createMiner(Subscribers.none(), parentHeader);
}

private CliqueBlockMiner createMiner(
final Subscribers<MinedBlockObserver> observers, final BlockHeader parentHeader) {
final Function<BlockHeader, CliqueBlockCreator> blockCreator =
(header) ->
new CliqueBlockCreator(
localAddress, // TOOD(tmm): This can be removed (used for voting not coinbase).
this::calculateExtraData,
pendingTransactions,
protocolContext,
protocolSchedule,
(gasLimit) -> gasLimit,
nodeKeys,
minTransactionGasPrice,
header,
epochManager);

return new CliqueBlockMiner(
blockCreator,
protocolSchedule,
protocolContext,
observers,
blockScheduler,
parentHeader,
localAddress);
}

@Override
public Optional<Address> getCoinbase() {
return Optional.of(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import tech.pegasys.pantheon.ethereum.chain.BlockAddedObserver;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.List;
import java.util.Optional;

import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -78,6 +82,15 @@ public Optional<Address> getCoinbase() {
return Optional.of(blockCreatorFactory.getLocalAddress());
}

@Override
public Optional<Block> createBlock(
final BlockHeader parentHeader,
final List<Transaction> transactions,
final List<BlockHeader> ommers) {
// One-off block creation has not been implemented
return Optional.empty();
}

@Override
public void onBlockAdded(final BlockAddedEvent event, final Blockchain blockchain) {
if (event.isNewCanonicalHead()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.ProcessableBlockHeader;
import tech.pegasys.pantheon.ethereum.core.SealableBlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.core.WorldUpdater;
import tech.pegasys.pantheon.ethereum.eth.transactions.PendingTransactions;
Expand All @@ -39,6 +40,7 @@

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
Expand Down Expand Up @@ -112,6 +114,19 @@ public AbstractBlockCreator(
*/
@Override
public Block createBlock(final long timestamp) {
return createBlock(Optional.empty(), Optional.empty(), timestamp);
}

@Override
public Block createBlock(
final List<Transaction> transactions, final List<BlockHeader> ommers, final long timestamp) {
return createBlock(Optional.of(transactions), Optional.of(ommers), timestamp);
}

private Block createBlock(
final Optional<List<Transaction>> maybeTransactions,
final Optional<List<BlockHeader>> maybeOmmers,
final long timestamp) {
try {
final ProcessableBlockHeader processableBlockHeader = createPendingBlockHeader(timestamp);

Expand All @@ -121,12 +136,12 @@ public Block createBlock(final long timestamp) {

throwIfStopped();

final List<BlockHeader> ommers = selectOmmers();
final List<BlockHeader> ommers = maybeOmmers.orElse(selectOmmers());

throwIfStopped();

final BlockTransactionSelector.TransactionSelectionResults transactionResults =
selectTransactions(processableBlockHeader, disposableWorldState);
selectTransactions(processableBlockHeader, disposableWorldState, maybeTransactions);

throwIfStopped();

Expand Down Expand Up @@ -174,7 +189,8 @@ public Block createBlock(final long timestamp) {

private BlockTransactionSelector.TransactionSelectionResults selectTransactions(
final ProcessableBlockHeader processableBlockHeader,
final MutableWorldState disposableWorldState)
final MutableWorldState disposableWorldState,
final Optional<List<Transaction>> transactions)
throws RuntimeException {
final long blockNumber = processableBlockHeader.getNumber();

Expand All @@ -196,7 +212,11 @@ private BlockTransactionSelector.TransactionSelectionResults selectTransactions(
isCancelled::get,
miningBeneficiary);

return selector.buildTransactionListForBlock();
if (transactions.isPresent()) {
return selector.evaluateTransactions(transactions.get());
} else {
return selector.buildTransactionListForBlock();
}
}

private MutableWorldState duplicateWorldStateAtParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public AbstractMinerExecutor(
public abstract M startAsyncMining(
final Subscribers<MinedBlockObserver> observers, final BlockHeader parentHeader);

public abstract M createMiner(final BlockHeader parentHeader);

public void setExtraData(final BytesValue extraData) {
this.extraData = extraData.copy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.chain.MinedBlockObserver;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState;
import tech.pegasys.pantheon.util.Subscribers;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.List;
import java.util.Optional;

import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -53,6 +56,15 @@ public AbstractMiningCoordinator(
syncState.addInSyncListener(this::inSyncChanged);
}

@Override
public Optional<Block> createBlock(
final BlockHeader parentHeader,
final List<Transaction> transactions,
final List<BlockHeader> ommers) {
M miner = executor.createMiner(parentHeader);
return Optional.of(miner.createBlock(parentHeader, transactions, ommers));
}

@Override
public void enable() {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
package tech.pegasys.pantheon.ethereum.blockcreation;

import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;

import java.util.List;

public interface BlockCreator {
Block createBlock(final long timestamp);

Block createBlock(
final List<Transaction> transactions, final List<BlockHeader> ommers, final long timestamp);
}
Loading

0 comments on commit 6998e65

Please sign in to comment.