forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimedata.js
148 lines (116 loc) · 2.78 KB
/
timedata.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
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
/*!
* timedata.js - time management for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
const EventEmitter = require('events');
const util = require('../utils/util');
const binary = require('../utils/binary');
/**
* Time Data
* An object which handles "adjusted time". This may not
* look it, but this is actually a semi-consensus-critical
* piece of code. It handles version packets from peers
* and calculates what to offset our system clock's time by.
* @alias module:protocol.TimeData
* @extends EventEmitter
* @property {Array} samples
* @property {Object} known
* @property {Number} limit
* @property {Number} offset
*/
class TimeData extends EventEmitter {
/**
* Create time data.
* @constructor
* @param {Number} [limit=200]
*/
constructor(limit) {
super();
if (limit == null)
limit = 200;
this.samples = [];
this.known = new Map();
this.limit = limit;
this.offset = 0;
this.checked = false;
}
/**
* Add time data.
* @param {String} id
* @param {Number} time
*/
add(id, time) {
if (this.samples.length >= this.limit)
return;
if (this.known.has(id))
return;
const sample = time - util.now();
this.known.set(id, sample);
binary.insert(this.samples, sample, compare);
this.emit('sample', sample, this.samples.length);
if (this.samples.length >= 5 && this.samples.length % 2 === 1) {
let median = this.samples[this.samples.length >>> 1];
if (Math.abs(median) >= 70 * 60) {
if (!this.checked) {
let match = false;
for (const offset of this.samples) {
if (offset !== 0 && Math.abs(offset) < 5 * 60) {
match = true;
break;
}
}
if (!match) {
this.checked = true;
this.emit('mismatch');
}
}
median = 0;
}
this.offset = median;
this.emit('offset', this.offset);
}
}
/**
* Get the current adjusted time.
* @returns {Number} Adjusted Time.
*/
now() {
return util.now() + this.offset;
}
/**
* Adjust a timestamp.
* @param {Number} time
* @returns {Number} Adjusted Time.
*/
adjust(time) {
return time + this.offset;
}
/**
* Unadjust a timestamp.
* @param {Number} time
* @returns {Number} Local Time.
*/
local(time) {
return time - this.offset;
}
/**
* Get the current adjusted time in milliseconds.
* @returns {Number} Adjusted Time.
*/
ms() {
return Date.now() + this.offset * 1000;
}
}
/*
* Helpers
*/
function compare(a, b) {
return a - b;
}
/*
* Expose
*/
module.exports = TimeData;