Search code examples
javacordabootcamp

How to resolve - NotSerializableException in bootcamp-cordapp. When testing Contract


I am trying to run corda-bootcamp test case for Contract. I am following as given in the video, but when trying to run the contract test - I get the error

java.io.NotSerializableException: data(net.corda.core.contracts.ContractState) -> Trying to build an object serializer for bootcamp.TokenState, but it is not constructible from its public properties, and so requires a custom serialiser.

Can someone help, how to resolve this ? I found a similar issue - here , but that did not work.

Below is the Tokenstate used,

package bootcamp;

import com.google.common.collect.ImmutableList;
import net.corda.core.contracts.BelongsToContract;
import net.corda.core.contracts.ContractState;
import net.corda.core.identity.AbstractParty;
import net.corda.core.identity.Party;

import java.util.ArrayList;
import java.util.List;

/* Our state, defining a shared fact on the ledger.
 * See src/main/java/examples/ArtState.java for an example. */
@BelongsToContract(TokenContract.class)
public class TokenState implements ContractState {

    private Party issuer;
    private Party owner;
    private int amount;

    public TokenState(Party issuer, Party owner, int amount) {
        this.issuer = issuer;
        this.owner = owner;
        this.amount = amount;
    }

    public Party getIssuer() {
        return issuer;
    }

    public Party getOwner() {
        return owner;
    }

    public int getAmount() {
        return amount;
    }

    public List<AbstractParty> getParticipants() {
        List<AbstractParty> participants = new ArrayList<>();
        participants.add(issuer);
        participants.add(owner);
        return participants;
    }
}

Test case that throws the error,

private final TestIdentity alice = new TestIdentity(new CordaX500Name("Alice", "", "GB"));
    private final TestIdentity bob = new TestIdentity(new CordaX500Name("Bob", "", "GB"));
    private MockServices ledgerServices = new MockServices(new TestIdentity(new CordaX500Name("TestId", "", "GB")));

    private TokenState tokenState = new TokenState(alice.getParty(), bob.getParty(), 1);

@Test
public void tokenContractRequiresZeroInputsInTheTransaction() {
        transaction(ledgerServices, tx -> {
            // Has an input, will fail.
            tx.input(TokenContract.ID, tokenState);
            tx.output(TokenContract.ID, tokenState);
            tx.command(Arrays.asList(alice.getPublicKey(), bob.getPublicKey()), new TokenContract.Commands.Issue());
            tx.fails();
            return null;
        });

        transaction(ledgerServices, tx -> {
            // Has no input, will verify.
            tx.output(TokenContract.ID, tokenState);
            tx.command(Arrays.asList(alice.getPublicKey(), bob.getPublicKey()), new TokenContract.Commands.Issue());
            tx.verifies();
            return null;
        });
    }

Solution

  • Corda uses its own serialisation framework which requires the Java compiler to preserve the argument names when generating bytecode, so that objects can be correctly recreated later.

    For this to work with IntelliJ do the following:

    1. Open the settings:

      • Windows: File -> Settings
      • osX/ Ubuntu: IntelliJ IDEA -> Preferences
    2. Go to Build, Execution, Deployment -> Compiler -> Java Compiler Write -parameters in the Additional command line parameters field

    enter image description here

    1. Completely rebuild the project (Build -> Rebuild Project)

    enter image description here