This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
streams.js
277 lines (265 loc) · 8.53 KB
/
streams.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
const Verifier = require('./receiver/StateSyncReqVerifier');
const EncoderUtil = require('../../common/EncoderUtil');
const constants = require('../../common/constants');
const SyncMsgMgmgt = require('../../policy/p2p_messages/sync_messages');
const SyncMsgBuilder = SyncMsgMgmgt.SyncMsgBuilder;
/**
* global methods
* */
/**
* @param {JSON} globalState - a global state the is accessible to all of the stream methods.
* @param {Receiver/Provider} context - access other methods i.e db requests
* @param {Logger} logger
* */
const globalState = {
providerContext : null,
receiverContext : null,
logger : null,
};
/**
* Set the global state, this function will be executed only once
* regardless of the amount of times it is being called.
* */
module.exports.setGlobalState = (state)=>{
if(state.providerContext && globalState.providerContext === null){
globalState.providerContext = state.providerContext;
}
if(state.receiverContext && globalState.receiverContext === null){
globalState.receiverContext = state.receiverContext;
}
if(state.logger && globalState.logger === null){
globalState.logger = state.logger;
}
};
/**
* Actuall streams implementation
* */
// TODO:: lena, verification stream here is the "consensus" this stream is verifying deltas/bytecode.
// TODO:: lena, the 'read' variable this function takes as input is a response object.
// TODO:: lena, read is an array of encoded msgpack bytes, inside this code if you'll look there are 3 lines that decode, and turn it into an actuall using the SyncMsgBuilder.
// TODO:: lena,this verification stream should take the object which is a response of a range of deltas or 1 bytecode, hash it and verify it against whats in ethereum
// TODO:: lena, THERE IS NO NEED to actually go to ethem because that your StateSync (your function that returns all the missing states) already has that data
// TODO:: lena, so it should be passed here, so just to emphesize: there is no need to go to ethereum here, we already have the data for verification.
// TODO:: lena, you will need this stream to have access to that missing states object obviously so the way you do it is with an Action that does everything and returns a callback.
// TODO:: lena, the best example of this is fakeSaveToDb() stream and _fakeFromDbStream() (to see how to trigger a command and callback when done)
/**
* from providerStream => verify (consensus)
* @param {stream} read
* @return {function()}
* */
module.exports.verificationStream = (read)=>{
return _verificationStream(read);
};
/**
* After verificationStream => into db
* @param {stream} read
* @return {function()}
* */
// module.exports.toDbStream = (read)=>{
// return _toDbStream(read);
// };
module.exports.fromDbStream = (read) =>{
return _fromDbStream(read);
};
/**
* parses the data from the requester into a format that core can read
* this is preperation before loading the deltas from the db.
* the data here gets one by one from remote. (i.e for list of request, this is activated for each request)
* @param {stream} read
* @return {function()}
* */
module.exports.requestParserStream = (read) =>{
return _requestParserStream(read);
};
/**
* Taking the result from the Database (done by the provider)
* and parse them into network mode (i.e msgpack etc.)
* */
module.exports.toNetworkParser = (read) =>{
return _toNetworkParse(read);
};
/** Parse the request state sync msg from the reciever before passing the request to the provider stream
* msgpack serialize */
module.exports.toNetworkSyncReqParser = (read)=>{
return _toNetworkSyncReqParser(read);
};
/**
* used by the receiver to store the deltas into db
* After verificationStream => into db
* @param {stream} read
* @return {function()}
* */
module.exports.throughDbStream = (read)=>{
return _throughDbStream(read);
};
function _throughDbStream(read){
return function readble(end,cb){
read(end,(end,data)=>{
if(data != null){
fakeSaveToDb(data,(err, status)=>{
if(!status || err ){
globalState.logger.error("some fake error saving to db ");
throw end;
}else{
cb(end,{status:status, data : data});
}
});
}else{
cb(end,null);
}
});
};
}
function _toNetworkSyncReqParser(read) {
return function readble(end, cb) {
read(end, (end, data) => {
if (data != null) {
cb(end, data.toNetwork());
} else {
cb(end, null);
}
});
};
}
function _fakeParseFromDbToNetwork(dbResult, callback){
//TODO:: add toNetwork() method to all the dbResults.
// parse all to network
if(dbResult.type === constants.CORE_REQUESTS.GetDeltas){
dbResult.msgType = constants.P2P_MESSAGES.SYNC_STATE_RES;
}else if(dbResult.type === constants.CORE_REQUESTS.GetContract){
dbResult.msgType = constants.P2P_MESSAGES.SYNC_BCODE_RES;
}
dbResult = EncoderUtil.encode(JSON.stringify(dbResult));
const parsed = dbResult;
const isError = null;
callback(isError, parsed);
}
// this takes result from the db (done by the provider) and
// returns the result directly into the other peer stream (source)
function _toNetworkParse(read) {
return function readble(end, cb) {
read(end, (end, data)=>{
if (data!=null) {
_fakeParseFromDbToNetwork(data, (err, parsedResult)=>{
if (err) {
cb(err, null);
} else {
cb(end, parsedResult);
}
});
} else {
cb(end, null);
}
});
};
}
function _fakeFromDbStream(syncReqMsg, callback){
// TODO:: create a db call ...
// TODO:: validate that the range < limit here or somewhere else.
let queryType = null;
if(syncReqMsg.type() === constants.P2P_MESSAGES.SYNC_BCODE_REQ){
queryType = constants.CORE_REQUESTS.GetContract;
}else if(syncReqMsg.type() === constants.P2P_MESSAGES.SYNC_STATE_REQ){
queryType = constants.CORE_REQUESTS.GetDeltas;
}else{
// TODO:: handle error
console.log("[-] error in _fakeFromDbStream");
}
globalState.providerContext.dbRequest({
dbQueryType : queryType,
requestMsg : syncReqMsg,
onResponse : (ctxErr,dbResult) =>{
callback(ctxErr,dbResult)
}
});
}
// fake load from the database, this will return the deltas for the requester
function _fromDbStream(read) {
return function readble(end, cb) {
read(end, (end, data)=>{
if (data != null) {
_fakeFromDbStream(data, (err, dbResult)=>{
if (err) {
console.log('error in fakeFromDbStream {%s}',err);
cb(err, null);
} else {
cb(end, dbResult);
}
});
} else {
cb(end, null);
}
});
};
}
// used by _requestParserStream() this should parse the msgs from network
// into something that core can read and load from db
function _fakeRequestParser(data, callback){
let err = null;
//TODO:: validate network input validity
data = EncoderUtil.decode(data);
let parsedData = JSON.parse(data);
parsedData = SyncMsgBuilder.msgReqFromObjNoValidation(parsedData);
if(parsedData === null){
err = 'error building request message';
}
return callback(err, parsedData);
}
function _requestParserStream(read){
return function readble(end, cb) {
read(end, (end, data)=>{
if (data != null) {
_fakeRequestParser(data, (err, parsed)=>{
if (err) {
cb(true, null);
} else {
cb(end, parsed);
}
});
} else {
cb(end, null);
}
});
};
}
// used by _toDbStream()
// save response (after validation) to db
// the objects are either SYNC_STATE_RES or SYNC_BCODE_RES
function fakeSaveToDb(msgObj, callback) {
if(msgObj === null || msgObj === undefined){
let err = "error saving to db";
globalState.logger.error(err);
return callback(err);
}
globalState.receiverContext.dbWrite({
data : msgObj,
callback : (err,status)=>{
callback(err,status);
}
});
}
function _verificationStream(read) {
return function readble(end, cb) {
read(end, (end, data)=>{
if (data !=null) {
data = EncoderUtil.decode(data);
data = JSON.parse(data);
data = SyncMsgBuilder.msgResFromObjNoValidation(data);
if(data == null){
return cb(true, null);
}
// TODO:: placeholder for future ethereum veirfier.
// verify the data
new Verifier().verify(data, (isOk)=>{
if (isOk) {
return cb(end, data);
} else {
return cb(true, null);
}
});
} else {
return cb(end, null);
}
});
};
}