voddan
voddan

Reputation: 33779

TransactionBuilder becomes immutable after sending

I need several nodes to build one transaction. To achieve that I create a TransactionBuilder and pass it to nodes that then add their states to it.

This seems to be a legit practice since the official documentation mentions TransactionBuilder is intended to be passed around contracts that may edit it by adding new states/commands.

I've added TransactionBuilder to the serialization white-list so that Corda can send/receive it.

However when running a unit test the network throws this exception:

java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at net.corda.core.transactions.TransactionBuilder.addInputState(TransactionBuilder.kt:149)

I looked into it in debugger, and the type of the builder after it has been received is UnmodifiableList.

My workaroand is to copy the builder after receiving it.

But why it works like that? Did I interpreted the documentation wrong?

We use Corda 3.3

Upvotes: 1

Views: 146

Answers (1)

Joel
Joel

Reputation: 23140

Corda serialises objects at various points (e.g. when they are sent or received between nodes within flows, when they are sent via RPC) using its own serialisation framework.

In Java, if you receive a serialised object where one of the fields is a List, it is not possible to check whether the list in question was originally mutable or immutable. In Corda, we therefore default to deserialising such lists as immutable lists.

You should make a copy of the original TransactionBuilder, as you are already doing.

Upvotes: 1

Related Questions