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

74_李想_第五次作业 #523

Open
wants to merge 22 commits into
base: 74-李想
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
40 changes: 40 additions & 0 deletions Lesson2/assignment/gas_cost.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
### calculateRunway cost

- employee 1
+ transaction cost 22966 gas
+ execution cost 1694 gas
- employee 2
+ transaction cost 23747 gas
+ execution cost 2475 gas
- employee 3
+ transaction cost 24528 gas
+ execution cost 3256 gas
- employee 4
+ transaction cost 25309 gas
+ execution cost 4037 gas
- employee 5
+ transaction cost 25309 gas
+ execution cost 4037 gas
- employee 6
+ transaction cost 26090 gas
+ execution cost 4818 gas
- employee 7
+ transaction cost 26871 gas
+ execution cost 5599 gas
- employee 8
+ transaction cost 27652 gas
+ execution cost 6380 gas
- employee 9
+ transaction cost 28433 gas
+ execution cost 7161 gas
- employee 10
+ transaction cost 29214 gas
+ execution cost 7942 gas

### why

calculateRunway每次都循环一次employees array来计算totalSalay, employee的人数越多消耗的gas越多。

### 优化

每次加入或者删除employee的时候直接更新totalSalary,就免去了calculaterunway中的遍历。
95 changes: 94 additions & 1 deletion Lesson2/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,94 @@
/*作业请提交在这个目录下*/
pragma solidity ^0.4.14;

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

uint constant payDuration = 10 seconds;
uint totalSalary;

address owner;
Employee[] employees;

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

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

function _findEmployee(address employeeId) private returns (Employee, uint) {
for (uint i; i < employees.length; i++) {
if (employees[i].id == employeeId) {
return (employees[i], i);
}
}
}

function addEmployee(address employeeId, uint salary) {
require(msg.sender == owner);

var (employee, index) = _findEmployee(employeeId);

assert(employee.id == 0x0);

totalSalary += salary;
employees.push(Employee(employeeId, salary, now));
}

function removeEmployee(address employeeId) {
require(msg.sender == owner);
var (employee, index) = _findEmployee(employeeId);
assert(employee.id != 0x0);

_partialPaid(employee);
totalSalary -= employee.salary;
delete employees[index];
employees[index] = employees[employees.length-1];
employees.length -= 1;
}

function updateEmployee(address employeeId, uint salary) {
require(msg.sender == owner);
var (employee, index) = _findEmployee(employeeId);
assert(employee.id != 0x0);

_partialPaid(employees[index]);
totalSalary -= employee.salary;
totalSalary += salary;
employees[index].salary = salary;
employees[index].lastPayday = now;
}

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

function calculateRunway() returns (uint) {
assert(totalSalary != 0x0);
return this.balance / totalSalary;
}

function hasEnoughFund() returns (bool) {
if (totalSalary == 0x0) {
return true;
}
return calculateRunway() > 0;
}

function getPaid() {
var (employee, index) = _findEmployee(msg.sender);
assert(employee.id != 0x0);

uint nextPayday = employee.lastPayday + payDuration;
assert(nextPayday < now);

employee.lastPayday = nextPayday;
employee.id.transfer(employee.salary);
}
}
95 changes: 0 additions & 95 deletions Lesson2/hw-2.sol

This file was deleted.

Binary file added Lesson3/assignment/screenshots/addEmployee.png
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/screenshots/addFund.png
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/screenshots/checkEmployee.png
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/screenshots/getPaid.png
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/screenshots/hasEnoughFund.png
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/screenshots/removeEmployee.png
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/screenshots/updateEmployee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading