diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java index e560f8b0ec..c788f26015 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java @@ -17,6 +17,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; 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.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.consensus.clique.headervalidationrules.SignerRateLimitValidationRule; import tech.pegasys.pantheon.consensus.common.VoteProposer; @@ -31,7 +32,6 @@ 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 java.util.List; @@ -70,7 +70,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(proposerKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -95,7 +95,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(otherNodeKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -129,7 +129,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(proposerKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -159,7 +159,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(proposerKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -204,7 +204,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(proposerKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -233,7 +233,7 @@ public class NodeCanProduceNextBlockTest { genesisBlock = createEmptyBlock(proposerKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); @@ -257,7 +257,7 @@ public class NodeCanProduceNextBlockTest { public void nonValidatorIsNotAllowedToCreateABlock() { genesisBlock = createEmptyBlock(otherNodeKeyPair); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/VoteTallyCacheTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/VoteTallyCacheTest.java index d767d25239..b85e99685e 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/VoteTallyCacheTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/VoteTallyCacheTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.consensus.common.EpochManager; import tech.pegasys.pantheon.consensus.common.VoteTallyUpdater; @@ -31,7 +32,6 @@ 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.util.bytes.BytesValue; import java.math.BigInteger; @@ -69,7 +69,7 @@ public class VoteTallyCacheTest { genesisBlock = createEmptyBlock(0, Hash.ZERO); - blockChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + blockChain = createInMemoryBlockchain(genesisBlock); block_1 = createEmptyBlock(1, genesisBlock.getHeader().getHash()); block_2 = createEmptyBlock(1, block_1.getHeader().getHash()); diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java index 2b577b22f4..00a7980855 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java @@ -16,8 +16,8 @@ import static org.assertj.core.api.Java6Assertions.assertThat; 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 static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.consensus.clique.CliqueContext; diff --git a/consensus/ibftlegacy/src/test/java/tech/pegasys/pantheon/consensus/ibftlegacy/blockcreation/IbftBlockCreatorTest.java b/consensus/ibftlegacy/src/test/java/tech/pegasys/pantheon/consensus/ibftlegacy/blockcreation/IbftBlockCreatorTest.java index 660d8c6de9..b9553a696d 100644 --- a/consensus/ibftlegacy/src/test/java/tech/pegasys/pantheon/consensus/ibftlegacy/blockcreation/IbftBlockCreatorTest.java +++ b/consensus/ibftlegacy/src/test/java/tech/pegasys/pantheon/consensus/ibftlegacy/blockcreation/IbftBlockCreatorTest.java @@ -16,7 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; 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.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.consensus.common.VoteProposer; diff --git a/ethereum/blockcreation/src/test/java/tech/pegasys/pantheon/ethereum/blockcreation/BlockTransactionSelectorTest.java b/ethereum/blockcreation/src/test/java/tech/pegasys/pantheon/ethereum/blockcreation/BlockTransactionSelectorTest.java index a3b2f5a3d2..99d833852e 100644 --- a/ethereum/blockcreation/src/test/java/tech/pegasys/pantheon/ethereum/blockcreation/BlockTransactionSelectorTest.java +++ b/ethereum/blockcreation/src/test/java/tech/pegasys/pantheon/ethereum/blockcreation/BlockTransactionSelectorTest.java @@ -42,9 +42,9 @@ import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.TransactionProcessor; import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason; import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.vm.TestBlockchain; import tech.pegasys.pantheon.ethereum.worldstate.DefaultMutableWorldState; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.util.bytes.BytesValue; import tech.pegasys.pantheon.util.uint.UInt256; diff --git a/ethereum/core/src/integration-test/java/tech/pegasys/pantheon/ethereum/vm/EntriesFromIntegrationTest.java b/ethereum/core/src/integration-test/java/tech/pegasys/pantheon/ethereum/vm/EntriesFromIntegrationTest.java index ff9f8220f2..ab8533f580 100644 --- a/ethereum/core/src/integration-test/java/tech/pegasys/pantheon/ethereum/vm/EntriesFromIntegrationTest.java +++ b/ethereum/core/src/integration-test/java/tech/pegasys/pantheon/ethereum/vm/EntriesFromIntegrationTest.java @@ -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.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Hash; diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/chain/GenesisState.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/chain/GenesisState.java index e6b307c2b4..73026ac0f6 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/chain/GenesisState.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/chain/GenesisState.java @@ -25,8 +25,8 @@ import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.core.WorldUpdater; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.worldstate.DefaultMutableWorldState; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.util.bytes.Bytes32; import tech.pegasys.pantheon.util.bytes.BytesValue; diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/StorageProvider.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/StorageProvider.java new file mode 100644 index 0000000000..9f6c46f3e0 --- /dev/null +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/StorageProvider.java @@ -0,0 +1,26 @@ +/* + * 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.ethereum.storage; + +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; +import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; +import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; + +import java.io.Closeable; + +public interface StorageProvider extends Closeable { + + BlockchainStorage createBlockchainStorage(ProtocolSchedule protocolSchedule); + + WorldStateStorage createWorldStateStorage(); +} diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/db/KeyValueStoragePrefixedKeyBlockchainStorage.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java similarity index 98% rename from ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/db/KeyValueStoragePrefixedKeyBlockchainStorage.java rename to ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java index 1333e92314..aebd63eb07 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/db/KeyValueStoragePrefixedKeyBlockchainStorage.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java @@ -10,7 +10,7 @@ * 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.ethereum.db; +package tech.pegasys.pantheon.ethereum.storage.keyvalue; import tech.pegasys.pantheon.ethereum.chain.TransactionLocation; import tech.pegasys.pantheon.ethereum.core.BlockBody; @@ -18,6 +18,7 @@ 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.TransactionReceipt; +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; import tech.pegasys.pantheon.ethereum.rlp.RLP; import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; import tech.pegasys.pantheon.util.bytes.Bytes32; diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageProvider.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageProvider.java new file mode 100644 index 0000000000..4dd2a3d857 --- /dev/null +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageProvider.java @@ -0,0 +1,47 @@ +/* + * 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.ethereum.storage.keyvalue; + +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; +import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; +import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; +import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; + +import java.io.IOException; + +public class KeyValueStorageProvider implements StorageProvider { + + private final KeyValueStorage keyValueStorage; + + public KeyValueStorageProvider(final KeyValueStorage keyValueStorage) { + this.keyValueStorage = keyValueStorage; + } + + @Override + public BlockchainStorage createBlockchainStorage(final ProtocolSchedule protocolSchedule) { + return new KeyValueStoragePrefixedKeyBlockchainStorage( + keyValueStorage, ScheduleBasedBlockHashFunction.create(protocolSchedule)); + } + + @Override + public WorldStateStorage createWorldStateStorage() { + return new KeyValueStorageWorldStateStorage(keyValueStorage); + } + + @Override + public void close() throws IOException { + keyValueStorage.close(); + } +} diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/KeyValueStorageWorldStateStorage.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageWorldStateStorage.java similarity index 95% rename from ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/KeyValueStorageWorldStateStorage.java rename to ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageWorldStateStorage.java index 356356e17e..761604479e 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/KeyValueStorageWorldStateStorage.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/KeyValueStorageWorldStateStorage.java @@ -10,9 +10,10 @@ * 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.ethereum.worldstate; +package tech.pegasys.pantheon.ethereum.storage.keyvalue; import tech.pegasys.pantheon.ethereum.core.Hash; +import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; import tech.pegasys.pantheon.util.bytes.Bytes32; import tech.pegasys.pantheon.util.bytes.BytesValue; diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/RocksDbStorageProvider.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/RocksDbStorageProvider.java new file mode 100644 index 0000000000..073e9e42d7 --- /dev/null +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/RocksDbStorageProvider.java @@ -0,0 +1,29 @@ +/* + * 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.ethereum.storage.keyvalue; + +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; +import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class RocksDbStorageProvider { + + public static StorageProvider create(final Path databaseDir) throws IOException { + final KeyValueStorage kv = RocksDbKeyValueStorage.create(Files.createDirectories(databaseDir)); + return new KeyValueStorageProvider(kv); + } +} diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/DebuggableMutableWorldState.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/DebuggableMutableWorldState.java index 074ffcac21..b6aa0904b7 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/DebuggableMutableWorldState.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/worldstate/DebuggableMutableWorldState.java @@ -18,6 +18,7 @@ import tech.pegasys.pantheon.ethereum.core.MutableAccount; import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.core.WorldState; import tech.pegasys.pantheon.ethereum.core.WorldUpdater; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import java.util.Collection; diff --git a/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/ExecutionContextTestFixture.java b/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/ExecutionContextTestFixture.java index 72e21e96a5..6f33efd5c4 100644 --- a/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/ExecutionContextTestFixture.java +++ b/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/ExecutionContextTestFixture.java @@ -18,13 +18,13 @@ import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.GenesisState; 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.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolScheduleBuilder; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.worldstate.DefaultMutableWorldState; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; diff --git a/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryTestFixture.java b/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryStorageProvider.java similarity index 62% rename from ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryTestFixture.java rename to ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryStorageProvider.java index b3f621294c..b2e56e38a5 100644 --- a/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryTestFixture.java +++ b/ethereum/core/src/test-support/java/tech/pegasys/pantheon/ethereum/core/InMemoryStorageProvider.java @@ -13,14 +13,19 @@ package tech.pegasys.pantheon.ethereum.core; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; 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.ethereum.mainnet.ProtocolSchedule; +import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; +import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; -public class InMemoryTestFixture { +public class InMemoryStorageProvider implements StorageProvider { public static MutableBlockchain createInMemoryBlockchain(final Block genesisBlock) { return createInMemoryBlockchain(genesisBlock, MainnetBlockHashFunction::createHash); @@ -38,4 +43,18 @@ public class InMemoryTestFixture { return new WorldStateArchive( new KeyValueStorageWorldStateStorage(new InMemoryKeyValueStorage())); } + + @Override + public BlockchainStorage createBlockchainStorage(final ProtocolSchedule protocolSchedule) { + return new KeyValueStoragePrefixedKeyBlockchainStorage( + new InMemoryKeyValueStorage(), ScheduleBasedBlockHashFunction.create(protocolSchedule)); + } + + @Override + public WorldStateStorage createWorldStateStorage() { + return new KeyValueStorageWorldStateStorage(new InMemoryKeyValueStorage()); + } + + @Override + public void close() {} } diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/chain/GenesisStateTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/chain/GenesisStateTest.java index 29eab6a7b8..03c08aa657 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/chain/GenesisStateTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/chain/GenesisStateTest.java @@ -20,8 +20,8 @@ import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule; import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.worldstate.DefaultMutableWorldState; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.util.bytes.BytesValue; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/DefaultMutableBlockchainTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/DefaultMutableBlockchainTest.java index dbc9973177..9cd6cdac40 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/DefaultMutableBlockchainTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/DefaultMutableBlockchainTest.java @@ -22,6 +22,7 @@ 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.mainnet.MainnetBlockHashFunction; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage; import tech.pegasys.pantheon.ethereum.testutil.BlockDataGenerator; import tech.pegasys.pantheon.ethereum.testutil.BlockDataGenerator.BlockOptions; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/GenesisBlockMismatchTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/GenesisBlockMismatchTest.java index 6cdcffc262..c610596c34 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/GenesisBlockMismatchTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/db/GenesisBlockMismatchTest.java @@ -23,6 +23,7 @@ import tech.pegasys.pantheon.ethereum.core.BlockHeaderBuilder; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.core.LogsBloomFilter; import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage; import tech.pegasys.pantheon.ethereum.util.InvalidConfigurationException; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/testutil/BlockDataGenerator.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/testutil/BlockDataGenerator.java index 6ce404dbae..875500b839 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/testutil/BlockDataGenerator.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/testutil/BlockDataGenerator.java @@ -13,7 +13,7 @@ package tech.pegasys.pantheon.ethereum.testutil; import static com.google.common.base.Preconditions.checkArgument; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.crypto.SECP256K1; import tech.pegasys.pantheon.ethereum.core.Address; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/util/BlockchainUtilParameterizedTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/util/BlockchainUtilParameterizedTest.java index 246d027512..d698660b21 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/util/BlockchainUtilParameterizedTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/util/BlockchainUtilParameterizedTest.java @@ -13,12 +13,12 @@ package tech.pegasys.pantheon.ethereum.util; import static org.assertj.core.api.Assertions.assertThat; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; 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.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.TransactionReceipt; import tech.pegasys.pantheon.ethereum.testutil.BlockDataGenerator; import tech.pegasys.pantheon.util.uint.UInt256; @@ -58,7 +58,7 @@ public class BlockchainUtilParameterizedTest { @BeforeClass public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); - localBlockchain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + localBlockchain = createInMemoryBlockchain(genesisBlock); // Setup local chain. for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = @@ -73,7 +73,7 @@ public class BlockchainUtilParameterizedTest { @Before public void setup() { - remoteBlockchain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + remoteBlockchain = createInMemoryBlockchain(genesisBlock); commonHeader = genesisBlock.getHeader(); for (long i = 1; i <= commonAncestorHeight; i++) { diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/BlockchainReferenceTestCaseSpec.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/BlockchainReferenceTestCaseSpec.java index 63e36e6fdf..249efbc6bf 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/BlockchainReferenceTestCaseSpec.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/BlockchainReferenceTestCaseSpec.java @@ -12,8 +12,8 @@ */ package tech.pegasys.pantheon.ethereum.vm; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import static tech.pegasys.pantheon.ethereum.vm.WorldStateMock.insertAccount; import tech.pegasys.pantheon.ethereum.ProtocolContext; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/operations/ExtCodeHashOperationTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/operations/ExtCodeHashOperationTest.java index a259f4fa36..504d861abe 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/operations/ExtCodeHashOperationTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/vm/operations/ExtCodeHashOperationTest.java @@ -27,9 +27,9 @@ import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.core.WorldUpdater; import tech.pegasys.pantheon.ethereum.db.WorldStateArchive; import tech.pegasys.pantheon.ethereum.mainnet.ConstantinopleGasCalculator; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.vm.MessageFrame; import tech.pegasys.pantheon.ethereum.vm.Words; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.util.bytes.Bytes32; import tech.pegasys.pantheon.util.bytes.BytesValue; diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/worldstate/DefaultMutableWorldStateTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/worldstate/DefaultMutableWorldStateTest.java index b2c95ce5c8..4fcfa153b3 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/worldstate/DefaultMutableWorldStateTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/worldstate/DefaultMutableWorldStateTest.java @@ -25,6 +25,7 @@ import tech.pegasys.pantheon.ethereum.core.MutableWorldState; import tech.pegasys.pantheon.ethereum.core.Wei; import tech.pegasys.pantheon.ethereum.core.WorldState; import tech.pegasys.pantheon.ethereum.core.WorldUpdater; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.KeyValueStorageWorldStateStorage; import tech.pegasys.pantheon.ethereum.trie.MerklePatriciaTrie; import tech.pegasys.pantheon.services.kvstore.InMemoryKeyValueStorage; import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTest.java index 0b95ecf204..c6f2cf1fcd 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTest.java @@ -17,6 +17,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.ethereum.chain.Blockchain; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; @@ -24,7 +25,6 @@ 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.Hash; -import tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.TransactionReceipt; import tech.pegasys.pantheon.ethereum.eth.EthProtocol; import tech.pegasys.pantheon.ethereum.eth.EthProtocol.EthVersion; @@ -694,7 +694,7 @@ public final class EthProtocolManagerTest { public void shouldSuccessfullyRespondToGetHeadersRequestLessThanZero() throws ExecutionException, InterruptedException { final Block genesisBlock = gen.genesisBlock(); - final MutableBlockchain blockchain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + final MutableBlockchain blockchain = createInMemoryBlockchain(genesisBlock); final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTestUtil.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTestUtil.java index 4d87bbae24..eb0b8055c0 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTestUtil.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/EthProtocolManagerTestUtil.java @@ -12,7 +12,7 @@ */ package tech.pegasys.pantheon.ethereum.eth.manager; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.ethereum.chain.Blockchain; diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/ethtaskutils/BlockchainSetupUtil.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/ethtaskutils/BlockchainSetupUtil.java index 531dda7f49..98f8561548 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/ethtaskutils/BlockchainSetupUtil.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/manager/ethtaskutils/BlockchainSetupUtil.java @@ -13,8 +13,8 @@ package tech.pegasys.pantheon.ethereum.eth.manager.ethtaskutils; import static org.assertj.core.util.Preconditions.checkArgument; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.Blockchain; diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/DownloaderTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/DownloaderTest.java index 2986a33bf5..09be7545c4 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/DownloaderTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/DownloaderTest.java @@ -18,6 +18,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.internal.verification.VerificationModeFactory.times; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.Blockchain; @@ -25,7 +26,6 @@ import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; 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.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.TransactionReceipt; import tech.pegasys.pantheon.ethereum.eth.manager.EthContext; import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager; @@ -617,7 +617,7 @@ public class DownloaderTest { blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); - final MutableBlockchain shortChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java index 8f6f29ce94..ee979016ba 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java @@ -13,8 +13,8 @@ package tech.pegasys.pantheon.ethereum.eth.sync.tasks; import static org.assertj.core.api.Assertions.assertThat; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java index 9aa3ec1f4a..9b8b5272fb 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java @@ -20,8 +20,8 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ImportBlocksTaskTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ImportBlocksTaskTest.java index 8963dab433..17c6c02924 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ImportBlocksTaskTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ImportBlocksTaskTest.java @@ -13,13 +13,13 @@ package tech.pegasys.pantheon.ethereum.eth.sync.tasks; import static org.assertj.core.api.Assertions.assertThat; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; 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.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.TransactionReceipt; import tech.pegasys.pantheon.ethereum.eth.manager.AbstractPeerTask.PeerTaskResult; import tech.pegasys.pantheon.ethereum.eth.manager.EthPeer; @@ -165,7 +165,7 @@ public class ImportBlocksTaskTest blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); - final MutableBlockchain shortChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/PipelinedImportChainSegmentTaskTest.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/PipelinedImportChainSegmentTaskTest.java index 4f74224e6c..c663f8636a 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/PipelinedImportChainSegmentTaskTest.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/PipelinedImportChainSegmentTaskTest.java @@ -13,13 +13,13 @@ package tech.pegasys.pantheon.ethereum.eth.sync.tasks; import static org.assertj.core.api.Assertions.assertThat; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; 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.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.TransactionReceipt; import tech.pegasys.pantheon.ethereum.eth.manager.EthPeer; import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManagerTestUtil; @@ -371,7 +371,7 @@ public class PipelinedImportChainSegmentTaskTest blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); - final MutableBlockchain shortChain = InMemoryTestFixture.createInMemoryBlockchain(genesisBlock); + final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= lastBlockToInclude) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); diff --git a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/transactions/TestNode.java b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/transactions/TestNode.java index 8978c34eba..fb478c3352 100644 --- a/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/transactions/TestNode.java +++ b/ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/transactions/TestNode.java @@ -14,8 +14,8 @@ package tech.pegasys.pantheon.ethereum.eth.transactions; import static java.util.Collections.singletonList; import static org.assertj.core.util.Preconditions.checkNotNull; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.crypto.SECP256K1; diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java index 4d685b1180..fa281775ef 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java @@ -13,6 +13,8 @@ package tech.pegasys.pantheon.ethereum.jsonrpc; import static org.mockito.Mockito.mock; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator; @@ -20,7 +22,6 @@ import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; import tech.pegasys.pantheon.ethereum.core.Block; import tech.pegasys.pantheon.ethereum.core.BlockImporter; import tech.pegasys.pantheon.ethereum.core.Hash; -import tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture; import tech.pegasys.pantheon.ethereum.core.Synchronizer; import tech.pegasys.pantheon.ethereum.core.TransactionPool; import tech.pegasys.pantheon.ethereum.db.WorldStateArchive; @@ -50,12 +51,11 @@ public class JsonRpcTestMethodsFactory { } public Map methods() { - final WorldStateArchive stateArchive = InMemoryTestFixture.createInMemoryWorldStateArchive(); + final WorldStateArchive stateArchive = createInMemoryWorldStateArchive(); importer.getGenesisState().writeStateTo(stateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); - final MutableBlockchain blockchain = - InMemoryTestFixture.createInMemoryBlockchain(importer.getGenesisBlock()); + final MutableBlockchain blockchain = createInMemoryBlockchain(importer.getGenesisBlock()); final ProtocolContext context = new ProtocolContext<>(blockchain, stateArchive, null); for (final Block block : importer.getBlocks()) { diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java index 8a3977772d..0f2b396024 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java @@ -16,8 +16,8 @@ import static org.assertj.core.api.Assertions.assertThat; 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 static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/queries/BlockchainQueriesTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/queries/BlockchainQueriesTest.java index ed78ae0360..3a2a64f6e8 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/queries/BlockchainQueriesTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/queries/BlockchainQueriesTest.java @@ -17,8 +17,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryBlockchain; -import static tech.pegasys.pantheon.ethereum.core.InMemoryTestFixture.createInMemoryWorldStateArchive; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain; +import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; import tech.pegasys.pantheon.ethereum.core.Account; diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonControllerBuilder.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonControllerBuilder.java index 63a9136db9..ae0c9f4bc8 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonControllerBuilder.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonControllerBuilder.java @@ -14,6 +14,7 @@ package tech.pegasys.pantheon.cli; import static java.nio.charset.StandardCharsets.UTF_8; import static tech.pegasys.pantheon.controller.KeyPairUtil.loadKeyPair; +import static tech.pegasys.pantheon.controller.PantheonController.DATABASE_PATH; import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.controller.MainnetPantheonController; @@ -22,6 +23,8 @@ import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.ethereum.core.MiningParameters; import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.RocksDbStorageProvider; import java.io.File; import java.io.IOException; @@ -43,10 +46,13 @@ public class PantheonControllerBuilder { // instantiate a controller with mainnet config if no genesis file is defined // otherwise use the indicated genesis file final KeyPair nodeKeys = loadKeyPair(nodePrivateKeyFile); + + final StorageProvider storageProvider = + RocksDbStorageProvider.create(homePath.resolve(DATABASE_PATH)); if (isDevMode) { final GenesisConfigFile genesisConfig = GenesisConfigFile.development(); return MainnetPantheonController.init( - homePath, + storageProvider, genesisConfig, DevelopmentProtocolSchedule.create(genesisConfig.getConfigOptions()), synchronizerConfiguration, @@ -55,10 +61,11 @@ public class PantheonControllerBuilder { } else { final String genesisConfig = Resources.toString(ethNetworkConfig.getGenesisConfig().toURL(), UTF_8); + final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisConfig); return PantheonController.fromConfig( + genesisConfigFile, synchronizerConfiguration, - genesisConfig, - homePath, + storageProvider, syncWithOttoman, ethNetworkConfig.getNetworkId(), miningParameters, diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java index 40fd3e4294..915c51ba5c 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java @@ -16,7 +16,6 @@ import static org.apache.logging.log4j.LogManager.getLogger; import tech.pegasys.pantheon.config.CliqueConfigOptions; import tech.pegasys.pantheon.config.GenesisConfigFile; -import tech.pegasys.pantheon.config.GenesisConfigOptions; import tech.pegasys.pantheon.consensus.clique.CliqueContext; import tech.pegasys.pantheon.consensus.clique.CliqueProtocolSchedule; import tech.pegasys.pantheon.consensus.clique.CliqueVotingBlockInterface; @@ -32,14 +31,13 @@ import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator; import tech.pegasys.pantheon.ethereum.chain.GenesisState; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; -import tech.pegasys.pantheon.ethereum.core.BlockHashFunction; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.core.MiningParameters; import tech.pegasys.pantheon.ethereum.core.Synchronizer; import tech.pegasys.pantheon.ethereum.core.TransactionPool; import tech.pegasys.pantheon.ethereum.core.Util; +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; 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.eth.EthProtocol; import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager; @@ -49,16 +47,12 @@ import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState; import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPoolFactory; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; -import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; -import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; -import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.time.Clock; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -69,7 +63,6 @@ import org.apache.logging.log4j.Logger; public class CliquePantheonController implements PantheonController { private static final Logger LOG = getLogger(); - private final GenesisConfigOptions genesisConfigOptions; private final ProtocolSchedule protocolSchedule; private final ProtocolContext context; private final Synchronizer synchronizer; @@ -81,7 +74,6 @@ public class CliquePantheonController implements PantheonController protocolSchedule, final ProtocolContext context, final ProtocolManager ethProtocolManager, @@ -91,7 +83,6 @@ public class CliquePantheonController implements PantheonController init( - final Path home, + final StorageProvider storageProvider, final GenesisConfigFile genesisConfig, final SynchronizerConfiguration taintedSyncConfig, final MiningParameters miningParams, final int networkId, - final KeyPair nodeKeys) - throws IOException { + final KeyPair nodeKeys) { final CliqueConfigOptions cliqueConfig = genesisConfig.getConfigOptions().getCliqueConfigOptions(); final long blocksPerEpoch = cliqueConfig.getEpochLength(); final long secondsBetweenBlocks = cliqueConfig.getBlockPeriodSeconds(); final EpochManager epochManger = new EpochManager(blocksPerEpoch); - final KeyValueStorage kv = - RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); final ProtocolSchedule protocolSchedule = CliqueProtocolSchedule.create(genesisConfig.getConfigOptions(), nodeKeys); - final BlockHashFunction blockHashFunction = - ScheduleBasedBlockHashFunction.create(protocolSchedule); final GenesisState genesisState = GenesisState.fromConfig(genesisConfig, protocolSchedule); - final KeyValueStoragePrefixedKeyBlockchainStorage blockchainStorage = - new KeyValueStoragePrefixedKeyBlockchainStorage(kv, blockHashFunction); + final BlockchainStorage blockchainStorage = + storageProvider.createBlockchainStorage(protocolSchedule); final MutableBlockchain blockchain = new DefaultMutableBlockchain(genesisState.getBlock(), blockchainStorage); - final KeyValueStorageWorldStateStorage worldStateStorage = - new KeyValueStorageWorldStateStorage(kv); + final WorldStateStorage worldStateStorage = storageProvider.createWorldStateStorage(); final WorldStateArchive worldStateArchive = new WorldStateArchive(worldStateStorage); genesisState.writeStateTo(worldStateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); @@ -191,7 +176,6 @@ public class CliquePantheonController implements PantheonController { private static final Logger LOG = getLogger(); - private final GenesisConfigOptions genesisConfig; private final ProtocolSchedule protocolSchedule; private final ProtocolContext context; private final Synchronizer synchronizer; @@ -86,7 +79,6 @@ public class IbftPantheonController implements PantheonController { private final Runnable closer; IbftPantheonController( - final GenesisConfigOptions genesisConfig, final ProtocolSchedule protocolSchedule, final ProtocolContext context, final SubProtocol ethSubProtocol, @@ -98,7 +90,6 @@ public class IbftPantheonController implements PantheonController { final IbftProcessor ibftProcessor, final Runnable closer) { - this.genesisConfig = genesisConfig; this.protocolSchedule = protocolSchedule; this.context = context; this.ethSubProtocol = ethSubProtocol; @@ -112,27 +103,21 @@ public class IbftPantheonController implements PantheonController { } public static PantheonController init( - final Path home, + final StorageProvider storageProvider, final GenesisConfigFile genesisConfig, final SynchronizerConfiguration taintedSyncConfig, final boolean ottomanTestnetOperation, final int networkId, - final KeyPair nodeKeys) - throws IOException { - final KeyValueStorage kv = - RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); + final KeyPair nodeKeys) { final ProtocolSchedule protocolSchedule = IbftProtocolSchedule.create(genesisConfig.getConfigOptions()); - final BlockHashFunction blockHashFunction = - ScheduleBasedBlockHashFunction.create(protocolSchedule); final GenesisState genesisState = GenesisState.fromConfig(genesisConfig, protocolSchedule); - final KeyValueStoragePrefixedKeyBlockchainStorage blockchainStorage = - new KeyValueStoragePrefixedKeyBlockchainStorage(kv, blockHashFunction); + final BlockchainStorage blockchainStorage = + storageProvider.createBlockchainStorage(protocolSchedule); final MutableBlockchain blockchain = new DefaultMutableBlockchain(genesisState.getBlock(), blockchainStorage); - final KeyValueStorageWorldStateStorage worldStateStorage = - new KeyValueStorageWorldStateStorage(kv); + final WorldStateStorage worldStateStorage = storageProvider.createWorldStateStorage(); final WorldStateArchive worldStateArchive = new WorldStateArchive(worldStateStorage); genesisState.writeStateTo(worldStateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); @@ -202,9 +187,9 @@ public class IbftPantheonController implements PantheonController { LOG.error("Failed to shutdown ibft processor executor"); } try { - kv.close(); + storageProvider.close(); } catch (final IOException e) { - LOG.error("Failed to close key value storage", e); + LOG.error("Failed to close storage provider", e); } }; @@ -216,7 +201,6 @@ public class IbftPantheonController implements PantheonController { new IbftNetworkPeers(protocolContext.getConsensusState().getVoteTally()); return new IbftPantheonController( - genesisConfig.getConfigOptions(), protocolSchedule, protocolContext, ethSubProtocol, @@ -239,11 +223,6 @@ public class IbftPantheonController implements PantheonController { return protocolSchedule; } - @Override - public GenesisConfigOptions getGenesisConfigOptions() { - return genesisConfig; - } - @Override public Synchronizer getSynchronizer() { return synchronizer; diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java index 4f1ed72ffb..fcc4353608 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java @@ -12,10 +12,7 @@ */ package tech.pegasys.pantheon.controller; -import static tech.pegasys.pantheon.controller.KeyPairUtil.loadKeyPair; - import tech.pegasys.pantheon.config.GenesisConfigFile; -import tech.pegasys.pantheon.config.GenesisConfigOptions; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.blockcreation.DefaultBlockScheduler; @@ -24,13 +21,12 @@ import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator; import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator; import tech.pegasys.pantheon.ethereum.chain.GenesisState; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; -import tech.pegasys.pantheon.ethereum.core.BlockHashFunction; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.core.MiningParameters; import tech.pegasys.pantheon.ethereum.core.Synchronizer; import tech.pegasys.pantheon.ethereum.core.TransactionPool; +import tech.pegasys.pantheon.ethereum.db.BlockchainStorage; 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.eth.EthProtocol; import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager; @@ -42,16 +38,11 @@ import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPoolFactory; import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHeaderValidator; import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; -import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; -import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; -import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; -import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.time.Clock; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -64,7 +55,6 @@ public class MainnetPantheonController implements PantheonController { private static final Logger LOG = LogManager.getLogger(); - private final GenesisConfigFile genesisConfig; private final ProtocolSchedule protocolSchedule; private final ProtocolContext protocolContext; private final ProtocolManager ethProtocolManager; @@ -76,7 +66,6 @@ public class MainnetPantheonController implements PantheonController { private final Runnable close; public MainnetPantheonController( - final GenesisConfigFile genesisConfig, final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext, final ProtocolManager ethProtocolManager, @@ -85,7 +74,6 @@ public class MainnetPantheonController implements PantheonController { final TransactionPool transactionPool, final MiningCoordinator miningCoordinator, final Runnable close) { - this.genesisConfig = genesisConfig; this.protocolSchedule = protocolSchedule; this.protocolContext = protocolContext; this.ethProtocolManager = ethProtocolManager; @@ -96,40 +84,22 @@ public class MainnetPantheonController implements PantheonController { this.close = close; } - public static PantheonController mainnet(final Path home) throws IOException { - final MiningParameters miningParams = new MiningParameters(null, null, null, false); - final KeyPair nodeKeys = loadKeyPair(home); - final GenesisConfigFile genesisConfig = GenesisConfigFile.mainnet(); - return init( - home, - genesisConfig, - MainnetProtocolSchedule.fromConfig(genesisConfig.getConfigOptions()), - SynchronizerConfiguration.builder().build(), - miningParams, - nodeKeys); - } - public static PantheonController init( - final Path home, + final StorageProvider storageProvider, final GenesisConfigFile genesisConfig, final ProtocolSchedule protocolSchedule, final SynchronizerConfiguration taintedSyncConfig, final MiningParameters miningParams, - final KeyPair nodeKeys) - throws IOException { + final KeyPair nodeKeys) { final GenesisState genesisState = GenesisState.fromConfig(genesisConfig, protocolSchedule); - final KeyValueStorage kv = - RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); - final BlockHashFunction blockHashFunction = - ScheduleBasedBlockHashFunction.create(protocolSchedule); - final KeyValueStoragePrefixedKeyBlockchainStorage blockchainStorage = - new KeyValueStoragePrefixedKeyBlockchainStorage(kv, blockHashFunction); + final BlockchainStorage blockchainStorage = + storageProvider.createBlockchainStorage(protocolSchedule); final MutableBlockchain blockchain = new DefaultMutableBlockchain(genesisState.getBlock(), blockchainStorage); final WorldStateArchive worldStateArchive = - new WorldStateArchive(new KeyValueStorageWorldStateStorage(kv)); + new WorldStateArchive(storageProvider.createWorldStateStorage()); genesisState.writeStateTo(worldStateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); final ProtocolContext protocolContext = @@ -182,7 +152,6 @@ public class MainnetPantheonController implements PantheonController { } return new MainnetPantheonController( - genesisConfig, protocolSchedule, protocolContext, ethProtocolManager, @@ -199,9 +168,9 @@ public class MainnetPantheonController implements PantheonController { LOG.error("Failed to shutdown miner executor"); } try { - kv.close(); + storageProvider.close(); } catch (final IOException e) { - LOG.error("Failed to close key value storage", e); + LOG.error("Failed to close storage provider", e); } }); } @@ -216,11 +185,6 @@ public class MainnetPantheonController implements PantheonController { return protocolSchedule; } - @Override - public GenesisConfigOptions getGenesisConfigOptions() { - return genesisConfig.getConfigOptions(); - } - @Override public Synchronizer getSynchronizer() { return synchronizer; diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java index f0445d3f7c..3cbc739121 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java @@ -24,42 +24,44 @@ import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Path; public interface PantheonController extends Closeable { String DATABASE_PATH = "database"; static PantheonController fromConfig( + final GenesisConfigFile genesisConfigFile, final SynchronizerConfiguration syncConfig, - final String configContents, - final Path pantheonHome, + final StorageProvider storageProvider, final boolean ottomanTestnetOperation, final int networkId, final MiningParameters miningParameters, - final KeyPair nodeKeys) - throws IOException { + final KeyPair nodeKeys) { - final GenesisConfigFile config = GenesisConfigFile.fromConfig(configContents); - final GenesisConfigOptions configOptions = config.getConfigOptions(); + final GenesisConfigOptions configOptions = genesisConfigFile.getConfigOptions(); if (configOptions.isEthHash()) { return MainnetPantheonController.init( - pantheonHome, - config, + storageProvider, + genesisConfigFile, MainnetProtocolSchedule.fromConfig(configOptions), syncConfig, miningParameters, nodeKeys); } else if (configOptions.isIbft()) { return IbftPantheonController.init( - pantheonHome, config, syncConfig, ottomanTestnetOperation, networkId, nodeKeys); + storageProvider, + genesisConfigFile, + syncConfig, + ottomanTestnetOperation, + networkId, + nodeKeys); } else if (configOptions.isClique()) { return CliquePantheonController.init( - pantheonHome, config, syncConfig, miningParameters, networkId, nodeKeys); + storageProvider, genesisConfigFile, syncConfig, miningParameters, networkId, nodeKeys); } else { throw new IllegalArgumentException("Unknown consensus mechanism defined"); } @@ -69,8 +71,6 @@ public interface PantheonController extends Closeable { ProtocolSchedule getProtocolSchedule(); - GenesisConfigOptions getGenesisConfigOptions(); - Synchronizer getSynchronizer(); SubProtocolConfiguration subProtocolConfiguration(); diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java index 26fab18278..8389cf47c7 100644 --- a/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java +++ b/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java @@ -23,6 +23,7 @@ import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.core.Block; import tech.pegasys.pantheon.ethereum.core.BlockImporter; import tech.pegasys.pantheon.ethereum.core.BlockSyncTestUtils; +import tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider; import tech.pegasys.pantheon.ethereum.core.MiningParametersTestBuilder; import tech.pegasys.pantheon.ethereum.eth.sync.SyncMode; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; @@ -33,8 +34,11 @@ import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec; import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; +import tech.pegasys.pantheon.ethereum.storage.StorageProvider; +import tech.pegasys.pantheon.ethereum.storage.keyvalue.RocksDbStorageProvider; import tech.pegasys.pantheon.util.uint.UInt256; +import java.io.IOException; import java.net.InetAddress; import java.nio.file.Path; import java.util.Collections; @@ -64,7 +68,6 @@ import org.junit.rules.TemporaryFolder; /** Tests for {@link Runner}. */ public final class RunnerTest { - private static final int NETWORK_ID = 10; @Rule public final TemporaryFolder temp = new TemporaryFolder(); @Test @@ -93,7 +96,7 @@ public final class RunnerTest { // Setup state with block data try (final PantheonController controller = MainnetPantheonController.init( - dbAhead, + createKeyValueStorageProvider(dbAhead), GenesisConfigFile.mainnet(), MainnetProtocolSchedule.create(), fastSyncConfig, @@ -105,7 +108,7 @@ public final class RunnerTest { // Setup Runner with blocks final PantheonController controllerAhead = MainnetPantheonController.init( - dbAhead, + createKeyValueStorageProvider(dbAhead), GenesisConfigFile.mainnet(), MainnetProtocolSchedule.create(), fastSyncConfig, @@ -136,16 +139,14 @@ public final class RunnerTest { final WebSocketConfiguration behindWebSocketConfiguration = wsRpcConfiguration(); // Setup runner with no block data - final Path dbBehind = temp.newFolder().toPath(); - final KeyPair behindDbNodeKeys = loadKeyPair(dbBehind.resolve("key").toFile()); final PantheonController controllerBehind = MainnetPantheonController.init( - temp.newFolder().toPath(), + new InMemoryStorageProvider(), GenesisConfigFile.mainnet(), MainnetProtocolSchedule.create(), fastSyncConfig, new MiningParametersTestBuilder().enabled(false).build(), - behindDbNodeKeys); + KeyPair.generate()); final Runner runnerBehind = runnerBuilder.build( Vertx.vertx(), @@ -162,7 +163,7 @@ public final class RunnerTest { 3, behindJsonRpcConfiguration, behindWebSocketConfiguration, - dbBehind, + temp.newFolder().toPath(), Collections.emptySet()); executorService.submit(runnerBehind::execute); @@ -233,6 +234,10 @@ public final class RunnerTest { } } + private StorageProvider createKeyValueStorageProvider(final Path dbAhead) throws IOException { + return RocksDbStorageProvider.create(dbAhead); + } + private JsonRpcConfiguration jsonRpcConfiguration() { final JsonRpcConfiguration configuration = JsonRpcConfiguration.createDefault(); configuration.setPort(0); diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/util/BlockImporterTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/util/BlockImporterTest.java index 6483b4266b..32344c044c 100644 --- a/pantheon/src/test/java/tech/pegasys/pantheon/util/BlockImporterTest.java +++ b/pantheon/src/test/java/tech/pegasys/pantheon/util/BlockImporterTest.java @@ -15,9 +15,10 @@ package tech.pegasys.pantheon.util; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; -import tech.pegasys.pantheon.controller.MainnetPantheonController; +import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.controller.PantheonController; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; +import tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider; import tech.pegasys.pantheon.ethereum.core.MiningParametersTestBuilder; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; import tech.pegasys.pantheon.testutil.BlockTestUtil; @@ -43,10 +44,18 @@ public final class BlockImporterTest { @Test public void blockImport() throws IOException { final Path source = folder.newFile().toPath(); - final Path target = folder.newFolder().toPath(); BlockTestUtil.write1000Blocks(source); + final PantheonController targetController = + PantheonController.fromConfig( + GenesisConfigFile.mainnet(), + SynchronizerConfiguration.builder().build(), + new InMemoryStorageProvider(), + false, + 1, + new MiningParametersTestBuilder().enabled(false).build(), + KeyPair.generate()); final BlockImporter.ImportResult result = - blockImporter.importBlockchain(source, MainnetPantheonController.mainnet(target)); + blockImporter.importBlockchain(source, targetController); assertThat(result.count).isEqualTo(1000); assertThat(result.td).isEqualTo(UInt256.of(21991996248790L)); } @@ -54,7 +63,6 @@ public final class BlockImporterTest { @Test public void ibftImport() throws IOException { final Path source = folder.newFile().toPath(); - final Path target = folder.newFolder().toPath(); final String config = Resources.toString(Resources.getResource("ibft_genesis.json"), UTF_8); try { @@ -69,9 +77,9 @@ public final class BlockImporterTest { final PantheonController controller = PantheonController.fromConfig( + GenesisConfigFile.fromConfig(config), SynchronizerConfiguration.builder().build(), - config, - target, + new InMemoryStorageProvider(), false, 10, new MiningParametersTestBuilder().enabled(false).build(),