Hyperledger Fabric Java SDK查询正常但创建交易报无效交易合约异常
Hyperledger Fabric创建交易失败:Contract Exception提示Invalid Transaction
我正在开发基于Hyperledger Fabric的应用,使用Java SDK与智能合约交互。查询交易可正常执行,但调用创建交易时,触发“Contract Exception”并提示“Invalid Transaction”。
详细信息
智能合约代码(Go)
Fabric网络上部署的智能合约中,CreateContract函数用于根据传入参数创建新合约,若合约已存在则抛出错误,代码如下:
func (s *SmartContract) CreateContract(ctx contractapi.TransactionContextInterface, contractId string, client string, supplier string, description string) (string, error) { if contractId == "" { return "", fmt.Errorf("Contract Id cannot be empty.") } if client == "" { return "", fmt.Errorf("Client field cannot be empty.") } if supplier == "" { return "", fmt.Errorf("Supplier field cannot be empty.") } chatAsBytes, err := ctx.GetStub().GetState(contractId) if err != nil { return "", fmt.Errorf("Failed to read from world state. %s", err.Error()) } if chatAsBytes == nil { newContract := LegalContractSchema{ DocType: "Legal Contract", Participants: []string{client, supplier}, Description: description, } newContractAsBytes, _ := json.Marshal(newContract) err := ctx.GetStub().PutState(contractId, newContractAsBytes) if err != nil { return "", fmt.Errorf("Failed writing to world state. %s", err.Error()) } return "Successfully created contract", nil } else { return "", fmt.Errorf("Failed to create contract schema, contract already exists.") } }
Java SDK代码
调用CreateContract函数的Java代码如下:
Gateway.Builder builder = Gateway.createBuilder() .identity(wallet, "admin") .networkConfig(networkConfigFile) .discovery(true) .commitTimeout(60, TimeUnit.SECONDS); try (Gateway gateway = builder.connect()) { Network network = gateway.getNetwork("mychannel"); Contract contract = network.getContract("legalContract"); // 提交创建交易 byte[] createResult = contract.createTransaction("CreateContract") .submit("contractId123", "ClientName", "SupplierName", "This is a test description"); System.out.println("Create Transaction Result: " + new String(createResult, StandardCharsets.UTF_8)); }
查询交易
采用相同代码结构的查询交易(GetLegalContractCert)可正常执行:
byte[] queryResult = contract.evaluateTransaction("GetLegalContractCert", "contractId123"); System.out.println("Query Result: " + new String(queryResult, StandardCharsets.UTF_8));
问题
调用CreateContract函数时,触发错误:Invalid transaction Contract Exception,查询交易正常,但创建交易失败。
已尝试的排查操作
- 验证输入参数:确保contractId、client、supplier和description为UTF-8编码字符串;
- 检查背书策略:确认背书策略与交易使用的节点匹配;
- 调试智能合约:为
CreateContract函数添加日志,但未触发日志,表明交易未到达合约; - 延长超时时间:设置更长的提交和交付超时。
环境信息
- Hyperledger Fabric: v2.4
- Java SDK: v2.4
- Go智能合约: Fabric Contract API
- 网络: 单通道(mychannel),包含2个组织和2个节点
连接配置文件
{ "name": "test-network-org1", "version": "1.0.0", "client": { "organization": "Org1", "connection": { "timeout": { "peer": { "endorser": "300" } } } }, "organizations": { "Org1": { "mspid": "Org1MSP", "peers": [ "peer0.org1.example.com" ], "certificateAuthorities": [ "ca.org1.example.com" ] } }, "peers": { "peer0.org1.example.com": { "url": "grpcs://localhost:7051", "tlsCACerts": { "pem": "-----BEGIN CERTIFICATE-----\XYF\n-----END CERTIFICATE-----\n" }, "grpcOptions": { "ssl-target-name-override": "peer0.org1.example.com", "hostnameOverride": "peer0.org1.example.com" } } }, "certificateAuthorities": { "ca.org1.example.com": { "url": "https://localhost:7054", "caName": "ca-org1", "tlsCACerts": { "pem": ["-----BEGIN CERTIFICATE-----\XYZ\n-----END CERTIFICATE-----\n"] }, "httpOptions": { "verify": false } } } }
内容的提问来源于stack exchange,提问作者Abdul Mueed
相关产品推荐
相关产品推荐

