Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Added BlockCreatorFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tmohay committed Nov 13, 2018
1 parent 4a75fb7 commit 46409d2
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
*/
package tech.pegasys.pantheon.consensus.ibft;

import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftBlockCreatorFactory;

/** Stateful evaluator for ibft events */
public class IbftStateMachine {

private final IbftBlockCreatorFactory blockCreatorFactory;

public IbftStateMachine(final IbftBlockCreatorFactory blockCreatorFactory) {
this.blockCreatorFactory = blockCreatorFactory;
}

/**
* Attempt to consume the event and update the maintained state
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Vote {
private final Address recipient;
private final IbftVoteType voteType;

private Vote(final Address recipient, final IbftVoteType voteType) {
public Vote(final Address recipient, final IbftVoteType voteType) {
this.recipient = recipient;
this.voteType = voteType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,55 @@
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractBlockCreator;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHashFunction;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderBuilder;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.PendingTransactions;
import tech.pegasys.pantheon.ethereum.core.SealableBlockHeader;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction;

import java.util.function.Function;

// TODO: Just a placeholder. Implementation is required.
// This class is responsible for creating a block without committer seals (basically it was just
// too hard to coordinate with the state machine).
public class IbftBlockCreator extends AbstractBlockCreator<IbftContext> {
public IbftBlockCreator(
final Address coinbase,
final Address localAddress,
final ExtraDataCalculator extraDataCalculator,
final PendingTransactions pendingTransactions,
final ProtocolContext<IbftContext> protocolContext,
final ProtocolSchedule<IbftContext> protocolSchedule,
final Function<Long, Long> gasLimitCalculator,
final Wei minTransactionGasPrice,
final Address miningBeneficiary,
final BlockHeader parentHeader) {
super(
coinbase,
localAddress,
extraDataCalculator,
pendingTransactions,
protocolContext,
protocolSchedule,
gasLimitCalculator,
minTransactionGasPrice,
miningBeneficiary,
localAddress,
parentHeader);
}

@Override
protected BlockHeader createFinalBlockHeader(final SealableBlockHeader sealableBlockHeader) {
return null;

final BlockHashFunction blockHashFunction =
ScheduleBasedBlockHashFunction.create(protocolSchedule);

final BlockHeaderBuilder builder =
BlockHeaderBuilder.create()
.populateFrom(sealableBlockHeader)
.mixHash(Hash.ZERO)
.nonce(0L)
.blockHashFunction(blockHashFunction);

return builder.buildBlockHeader();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.consensus.ibft.blockcreation;

import tech.pegasys.pantheon.consensus.common.VoteTally;
import tech.pegasys.pantheon.consensus.common.VoteType;
import tech.pegasys.pantheon.consensus.ibft.IbftContext;
import tech.pegasys.pantheon.consensus.ibft.IbftExtraData;
import tech.pegasys.pantheon.consensus.ibft.IbftVoteType;
import tech.pegasys.pantheon.consensus.ibft.Vote;
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.MiningParameters;
import tech.pegasys.pantheon.ethereum.core.PendingTransactions;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.bytes.BytesValues;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public class IbftBlockCreatorFactory {

private final Function<Long, Long> gasLimitCalculator;
private final PendingTransactions pendingTransactions;
protected final ProtocolContext<IbftContext> protocolContext;
protected final ProtocolSchedule<IbftContext> protocolSchedule;
private final Address localAddress;

private volatile BytesValue vanityData;
private volatile Wei minTransactionGasPrice;

public IbftBlockCreatorFactory(
final Function<Long, Long> gasLimitCalculator,
final PendingTransactions pendingTransactions,
final ProtocolContext<IbftContext> protocolContext,
final ProtocolSchedule<IbftContext> protocolSchedule,
final MiningParameters miningParams,
final Address localAddress) {
this.gasLimitCalculator = gasLimitCalculator;
this.pendingTransactions = pendingTransactions;
this.protocolContext = protocolContext;
this.protocolSchedule = protocolSchedule;
this.localAddress = localAddress;
this.minTransactionGasPrice = miningParams.getMinTransactionGasPrice();
this.vanityData = miningParams.getExtraData();
}

public IbftBlockCreator create(final BlockHeader parentHeader, final int round) {
return new IbftBlockCreator(
localAddress,
ph -> createExtraData(round),
pendingTransactions,
protocolContext,
protocolSchedule,
gasLimitCalculator,
minTransactionGasPrice,
parentHeader);
}

public void setExtraData(final BytesValue extraData) {
this.vanityData = extraData.copy();
}

public void setMinTransactionGasPrice(final Wei minTransactionGasPrice) {
this.minTransactionGasPrice = minTransactionGasPrice.copy();
}

public Wei getMinTransactionGasPrice() {
return minTransactionGasPrice;
}

public BytesValue createExtraData(final int round) {
final VoteTally voteTally = protocolContext.getConsensusState().getVoteTally();
Optional<Map.Entry<Address, VoteType>> proposal =
protocolContext.getConsensusState().getVoteProposer().getVote(localAddress, voteTally);

// TODO(tmm): This needs to be reworked to not always be "add"
// TODO(tmm): The Vote type probably shouldn't know about serialisation values, should bein the
// extra data content.
Optional<Vote> vote =
proposal
.map(p -> Optional.of(new Vote(p.getKey(), IbftVoteType.ADD)))
.orElse(Optional.empty());

List<Address> validators = new ArrayList<>(voteTally.getCurrentValidators());

IbftExtraData extraData =
new IbftExtraData(
createCorrectlySizedVanityData(), new ArrayList<>(), vote, round, validators);

return extraData.encode();
}

// TODO(tmm): Copied from CliqueMinerExecutor
private BytesValue createCorrectlySizedVanityData() {
final int vanityPadding = Math.max(0, IbftExtraData.EXTRA_VANITY_LENGTH - vanityData.size());
return BytesValues.concatenate(BytesValue.wrap(new byte[vanityPadding]), vanityData)
.slice(0, IbftExtraData.EXTRA_VANITY_LENGTH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.consensus.ibft.blockcreation;

import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.util.bytes.BytesValue;

public class IbftMiningCoordinator implements MiningCoordinator {

private final IbftBlockCreatorFactory blockCreatorFactory;

public IbftMiningCoordinator(final IbftBlockCreatorFactory blockCreatorFactory) {
this.blockCreatorFactory = blockCreatorFactory;
}

@Override
public void enable() {}

@Override
public void disable() {}

@Override
public boolean isRunning() {
return false;
}

@Override
public void setMinTransactionGasPrice(final Wei minGasPrice) {}

@Override
public Wei getMinTransactionGasPrice() {
return null;
}

@Override
public void setExtraData(final BytesValue extraData) {
blockCreatorFactory.setExtraData(extraData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import tech.pegasys.pantheon.ethereum.chain.BlockAddedObserver;
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.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState;
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolution;
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolverInputs;
import tech.pegasys.pantheon.util.Subscribers;
import tech.pegasys.pantheon.util.bytes.BytesValue;

Expand Down Expand Up @@ -138,34 +135,4 @@ public Wei getMinTransactionGasPrice() {
public void setExtraData(final BytesValue extraData) {
executor.setExtraData(extraData);
}

@Override
public void setCoinbase(final Address coinbase) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents setting coinbase.");
}

@Override
public Optional<Address> getCoinbase() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of coinbase.");
}

@Override
public Optional<Long> hashesPerSecond() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of hashrate.");
}

@Override
public Optional<EthHashSolverInputs> getWorkDefinition() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying work definition.");
}

@Override
public boolean submitWork(final EthHashSolution solution) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents submission of work solutions.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,28 @@ public interface MiningCoordinator {

void setExtraData(BytesValue extraData);

void setCoinbase(Address coinbase);

Optional<Address> getCoinbase();

Optional<Long> hashesPerSecond();

Optional<EthHashSolverInputs> getWorkDefinition();

boolean submitWork(EthHashSolution solution);
default void setCoinbase(final Address coinbase) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents setting coinbase.");
}

default Optional<Address> getCoinbase() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of coinbase.");
}

default Optional<Long> hashesPerSecond() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of hashrate.");
}

default Optional<EthHashSolverInputs> getWorkDefinition() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying work definition.");
}

default boolean submitWork(final EthHashSolution solution) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents submission of work solutions.");
}
}
2 changes: 1 addition & 1 deletion ethereum/referencetests/src/test/resources
Submodule resources updated 929 files
Loading

0 comments on commit 46409d2

Please sign in to comment.