nitesh solanki
nitesh solanki

Reputation: 177

kotlin lambda passed as argument to corda (v3.2) flow throws kryo serialization exception

I have a Corda flow written in kotlin which takes lambda as an argument:

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

If I try to invoke the flow over RPC as shown below, Kryo throws an exception:

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

computeValue function is defined as below:

@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

  }
}

Exception:

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

Upvotes: 0

Views: 112

Answers (1)

Mike Hearn
Mike Hearn

Reputation: 1473

Passing a function as a parameter to a flow is probably a bit ambitious. The node needs to evaluate it, after all, and checkpoint it so it can be evaluated later. If you really want to do this you can like so:

val lambda = @CordaSerializable Runnable { whatever() }

or

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

.... but I'd suggest keeping it simple and just passing into plain old data objects. Yes, object serialization is powerful. But with great power comes great responsibility!

Upvotes: 1

Related Questions