-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNodeapi.js
More file actions
135 lines (127 loc) · 4.5 KB
/
Nodeapi.js
File metadata and controls
135 lines (127 loc) · 4.5 KB
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
/**
* Node Trinity source code
* See LICENCE file at the top of the source tree
*
* ******************************************
*
* nodeapi.service.js
* Node service business logic
*
* ******************************************
*
* Authors: K. Zhidanov, A. Prudanov, M. Vasil'ev
*/
const Transport = require('./Transport').Tip;
const ExplorerService = require('./explorer.service').ExplorerService;
const NodeapiService = require('./nodeapi.service').NodeapiService;
class NodeAPI {
constructor(config, db) {
this.db = db;
this.config = config;
this.explorerService = new ExplorerService(this.db);
this.nodeapiService = new NodeapiService(this.db);
this.transport = new Transport(this.config.id, 'nodeAPI');
if(config.enable_post_tx)
this.transport.on('post_tx', this.on_post_tx.bind(this));
this.transport.on('get_active_balance', this.on_get_active_balance.bind(this));
this.transport.on('get_txpool', this.on_get_txpool.bind(this));
this.transport.on('peek', this.on_peek.bind(this));
this.transport.on('snapshot', this.on_get_snapshot.bind(this));
this.transport.on('snapshot_chunk', this.on_get_snapshot_chunk.bind(this));
this.transport.on('get_macroblock', this.on_get_macroblock.bind(this));
this.transport.on('get_chain_start', this.on_get_chain_start.bind(this));
}
async on_post_tx(msg) {
let tx = msg.data;
let res = await this.nodeapiService.post_tx(tx);
if(res.err === 0)
this.transport.broadcast("post_tx", tx);
return res;
};
// TODO: Call Explorer Service method instead
async on_get_active_balance(msg) {
let data = msg.data;
let res = await this.db.get_balance_all(data.id);
return res;
};
async on_get_txpool(msg) {
return await this.db.get_pending();
};
async on_get_macroblock(msg) {
let hash;
if (msg.data) {
hash = msg.data.hash;
}
console.debug(`on get_macroblock ${hash}`);
if (hash) {
let succ = (await this.db.get_kblock(hash))[0];
console.trace(`succ = ${JSON.stringify(succ)}`);
let pred = await this.db.get_macroblock(succ.link);
return {candidate: succ, macroblock: pred};
}
};
//return start of chain macroblock (actual for fastsync nodes)
async on_get_chain_start(){
console.debug(`on get_start_chain`);
let res = await this.db.get_chain_start_macroblock();
return res;
};
//returns: hash, size_bytes
async on_get_snapshot(msg){
let height;
if (msg.data) {
height = msg.data.height;
}
if(height) {
let snapshot_info = await this.db.get_snapshot_before(height);
console.debug(`on get_snapshot before height ${height}`);
if (snapshot_info) {
return snapshot_info;
} else {
console.warn(`Not found snapshot before ${height}`);
}
} else
return undefined;
};
//returns: binary data
async on_get_snapshot_chunk(msg) {
let hash;
let chunk_no;
let chunk_size_bytes;
if (msg.data) {
hash = msg.data.hash;
chunk_no = msg.data.chunk_no;
chunk_size_bytes = msg.data.chunk_size_bytes;
} else {
console.warn(`get_snapshot_chunk - undefined msg.data`);
return undefined;
}
console.debug(`on get_snapshot_chunk hash=${hash} chunk_no=${chunk_no} chunk_size_bytes=${chunk_size_bytes}`);
let snapshot_chunk = await this.db.get_snapshot_chunk(hash, chunk_no, chunk_size_bytes);
if (snapshot_chunk) {
return snapshot_chunk;
} else {
console.warn(`Not found snapshot_chunk`);
}
};
async on_peek(msg) {
let min, max;
if (msg.data) {
min = msg.data.min;
max = msg.data.max;
}
console.debug('on peek', min, max);
if (min !== undefined) {
if (max !== undefined) {
let lim = min + Math.min(max - min, this.config.peek_limit - 1);
return this.db.peek_range(min, lim);
} else {
return this.db.peek_range(min, min + this.config.peek_limit - 1)
}
} else {
console.silly('peeking tail');
return this.db.peek_tail();
}
};
}
module.exports.NodeAPI = NodeAPI;