Kotlin Lambda作为参数传递给Corda(v3.2)流时引发Kryo序列化异常

时间:2019-02-19 11:52:00

标签: corda

我有一个用kotlin编写的Corda流,它以lambda作为参数:

class ApproveFlow(val arg1: String, val arg2: (Amount<Currency>) -> Amount<Currency>) : FlowLogic<SignedTransaction>()

如果我尝试如下所示通过RPC调用流,Kryo会引发异常:

nodeBProxy.startFlow(::ApproveFlow, "abc", (BasicCalculator()::computeValue))

computeValue函数的定义如下:

@CordaSerializable
interface Calculator {
    fun computeValue(purchasePrice: Amount<Currency>): Amount<Currency>
}

@CordaSerializable
class BasicCalculator  : Calculator {

    override fun computeValue(purchasePrice: Amount<Currency>): Amount<Currency> 
  { ...
    ...
    return 100.POUNDS

  }
}

例外:

is not annotated or on the whitelist, so cannot be used in serialization corda

1 个答案:

答案 0 :(得分:1)

将函数作为参数传递给流可能有点野心。毕竟,该节点需要对其进行评估,并对其进行检查,以便稍后进行评估。如果您真的想这样做,可以这样:

val lambda = @CordaSerializable Runnable { whatever() }

val lambda = @CordaSerializable fun() { whatever() }

.... 但是我建议保持简单,只将其传递给普通的旧数据对象。是的,对象序列化功能强大。但是,强大的力量伴随着巨大的责任!