-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashlog.js
49 lines (46 loc) · 1.42 KB
/
hashlog.js
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
import { fingerprint64 } from 'farmhash'
export default class HashLog {
constructor(data) {
this.tip = null
this.chain = []
this.blocks = {}
this.commits = {}
if(data) data.forEach(block => this.push(block))
}
get length() { return this.chain.length }
hash(data) {
return fingerprint64(data)
}
push(data, preComputedDelta, preComputedCommit) {
let tiphash = this.tip ? this.tip.chainhash : ''
let chainhash = this.hash(data+tiphash)
let commit = chainhash
let delta = this.getPushDelta(preComputedDelta)
let block = {
chainhash : chainhash,
commit : preComputedCommit || commit,
value : data,
delta : delta
}
this.blocks[chainhash] = block
this.commits[commit] = block
this.chain.push(chainhash)
this.tip = block
this.tipseen = process.hrtime()
}
getPushDelta(preComputedDelta) {
if (this.length == 0) return 0
if (preComputedDelta) return preComputedDelta
let delta = process.hrtime(this.tipseen)
return delta[0] * 1e9 + delta[1]
}
contains(hash) {
return this.blocks[hash] != undefined
}
containsCommit(hash) {
return this.commits[hash] != undefined
}
getBlockAtIndex(index) {
return this.blocks[this.chain[index]]
}
}