英文:
java.lang.ClassNotFoundException: net.corda.testing.core.DummyCommandData
问题
以下是您提供的内容的翻译部分:
MetalContract
package com.template.contracts;
import com.template.states.MetalState;
import net.corda.core.contracts.Command;
import net.corda.core.contracts.CommandData;
import net.corda.core.contracts.Contract;
import net.corda.core.contracts.ContractState;
import net.corda.core.identity.Party;
import net.corda.core.transactions.LedgerTransaction;
import org.jetbrains.annotations.NotNull;
import java.security.PublicKey;
import java.util.List;
// ************
// * 合约 *
// ************
public class MetalContract implements Contract {
// 用于在构建交易时识别我们的合约。
public static final String CID = "com.template.contracts.MetalContract";
// 如果交易的输入和输出状态的合约的verify()函数都不抛出异常,那么交易是有效的。
@Override
public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentException {
// ...
}
// 用于指示交易的意图。
public static class Issue implements CommandData {}
public static class Transfer implements CommandData {}
}
ContractTests
package com.template.contracts;
import com.template.states.MetalState;
import com.template.contracts.MetalContract;
import net.corda.core.contracts.Contract;
import net.corda.core.identity.CordaX500Name;
import net.corda.testing.contracts.DummyState;
import net.corda.testing.core.DummyCommandData;
import net.corda.testing.core.TestIdentity;
import net.corda.testing.node.MockServices;
import org.junit.Test;
import static net.corda.testing.node.NodeTestUtils.transaction;
public class ContractTests {
// ...
@Test
public void metalContractImplementsContract() {
assert (new MetalContract() instanceof Contract);
}
@Test
public void MetalContractRequiresTheTransactionCommandToBeATransferCommand() {
transaction(ledgerServices, tx -> {
// ...
});
}
}
请注意,代码中的某些部分可能在翻译时出现格式错误或遗漏,因此请确保进行必要的校对。另外,错误信息和构建脚本的部分未提供翻译,以保持代码的完整性。如果您有任何疑问或需要进一步的帮助,请随时提问。
英文:
When trying to run test for CORDA the given Test case getting the following error. I am using JDK 1.8. Intellij IDEA.When trying to run test for CORDA the given Test case getting the following error. I am using JDK 1.8. Intellij IDEA.When trying to run test for CORDA the given Test case getting the following error. I am using JDK 1.8. Intellij IDEA.
MetalContract
package com.template.contracts;
import com.template.states.MetalState;
import net.corda.core.contracts.Command;
import net.corda.core.contracts.CommandData;
import net.corda.core.contracts.Contract;
import net.corda.core.contracts.ContractState;
import net.corda.core.identity.Party;
import net.corda.core.transactions.LedgerTransaction;
import org.jetbrains.annotations.NotNull;
import java.security.PublicKey;
import java.util.List;
// ************
// * Contract *
// ************
public class MetalContract implements Contract {
// This is used to identify our contract when building a transaction.
public static final String CID = "com.template.contracts.MetalContract";
// A transaction is valid if the verify() function of the contract of all the transaction's input and output states
// does not throw an exception.
@Override
public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentException{
if (tx.getCommands().size() != 1)
throw new IllegalArgumentException("Transaction must have one Command.");
Command command = tx.getCommand(0);
CommandData commandType = command.getValue();
List<PublicKey> requiredSigners = command.getSigners();
// -------------------------------- Issue Command Contract Rules ------------------------------------------
if (commandType instanceof Issue) {
// Issue transaction logic
// Shape Rules
if (tx.getInputs().size() != 0)
throw new IllegalArgumentException("Issue cannot have inputs");
if (tx.getOutputs().size() != 1)
throw new IllegalArgumentException("Issue can only have one output");
// Content Rules
ContractState outputState = tx.getOutput(0);
if (!(outputState instanceof MetalState))
throw new IllegalArgumentException("Output must be a metal State");
MetalState metalState = (MetalState) outputState;
if (!metalState.getMetalName().equals("Gold")&&!metalState.getMetalName().equals("Silver")){
throw new IllegalArgumentException("Metal is not Gold or Silver");
}
// Signer Rules
Party issuer = metalState.getIssuer();
PublicKey issuersKey = issuer.getOwningKey();
if (!(requiredSigners.contains(issuersKey)))
throw new IllegalArgumentException("Issuer has to sign the issuance");
}
// -------------------------------- Transfer Command Contract Rules ------------------------------------------
else if (commandType instanceof Transfer) {
// Transfer transaction logic
// Shape Rules
if (tx.getInputs().size() != 1)
throw new IllegalArgumentException("Transfer needs to have one input");
if (tx.getOutputs().size() != 1)
throw new IllegalArgumentException("Transfer can only have one output");
// Content Rules
ContractState inputState = tx.getInput(0);
ContractState outputState = tx.getOutput(0);
if (!(outputState instanceof MetalState))
throw new IllegalArgumentException("Output must be a metal State");
MetalState metalState = (MetalState) inputState;
if (!metalState.getMetalName().equals("Gold")&&!metalState.getMetalName().equals("Silver")){
throw new IllegalArgumentException("Metal is not Gold or Silver");
}
// Signer Rules
Party owner = metalState.getOwner();
PublicKey ownersKey = owner.getOwningKey();
if (!(requiredSigners.contains(ownersKey)))
throw new IllegalArgumentException("Owner has to sign the transfer");
}
else throw new IllegalArgumentException("Unrecognised command.");
}
// Used to indicate the transaction's intent.
public static class Issue implements CommandData {}
public static class Transfer implements CommandData {}
}
ContractTests
package com.template.contracts;
import com.template.states.MetalState;
import com.template.contracts.MetalContract;
import net.corda.core.contracts.Contract;
import net.corda.core.identity.CordaX500Name;
import net.corda.testing.contracts.DummyState;
import net.corda.testing.core.DummyCommandData;
import net.corda.testing.core.TestIdentity;
import net.corda.testing.node.MockServices;
import org.junit.Test;
import static net.corda.testing.node.NodeTestUtils.transaction;
public class ContractTests {
private final TestIdentity Mint = new TestIdentity (new CordaX500Name ("mint", "", "GB"));
private final TestIdentity TraderA = new TestIdentity (new CordaX500Name ("traderA", "", "GB"));
private final TestIdentity TraderB = new TestIdentity (new CordaX500Name ("traderB", "", "GB"));
private final MockServices ledgerServices = new MockServices();
private MetalState metalState = new MetalState("Gold", 10, Mint.getParty(), TraderA.getParty());
private MetalState metalStateInput = new MetalState("Gold", 10, Mint.getParty(), TraderA.getParty());
private MetalState metalStateOutput = new MetalState("Gold", 10, Mint.getParty(), TraderB.getParty());
@Test
public void metalContractImplementsContract() {
assert (new MetalContract() instanceof Contract);
}
@Test
public void MetalContractRequiresTheTransactionCommandToBeATransferCommand() {
transaction(ledgerServices, tx -> {
// Has wrong command, will fail
tx.input(MetalContract.CID, metalStateInput);
tx.output(MetalContract.CID, metalStateOutput);
tx.command(TraderA.getPublicKey(), DummyCommandData.INSTANCE);
tx.fails();
return null;
});
}
The Error is as following :
[WARN] 12:35:16,753 [main] contracts.AttachmentConstraint. - Found state com.template.contracts.MetalContract that is constrained by the insecure: AlwaysAcceptAttachmentConstraint.
net.corda.core.internal.TransactionDeserialisationException: Failed to deserialise group COMMANDS_GROUP at index 0 in transaction: Internal deserialization failure: java.lang.ClassNotFoundException: net.corda.testing.core.DummyCommandData
at net.corda.core.internal.TransactionUtilsKt$deserialiseComponentGroup$1.invoke(TransactionUtils.kt:82)
at net.corda.core.internal.TransactionUtilsKt$deserialiseComponentGroup$1.invoke(TransactionUtils.kt)
at net.corda.core.internal.LazyMappedList.get(InternalUtils.kt:567)
at net.corda.core.internal.LazyMappedList.get(InternalUtils.kt:567)
at java.util.AbstractList$Itr.next(AbstractList.java:358)
at net.corda.core.transactions.LedgerTransaction.createLtxForVerification(LedgerTransaction.kt:668)
at net.corda.core.transactions.LedgerTransaction.access$createLtxForVerification(LedgerTransaction.kt:44)
at net.corda.core.transactions.LedgerTransaction$internalPrepareVerify$1.invoke(LedgerTransaction.kt:154)
at net.corda.core.transactions.LedgerTransaction$internalPrepareVerify$1.invoke(LedgerTransaction.kt:44)
at net.corda.core.serialization.internal.AttachmentsClassLoaderBuilder$withAttachmentsClassloaderContext$1.invoke(AttachmentsClassLoader.kt:345)
at net.corda.core.serialization.SerializationFactory.withCurrentContext(SerializationAPI.kt:71)
at net.corda.core.serialization.internal.AttachmentsClassLoaderBuilder.withAttachmentsClassloaderContext(AttachmentsClassLoader.kt:344)
at net.corda.core.serialization.internal.AttachmentsClassLoaderBuilder.withAttachmentsClassloaderContext$default(AttachmentsClassLoader.kt:319)
at net.corda.core.transactions.LedgerTransaction.internalPrepareVerify$core(LedgerTransaction.kt:146)
at net.corda.core.transactions.LedgerTransaction.verify(LedgerTransaction.kt:136)
at net.corda.core.transactions.TransactionBuilder.addMissingDependency(TransactionBuilder.kt:185)
at net.corda.core.transactions.TransactionBuilder.toWireTransactionWithContext$core(TransactionBuilder.kt:165)
at net.corda.core.transactions.TransactionBuilder.toWireTransactionWithContext$core$default(TransactionBuilder.kt:133)
at net.corda.core.transactions.TransactionBuilder.toWireTransaction(TransactionBuilder.kt:130)
at net.corda.testing.dsl.TestTransactionDSLInterpreter.toWireTransaction$test_utils(TestDSL.kt:131)
at net.corda.testing.dsl.TestLedgerDSLInterpreter.recordTransactionWithTransactionMap(TestDSL.kt:298)
at net.corda.testing.dsl.TestLedgerDSLInterpreter._unverifiedTransaction(TestDSL.kt:336)
at net.corda.testing.dsl.TransactionDSL.input(TransactionDSLInterpreter.kt:150)
at com.template.contracts.ContractTests.lambda$MetalContractRequiresTheTransactionCommandToBeATransferCommand$13(ContractTests.java:190)
at net.corda.testing.node.NodeTestUtils$transaction$1.invoke(NodeTestUtils.kt:54)
at net.corda.testing.node.NodeTestUtils$transaction$1.invoke(NodeTestUtils.kt)
at net.corda.testing.node.NodeTestUtils$ledger$2.invoke(NodeTestUtils.kt:39)
at net.corda.testing.node.NodeTestUtils$ledger$2.invoke(NodeTestUtils.kt)
at net.corda.testing.internal.InternalTestUtilsKt$withTestSerializationEnvIfNotSet$1.invoke(InternalTestUtils.kt:231)
at net.corda.testing.internal.InternalTestUtilsKt$withTestSerializationEnvIfNotSet$1.invoke(InternalTestUtils.kt)
at net.corda.testing.common.internal.CommonSerializationTestHelpersKt.asContextEnv(CommonSerializationTestHelpers.kt:11)
at net.corda.testing.internal.InternalSerializationTestHelpersKt.asTestContextEnv(InternalSerializationTestHelpers.kt:33)
at net.corda.testing.internal.InternalSerializationTestHelpersKt.asTestContextEnv$default(InternalSerializationTestHelpers.kt:31)
at net.corda.testing.internal.InternalTestUtilsKt.withTestSerializationEnvIfNotSet(InternalTestUtils.kt:231)
at net.corda.testing.node.NodeTestUtils.ledger(NodeTestUtils.kt:36)
at net.corda.testing.node.NodeTestUtils.transaction(NodeTestUtils.kt:53)
at net.corda.testing.node.NodeTestUtils.transaction$default(NodeTestUtils.kt:51)
at net.corda.testing.node.NodeTestUtils.transaction(NodeTestUtils.kt)
at com.template.contracts.ContractTests.MetalContractRequiresTheTransactionCommandToBeATransferCommand(ContractTests.java:188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:64)
Caused by: java.io.NotSerializableException: Internal deserialization failure: java.lang.ClassNotFoundException: net.corda.testing.core.DummyCommandData
at net.corda.serialization.internal.amqp.DeserializationInput.des(DeserializationInput.kt:106)
at net.corda.serialization.internal.amqp.DeserializationInput.deserialize(DeserializationInput.kt:119)
at net.corda.serialization.internal.amqp.AbstractAMQPSerializationScheme.deserialize(AMQPSerializationScheme.kt:145)
at net.corda.serialization.internal.SerializationFactoryImpl$deserialize$1$1.invoke(SerializationScheme.kt:105)
at net.corda.core.serialization.SerializationFactory.withCurrentContext(SerializationAPI.kt:71)
at net.corda.serialization.internal.SerializationFactoryImpl$deserialize$1.invoke(SerializationScheme.kt:105)
at net.corda.serialization.internal.SerializationFactoryImpl$deserialize$1.invoke(SerializationScheme.kt:73)
at net.corda.core.serialization.SerializationFactory.asCurrent(SerializationAPI.kt:85)
at net.corda.serialization.internal.SerializationFactoryImpl.deserialize(SerializationScheme.kt:105)
at net.corda.core.internal.TransactionUtilsKt$deserialiseComponentGroup$1.invoke(TransactionUtils.kt:78)
... 65 more
Caused by: java.lang.ClassNotFoundException: net.corda.testing.core.DummyCommandData
at java.lang.ClassLoader.findClass(ClassLoader.java:523)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at net.corda.serialization.internal.model.TypeIdentifier$Unparameterised.getLocalType(TypeIdentifier.kt:151)
at net.corda.serialization.internal.model.ClassCarpentingTypeLoader$load$noCarpentryRequired$1$1.apply(TypeLoader.kt:38)
at net.corda.serialization.internal.model.ClassCarpentingTypeLoader$load$noCarpentryRequired$1$1.apply(TypeLoader.kt:25)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at net.corda.serialization.internal.model.ClassCarpentingTypeLoader$load$noCarpentryRequired$1.invoke(TypeLoader.kt:38)
at net.corda.serialization.internal.model.ClassCarpentingTypeLoader$load$noCarpentryRequired$1.invoke(TypeLoader.kt:25)
at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:149)
at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:109)
at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:133)
at kotlin.collections.MapsKt__MapsKt.putAll(Maps.kt:339)
at kotlin.collections.MapsKt__MapsKt.toMap(Maps.kt:504)
at kotlin.collections.MapsKt__MapsKt.toMap(Maps.kt:498)
at net.corda.serialization.internal.model.ClassCarpentingTypeLoader.load(TypeLoader.kt:45)
at net.corda.serialization.internal.amqp.DefaultRemoteSerializerFactory.reflect(RemoteSerializerFactory.kt:129)
at net.corda.serialization.internal.amqp.DefaultRemoteSerializerFactory.access$reflect(RemoteSerializerFactory.kt:47)
at net.corda.serialization.internal.amqp.DefaultRemoteSerializerFactory$get$1.invoke(RemoteSerializerFactory.kt:71)
at net.corda.serialization.internal.amqp.DefaultRemoteSerializerFactory$get$1.invoke(RemoteSerializerFactory.kt:47)
at net.corda.serialization.internal.amqp.DefaultDescriptorBasedSerializerRegistry.getOrBuild(DescriptorBasedSerializerRegistry.kt:28)
at net.corda.serialization.internal.amqp.DefaultRemoteSerializerFactory.get(RemoteSerializerFactory.kt:66)
at net.corda.serialization.internal.amqp.ComposedSerializerFactory.get(SerializerFactory.kt)
at net.corda.serialization.internal.amqp.DeserializationInput.readObject(DeserializationInput.kt:172)
at net.corda.serialization.internal.amqp.DeserializationInput.readObjectOrNull(DeserializationInput.kt:147)
at net.corda.serialization.internal.amqp.DeserializationInput$deserialize$1.invoke(DeserializationInput.kt:124)
at net.corda.serialization.internal.amqp.DeserializationInput.des(DeserializationInput.kt:99)
... 74 more
build.gradle file
apply plugin: 'net.corda.plugins.cordapp'
apply plugin: 'net.corda.plugins.cordformation'
cordapp {
targetPlatformVersion corda_platform_version
minimumPlatformVersion corda_platform_version
contract {
name "Template CorDapp"
vendor "Corda Open Source"
licence "Apache License, Version 2.0"
versionId 1
}
}
sourceSets {
main{
java {
srcDir 'src/main/java'
java.outputDir = file('bin/main')
}
}
test{
java{
srcDir 'src/test/java'
java.outputDir = file('bin/test')
}
}
}
dependencies {
// Corda dependencies.
cordaCompile "$corda_release_group:corda-core:$corda_release_version"
cordaRuntime "$corda_release_group:corda:$corda_release_version"
testCompile "$corda_release_group:corda-node-driver:$corda_release_version"
}
答案1
得分: 1
这是由于您的DummyCommandData引起的问题。
命令必须在您的合同内部定义,而不是创建另一个数据类。
尝试将命令放在您的合同内。
例如:
public class MetalContract extends Contract {
public interface Commands extends CommandData {
class Create implements Commands {}
}
//...
}
英文:
This is a problem caused by your DummyCommandData.
The Command has to be defined inside your contract instead of creating another data class aside.
Try putting a command inside your contract.
ie.
public MetalContract extends Contract{
public interface Commands extends CommandData {
class Create implements Commands {}
}
//...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论