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

73 _黄军政_第五课作业 #472

Open
wants to merge 61 commits into
base: 73-黄军政
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
b90d86f
payroll homework
Jan 8, 2018
0c1e86f
Merge branch '91-Chris' of https://github.com/taojing10/guigulive-ope…
Jan 8, 2018
8512a0c
remove orginal address
Jan 8, 2018
2874dfb
Merge pull request #2 from linjie-1/master
steven672 Jan 9, 2018
27587f6
Create 第一课作业
steven672 Jan 9, 2018
04d95b4
Delete 第一课作业
steven672 Jan 9, 2018
3040b06
Update yours.sol
steven672 Jan 9, 2018
c6032c2
Merge pull request #1 from linjie-1/master
zl777 Jan 10, 2018
602f1c1
Update yours.sol
zl777 Jan 10, 2018
10b864f
根据要求加了require 函数, 并更新了源代码的基本function。 还请老师在看下~
steven672 Jan 12, 2018
60f2dcd
update
steven672 Jan 12, 2018
8a87868
update
steven672 Jan 12, 2018
9923178
Merge pull request #1 from linjie-1/master
zyzlouis Jan 13, 2018
d07ab7f
Update yours.sol
zl777 Jan 13, 2018
b99b727
Merge pull request #217 from linjie-1/zl777-patch-1
hezhihua81 Jan 13, 2018
55fac2b
Merge pull request #3 from linjie-1/master
steven672 Jan 13, 2018
5b96d76
revert last mis-checkin...
hezhihua81 Jan 13, 2018
31621b6
Update
steven672 Jan 13, 2018
2e095ae
update hw2
steven672 Jan 13, 2018
dbff9b0
创建第三课目录
lsdlinshunda Jan 14, 2018
78fa6e7
添加第三课说明
lsdlinshunda Jan 14, 2018
4d8f45b
添加第三课作业说明
lsdlinshunda Jan 14, 2018
66f27b5
Merge pull request #3 from linjie-1/master
jaryhjz Jan 17, 2018
1010b85
Merge branch 'master' into master
steven672 Jan 17, 2018
80e53bb
delete hw2
steven672 Jan 17, 2018
ec1f212
restore
steven672 Jan 17, 2018
e52488a
Merge pull request #2 from linjie-1/master
zyzlouis Jan 17, 2018
20e9d21
hw-2
Jan 17, 2018
1c3c3d6
提交第三课代码
jaryhjz Jan 17, 2018
8275d09
Create 函数运行截图
jaryhjz Jan 17, 2018
b11d372
函数运行截图
jaryhjz Jan 17, 2018
1dd79c5
更新题目说明
jaryhjz Jan 17, 2018
964169b
补上增加函数的图片
jaryhjz Jan 17, 2018
68df86a
Merge pull request #304 from zl777/master
zl777 Jan 17, 2018
bf357cf
Create README.md
linjie-1 Jan 17, 2018
50c15e3
Create README.md
linjie-1 Jan 17, 2018
83e2b9b
Create payroll.sol
linjie-1 Jan 17, 2018
b38d0ff
Create README.md
linjie-1 Jan 17, 2018
953eab2
Create yours.sol
linjie-1 Jan 17, 2018
e28b1b3
Delete yours.sol
linjie-1 Jan 17, 2018
b72fc45
Create yours.sol
linjie-1 Jan 17, 2018
c66e0cc
Update yours.sol
linjie-1 Jan 17, 2018
159bd42
Revert "第3课的代码"
lsdlinshunda Jan 17, 2018
cdece6b
Merge pull request #315 from linjie-1/revert-304-master
lsdlinshunda Jan 17, 2018
656972c
Merge branch 'master' of https://github.com/linjie-1/guigulive-operation
Jan 17, 2018
d355bf4
HW-3
Jan 17, 2018
15f22fd
Merge pull request #321 from zyzlouis/master
zyzlouis Jan 18, 2018
e8a7a93
Revert "Get 第四课"
lsdlinshunda Jan 18, 2018
e8c390c
Merge pull request #327 from linjie-1/revert-321-master
lsdlinshunda Jan 18, 2018
b630619
Merge pull request #4 from linjie-1/master
jaryhjz Jan 20, 2018
29a254b
提交第4四次课程作业和详细说明
jaryhjz Jan 20, 2018
eb3ae87
创建代码目录
jaryhjz Jan 20, 2018
f6d9e79
提交代码,除了build和node_modules目录
jaryhjz Jan 20, 2018
386acd7
Merge pull request #316 from taojing10/master
LeapM Jan 20, 2018
3ec4c1e
Revert "91_HW-3"
lsdlinshunda Jan 22, 2018
1c6c8df
Merge pull request #396 from linjie-1/revert-316-master
lsdlinshunda Jan 22, 2018
e834fa8
添加第五课目录
lsdlinshunda Jan 22, 2018
6aa2351
whatever
Jan 22, 2018
c5669e8
创建第六、七课目录
lsdlinshunda Jan 25, 2018
7470e45
Merge pull request #5 from linjie-1/master
jaryhjz Jan 27, 2018
81dc57c
实现基本的用户界面,编译运行通过
jaryhjz Jan 27, 2018
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
16 changes: 16 additions & 0 deletions Lesson3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 硅谷live以太坊智能合约频道官方地址

