Minor: eliminate compiler warnings

This commit is contained in:
Mike Hearn 2016-04-07 19:02:14 +02:00
parent 1da7b4bf01
commit f3863ac5ef
13 changed files with 58 additions and 57 deletions

View File

@ -453,7 +453,6 @@ class InterestRateSwap() : Contract {
val calculation: Calculation,
val common: Common
) : FixableDealState {
override val programRef = IRS_PROGRAM_ID
override val thread = SecureHash.sha256(common.tradeID)
override val ref = common.tradeID
@ -465,7 +464,8 @@ class InterestRateSwap() : Contract {
override val parties: Array<Party>
get() = arrayOf(fixedLeg.fixedRatePayer, floatingLeg.floatingRatePayer)
override fun withPublicKey(before: Party, after: PublicKey): State {
// TODO: This changing of the public key violates the assumption that Party is a fixed identity key.
override fun withPublicKey(before: Party, after: PublicKey): DealState {
val newParty = Party(before.name, after)
if (before == fixedLeg.fixedRatePayer) {
val deal = copy()

View File

@ -45,8 +45,8 @@ open class PercentageRatioUnit(percentageAsString: String) : RatioUnit(BigDecima
val String.percent: PercentageRatioUnit get() = PercentageRatioUnit(this)
/**
* Interface representing an agreement that exposes various attributes that are common and allow
* implementation of general protocols that manipulate many agreement types
* Interface representing an agreement that exposes various attributes that are common. Implementing it simplifies
* implementation of general protocols that manipulate many agreement types.
*/
interface DealState : LinearState {
@ -56,7 +56,7 @@ interface DealState : LinearState {
/** Exposes the Parties involved in a generic way */
val parties: Array<Party>
/** Allow swapping in of potentially transaction specific public keys prior to signing */
// TODO: This works by editing the keys used by a Party which is invalid.
fun withPublicKey(before: Party, after: PublicKey): DealState
/**

View File

@ -18,7 +18,6 @@ import java.io.OutputStream
import java.security.PublicKey
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.util.jar.JarInputStream
/** Implemented by anything that can be named by a secure hash value (e.g. transactions, attachments). */
@ -75,6 +74,11 @@ data class StateRef(val txhash: SecureHash, val index: Int) {
/** A StateAndRef is simply a (state, ref) pair. For instance, a wallet (which holds available assets) contains these. */
data class StateAndRef<out T : ContractState>(val state: T, val ref: StateRef)
/** Filters a list of [StateAndRef] objects according to the type of the states */
inline fun <reified T : ContractState> List<StateAndRef<ContractState>>.filterStatesOfType(): List<StateAndRef<T>> {
return mapNotNull { if (it.state is T) StateAndRef(it.state, it.ref) else null }
}
/** A [Party] is well known (name, pubkey) pair. In a real system this would probably be an X.509 certificate. */
data class Party(val name: String, val owningKey: PublicKey) {
override fun toString() = name

View File

@ -2,10 +2,14 @@ package api
import com.google.common.util.concurrent.ListenableFuture
import contracts.DealState
import core.*
import core.ContractState
import core.SignedTransaction
import core.StateRef
import core.WireTransaction
import core.crypto.DigitalSignature
import core.crypto.SecureHash
import core.node.AbstractNode
import core.node.services.linearHeadsOfType
import core.protocols.ProtocolLogic
import core.serialization.SerializedBytes
import core.utilities.ANSIProgressRenderer
@ -28,8 +32,8 @@ class APIServerImpl(val node: AbstractNode): APIServer {
return states.values.map { it.ref }
}
else if (query.criteria is StatesQuery.Criteria.Deal) {
val states = node.services.walletService.linearHeadsInstanceOf(DealState::class.java) {
it.ref == query.criteria.ref
val states = node.services.walletService.linearHeadsOfType<DealState>().filterValues {
it.state.ref == query.criteria.ref
}
return states.values.map { it.ref }
}

View File

@ -48,7 +48,7 @@ class NodeWalletService(private val services: ServiceHub) : WalletService {
*/
override val linearHeads: Map<SecureHash, StateAndRef<LinearState>>
get() = mutex.locked { wallet }.let { wallet ->
wallet.states.filter { it.state is LinearState }.associateBy { (it.state as LinearState).thread }.mapValues { it.value as StateAndRef<LinearState> }
wallet.states.filterStatesOfType<LinearState>().associateBy { it.state.thread }.mapValues { it.value }
}
override fun notifyAll(txns: Iterable<WireTransaction>): Wallet {

View File

@ -72,8 +72,12 @@ interface WalletService {
*/
val linearHeads: Map<SecureHash, StateAndRef<LinearState>>
fun <T : LinearState> linearHeadsInstanceOf(clazz: Class<T>, predicate: (T) -> Boolean = { true } ): Map<SecureHash, StateAndRef<T>> {
return linearHeads.filterValues { clazz.isInstance(it.state) }.filterValues { predicate(it.state as T) }.mapValues { StateAndRef(it.value.state as T, it.value.ref) }
// TODO: When KT-10399 is fixed, rename this and remove the inline version below.
/** Returns the [linearHeads] only when the type of the state would be considered an 'instanceof' the given type. */
@Suppress("UNCHECKED_CAST")
fun <T : LinearState> linearHeadsOfType_(stateType: Class<T>): Map<SecureHash, StateAndRef<T>> {
return linearHeads.filterValues { stateType.isInstance(it.state) }.mapValues { StateAndRef(it.value.state as T, it.value.ref) }
}
fun statesForRefs(refs: List<StateRef>): Map<StateRef, ContractState?> {
@ -95,17 +99,7 @@ interface WalletService {
fun notify(tx: WireTransaction): Wallet = notifyAll(listOf(tx))
}
// TODO: Document this
@Suppress("UNCHECKED_CAST")
inline fun <reified T : LinearState> WalletService.linearHeadsOfType(): Map<SecureHash, StateAndRef<T>> {
return linearHeads.mapNotNull {
val s = it.value.state
if (s is T)
Pair(it.key, it.value as StateAndRef<T>)
else
null
}.toMap()
}
inline fun <reified T : LinearState> WalletService.linearHeadsOfType() = linearHeadsOfType_(T::class.java)
/**
* The KMS is responsible for storing and using private keys to sign things. An implementation of this may, for example,

View File

@ -16,12 +16,13 @@ open class StorageServiceImpl(attachments: AttachmentStorage,
// This parameter is for unit tests that want to observe operation details.
val recordingAs: (String) -> String = { tableName -> "" })
: StorageService {
protected val tables = HashMap<String, MutableMap<Any, Any>>()
protected val tables = HashMap<String, MutableMap<*, *>>()
private fun <K, V> getMapOriginal(tableName: String): MutableMap<K, V> {
synchronized(tables) {
@Suppress("UNCHECKED_CAST")
return tables.getOrPut(tableName) {
recorderWrap(Collections.synchronizedMap(HashMap<Any, Any>()), tableName);
recorderWrap(Collections.synchronizedMap(HashMap<K, V>()), tableName)
} as MutableMap<K, V>
}
}

View File

@ -89,15 +89,15 @@ class InMemoryMessagingNetwork {
if (calc != null && recipients is SingleMessageRecipient) {
// Inject some artificial latency.
timer.schedule(calc.between(from.myAddress, recipients).toMillis()) {
msgSendInternal(from, message, recipients)
msgSendInternal(message, recipients)
}
} else {
msgSendInternal(from, message, recipients)
msgSendInternal(message, recipients)
}
_allMessages.onNext(Triple(from.myAddress, message, recipients))
}
private fun msgSendInternal(from: InMemoryMessaging, message: Message, recipients: MessageRecipients) {
private fun msgSendInternal(message: Message, recipients: MessageRecipients) {
when (recipients) {
is Handle -> getQueueForHandle(recipients).add(message)

View File

@ -16,14 +16,14 @@ import core.node.Node
import core.node.NodeConfiguration
import core.node.NodeConfigurationFromConfig
import core.node.services.ArtemisMessagingService
import core.node.services.NodeInfo
import core.node.services.MockNetworkMapCache
import core.node.services.NodeInfo
import core.serialization.deserialize
import core.utilities.BriefLogFormatter
import joptsimple.OptionParser
import demos.protocols.AutoOfferProtocol
import demos.protocols.ExitServerProtocol
import demos.protocols.UpdateBusinessDayProtocol
import joptsimple.OptionParser
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
@ -122,8 +122,10 @@ fun main(args: Array<String>) {
UpdateBusinessDayProtocol.Handler.register(node)
ExitServerProtocol.Handler.register(node)
while(true) {
Thread.sleep(1000L)
try {
while (true) Thread.sleep(Long.MAX_VALUE)
} catch(e: InterruptedException) {
node.stop()
}
exitProcess(0)
}

View File

@ -3,8 +3,8 @@ package demos.protocols
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.strands.Strand
import core.node.Node
import core.node.services.NodeInfo
import core.node.services.MockNetworkMapCache
import core.node.services.NodeInfo
import core.protocols.ProtocolLogic
import core.serialization.deserialize
import java.util.concurrent.TimeUnit
@ -37,7 +37,7 @@ object ExitServerProtocol {
* This takes a Java Integer rather than Kotlin Int as that is what we end up with in the calling map and currently
* we do not support coercing numeric types in the reflective search for matching constructors
*/
class Broadcast(val exitCode: Integer) : ProtocolLogic<Boolean>() {
class Broadcast(@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") val exitCode: Integer) : ProtocolLogic<Boolean>() {
@Suspendable
override fun call(): Boolean {

View File

@ -6,8 +6,9 @@ import contracts.DealState
import contracts.InterestRateSwap
import core.StateAndRef
import core.node.Node
import core.node.services.NodeInfo
import core.node.services.MockNetworkMapCache
import core.node.services.NodeInfo
import core.node.services.linearHeadsOfType
import core.protocols.ProtocolLogic
import core.random63BitValue
import core.serialization.deserialize
@ -41,7 +42,7 @@ object UpdateBusinessDayProtocol {
override fun call(): Boolean {
// Get deals
progressTracker.currentStep = FETCHING
val dealStateRefs = serviceHub.walletService.linearHeadsInstanceOf(DealState::class.java)
val dealStateRefs = serviceHub.walletService.linearHeadsOfType<DealState>()
val otherPartyToDeals = dealStateRefs.values.groupBy { otherParty(it.state) }
// TODO we need to process these in parallel to stop there being an ordering problem across more than two nodes
@ -65,8 +66,9 @@ object UpdateBusinessDayProtocol {
// TODO we should make this more object oriented when we can ask a state for it's contract
@Suspendable
fun processDeal(party: NodeInfo, deal: StateAndRef<DealState>, date: LocalDate, sessionID: Long) {
when(deal.state) {
is InterestRateSwap.State -> processInterestRateSwap(party, StateAndRef(deal.state as InterestRateSwap.State, deal.ref), date, sessionID)
val s = deal.state
when (s) {
is InterestRateSwap.State -> processInterestRateSwap(party, StateAndRef(s, deal.ref), date, sessionID)
}
}

View File

@ -239,7 +239,7 @@ object TwoPartyDealProtocol {
private fun receiveAndValidateHandshake(): Handshake<U> {
progressTracker.currentStep = RECEIVING
// Wait for a trade request to come in on our pre-provided session ID.
val handshake = receive(DEAL_TOPIC, sessionID, Handshake::class.java)
val handshake = receive<Handshake<U>>(DEAL_TOPIC, sessionID)
progressTracker.currentStep = VERIFYING
handshake.validate {
@ -267,7 +267,7 @@ object TwoPartyDealProtocol {
return ptx.toSignedTransaction(checkSufficientSignatures = false)
}
@Suspendable protected abstract fun validateHandshake(handshake: Handshake<*>): Handshake<U>
@Suspendable protected abstract fun validateHandshake(handshake: Handshake<U>): Handshake<U>
@Suspendable protected abstract fun assembleSharedTX(handshake: Handshake<U>): Pair<TransactionBuilder, List<PublicKey>>
}
@ -291,10 +291,10 @@ object TwoPartyDealProtocol {
override val progressTracker: ProgressTracker = Secondary.tracker()) : Secondary<T>(otherSide, timestampingAuthority, sessionID) {
@Suspendable
override fun validateHandshake(handshake: Handshake<*>): Handshake<T> {
with(handshake as Handshake<T>) {
override fun validateHandshake(handshake: Handshake<T>): Handshake<T> {
with(handshake) {
// What is the seller trying to sell us?
val deal = handshake.payload
val deal: T = handshake.payload
val otherKey = handshake.publicKey
logger.trace { "Got deal request for: ${handshake.payload}" }
@ -308,7 +308,10 @@ object TwoPartyDealProtocol {
val myOldParty = deal.parties.single { it.name == myName }
val theirOldParty = deal.parties.single { it.name != myName }
val newDeal = deal.withPublicKey(myOldParty, serviceHub.keyManagementService.freshKey().public).withPublicKey(theirOldParty, otherKey) as T
@Suppress("UNCHECKED_CAST")
val newDeal = deal.
withPublicKey(myOldParty, serviceHub.keyManagementService.freshKey().public).
withPublicKey(theirOldParty, otherKey) as T
return handshake.copy(payload = newDeal)
}
@ -341,8 +344,8 @@ object TwoPartyDealProtocol {
override val progressTracker: ProgressTracker = Secondary.tracker()) : Secondary<StateRef>(otherSide, timestampingAuthority, sessionID) {
@Suspendable
override fun validateHandshake(handshake: Handshake<*>): Handshake<StateRef> {
with(handshake as Handshake<StateRef>) {
override fun validateHandshake(handshake: Handshake<StateRef>): Handshake<StateRef> {
with(handshake) {
logger.trace { "Got fixing request for: ${dealToFix.state}" }
// Check the start message for acceptability.
@ -363,11 +366,12 @@ object TwoPartyDealProtocol {
// TODO Do we need/want to substitute in new public keys for the Parties?
val myName = serviceHub.storageService.myLegalIdentity.name
val deal = dealToFix.state
val deal: T = dealToFix.state
val myOldParty = deal.parties.single { it.name == myName }
val theirOldParty = deal.parties.single { it.name != myName }
val myNewKey = serviceHub.keyManagementService.freshKey().public
@Suppress("UNCHECKED_CAST")
val newDeal = deal.withPublicKey(myOldParty, myNewKey).withPublicKey(theirOldParty, handshake.publicKey) as T
val oldRef = dealToFix.ref

View File

@ -326,19 +326,9 @@ class IRSTests {
assert(100 * r1 == 5)
}
@Test
fun `more rate tests`() {
val r1 = FixedRate(PercentageRatioUnit("10"))
val r2 = FixedRate(PercentageRatioUnit("10"))
// TODO: r1+r2 ? Do we want to allow these.
// TODO: r1*r2 ?
}
@Test
fun `expression calculation testing`() {
val dummyIRS = singleIRS()
val v = FixedRate(PercentageRatioUnit("4.5"))
val stuffToPrint: ArrayList<String> = arrayListOf(
"fixedLeg.notional.pennies",
"fixedLeg.fixedRate.ratioUnit",