Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ jobs:
postgres:
image: postgres:latest
env:
POSTGRES_DB: kryptokrona_api_dev
POSTGRES_DB: kryptokrona_api_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: test1234
ports:
- 5432:5432
- 5433:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand All @@ -61,7 +61,7 @@ jobs:
cache: 'gradle'

- name: Run Liquibase Migrations
run: ./gradlew update -PdbEnv=dev
run: ./gradlew update -PdbEnv=test

- name: Run Unit Test
run: ./gradlew test
Expand Down
15 changes: 3 additions & 12 deletions .github/workflows/backend-pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,34 @@ on:
jobs:
ansible-syntax-check:
name: Ansible Syntax Check
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

# Install python3 pip and ansible latest
- name: Install python and ansible
run: sudo apt update && sudo apt install -y python3-pip && sudo pip3 install ansible

# If `roles` folder exists, create a playbook_repo.yml playbook to perform the syntax check
- name: Creating playbook_repo.yml from roles folder (if exists)
run: test -d "kryptokrona-api/ansible/roles" || exit 0 && echo $LINE1 > playbook_repo.yml && echo $LINE2 >> playbook_repo.yml && printf " roles:\n" >> playbook_repo.yml && for i in `ls kryptokrona-api/ansible/roles`; do echo " - $i" >> playbook_repo.yml; done
env:
LINE1: "---"
LINE2: "- hosts: localhost"

# Check ansible syntax if roles folder exists (repo with playbooks and roles)
- name: Ansible Syntax Check of all roles of the playbook
run: test -f "playbook_repo.yml" || exit 0 && ansible-playbook $GITHUB_WORKSPACE/playbook_repo.yml --syntax-check
env:
ANSIBLE_ACTION_WARNINGS: False

# If `roles` folder does not exist, assume we are in a repository containing just the role
- name: Creating role_repo.yml from roles folder (if exists)
run: test -d "tasks" || exit 0 && echo $LINE1 > /tmp/role_repo.yml && echo $LINE2 >> /tmp/role_repo.yml && printf " roles:\n" >> /tmp/role_repo.yml && printf " - `echo $GITHUB_REPOSITORY | tr "/" "\n" | tail -1`\n" >> /tmp/role_repo.yml
env:
LINE1: "---"
LINE2: "- hosts: localhost"

# Copy the repo content for syntax check
- name: Copy the repo content to /tmp/ for syntax check
run: test -f "/tmp/role_repo.yml" || exit 0 && mkdir -p /tmp/roles/ && cd .. && cp -a `echo $GITHUB_REPOSITORY | tr "/" "\n" | tail -1` /tmp/roles/

# Check ansible syntax if tasks folder exists (repo with just the role)
- name: Ansible Syntax Check of role
run: test -f "/tmp/role_repo.yml" || exit 0 && ANSIBLE_ROLES_PATH=/tmp/roles ansible-playbook /tmp/role_repo.yml --syntax-check
env:
Expand Down Expand Up @@ -81,11 +72,11 @@ jobs:
postgres:
image: postgres:latest
env:
POSTGRES_DB: kryptokrona_api_dev
POSTGRES_DB: kryptokrona_api_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: test1234
ports:
- 5432:5432
- 5433:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand All @@ -102,7 +93,7 @@ jobs:
cache: 'gradle'

- name: Run Liquibase Migrations
run: ./gradlew update -PdbEnv=dev
run: ./gradlew update -PdbEnv=test

- name: Run Unit Test
run: ./gradlew test
Expand Down
14 changes: 13 additions & 1 deletion .run/migrate.run.xml → .run/migrate-dev.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="migrate" type="GradleRunConfiguration" factoryName="Gradle">
<configuration default="false" name="migrate-dev" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
Expand All @@ -17,6 +17,18 @@
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<EXTENSION ID="com.intellij.execution.ExternalSystemRunConfigurationJavaExtension">
<extension name="net.ashald.envfile">
<option name="IS_ENABLED" value="false" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
</ENTRIES>
</extension>
</EXTENSION>
<DebugAllEnabled>false</DebugAllEnabled>
<method v="2" />
</configuration>
Expand Down
35 changes: 35 additions & 0 deletions .run/migrate-test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="migrate-test" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="-PdbEnv=test" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="update" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<EXTENSION ID="com.intellij.execution.ExternalSystemRunConfigurationJavaExtension">
<extension name="net.ashald.envfile">
<option name="IS_ENABLED" value="false" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
</ENTRIES>
</extension>
</EXTENSION>
<DebugAllEnabled>false</DebugAllEnabled>
<method v="2" />
</configuration>
</component>
14 changes: 13 additions & 1 deletion .run/tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@
<option value=":test" />
</list>
</option>
<option name="vmOptions" value="-DDB_URL=jdbc:postgresql://127.0.0.1:5432/kryptokrona_api_dev -DDB_USER=postgres -DDB_PASSWORD=test1234" />
<option name="vmOptions" value="-DDB_URL=jdbc:postgresql://127.0.0.1:5433/kryptokrona_api_test -DDB_USER=postgres -DDB_PASSWORD=test1234" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<EXTENSION ID="com.intellij.execution.ExternalSystemRunConfigurationJavaExtension">
<extension name="net.ashald.envfile">
<option name="IS_ENABLED" value="false" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
</ENTRIES>
</extension>
</EXTENSION>
<DebugAllEnabled>false</DebugAllEnabled>
<method v="2" />
</configuration>
Expand Down
15 changes: 15 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ liquibase {
)
}

