This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-store.ts
194 lines (168 loc) · 6.81 KB
/
segment-store.ts
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
import {
SegmentState,
chain,
Block,
} from './pb'
import { IDb } from '@aperturerobotics/objstore'
import { Prefixer } from '@aperturerobotics/objstore'
import { storageref, storageRefEquals } from '@aperturerobotics/storageref'
import { IStrategy } from './encryption/encryption'
import { ObjectStore } from '@aperturerobotics/objstore'
import { Segment } from './segment'
import {
FollowBlockRef,
GetBlock,
FollowBlockHeaderRef,
} from './block'
import * as Collections from 'typescript-collections'
import * as uuidv4 from 'uuid/v4'
import * as toBuffer from 'typedarray-to-buffer'
// SegmentQueue is a priority queue for rewinding segments.
export class SegmentQueue extends Collections.PriorityQueue<Segment> {
/**
* Creates an empty segment queue.
*/
constructor() {
super((a: Segment, b: Segment) => this.compareElementsLess(a, b))
}
/*
* compareElementsLess is the function used to compare two element priorities.
* Must return a negative integer, zero, or a positive integer as the first argument
* is less than, equal to, or greater than the second.
*/
private compareElementsLess(a: Segment, b: Segment): number {
if (!a.tailBlockRound || !b.tailBlockRound) {
return 0
}
return ((a.tailBlockRound.height as number) || 0) -
((b.tailBlockRound.height as number) || 0)
}
}
// SegmentStore stores segments in memory and loads them out of the database.
export class SegmentStore {
// segmentMap stores segments loaded into memory by id.
private segmentMap: { [id: string]: Segment } = {}
// db is the database.
private db: IDb
// segmentDb is the segment database.
private segmentDb: IDb
// blockDb is the block database.
private blockDb: IDb
// objStore is the object store.
private objStore: ObjectStore
// segmentQueue is the segment rewinding queue.
private segmentQueue: SegmentQueue
constructor(dbm: IDb, objStore: ObjectStore) {
this.db = dbm
this.segmentDb = new Prefixer(dbm, "/segments")
this.blockDb = new Prefixer(dbm, "/blocks")
this.objStore = objStore
this.segmentQueue = new SegmentQueue()
}
// getSegmentByID gets a segment instance from the db or creates it.
public async getSegmentByID(id: string): Promise<Segment> {
if (this.segmentMap.hasOwnProperty(id)) {
return this.segmentMap[id]
}
let seg = new Segment(this.objStore, this.segmentDb)
seg.id = id
seg.status = chain.SegmentStatus.SegmentStatus_DISJOINTED
await seg.readState()
this.segmentMap[seg.id] = seg
return seg
}
// getDigestKey returns the key for the given digest.
public getDigestKey(hash: Uint8Array): string {
let hashHex = toBuffer(hash).toString('hex')
return "/" + hashHex
}
// newSegment builds a new segment for the block.
public async newSegment(blk: Block, blkRef: storageref.IStorageRef): Promise<Segment> {
let blkHeader = blk.getHeader()
if (!blkHeader) {
throw new Error('newSegment: block header must be attached to block')
}
let seg = new Segment(this.objStore, this.segmentDb)
seg.id = uuidv4()
seg.headBlock = blkRef
seg.tailBlock = blkRef
seg.status = chain.SegmentStatus.SegmentStatus_DISJOINTED
seg.tailBlockRound = blkHeader.roundInfo
await seg.writeState()
this.segmentMap[seg.id] = seg
return seg
}
// listSegments lists all known segments in the database.
public async listSegments(): Promise<string[]> {
let keys = await this.segmentDb.listKeys("/")
let res: string[] = []
for (let key of keys) {
let keyPts = key.split('/')
res.push(keyPts[keyPts.length - 1])
}
return res
}
// rewindOnce rewinds the highest priority segment once.
// The segment that was rewound is returned.
public async rewindOnce(encStrat: IStrategy): Promise<Segment | null> {
let nseg = this.segmentQueue.dequeue()
if (!nseg) {
return null
}
await this.rewindSegment(nseg, encStrat)
return nseg
}
// rewindSegment rewinds a particular segment once.
private async rewindSegment(seg: Segment, encStrat: IStrategy): Promise<void> {
let tailRef: storageref.IStorageRef | null = seg.tailBlock || null
let tailBlk = await FollowBlockRef(tailRef, encStrat, this.objStore)
let tailBlkObj = await GetBlock(encStrat, this.blockDb, this.objStore, tailRef)
let tailBlkHeaderRef: storageref.IStorageRef | null = tailBlk.blockHeaderRef || null
let tailBlkHeader = await FollowBlockHeaderRef(tailBlkHeaderRef, encStrat, this.objStore)
let prevBlockRef: storageref.IStorageRef | null = tailBlkHeader.lastBlockRef || null
let prevBlk = await GetBlock(encStrat, this.blockDb, this.objStore, prevBlockRef)
let prevSegID = prevBlk.getSegmentID()
if (prevSegID && prevSegID.length) {
let prevSeg = await this.getSegmentByID(prevSegID)
if (prevSeg === seg) {
throw new Error('previous segment resolved to same segment')
}
return prevSeg.appendSegment(this.blockDb, encStrat, seg)
}
prevBlk.setSegmentID(seg.id)
prevBlk.setNextBlock(tailBlkObj.getBlockRef())
await prevBlk.writeState()
seg.tailBlock = prevBlockRef
let prevBlkHeader = prevBlk.getHeader()
if (!prevBlkHeader) {
throw new Error('previous block header not known')
}
seg.tailBlockRound = prevBlkHeader.roundInfo
try {
await prevBlk.validateChild(tailBlkObj)
} catch (e) {
seg.status = chain.SegmentStatus.SegmentStatus_INVALID
}
if (seg.status === chain.SegmentStatus.SegmentStatus_DISJOINTED) {
let prevBlkRound = prevBlkHeader.roundInfo
if (!prevBlkRound) {
throw new Error('previous block round is empty')
}
// height = 0 -> we have traversed to genesis.
if (prevBlkRound.height === 0) {
// TODO: more checking.
/*
if prevBlk.GetHeader().GetChainConfigRef().Equals(chain.GetGenesis().GetInitChainConfigRef()) {
s.le.Info("traversed to the genesis block, marking segment as valid")
s.state.Status = ichain.SegmentStatus_SegmentStatus_VALID
} else {
s.le.Warn("segment terminates at invalid genesis")
s.state.Status = ichain.SegmentStatus_SegmentStatus_INVALID
}
*/
seg.status = chain.SegmentStatus.SegmentStatus_VALID
}
}
await seg.writeState()
}
}