-
Notifications
You must be signed in to change notification settings - Fork 111
/
Rooter.ts
121 lines (100 loc) · 3.44 KB
/
Rooter.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
import * as Deque from 'double-ended-queue';
import MerkleTree from './lib/MerkleTree';
import Protocol from './Protocol';
import { Blockchain } from './Blockchain';
import { Cas } from './Cas';
/**
* Class that performs periodic rooting of batches of Sidetree operations.
*/
export default class Rooter {
private operations: Deque<Buffer> = new Deque<Buffer>();
/**
* Flag indicating if the rooter is currently processing a batch of operations.
*/
private processing: boolean = false;
public constructor (
private blockchain: Blockchain,
private cas: Cas,
private batchIntervalInSeconds: number,
startPeriodicRooting: boolean = true) {
if (startPeriodicRooting) {
this.startPeriodicRooting();
}
}
/**
* Adds the given operation to a queue to be batched and anchored on blockchain.
*/
public add (operation: Buffer) {
this.operations.push(operation);
}
/**
* Returns the current operation queue length.
*/
public getOperationQueueLength (): number {
return this.operations.length;
}
/**
* The function that starts periodically anchoring operation batches to blockchain.
*/
public startPeriodicRooting () {
setInterval(async () => this.rootOperations(), this.batchIntervalInSeconds * 1000);
}
/**
* Processes the operations in the queue.
*/
public async rootOperations () {
// Wait until the next interval if the rooter is still processing a batch.
if (this.processing) {
return;
}
try {
console.info(Date.now() + ' Start batch processing...');
this.processing = true;
// Get the batch of operations to be anchored on the blockchain.
const batch = this.getBatch();
console.info(Date.now() + ' Batch size = ' + batch.length);
// Combine all operations into one JSON buffer.
const batchBuffer = Buffer.from(JSON.stringify(batch));
// TODO: Compress the batch buffer.
// Make the 'batch file' available in CAS.
const batchFileAddress = await this.cas.write(batchBuffer);
// Compute the Merkle root hash.
const merkleRoot = MerkleTree.create(batch).rootHash;
// Construct the 'anchor file'.
const anchorFile = {
batchFile: batchFileAddress,
merkleRoot: merkleRoot
};
// Make the 'anchor file' available in CAS.
const anchorFileJsonBuffer = Buffer.from(JSON.stringify(anchorFile));
const anchorFileAddress = await this.cas.write(anchorFileJsonBuffer);
// Anchor the 'anchor file hash' on blockchain.
await this.blockchain.write(anchorFileAddress);
} catch (e) {
console.info('TODO: batch rooting error handling not implemented.');
console.info(e);
} finally {
this.processing = false;
console.info(Date.now() + ' End batch processing.');
}
}
/**
* Gets a batch of operations to be anchored on the blockchain.
* If number of pending operations is greater than the Sidetree protocol's maximum allowed number per batch,
* then the maximum allowed number of operation is returned.
*/
private getBatch (): Buffer[] {
let queueSize = this.operations.length;
let batchSize = queueSize;
if (queueSize > Protocol.maxOperationsPerBatch) {
batchSize = Protocol.maxOperationsPerBatch;
}
const batch = new Array<Buffer>(batchSize);
let count = 0;
while (count < batchSize) {
batch[count] = this.operations.shift()!;
count++;
}
return batch;
}
}