### 第三课《智能合约后端优化和产品化》

目录结构
<br/>|
<br/>|--orgin 课程初始代码
<br/>|
<br/>|--assignment 课程作业提交代码
<br/>
### 本节知识点
第3课:智能合约后端优化和产品化
- 如何通过数据结构优化降低合约执行成本
- 合约的继承
- 巧用modifier
- 以太坊函数库的使用和基本介绍
14 changes: 14 additions & 0 deletions Lesson3/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## 硅谷live以太坊智能合约 第三课作业
这里是同学提交作业的目录

### 第三课:课后作业
- 第一题:完成今天所开发的合约产品化内容,使用Remix调用每一个函数,提交函数调用截图
- 第二题:增加 changePaymentAddress 函数,更改员工的薪水支付地址,思考一下能否使用modifier整合某个功能
- 第三题(加分题):自学C3 Linearization, 求以下 contract Z 的继承线
- contract O
- contract A is O
- contract B is O
- contract C is O
- contract K1 is A, B
- contract K2 is A, C
- contract Z is K1, K2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/img/Payroll.addFund().jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/img/Payroll.getPaid().jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Lesson3/assignment/img/函数运行截图
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
函数运行截图
119 changes: 119 additions & 0 deletions Lesson3/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*作业请提交在这个目录下*/

/****第一题:
放在img文件夹
*/

/****第二题:
employee可以改Payment地址,Modifier不是必须的。
function changePaymentAddress(address empolyeeId, address newEmployeeId) onlyOwner employeeExist(empolyeeId) {
var employee = employees[empolyeeId];
employees[newEmployeeId] = Employee(newEmployeeId, employee.salary, employee.lastPayDay);
delete employees[empolyeeId];
}
*/

/*****第三题:
L(O) := [O]
L(A) := [A, O]
L(B) := [B, O]
L(C) := [C, O]
L(K1) := [K1, A, B, O]
L(K2) := [K2, A, C, O]
L(Z) := [Z, K1, K2, A, B, C, O]
*/


/**
完整代码
*/
pragma solidity ^0.4.14;

