区块链部署指南
1. 创建Truffle项目,初始化Truffle项目
mkdir my-project
cd my-project
truffle init
初始化之后会生成几个文件夹,以下均在Truffle项目中执行
2. 编写智能合约(sol文件,可以根据自己需要使用自己的智能合约),并将写好的sol文件存储到/contracts,还需要在/migrations编辑迁移文件
以下以存储数据库数据的智能合约(sol文件)为例
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
//DataStorage就是合约的名字,即编译成功后的文件名,在迁移文件也要用到
contract DataStorage {
struct Data {
uint256 id;
string dataType;
string way;
string acqTime;
string place;
string temperature;
string pressure;
string altitude;
address owner;
bool isDeleted;
}
mapping(uint256 => Data) private dataStore;
uint256 private dataCounter;
// Event emitted when data is stored
event DataStored(uint256 indexed dataId, address indexed owner);
// Function to store data
function storeData(
string memory dataType,
string memory way,
string memory acqTime,
string memory place,
string memory temperature,
string memory pressure,
string memory altitude
) public returns (uint256) {
dataCounter++;
dataStore[dataCounter] = Data({
id: dataCounter,
dataType: dataType,
way: way,
acqTime: acqTime,
place: place,
temperature: temperature,
pressure: pressure,
altitude: altitude,
owner: msg.sender,
isDeleted: false
});
emit DataStored(dataCounter, msg.sender);
return dataCounter;
}
// Function to retrieve data
function retrieveData(uint256 id) public view returns (
uint256,
string memory,
string memory,
string memory,
string memory,
string memory,
string memory,
string memory,
address,
bool
) {
require(dataStore[id].owner == msg.sender, "Not authorized to access this data");
Data storage data = dataStore[id];
return (
data.id,
data.dataType,
data.way,
data.acqTime,
data.place,
data.temperature,
data.pressure,
data.altitude,
data.owner,
data.isDeleted
);
}
// Function to share data with another user
function shareData(uint256 id, address newOwner) public {
require(dataStore[id].owner == msg.sender, "Not authorized to share this data");
dataStore[id].owner = newOwner;
}
// Function to delete data
function deleteData(uint256 id) public {
require(dataStore[id].owner == msg.sender, "Not authorized to delete this data");
dataStore[id].isDeleted = true;
}
}
迁移文件示例
const DataStorage = artifacts.require("DataStorage"); //DataStorage sol文件中有提到
const Test = artifacts.require("Test");
module.exports = function (deployer) {
deployer.deploy(DataStorage); //与上面的同步
};
3. 编译并部署智能合约,在项目中/build/contracts文件夹中生成
**注:truffle compile编译智能合约,进行truffle migrate部署智能合约之前需要先打开Ganacha,并在truffle项目文件中调整好配置(truffle-config.js)
truffle compile
truffle migrate //该命令仅限于在本地区块链(ganache)测试时使用
// truffle-config.js
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: ganache)
port: 7545, // 根据Ganacha可视化界面中的端口号设定,默认为7545
network_id: "*", // 设置为"*",即可连接所有
},
},
compilers: {
solc: {
version: "0.8.0", // Specify compiler version
},
},
};
4. 提取编译文件(.json)的.abi和.bin文件
**在编译出的文件中,找到.abi和.bin两部分,填写到java或者go项目中(这里以java为例)
5. 在java项目中安装配置web3j,使用web3j命令生成Java类
web3j solidity generate -a D:\javaweb\project\UnderwaterDataManagement-backend\build\contracts\DataStorage.abi -b D:\javaweb\project\UnderwaterDataManagement-backend\build\contracts\DataStorage.bin -o D:\javaweb\project\UnderwaterDataManagement-backend\src\main\java -p com.guanzhi.springbootinit.utils
D:\javaweb\project\UnderwaterDataManagement-backend\build\contracts\DataStorage.abi,.abi文件的存储地址(创建.abi文件,并把提取的abi内容粘贴到文件中,下方的.bin同理)
D:\javaweb\project\UnderwaterDataManagement-backend\build\contracts\DataStorage.bin,.bin文件的存储地址(创建.bin文件,提取json文件中的bytecode粘贴到文件中)
D:\javaweb\project\UnderwaterDataManagement-backend\src\main\java -p com.guanzhi.springbootinit.utils(生成的java类的存储地址)
在其他语言中均有其他的处理方式,golang中使用abigen或者直接解析abi文件即可
6. 根据区块链网络填写私钥和地址
- 项目部署成功后,黑窗口如下即为部署成功
然后打开Ganache,首页随意点击一个钥匙(show key),展示出的ACCOUNT ADDRESS和PRIVATE KEY均是我们需要的值。
然后把区块链信息添加到java项目中
56759728)]
然后打开Ganache,首页随意点击一个钥匙(show key),展示出的ACCOUNT ADDRESS和PRIVATE KEY均是我们需要的值。