From 2f0fcd265655bffed746c29c28760a02975c5b1d Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Fri, 7 Feb 2025 15:37:50 +0100 Subject: [PATCH 1/5] Change coroutine usage --- build.gradle | 2 +- .../trustchain/demo/ui/DemoActivity.kt | 83 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/build.gradle b/build.gradle index 16a15c11..72bf7cb2 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ buildscript { maven { url 'https://jitpack.io' } } dependencies { - classpath 'com.android.tools.build:gradle:8.8.0' + classpath 'com.android.tools.build:gradle:8.8.0-alpha05' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktlint_gradle_version" classpath "app.cash.sqldelight:gradle-plugin:$sqldelight_version" diff --git a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt index 6fa0644f..b6a35577 100644 --- a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt +++ b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt @@ -5,12 +5,15 @@ import android.widget.LinearLayout import androidx.appcompat.app.AppCompatActivity import androidx.core.content.res.ResourcesCompat import androidx.core.view.isVisible +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.mattskala.itemadapter.ItemAdapter import kotlinx.coroutines.delay import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import nl.tudelft.ipv8.android.IPv8Android import nl.tudelft.trustchain.demo.DemoCommunity import nl.tudelft.trustchain.demo.R @@ -45,46 +48,48 @@ class DemoActivity : AppCompatActivity() { } private fun loadNetworkInfo() { - lifecycleScope.launchWhenStarted { - while (isActive) { - val demoCommunity = IPv8Android.getInstance().getOverlay()!! - val peers = demoCommunity.getPeers() - - val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) - - val discoveredBluetoothAddresses = - demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } - - val peerItems = peers.map { - PeerItem( - it - ) - } - - val addressItems = discoveredAddresses.map { address -> - val contacted = demoCommunity.discoveredAddressesContacted[address] - AddressItem( - address, null, contacted - ) + lifecycleScope.launch { + lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { + while (isActive) { + val demoCommunity = IPv8Android.getInstance().getOverlay()!! + val peers = demoCommunity.getPeers() + + val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) + + val discoveredBluetoothAddresses = + demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } + + val peerItems = peers.map { + PeerItem( + it + ) + } + + val addressItems = discoveredAddresses.map { address -> + val contacted = demoCommunity.discoveredAddressesContacted[address] + AddressItem( + address, null, contacted + ) + } + + val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> + AddressItem( + address, null, null + ) + } + + val items = peerItems + bluetoothAddressItems + addressItems + + adapter.updateItems(items) + binding.txtCommunityName.text = demoCommunity.javaClass.simpleName + binding.txtPeerCount.text = "${peers.size} peers" + val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red + val textColor = ResourcesCompat.getColor(resources, textColorResId, null) + binding.txtPeerCount.setTextColor(textColor) + binding.imgEmpty.isVisible = items.isEmpty() + + delay(1000) } - - val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> - AddressItem( - address, null, null - ) - } - - val items = peerItems + bluetoothAddressItems + addressItems - - adapter.updateItems(items) - binding.txtCommunityName.text = demoCommunity.javaClass.simpleName - binding.txtPeerCount.text = "${peers.size} peers" - val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red - val textColor = ResourcesCompat.getColor(resources, textColorResId, null) - binding.txtPeerCount.setTextColor(textColor) - binding.imgEmpty.isVisible = items.isEmpty() - - delay(1000) } } } From 000469702c1a5368a5e5df7faea506629b01e13e Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Fri, 7 Feb 2025 15:48:55 +0100 Subject: [PATCH 2/5] Revert "Use Default dispatcher instead of Main in Community scope." This reverts commit 6a40fa2381ce625088bc6b69bbb8b390735f3497. --- ipv8/src/main/java/nl/tudelft/ipv8/Community.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt b/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt index 26cedae8..ad972e2e 100644 --- a/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt +++ b/ipv8/src/main/java/nl/tudelft/ipv8/Community.kt @@ -73,7 +73,7 @@ abstract class Community : Overlay { network.blacklist.addAll(DEFAULT_ADDRESSES) job = SupervisorJob() - scope = CoroutineScope(Dispatchers.Default + job) + scope = CoroutineScope(Dispatchers.Main + job) if (evaProtocolEnabled) evaProtocol = EVAProtocol(this, scope) From 6d19d5b69f084b8caed09de74a18a700397e3e68 Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Fri, 7 Feb 2025 16:12:39 +0100 Subject: [PATCH 3/5] Remove unnecessary `isActive` --- .../trustchain/demo/ui/DemoActivity.kt | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt index b6a35577..b14fc96a 100644 --- a/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt +++ b/demo-android/src/main/java/nl/tudelft/trustchain/demo/ui/DemoActivity.kt @@ -12,7 +12,6 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.mattskala.itemadapter.ItemAdapter import kotlinx.coroutines.delay -import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import nl.tudelft.ipv8.android.IPv8Android import nl.tudelft.trustchain.demo.DemoCommunity @@ -44,53 +43,54 @@ class DemoActivity : AppCompatActivity() { binding.recyclerView.layoutManager = LinearLayoutManager(this) binding.recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL)) - loadNetworkInfo() - } - - private fun loadNetworkInfo() { lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { - while (isActive) { - val demoCommunity = IPv8Android.getInstance().getOverlay()!! - val peers = demoCommunity.getPeers() - - val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) - - val discoveredBluetoothAddresses = - demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } - - val peerItems = peers.map { - PeerItem( - it - ) - } - - val addressItems = discoveredAddresses.map { address -> - val contacted = demoCommunity.discoveredAddressesContacted[address] - AddressItem( - address, null, contacted - ) - } - - val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> - AddressItem( - address, null, null - ) - } - - val items = peerItems + bluetoothAddressItems + addressItems - - adapter.updateItems(items) - binding.txtCommunityName.text = demoCommunity.javaClass.simpleName - binding.txtPeerCount.text = "${peers.size} peers" - val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red - val textColor = ResourcesCompat.getColor(resources, textColorResId, null) - binding.txtPeerCount.setTextColor(textColor) - binding.imgEmpty.isVisible = items.isEmpty() - + while (true) { + loadNetworkInfo() delay(1000) } } } } + + private fun loadNetworkInfo() { + val demoCommunity = IPv8Android.getInstance().getOverlay()!! + val peers = demoCommunity.getPeers() + + val discoveredAddresses = demoCommunity.network.getWalkableAddresses(demoCommunity.serviceId) + + val discoveredBluetoothAddresses = + demoCommunity.network.getNewBluetoothPeerCandidates().map { it.address } + + val peerItems = peers.map { + PeerItem( + it + ) + } + + val addressItems = discoveredAddresses.map { address -> + val contacted = demoCommunity.discoveredAddressesContacted[address] + AddressItem( + address, null, contacted + ) + } + + val bluetoothAddressItems = discoveredBluetoothAddresses.map { address -> + AddressItem( + address, null, null + ) + } + + val items = peerItems + bluetoothAddressItems + addressItems + + adapter.updateItems(items) + binding.txtCommunityName.text = demoCommunity.javaClass.simpleName + binding.txtPeerCount.text = "${peers.size} peers" + val textColorResId = if (peers.isNotEmpty()) R.color.green else R.color.red + val textColor = ResourcesCompat.getColor(resources, textColorResId, null) + binding.txtPeerCount.setTextColor(textColor) + binding.imgEmpty.isVisible = items.isEmpty() + + + } } From 41fdb07f43ff6a4446916f794331fd89fc31457d Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Fri, 7 Feb 2025 16:19:59 +0100 Subject: [PATCH 4/5] Add additional build tasks --- .github/workflows/build.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 587a7444..e45f84c3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,6 +11,14 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Debug - Print Last Commit + run: git log -1 + + - name: Debug - Check File Changes + run: git diff --name-only HEAD~1 HEAD - name: Setup Java uses: actions/setup-java@v2 @@ -22,6 +30,9 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x gradlew + - name: Clear Gradle Cache + run: ./gradlew clean + - name: Run Check run: ./gradlew check From ec7715015d5752e39bbe9af7f052aff6fb5ff0bf Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Fri, 7 Feb 2025 16:28:39 +0100 Subject: [PATCH 5/5] Update `build.yml` --- .github/workflows/build.yml | 47 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e45f84c3..ac1d3f44 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,24 +1,22 @@ name: build on: - pull_request_target: + pull_request_target: # Runs on PRs from forks, safely (no secrets) push: - branches: master + branches: master # Runs on direct pushes to master jobs: test: runs-on: ubuntu-latest + steps: - - name: Checkout + - name: Checkout PR Code Securely uses: actions/checkout@v3 with: - fetch-depth: 0 - - - name: Debug - Print Last Commit - run: git log -1 + ref: ${{ github.event.pull_request.head.sha }} - - name: Debug - Check File Changes - run: git diff --name-only HEAD~1 HEAD + - name: Debug - Print GitHub Event + run: echo "Triggered by ${{ github.event_name }}" - name: Setup Java uses: actions/setup-java@v2 @@ -30,22 +28,32 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Clear Gradle Cache - run: ./gradlew clean - - - name: Run Check + - name: Run Check (No Secrets) run: ./gradlew check - - name: Run Jacoco + - name: Run Jacoco (No Secrets) run: ./gradlew jacocoTestReport - - name: Upload Report + - name: Upload Test Report (No Secrets) uses: 'actions/upload-artifact@v4' with: name: report.xml path: ${{ github.workspace }}/ipv8/build/reports/jacoco/test/jacocoTestReport.xml - - name: Add coverage to PR + secure-tasks: + needs: test + runs-on: ubuntu-latest + if: github.event_name == 'push' || github.event.pull_request.head.repo.fork == false # Runs only if merged or trusted contributor + steps: + - name: Checkout Latest Code + uses: actions/checkout@v3 + + - name: Upload Coverage to Codecov (Requires Secrets) + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Add Coverage to PR (Requires Secrets) id: jacoco uses: madrapps/jacoco-report@v1.7.1 with: @@ -54,12 +62,7 @@ jobs: min-coverage-overall: 60 min-coverage-changed-files: 80 - - name: Get the Coverage info + - name: Get Coverage Info run: | echo "Total coverage ${{ steps.jacoco.outputs.coverage-overall }}" echo "Changed Files coverage ${{ steps.jacoco.outputs.coverage-changed-files }}" - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }}