You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/10/19 02:50:52 UTC

[incubator-tuweni] branch master updated: Add in memory helper for BlockchainRepository

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 0883010  Add in memory helper for BlockchainRepository
     new 43e6e10  Merge pull request #155 from atoulme/add_in_memory
0883010 is described below

commit 088301027eec9018cd866e1c660a37b6233b7611
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Mon Oct 12 22:40:55 2020 -0700

    Add in memory helper for BlockchainRepository
---
 .../apache/tuweni/devp2p/eth/EthSubprotocolTest.kt | 43 ++++------------------
 .../tuweni/eth/repository/BlockchainRepository.kt  | 35 ++++++++++++++++--
 .../apache/tuweni/les/LESSubProtocolHandlerTest.kt | 16 ++------
 .../org/apache/tuweni/les/LESSubprotocolTest.kt    | 43 ++++------------------
 4 files changed, 48 insertions(+), 89 deletions(-)

diff --git a/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthSubprotocolTest.kt b/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthSubprotocolTest.kt
index 37c3082..e407c38 100644
--- a/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthSubprotocolTest.kt
+++ b/devp2p-eth/src/test/kotlin/org/apache/tuweni/devp2p/eth/EthSubprotocolTest.kt
@@ -16,17 +16,12 @@
  */
 package org.apache.tuweni.devp2p.eth
 
-import org.apache.lucene.index.IndexWriter
 import org.apache.tuweni.bytes.Bytes32
 import org.apache.tuweni.eth.Hash
-import org.apache.tuweni.eth.repository.BlockchainIndex
 import org.apache.tuweni.eth.repository.BlockchainRepository
 import org.apache.tuweni.eth.repository.MemoryTransactionPool
 import org.apache.tuweni.junit.BouncyCastleExtension
-import org.apache.tuweni.junit.LuceneIndexWriter
-import org.apache.tuweni.junit.LuceneIndexWriterExtension
 import org.apache.tuweni.junit.VertxExtension
-import org.apache.tuweni.kv.MapKeyValueStore
 import org.apache.tuweni.rlpx.wire.SubProtocolIdentifier
 import org.apache.tuweni.units.bigints.UInt256
 import org.junit.jupiter.api.Assertions.assertEquals
@@ -35,7 +30,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.extension.ExtendWith
 
