Skip to content

Commit ca9c86f

Browse files
committed
Looking for websocket issues.
1 parent faeb03d commit ca9c86f

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/main/kotlin/chain/Chain.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import kotlin.concurrent.withLock
1616
* on 16/11/2021 at 16:21
1717
* using IntelliJ IDEA
1818
*/
19-
class Chain(private val verifiableDelay: VerifiableDelay, private val initialDifficulty: Int, private val committeeSize: Int) {
19+
class Chain(
20+
private val verifiableDelay: VerifiableDelay,
21+
private val initialDifficulty: Int,
22+
private val committeeSize: Int
23+
) {
2024

2125
private val lock = ReentrantLock(true)
2226
private val blocks = CircularList<Block>(50) // ToDo: Remove, do not use Circular List but use persistent storage!
@@ -37,7 +41,8 @@ class Chain(private val verifiableDelay: VerifiableDelay, private val initialDif
3741
val lastBlock = getLastBlock()
3842
val lastHash = lastBlock?.hash ?: "FFFF".toByteArray()
3943
val difficulty = lastBlock?.difficulty ?: initialDifficulty
40-
val isLegitimate = nextBlock.slot == (lastBlock?.slot ?: 0) + 1 // verifiableDelay.verifyProof(lastHash, difficulty, nextBlock.vdfProof)
44+
val isLegitimate = nextBlock.slot == (lastBlock?.slot
45+
?: 0) + 1 // verifiableDelay.verifyProof(lastHash, difficulty, nextBlock.vdfProof)
4146
if (!isLegitimate) {
4247
Logger.trace("Proof is not legitimate for block ${nextBlock.slot}!")
4348
Logger.chain("Last hash: ${lastHash.asHex}")
@@ -48,6 +53,7 @@ class Chain(private val verifiableDelay: VerifiableDelay, private val initialDif
4853
return false
4954
}
5055
lock.tryWithLock {
56+
5157
blocks.add(nextBlock)
5258
// ToDo: Put chain history in some sort of storage instead of keeping in memory.
5359
transaction {

src/main/kotlin/network/messaging/Server.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ abstract class Server(val configuration: Configuration) : RPCManager(configurati
6565
Logger.trace("Clearing message history...")
6666
messageHistory.entries.removeIf { (_, value) -> currentTime - value > maximumAge }
6767
}
68+
6869
}
6970

7071
abstract fun processMessage(message: Message)
@@ -91,7 +92,10 @@ abstract class Server(val configuration: Configuration) : RPCManager(configurati
9192

9293
/** Returns [Configuration.broadcastSpreadPercentage] number of nodes. */
9394
fun pickRandomNodes(amount: Int = 0): List<Node> {
94-
val toTake = if (amount > 0) amount else 5 + (configuration.broadcastSpreadPercentage * Integer.max(totalKnownNodes, 1) / 100)
95+
val toTake = if (amount > 0) amount else 5 + (configuration.broadcastSpreadPercentage * Integer.max(
96+
totalKnownNodes,
97+
1
98+
) / 100)
9599
return getRandomNodes(toTake).filter { it.identifier != localNode.identifier }
96100
}
97101

@@ -160,7 +164,11 @@ abstract class Server(val configuration: Configuration) : RPCManager(configurati
160164
val message = ProtoBuf.decodeFromByteArray<Message>(data)
161165
if (alreadySeen(message.uid.asHex)) return@use
162166
processingQueue.add(message)
163-
if (message.endpoint.transmissionType == TransmissionType.Broadcast) broadcast(TransmissionLayer.TCP, message.uid.asHex, data)
167+
if (message.endpoint.transmissionType == TransmissionType.Broadcast) broadcast(
168+
TransmissionLayer.TCP,
169+
message.uid.asHex,
170+
data
171+
)
164172
}
165173
}
166174
}
@@ -199,22 +207,30 @@ abstract class Server(val configuration: Configuration) : RPCManager(configurati
199207
broadcastNodes.add(neighbour)
200208
broadcastNodes.addAll(childrenKeys)
201209
broadcastNodes.addAll(neighbourChildrenKeys)
202-
Logger.error("[$index] [$children] Neighbour: $neighbourIndex ... Children: ${childrenKeys.joinToString(",") { "${shuffled.indexOf(it)}" }}")
210+
Logger.error(
211+
"[$index] [$children] Neighbour: $neighbourIndex ... Children: ${
212+
childrenKeys.joinToString(
213+
","
214+
) { "${shuffled.indexOf(it)}" }
215+
}"
216+
)
203217

204218
}
205219

206220
else -> broadcastNodes.addAll(pickRandomNodes().map { it.publicKey })
207221
}
208222

209-
val knownAndNotInSet = knownNodes.values.map(Node::publicKey).filter { !validatorSet.activeValidators.contains(it) }
223+
val knownAndNotInSet =
224+
knownNodes.values.map(Node::publicKey).filter { !validatorSet.activeValidators.contains(it) }
210225
broadcastNodes.addAll(knownAndNotInSet)
211226

212227
Logger.trace("We have to retransmit to [total: ${shuffled.size}] --> ${broadcastNodes.size} nodes.")
213228

214229
broadcastNodes.forEach { publicKey ->
215230
query(publicKey) {
216231
val outgoingData = OutgoingData(it, *data)
217-
val outgoingQueue = if (transmissionLayer == TransmissionLayer.UDP) udpOutgoingQueue else tcpOutgoingQueue
232+
val outgoingQueue =
233+
if (transmissionLayer == TransmissionLayer.UDP) udpOutgoingQueue else tcpOutgoingQueue
218234
outgoingQueue.add(outgoingData)
219235
}
220236
}
@@ -242,7 +258,11 @@ abstract class Server(val configuration: Configuration) : RPCManager(configurati
242258
if (alreadySeen(packetId) || alreadySeen(messageId)) return@tryAndReport
243259

244260
val endpoint = Endpoint.byId(inputStream.read().toByte()) ?: return@tryAndReport
245-
if (endpoint.transmissionType == TransmissionType.Broadcast) broadcast(TransmissionLayer.UDP, messageId, packet.data.copyOf())
261+
if (endpoint.transmissionType == TransmissionType.Broadcast) broadcast(
262+
TransmissionLayer.UDP,
263+
messageId,
264+
packet.data.copyOf()
265+
)
246266

247267
val totalSlices = inputStream.readInt()
248268
val currentSlice = inputStream.readInt()

src/main/kotlin/network/rpc/RPCManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ open class RPCManager(configuration: Configuration) : Kademlia(configuration) {
7777
val serialisedData = Json.encodeToString(message)
7878
val clientList = subscribedClients[topic] ?: return
7979
clientList.forEach {
80+
println("Websocket data: $serialisedData")
8081
try {
8182
it.send(serialisedData)
8283
} catch (e: Exception) {

0 commit comments

Comments
 (0)