java-sdk调用solidity 0.7.5版本合约失败
【问题分类】
- bug
- P2P网络相关(包含libp2p,liquid)
- 链账户身份与权限相关(证书问题、public、多签投票问题)
- 核心交易引擎相关(交易池、DAG)
- 共识相关
- 智能合约相关
- 存储相关
- [
✅ ] SDK相关 - 长安链CMC工具
- 长安链管理台
- 长安链浏览器
- 长安链合约IDE
- 长安链web签名插件
- 跨链相关
- 轻节点相关
- 隐私计算相关
- 密码学相关
- 环境依赖
- 其他补充:
【问题描述】(请对问题进行描述,方便定位问题)
具体的现象,操作步骤,前置条件等,尽可能描述清楚,有截图说明更佳
- solidty合约如下: // SPDX-License-Identifier: MIT pragma solidity 0.7.5; pragma abicoder v2;
contract IdentityVC { struct Credential { string credentialType; // 凭证类型,如“毕业证” string issuedData; // 发放细节,例如发放机构、地点 bytes32 hash; // 凭证的哈希值 uint256 timestamp; // 存储凭证的时间戳 address issuedBy; // 凭证颁发部门的地址 string additionalData; // 其他附加信息 bool exists; // 凭证是否存在 }
mapping(address => mapping(bytes32 => Credential)) private credentials;
// 用户地址 -> 凭证哈希 -> 凭证结构体
event CredentialIssued(address indexed user, bytes32 indexed credentialHash, string credentialType);
event CredentialVerified(address indexed user, bytes32 indexed credentialHash, bool isValid);
// 1. 可验证凭证生成与存储
function issueCredential(
address user,
bytes32 credentialHash,
string memory credentialType,
string memory issuedData,
string memory additionalData
) external {
require(!credentials[user][credentialHash].exists, "Credential already exists");
credentials[user][credentialHash] = Credential({
credentialType: credentialType,
issuedData: issuedData,
hash: credentialHash,
timestamp: block.timestamp,
issuedBy: msg.sender,
additionalData: additionalData,
exists: true
});
emit CredentialIssued(user, credentialHash, credentialType);
}
// 2. 凭证验证
function verifyCredential(address user, bytes32 credentialHash) external returns (bool) {
Credential storage credential = credentials[user][credentialHash];
// 验证凭证是否存在
if (credential.exists) {
emit CredentialVerified(user, credentialHash, true);
return true;
} else {
emit CredentialVerified(user, credentialHash, false);
return false;
}
}
// 3. 获取凭证信息
function getCredential(address user, bytes32 credentialHash) external view returns (Credential memory) {
Credential storage credential = credentials[user][credentialHash];
require(credential.exists, "Credential does not exist");
return (credential);
}
} 2.调用方法issueCredential,java代码如下: public static void invokeContract(ChainClient chainClient) { System.out.println("=====================调用合约=============================="); ResultOuterClass.TxResponse responseInfo = null;
Map<String, byte[]> params = new HashMap<>();
String addr = "0x1234567890abcdef1234567890abcdef12345678"; // Example Ethereum address
String credentialType = "Employee ID";
String issuedData = "Employee of the Year 2023";
String additionalData = "Issued for outstanding performance";
// Convert "test" to 32 bytes
byte[] testBytes = new byte[32];
System.arraycopy("test".getBytes(), 0, testBytes, 0, "test".length());
System.out.println(testBytes);
Function function = new Function(
"issueCredential",
Arrays.asList(
new Address(addr),
new Bytes32(testBytes),
new Utf8String(credentialType),
new Utf8String(issuedData),
new Utf8String(additionalData)
),
Collections.emptyList()
);
String methodDataStr = FunctionEncoder.encode(function);
String method = methodDataStr.substring(0, 10);
System.out.println(method);
params.put(CONTRACT_ARGS_EVM_PARAM, methodDataStr.getBytes());
try {
responseInfo = chainClient.invokeContract(Utils.calcContractName(CONTRACT_NAME), method,
null, params, 100000, 100000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("执行合约结果:");
System.out.println(responseInfo);
System.out.println("==========================================================");
}
【相关日志文件】(如果有报错日志请贴图,或者上传附件)
【系统信息】(请填写系统信息,方便定位问题)
- chainmaker-java version * : [v2.3.0]
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information