-@ExtendWith(BouncyCastleExtension::class, VertxExtension::class, LuceneIndexWriterExtension::class)
+@ExtendWith(BouncyCastleExtension::class, VertxExtension::class)
 class EthSubprotocolTest {
 
   val blockchainInfo = SimpleBlockchainInformation(
@@ -47,16 +42,8 @@ class EthSubprotocolTest {
   )
 
   @Test
-  fun testVersion(@LuceneIndexWriter writer: IndexWriter) {
-    val repository = BlockchainRepository(
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      BlockchainIndex(writer)
-    )
+  fun testVersion() {
+    val repository = BlockchainRepository.inMemory()
     val eth = EthSubprotocol(
       blockchainInfo = blockchainInfo,
       repository = repository,
@@ -66,16 +53,8 @@ class EthSubprotocolTest {
   }
 
   @Test
-  fun testSupports(@LuceneIndexWriter writer: IndexWriter) {
-    val repository = BlockchainRepository(
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      BlockchainIndex(writer)
-    )
+  fun testSupports() {
+    val repository = BlockchainRepository.inMemory()
     val eth = EthSubprotocol(
       blockchainInfo = blockchainInfo,
       repository = repository,
@@ -90,16 +69,8 @@ class EthSubprotocolTest {
   }
 
   @Test
-  fun rangeCheck(@LuceneIndexWriter writer: IndexWriter) {
-    val repository = BlockchainRepository(
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      BlockchainIndex(writer)
-    )
+  fun rangeCheck() {
+    val repository = BlockchainRepository.inMemory()
     val eth = EthSubprotocol(
       blockchainInfo = blockchainInfo,
       repository = repository,
diff --git a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
index 9a40ffb..a4da1a9 100644
--- a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
+++ b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
@@ -16,6 +16,10 @@
  */
 package org.apache.tuweni.eth.repository
 
+import org.apache.lucene.analysis.standard.StandardAnalyzer
+import org.apache.lucene.index.IndexWriter
+import org.apache.lucene.index.IndexWriterConfig
+import org.apache.lucene.store.ByteBuffersDirectory
 import org.apache.tuweni.bytes.Bytes
 import org.apache.tuweni.bytes.Bytes32
 import org.apache.tuweni.eth.AccountState
@@ -27,6 +31,7 @@ import org.apache.tuweni.eth.Hash
 import org.apache.tuweni.eth.Transaction
 import org.apache.tuweni.eth.TransactionReceipt
 import org.apache.tuweni.kv.KeyValueStore
+import org.apache.tuweni.kv.MapKeyValueStore
 import org.apache.tuweni.trie.MerkleStorage
 import org.apache.tuweni.trie.StoredMerklePatriciaTrie
 import org.slf4j.LoggerFactory
@@ -64,6 +69,26 @@ class BlockchainRepository
     internal val GENESIS_BLOCK = Bytes.wrap("genesisBlock".toByteArray())
 
     /**
+     * Constructs a blockchain repository that resides entirely in heap.
+     *
+     * @return an in-memory repository
+     */
+    fun inMemory(): BlockchainRepository {
+      val analyzer = StandardAnalyzer()
+      val config = IndexWriterConfig(analyzer)
+      val writer = IndexWriter(ByteBuffersDirectory(), config)
+      return BlockchainRepository(
+        MapKeyValueStore(),
+        MapKeyValueStore(),
+        MapKeyValueStore(),
+        MapKeyValueStore(),
+        MapKeyValueStore(),
+        MapKeyValueStore(),
+        BlockchainIndex(writer)
+      )
+    }
+
+    /**
      * Initializes a blockchain repository with metadata, placing it in key-value stores.
      *
      * @return a new blockchain repository made from the metadata passed in parameter.
@@ -78,13 +103,15 @@ class BlockchainRepository
       blockchainIndex: BlockchainIndex,
       genesisBlock: Block
     ): BlockchainRepository {
-      val repo = BlockchainRepository(chainMetadata,
+      val repo = BlockchainRepository(
+        chainMetadata,
         blockBodyStore,
         blockHeaderStore,
         transactionReceiptsStore,
         transactionStore,
         stateStore,
-        blockchainIndex)
+        blockchainIndex
+      )
       repo.setGenesisBlock(genesisBlock)
       repo.storeBlock(genesisBlock)
       return repo
@@ -209,8 +236,8 @@ class BlockchainRepository
    * @return a future with the block if found
    */
   suspend fun retrieveBlock(blockHash: Bytes): Block? {
-    return retrieveBlockBody(blockHash)?.let {
-        body -> this.retrieveBlockHeader(blockHash)?.let { Block(it, body) }
+    return retrieveBlockBody(blockHash)?.let { body ->
+      this.retrieveBlockHeader(blockHash)?.let { Block(it, body) }
     } ?: return null
   }
 
diff --git a/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt b/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
index 124f9ea..bba84a8 100644
--- a/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
+++ b/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
@@ -61,15 +61,13 @@ import org.junit.jupiter.api.Assertions.assertThrows
 import org.junit.jupiter.api.Assertions.assertTrue
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.extension.ExtendWith
-import java.io.IOException
 import java.net.InetSocketAddress
 import java.time.Instant
 import java.time.temporal.ChronoUnit
 import java.util.UUID
 
 @ExtendWith(BouncyCastleExtension::class, VertxExtension::class, LuceneIndexWriterExtension::class)
-internal class LESSubProtocolHandlerTest @Throws(IOException::class)
-constructor() {
+internal class LESSubProtocolHandlerTest {
 
   private val header = BlockHeader(
     Hash.fromBytes(Bytes32.random()),
@@ -268,17 +266,9 @@ constructor() {
 
   @Test
   @Throws(Exception::class)
-  fun receiveOtherMessageBeforeStatus(@LuceneIndexWriter writer: IndexWriter) = runBlocking {
+  fun receiveOtherMessageBeforeStatus() = runBlocking {
     val service = MyRLPxService()
-    val repo = BlockchainRepository(
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      MapKeyValueStore(),
-      BlockchainIndex(writer)
-    )
+    val repo = BlockchainRepository.inMemory()
     val pool = MemoryTransactionPool()
     val controller = EthController(repo, pool, EthClient(service, pool))
 
diff --git a/les/src/test/kotlin/org/apache/tuweni/les/LESSubprotocolTest.kt b/les/src/test/kotlin/org/apache/tuweni/les/LESSubprotocolTest.kt
index cef9841..fdf6402 100644
--- a/les/src/test/kotlin/org/apache/tuweni/les/LESSubprotocolTest.kt
+++ b/les/src/test/kotlin/org/apache/tuweni/les/LESSubprotocolTest.kt
@@ -17,17 +17,12 @@
 package org.apache.tuweni.les
 
 import kotlinx.coroutines.Dispatchers
-import org.apache.lucene.index.IndexWriter
 import org.apache.tuweni.bytes.Bytes32
 import org.apache.tuweni.devp2p.eth.SimpleBlockchainInformation
 import org.apache.tuweni.eth.Hash
-import org.apache.tuweni.eth.repository.BlockchainIndex
 import org.apache.tuweni.eth.repository.BlockchainRepository
 import org.apache.tuweni.eth.repository.MemoryTransactionPool
-import org.apache.tuweni.junit.LuceneIndexWriter
-import org.apache.tuweni.junit.LuceneIndexWriterExtension
 import org.apache.tuweni.junit.TempDirectoryExtension
-import org.apache.tuweni.kv.MapKeyValueStore
 import org.apache.tuweni.rlpx.wire.SubProtocolIdentifier
 import org.apache.tuweni.units.bigints.UInt256
 import org.junit.jupiter.api.Assertions.assertFalse
@@ -36,7 +31,7 @@ import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.TestInstance
 import org.junit.jupiter.api.extension.ExtendWith
 
-@ExtendWith(TempDirectoryExtension::class, LuceneIndexWriterExtension::class)
+@ExtendWith(TempDirectoryExtension::class)
 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
 internal class LESSubprotocolTest {
 
@@ -50,7 +45,7 @@ internal class LESSubprotocolTest {
 
   @Test
   @Throws(Exception::class)
-  fun supportsLESv2(@LuceneIndexWriter writer: IndexWriter) {
+  fun supportsLESv2() {
 
     val sp = LESSubprotocol(
       Dispatchers.Default,
@@ -61,15 +56,7 @@ internal class LESSubprotocolTest {
       UInt256.ZERO,
       UInt256.ZERO,
       UInt256.ZERO,
-      BlockchainRepository(
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        BlockchainIndex(writer)
-      ),
+      BlockchainRepository.inMemory(),
       MemoryTransactionPool()
     )
     assertTrue(sp.supports(SubProtocolIdentifier.of("les", 2)))
@@ -77,7 +64,7 @@ internal class LESSubprotocolTest {
 
   @Test
   @Throws(Exception::class)
-  fun noSupportForv3(@LuceneIndexWriter writer: IndexWriter) {
+  fun noSupportForv3() {
 
     val sp = LESSubprotocol(
       Dispatchers.Default,
@@ -88,15 +75,7 @@ internal class LESSubprotocolTest {
       UInt256.ZERO,
       UInt256.ZERO,
       UInt256.ZERO,
-      BlockchainRepository(
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        BlockchainIndex(writer)
-      ),
+      BlockchainRepository.inMemory(),
       MemoryTransactionPool()
     )
     assertFalse(sp.supports(SubProtocolIdentifier.of("les", 3)))
@@ -104,7 +83,7 @@ internal class LESSubprotocolTest {
 
   @Test
   @Throws(Exception::class)
-  fun noSupportForETH(@LuceneIndexWriter writer: IndexWriter) {
+  fun noSupportForETH() {
     val sp = LESSubprotocol(
       Dispatchers.Default,
       blockchainInformation,
@@ -114,15 +93,7 @@ internal class LESSubprotocolTest {
       UInt256.ZERO,
       UInt256.ZERO,
       UInt256.ZERO,
-      BlockchainRepository(
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        MapKeyValueStore(),
-        BlockchainIndex(writer)
-      ),
+      BlockchainRepository.inMemory(),
       MemoryTransactionPool()
     )
     assertFalse(sp.supports(SubProtocolIdentifier.of("eth", 2)))


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org