This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
63 lines (56 loc) · 1.99 KB
/
block.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
const mongoose = require('mongoose');
const Block = new mongoose.Schema(
{
_id: { type: String }, // block hash
author: { type: String, index: true },
extraData: { type: String },
gasLimit: { type: Number },
gasUsed: { type: Number },
logsBloom: { type: String },
mixHash: { type: String },
nonce: { type: Number },
number: { type: Number, unique: true },
parentHash: { type: String },
receiptsRoot: { type: String },
reward: { type: Number, default: 0 },
sha3Uncles: { type: String },
size: { type: Number },
stateRoot: { type: String },
timestamp: { type: Date, index: true },
totalDifficulty: { type: Number },
transactions: [{ type: String, ref: 'Tx' }],
transactionsRoot: { type: String },
uncles: [{ type: String }],
},
{
versionKey: false,
},
);
Block.statics.fromRPC = function fromRPC(data) {
const json = Object.assign({}, data);
// move hash to primary key _id
json._id = json.hash;
delete json.hash;
const hexKeys = ['gasLimit', 'gasUsed', 'nonce', 'number', 'size', 'timestamp'];
hexKeys.forEach((key) => {
if (json[key]) {
json[key] = parseInt(json[key], 16);
}
if (key === 'timestamp') {
json[key] = json[key] * 1000;
}
});
if (json.transactions && json.transactions.length) {
json.transactions = json.transactions.map((doc) => doc.hash || doc);
}
return new this(json);
};
Block.index({ timestamp: -1 }, { background: true });
Block.index({ author: 1, gasUsed: 1 }, { background: true, sparse: true });
Block.index({ author: 1, timestamp: -1 }, { background: true });
Block.index({ author: 1, timestamp: 1 }, { background: true });
Block.index({ timestamp: 1, _id: 1 }, { background: true });
Block.index({ timestamp: -1, _id: 1 }, { background: true });
Block.index({ author: 1, timestamp: 1, _id: 1 }, { background: true });
Block.index({ author: 1, timestamp: -1, _id: 1 }, { background: true });
module.exports = mongoose.models?.Block || mongoose.model('Block', Block);