This repository was 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
/
Copy pathblock.ts
205 lines (174 loc) · 6.62 KB
/
block.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
195
196
197
198
199
200
201
202
203
204
205
import { IStrategy } from './encryption/encryption'
import { IDb } from '@aperturerobotics/objstore'
import { timestampToTime } from '@aperturerobotics/timestamp';
import { storageref } from '@aperturerobotics/storageref'
import { ObjectStore } from '@aperturerobotics/objstore';
import {
Block,
BlockHeader,
ChainConfig,
block,
} from './pb'
import * as toBuffer from 'typedarray-to-buffer'
// BlockImpl contains the implementation of the block logic loaded into memory.
export class BlockImpl extends Block {
// state is the block state.
public state: block.State
// db is a reference to the db.
private db: IDb
// objStore is the object store.
private objStore: ObjectStore;
// encStrat is the encryption strategy
private encStrat: IStrategy
// blkRef is the block storage reference
private blkRef: storageref.IStorageRef
// id is the block id
private id: string
// dbKey is the database key
private dbKey: string
constructor(
db: IDb,
objStore: ObjectStore,
blockRef: storageref.IStorageRef,
blk: Block,
header: BlockHeader,
encStrat: IStrategy,
) {
super(blk)
let digestBuf = toBuffer(blockRef.objectDigest)
let id = digestBuf.toString('base64')
this.state = new block.State({ segmentId: "" })
this.objStore = objStore
this.id = id
this.db = db
this.blkRef = blockRef
this.setHeader(header)
this.encStrat = encStrat
this.dbKey = "/" + id
}
// writeState attempts to write state to the db.
public async writeState(): Promise<void> {
let dbKey = this.dbKey
let val = block.State.encode(this.state).finish()
await this.db.setKey(dbKey, val)
}
// readState attempts to read state from the db.
public async readState(): Promise<void> {
let dbKey = this.dbKey
let dat = await this.db.getKey(dbKey)
if (!dat || !dat.length) {
return
}
this.state = block.State.decode(dat)
}
// getSegmentID returns the current segment ID.
public getSegmentID(): string {
return this.state.segmentId
}
// setSegmentID sets the segment ID.
public setSegmentID(id: string) {
this.state.segmentId = id
}
// setNextBlock sets the reference to the next block.
public setNextBlock(ref: storageref.IStorageRef) {
this.state.nextBlock = ref
}
// getBlockRef returns the block reference.
public getBlockRef(): storageref.IStorageRef {
return this.blkRef
}
// validateChild throws if the child is not valid.
public async validateChild(child: BlockImpl): Promise<void> {
let childHeader = child.getHeader()
if (!childHeader) {
throw new Error('child block header unset, cannot validate')
}
let bHeader = this.getHeader()
if (!bHeader) {
throw new Error('parent block header unset, cannot validate child')
}
let bTs = timestampToTime(bHeader.blockTs || null)
let childTs = timestampToTime(childHeader.blockTs || null)
let bRoundInfo = bHeader.roundInfo
if (!bRoundInfo) {
throw new Error('parent block header did not contain round info, cannot validate child')
}
let childRoundInfo = childHeader.roundInfo
if (!childRoundInfo) {
throw new Error('child block header did not contain round info, cannot validate')
}
let childExpectedHeight = ((bRoundInfo.height as number) || 0) + 1
if (childRoundInfo.height !== childExpectedHeight) {
throw new Error(`child height ${childRoundInfo.height} != expected ${childExpectedHeight}`)
}
if (childTs.getTime() < bTs.getTime()) {
throw new Error(`child timestamp ${childTs.toString()} before parent ${bTs.toString()}`)
}
let chainConf = await this.fetchChainConfig()
}
// fetchChainConfig fetches the chain config.
private async fetchChainConfig(): Promise<ChainConfig> {
let header = this.getHeader()
if (!header || !header.chainConfigRef || !header.chainConfigRef.objectDigest) {
throw new Error('header reference required')
}
let chainConfRef = header.chainConfigRef
if (!chainConfRef || !chainConfRef.objectDigest) {
throw new Error('chain config reference and object digest required')
}
let encConf = this.encStrat.getBlockEncryptionConfigWithDigest(chainConfRef.objectDigest)
let chainConf = await this.objStore.getOrFetchReference(chainConfRef, new ChainConfig(), encConf) as ChainConfig
if (!chainConf) {
throw new Error('cannot find chain config in network')
}
return chainConf
}
}
// FollowBlockRef follows a reference to a Block object.
export async function FollowBlockRef(
ref: storageref.IStorageRef | null,
encStrat: IStrategy,
objStore: ObjectStore,
): Promise<Block> {
if (!ref || !ref.objectDigest) {
throw new Error('block reference and object digest expected')
}
let encConf = encStrat.getBlockEncryptionConfigWithDigest(ref.objectDigest)
let blockRef = await objStore.getOrFetchReference(ref, new Block(), encConf) as Block
if (!blockRef) {
throw new Error('cannot find block reference in network')
}
return blockRef
}
// FollowBlockHeaderRef follows a reference to a BlockHeader object.
export async function FollowBlockHeaderRef(
ref: storageref.IStorageRef | null,
encStrat: IStrategy,
objStore: ObjectStore,
): Promise<BlockHeader> {
if (!ref || !ref.objectDigest) {
throw new Error('block header reference and object digest expected')
}
let encConf = encStrat.getBlockEncryptionConfigWithDigest(ref.objectDigest)
let blockHeader = await objStore.getOrFetchReference(ref, new BlockHeader(), encConf) as BlockHeader
if (!blockHeader) {
throw new Error('cannot find block header reference in network')
}
return blockHeader
}
// GetBlock gets the Block object associated with the given Block storage ref.
export async function GetBlock(
encStrat: IStrategy,
dbm: IDb,
objStore: ObjectStore,
blockRef: storageref.IStorageRef | null,
): Promise<BlockImpl> {
if (!blockRef) {
throw new Error('block reference expected')
}
let blk = await FollowBlockRef(blockRef, encStrat, objStore)
let blkHeader = await FollowBlockHeaderRef(blk.blockHeaderRef || null, encStrat, objStore)
let b = new BlockImpl(dbm, objStore, blockRef, blk, blkHeader, encStrat)
await b.readState()
return b
}