gpt4 book ai didi

java - 如何解决 bootcamp-cordapp 中的 NotSerializedException。测试合约时

转载 作者:太空宇宙 更新时间:2023-11-04 09:23:29 25 4
gpt4 key购买 nike

我正在尝试运行 Contract 的 corda-bootcamp 测试用例。我按照视频中给出的方式进行操作,但是当尝试运行契约(Contract)测试时 - 我收到错误

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.

有人可以帮忙吗,如何解决这个问题?我发现了类似的问题 - here ,但这没有用。

下面是使用的Tokenstate,

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;
}
}

抛出错误的测试用例,

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;
});
}

最佳答案

Corda 使用自己的序列化框架,该框架要求 Java 编译器在生成字节码时保留参数名称,以便稍后可以正确地重新创建对象。

要使其与 IntelliJ 配合使用,请执行以下操作:

  1. 打开设置:

    • Windows:文件 -> 设置
    • osX/Ubuntu:IntelliJ IDEA -> 首选项
  2. 转到构建、执行、部署 -> 编译器 -> Java 编译器在“附加命令行参数”字段中写入 -parameters

enter image description here

  • 完全重建项目(构建 -> 重建项目)
  • enter image description here

    关于java - 如何解决 bootcamp-cordapp 中的 NotSerializedException。测试合约时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033294/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com