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

Decouple DefaultMutableBlockchain from KeyValueStorage #211

Merged
merged 3 commits into from
Oct 31, 2018
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 @@ -23,18 +23,16 @@
import tech.pegasys.pantheon.consensus.common.VoteTally;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.AddressHelpers;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockBody;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture;
import tech.pegasys.pantheon.ethereum.core.Util;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;

import java.util.List;

Expand All @@ -51,7 +49,7 @@ public class NodeCanProduceNextBlockTest {
private final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture();
private ProtocolContext<CliqueContext> cliqueProtocolContext;

DefaultMutableBlockchain blockChain;
MutableBlockchain blockChain;
private Block genesisBlock;

private Block createEmptyBlock(final KeyPair blockSigner) {
Expand All @@ -72,10 +70,7 @@ public void networkWithOneValidatorIsAllowedToCreateConsecutiveBlocks() {

genesisBlock = createEmptyBlock(proposerKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand All @@ -100,10 +95,7 @@ public void networkWithTwoValidatorsIsAllowedToProduceBlockIfNotPreviousBlockPro

genesisBlock = createEmptyBlock(otherNodeKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand Down Expand Up @@ -137,10 +129,7 @@ public void networkWithTwoValidatorsIsNotAllowedToProduceBlockIfIsPreviousBlockP

genesisBlock = createEmptyBlock(proposerKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand Down Expand Up @@ -170,10 +159,7 @@ public void withThreeValidatorsMustHaveOneBlockBetweenSignings() {

genesisBlock = createEmptyBlock(proposerKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand Down Expand Up @@ -218,10 +204,7 @@ public void signerIsValidIfInsufficientBlocksExistInHistory() {

genesisBlock = createEmptyBlock(proposerKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand Down Expand Up @@ -250,10 +233,7 @@ public void exceptionIsThrownIfOnAnOrphanedChain() {

genesisBlock = createEmptyBlock(proposerKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand All @@ -277,10 +257,7 @@ public void exceptionIsThrownIfOnAnOrphanedChain() {
public void nonValidatorIsNotAllowedToCreateABlock() {
genesisBlock = createEmptyBlock(otherNodeKeyPair);

final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@

import tech.pegasys.pantheon.consensus.common.EpochManager;
import tech.pegasys.pantheon.crypto.SECP256K1.Signature;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.AddressHelpers;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockBody;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;
import tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.math.BigInteger;
Expand All @@ -54,7 +52,7 @@ private Block createEmptyBlock(final long blockNumber, final Hash parentHash) {
headerBuilder.buildHeader(), new BlockBody(Lists.emptyList(), Lists.emptyList()));
}

DefaultMutableBlockchain blockChain;
MutableBlockchain blockChain;
private Block genesisBlock;
private Block block_1;
private Block block_2;
Expand All @@ -69,11 +67,8 @@ public void constructThreeBlockChain() {
.encode());

genesisBlock = createEmptyBlock(0, Hash.ZERO);
final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();

blockChain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock);

block_1 = createEmptyBlock(1, genesisBlock.getHeader().getHash());
block_2 = createEmptyBlock(1, block_1.getHeader().getHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive;

import tech.pegasys.pantheon.consensus.clique.CliqueContext;
import tech.pegasys.pantheon.consensus.clique.CliqueExtraData;
Expand All @@ -39,13 +41,8 @@
import tech.pegasys.pantheon.ethereum.core.PendingTransactions;
import tech.pegasys.pantheon.ethereum.core.Util;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.mainnet.MutableProtocolSchedule;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.List;
Expand All @@ -62,11 +59,8 @@ public class CliqueBlockCreatorTest {
private final List<Address> validatorList = Lists.newArrayList();

private final Block genesis = GenesisConfig.mainnet().getBlock();
private final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
private final MutableBlockchain blockchain =
new DefaultMutableBlockchain(genesis, keyValueStorage, MainnetBlockHashFunction::createHash);
private final WorldStateArchive stateArchive =
new WorldStateArchive(new KeyValueStorageWorldStateStorage(keyValueStorage));
private final MutableBlockchain blockchain = createInMemoryBlockchain(genesis);
private final WorldStateArchive stateArchive = createInMemoryWorldStateArchive();

private ProtocolContext<CliqueContext> protocolContext;
private final MutableProtocolSchedule<CliqueContext> protocolSchedule =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.InMemoryWorldState.createInMemoryWorldStateArchive;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive;

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.consensus.common.VoteTally;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package tech.pegasys.pantheon.ethereum.vm;

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.pantheon.ethereum.core.InMemoryWorldState.createInMemoryWorldStateArchive;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive;

import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive;

import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.chain.GenesisConfig;
Expand All @@ -26,18 +28,13 @@
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.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.debug.TraceFrame;
import tech.pegasys.pantheon.ethereum.debug.TraceOptions;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionProcessor;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionProcessor.Result;
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPInput;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;
import tech.pegasys.pantheon.util.bytes.Bytes32;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.uint.UInt256;
Expand Down Expand Up @@ -68,12 +65,8 @@ public class TraceTransactionIntegrationTest {
public void setUp() {
final GenesisConfig<Void> genesisConfig = GenesisConfig.development();
genesisBlock = genesisConfig.getBlock();
final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockchain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
worldStateArchive =
new WorldStateArchive(new KeyValueStorageWorldStateStorage(keyValueStorage));
blockchain = createInMemoryBlockchain(genesisBlock);
worldStateArchive = createInMemoryWorldStateArchive();
final ProtocolSchedule<Void> protocolSchedule = genesisConfig.getProtocolSchedule();
genesisConfig.writeStateTo(worldStateArchive.getMutable(Hash.EMPTY_TRIE_HASH));
transactionProcessor = protocolSchedule.getByBlockNumber(0).getTransactionProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import tech.pegasys.pantheon.ethereum.chain.TransactionLocation;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockBody;
import tech.pegasys.pantheon.ethereum.core.BlockHashFunction;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;
import tech.pegasys.pantheon.ethereum.util.InvalidConfigurationException;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;
import tech.pegasys.pantheon.util.Subscribers;
import tech.pegasys.pantheon.util.uint.UInt256;

Expand All @@ -53,12 +51,9 @@ public class DefaultMutableBlockchain implements MutableBlockchain {
private final Subscribers<BlockAddedObserver> blockAddedObservers = new Subscribers<>();

public DefaultMutableBlockchain(
final Block genesisBlock,
final KeyValueStorage keyValueStorage,
final BlockHashFunction blockHashFunction) {
checkArgument(genesisBlock != null, "Missing required KeyValueStorage");
this.blockchainStorage =
new KeyValueStoragePrefixedKeyBlockchainStorage(keyValueStorage, blockHashFunction);
final Block genesisBlock, final BlockchainStorage blockchainStorage) {
checkNotNull(genesisBlock);
this.blockchainStorage = blockchainStorage;
this.setGenesis(genesisBlock);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tech.pegasys.pantheon.ethereum.chain.GenesisConfig;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.KeyValueStoragePrefixedKeyBlockchainStorage;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
Expand All @@ -42,7 +43,9 @@ private ExecutionContextTestFixture(
this.keyValueStorage = keyValueStorage;
this.blockchain =
new DefaultMutableBlockchain(
genesis, keyValueStorage, MainnetBlockHashFunction::createHash);
genesis,
new KeyValueStoragePrefixedKeyBlockchainStorage(
keyValueStorage, MainnetBlockHashFunction::createHash));
this.stateArchive =
new WorldStateArchive(new KeyValueStorageWorldStateStorage(keyValueStorage));
this.protocolSchedule = protocolSchedule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@
*/
package tech.pegasys.pantheon.ethereum.core;

import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.KeyValueStoragePrefixedKeyBlockchainStorage;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;

public class InMemoryWorldState {
public class InMemoryTestFixture {

public static MutableBlockchain createInMemoryBlockchain(final Block genesisBlock) {
return createInMemoryBlockchain(genesisBlock, MainnetBlockHashFunction::createHash);
}

public static MutableBlockchain createInMemoryBlockchain(
final Block genesisBlock, final BlockHashFunction blockHashFunction) {
final InMemoryKeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
return new DefaultMutableBlockchain(
genesisBlock,
new KeyValueStoragePrefixedKeyBlockchainStorage(keyValueStorage, blockHashFunction));
}

public static WorldStateArchive createInMemoryWorldStateArchive() {
return new WorldStateArchive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.InMemoryWorldState.createInMemoryWorldStateArchive;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain;
import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive;
import static tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason.EXCEEDS_BLOCK_GAS_LIMIT;
import static tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason.NONCE_TOO_LOW;
import static tech.pegasys.pantheon.ethereum.mainnet.ValidationResult.valid;
Expand All @@ -38,15 +39,11 @@
import tech.pegasys.pantheon.ethereum.chain.GenesisConfig;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.TransactionPool.TransactionBatchAddedListener;
import tech.pegasys.pantheon.ethereum.db.DefaultMutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator;
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage;
import tech.pegasys.pantheon.services.kvstore.KeyValueStorage;
import tech.pegasys.pantheon.util.uint.UInt256;

import java.util.List;
Expand Down Expand Up @@ -82,10 +79,7 @@ public class TransactionPoolTest {
public void setUp() {
final GenesisConfig<Void> genesisConfig = GenesisConfig.development();
final Block genesisBlock = genesisConfig.getBlock();
final KeyValueStorage keyValueStorage = new InMemoryKeyValueStorage();
blockchain =
new DefaultMutableBlockchain(
genesisBlock, keyValueStorage, MainnetBlockHashFunction::createHash);
blockchain = createInMemoryBlockchain(genesisBlock);
final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
final ProtocolContext<Void> protocolContext =
new ProtocolContext<>(blockchain, worldStateArchive, null);
Expand Down
Loading