indexer: simplify block meta and remove records
This commit is contained in:
parent
f30276bc27
commit
fae647b9e9
@ -15,7 +15,7 @@ const Logger = require('blgr');
|
|||||||
const Network = require('../protocol/network');
|
const Network = require('../protocol/network');
|
||||||
const util = require('../utils/util');
|
const util = require('../utils/util');
|
||||||
const layout = require('./layout');
|
const layout = require('./layout');
|
||||||
const {BlockMeta} = require('./records');
|
const {ZERO_HASH} = require('../protocol/consensus');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indexer
|
* Indexer
|
||||||
@ -221,11 +221,7 @@ class Indexer extends EventEmitter {
|
|||||||
if (!data)
|
if (!data)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
const block = new BlockMeta();
|
return new BlockMeta(data, height);
|
||||||
block.hash = data;
|
|
||||||
block.height = height;
|
|
||||||
|
|
||||||
return block;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -399,7 +395,7 @@ class Indexer extends EventEmitter {
|
|||||||
if (entry.height !== this.height + 1)
|
if (entry.height !== this.height + 1)
|
||||||
throw new Error('Indexer: Can not add block.');
|
throw new Error('Indexer: Can not add block.');
|
||||||
|
|
||||||
const tip = BlockMeta.fromEntry(entry);
|
const tip = new BlockMeta(entry.hash, entry.height);
|
||||||
|
|
||||||
// Start the batch write.
|
// Start the batch write.
|
||||||
this.start();
|
this.start();
|
||||||
@ -458,7 +454,7 @@ class Indexer extends EventEmitter {
|
|||||||
if (entry.height !== this.height)
|
if (entry.height !== this.height)
|
||||||
throw new Error('Indexer: Can not remove block.');
|
throw new Error('Indexer: Can not remove block.');
|
||||||
|
|
||||||
const tip = BlockMeta.fromEntry(entry);
|
const tip = new BlockMeta(entry.hash, entry.height);
|
||||||
|
|
||||||
// Start the batch write.
|
// Start the batch write.
|
||||||
this.start();
|
this.start();
|
||||||
@ -498,7 +494,7 @@ class Indexer extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add to batch write to save tip and height.
|
// Add to batch write to save tip and height.
|
||||||
this.put(layout.h.encode(tip.height), tip.toHash());
|
this.put(layout.h.encode(tip.height), tip.hash);
|
||||||
|
|
||||||
const raw = bio.write(4).writeU32(tip.height).render();
|
const raw = bio.write(4).writeU32(tip.height).render();
|
||||||
this.put(layout.R.encode(), raw);
|
this.put(layout.R.encode(), raw);
|
||||||
@ -544,9 +540,22 @@ class Indexer extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Block Meta
|
||||||
|
*/
|
||||||
|
|
||||||
|
class BlockMeta {
|
||||||
|
constructor(hash, height) {
|
||||||
|
this.hash = hash || ZERO_HASH;
|
||||||
|
this.height = height || 0;
|
||||||
|
|
||||||
|
assert(Buffer.isBuffer(this.hash) && this.hash.length === 32);
|
||||||
|
assert(Number.isInteger(this.height));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Index Options
|
* Index Options
|
||||||
* @alias module:indexer.IndexOptions
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class IndexOptions {
|
class IndexOptions {
|
||||||
|
|||||||
@ -9,12 +9,13 @@
|
|||||||
const bdb = require('bdb');
|
const bdb = require('bdb');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Index Database Layout:
|
* Index database layout:
|
||||||
* To be extended by indexer implementations
|
* To be extended by indexer implementations.
|
||||||
|
*
|
||||||
* V -> db version
|
* V -> db version
|
||||||
* O -> flags
|
* O -> flags
|
||||||
* h[height] -> recent block hash
|
* h[height] -> block hash
|
||||||
* R -> chain sync state
|
* R -> index sync height
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const layout = {
|
const layout = {
|
||||||
|
|||||||
@ -1,152 +0,0 @@
|
|||||||
/*!
|
|
||||||
* records.js - indexer records
|
|
||||||
* Copyright (c) 2018, the bcoin developers (MIT License).
|
|
||||||
* https://github.com/bcoin-org/bcoin
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @module lib/records
|
|
||||||
*/
|
|
||||||
|
|
||||||
const bio = require('bufio');
|
|
||||||
const util = require('../utils/util');
|
|
||||||
const consensus = require('../protocol/consensus');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Block Meta
|
|
||||||
* @alias module:indexer.BlockMeta
|
|
||||||
*/
|
|
||||||
|
|
||||||
class BlockMeta {
|
|
||||||
/**
|
|
||||||
* Create block meta.
|
|
||||||
* @constructor
|
|
||||||
* @param {Hash} hash
|
|
||||||
* @param {Number} height
|
|
||||||
*/
|
|
||||||
|
|
||||||
constructor(hash, height) {
|
|
||||||
this.hash = hash || consensus.NULL_HASH;
|
|
||||||
this.height = height != null ? height : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clone the block.
|
|
||||||
* @returns {BlockMeta}
|
|
||||||
*/
|
|
||||||
|
|
||||||
clone() {
|
|
||||||
return new this.constructor(this.hash, this.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get block meta hash as a buffer.
|
|
||||||
* @returns {Buffer}
|
|
||||||
*/
|
|
||||||
|
|
||||||
toHash() {
|
|
||||||
return Buffer.from(this.hash, 'hex');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from chain entry.
|
|
||||||
* @private
|
|
||||||
* @param {IndexEntry} entry
|
|
||||||
*/
|
|
||||||
|
|
||||||
fromEntry(entry) {
|
|
||||||
this.hash = entry.hash;
|
|
||||||
this.height = entry.height;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from json object.
|
|
||||||
* @private
|
|
||||||
* @param {Object} json
|
|
||||||
*/
|
|
||||||
|
|
||||||
fromJSON(json) {
|
|
||||||
this.hash = util.revHex(json.hash);
|
|
||||||
this.height = json.height;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from serialized tip data.
|
|
||||||
* @private
|
|
||||||
* @param {Buffer} data
|
|
||||||
*/
|
|
||||||
|
|
||||||
fromRaw(data) {
|
|
||||||
const br = bio.read(data);
|
|
||||||
this.hash = br.readHash('hex');
|
|
||||||
this.height = br.readI32();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from chain entry.
|
|
||||||
* @param {IndexEntry} entry
|
|
||||||
* @returns {BlockMeta}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static fromEntry(entry) {
|
|
||||||
return new this().fromEntry(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from json object.
|
|
||||||
* @param {Object} json
|
|
||||||
* @returns {BlockMeta}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static fromJSON(json) {
|
|
||||||
return new this().fromJSON(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate block meta from serialized data.
|
|
||||||
* @param {Hash} hash
|
|
||||||
* @param {Buffer} data
|
|
||||||
* @returns {BlockMeta}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static fromRaw(data) {
|
|
||||||
return new this().fromRaw(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serialize the block meta.
|
|
||||||
* @returns {Buffer}
|
|
||||||
*/
|
|
||||||
|
|
||||||
toRaw() {
|
|
||||||
const bw = bio.write(36);
|
|
||||||
bw.writeHash(this.hash);
|
|
||||||
bw.writeI32(this.height);
|
|
||||||
return bw.render();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert the block meta to a more json-friendly object.
|
|
||||||
* @returns {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return {
|
|
||||||
hash: util.revHex(this.hash),
|
|
||||||
height: this.height
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Expose
|
|
||||||
*/
|
|
||||||
|
|
||||||
exports.BlockMeta = BlockMeta;
|
|
||||||
|
|
||||||
module.exports = exports;
|
|
||||||
Loading…
Reference in New Issue
Block a user