-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
81 lines (39 loc) · 2.06 KB
/
functions.js
File metadata and controls
81 lines (39 loc) · 2.06 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
import { DATABASE, CONFIGS } from "./index.js"
export let returnBlocksDataForPod = async(data,connection) => {
// Input data is {fromHeight}
// Output data is {n:{block,afpForBlock},n+1:{block,afpForBlock},...,n+x:{block,afpForBlock}}
let responseStructure = {}
for(let i = 0 ; i < 500 ; i++){
let relativeHeight = data.fromHeight + i
let blockIdByRelativeHeight = await DATABASE.get('RID:'+relativeHeight).catch(()=>null)
if(blockIdByRelativeHeight){
let block = await DATABASE.get(blockIdByRelativeHeight).catch(()=>null)
let afpForBlock = await DATABASE.get('AFP:'+blockIdByRelativeHeight).catch(()=>null)
if(block && afpForBlock) {
responseStructure[`HEIGHT:${relativeHeight}`] = {block, afpForBlock}
} else break
} else break
}
connection.sendUTF(JSON.stringify(responseStructure))
}
export let returnBlocksRange = async(data,connection) => {
// We need to send range of blocks from <heightThatUserHave+1> to <heightThatUserHave+499> or less(limit is up to 500 blocks). Also, send the AFP for latest block
// Also, the response structure is {blocks:[],afpForLatest}
let responseStructure = {
blocks:[],
afpForLatest:{}
}
for(let i = 1 ; i < 500 ; i++){
let blockIdToFind = data.epochIndex+':'+CONFIGS.SEQUENCER_PUBKEY+':'+(data.hasUntilHeight+i)
let blockIdToFindAfp = data.epochIndex+':'+CONFIGS.SEQUENCER_PUBKEY+':'+(data.hasUntilHeight+i+1)
let block = await DATABASE.get(blockIdToFind).catch(()=>null)
let afpForBlock = await DATABASE.get('AFP:'+blockIdToFindAfp).catch(()=>null)
if(block && afpForBlock){
responseStructure.blocks.push(block)
responseStructure.afpForLatest = afpForBlock
}else if(block && data.sendWithNoAfp && data.sendWithNoAfp.index === block.index){
responseStructure.blocks.push(block)
}else break
}
connection.sendUTF(JSON.stringify(responseStructure))
}