在 Corda 中,我定义了以下流程:
object Flow {
@InitiatingFlow
@StartableByRPC
class Initiator(val otherParty: Party) : FlowLogic<Unit>() {
override val progressTracker = ProgressTracker()
@Suspendable
override fun call() {
val otherPartyFlow = initiateFlow(otherParty)
otherPartyFlow.send(MyClass())
}
}
@InitiatedBy(Initiator::class)
class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val unregisteredClassInstance = otherPartyFlow.receive<MyClass>()
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行流程时,出现以下错误:
com.example.flow.MyClass 类没有注释或者在白名单中,所以不能用于序列化
如何注释或将类列入白名单以允许它在流中发送?为什么我需要这样做?
默认情况下,出于安全目的,只有默认序列化白名单中存在的类才能在流中或通过 RPC 发送。
有两种方法可以将特定类添加到序列化白名单中:
1. 将类注释为@CordaSerializable:
@CordaSerializable
class MyClass
Run Code Online (Sandbox Code Playgroud)
2.创建序列化白名单插件:
定义一个序列化插件如下:
class TemplateSerializationWhitelist : SerializationWhitelist {
override val whitelist: List<Class<*>> = listOf(MyClass::class.java)
}
Run Code Online (Sandbox Code Playgroud)
然后列出序列化白名单插件的完全限定类名(例如com.example.TemplateSerializationWhitelist,net.corda.core.serialization.SerializationWhitelist在src/main/resources/META-INF/servicesCorDapp.xml 文件夹中调用的文件中)。
为什么有两种方法可以将类添加到序列化白名单中?
第一种方法更简单,但当您无法将注释添加到您希望发送的类时,就不可能了。