Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the TransactionsEngine API #154

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions detoks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
}

// AndroidX
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class DeToksCommunity(
companion object {
const val LOGGING_TAG = "DeToksCommunity"
const val MESSAGE_TORRENT_ID = 1
const val MESSAGE_TRANSACTION_ID = 2
const val MESSAGE_WATCH_TIME_ID = 3
const val MESSAGE_NETWORK_SIZE_ID = 4
const val MESSAGE_BOOT_REQUEST = 5
Expand All @@ -51,13 +50,7 @@ class DeToksCommunity(
recipientWallet.balance += amount
walletManager.setWalletBalance(recipientMid, recipientWallet.balance)

for (peer in getPeers()) {
val packet = serializePacket(
MESSAGE_TRANSACTION_ID,
TransactionMessage(amount, myPeer.mid, recipientMid)
)
send(peer.address, packet)
}
broadcastTokenTransaction(amount, myPeer.mid, recipientMid)
} else {
Log.d(LOGGING_TAG, "Insufficient funds!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ package nl.tudelft.trustchain.detoks
class DetoksConfig() {
companion object {
const val DETOKS_SERVICE_ID = "c86a7db45eb3563ae047639817baec4db2bc7c25"
const val DEFAULT_MESSAGE_LENGTH = 200
const val DEFAULT_PUBLIC_KEY_LENGTH = 64
}
}
104 changes: 52 additions & 52 deletions detoks/src/main/java/nl/tudelft/trustchain/detoks/TransactionEngine.kt
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
package nl.tudelft.trustchain.detoks

import android.util.Log
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.Overlay
import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.attestation.trustchain.BlockBuilder
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import nl.tudelft.ipv8.attestation.trustchain.payload.HalfBlockBroadcastPayload
import nl.tudelft.ipv8.attestation.trustchain.payload.HalfBlockPayload
import nl.tudelft.ipv8.messaging.Address
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.util.random
import java.util.*
import mu.KotlinLogging


open class TransactionEngine (override val serviceId: String): Community() {
private val broadcastFanOut = 25
private val ttl = 100
private val logger = KotlinLogging.logger {}

object MessageId {
const val HALF_BLOCK: Int = 11
const val HALF_BLOCK_ENCRYPTED: Int = 12
const val HALF_BLOCK_BROADCAST: Int = 13
const val HALF_BLOCK_BROADCAST_ENCRYPTED: Int = 14

companion object {
const val MESSAGE_TRANSACTION_ID = 2
}

init {
messageHandlers[MessageId.HALF_BLOCK] = ::onHalfBlockPacket
messageHandlers[MessageId.HALF_BLOCK_BROADCAST] = ::onHalfBlockBroadcastPacket
messageHandlers[MessageId.HALF_BLOCK_ENCRYPTED] = ::onHalfBlockPacket
messageHandlers[MessageId.HALF_BLOCK_BROADCAST_ENCRYPTED] = ::onHalfBlockBroadcastPacket
/**
* Broadcasts a token transaction to all known peers.
* @param amount the amount of tokens to send
* @param senderMid the member ID of the peer that sends the amount of tokens
* @param recipientMid the member ID of the peer that will receive the amount of tokens
*/
fun broadcastTokenTransaction(amount: Int,
senderMid: String,
recipientMid: String) {
for (peer in getPeers()) {
sendTokenTransaction(amount, senderMid, recipientMid, peer.address)
}
}

fun sendTransaction(blockBuilder: BlockBuilder, peer: Peer?, encrypt: Boolean = false) {
logger.info { "Sending transaction..." }
val block = blockBuilder.sign()
/**
* Sends a token transaction to a peer.
* @param amount the amount of tokens to be sent
* @param senderMid the member ID of the peer that sends the amount of tokens
* @param recipientMid the member ID of the peer that will receive the amount of tokens
* @param receiverAddress the address of the peer that receives the transaction. That
* peer may be different than the recipient of the amount of tokens in the transaction
*/
fun sendTokenTransaction(amount: Int,
senderMid: String,
recipientMid: String,
receiverAddress: Address) {
val packet = serializePacket(
MESSAGE_TRANSACTION_ID,
TransactionMessage(amount, senderMid, recipientMid)
)
send(receiverAddress, packet)
}

if (peer != null) {
sendBlockToRecipient(peer, block, encrypt)
} else {
sendBlockBroadcast(block, encrypt)
}
fun sendTransaction(block: TrustChainBlock,
peer: Peer,
encrypt: Boolean = false,
msgID: Int) {
Log.d("TransactionEngine",
"Creating transaction to peer with public key: " + peer.key.pub().toString())
sendBlockToRecipient(peer, block, encrypt, msgID)
}

private fun sendBlockToRecipient(peer: Peer, block: TrustChainBlock, encrypt: Boolean) {
private fun sendBlockToRecipient(peer: Peer,
block: TrustChainBlock,
encrypt: Boolean,
msgID: Int) {
val payload = HalfBlockPayload.fromHalfBlock(block)

val data = if (encrypt) {
serializePacket(MessageId.HALF_BLOCK_ENCRYPTED, payload, false, encrypt = true, recipient = peer)
serializePacket(
msgID, payload, false, encrypt = true, recipient = peer, peer = myPeer
)
} else {
serializePacket(MessageId.HALF_BLOCK, payload, false)
serializePacket(msgID, payload, false, peer = myPeer)
}

send(peer, data)
}

private fun sendBlockBroadcast(block: TrustChainBlock, encrypt: Boolean) {
val payload = HalfBlockBroadcastPayload.fromHalfBlock(block, ttl.toUInt())
val randomPeers = getPeers().random(broadcastFanOut)
for (randomPeer in randomPeers) {
val data = if (encrypt) {
serializePacket(MessageId.HALF_BLOCK_BROADCAST_ENCRYPTED, payload, false, encrypt = true, recipient = randomPeer)
} else {
serializePacket(MessageId.HALF_BLOCK_BROADCAST, payload, false)
}
send(randomPeer, data)
}
}

private fun onHalfBlockPacket(packet: Packet) {
logger.info { ("Half block packet received from: " + packet.source.toString()) }
}

private fun onHalfBlockBroadcastPacket(packet: Packet) {
logger.info { ("Half block packet received from broadcast from: " + packet.source.toString()) }
}

override fun onPacket(packet: Packet) {
val sourceAddress = packet.source
val data = packet.data
Expand All @@ -102,7 +101,8 @@ open class TransactionEngine (override val serviceId: String): Community() {
e.printStackTrace()
}
} else {
logger.info { "Received unknown message $msgId from $sourceAddress" }
Log.d("TransactionEngine",
"Received unknown message $msgId from $sourceAddress" )
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nl.tudelft.trustchain.detoks.benchmark

import nl.tudelft.ipv8.keyvault.PrivateKey

class BasicBlock (
private val type: String,
private val message: ByteArray,
private val senderPublicKey: ByteArray,
val receiverPublicKey: ByteArray,
) {
private var signature: ByteArray = "".toByteArray()

fun sign(key: PrivateKey) {
signature = key.sign(type.toByteArray() + message + senderPublicKey)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nl.tudelft.trustchain.detoks.benchmark

import com.github.mikephil.charting.data.Entry

/**
* This is a wrapper around benchmark results for the TransactionEngine.
*/
data class BenchmarkResult (
var timePerBlock : ArrayList<Entry>,
var totalTime : Long,
var payloadBandwith : Double,
var lostPackets: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.tudelft.trustchain.detoks.benchmark

import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.attestation.trustchain.BlockBuilder
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import nl.tudelft.ipv8.attestation.trustchain.UNKNOWN_SEQ
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainSQLiteStore

/**
* This is a class for creating TrustChainBlocks for benchmarks.
*/
class SimpleBlockBuilder(
myPeer : Peer,
database: TrustChainSQLiteStore,
private val blockType: String,
private val transaction: ByteArray,
private val publicKey: ByteArray
) : BlockBuilder(myPeer, database) {
override fun update(builder: TrustChainBlock.Builder) {
builder.type = blockType
builder.rawTransaction = transaction
builder.linkPublicKey = publicKey
builder.linkSequenceNumber = UNKNOWN_SEQ
}
}