Refactor to utilize Kotlin features

This commit is contained in:
2018-02-16 17:04:08 +01:00
parent b93f382ccd
commit fab1c06135
7 changed files with 92 additions and 53 deletions

View File

@ -37,33 +37,33 @@ import java.util.*
data class Message constructor(
val subject: String,
val body: String,
val parents: List<InventoryVector>,
val files: List<Attachment>
val parents: List<InventoryVector> = emptyList(),
val files: List<Attachment> = emptyList()
) : ExtendedEncoding.ExtendedType {
override val type: String = TYPE
override fun pack(): MPMap<MPString, MPType<*>> {
val result = MPMap<MPString, MPType<*>>()
result.put("".mp, TYPE.mp)
result.put("subject".mp, subject.mp)
result.put("body".mp, body.mp)
result["".mp] = TYPE.mp
result["subject".mp] = subject.mp
result["body".mp] = body.mp
if (!files.isEmpty()) {
val items = MPArray<MPMap<MPString, MPType<*>>>()
result.put("files".mp, items)
result["files".mp] = items
for (file in files) {
val item = MPMap<MPString, MPType<*>>()
item.put("name".mp, file.name.mp)
item.put("data".mp, file.data.mp)
item.put("type".mp, file.type.mp)
item.put("disposition".mp, file.disposition.name.mp)
item["name".mp] = file.name.mp
item["data".mp] = file.data.mp
item["type".mp] = file.type.mp
item["disposition".mp] = file.disposition.name.mp
items.add(item)
}
}
if (!parents.isEmpty()) {
val items = MPArray<MPBinary>()
result.put("parents".mp, items)
result["parents".mp] = items
for ((hash) in parents) {
items.add(mp(*hash))
}
@ -179,6 +179,6 @@ data class Message constructor(
companion object {
private val LOG = LoggerFactory.getLogger(Message::class.java)
val TYPE = "message"
const val TYPE = "message"
}
}

View File

@ -156,6 +156,16 @@ interface Cryptography {
fun doProofOfWork(objectMessage: ObjectMessage, nonceTrialsPerByte: Long,
extraBytes: Long, callback: ProofOfWorkEngine.Callback)
@JvmSynthetic
fun doProofOfWork(objectMessage: ObjectMessage, nonceTrialsPerByte: Long,
extraBytes: Long, callback: (ByteArray, ByteArray) -> Unit) {
doProofOfWork(objectMessage, nonceTrialsPerByte, extraBytes, object : ProofOfWorkEngine.Callback {
override fun onNonceCalculated(initialHash: ByteArray, nonce: ByteArray) {
callback.invoke(initialHash, nonce)
}
})
}
/**
* @param objectMessage to be checked
* *

View File

@ -23,16 +23,32 @@ interface ProofOfWorkEngine {
/**
* Returns a nonce, such that the first 8 bytes from sha512(sha512(nonce||initialHash)) represent a unsigned long
* smaller than target.
*
* @param initialHash the SHA-512 hash of the object to send, sans nonce
* *
* @param target the target, representing an unsigned long
* *
* @param callback called with the calculated nonce as argument. The ProofOfWorkEngine implementation must make
* * sure this is only called once.
* @param callback called with the initial hash and the calculated nonce as argument. The ProofOfWorkEngine
* implementation must make sure this is only called once.
*/
fun calculateNonce(initialHash: ByteArray, target: ByteArray, callback: Callback)
/**
* Returns a nonce, such that the first 8 bytes from sha512(sha512(nonce||initialHash)) represent a unsigned long
* smaller than target.
*
* @param initialHash the SHA-512 hash of the object to send, sans nonce
* @param target the target, representing an unsigned long
* @param callback called with the initial hash and the calculated nonce as argument. The ProofOfWorkEngine
* implementation must make sure this is only called once.
*/
@JvmSynthetic
fun calculateNonce(initialHash: ByteArray, target: ByteArray, callback: (ByteArray, ByteArray) -> Unit) {
calculateNonce(initialHash, target, object : Callback {
override fun onNonceCalculated(initialHash: ByteArray, nonce: ByteArray) {
callback.invoke(initialHash, nonce)
}
})
}
interface Callback {
/**
* @param nonce 8 bytes nonce