Migrated everything except

- the Bytes utilities class - it's easier to do in Java as with Kotlin byte + byte = int
- the demo project, which I'm not sure I'll migrate. Maybe I'll make a new Kotlin Demo application
This commit is contained in:
Christian Basler 2017-06-28 00:01:05 +02:00
parent aee5debdd2
commit a245288359
120 changed files with 810 additions and 845 deletions

View File

@ -17,7 +17,7 @@
package ch.dissem.bitmessage.constants
/**
* Created by chrigu on 03.06.17.
* Some network constants
*/
object Network {
@JvmField val NETWORK_MAGIC_NUMBER = 8

View File

@ -28,9 +28,7 @@ import java.nio.ByteBuffer
class GetPubkey : ObjectPayload {
override val type: ObjectType = ObjectType.GET_PUBKEY
override var stream: Long = 0
private set
override val stream: Long
/**
* @return an array of bytes that represent either the ripe, or the tag of an address, depending on the

View File

@ -48,7 +48,7 @@ data class InventoryVector constructor(
}
override fun toString(): String {
return Strings.hex(hash).toString()
return Strings.hex(hash)
}
companion object {

View File

@ -72,6 +72,9 @@ data class NetworkAddress(
constructor(time: Long, stream: Long, services: Long = 1, socket: Socket)
: this(time, stream, services, ip6(socket.inetAddress), socket.port)
constructor(time: Long, stream: Long, services: Long = 1, inetAddress: InetAddress, port: Int)
: this(time, stream, services, ip6(inetAddress), port)
fun provides(service: Version.Service?): Boolean = service?.isEnabled(services) ?: false
fun toInetAddress(): InetAddress {

View File

@ -21,7 +21,7 @@ import ch.dissem.bitmessage.entity.NetworkMessage
import ch.dissem.bitmessage.entity.ObjectMessage
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.payload.*
import ch.dissem.bitmessage.entity.payload.ObjectType.MSG
import ch.dissem.bitmessage.entity.payload.ObjectType.*
import ch.dissem.bitmessage.entity.valueobject.PrivateKey
import ch.dissem.bitmessage.exception.NodeException
import ch.dissem.bitmessage.utils.Singleton.cryptography
@ -56,7 +56,7 @@ object Factory {
}
@JvmStatic fun getObjectMessage(version: Int, stream: InputStream, length: Int): ObjectMessage? {
@JvmStatic fun getObjectMessage(@Suppress("UNUSED_PARAMETER") version: Int, stream: InputStream, length: Int): ObjectMessage? {
try {
return V3MessageFactory.readObject(stream, length)
} catch (e: IOException) {
@ -141,11 +141,10 @@ object Factory {
val type = ObjectType.fromNumber(objectType)
if (type != null) {
when (type) {
ObjectType.GET_PUBKEY -> return parseGetPubkey(version, streamNumber, stream, length)
ObjectType.PUBKEY -> return parsePubkey(version, streamNumber, stream, length)
GET_PUBKEY -> return parseGetPubkey(version, streamNumber, stream, length)
PUBKEY -> return parsePubkey(version, streamNumber, stream, length)
MSG -> return parseMsg(version, streamNumber, stream, length)
ObjectType.BROADCAST -> return parseBroadcast(version, streamNumber, stream, length)
else -> LOG.error("This should not happen, someone broke something in the code!")
BROADCAST -> return parseBroadcast(version, streamNumber, stream, length)
}
}
// fallback: just store the message - we don't really care what it is
@ -172,7 +171,7 @@ object Factory {
return pubkey ?: GenericPayload.read(version, streamNumber, stream, length)
}
@JvmStatic private fun parseMsg(version: Long, streamNumber: Long, stream: InputStream, length: Int): ObjectPayload {
@JvmStatic private fun parseMsg(@Suppress("UNUSED_PARAMETER") version: Long, streamNumber: Long, stream: InputStream, length: Int): ObjectPayload {
return Msg.read(stream, streamNumber, length)
}

View File

@ -99,7 +99,7 @@ object V3MessageFactory {
} catch (e: Exception) {
if (LOG.isTraceEnabled) {
LOG.trace("Could not parse object payload - using generic payload instead", e)
LOG.trace(Strings.hex(data).toString())
LOG.trace(Strings.hex(data))
}
payload = GenericPayload(version, stream, data)
}

View File

@ -17,6 +17,7 @@
package ch.dissem.bitmessage.ports
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress
import ch.dissem.bitmessage.utils.UnixTime
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.InetAddress
@ -46,9 +47,12 @@ object NodeRegistryHelper {
val portIndex = line.lastIndexOf(':')
val inetAddresses = InetAddress.getAllByName(line.substring(0, portIndex))
val port = Integer.valueOf(line.substring(portIndex + 1))!!
for (inetAddress in inetAddresses) {
streamSet.add(NetworkAddress.Builder().ip(inetAddress).port(port).stream(stream).build())
}
inetAddresses.mapTo(streamSet) { NetworkAddress(
time = UnixTime.now,
stream = stream,
inetAddress = it,
port = port
) }
}
} catch (e: IOException) {
LOG.warn(e.message, e)

View File

@ -37,7 +37,7 @@ object DebugUtils {
}
@JvmStatic fun <K> inc(map: MutableMap<K, Int>, key: K) {
val value = map.get(key);
val value = map[key]
if (value == null) {
map.put(key, 1)
} else {

View File

@ -41,13 +41,14 @@ class ProofOfWorkEngineTest : TestBase() {
val waiter1 = CallbackWaiter<ByteArray>()
engine.calculateNonce(initialHash, target,
object : ProofOfWorkEngine.Callback {
@Suppress("NAME_SHADOWING")
override fun onNonceCalculated(initialHash: ByteArray, nonce: ByteArray) {
waiter1.setValue(nonce)
}
})
val nonce = waiter1.waitForValue()!!
println("Calculating nonce took " + waiter1.time + "ms")
assertTrue(Bytes.lt(cryptography().doubleSha512(nonce, initialHash), target, 8))
val nonce1 = waiter1.waitForValue()!!
println("Calculating nonce1 took ${waiter1.time}ms")
assertTrue(Bytes.lt(cryptography().doubleSha512(nonce1, initialHash), target, 8))
// Let's add a second (shorter) run to find possible multi threading issues
val initialHash2 = cryptography().sha512(byteArrayOf(1, 3, 6, 5))
@ -56,13 +57,14 @@ class ProofOfWorkEngineTest : TestBase() {
val waiter2 = CallbackWaiter<ByteArray>()
engine.calculateNonce(initialHash2, target2,
object : ProofOfWorkEngine.Callback {
@Suppress("NAME_SHADOWING")
override fun onNonceCalculated(initialHash: ByteArray, nonce: ByteArray) {
waiter2.setValue(nonce)
}
})
val nonce2 = waiter2.waitForValue()!!
println("Calculating nonce took " + waiter2.time + "ms")
println("Calculating nonce1 took ${waiter2.time}ms")
assertTrue(Bytes.lt(cryptography().doubleSha512(nonce2, initialHash2), target2, 8))
assertTrue("Second nonce must be quicker to find", waiter1.time > waiter2.time)
assertTrue("Second nonce1 must be quicker to find", waiter1.time > waiter2.time)
}
}

Some files were not shown because too many files have changed in this diff Show More