Skip to content

Commit dcbb5b2

Browse files
committed
Fixed tests on Job wrapper contract
1 parent 1dbbfee commit dcbb5b2

13 files changed

+176
-371
lines changed

contracts/agent/Agent.sol

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import "zeppelin-solidity/contracts/ownership/Ownable.sol";
66
contract Agent is AgentInterface, Ownable {
77

88
bytes[] public packets;
9-
MarketJobInterface public job;
109

1110
function sendPacket(address target, bytes packet) external onlyOwner {
1211
Agent(target).appendPacket(packet);
@@ -21,8 +20,4 @@ contract Agent is AgentInterface, Ownable {
2120
return packets[id];
2221
}
2322

24-
function setJob(MarketJob _job) external returns (address) {
25-
job = _job;
26-
}
27-
2823
}

contracts/agent/AgentInterface.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
pragma solidity ^0.4.18;
22

3-
import "../market/MarketJob.sol";
43

54
contract AgentInterface {
65

76
function sendPacket(address target, bytes packet) external;
87
function appendPacket(bytes packet) external;
98
function getPacket(uint id) external constant returns (bytes);
10-
function setJob(MarketJob _job) external returns (address);
119

1210
}

contracts/market/Job.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.4.18;
22

3-
import './JobValidator.sol';
3+
import "./JobValidator.sol";
44
/**
55
* @title The wrapped Job contract
66
*/
@@ -49,7 +49,6 @@ contract Job is JobValidator {
4949
function setResult(bytes32 _result) public returns (bool) {
5050
require(msg.sender == payee);
5151
require(descriptor.length > 0);
52-
require(result.length == 0);
5352

5453
end = now;
5554

contracts/market/JobValidator.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.4.18;
22

3-
import './JobInterface.sol';
3+
import "./JobInterface.sol";
44

55
/**
66
* @title The Job Validator contract

contracts/market/MarketJob.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ pragma solidity ^0.4.18;
22

33
import "zeppelin-solidity/contracts/math/SafeMath.sol";
44
import "../tokens/SingularityNetToken.sol";
5-
import "./MarketJobInterface.sol";
65

76

8-
contract MarketJob is MarketJobInterface {
7+
contract MarketJob {
98
using SafeMath for uint256;
109

1110
SingularityNetToken public token;

job.sol

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
pragma solidity ^0.4.13;
2+
3+
contract JobInterface {
4+
/**
5+
* @dev Starting hash of the job
6+
*/
7+
bytes32 public descriptor;
8+
9+
/**
10+
* @dev Result hash of the job
11+
*/
12+
bytes32 public result;
13+
14+
/**
15+
* @dev Start time timestamp
16+
*/
17+
uint256 public start;
18+
19+
/**
20+
* @dev End time timestamp
21+
*/
22+
uint256 public end;
23+
24+
/**
25+
* @dev An agent who pays the job.
26+
*/
27+
address public payer;
28+
29+
/**
30+
* @dev An agent to whom the job is paid.
31+
*/
32+
address public payee;
33+
34+
/**
35+
* @dev Broadcast new hash as job result.
36+
*/
37+
event Result(bytes32 result);
38+
}
39+
40+
contract JobValidator is JobInterface {
41+
42+
/**
43+
* @dev Reward for validator in AGI.
44+
*/
45+
address public validator;
46+
47+
/**
48+
* @dev Validation status
49+
* 0 == pending && 1 == accepted && 2 == rejected
50+
*/
51+
address public status = 0;
52+
53+
/**
54+
* @dev Reward for validator in AGI.
55+
*/
56+
uint256 public reward;
57+
58+
/**
59+
* @dev Completed event to notice the validators pool
60+
*/
61+
event Completed();
62+
63+
/**
64+
* @dev Approved event to notice the parties
65+
*/
66+
event Accepted();
67+
68+
/**
69+
* @dev Rejected event to notice the parties
70+
*/
71+
event Rejected();
72+
73+
74+
/**
75+
* @dev Confirm job result
76+
*/
77+
function accept() public {
78+
validator = msg.sender;
79+
status = 1;
80+
81+
Accepted();
82+
}
83+
84+
/**
85+
* @dev Reject job result
86+
*/
87+
function reject() public {
88+
validator = msg.sender;
89+
status = 2;
90+
91+
Rejected();
92+
}
93+
94+
}
95+
96+
contract Job is JobValidator {
97+
98+
/**
99+
* @dev Kovan AGI token.
100+
* https://kovan.etherscan.io/token/0x3b226ff6aad7851d3263e53cb7688d13a07f6e81#readContract
101+
* ERC20Basic public constant AGI = ERC20Basic(0x3b226ff6aad7851d3263e53cb7688d13a07f6e81);
102+
*/
103+
104+
/**
105+
* @dev Cost in AGI for the exection of the job
106+
* (10**8 cogs === 1 AGI)
107+
*/
108+
uint256 public cost;
109+
110+
/**
111+
* @dev Started event
112+
*/
113+
event Started(address indexed payer, address indexed payee, bytes32 descriptor, address job);
114+
115+
/**
116+
* @dev Job constructor.
117+
* @param _payer An agent who pays the job.
118+
* @param _payee An agent to whom the job is paid.
119+
* @param _cost Cost in AGI
120+
*/
121+
function Job(address _payer, address _payee, bytes32 _descriptor, uint256 _cost, uint256 _reward) public {
122+
123+
payer = _payer;
124+
payee = _payee;
125+
descriptor = _descriptor;
126+
cost = _cost;
127+
reward = _reward;
128+
129+
start = now;
130+
131+
Started(payer, payee, descriptor, address(this));
132+
}
133+
134+
/**
135+
* @dev Set result of this Job
136+
* @param _result Result data hash
137+
*/
138+
function setResult(bytes32 _result) public returns (bool) {
139+
require(msg.sender == payee);
140+
require(descriptor.length > 0);
141+
142+
end = now;
143+
144+
Result(_result);
145+
result = _result;
146+
147+
Completed();
148+
149+
return true;
150+
}
151+
152+
153+
154+
}
155+

migrations/2_deploy_contracts.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
const fs = require('fs')
22

3-
const AgentRegistry = artifacts.require('registries/AgentRegistry.sol')
43
const Job = artifacts.require('market/Job.sol')
54
/* const AgentFactory = artifacts.require('agent/AgentFactory.sol')
5+
const AgentRegistry = artifacts.require('registries/AgentRegistry.sol')
66
const MarketJobFactory = artifacts.require('market/MarketJobFactory.sol')
77
const SingularityNetToken = artifacts.require('tokens/SingularityNetToken.sol')
88
const TokenVestingFactory = artifacts.require('tokens/TokenVestingFactory.sol') */
99

1010
module.exports = function(deployer, network, accounts) {
1111
deployer.deploy([
12-
Job,
13-
AgentRegistry
12+
Job
1413
]).then(() => {
1514
const fileName = "addresses.json"
1615
const content = {
1716
//AgentFactory: AgentFactory.address,
1817
Job: Job.address,
19-
AgentRegistry: AgentRegistry.address,
18+
//AgentRegistry: AgentRegistry.address,
2019
//MarketJobFactory: MarketJobFactory.address,
2120
//SingularityNetToken: SingularityNetToken.address,
2221
//TokenVestingFactory: TokenVestingFactory.address

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"compile": "node_modules/.bin/truffle compile",
1111
"deploy": "node_modules/.bin/truffle migrate",
12-
"test": "node_modules/.bin/truffle test"
12+
"test": "rm -rf build && node_modules/.bin/truffle test"
1313
},
1414
"author": "tiero",
1515
"license": "MIT",

test/TestJob.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
const Job = artifacts.require('market/Job.sol')
22

33

4-
contract('Job', function ([payer, payee]) {
4+
contract('Job', function ([payee, payer]) {
55

66
beforeEach(async () => {
7-
this.job = await Job.new()
8-
})
9-
10-
it('JOB STARTED', async () => {
117
const jobDescriptor = "0x0"
12-
13-
const { logs } = await this.job.setJobStarted(payer, payee, jobDescriptor)
14-
const found = logs.find(e => e.event === 'JobStarted')
15-
assert.strictEqual(found.event, 'JobStarted', 'Job not started')
8+
this.job = await Job.new(payer, payee, jobDescriptor, 800, 0)
9+
console.log(this.job)
1610
})
1711

18-
it('JOB COMPLETED', async () => {
19-
const jobDescriptor = "0x0"
20-
const jobResult = "0x0102"
2112

22-
await this.job.setJobStarted(payer, payee, jobDescriptor)
13+
it('JOB COMPLETED - set result', async () => {
14+
const jobResult = "0x101"
2315
//Complete jobs
24-
const { logs } = await this.job.setJobCompleted(jobResult, {from:payee})
25-
const found = logs.find(e => e.event === 'JobCompleted')
26-
assert.strictEqual(found.event, 'JobCompleted', 'Job not completed')
16+
const result = await this.job.setResult(jobResult, {from:payee})
17+
18+
const found = result.logs.find(e => e.event === 'Result')
19+
assert.strictEqual(found.event, 'Result', 'Result event not fired')
20+
21+
const found2 = result.logs.find(e => e.event === 'Completed')
22+
assert.strictEqual(found2.event, 'Completed', 'Completed event not fired')
23+
2724
})
2825

2926

0 commit comments

Comments
 (0)