contract Payroll {
struct Employee {
address id;
uint salary;
uint lastPayDay;
}

uint constant payDuration = 10 seconds;
address owner;
mapping (address => Employee) public employees;
uint totalSalary = 0;

modifier onlyOwner {
require(msg.sender == owner);
_;
}

modifier employeeExist(address employeeId){
assert(employees[employeeId].id != 0x0);
_;
}

modifier employeeNotExist(address employeeId){
assert(employees[employeeId].id == 0x0);
_;
}

function Payroll() {
owner = msg.sender;
}

function _partialPaid(Employee employee) private {
uint payment = employee.salary * (now - employee.lastPayDay) / payDuration;
employee.id.transfer(payment);
}

function addEmployee(address employeeId, uint salary) onlyOwner employeeNotExist(employeeId) {
var employee = employees[employeeId];
salary = salary * 1 ether;
employees[employeeId] = Employee(employeeId, salary, now);
totalSalary += salary;
}

function removeEmployee(address employeeId) onlyOwner employeeExist(employeeId) {
var employee = employees[employeeId];
_partialPaid(employee);
totalSalary -= employee.salary;
delete employees[employeeId];
}

function updateEmployee(address employeeId, uint salary) onlyOwner employeeExist(employeeId) {
var employee = employees[employeeId];
_partialPaid(employee);

uint oldSalary = employee.salary;
uint newSalary = salary * 1 ether;
employees[employeeId] = Employee(employeeId, newSalary, now);
totalSalary = totalSalary + newSalary - oldSalary;
}

function changePaymentAddress(address empolyeeId, address newEmployeeId) onlyOwner employeeExist(empolyeeId) {
var employee = employees[empolyeeId];
employees[newEmployeeId] = Employee(newEmployeeId, employee.salary, employee.lastPayDay);
delete employees[empolyeeId];
}

function addFund() payable returns (uint){
return this.balance;
}

function calculateRunway() returns (uint) {
return this.balance / totalSalary;
}

function hasEnoughFund() returns (bool){
return calculateRunway() > 0;
}

function getPaid() employeeExist(msg.sender) {
var employee = employees[msg.sender];

uint nextPayDay = employee.lastPayDay + payDuration;
assert(nextPayDay < now);

employee.lastPayDay = nextPayDay;
employee.id.transfer(employee.salary);
}
}
3 changes: 3 additions & 0 deletions Lesson3/orgin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约 第三课

这里是每一课的初始代码,有需要的同学可以参考
Empty file added Lesson3/orgin/payroll.sol
Empty file.
16 changes: 16 additions & 0 deletions Lesson4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 硅谷live以太坊智能合约频道官方地址

### 第四课《使用Truffle架构进行前后端交互,测试,部署》

目录结构
<br/>|
<br/>|--orgin 课程初始代码
<br/>|
<br/>|--assignment 课程作业提交代码
<br/>
### 本节知识点
第4课:使用Truffle架构进行前后端交互,测试,部署
- 为什么要用Truffle,Truffle的基本概念
- Truffle 的command line 功能
- 初始化项目与Truffle项目目录结构
- 编译部署合约到testrpc
12 changes: 12 additions & 0 deletions Lesson4/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## 硅谷live以太坊智能合约 第四课作业
这里是同学提交作业的目录

### 第四课:课后作业
- 将第三课完成的payroll.sol程序导入truffle工程
- 在test文件夹中,写出对如下两个函数的单元测试:
- function addEmployee(address employeeId, uint salary) onlyOwner
- function removeEmployee(address employeeId) onlyOwner employeeExist(employeeId)
- 思考一下我们如何能覆盖所有的测试路径,包括函数异常的捕捉
- (加分题,选作)
- 写出对以下函数的基于solidity或javascript的单元测试 function getPaid() employeeExist(msg.sender)
- Hint:思考如何对timestamp进行修改,是否需要对所测试的合约进行修改来达到测试的目的?
1 change: 1 addition & 0 deletions Lesson4/assignment/src/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*创建代码目录*
Binary file added Lesson4/assignment/src/payroll.tar.gz
Binary file not shown.
142 changes: 142 additions & 0 deletions Lesson4/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*作业请提交在这个目录下*/
/******
题目1:
完成代码见src目录
*/

/******
题目2:
列举不同的输入参数:异常参数和正常参数;
使用不同的调用address:需要学习如何切换address
函数异常用 .catch(function(error) 来捕捉。

基于JS的测试:
步骤1:修改文件夹migrations下的文件2_deploy_contracts.js

var SimpleStorage = artifacts.require("./SimpleStorage.sol");
var Payroll = artifacts.require("./Payroll.sol");

module.exports = function(deployer) {
deployer.deploy(SimpleStorage);
deployer.deploy(Payroll);
};

步骤2:在test目录下新建payroll.js文件

var Payroll = artifacts.require("./Payroll.sol");

// 测试删除员工接口
contract("Payroll-removeEmployee", function(accounts) {
var removeAddress = 0x1238420F4922DB63eA2392194760348ED4Fe4DEF;
it("...Remove Employee removeAddress success.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee(removeAddress,1);
}).then(function() {
return payrollInstance.removeEmployee(removeAddress);
}).then(function() {
return payrollInstance.employeeIsExist.call(removeAddress);
}).then(function(isExist) {
assert.equal(isExist, false, "Remove Employee removeAddress success.");
});
});

});

//测试新增员工接口
contract("Payroll-addEmployee", function(accounts) {
var newAddress = 0x1238420F4922DB63eA2392194760348ED4Fe4ABC;
it("...Add Employee newAddress success.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee(newAddress,1,{from: accounts[0]});
}).then(function() {
return payrollInstance.employeeIsExist.call(newAddress);
}).then(function(isExist) {
assert.equal(isExist, true, "Add Employee newAddress success.");
});
});
});

//测试新增员工异常
contract("Payroll-addEmployee-catchError", function(accounts) {
var newAddress = 0x1238420F4922DB63eA2392194760348ED4Fe4ABC;
it("...Add Employee newAddress success.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee(newAddress,1,{from: accounts[0]});
}).then(function() {
return payrollInstance.addEmployee(newAddress,2,{from: accounts[0]});
}).catch(function(error) {
console.log(error.toString());
assert(error.toString().includes('invalid'), "addEmployee");

});
});
});

//测试删除员工异常
contract("Payroll-removeEmployee-catchError", function(accounts) {
var removeAddress = 0x1238420F4922DB63eA2392194760348ED4Fe4DEF;
it("...Remove Employee removeAddress success.", function() {
return Payroll.deployed().then(function(instance) {
return payrollInstance.removeEmployee(removeAddress);
}).catch(function(error) {
console.log(error.toString());
assert(error.toString().includes('invalid'), "removeEmployee");
});
});
});
*/


/******
题目3:
需要对原来的接口做一定的变化,getPaid()返回转帐数目,以便和收到数目做对比。
遗留学习点:如何切换用户地址以及获得某地址下的账目数。

步骤1:修改Payroll.sol下的getPaid函数:
function getPaid() employeeExist(msg.sender) returns (uint) {
var employee = employees[msg.sender];

uint nextPayDay = employee.lastPayDay + payDuration;
assert(nextPayDay < now);

employee.lastPayDay = nextPayDay;
employee.id.transfer(employee.salary);
return employee.salary;
}

步骤2:在test目录下的payroll.js文件增加:
contract("Payroll-getPaid-MsgSender", function(accounts) {
it("...getPaid success.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee(accounts[0],1);
}).then(function() {
setTimeout(function(){
return payrollInstance.getPaid();
},11000);
}).then(function(getsalary) {
console.log(getsalary);
assert.equal(getsalary > 0, true, "getPaid success.");
});
});
});

contract("Payroll-getPaid-notMsgSender", function(accounts) {
it("...getPaid success.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee(accounts[1],1);
}).then(function() {
setTimeout(function(){
return payrollInstance.getPaid();
},11000);
}).then(function(getsalary) {
assert.equal(getsalary > 0, true, "getPaid success.");
});
});
});
*/

3 changes: 3 additions & 0 deletions Lesson4/orgin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约 第四课

这里是每一课的初始代码,有需要的同学可以参考
1 change: 1 addition & 0 deletions Lesson4/orgin/payroll.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 13 additions & 0 deletions Lesson5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 硅谷live以太坊智能合约频道官方地址

### 第五课

目录结构
<br/>|
<br/>|--orgin 课程初始代码
<br/>|
<br/>|--assignment 课程作业提交代码
<br/>

### 本节知识点

2 changes: 2 additions & 0 deletions Lesson5/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 硅谷live以太坊智能合约 第五课作业
这里是同学提交作业的目录
Binary file added Lesson5/assignment/src.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions Lesson5/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*作业请提交在这个目录下*/
Loading