智能合约代码
-
主要存放两个第三章两个方案的智能合约主要代码
基于验证的可验证计算的智能合约主要代码
Contrat-STSBC
contract tlocked_servicepay {
mapping(address =>int ) balanceOf;//余额
address payable outsourcer;
address payable worker;
address payable deposit; //押金池
enum State{Created,Remdeposited,Deposited,Confirmed, Paid,Claimed,End}
State public state;
uint fee;//报酬
uint value1;//用户交的押金
uint value2;//工人交的押金
struct Task{
string f; //外包任务函数
uint D; //外包任务预期值,外包任务的数据集
uint d; //工人对D进行哈希计算生成的值,d=H(D)
uint q; //累加器算法随机数的哈希值q=H{b}
uint p; //累加器算法生成的估值
uint v; //累加器算法生成的见证
}
Task [] task;
Task tmpTask;
//用户存入押金
function depositoutsourcer( uint d, uint z, uint c,uint value1) public {
require(msg.sender == outsourcer);
//检测外包用户是否有足够余额
if (value1 >= fee){
balanceOf[outsourcer] -= fee;
balanceOf[deposit] += fee;
}
state = State.Remdeposited;
}
//工作节点存入押金
function depositworker(uint q, uint p,uint value2) public {
require(msg.sender == worker);
tmpTask.q=q;
//检测工作节点是否有足够余额
if(value2 >= fee){
balanceOf[worker] -= fee;
balanceOf[deposit] += fee;
}
state = State.Deposited;
}
// 如果工人收到的任务有问题,发送错误的密文C1))
function confirmTask(uint c1,uint d,uint z) public {
require(msg.sender == worker);
tmpTask.c1=c1;
tmpTask.d=d;
if(c1 != tmpTask.c|| c1 != d + z || tmpTask.c!= d + z ){
balanceOf[deposit] -= 2*fee;
balanceOf[worker] += fee;
balanceOf[outsourcer] += fee;
abort();
}
state = State.Confirmed;} function receiveAnswer(uint p,uint d1,uint v)public { require(msg.sender== outsourcer); tmpTask.p=p; tmpTask.v=v; tmpTask.d1=d1; if(d1 != tmpTask.d){ balanceOf[deposit] -= 2*fee; balanceOf[worker] += fee; balanceOf[outsourcer] += fee; abort(); } state=State.Claimed; } function payworker(uint L,uint v, uint g) public { require(msg.sender == outsourcer); tmpTask.g=g; // 如果用户收到的结果有问题,发送错误的结果x和对应的见证vx if ( g != tmpTask.d ) { balanceOf[deposit] -= 2*fee; balanceOf[outsourcer] += 2*fee; } balanceOf[deposit] -= 2*fee; balanceOf[worker] += 2*fee; state= State.Paid; } function abort() public { state= State.End; }
}
基于对比的可验证计算的智能合约主要代码
Contrat-MTSBC
contract tlocked_servicepay {
uint256 public value;
mapping(address =>uint ) balanceOf;//余额
address payable outsourcer;
address payable worker;
address payable deposit; //押金池
enum State{Created,Remdeposited,Deposited,Confirmed, Paid,Claimed,End}
State public state;
uint fee;//报酬
uint value1;//用户交的押金
uint value2;//工人交的押金
uint L;
struct Task{
uint f; //外包任务函数
uint D; //外包任务预期值
uint d; //工人对D进行哈希计算生成的值,d=H(D
uint d1;// 用户对D进行哈希计算生成的值,d1=H(D)
uint x; //错误解
uint g; //错误解x的函数值f(x)的哈希值,即g=H(f(x))
}
Task [] task;
Task tmpTask;
//用户存入押金
function depositoutsourcer( uint d, uint z, uint c,uint value1) public {
require(msg.sender == outsourcer);
tmpTask.d=d;
//检测外包用户是否有足够余额
if (value1 >= fee){
balanceOf[outsourcer] -= fee;
balanceOf[deposit] += fee;
}
state = State.Remdeposited;} //工作节点存入押金 function depositworker(uint q, uint p,uint value2) public { require(msg.sender == worker); //检测工作节点是否有足够余额 if(value2 >= fee){ balanceOf[worker] -= fee; balanceOf[deposit] += fee; } state = State.Deposited; } // 如果工人收到的任务数据有问题,发送错误的d1 function confirmTask(uint c1,uint d,uint z) public { require(msg.sender == worker); tmpTask.d1=d1; if(d1 != d| ){ balanceOf[deposit] -= 2*fee; balanceOf[worker] += fee; balanceOf[outsourcer] += fee; abort(); } state = State.Confirmed; } function receiveAnswer(uint m[k] )public { require(msg.sender== outsourcer); for(int k =0;0≤k≤k1;k++){ m[k]=msg.value; } if(m[k] != m[k+1]){ receiveAnswer (); } mT=m[k];
state=State.Claimed;
}
// 如果用户收到的结果有问题,求解mT的函数值f(x)的哈希
值g,即g=H(f(mTx))
function payworker(uint mT,uint v, uint g) public {
require(msg.sender == outsourcer);
if ( g != tmpTask.d )
{
balanceOf[deposit] -= 2fee;
balanceOf[outsourcer] += 2fee;
}
balanceOf[deposit] -= 2fee;
balanceOf[worker] += 2fee;
state= State.Paid;
}
function abort() public {
state= State.End;
}
}