Skip to content

Commit

Permalink
Add checks for peers being in the same dao, and make tests work again
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinten Van Opstal committed May 3, 2024
1 parent eb9be9e commit 1afce7b
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCommunity
import nl.tudelft.ipv8.attestation.trustchain.TrustChainTransaction
import nl.tudelft.ipv8.keyvault.PublicKey
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.util.hexToBytes
import nl.tudelft.ipv8.util.toHex
Expand Down Expand Up @@ -38,8 +39,7 @@ import nl.tudelft.trustchain.currencyii.util.DAOTransferFundsHelper
open class CoinCommunity constructor(
private val context: Context,
serviceId: String = "02313685c1912a141279f8248fc8db5899c5df5b",
) : Community() {

) : Community() {
class Factory(
private val context: Context,
) : Overlay.Factory<CoinCommunity>(CoinCommunity::class.java) {
Expand All @@ -56,9 +56,7 @@ open class CoinCommunity constructor(
messageHandlers[MessageId.ELECTION_REQUEST] = ::onElectionRequestPacket
messageHandlers[MessageId.ELECTED_RESPONSE] = ::onElectedResponsePacket
messageHandlers[MessageId.ALIVE_RESPONSE] = ::onAliveResponsePacket
messageHandlers[MessageId.SIGNATURE_ASK] = ::onSignPayloadResponsePacket
messageHandlers[MessageId.JOIN_DAO_DATA] = ::onDaoJoinDataPacket

}

private fun getTrustChainCommunity(): TrustChainCommunity {
Expand Down Expand Up @@ -282,7 +280,7 @@ open class CoinCommunity constructor(
signatures: List<SWResponseSignatureBlockTD>
): ByteArray {
val payload = SignPayload(dAOid, recentSWBlock, proposeBlockData, signatures)
return serializePacket(MessageId.SIGNATURE_ASK, payload)
return serializePacket(MessageId.JOIN_DAO_DATA, payload)
}

fun onAliveResponsePacket(packet: Packet) {
Expand All @@ -293,14 +291,6 @@ open class CoinCommunity constructor(
this.onAliveResponse(peer, payload)
}

fun onSignPayloadResponsePacket(packet: Packet) {
val (peer, payload) =
packet.getAuthPayload(
SignPayload.Deserializer
)
this.onSignPayloadResponse(peer, payload)
}

private fun onDaoJoinDataPacket(packet: Packet) {
Log.d("LEADER", "Received data from Peer wanting to join")

Expand Down Expand Up @@ -328,7 +318,7 @@ open class CoinCommunity constructor(
peer: Peer,
payload: SignPayload
) {
//TODO: Implement adding to the wallet without a Context
// TODO: Implement adding to the wallet without a Context
try {
joinBitcoinWallet(
payload.mostRecentSWBlock.transaction,
Expand All @@ -344,8 +334,6 @@ open class CoinCommunity constructor(
}
}



fun onAliveResponse(
peer: Peer,
payload: AlivePayload
Expand Down Expand Up @@ -463,43 +451,40 @@ open class CoinCommunity constructor(
Log.d("LEADER", "Leader doesn't exists.")
Log.d("LEADER", "Requesting election...")
val peers = this.getPeers()
val peerPK = getPeersPKInDao(publicKeyBlock)
for (peer in peers) {
sendPayload(peer, this.createElectionRequest(publicKeyBlock))
Log.d("LEADER", "Sending to peer at " + peer.address + " in " + serviceId + "...")
if (peerPK.contains(peer.publicKey.keyToBin().decodeToString())) {
sendPayload(peer, this.createElectionRequest(publicKeyBlock))
Log.d("LEADER", "Sending to peer at " + peer.address + " in " + serviceId + "...")
}
}
Log.d("LEADER", "Waiting for leader...")
while (!this.checkLeaderExists(publicKeyBlock)) {
Thread.sleep(1000)
}
Log.d("LEADER", "Leader found.")



val currentLeader = getCurrentLeader()[publicKeyBlock.decodeToString()]!!
Log.d("LEADER", "sending dao join transaction data to leader ${currentLeader.publicKey}")
val payload = SignPayload(
getServiceIdNew().toByteArray(),
mostRecentSWBlock,
proposeBlockData,
signatures);
val payload =
SignPayload(
getServiceIdNew().toByteArray(),
mostRecentSWBlock,
proposeBlockData,
signatures
)
val packet = serializePacket(MessageId.JOIN_DAO_DATA, payload)
send(currentLeader, packet)
sendPayload(currentLeader, packet)

toastLeaderSignProposal(currentLeader.publicKey)
}

fun toastLeaderSignProposal(publicKey: PublicKey) {
Toast.makeText(
context,
"Sending DAO join data to ${currentLeader.publicKey}",
"Sending DAO join data to $publicKey",
Toast.LENGTH_SHORT
).show()

sendPayload(
getCurrentLeader()[publicKeyBlock.decodeToString()]!!,
SignPayload(
getServiceIdNew().toByteArray(),
mostRecentSWBlock,
proposeBlockData,
signatures
).serialize()
)
}

fun checkLeaderExists(dAOid: ByteArray): Boolean {
Expand Down Expand Up @@ -696,9 +681,7 @@ open class CoinCommunity constructor(
const val ELECTION_REQUEST = 1
const val ELECTED_RESPONSE = 2
const val ALIVE_RESPONSE = 3
const val SIGNATURE_ASK = 4
const val JOIN_DAO_DATA = 5

const val JOIN_DAO_DATA = 4
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SignPayload(
val signatures: List<SWResponseSignatureBlockTD>
) : Serializable {
override fun serialize(): ByteArray {
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create()

val daoIdSizeBytes = serializeUShort(DAOid.size)

Expand All @@ -45,8 +45,6 @@ class SignPayload(
val timeStampBytes = gson.toJson(mostRecentSWBlock.timestamp).toByteArray()
val timeStampSizeBytes = serializeUShort(timeStampBytes.size)



val proposeBlockDataJson = gson.toJson(proposeBlockData)
val proposeBlockDataBytes = proposeBlockDataJson.toByteArray()
val proposeBlockDataSizeBytes = serializeUShort(proposeBlockDataBytes.size)
Expand All @@ -70,7 +68,7 @@ class SignPayload(
}

companion object Deserializer : Deserializable<SignPayload> {
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create()

override fun deserialize(
buffer: ByteArray,
Expand Down Expand Up @@ -132,9 +130,18 @@ class SignPayload(
localOffset += timestampSize

val date = gson.fromJson(timestamp.decodeToString(), Date::class.java)
val mostRecentSWBlock = TrustChainBlock(type.decodeToString(), rawTransaction, publicKey, deserializeUInt(sequenceNumber),
linkPublicKey, deserializeUInt(linkSequenceNumber), previousHash, signature, date)

val mostRecentSWBlock =
TrustChainBlock(
type.decodeToString(),
rawTransaction,
publicKey,
deserializeUInt(sequenceNumber),
linkPublicKey,
deserializeUInt(linkSequenceNumber),
previousHash,
signature,
date
)

val proposeBlockDataSize = deserializeUShort(buffer, offset + localOffset)
localOffset += SERIALIZED_USHORT_SIZE
Expand Down
Loading

0 comments on commit 1afce7b

Please sign in to comment.