mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Validate protocol type and args via whitelist ahead of class loading. Also, push this work into another method on the Factory.
This commit is contained in:
@ -49,6 +49,22 @@ class ProtocolLogicRefFactory(private val protocolWhitelist: Map<String, Set<Str
|
|||||||
require(protocolWhitelist[className]!!.contains(argClassName)) { "Args to ${className} must have types on the args whitelist: $argClassName" }
|
require(protocolWhitelist[className]!!.contains(argClassName)) { "Args to ${className} must have types on the args whitelist: $argClassName" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a [ProtocolLogicRef] for the Kotlin primary constructor of a named [ProtocolLogic]
|
||||||
|
*/
|
||||||
|
fun createKotlin(protocolLogicClassName: String, args: Map<String,Any?>, attachments: List<SecureHash> = emptyList()): ProtocolLogicRef {
|
||||||
|
val context = AppContext(attachments)
|
||||||
|
validateProtocolClassName(protocolLogicClassName, context)
|
||||||
|
for(arg in args.values.filterNotNull()) {
|
||||||
|
validateArgClassName(protocolLogicClassName, arg.javaClass.name, context)
|
||||||
|
}
|
||||||
|
val clazz = Class.forName(protocolLogicClassName)
|
||||||
|
require(ProtocolLogic::class.java.isAssignableFrom(clazz)) { "$protocolLogicClassName is not a ProtocolLogic" }
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
|
||||||
|
return createKotlin(logic, args)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a [ProtocolLogicRef] for the Kotlin primary constructor or Java constructor and the given args.
|
* Create a [ProtocolLogicRef] for the Kotlin primary constructor or Java constructor and the given args.
|
||||||
*/
|
*/
|
||||||
|
@ -5,7 +5,6 @@ import com.r3corda.core.contracts.*
|
|||||||
import com.r3corda.core.crypto.DigitalSignature
|
import com.r3corda.core.crypto.DigitalSignature
|
||||||
import com.r3corda.core.crypto.SecureHash
|
import com.r3corda.core.crypto.SecureHash
|
||||||
import com.r3corda.core.node.services.linearHeadsOfType
|
import com.r3corda.core.node.services.linearHeadsOfType
|
||||||
import com.r3corda.core.protocols.ProtocolLogic
|
|
||||||
import com.r3corda.core.serialization.SerializedBytes
|
import com.r3corda.core.serialization.SerializedBytes
|
||||||
import com.r3corda.node.api.*
|
import com.r3corda.node.api.*
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@ -67,15 +66,9 @@ class APIServerImpl(val node: AbstractNode) : APIServer {
|
|||||||
|
|
||||||
private fun invokeProtocolAsync(type: ProtocolRef, args: Map<String, Any?>): ListenableFuture<out Any?> {
|
private fun invokeProtocolAsync(type: ProtocolRef, args: Map<String, Any?>): ListenableFuture<out Any?> {
|
||||||
if (type is ProtocolClassRef) {
|
if (type is ProtocolClassRef) {
|
||||||
val clazz = Class.forName(type.className)
|
val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(type.className, args)
|
||||||
if (ProtocolLogic::class.java.isAssignableFrom(clazz)) {
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
|
|
||||||
val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(logic, args)
|
|
||||||
val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef)
|
val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef)
|
||||||
return node.services.startProtocol(clazz.name, protocolInstance)
|
return node.services.startProtocol(type.className, protocolInstance)
|
||||||
}
|
|
||||||
throw UnsupportedOperationException("Could not find matching protocol and constructor for: $type $args")
|
|
||||||
} else {
|
} else {
|
||||||
throw UnsupportedOperationException("Unsupported ProtocolRef type: $type")
|
throw UnsupportedOperationException("Unsupported ProtocolRef type: $type")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user