Skip to content

Commit

Permalink
Use directly Blockchair API for transaction broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Apr 1, 2024
1 parent 84ee132 commit 2387c14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.horizontalsystems.bitcoincore.apisync.blockchair

import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonObject
import io.horizontalsystems.bitcoincore.apisync.model.AddressItem
import io.horizontalsystems.bitcoincore.apisync.model.BlockHeaderItem
import io.horizontalsystems.bitcoincore.apisync.model.TransactionItem
Expand Down Expand Up @@ -77,7 +79,14 @@ class BlockchairApi(
}

fun broadcastTransaction(rawTransactionHex: String) {
apiManager.post("$chainId/push/transaction", "{ \"data\" : \"$rawTransactionHex\"}")
val apiManager = ApiManager("https://api.blockchair.com")
val url = "$chainId/push/transaction"

val body = JsonObject().apply {
this["data"] = Json.value(rawTransactionHex)
}.toString()

apiManager.post(url, body)
}

private fun fetchTransactions(
Expand Down Expand Up @@ -129,7 +138,7 @@ class BlockchairApi(

private fun dateStringToTimestamp(date: String): Long? {
return try {
dateFormat.parse(date)?.time?.let { it / 1000 }
dateFormat.parse(date)?.time?.let { it / 1000 }
} catch (e: ParseException) {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.horizontalsystems.bitcoincore.managers

import android.util.Log
import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonValue
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.BufferedOutputStream
import java.io.BufferedWriter
import java.io.IOException
Expand All @@ -31,6 +32,7 @@ class ApiManager(private val host: String) {
connectTimeout = 5000
readTimeout = 60000
setRequestProperty("Accept", "application/json")
setRequestProperty("content-type", "application/json")
}.getInputStream()
} catch (exception: IOException) {
throw ApiManagerException.Other("${exception.javaClass.simpleName}: $host")
Expand All @@ -42,14 +44,12 @@ class ApiManager(private val host: String) {
try {
val path = "$host/$resource"

Log.e("e", "path: $path")
Log.e("e", "body: $data")

logger.info("Fetching $path")

val url = URL(path)
val urlConnection = url.openConnection() as HttpURLConnection
urlConnection.requestMethod = "POST"
urlConnection.setRequestProperty("Content-Type", "application/json")
val out = BufferedOutputStream(urlConnection.outputStream)
val writer = BufferedWriter(OutputStreamWriter(out, "UTF-8"))
writer.write(data)
Expand Down Expand Up @@ -99,6 +99,7 @@ class ApiManager(private val host: String) {
throw ApiManagerException.Other("${e.javaClass.simpleName}: $host, ${e.localizedMessage}")
}
}

}

sealed class ApiManagerException : Exception() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.horizontalsystems.bitcoincore.transactions

import android.util.Log
import io.horizontalsystems.bitcoincore.BitcoinCore
import io.horizontalsystems.bitcoincore.apisync.blockchair.BlockchairApi
import io.horizontalsystems.bitcoincore.core.IInitialDownload
Expand Down Expand Up @@ -102,16 +101,14 @@ class TransactionSender(
}

private fun sendViaAPI(transactions: List<FullTransaction>, blockchairApi: BlockchairApi) {
timer.startIfNotRunning()

transactions.forEach { transaction ->
try {
transactionSendStart(transaction)

val hex = transactionSerializer.serialize(transaction).toHexString()
blockchairApi.broadcastTransaction(hex)

transactionSyncer.handleRelayed(listOf(transaction))
} catch (error: Throwable) {
Log.e("e", "API send error", error)
transactionSyncer.handleInvalid(transaction)
}
}
}
Expand Down Expand Up @@ -172,6 +169,7 @@ class TransactionSender(
transactionSendSuccess(task.transaction)
true
}

else -> false
}
}
Expand Down

0 comments on commit 2387c14

Please sign in to comment.