-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
122 lines (108 loc) · 3.32 KB
/
db.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
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var DBName = 'blockDB';
var { waitFor } = require('./lib/utils');
var Block = new Schema(
{
"number": { type: Number, index: { unique: true } },
"hash": String,
"parentHash": String,
"nonce": String,
"sha3Uncles": String,
"logsBloom": String,
"transactionsRoot": String,
"stateRoot": String,
"receiptRoot": String,
"proposer": String,
"difficulty": String,
"totalDifficulty": String,
"size": Number,
"extraData": String,
"gasLimit": Number,
"gasUsed": Number,
"timestamp": Number,
"blockTime": Number,
"validators": [String]
});
var Account = new Schema(
{
"address": { type: String, index: { unique: true } },
"balance": Number,
"blockNumber": Number,
"type": { type: Number, default: 0 } // address: 0x0, contract: 0x1
});
var Contract = new Schema(
{
"address": { type: String, index: { unique: true } },
"creationTransaction": String,
"contractName": String,
"compilerVersion": String,
"optimization": Boolean,
"sourceCode": String,
"abi": String,
"byteCode": String
}, { collection: "Contract" });
var Transaction = new Schema(
{
"hash": { type: String, index: { unique: true } },
"nonce": Number,
"blockHash": String,
"blockNumber": Number,
"transactionIndex": Number,
"from": String,
"to": String,
"value": String,
"gas": Number,
"gasPrice": String,
"timestamp": Number,
"input": String
}, { collection: "Transaction" });
var BlockStat = new Schema(
{
"number": { type: Number, index: { unique: true } },
"timestamp": Number,
"difficulty": String,
"hashrate": String,
"txCount": Number,
"gasUsed": Number,
"gasLimit": Number,
"miner": String,
"blockTime": Number,
"uncleCount": Number
});
// create indices
Transaction.index({ blockNumber: -1 });
Transaction.index({ from: 1, blockNumber: -1 });
Transaction.index({ to: 1, blockNumber: -1 });
Account.index({ balance: -1 });
Account.index({ balance: -1, blockNumber: -1 });
Account.index({ type: -1, balance: -1 });
Block.index({ miner: 1 });
Block.index({ miner: 1, blockNumber: -1 });
mongoose.model('BlockStat', BlockStat);
mongoose.model('Block', Block);
mongoose.model('Account', Account);
mongoose.model('Contract', Contract);
mongoose.model('Transaction', Transaction);
module.exports.BlockStat = mongoose.model('BlockStat');
module.exports.Block = mongoose.model('Block');
module.exports.Contract = mongoose.model('Contract');
module.exports.Transaction = mongoose.model('Transaction');
module.exports.Account = mongoose.model('Account');
mongoose.Promise = global.Promise;
mongoose.set('useNewUrlParser', true);
module.exports.Connect = () => {
return new Promise((resolve) => {
mongoUri = process.env.MONGO_URI || `mongodb://localhost/${DBName}`;
mongoose.connect(mongoUri, async (err) => {
if (err) {
console.error(`Cannot connect to mongodb ${mongoUri}. Retrying in 1s...`);
await waitFor(1);
await this.Connect();
}
console.log(`Connected correctly to mongodb ${mongoUri}`);
resolve();
});
});
};
// mongoose.set('debug', true);