Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ allprojects {
mavenLocal()
jcenter()
maven("https://jitpack.io")
mavenCentral()
}

tasks.withType<KotlinCompile> {
Expand Down
74 changes: 60 additions & 14 deletions nodecore-spv/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
// https://www.veriblock.org
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.adarshr.gradle.testlogger.TestLoggerExtension
import com.adarshr.gradle.testlogger.TestLoggerPlugin
import com.adarshr.gradle.testlogger.theme.ThemeType

plugins {
java
Expand All @@ -15,38 +18,35 @@ plugins {
`java-library`
`maven-publish`
id("com.jfrog.artifactory")
id("com.adarshr.test-logger") version "3.1.0"
}

configurations.all {
// check for updates every build for changing modules
resolutionStrategy.cacheChangingModulesFor(0, "seconds")
}

val log4jVersion = "2.16.0"

dependencies {
api(project(":veriblock-core"))
api(project(":veriblock-extensions"))
api(project(":nodecore-grpc"))
api(project(":nodecore-p2p"))

implementation("io.ktor:ktor-network-jvm:$ktorVersion")
implementation("io.ktor:ktor-network-jvm:1.6.4")

// Logging
implementation("io.github.microutils:kotlin-logging:1.6.26")
implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
implementation("org.slf4j:slf4j-api:$slf4jVersion")

implementation("org.freemarker:freemarker:2.3.14")
implementation("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")
implementation("io.github.microutils:kotlin-logging:2.1.16")

testImplementation("junit:junit:4.12")
testImplementation("io.kotest:kotest-assertions-core-jvm:4.3.0")
testImplementation("io.mockk:mockk:1.9.3")
}
implementation("org.freemarker:freemarker:2.3.31")

tasks.test {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
testImplementation("junit:junit:4.13.2")
testImplementation("io.kotest:kotest-assertions-core-jvm:4.6.3")
testImplementation("io.mockk:mockk:1.12.0")
}

setupJar("VeriBlock Lite Toolkit", "veriblock.lite")
Expand All @@ -59,3 +59,49 @@ publish(
)

setupJacoco()

tasks.test {
useJUnitPlatform()
maxParallelForks = 1
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<JavaCompile> {
targetCompatibility = "1.8"
sourceCompatibility = "1.8"
}

tasks.withType<Test> {
useJUnitPlatform()

reports {
html.required.set(true)
junitXml.required.set(true)
junitXml.apply {
isOutputPerTestCase = true // defaults to false
}
}
}

plugins.withType<TestLoggerPlugin> {
configure<TestLoggerExtension> {
theme = ThemeType.MOCHA
showExceptions = true
showStackTraces = true
showFullStackTraces = false
showCauses = true
slowThreshold = 20000
showSummary = true
showSimpleNames = false
showPassed = true
showSkipped = true
showFailed = true
showStandardStreams = true
showPassedStandardStreams = false
showSkippedStandardStreams = false
showFailedStandardStreams = true
logLevel = LogLevel.LIFECYCLE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ private fun run(): Int {
logger.info { "Type 'importwallet <sourceLocation>' to import an existing wallet" }
logger.info { "Type 'help' to display a list of available commands" }

if (!spvContext.addressManager.isLocked) {
if (!spvContext.wallet.isLocked) {
try {
spvContext.spvService.importWallet(spvContext.addressManager.walletPath())
logger.info { "Successfully imported the wallet file: ${spvContext.addressManager.walletPath()}" }
spvContext.spvService.importWallet(spvContext.wallet.walletPath())
logger.info { "Successfully imported the wallet file: ${spvContext.wallet.walletPath()}" }
logger.info { "Type 'getbalance' to see the balances of all of your addresses" }
} catch (exception: Exception) {
logger.info { "Failed to import the wallet file: ${exception.message}" }
Expand Down
44 changes: 18 additions & 26 deletions nodecore-spv/src/main/java/org/veriblock/spv/SpvContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,45 @@ private val logger = createLogger {}
* Initialize and hold beans/classes.
*/
class SpvContext(
config: SpvConfig
val config: SpvConfig
) {
val networkParameters: NetworkParameters = config.networkParameters

private val p2pConfiguration: P2pConfiguration
val directory: File
val filePrefix: String
val transactionPool: TransactionPool
val blockStore: BlockStore
val blockchain: Blockchain
val spvService: SpvService
val peerTable: PeerTable
val addressManager: AddressManager
val transactionService: TransactionService
val wallet: AddressManager
val blockchain: Blockchain
val transactionPool: TransactionPool
val pendingTransactionContainer: PendingTransactionContainer
val pendingTransactionDownloadedListener: PendingTransactionDownloadedListener
val transactionService: TransactionService
val spvService: SpvService

private val p2pConfiguration: P2pConfiguration
private val addressState: ConcurrentHashMap<Address, LedgerContext> = ConcurrentHashMap()

val trustPeerHashes = config.trustPeerHashes
val startTime: Instant = Instant.now()

init {
if (trustPeerHashes) {
if (config.trustPeerHashes) {
logger.info { "Fast sync mode is enabled." }
}

if (!Context.isCreated()) {
Context.create(networkParameters)
} else if (Context.get().networkParameters.name != networkParameters.name) {
throw IllegalStateException("Attempting to create $networkParameters SPV context while on ${Context.get().networkParameters}")
Context.create(config.networkParameters)
} else if (Context.get().networkParameters.name != config.networkParameters.name) {
throw IllegalStateException("Attempting to create $config.networkParameters SPV context while on ${Context.get().networkParameters}")
}

val baseDir = File(config.dataDir)
baseDir.mkdirs()

try {
directory = baseDir
filePrefix = networkParameters.name
blockStore = BlockStore(networkParameters, directory)
transactionPool = TransactionPool()
blockchain = Blockchain(blockStore)
blockchain = Blockchain(config.networkParameters, directory)
pendingTransactionContainer = PendingTransactionContainer(this)
addressManager = AddressManager()
val walletFile = File(directory, filePrefix + FILE_EXTENSION)
addressManager.load(walletFile)
wallet = AddressManager()
val walletFile = File(directory, config.networkParameters.name + FILE_EXTENSION)
wallet.load(walletFile)
pendingTransactionDownloadedListener = PendingTransactionDownloadedListener(this)

val externalPeerEndpoints = config.connectDirectlyTo.map {
Expand All @@ -105,7 +98,7 @@ class SpvContext(

val uri = URI.create(input)
// if port is not provided, use standard port from networkParameters
val port = if (uri.port == -1) networkParameters.p2pPort else uri.port
val port = if (uri.port == -1) config.networkParameters.p2pPort else uri.port
NetworkAddress(uri.host, port)
} catch (e: Exception) {
throw ConfigurationException("Wrong format for peer address ${it}, it should be host:port")
Expand Down Expand Up @@ -134,9 +127,9 @@ class SpvContext(
val bootstrapper = PeerTableBootstrapper(p2pConfiguration, DnsResolver())
peerTable = PeerTable(p2pConfiguration, warden, bootstrapper)

transactionService = TransactionService(addressManager, networkParameters)
transactionService = TransactionService(wallet, config.networkParameters)
spvService = SpvService(
this, peerTable, transactionService, addressManager,
this, peerTable, transactionService, wallet,
pendingTransactionContainer, blockchain
)

Expand All @@ -155,7 +148,6 @@ class SpvContext(
logger.info { "SPV Disconnected" }
}
)
startPendingTransactionsUpdateTask()
startAddressStateUpdateTask()

Threading.PEER_TABLE_SCOPE.launchWithFixedDelay(40_000, 120_000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ enum class DownloadStatus {

fun isDownloading(): Boolean =
DOWNLOADING == this

fun isReady(): Boolean =
READY == this
}
Loading