-
Notifications
You must be signed in to change notification settings - Fork 2
/
testdbserver.js
321 lines (255 loc) · 11.4 KB
/
testdbserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const cors = require('cors');
const fs = require('fs');
const fsPromises = require('fs').promises;
var database = {};
// Basically this will be a database service until we put this on ipfs or something.
var pullRequestsVoteCloseHistory = [];
var pullRequestsVoteMergeHistory = [];
// The object representing authorized repos and contributors.
var pullRequestsDB = {
'default/default': ['vote_code']
};
var schema = buildSchema(`
type PullRequest {
vote_code: [String]
}
type Query {
getPullRequestFromHistory(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String): String,
createRepo(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String): String,
createTokenSupply(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, tokens: String): String,
setTSrepoHead(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, head: String): String,
setQuorum(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, quorum: String): String,
newPullRequest(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, vote_status: String): String,
setContributorVotedTokens(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, tokens: String): String,
addToTotalVotedYesTokens(owner: String, repo: String, defaultHash: String, contributor_id: String, side: String, tokens: String): String,
}
`);
async function getGithubUser () {
const data = await fsPromises.readFile('/usr/src/app/.config.json')
.catch((err) => console.error('Failed to read file', err));
let json = JSON.parse(data);
let user = json.github.user;
if (user === undefined) {
throw new Error('Failed to load Github user ' + user);
} else {
console.log('Successfully read Github ' + user);
}
return user;
}
var root = {
createRepo: async (args) => {
const user = await getGithubUser();
database[args.owner + '/' + args.repo] = {
//'head': head,//'c20e46a4e3efcd408ef132872238144ea34f7ae5',
'tokenSupply': 1_000_000,
'openPullRequest': '',
'contributors': {
'mary': 500_001,
'am': 15_000,
'jc': 10_000,
'pc': 75_000,
'mb': 75_000,
'np': 5_000,
'nn': 100_000,
'jp': 50_000,
'ts': 50_000,
'af': 10_000,
'ds': 75_000,
'ri': 1_000
},
'pullRequests': {
}
};
database[args.owner + '/' + args.repo].contributors[user] = 33_999;
database[args.owner + '/' + args.repo].quorum = 0.50;
// For testing.
fs.writeFileSync('testing/special/turbo-src-test-database-create-repo.json', JSON.stringify(database, null, 2) , 'utf-8');
},
createTokenSupply: function (args) {
database[args.owner + '/' + args.repo].tokenSupply = Number(args.tokens);
// For testing.
fs.writeFileSync('testing/special/turbo-src-test-database-create-token-supply.json', JSON.stringify(database, null, 2) , 'utf-8');
},
setQuorum: function (args) {
database[args.owner + '/' + args.repo].quorum = Number(args.quorum);
fs.writeFileSync('testing/special/turbo-src-test-database-set-quorum.json', JSON.stringify(database, null, 2) , 'utf-8');
},
newPullRequest: function (args) {
const defaultHash = args.defaultHash;
const tokens = database[args.owner + '/' + args.repo].contributors[args.contributor_id];
const vote_code = args.vote_status + '%' + args.repo + '%' + args.contributor_id + '%' + tokens + '%' + args.side;
pullRequestsDB[args.defaultHash] = [vote_code];
database[args.owner + '/' + args.repo].pullRequests[defaultHash] = {};
database[args.owner + '/' + args.repo].pullRequests[defaultHash].pullRequestStatus = 'open';
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedTokens = 0;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedYesTokens = 0;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedNoTokens = 0;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].votedTokens = {};
fs.writeFileSync('testing/special/turbo-src-test-database-new-pull-request.json', JSON.stringify(database, null, 2) , 'utf-8');
},
getContributorTokens: function (database, args) {
tokens = database[args.owner + '/' + args.repo].contributors[args.contributor_id];
return tokens;
},
setContributorVotedTokens: async function (args) {
const defaultHash = (args.defaultHash);
database[args.owner + '/' + args.repo].pullRequests[defaultHash].votedTokens[args.contributor_id] = {
tokens: Number(args.tokens),
side: args.side
};
fs.writeFileSync('testing/special/turbo-src-test-database-set-contributor-voted-tokens.json', JSON.stringify(database, null, 2) , 'utf-8');
},
setVoteSide: function (database, args) {
const defaultHash = (args.defaultHash);
database[args.owner + '/' + args.repo].pullRequests[defaultHash].votedTokens[args.contributor_id].side = args.side;
return database;
},
// Soon to be tdefaultHash. Right now it's the HEAD of the
// pull request fork on Github.
setTSrepoHead: function (args) {
database[args.owner + '/' + args.repo].head = args.head;
fs.writeFileSync('testing/special/turbo-src-test-database-set-ts-repo-head.json', JSON.stringify(database, null, 2) , 'utf-8');
return database;
},
setPullRequestStatus: function (database, args, status) {
const defaultHash = (args.defaultHash);
database[args.owner + '/' + args.repo].pullRequests[defaultHash]['pullRequestStatus'] = status;
return database;
},
getTSrepoHead: function (database, args) {
const head = database[args.owner + '/' + args.repo].head;
return head;
},
getOpenPullRequest: function (database, args) {
const openPullRequest = database[args.owner + '/' + args.repo].openPullRequest;
return openPullRequest;
},
setOpenPullRequest: function (database, args, openPullRequest) {
database[args.owner + '/' + args.repo].openPullRequest = openPullRequest;
return database;
},
getTSpullRequest: function (database, args) {
const defaultHash = (args.defaultHash);
const pullRequest = database[args.owner + '/' + args.repo].pullRequests[defaultHash];
return pullRequest;
},
getAllTSpullRequests: function (database, args) {
const allTSpullRequests = database[args.owner + '/' + args.repo].pullRequests;
return allTSpullRequests;
},
deleteTSpullRequest: function (database, args) {
const defaultHash = (args.defaultHash);
delete database[args.owner + '/' + args.repo].pullRequests[defaultHash];
return database;
},
getContributorVotedTokens: function (database, args) {
const defaultHash = (args.defaultHash);
const votedTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].votedTokens[args.contributor_id];
return votedTokens;
},
getAllVotedTokens: function (database, args) {
const defaultHash = (args.defaultHash);
const allVotedTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].votedTokens;
return allVotedTokens;
},
getTokenSupply: function (database, args) {
const supply = database[args.owner + '/' + args.repo].tokenSupply;
return supply;
},
getQuorum: function (database, args) {
const quorum = database[args.owner + '/' + args.repo].quorum;
return quorum;
},
getTotalVotedTokens: function (database, args) {
const defaultHash = (args.defaultHash);
const totalVotedTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedTokens;
return totalVotedTokens;
},
getTotalVotedYesTokens: function (database, args) {
const defaultHash = (args.defaultHash);
const totalVotedYesTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedYesTokens;
return totalVotedYesTokens;
},
getPullRequestFromHistory: function (pullRequestsDB, args) {
var pullRequest = pullRequestsDB[args.defaultHash];
return pullRequest;
},
getTotalVotedNoTokens: function (database, args) {
const defaultHash = (args.defaultHash);
const totalVotedNoTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedNoTokens;
return totalVotedNoTokens;
},
getRepoStatus: function (database, args) {
return Object.keys(database).includes(args.repo_id);
},
checkContributor: function (database, args) {
const contributors = database[args.repo_id].contributors;
const contributor_exists = Object.keys(contributors).includes(args.contributor_id);
return contributor_exists;
},
addToTotalVotedTokens: function (database, args, tokens) {
const defaultHash = (args.defaultHash);
const totalVotedTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedTokens;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedTokens = totalVotedTokens + tokens;
return database;
},
addToTotalVotedYesTokens: function (args) {
const defaultHash = (args.defaultHash);
const totalVotedYesTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedYesTokens;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedYesTokens = totalVotedYesTokens + Number(args.tokens);
fs.writeFileSync('testing/special/turbo-src-test-database-add-voted-yes.json', JSON.stringify(database, null, 2) , 'utf-8');
},
addToTotalVotedNoTokens: function (database, args, tokens) {
const defaultHash = (args.defaultHash);
const totalVotedNoTokens = database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedNoTokens;
database[args.owner + '/' + args.repo].pullRequests[defaultHash].totalVotedNoTokens = totalVotedNoTokens + tokens;
return database;
},
addToMergePullRequestHistory: function (pullRequestVoteMergeHistory, args) {
const defaultHash = (args.defaultHash);
pullRequestVoteMergeHistory.push(defaultHash);
return pullRequestVoteMergeHistory;
},
addToRejectPullRequestHistory: function (pullRequestVoteCloseHistory, args) {
const defaultHash = (args.defaultHash);
pullRequestVoteCloseHistory.push(defaultHash);
return pullRequestVoteCloseHistory;
},
checkMergePullRequestHistory: function (pullRequestVoteMergeHistory, args) {
const defaultHash = (args.defaultHash);
return pullRequestVoteMergeHistory.includes(defaultHash);
},
checkRejectPullRequestHistory: function (pullRequestVoteCloseHistory, args) {
const defaultHash = (args.defaultHash);
return pullRequestVoteCloseHistory.includes(defaultHash);
}
};
var app = express();
//app.use(loggingMiddleware);
app.use(cors());
app.use(function (req, res, next) {
let originalSend = res.send;
res.send = function (data) {
console.log(data + '\n');
originalSend.apply(res, Array.from(arguments));
};
next();
});
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
}));
var way = false;
//if (way === true) {
// console.log("true");
// return true;
// } else {
// console.log("false");
// return false;
//}
app.listen(8081);
console.log('Running a GraphQL API server at container\'s localhost:8081/graphql');