-
Notifications
You must be signed in to change notification settings - Fork 30
Description
The Trustchain functionality does not work at all when running from the command line. The tests do, however, pass. This is specifically due to this snippet in Community.kt:
scope = CoroutineScope(Dispatchers.Main + job)The Main dispatcher is meant to interact with UI objects and as such would like an Android, JavaFX, or Swing dependency. This is of course undesirable when running from command line. When running from command line, the application crashes when functionality using the Main dispatcher is used, for instance when signing agreement blocks. The demo-jvm application is too rudimentary to pick up on this. The unit tests also do not find this because during testing, the Main dispatcher is set to be a TestCoroutineDispatcher (see BaseCommunityTest.kt) which does work without a GUI. Additionally, it is considered bad practice to hardcode your dispatchers.
How to reproduce
Replace startIpv8() in Application.kt with the following and run two instances of the application:
private fun startIpv8() {
val myKey = JavaCryptoProvider.generateKey()
val myPeer = Peer(myKey)
val udpEndpoint = UdpEndpoint(8090, InetAddress.getByName("0.0.0.0"))
val endpoint = EndpointAggregator(udpEndpoint, null)
val config = IPv8Configuration(overlays = listOf(
createDiscoveryCommunity(),
createTrustChainCommunity(),
createDemoCommunity()
), walkerInterval = 1.0)
val ipv8 = IPv8(endpoint, config, myPeer)
ipv8.start()
val trustCom = ipv8.getOverlay<TrustChainCommunity>()!!
val demoCom = ipv8.getOverlay<DemoCommunity>()!!
trustCom.registerBlockSigner("trust_issue_block", object : BlockSigner {
override fun onSignatureRequest(block: TrustChainBlock) {
logger.info("Received an issue block.")
trustCom.createAgreementBlock(block, mapOf<Any?, Any?>())
}
})
scope.launch {
while (true) {
if (demoCom.getPeers().isNotEmpty()) {
trustCom.createProposalBlock(
"trust_issue_block",
mapOf("message" to "hi"),
demoCom.getPeers()[0].publicKey.keyToBin()
)
}
for ((_, overlay) in ipv8.overlays) {
printPeersInfo(overlay)
}
logger.info("===")
delay(5000)
}
}
while (ipv8.isStarted()) {
Thread.sleep(1000)
}
}