diff --git a/.gitignore b/.gitignore index d4b55c98..ad545edc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,8 @@ .classpath .externalToolBuilders/ .gradle/ -.idea/ +.idea/* +!.idea/codeStyles/ .loadpath .metadata .prefs diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..57a04781 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..79ee123c --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/eth-reference-tests/src/test/java/net/consensys/cava/eth/reference/MerkleTrieTestSuite.java b/eth-reference-tests/src/test/java/net/consensys/cava/eth/reference/MerkleTrieTestSuite.java index 01704822..33806184 100644 --- a/eth-reference-tests/src/test/java/net/consensys/cava/eth/reference/MerkleTrieTestSuite.java +++ b/eth-reference-tests/src/test/java/net/consensys/cava/eth/reference/MerkleTrieTestSuite.java @@ -18,14 +18,13 @@ import net.consensys.cava.bytes.Bytes; import net.consensys.cava.io.Resources; import net.consensys.cava.junit.BouncyCastleExtension; -import net.consensys.cava.trie.experimental.MerklePatriciaTrie; +import net.consensys.cava.trie.MerklePatriciaTrie; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; import java.util.List; import java.util.Map; -import java.util.function.Function; import java.util.stream.Stream; import com.fasterxml.jackson.databind.ObjectMapper; @@ -50,7 +49,7 @@ private Bytes readFromString(String value) { @MethodSource("readAnyOrderTrieTests") @SuppressWarnings({"unchecked", "rawtypes"}) void testAnyOrderTrieTrees(String name, Map input, String root) throws Exception { - MerklePatriciaTrie trie = new MerklePatriciaTrie<>((Function) this::readFromString); + MerklePatriciaTrie trie = MerklePatriciaTrie.create(this::readFromString); for (Object entry : input.entrySet()) { Map.Entry keyValue = (Map.Entry) entry; trie.putAsync(readFromString((String) keyValue.getKey()), (String) keyValue.getValue()).join(); @@ -62,7 +61,7 @@ void testAnyOrderTrieTrees(String name, Map input, String root) throws Exception @MethodSource("readTrieTests") @SuppressWarnings({"unchecked", "rawtypes"}) void testTrieTrees(String name, List input, String root) throws Exception { - MerklePatriciaTrie trie = new MerklePatriciaTrie<>((Function) this::readFromString); + MerklePatriciaTrie trie = MerklePatriciaTrie.create(this::readFromString); for (Object entry : input) { List keyValue = (List) entry; trie.putAsync(readFromString((String) keyValue.get(0)), (String) keyValue.get(1)).join(); diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..7fc6f1ff --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/kv/src/main/java/net/consensys/cava/kv/KeyValueStore.java b/kv/src/main/java/net/consensys/cava/kv/KeyValueStore.java deleted file mode 100644 index 74c322a6..00000000 --- a/kv/src/main/java/net/consensys/cava/kv/KeyValueStore.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 net.consensys.cava.kv; - -import net.consensys.cava.bytes.Bytes; -import net.consensys.cava.concurrent.AsyncCompletion; -import net.consensys.cava.concurrent.AsyncResult; - -import java.io.Closeable; - -/** - * A key-value store. - */ -public interface KeyValueStore extends Closeable { - - /** - * Retrieves data from the store. - * - * @param key The key for the content. - * @return An {@link AsyncResult} that will complete with the stored content, or {@code null} if no content was - * available. - */ - AsyncResult getAsync(Bytes key); - - /** - * Puts data into the store. - * - * Note: if the storage implementation already contains content for the given key, it does not need to replace the - * existing content. - * - * @param key The key to associate with the data, for use when retrieving. - * @param value The data to store. - * @return An {@link AsyncCompletion} that will complete when the content is stored. - */ - AsyncCompletion putAsync(Bytes key, Bytes value); -} diff --git a/kv/src/main/java/net/consensys/cava/kv/LevelDBKeyValueStore.java b/kv/src/main/java/net/consensys/cava/kv/LevelDBKeyValueStore.java deleted file mode 100644 index 957eb13d..00000000 --- a/kv/src/main/java/net/consensys/cava/kv/LevelDBKeyValueStore.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 net.consensys.cava.kv; - -import java.io.IOException; -import java.nio.file.Path; - -import org.iq80.leveldb.Options; - -/** - * A key-value store backed by LevelDB. - */ -public interface LevelDBKeyValueStore extends KeyValueStore { - - /** - * Open a LevelDB-backed key-value store. - * - * @param dbPath The path to the levelDB database. - * @return A key-value store. - * @throws IOException If an I/O error occurs. - */ - static LevelDBKeyValueStore open(Path dbPath) throws IOException { - return new net.consensys.cava.kv.experimental.LevelDBKeyValueStore(dbPath); - } - - /** - * Open a LevelDB-backed key-value store. - * - * @param dbPath The path to the levelDB database. - * @param options Options for the levelDB database. - * @return A key-value store. - * @throws IOException If an I/O error occurs. - */ - static LevelDBKeyValueStore open(Path dbPath, Options options) throws IOException { - return new net.consensys.cava.kv.experimental.LevelDBKeyValueStore(dbPath, options); - } -} diff --git a/kv/src/main/java/net/consensys/cava/kv/MapDBKeyValueStore.java b/kv/src/main/java/net/consensys/cava/kv/MapDBKeyValueStore.java deleted file mode 100644 index b7ae8738..00000000 --- a/kv/src/main/java/net/consensys/cava/kv/MapDBKeyValueStore.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 net.consensys.cava.kv; - -import java.io.IOException; -import java.nio.file.Path; - -/** - * A key-value store backed by a MapDB instance. - */ -public interface MapDBKeyValueStore extends KeyValueStore { - - /** - * Open a MapDB-backed key-value store. - * - * @param dbPath The path to the MapDB database. - * @return A key-value store. - * @throws IOException If an I/O error occurs. - */ - static MapDBKeyValueStore open(Path dbPath) throws IOException { - return new net.consensys.cava.kv.experimental.MapDBKeyValueStore(dbPath); - } -} diff --git a/kv/src/main/java/net/consensys/cava/kv/MapKeyValueStore.java b/kv/src/main/java/net/consensys/cava/kv/MapKeyValueStore.java deleted file mode 100644 index 029cf13d..00000000 --- a/kv/src/main/java/net/consensys/cava/kv/MapKeyValueStore.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 net.consensys.cava.kv; - -import net.consensys.cava.bytes.Bytes; - -import java.util.Map; - -/** - * A key-value store backed by an in-memory Map. - */ -public interface MapKeyValueStore extends KeyValueStore { - - /** - * Open an in-memory key-value store. - * - * This store will use a {@link java.util.HashMap} as a backing store. - * - * @return A key-value store. - */ - static MapKeyValueStore open() { - return new net.consensys.cava.kv.experimental.MapKeyValueStore(); - } - - /** - * Open an in-memory key-value store. - * - * @param map The backing map for this store. - * @return A key-value store. - */ - static MapKeyValueStore open(Map map) { - return new net.consensys.cava.kv.experimental.MapKeyValueStore(map); - } -} diff --git a/kv/src/main/java/net/consensys/cava/kv/RedisKeyValueStore.java b/kv/src/main/java/net/consensys/cava/kv/RedisKeyValueStore.java deleted file mode 100644 index 1ce0ad06..00000000 --- a/kv/src/main/java/net/consensys/cava/kv/RedisKeyValueStore.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 net.consensys.cava.kv; - -import net.consensys.cava.bytes.Bytes; - -import java.net.InetAddress; - -import io.lettuce.core.codec.RedisCodec; - -/** - * A key-value store backed by Redis. - */ -public interface RedisKeyValueStore extends KeyValueStore { - - /** - * Open a Redis-backed key-value store. - * - * @param uri The uri to the Redis store. - * @return A key-value store. - */ - static RedisKeyValueStore open(String uri) { - return new net.consensys.cava.kv.experimental.RedisKeyValueStore(uri); - } - - /** - * Open a Redis-backed key-value store. - * - * @param port The port for the Redis store. - * @return A key-value store. - */ - static RedisKeyValueStore open(int port) { - return new net.consensys.cava.kv.experimental.RedisKeyValueStore(port); - } - - /** - * Open a Redis-backed key-value store. - * - * @param address The address for the Redis store. - * @return A key-value store. - */ - static RedisKeyValueStore open(InetAddress address) { - return new net.consensys.cava.kv.experimental.RedisKeyValueStore(6379, address); - } - - /** - * Open a Redis-backed key-value store. - * - * @param port The port for the Redis store. - * @param address The address for the Redis store. - * @return A key-value store. - */ - static RedisKeyValueStore open(int port, InetAddress address) { - return new net.consensys.cava.kv.experimental.RedisKeyValueStore(port, address); - } - - /** - * A {@link RedisCodec} for working with cava Bytes classes. - * - * @return A {@link RedisCodec} for working with cava Bytes classes. - */ - static RedisCodec codec() { - return new RedisBytesCodec(); - } -} diff --git a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/KeyValueStore.kt b/kv/src/main/kotlin/net/consensys/cava/kv/KeyValueStore.kt similarity index 88% rename from kv/src/main/kotlin/net/consensys/cava/kv/experimental/KeyValueStore.kt rename to kv/src/main/kotlin/net/consensys/cava/kv/KeyValueStore.kt index 05ecce03..cb6f59aa 100644 --- a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/KeyValueStore.kt +++ b/kv/src/main/kotlin/net/consensys/cava/kv/KeyValueStore.kt @@ -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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers @@ -20,13 +20,12 @@ import net.consensys.cava.concurrent.AsyncCompletion import net.consensys.cava.concurrent.AsyncResult import net.consensys.cava.concurrent.coroutines.asyncCompletion import net.consensys.cava.concurrent.coroutines.asyncResult +import java.io.Closeable /** * A key-value store. - * - * This interface extends [net.consensys.cava.kv.KeyValueStore], exposing co-routine based access methods. */ -interface KeyValueStore : net.consensys.cava.kv.KeyValueStore { +interface KeyValueStore : Closeable { /** * Retrieves data from the store. @@ -43,7 +42,7 @@ interface KeyValueStore : net.consensys.cava.kv.KeyValueStore { * @return An [AsyncResult] that will complete with the stored content, * or an empty optional if no content was available. */ - override fun getAsync(key: Bytes): AsyncResult = getAsync(Dispatchers.Default, key) + fun getAsync(key: Bytes): AsyncResult = getAsync(Dispatchers.Default, key) /** * Retrieves data from the store. @@ -74,7 +73,7 @@ interface KeyValueStore : net.consensys.cava.kv.KeyValueStore { * @param value The data to store. * @return An [AsyncCompletion] that will complete when the content is stored. */ - override fun putAsync(key: Bytes, value: Bytes): AsyncCompletion = putAsync(Dispatchers.Default, key, value) + fun putAsync(key: Bytes, value: Bytes): AsyncCompletion = putAsync(Dispatchers.Default, key, value) /** * Puts data into the store. diff --git a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/LevelDBKeyValueStore.kt b/kv/src/main/kotlin/net/consensys/cava/kv/LevelDBKeyValueStore.kt similarity index 72% rename from kv/src/main/kotlin/net/consensys/cava/kv/experimental/LevelDBKeyValueStore.kt rename to kv/src/main/kotlin/net/consensys/cava/kv/LevelDBKeyValueStore.kt index 20de945d..503b97f9 100644 --- a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/LevelDBKeyValueStore.kt +++ b/kv/src/main/kotlin/net/consensys/cava/kv/LevelDBKeyValueStore.kt @@ -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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers @@ -34,13 +34,37 @@ import java.nio.file.Path * @constructor Open a LevelDB-backed key-value store. */ class LevelDBKeyValueStore -@JvmOverloads @Throws(IOException::class) constructor( dbPath: Path, options: Options = Options().createIfMissing(true).cacheSize((100 * 1048576).toLong()), private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : KeyValueStore, net.consensys.cava.kv.LevelDBKeyValueStore { +) : KeyValueStore { + + companion object { + /** + * Open a LevelDB-backed key-value store. + * + * @param dbPath The path to the levelDB database. + * @return A key-value store. + * @throws IOException If an I/O error occurs. + */ + @JvmStatic + @Throws(IOException::class) + fun open(dbPath: Path) = LevelDBKeyValueStore(dbPath) + + /** + * Open a LevelDB-backed key-value store. + * + * @param dbPath The path to the levelDB database. + * @param options Options for the levelDB database. + * @return A key-value store. + * @throws IOException If an I/O error occurs. + */ + @JvmStatic + @Throws(IOException::class) + fun open(dbPath: Path, options: Options) = LevelDBKeyValueStore(dbPath, options) + } private val db: DB diff --git a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapDBKeyValueStore.kt b/kv/src/main/kotlin/net/consensys/cava/kv/MapDBKeyValueStore.kt similarity index 82% rename from kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapDBKeyValueStore.kt rename to kv/src/main/kotlin/net/consensys/cava/kv/MapDBKeyValueStore.kt index 376c71c3..da90d652 100644 --- a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapDBKeyValueStore.kt +++ b/kv/src/main/kotlin/net/consensys/cava/kv/MapDBKeyValueStore.kt @@ -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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers @@ -35,12 +35,24 @@ import java.nio.file.Path * @constructor Open a MapDB-backed key-value store. */ class MapDBKeyValueStore -@JvmOverloads @Throws(IOException::class) constructor( dbPath: Path, private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : KeyValueStore, net.consensys.cava.kv.MapDBKeyValueStore { +) : KeyValueStore { + + companion object { + /** + * Open a MapDB-backed key-value store. + * + * @param dbPath The path to the MapDB database. + * @return A key-value store. + * @throws IOException If an I/O error occurs. + */ + @JvmStatic + @Throws(IOException::class) + fun open(dbPath: Path) = MapDBKeyValueStore(dbPath) + } private val db: DB private val storageData: HTreeMap @@ -48,7 +60,11 @@ constructor( init { Files.createDirectories(dbPath.parent) db = DBMaker.fileDB(dbPath.toFile()).transactionEnable().closeOnJvmShutdown().make() - storageData = db.hashMap("storageData", BytesSerializer(), BytesSerializer()).createOrOpen() + storageData = db.hashMap( + "storageData", + BytesSerializer(), + BytesSerializer() + ).createOrOpen() } override suspend fun get(key: Bytes): Bytes? = withContext(dispatcher) { diff --git a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapKeyValueStore.kt b/kv/src/main/kotlin/net/consensys/cava/kv/MapKeyValueStore.kt similarity index 63% rename from kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapKeyValueStore.kt rename to kv/src/main/kotlin/net/consensys/cava/kv/MapKeyValueStore.kt index 42df8062..aa547e8e 100644 --- a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/MapKeyValueStore.kt +++ b/kv/src/main/kotlin/net/consensys/cava/kv/MapKeyValueStore.kt @@ -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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import net.consensys.cava.bytes.Bytes @@ -22,10 +22,28 @@ import net.consensys.cava.bytes.Bytes * @constructor Open an in-memory key-value store. */ class MapKeyValueStore -@JvmOverloads -constructor( - private val map: MutableMap = HashMap() -) : KeyValueStore, net.consensys.cava.kv.MapKeyValueStore { +constructor(private val map: MutableMap = HashMap()) : KeyValueStore { + + companion object { + /** + * Open an in-memory key-value store. + * + * This store will use a [java.util.HashMap] as a backing store. + * + * @return A key-value store. + */ + @JvmStatic + fun open(): MapKeyValueStore = MapKeyValueStore() + + /** + * Open an in-memory key-value store. + * + * @param map The backing map for this store. + * @return A key-value store. + */ + @JvmStatic + fun open(map: MutableMap) = MapKeyValueStore(map) + } override suspend fun get(key: Bytes): Bytes? = map[key] diff --git a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/RedisKeyValueStore.kt b/kv/src/main/kotlin/net/consensys/cava/kv/RedisKeyValueStore.kt similarity index 57% rename from kv/src/main/kotlin/net/consensys/cava/kv/experimental/RedisKeyValueStore.kt rename to kv/src/main/kotlin/net/consensys/cava/kv/RedisKeyValueStore.kt index 568ab697..e683cdf6 100644 --- a/kv/src/main/kotlin/net/consensys/cava/kv/experimental/RedisKeyValueStore.kt +++ b/kv/src/main/kotlin/net/consensys/cava/kv/RedisKeyValueStore.kt @@ -10,12 +10,13 @@ * 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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import io.lettuce.core.RedisClient import io.lettuce.core.RedisURI import io.lettuce.core.api.StatefulRedisConnection import io.lettuce.core.api.async.RedisAsyncCommands +import io.lettuce.core.codec.RedisCodec import kotlinx.coroutines.future.await import net.consensys.cava.bytes.Bytes import java.net.InetAddress @@ -27,8 +28,54 @@ import java.util.concurrent.CompletionStage * @param uri The uri to the Redis store. * @constructor Open a Redis-backed key-value store. */ -class RedisKeyValueStore(uri: String) - : KeyValueStore, net.consensys.cava.kv.RedisKeyValueStore { +class RedisKeyValueStore(uri: String) : KeyValueStore { + + companion object { + /** + * Open a Redis-backed key-value store. + * + * @param uri The uri to the Redis store. + * @return A key-value store. + */ + @JvmStatic + fun open(uri: String) = RedisKeyValueStore(uri) + + /** + * Open a Redis-backed key-value store. + * + * @param port The port for the Redis store. + * @return A key-value store. + */ + @JvmStatic + fun open(port: Int) = RedisKeyValueStore(port) + + /** + * Open a Redis-backed key-value store. + * + * @param address The address for the Redis store. + * @return A key-value store. + */ + @JvmStatic + fun open(address: InetAddress) = RedisKeyValueStore(6379, address) + + /** + * Open a Redis-backed key-value store. + * + * @param port The port for the Redis store. + * @param address The address for the Redis store. + * @return A key-value store. + */ + @JvmStatic + fun open(port: Int, address: InetAddress) = RedisKeyValueStore(port, address) + + /** + * A [RedisCodec] for working with cava Bytes classes. + * + * @return A [RedisCodec] for working with cava Bytes classes. + */ + @JvmStatic + fun codec(): RedisCodec = RedisBytesCodec() + } private val conn: StatefulRedisConnection private val asyncCommands: RedisAsyncCommands @@ -47,7 +94,7 @@ class RedisKeyValueStore(uri: String) init { val redisClient = RedisClient.create(uri) - conn = redisClient.connect(net.consensys.cava.kv.RedisKeyValueStore.codec()) + conn = redisClient.connect(RedisKeyValueStore.codec()) asyncCommands = conn.async() } diff --git a/kv/src/test/kotlin/net/consensys/cava/kv/experimental/KeyValueStoreSpec.kt b/kv/src/test/kotlin/net/consensys/cava/kv/KeyValueStoreSpec.kt similarity index 96% rename from kv/src/test/kotlin/net/consensys/cava/kv/experimental/KeyValueStoreSpec.kt rename to kv/src/test/kotlin/net/consensys/cava/kv/KeyValueStoreSpec.kt index c39585f8..bc7bf717 100644 --- a/kv/src/test/kotlin/net/consensys/cava/kv/experimental/KeyValueStoreSpec.kt +++ b/kv/src/test/kotlin/net/consensys/cava/kv/KeyValueStoreSpec.kt @@ -10,15 +10,15 @@ * 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 net.consensys.cava.kv.experimental +package net.consensys.cava.kv import com.google.common.io.MoreFiles import com.google.common.io.RecursiveDeleteOption import com.winterbe.expekt.should import kotlinx.coroutines.runBlocking import net.consensys.cava.bytes.Bytes -import net.consensys.cava.kv.experimental.Vars.foo -import net.consensys.cava.kv.experimental.Vars.foobar +import net.consensys.cava.kv.Vars.foo +import net.consensys.cava.kv.Vars.foobar import org.iq80.leveldb.DBException import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe diff --git a/merkle-trie/src/main/java/net/consensys/cava/trie/MerklePatriciaTrie.java b/merkle-trie/src/main/java/net/consensys/cava/trie/MerklePatriciaTrie.java deleted file mode 100644 index f3652b27..00000000 --- a/merkle-trie/src/main/java/net/consensys/cava/trie/MerklePatriciaTrie.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 net.consensys.cava.trie; - -import static java.nio.charset.StandardCharsets.UTF_8; - -import net.consensys.cava.bytes.Bytes; - -import java.util.function.Function; - -/** - * An in-memory {@link MerkleTrie}. - */ -public interface MerklePatriciaTrie extends MerkleTrie { - - /** - * Create a trie. - * - * @param valueSerializer A function for serializing values to bytes. - * @param The serialized type. - * @return A new merkle trie. - */ - static MerklePatriciaTrie create(Function valueSerializer) { - return new net.consensys.cava.trie.experimental.MerklePatriciaTrie<>(valueSerializer); - } - - /** - * Create a trie with value of type {@link Bytes}. - * - * @return A new merkle trie. - */ - static MerklePatriciaTrie storingBytes() { - return create(bytes -> bytes); - } - - /** - * Create a trie with value of type {@link String}. - *

- * Strings are stored in UTF-8 encoding. - * - * @return A new merkle trie. - */ - static MerklePatriciaTrie storingStrings() { - return create(s -> Bytes.wrap(s.getBytes(UTF_8))); - } -} diff --git a/merkle-trie/src/main/java/net/consensys/cava/trie/StoredMerklePatriciaTrie.java b/merkle-trie/src/main/java/net/consensys/cava/trie/StoredMerklePatriciaTrie.java deleted file mode 100644 index 0b31da0a..00000000 --- a/merkle-trie/src/main/java/net/consensys/cava/trie/StoredMerklePatriciaTrie.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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 net.consensys.cava.trie; - -import static java.nio.charset.StandardCharsets.UTF_8; - -import net.consensys.cava.bytes.Bytes; -import net.consensys.cava.bytes.Bytes32; - -import java.util.function.Function; - -public interface StoredMerklePatriciaTrie extends MerkleTrie { - - /** - * Create a trie. - * - * @param storage The storage to use for persistence. - * @param valueSerializer A function for serializing values to bytes. - * @param valueDeserializer A function for deserializing values from bytes. - * @param The serialized type. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie create( - MerkleStorage storage, - Function valueSerializer, - Function valueDeserializer) { - return new net.consensys.cava.trie.experimental.StoredMerklePatriciaTrie<>( - new CoroutineMerkleStorageAdapter(storage), - valueSerializer, - valueDeserializer); - } - - /** - * Create a trie. - * - * @param storage The storage to use for persistence. - * @param rootHash The initial root has for the trie, which should be already present in {@code storage}. - * @param valueSerializer A function for serializing values to bytes. - * @param valueDeserializer A function for deserializing values from bytes. - * @param The serialized type. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie create( - MerkleStorage storage, - Bytes32 rootHash, - Function valueSerializer, - Function valueDeserializer) { - return new net.consensys.cava.trie.experimental.StoredMerklePatriciaTrie<>( - new CoroutineMerkleStorageAdapter(storage), - rootHash, - valueSerializer, - valueDeserializer); - } - - /** - * Create a trie with value of type {@link Bytes}. - * - * @param storage The storage to use for persistence. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie storingBytes(MerkleStorage storage) { - return create(storage, bytes -> bytes, bytes -> bytes); - } - - /** - * Create a trie with value of type {@link Bytes}. - * - * @param storage The storage to use for persistence. - * @param rootHash The initial root has for the trie, which should be already present in `storage`. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie storingBytes(MerkleStorage storage, Bytes32 rootHash) { - return create(storage, rootHash, bytes -> bytes, bytes -> bytes); - } - - /** - * Create a trie with value of type {@link String}. - * - * Strings are stored in UTF-8 encoding. - * - * @param storage The storage to use for persistence. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie storingStrings(MerkleStorage storage) { - return create(storage, s -> Bytes.wrap(s.getBytes(UTF_8)), bytes -> new String(bytes.toArrayUnsafe(), UTF_8)); - } - - /** - * Create a trie with value of type {@link String}. - * - * Strings are stored in UTF-8 encoding. - * - * @param storage The storage to use for persistence. - * @param rootHash The initial root has for the trie, which should be already present in `storage`. - * @return A new merkle trie. - */ - static StoredMerklePatriciaTrie storingStrings(MerkleStorage storage, Bytes32 rootHash) { - return create( - storage, - rootHash, - s -> Bytes.wrap(s.getBytes(UTF_8)), - bytes -> new String(bytes.toArrayUnsafe(), UTF_8)); - } - - /** - * Forces any cached trie nodes to be released, so they can be garbage collected. - *

- * Note: nodes are already stored using {@link java.lang.ref.SoftReference}'s, so they will be released automatically - * based on memory demands. - */ - void clearCache(); -} diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/BranchNode.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/BranchNode.kt similarity index 97% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/BranchNode.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/BranchNode.kt index 9a0551e2..75493adc 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/BranchNode.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/BranchNode.kt @@ -10,14 +10,13 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.bytes.MutableBytes import net.consensys.cava.crypto.Hash.keccak256 import net.consensys.cava.rlp.RLP -import net.consensys.cava.trie.CompactEncoding import java.lang.ref.WeakReference private val NULL_NODE: NullNode<*> = NullNode.instance() diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/DefaultNodeFactory.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/DefaultNodeFactory.kt similarity index 93% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/DefaultNodeFactory.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/DefaultNodeFactory.kt index 70f89a14..b0917313 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/DefaultNodeFactory.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/DefaultNodeFactory.kt @@ -10,16 +10,18 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import java.util.Collections -internal class DefaultNodeFactory(private val valueSerializer: (V) -> Bytes) : NodeFactory { +internal class DefaultNodeFactory(private val valueSerializer: (V) -> Bytes) : + NodeFactory { private val nullNode: NullNode = NullNode.instance() - override suspend fun createExtension(path: Bytes, child: Node): Node = ExtensionNode(path, child, this) + override suspend fun createExtension(path: Bytes, child: Node): Node = + ExtensionNode(path, child, this) override suspend fun createBranch(leftIndex: Byte, left: Node, rightIndex: Byte, right: Node): Node { assert(leftIndex <= BranchNode.RADIX) diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/ExtensionNode.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/ExtensionNode.kt similarity index 96% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/ExtensionNode.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/ExtensionNode.kt index 4ce37e6b..773ef4e1 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/ExtensionNode.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/ExtensionNode.kt @@ -10,13 +10,12 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.crypto.Hash.keccak256 import net.consensys.cava.rlp.RLP -import net.consensys.cava.trie.CompactEncoding import java.lang.ref.WeakReference internal class ExtensionNode( diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/GetVisitor.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/GetVisitor.kt similarity index 93% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/GetVisitor.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/GetVisitor.kt index 116890b9..95d5887d 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/GetVisitor.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/GetVisitor.kt @@ -10,10 +10,9 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes -import net.consensys.cava.trie.CompactEncoding internal class GetVisitor : NodeVisitor { @@ -51,5 +50,6 @@ internal class GetVisitor : NodeVisitor { return leafNode } - override suspend fun visit(nullNode: NullNode, path: Bytes): Node = NullNode.instance() + override suspend fun visit(nullNode: NullNode, path: Bytes): Node = + NullNode.instance() } diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/LeafNode.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/LeafNode.kt similarity index 95% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/LeafNode.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/LeafNode.kt index 437dd837..1b653fa8 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/LeafNode.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/LeafNode.kt @@ -10,13 +10,12 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.crypto.Hash.keccak256 import net.consensys.cava.rlp.RLP -import net.consensys.cava.trie.CompactEncoding import java.lang.ref.WeakReference internal class LeafNode( diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrie.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerklePatriciaTrie.kt similarity index 71% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrie.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerklePatriciaTrie.kt index 14643083..ed2d2a66 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrie.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerklePatriciaTrie.kt @@ -10,10 +10,11 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.concurrent.AsyncCompletion @@ -33,16 +34,15 @@ internal fun stringDeserializer(b: Bytes): String = String(b.toArrayUnsafe(), UT * @param valueSerializer A function for serializing values to bytes. * @constructor Creates an empty trie. */ -class MerklePatriciaTrie( - valueSerializer: (V) -> Bytes -) : MerkleTrie, net.consensys.cava.trie.MerklePatriciaTrie { +class MerklePatriciaTrie(valueSerializer: (V) -> Bytes) : MerkleTrie { companion object { /** * Create a trie with keys and values of type [Bytes]. */ @JvmStatic - fun storingBytes(): MerklePatriciaTrie = MerklePatriciaTrie(::bytesIdentity) + fun storingBytes(): MerklePatriciaTrie = + MerklePatriciaTrie(::bytesIdentity) /** * Create a trie with value of type [String]. @@ -50,7 +50,19 @@ class MerklePatriciaTrie( * Strings are stored in UTF-8 encoding. */ @JvmStatic - fun storingStrings(): MerklePatriciaTrie = MerklePatriciaTrie(::stringSerializer) + fun storingStrings(): MerklePatriciaTrie = + MerklePatriciaTrie(::stringSerializer) + + /** + * Create a trie. + * + * @param valueSerializer A function for serializing values to bytes. + * @param The serialized type. + * @return A new merkle trie. + */ + @JvmStatic + fun create(valueSerializer: Function): MerklePatriciaTrie = + MerklePatriciaTrie(valueSerializer::apply) } private val getVisitor = GetVisitor() @@ -58,18 +70,13 @@ class MerklePatriciaTrie( private val nodeFactory: DefaultNodeFactory = DefaultNodeFactory(valueSerializer) private var root: Node = NullNode.instance() - /** - * Creates an empty trie. - * - * @param valueSerializer A function for serializing values to bytes. - */ - constructor(valueSerializer: Function) : this(valueSerializer::apply) - override suspend fun get(key: Bytes): V? = root.accept(getVisitor, bytesToPath(key)).value() // This implementation does not suspend, so we can use the unconfined context @UseExperimental(ExperimentalCoroutinesApi::class) - override fun getAsync(key: Bytes): AsyncResult = getAsync(Dispatchers.Unconfined, key) + override fun getAsync(key: Bytes): AsyncResult = runBlocking(Dispatchers.Unconfined) { + AsyncResult.completed(get(key)) + } override suspend fun put(key: Bytes, value: V?) { if (value == null) { @@ -80,7 +87,10 @@ class MerklePatriciaTrie( // This implementation does not suspend, so we can use the unconfined context @UseExperimental(ExperimentalCoroutinesApi::class) - override fun putAsync(key: Bytes, value: V?): AsyncCompletion = putAsync(Dispatchers.Unconfined, key, value) + override fun putAsync(key: Bytes, value: V?): AsyncCompletion = runBlocking(Dispatchers.Unconfined) { + put(key, value) + AsyncCompletion.completed() + } override suspend fun remove(key: Bytes) { this.root = root.accept(removeVisitor, bytesToPath(key)) @@ -88,14 +98,15 @@ class MerklePatriciaTrie( // This implementation does not suspend, so we can use the unconfined context @UseExperimental(ExperimentalCoroutinesApi::class) - override fun removeAsync(key: Bytes): AsyncCompletion = removeAsync(Dispatchers.Unconfined, key) + override fun removeAsync(key: Bytes): AsyncCompletion = runBlocking(Dispatchers.Unconfined) { + remove(key) + AsyncCompletion.completed() + } override fun rootHash(): Bytes32 = root.hash() /** * @return A string representation of the object. */ - override fun toString(): String { - return javaClass.simpleName + "[" + rootHash() + "]" - } + override fun toString(): String = javaClass.simpleName + "[" + rootHash() + "]" } diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorage.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorage.kt index 3155a9ab..bcea6941 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorage.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorage.kt @@ -27,9 +27,9 @@ interface MerkleStorage { * Get the stored content under the given hash. * * @param hash The hash for the content. - * @return An [AsyncResult] that will complete with the stored content or {@code null} if not found. + * @return The stored content, or {@code null} if not found. */ - fun getAsync(hash: Bytes32): AsyncResult + suspend fun get(hash: Bytes32): Bytes? /** * Store content with a given hash. @@ -39,16 +39,35 @@ interface MerkleStorage { * * @param hash The hash for the content. * @param content The content to store. - * @return An [AsyncCompletion] that will complete when the content is stored. */ - fun putAsync(hash: Bytes32, content: Bytes): AsyncCompletion + suspend fun put(hash: Bytes32, content: Bytes) } -internal class CoroutineMerkleStorageAdapter( - private val storage: MerkleStorage -) : net.consensys.cava.trie.experimental.MerkleStorage { +/** + * A [MerkleStorage] implementation using [AsyncResult]'s. + */ +abstract class AsyncMerkleStorage : MerkleStorage { + override suspend fun get(hash: Bytes32): Bytes? = getAsync(hash).await() + + /** + * Get the stored content under the given hash. + * + * @param hash The hash for the content. + * @return An [AsyncResult] that will complete with the stored content or {@code null} if not found. + */ + abstract fun getAsync(hash: Bytes32): AsyncResult - override suspend fun get(hash: Bytes32): Bytes? = storage.getAsync(hash).await() + override suspend fun put(hash: Bytes32, content: Bytes) = putAsync(hash, content).await() - override suspend fun put(hash: Bytes32, content: Bytes) = storage.putAsync(hash, content).await() + /** + * Store content with a given hash. + * + * Note: if the storage implementation already contains content for the given hash, it does not need to replace the + * existing content. + * + * @param hash The hash for the content. + * @param content The content to store. + * @return An [AsyncCompletion] that will complete when the content is stored. + */ + abstract fun putAsync(hash: Bytes32, content: Bytes): AsyncCompletion } diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorageException.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorageException.kt similarity index 96% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorageException.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorageException.kt index fd8183cd..97bf1506 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorageException.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorageException.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie /** * This exception is thrown when there is an issue retrieving or decoding values from [MerkleStorage]. diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleTrie.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleTrie.kt index 49b15c0e..4ffaabfc 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleTrie.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleTrie.kt @@ -12,10 +12,15 @@ */ package net.consensys.cava.trie +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.concurrent.AsyncCompletion import net.consensys.cava.concurrent.AsyncResult +import net.consensys.cava.concurrent.coroutines.asyncCompletion +import net.consensys.cava.concurrent.coroutines.asyncResult import net.consensys.cava.crypto.Hash import net.consensys.cava.rlp.RLP @@ -34,10 +39,50 @@ interface MerkleTrie { * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. * * @param key The key of the value to be returned. - * @return An Optional containing the value that corresponds to the specified key, or an empty Optional if no such - * value exists. + * @return The value that corresponds to the specified key, or {@code null} if no such value exists. + * @throws MerkleStorageException If there is an error while accessing or decoding data from storage. */ - fun getAsync(key: K): AsyncResult + suspend fun get(key: K): V? + + /** + * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. + * + * @param key The key of the value to be returned. + * @return A value that corresponds to the specified key, or {@code null} if no such value exists. + */ + fun getAsync(key: K): AsyncResult = getAsync(Dispatchers.Default, key) + + /** + * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. + * + * @param key The key of the value to be returned. + * @param dispatcher The co-routine dispatcher for asynchronous tasks. + * @return A value that corresponds to the specified key, or {@code null} if no such value exists. + */ + fun getAsync(dispatcher: CoroutineDispatcher, key: K): AsyncResult = + GlobalScope.asyncResult(dispatcher) { get(key) } + + /** + * Updates the value that corresponds to the specified key, creating the value if one does not already exist. + * + * If the value is null, deletes the value that corresponds to the specified key, if such a value exists. + * + * @param key The key that corresponds to the value to be updated. + * @param value The value to associate the key with. + * @throws MerkleStorageException If there is an error while writing to storage. + */ + suspend fun put(key: K, value: V?) + + /** + * Updates the value that corresponds to the specified key, creating the value if one does not already exist. + * + * If the value is null, deletes the value that corresponds to the specified key, if such a value exists. + * + * @param key The key that corresponds to the value to be updated. + * @param value The value to associate the key with. + * @return A completion that will complete when the value has been put into the trie. + */ + fun putAsync(key: K, value: V?): AsyncCompletion = putAsync(Dispatchers.Default, key, value) /** * Updates the value that corresponds to the specified key, creating the value if one does not already exist. @@ -46,17 +91,37 @@ interface MerkleTrie { * * @param key The key that corresponds to the value to be updated. * @param value The value to associate the key with. + * @param dispatcher The co-routine dispatcher for asynchronous tasks. * @return A completion that will complete when the value has been put into the trie. */ - fun putAsync(key: K, value: V?): AsyncCompletion + fun putAsync(dispatcher: CoroutineDispatcher, key: K, value: V?): AsyncCompletion = + GlobalScope.asyncCompletion(dispatcher) { put(key, value) } + + /** + * Deletes the value that corresponds to the specified key, if such a value exists. + * + * @param key The key of the value to be deleted. + * @throws MerkleStorageException If there is an error while writing to storage. + */ + suspend fun remove(key: K) + + /** + * Deletes the value that corresponds to the specified key, if such a value exists. + * + * @param key The key of the value to be deleted. + * @return A completion that will complete when the value has been removed. + */ + fun removeAsync(key: K): AsyncCompletion = removeAsync(Dispatchers.Default, key) /** * Deletes the value that corresponds to the specified key, if such a value exists. * * @param key The key of the value to be deleted. + * @param dispatcher The co-routine dispatcher for asynchronous tasks. * @return A completion that will complete when the value has been removed. */ - fun removeAsync(key: K): AsyncCompletion + fun removeAsync(dispatcher: CoroutineDispatcher, key: K): AsyncCompletion = + GlobalScope.asyncCompletion(dispatcher) { remove(key) } /** * Returns the KECCAK256 hash of the root node of the trie. diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/Node.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/Node.kt similarity index 95% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/Node.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/Node.kt index f97cddfe..d3e18cea 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/Node.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/Node.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeFactory.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeFactory.kt similarity index 95% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeFactory.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeFactory.kt index 2384c355..24100912 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeFactory.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeFactory.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeVisitor.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeVisitor.kt similarity index 95% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeVisitor.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeVisitor.kt index 186f30e4..3f627aba 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NodeVisitor.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeVisitor.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NullNode.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NullNode.kt similarity index 97% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NullNode.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/NullNode.kt index 6bccc94f..ddf0467e 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/NullNode.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/NullNode.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/PutVisitor.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/PutVisitor.kt similarity index 97% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/PutVisitor.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/PutVisitor.kt index d48627bb..9bdb650a 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/PutVisitor.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/PutVisitor.kt @@ -10,10 +10,9 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes -import net.consensys.cava.trie.CompactEncoding internal class PutVisitor( private val nodeFactory: NodeFactory, diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/RemoveVisitor.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/RemoveVisitor.kt similarity index 94% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/RemoveVisitor.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/RemoveVisitor.kt index f4af4929..205ffec5 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/RemoveVisitor.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/RemoveVisitor.kt @@ -10,10 +10,9 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes -import net.consensys.cava.trie.CompactEncoding internal class RemoveVisitor : NodeVisitor { @@ -54,5 +53,6 @@ internal class RemoveVisitor : NodeVisitor { return leafNode } - override suspend fun visit(nullNode: NullNode, path: Bytes): Node = NullNode.instance() + override suspend fun visit(nullNode: NullNode, path: Bytes): Node = + NullNode.instance() } diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrie.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrie.kt similarity index 77% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrie.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrie.kt index 97a2b49f..7eb92091 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrie.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrie.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 @@ -23,7 +23,7 @@ import java.util.function.Function * * @param The type of values stored by this trie. */ -class StoredMerklePatriciaTrie : MerkleTrie, net.consensys.cava.trie.StoredMerklePatriciaTrie { +class StoredMerklePatriciaTrie : MerkleTrie { companion object { /** @@ -67,6 +67,44 @@ class StoredMerklePatriciaTrie : MerkleTrie, net.consensys.cava.tri @JvmStatic fun storingStrings(storage: MerkleStorage, rootHash: Bytes32): StoredMerklePatriciaTrie = StoredMerklePatriciaTrie(storage, rootHash, ::stringSerializer, ::stringDeserializer) + + /** + * Create a trie. + * + * @param storage The storage to use for persistence. + * @param valueSerializer A function for serializing values to bytes. + * @param valueDeserializer A function for deserializing values from bytes. + * @param The serialized type. + * @return A new merkle trie. + */ + @JvmStatic + fun create( + storage: MerkleStorage, + valueSerializer: Function, + valueDeserializer: Function + ): StoredMerklePatriciaTrie { + return StoredMerklePatriciaTrie(storage, valueSerializer::apply, valueDeserializer::apply) + } + + /** + * Create a trie. + * + * @param storage The storage to use for persistence. + * @param rootHash The initial root has for the trie, which should be already present in `storage`. + * @param valueSerializer A function for serializing values to bytes. + * @param valueDeserializer A function for deserializing values from bytes. + * @param The serialized type. + * @return A new merkle trie. + */ + @JvmStatic + fun create( + storage: MerkleStorage, + rootHash: Bytes32, + valueSerializer: Function, + valueDeserializer: Function + ): StoredMerklePatriciaTrie { + return StoredMerklePatriciaTrie(storage, rootHash, valueSerializer::apply, valueDeserializer::apply) + } } private val getVisitor = GetVisitor() @@ -75,19 +113,6 @@ class StoredMerklePatriciaTrie : MerkleTrie, net.consensys.cava.tri private val nodeFactory: StoredNodeFactory private var root: Node - /** - * Create a trie. - * - * @param storage The storage to use for persistence. - * @param valueSerializer A function for serializing values to bytes. - * @param valueDeserializer A function for deserializing values from bytes. - */ - constructor( - storage: MerkleStorage, - valueSerializer: Function, - valueDeserializer: Function - ) : this(storage, EMPTY_TRIE_ROOT_HASH, valueSerializer::apply, valueDeserializer::apply) - /** * Create a trie. * @@ -101,21 +126,6 @@ class StoredMerklePatriciaTrie : MerkleTrie, net.consensys.cava.tri valueDeserializer: (Bytes) -> V ) : this(storage, EMPTY_TRIE_ROOT_HASH, valueSerializer, valueDeserializer) - /** - * Create a trie. - * - * @param storage The storage to use for persistence. - * @param rootHash The initial root has for the trie, which should be already present in `storage`. - * @param valueSerializer A function for serializing values to bytes. - * @param valueDeserializer A function for deserializing values from bytes. - */ - constructor( - storage: MerkleStorage, - rootHash: Bytes32, - valueSerializer: Function, - valueDeserializer: Function - ) : this(storage, rootHash, valueSerializer::apply, valueDeserializer::apply) - /** * Create a trie. * @@ -159,7 +169,7 @@ class StoredMerklePatriciaTrie : MerkleTrie, net.consensys.cava.tri * Note: nodes are already stored using [java.lang.ref.SoftReference]'s, so they will be released automatically * based on memory demands. */ - override fun clearCache() { + fun clearCache() { val currentRoot = root if (currentRoot is StoredNode<*>) { currentRoot.unload() diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNode.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNode.kt similarity index 98% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNode.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNode.kt index 0388d761..a07b87e6 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNode.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNode.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Deferred diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNodeFactory.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNodeFactory.kt similarity index 98% rename from merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNodeFactory.kt rename to merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNodeFactory.kt index 93e32a64..3a268a2f 100644 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/StoredNodeFactory.kt +++ b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/StoredNodeFactory.kt @@ -10,14 +10,13 @@ * 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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import net.consensys.cava.bytes.Bytes import net.consensys.cava.bytes.Bytes32 import net.consensys.cava.rlp.RLP import net.consensys.cava.rlp.RLPException import net.consensys.cava.rlp.RLPReader -import net.consensys.cava.trie.CompactEncoding import java.util.Collections internal class StoredNodeFactory( diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorage.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorage.kt deleted file mode 100644 index fba94778..00000000 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleStorage.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 net.consensys.cava.trie.experimental - -import net.consensys.cava.bytes.Bytes -import net.consensys.cava.bytes.Bytes32 - -/** - * Storage for use in a [StoredMerklePatriciaTrie]. - */ -interface MerkleStorage { - - /** - * Get the stored content under the given hash. - * - * @param hash The hash for the content. - * @return The stored content, or {@code null} if not found. - */ - suspend fun get(hash: Bytes32): Bytes? - - /** - * Store content with a given hash. - * - * Note: if the storage implementation already contains content for the given hash, it does not need to replace the - * existing content. - * - * @param hash The hash for the content. - * @param content The content to store. - */ - suspend fun put(hash: Bytes32, content: Bytes) -} diff --git a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleTrie.kt b/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleTrie.kt deleted file mode 100644 index a6db7b15..00000000 --- a/merkle-trie/src/main/kotlin/net/consensys/cava/trie/experimental/MerkleTrie.kt +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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 net.consensys.cava.trie.experimental - -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import net.consensys.cava.concurrent.AsyncCompletion -import net.consensys.cava.concurrent.AsyncResult -import net.consensys.cava.concurrent.coroutines.asyncCompletion -import net.consensys.cava.concurrent.coroutines.asyncResult - -/** - * A Merkle Trie. - */ -interface MerkleTrie : net.consensys.cava.trie.MerkleTrie { - - /** - * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. - * - * @param key The key of the value to be returned. - * @return The value that corresponds to the specified key, or null if no such value exists. - * @throws MerkleStorageException If there is an error while accessing or decoding data from storage. - */ - suspend fun get(key: K): V? - - /** - * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. - * - * @param key The key of the value to be returned. - * @return An Optional containing the value that corresponds to the specified key, or an empty Optional if no such - * value exists. - */ - override fun getAsync(key: K): AsyncResult = getAsync(Dispatchers.Default, key) - - /** - * Returns the value that corresponds to the specified key, or an empty byte array if no such value exists. - * - * @param key The key of the value to be returned. - * @param dispatcher The co-routine dispatcher for asynchronous tasks. - * @return An Optional containing the value that corresponds to the specified key, or an empty Optional if no such - * value exists. - */ - fun getAsync(dispatcher: CoroutineDispatcher, key: K): AsyncResult = - GlobalScope.asyncResult(dispatcher) { get(key) } - - /** - * Updates the value that corresponds to the specified key, creating the value if one does not already exist. - * - * If the value is null, deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key that corresponds to the value to be updated. - * @param value The value to associate the key with. - * @throws MerkleStorageException If there is an error while writing to storage. - */ - suspend fun put(key: K, value: V?) - - /** - * Updates the value that corresponds to the specified key, creating the value if one does not already exist. - * - * If the value is null, deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key that corresponds to the value to be updated. - * @param value The value to associate the key with. - * @return A completion that will complete when the value has been put into the trie. - */ - override fun putAsync(key: K, value: V?): AsyncCompletion = putAsync(Dispatchers.Default, key, value) - - /** - * Updates the value that corresponds to the specified key, creating the value if one does not already exist. - * - * If the value is null, deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key that corresponds to the value to be updated. - * @param value The value to associate the key with. - * @param dispatcher The co-routine dispatcher for asynchronous tasks. - * @return A completion that will complete when the value has been put into the trie. - */ - fun putAsync(dispatcher: CoroutineDispatcher, key: K, value: V?): AsyncCompletion = - GlobalScope.asyncCompletion(dispatcher) { put(key, value) } - - /** - * Deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key of the value to be deleted. - * @throws MerkleStorageException If there is an error while writing to storage. - */ - suspend fun remove(key: K) - - /** - * Deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key of the value to be deleted. - * @return A completion that will complete when the value has been removed. - */ - override fun removeAsync(key: K): AsyncCompletion = removeAsync(Dispatchers.Default, key) - - /** - * Deletes the value that corresponds to the specified key, if such a value exists. - * - * @param key The key of the value to be deleted. - * @param dispatcher The co-routine dispatcher for asynchronous tasks. - * @return A completion that will complete when the value has been removed. - */ - fun removeAsync(dispatcher: CoroutineDispatcher, key: K): AsyncCompletion = - GlobalScope.asyncCompletion(dispatcher) { remove(key) } -} diff --git a/merkle-trie/src/test/java/net/consensys/cava/trie/StoredMerklePatriciaTrieJavaTest.java b/merkle-trie/src/test/java/net/consensys/cava/trie/StoredMerklePatriciaTrieJavaTest.java index fffb2b60..dcdc1af8 100644 --- a/merkle-trie/src/test/java/net/consensys/cava/trie/StoredMerklePatriciaTrieJavaTest.java +++ b/merkle-trie/src/test/java/net/consensys/cava/trie/StoredMerklePatriciaTrieJavaTest.java @@ -37,7 +37,7 @@ class StoredMerklePatriciaTrieJavaTest { @BeforeEach void setup() { Map storage = new HashMap<>(); - merkleStorage = new MerkleStorage() { + merkleStorage = new AsyncMerkleStorage() { @Override public @NotNull AsyncResult getAsync(@NotNull Bytes32 hash) { return AsyncResult.completed(storage.get(hash)); diff --git a/merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrieKotlinTest.kt b/merkle-trie/src/test/kotlin/net/consensys/cava/trie/MerklePatriciaTrieKotlinTest.kt similarity index 99% rename from merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrieKotlinTest.kt rename to merkle-trie/src/test/kotlin/net/consensys/cava/trie/MerklePatriciaTrieKotlinTest.kt index dfed1d37..95263e3c 100644 --- a/merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/MerklePatriciaTrieKotlinTest.kt +++ b/merkle-trie/src/test/kotlin/net/consensys/cava/trie/MerklePatriciaTrieKotlinTest.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import kotlinx.coroutines.runBlocking import net.consensys.cava.bytes.Bytes diff --git a/merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrieKotlinTest.kt b/merkle-trie/src/test/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrieKotlinTest.kt similarity index 99% rename from merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrieKotlinTest.kt rename to merkle-trie/src/test/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrieKotlinTest.kt index 4bb4affc..ee080441 100644 --- a/merkle-trie/src/test/kotlin/net/consensys/cava/trie/experimental/StoredMerklePatriciaTrieKotlinTest.kt +++ b/merkle-trie/src/test/kotlin/net/consensys/cava/trie/StoredMerklePatriciaTrieKotlinTest.kt @@ -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 net.consensys.cava.trie.experimental +package net.consensys.cava.trie import kotlinx.coroutines.runBlocking import net.consensys.cava.bytes.Bytes @@ -29,6 +29,7 @@ internal class StoredMerklePatriciaTrieKotlinTest { private lateinit var storage: MutableMap private val merkleStorage = object : MerkleStorage { override suspend fun get(hash: Bytes32): Bytes? = storage[hash] + override suspend fun put(hash: Bytes32, content: Bytes) { storage[hash] = content } diff --git a/net-coroutines/src/test/java/net/consensys/cava/net/coroutines/experimental/SelectorTest.java b/net-coroutines/src/test/java/net/consensys/cava/net/coroutines/SelectorTest.java similarity index 98% rename from net-coroutines/src/test/java/net/consensys/cava/net/coroutines/experimental/SelectorTest.java rename to net-coroutines/src/test/java/net/consensys/cava/net/coroutines/SelectorTest.java index 2d23cf47..6e5e30f2 100644 --- a/net-coroutines/src/test/java/net/consensys/cava/net/coroutines/experimental/SelectorTest.java +++ b/net-coroutines/src/test/java/net/consensys/cava/net/coroutines/SelectorTest.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 net.consensys.cava.net.coroutines.experimental; +package net.consensys.cava.net.coroutines; import static java.nio.channels.SelectionKey.OP_READ; import static org.junit.jupiter.api.Assertions.assertFalse; diff --git a/units/src/test/java/net/consensys/cava/units/bigints/test/BaseUInt256ValueTest.java b/units/src/test/java/net/consensys/cava/units/bigints/BaseUInt256ValueTest.java similarity index 99% rename from units/src/test/java/net/consensys/cava/units/bigints/test/BaseUInt256ValueTest.java rename to units/src/test/java/net/consensys/cava/units/bigints/BaseUInt256ValueTest.java index 394a20b1..7b545039 100644 --- a/units/src/test/java/net/consensys/cava/units/bigints/test/BaseUInt256ValueTest.java +++ b/units/src/test/java/net/consensys/cava/units/bigints/BaseUInt256ValueTest.java @@ -10,14 +10,12 @@ * 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 net.consensys.cava.units.bigints.test; +package net.consensys.cava.units.bigints; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import net.consensys.cava.bytes.Bytes; -import net.consensys.cava.units.bigints.BaseUInt256Value; -import net.consensys.cava.units.bigints.UInt256; import java.math.BigInteger; import java.util.stream.Stream; diff --git a/units/src/test/java/net/consensys/cava/units/bigints/Uint256Test.java b/units/src/test/java/net/consensys/cava/units/bigints/UInt256Test.java similarity index 100% rename from units/src/test/java/net/consensys/cava/units/bigints/Uint256Test.java rename to units/src/test/java/net/consensys/cava/units/bigints/UInt256Test.java diff --git a/units/src/test/java/net/consensys/cava/units/bigints/test/GasTest.java b/units/src/test/java/net/consensys/cava/units/ethereum/GasTest.java similarity index 94% rename from units/src/test/java/net/consensys/cava/units/bigints/test/GasTest.java rename to units/src/test/java/net/consensys/cava/units/ethereum/GasTest.java index 6aee8545..efd39c42 100644 --- a/units/src/test/java/net/consensys/cava/units/bigints/test/GasTest.java +++ b/units/src/test/java/net/consensys/cava/units/ethereum/GasTest.java @@ -10,16 +10,13 @@ * 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 net.consensys.cava.units.bigints.test; +package net.consensys.cava.units.ethereum; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import net.consensys.cava.units.ethereum.Gas; -import net.consensys.cava.units.ethereum.Wei; - import java.math.BigInteger; import java.util.ArrayList; import java.util.List; diff --git a/units/src/test/java/net/consensys/cava/units/bigints/test/WeiTest.java b/units/src/test/java/net/consensys/cava/units/ethereum/WeiTest.java similarity index 95% rename from units/src/test/java/net/consensys/cava/units/bigints/test/WeiTest.java rename to units/src/test/java/net/consensys/cava/units/ethereum/WeiTest.java index aacb8464..65250d24 100644 --- a/units/src/test/java/net/consensys/cava/units/bigints/test/WeiTest.java +++ b/units/src/test/java/net/consensys/cava/units/ethereum/WeiTest.java @@ -10,15 +10,13 @@ * 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 net.consensys.cava.units.bigints.test; +package net.consensys.cava.units.ethereum; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import net.consensys.cava.units.ethereum.Wei; - import java.math.BigInteger; import java.util.ArrayList; import java.util.List;