Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

双人组队 TASK4 基于FISCO BCOS的并行合约设计与实践 #184

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Delivery.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;
// 派送合约
import "./StorageData.sol";
import "./ParallelContract.sol";
contract Delivery is StorageData,ParallelContract {

// 判断当前是否有快递员权限
modifier IsCourier(address courierAddress) {
require(UserMap[courierAddress].role == 2,"当前用户不是快递员");
_;
}


// 用户的注册业务
function userRegister(address _userAddress) public {
UserMap[_userAddress].u_address = _userAddress;
UserMap[_userAddress].role = 1;
}


// 快递员注册业务
function courierRegister(address _courierAddress) public {
UserMap[_courierAddress].u_address = _courierAddress;
UserMap[_courierAddress].role = 2;
}

// 签收生成签收记录
function courierToUserSign(address _from,address _to,uint256 _isSign) public IsCourier(_from) {
// 生成签收完成的订单记录
RecordCount++;
SignRecord storage _signRecord = RecordMap[RecordCount];
_signRecord.record_id = RecordCount;
_signRecord.c_address = _from;
_signRecord.u_address = _to;
_signRecord.sign_time = block.timestamp;

SignRecordList.push(RecordCount);

if (_isSign == 0){
// 设置该用户已签收
UserIsSignStatus[_to] = true;
}else {
// 设置用户拒绝签收
UserIsSignStatus[_to] = false;
emit Refusal(_to,block.timestamp);
}
emit Signed(_to,_isSign);
}

// 对物流进行售后
function afterSales(address _from,uint256 _isClaim) public {
require(UserIsClaimStatus[_from] == false,"当前用户已经申请售后");
if (_isClaim == 0){
UserIsClaimStatus[_from] = true;
}else {
UserIsClaimStatus[_from] = false;
}
}

// 查询所有的签收记录
function selectSignRecordList() public returns(SignRecord[] memory) {
SignRecord[] memory recordList = new SignRecord[](SignRecordList.length);
for (uint i = 0; i < SignRecordList.length; i++){
recordList[i] = RecordMap[SignRecordList[i]];
}
return recordList;
}

// 声明函数 enableParallel,用于启用并行计算功能
function enableParallel() public {
registerParallelFunction("courierToUserSign(address,address,uint256)",2);
registerParallelFunction("afterSales(address,uint256)",1);
}

// 声明函数 disableParallel,用于禁用并行计算功能
function disableParallel() public {
unregisterParallelFunction("courierToUserSign(address,address,bool)");
unregisterParallelFunction("afterSales(address,uint256)");
}
}
43 changes: 43 additions & 0 deletions ParallelContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pragma solidity ^0.4.25;
// 预编译合约 ParallelConfigPrecompiled

contract ParallelConfigPrecompiled {
// 注册并行函数
// 参数1:并行函数地址
// 参数2:并行函数名称
// 参数3:致命区间值,用于限制并行计算区间大小
function registerParallelFunctionInternal(address, string, uint256) public returns (int);

// 注销并行函数
// 参数1:并行函数地址
// 参数2:并行函数名称
function unregisterParallelFunctionInternal(address, string) public returns (int);
}

// 并行合约基类
// 主要的合约
contract ParallelContract {
// 实例化并使用 ParallelConfigPrecompiled 合约
ParallelConfigPrecompiled precompiled = ParallelConfigPrecompiled(0x1006);

// 注册并行函数,注册后可以在并行计算中使用
// 参数1:并行函数名称
// 参数2:致命区间值,用于限制并行计算区间大小
function registerParallelFunction(string functionName, uint256 criticalSize) public
{
precompiled.registerParallelFunctionInternal(address(this), functionName, criticalSize);
}

// 注销并行函数,在停用并行计算前建议注销所有已注册的并行函数
// 参数1:并行函数名称
function unregisterParallelFunction(string functionName) public
{
precompiled.unregisterParallelFunctionInternal(address(this), functionName);
}

// 启用并行计算功能
function enableParallel() public;

// 关闭并行计算功能
function disableParallel() public;
}
45 changes: 45 additions & 0 deletions StorageData.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
pragma solidity ^0.4.25;
// 数据结构合约 定义公共参数
contract StorageData {
// 快递员
struct User{
address u_address; // 用户的地址
uint role; // 角色
}

// 签收记录
struct SignRecord{
uint record_id;
address c_address;
address u_address;
uint256 sign_time;
}

// 签收记录ID
uint256 public RecordCount;
// 签收的记录数组
uint256[] public SignRecordList;


// 用户签收的状态
mapping(address => bool) public UserIsSignStatus;
// 用户售后申请
mapping(address => bool) public UserIsClaimStatus;
// 用户注册
mapping(address => User) public UserMap;
// 生成记录
mapping(uint256 => SignRecord) public RecordMap;


// 用户注册的事件
event Registered(address indexed _userAddress,uint indexed _role);


// 用户签收的事件
event Signed(address indexed _userAddress,uint256 indexed _isSign);


// 用户拒收事件
event Refusal(address indexed _userAddress,uint indexed _time);

}
Loading