You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuweni.apache.org by GitBox <gi...@apache.org> on 2021/04/21 20:15:33 UTC

[GitHub] [incubator-tuweni] nfmelendez commented on a change in pull request #209: add crawler

nfmelendez commented on a change in pull request #209:
URL: https://github.com/apache/incubator-tuweni/pull/209#discussion_r617846496



##########
File path: eth-crawler/src/main/kotlin/org/apache/tuweni/eth/crawler/RelationalPeerRepository.kt
##########
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.tuweni.eth.crawler
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import org.apache.tuweni.bytes.Bytes
+import org.apache.tuweni.concurrent.AsyncResult
+import org.apache.tuweni.concurrent.coroutines.asyncResult
+import org.apache.tuweni.crypto.SECP256K1
+import org.apache.tuweni.devp2p.Endpoint
+import org.apache.tuweni.devp2p.EthereumNodeRecord
+import org.apache.tuweni.devp2p.Peer
+import org.apache.tuweni.devp2p.PeerRepository
+import org.apache.tuweni.devp2p.parseEnodeUri
+import java.net.URI
+import java.sql.Timestamp
+import java.util.UUID
+import javax.sql.DataSource
+import kotlin.coroutines.CoroutineContext
+
+class RelationalPeerRepository(
+  private val dataSource: DataSource,
+  override val coroutineContext: CoroutineContext = Dispatchers.Default,
+) : CoroutineScope, PeerRepository {
+
+  private val listeners = mutableListOf<(Peer) -> Unit>()
+
+  override fun addListener(listener: (Peer) -> Unit) {
+    listeners.add(listener)
+  }
+
+  override suspend fun get(host: String, port: Int, nodeId: SECP256K1.PublicKey): Peer {
+    return get(nodeId, Endpoint(host, port))
+  }
+
+  fun get(nodeId: SECP256K1.PublicKey, endpoint: Endpoint): Peer {
+    dataSource.connection.use { conn ->
+      val stmt = conn.prepareStatement("select id,publickey from identity where publickey=?")
+      stmt.setString(1, nodeId.bytes().toUnprefixedHexString())
+      val rs = stmt.executeQuery()
+      rs.use {
+        if (!rs.next()) {
+          val id = UUID.randomUUID().toString()
+          val insert = conn.prepareStatement("insert into identity(id, publickey) values(?, ?)")

Review comment:
       does #56  rs.use  close the prepareStatement  insert  in line #59 ?  or maybe  it needs another close or a use




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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