activities.register("test") {
val url = properties.getProperty("liquibase.test.url") ?: System.getenv("LIQUIBASE_TEST_URL")
val user = properties.getProperty("liquibase.test.user") ?: System.getenv("LIQUIBASE_TEST_USER")
val password = properties.getProperty("liquibase.test.password") ?: System.getenv("LIQUIBASE_TEST_PASSWORD")

this.arguments = mapOf(
"logLevel" to "info",
"changeLogFile" to "src/main/resources/db/changelog/master.xml",
"url" to url,
"username" to user,
"password" to password,
"classpath" to "src/main/resources/"
)
}

activities.register("prod") {
val url = System.getenv("LIQUIBASE_URL")
val user = System.getenv("LIQUIBASE_USER")
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ services:
networks:
- kryptokrona_api_net

db-test:
image: postgres
container_name: kryptokrona_api_db_test
restart: always
environment:
POSTGRES_DB: kryptokrona_api_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: test1234
volumes:
- db:/data/postgres
ports:
- "5433:5432"
networks:
- kryptokrona_api_net

liquibase:
image: liquibase/liquibase
container_name: kryptokrona_api_liquibase
Expand Down
6 changes: 5 additions & 1 deletion local.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
liquibase.dev.url=jdbc:postgresql://localhost:5432/kryptokrona_api_dev
liquibase.dev.user=postgres
liquibase.dev.password=test1234
liquibase.dev.password=test1234

liquibase.test.url=jdbc:postgresql://localhost:5433/kryptokrona_api_test
liquibase.test.user=postgres
liquibase.test.password=test1234
2 changes: 1 addition & 1 deletion src/main/kotlin/org/kryptokrona/api/models/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import java.time.LocalDateTime
interface Block : Entity<Block> {
companion object : Entity.Factory<Block>()

val id: Long
var id: Long
var hash: String
var time: Long
var difficulty: Float
Expand Down
40 changes: 23 additions & 17 deletions src/main/kotlin/org/kryptokrona/api/syncers/HuginSyncer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

package org.kryptokrona.api.syncers

import io.ktor.client.plugins.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
Expand Down Expand Up @@ -74,25 +75,30 @@ class HuginSyncer {
launch {
while(isActive) {
logger.debug("Syncing Hugin...")
val retrievedData = poolChangesClient.getPoolChangesLite()
val transactions = retrievedData?.addedTxs

transactions?.let {
for (tx in it) {
val extra = tx.transactionPrefix.extra
val transactionHash = tx.transactionHash

if (extra.length > 200 && transactionHash !in knownPoolTxsList) {
logger.debug("Incoming transaction $transactionHash")
knownPoolTxsList += transactionHash
val extraData = trimExtra(extra)

if ("box" in extraData) savePostEncrypted(extraData, tx)
else if ("sb" in extraData) savePostEncryptedGroup(extraData, tx)
} else {
logger.debug("Extra is less than 200 in length, skipping...")

try {
val retrievedData = poolChangesClient.getPoolChangesLite()
val transactions = retrievedData?.addedTxs

transactions?.let {
for (tx in it) {
val extra = tx.transactionPrefix.extra
val transactionHash = tx.transactionHash

if (extra.length > 200 && transactionHash !in knownPoolTxsList) {
logger.debug("Incoming transaction $transactionHash")
knownPoolTxsList += transactionHash
val extraData = trimExtra(extra)

if ("box" in extraData) savePostEncrypted(extraData, tx)
else if ("sb" in extraData) savePostEncryptedGroup(extraData, tx)
} else {
logger.debug("Extra is less than 200 in length, skipping...")
}
}
}
} catch (e: HttpRequestTimeoutException) {
logger.error("Error syncing Hugin: ${e.message}")
}

delay(HuginConfig.SYNC_INTERVAL)
Expand Down
49 changes: 47 additions & 2 deletions src/test/kotlin/org/kryptokrona/api/routes/BlockRoutesTest.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
package org.kryptokrona.api.routes;

import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.Before
import org.kryptokrona.api.models.Block
import org.kryptokrona.api.services.block.BlockServiceImpl
import org.kryptokrona.api.utils.jsonObjectMapper
import org.kryptokrona.sdk.http.model.block.Blocks
import org.ktorm.entity.Entity
import org.slf4j.LoggerFactory
import java.time.LocalDateTime
import java.time.LocalDateTime.now
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class BlockRoutesTest {

private val logger = LoggerFactory.getLogger("BlockRoutesTest")

private val blockService: BlockServiceImpl = BlockServiceImpl()

private val baseEndpoint: String = "/api/v1/blocks"

@Before
fun beforeEach() = testApplication {
val block = Block {
id = 1
hash = "hash"
time = 10000
difficulty = 1.0F
reward = 1.0F
createdAt = now()
}
blockService.getById(block.id).let {
if (it == null) {
blockService.save(block)
logger.info("Block with id: ${block.id} saved")
} else {
logger.info("Block with id: ${it.id} already exists")
}
}
}

@Test
fun blockRouteTest() = testApplication {
client.get("/api/v1/blocks").apply {
fun `can get all blocks`() = testApplication {
client.get(baseEndpoint).apply {
assertEquals(HttpStatusCode.OK, status)
}
}

@Test
fun `can get a block by id`() = testApplication {
val blockId = 1
val response = client.get("$baseEndpoint/$blockId").body<Block>()

assertTrue { blockId.toLong() == response.id }
}
}
18 changes: 18 additions & 0 deletions src/test/kotlin/org/kryptokrona/api/routes/HashrateRoutesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kryptokrona.api.routes

import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertEquals

class HashrateRoutesTest {

@Test
fun `can get all hashrates`() = testApplication {
client.get("/api/v1/hashrates").apply {
assertEquals(HttpStatusCode.OK, status)
}
}

}
4 changes: 4 additions & 0 deletions src/test/kotlin/org/kryptokrona/api/routes/InfoRoutesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.kryptokrona.api.routes

class InfoRoutesTest {
}
Loading