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

Fix clique miner to respect changes to vanity data made via JSON-RPC #1408

Merged
merged 2 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -34,14 +34,14 @@
import java.util.List;
import java.util.concurrent.ExecutorService;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;

public class CliqueMinerExecutor extends AbstractMinerExecutor<CliqueContext, CliqueBlockMiner> {

private final Address localAddress;
private final KeyPair nodeKeys;
private final EpochManager epochManager;
private volatile BytesValue vanityData;

public CliqueMinerExecutor(
final ProtocolContext<CliqueContext> protocolContext,
Expand All @@ -62,7 +62,6 @@ public CliqueMinerExecutor(
this.nodeKeys = nodeKeys;
this.localAddress = Util.publicKeyToAddress(nodeKeys.getPublicKey());
this.epochManager = epochManager;
this.vanityData = miningParams.getExtraData();
}

@Override
Expand Down Expand Up @@ -94,11 +93,12 @@ public CliqueBlockMiner startAsyncMining(
return currentRunningMiner;
}

public BytesValue calculateExtraData(final BlockHeader parentHeader) {
@VisibleForTesting
BytesValue calculateExtraData(final BlockHeader parentHeader) {
final List<Address> validators = Lists.newArrayList();

final BytesValue vanityDataToInsert =
ConsensusHelpers.zeroLeftPad(vanityData, CliqueExtraData.EXTRA_VANITY_LENGTH);
ConsensusHelpers.zeroLeftPad(extraData, CliqueExtraData.EXTRA_VANITY_LENGTH);
// Building ON TOP of canonical head, if the next block is epoch, include validators.
if (epochManager.isEpochBlock(parentHeader.getNumber() + 1)) {
final VoteTally voteTally =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@

public class CliqueMinerExecutorTest {

private static final int EPOCH_LENGTH = 10;
private static final GenesisConfigOptions GENESIS_CONFIG_OPTIONS =
GenesisConfigFile.fromConfig(new JsonObject()).getConfigOptions();
private final KeyPair proposerKeyPair = KeyPair.generate();
private final Random random = new Random(21341234L);
private Address localAddress;
private final List<Address> validatorList = Lists.newArrayList();
private ProtocolContext<CliqueContext> cliqueProtocolContext;
Expand All @@ -80,10 +82,7 @@ public void setup() {

@Test
public void extraDataCreatedOnEpochBlocksContainsValidators() {
final byte[] vanityData = new byte[32];
new Random().nextBytes(vanityData);
final BytesValue wrappedVanityData = BytesValue.wrap(vanityData);
final int EPOCH_LENGTH = 10;
final BytesValue vanityData = generateRandomVanityData();

final CliqueMinerExecutor executor =
new CliqueMinerExecutor(
Expand All @@ -96,7 +95,7 @@ public void extraDataCreatedOnEpochBlocksContainsValidators() {
TestClock.fixed(),
metricsSystem),
proposerKeyPair,
new MiningParameters(AddressHelpers.ofValue(1), Wei.ZERO, wrappedVanityData, false),
new MiningParameters(AddressHelpers.ofValue(1), Wei.ZERO, vanityData, false),
mock(CliqueBlockScheduler.class),
new EpochManager(EPOCH_LENGTH));

Expand All @@ -107,17 +106,14 @@ public void extraDataCreatedOnEpochBlocksContainsValidators() {

final CliqueExtraData cliqueExtraData = CliqueExtraData.decode(extraDataBytes);

assertThat(cliqueExtraData.getVanityData()).isEqualTo(wrappedVanityData);
assertThat(cliqueExtraData.getVanityData()).isEqualTo(vanityData);
assertThat(cliqueExtraData.getValidators())
.containsExactly(validatorList.toArray(new Address[0]));
}

@Test
public void extraDataForNonEpochBlocksDoesNotContainValidaors() {
final byte[] vanityData = new byte[32];
new Random().nextBytes(vanityData);
final BytesValue wrappedVanityData = BytesValue.wrap(vanityData);
final int EPOCH_LENGTH = 10;
final BytesValue vanityData = generateRandomVanityData();

final CliqueMinerExecutor executor =
new CliqueMinerExecutor(
Expand All @@ -130,7 +126,7 @@ public void extraDataForNonEpochBlocksDoesNotContainValidaors() {
TestClock.fixed(),
metricsSystem),
proposerKeyPair,
new MiningParameters(AddressHelpers.ofValue(1), Wei.ZERO, wrappedVanityData, false),
new MiningParameters(AddressHelpers.ofValue(1), Wei.ZERO, vanityData, false),
mock(CliqueBlockScheduler.class),
new EpochManager(EPOCH_LENGTH));

Expand All @@ -141,7 +137,40 @@ public void extraDataForNonEpochBlocksDoesNotContainValidaors() {

final CliqueExtraData cliqueExtraData = CliqueExtraData.decode(extraDataBytes);

assertThat(cliqueExtraData.getVanityData()).isEqualTo(wrappedVanityData);
assertThat(cliqueExtraData.getVanityData()).isEqualTo(vanityData);
assertThat(cliqueExtraData.getValidators()).isEqualTo(Lists.newArrayList());
}

@Test
public void shouldUseLatestVanityData() {
final BytesValue initialVanityData = generateRandomVanityData();
final BytesValue modifiedVanityData = generateRandomVanityData();

final CliqueMinerExecutor executor =
new CliqueMinerExecutor(
cliqueProtocolContext,
Executors.newSingleThreadExecutor(),
CliqueProtocolSchedule.create(GENESIS_CONFIG_OPTIONS, proposerKeyPair),
new PendingTransactions(
PendingTransactions.DEFAULT_TX_RETENTION_HOURS,
1,
TestClock.fixed(),
metricsSystem),
proposerKeyPair,
new MiningParameters(AddressHelpers.ofValue(1), Wei.ZERO, initialVanityData, false),
mock(CliqueBlockScheduler.class),
new EpochManager(EPOCH_LENGTH));

executor.setExtraData(modifiedVanityData);
final BytesValue extraDataBytes = executor.calculateExtraData(blockHeaderBuilder.buildHeader());

final CliqueExtraData cliqueExtraData = CliqueExtraData.decode(extraDataBytes);
assertThat(cliqueExtraData.getVanityData()).isEqualTo(modifiedVanityData);
}

private BytesValue generateRandomVanityData() {
final byte[] vanityData = new byte[32];
random.nextBytes(vanityData);
return BytesValue.wrap(vanityData);
}
}