1
1
'use strict'
2
2
3
- /**
4
- * NOTES
5
- * meta.rawHead is the the head of the chain with the most POW
6
- * meta.head is the head of the chain that has had its state root verifed
7
- */
8
3
const async = require ( 'async' )
9
4
const Stoplight = require ( 'flow-stoplight' )
10
5
const semaphore = require ( 'semaphore' )
@@ -86,8 +81,8 @@ Blockchain.prototype = {
86
81
87
82
/**
88
83
* Fetches the meta info about the blockchain from the db. Meta info contains
89
- * hashes of the headerchain head, blockchain head, genesis block and verified
90
- * state root heads.
84
+ * hashes of the headerchain head, blockchain head, genesis block and iterator
85
+ * heads.
91
86
* @method _init
92
87
*/
93
88
Blockchain . prototype . _init = function ( cb ) {
@@ -113,8 +108,9 @@ Blockchain.prototype._init = function (cb) {
113
108
function getHeads ( genesisHash , cb ) {
114
109
self . _genesis = genesisHash
115
110
async . series ( [
116
- // load verified state root heads
111
+ // load verified iterator heads
117
112
( cb ) => self . db . get ( 'heads' , {
113
+ keyEncoding : 'binary' ,
118
114
valueEncoding : 'json'
119
115
} , ( err , heads ) => {
120
116
if ( err ) heads = { }
@@ -124,13 +120,15 @@ Blockchain.prototype._init = function (cb) {
124
120
} ) ,
125
121
// load headerchain head
126
122
( cb ) => self . db . get ( headHeaderKey , {
123
+ keyEncoding : 'binary' ,
127
124
valueEncoding : 'binary'
128
125
} , ( err , hash ) => {
129
126
self . _headHeader = err ? genesisHash : hash
130
127
cb ( )
131
128
} ) ,
132
129
// load blockchain head
133
130
( cb ) => self . db . get ( headBlockKey , {
131
+ keyEncoding : 'binary' ,
134
132
valueEncoding : 'binary'
135
133
} , ( err , hash ) => {
136
134
self . _headBlock = err ? genesisHash : hash
@@ -161,8 +159,9 @@ Blockchain.prototype.putGenesis = function (genesis, cb) {
161
159
}
162
160
163
161
/**
164
- * Returns that head block
162
+ * Returns the specified iterator head.
165
163
* @method getHead
164
+ * @param name name of the head (default: 'vm')
166
165
* @param cb Function the callback
167
166
*/
168
167
Blockchain . prototype . getHead = function ( name , cb ) {
@@ -185,6 +184,37 @@ Blockchain.prototype.getHead = function (name, cb) {
185
184
} )
186
185
}
187
186
187
+ /**
188
+ * Returns the latest header in the canonical chain.
189
+ * @method getLatestHeader
190
+ * @param cb Function the callback
191
+ */
192
+ Blockchain . prototype . getLatestHeader = function ( cb ) {
193
+ const self = this
194
+
195
+ // ensure init completed
196
+ self . _initLock . await ( function runGetLatestHeader ( ) {
197
+ self . getBlock ( self . _headHeader , ( err , block ) => {
198
+ if ( err ) return cb ( err )
199
+ cb ( null , block . header )
200
+ } )
201
+ } )
202
+ }
203
+
204
+ /**
205
+ * Returns the latest full block in the canonical chain.
206
+ * @method getLatestBlock
207
+ * @param cb Function the callback
208
+ */
209
+ Blockchain . prototype . getLatestBlock = function ( cb ) {
210
+ const self = this
211
+
212
+ // ensure init completed
213
+ self . _initLock . await ( function runGetLatestBlock ( ) {
214
+ self . getBlock ( self . _headBlock , cb )
215
+ } )
216
+ }
217
+
188
218
/**
189
219
* Adds many blocks to the blockchain
190
220
* @method putBlocks
@@ -518,9 +548,26 @@ Blockchain.prototype.selectNeededHashes = function (hashes, cb) {
518
548
}
519
549
520
550
Blockchain . prototype . _saveHeads = function ( cb ) {
521
- this . db . put ( 'heads' , this . _heads , {
522
- keyEncoding : 'json'
523
- } , cb )
551
+ var dbOps = [ {
552
+ type : 'put' ,
553
+ key : 'heads' ,
554
+ keyEncoding : 'binary' ,
555
+ valueEncoding : 'json' ,
556
+ value : this . _heads
557
+ } , {
558
+ type : 'put' ,
559
+ key : headHeaderKey ,
560
+ keyEncoding : 'binary' ,
561
+ valueEncoding : 'binary' ,
562
+ value : this . _headHeader
563
+ } , {
564
+ type : 'put' ,
565
+ key : headBlockKey ,
566
+ keyEncoding : 'binary' ,
567
+ valueEncoding : 'binary' ,
568
+ value : this . _headBlock
569
+ } ]
570
+ this . _batchDbOps ( dbOps , cb )
524
571
}
525
572
526
573
// delete canonical number assignments for specified number and above
@@ -537,7 +584,7 @@ Blockchain.prototype._deleteStaleAssignments = function (number, headHash, ops,
537
584
} )
538
585
self . _cache . numberToHash . del ( key )
539
586
540
- // reset stale verified state root heads to current canonical head
587
+ // reset stale iterator heads to current canonical head
541
588
Object . keys ( self . _heads ) . forEach ( function ( name ) {
542
589
if ( self . _heads [ name ] . equals ( hash ) ) {
543
590
self . _heads [ name ] = headHash
@@ -751,10 +798,11 @@ Blockchain.prototype._delChild = function (hash, number, headHash, ops, cb) {
751
798
}
752
799
753
800
/**
754
- * Iterates through blocks starting at the specified verified state root head
755
- * and calls the onBlock function on each block
801
+ * Iterates through blocks starting at the specified iterator head and calls
802
+ * the onBlock function on each block. The current location of an iterator head
803
+ * can be retrieved using the `getHead()`` method
756
804
* @method iterator
757
- * @param {String } name - the name of the verified state root head
805
+ * @param {String } name - the name of the iterator head
758
806
* @param {function } onBlock - function called on each block with params (block, reorg, cb)
759
807
* @param {function } cb - a callback function
760
808
*/
0 commit comments