-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
120 lines (112 loc) · 3.19 KB
/
config.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
import fs from 'fs'
import jsonschema from 'jsonschema'
export type SystemConfig = {
signature: SignatureConfig
clients: SenderConfig[]
nodes: NodeConfig[]
params: ParamConfig
}
export type SignatureConfig = {
enabled: boolean
}
export type ParamConfig = {
// f is the max number of faulty nodes tolerated
f: number
// k is a big number that is used to calculate the high-water mark
// NOTE: k is slightly different from the k in the osdi99 paper
// if checkpoint is genereated at every 100 requests, then k can be 100
// highWaterMark will be 2 * k + lowWaterMark
k: number
}
export type SenderConfig = {
name: string
pubkey: string
prikey: string
}
export type NodeConfig = {
host: string
port: number
} & SenderConfig
export const schema = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {
'signature': {
'type': 'object',
'properties': {
'enabled': {
'type': 'boolean',
'default': 'true'
}
},
},
'nodes': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'name': {
'type': 'string'
},
'host': {
'type': 'string'
},
'port': {
'type': 'number'
},
'pubkey': {
'type': 'string'
},
'prikey': {
'type': 'string'
}
},
'required': ['name', 'host', 'port', 'pubkey', 'prikey']
}
},
'clients': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'name': {
'type': 'string'
},
'pubkey': {
'type': 'string'
},
'prikey': {
'type': 'string'
}
},
'required': ['name', 'pubkey', 'prikey']
}
},
'params': {
'type': 'object',
'properties': {
'f': {
'type': 'number'
},
'k': {
'type': 'number'
}
},
'required': ['f', 'k']
}
},
'required': ['nodes', 'params']
}
export function readConfig(path?: string): SystemConfig {
path = path ?? 'configs/cluster.json'
console.log(`reading config from ${path}`)
const obj = JSON.parse(fs.readFileSync(path).toString())
const validator = new jsonschema.Validator()
const result = validator.validate(obj, schema)
if (!result.valid) {
throw new Error(`invalid config: ${result.errors}`)
}
const config = obj as SystemConfig
config.signature = config.signature ?? { enabled: true }
return config
}