// Part 1 Import Related Package
var Web3 = require('web3');
var mongoose = require('mongoose');
var moment = require('moment');
const startBlock = 14983823;
const endBlock = 14983831;
var preBlockNum = 0;
var preBlockMiner = "";
var preBlockBasefee = 0;
var preBlockTxcount = 0;
var preBlockGasusage = 0;
const ethContractSchema = mongoose.Schema({
blockNumber: String,
miner: String,
basefee:String,
txcount:String,
gasusage:String,
preblockNumber: String,
preminer: String,
prebasefee:String,
pretxcount:String,
pregasusage:String
//code: String,
//storage: String,
});
const EthContract = mongoose.model('emptypreinfo', ethContractSchema);
EthContract.addContract = function(newContract, callback) {
newContract.save(callback);
};
EthContract.updateContract = function(contractAddress, newblockNumber, newminer,newbasefee,newcount,newgasusage,newpreblockNumber, newpreminer,newprebasefee,newprecount,newpregasusage) {
EthContract.update({address: contractAddress}, { $set: {
"blockNumber": newblockNumber,
"miner": newminer,
"basefee":newbasefee,
"txcount":newcount,
"gasusage":newgasusage,
"preblockNumber": newpreblockNumber,
"preminer": newpreminer,
"prebasefee":newprebasefee,
"pretxcount":newprecount,
"pregasusage":newpregasusage
}}, (err, suc) => {});
}
EthContract.checkUnique = function(contractAddress) {
let res = EthContract.find({address: contractAddress});
console.log(res);
if(res) return false;
return true;
};
async function connectDB() {
mongoose.connect('mongodb://localhost:27017/eth_blockminer');
mongoose.connection.on('connected', () => {
console.log('MongoDB has started successfully.');
});
mongoose.connection.on('error', (err) => {
console.log('Database error' + err);
});
}
// Part Connection to Local RPC
async function connectWeb3() {
web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/daa578881869407da748a2603f5cee52"));
//web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8546"));
global.web3 = web3;
console.log('web3 has connected successfully.');
}
async function scanTheChain() {
for(let i = startBlock; i <= endBlock; i++) {
console.log('scanning the chain.');
console.log("[ " + moment().format('MMMM Do YYYY, h:mm:ss a') + " ] " + "Scanning Block " + i);
var blockInfo = await web3.eth.getBlock(i);
var blockMiner = blockInfo.miner;
var blockBasefee = blockInfo.baseFeePerGas;
var blockTxCnt = await web3.eth.getBlockTransactionCount(i);
var blockGasusage = blockInfo.gasUsed;
console.log('TxCount.'+blockTxCnt);
if (blockTxCnt = 0){
EthContract.addContract(new EthContract({
blockNumber: i,
miner: blockMiner,
basefee: blockBasefee,
txcount: blockTxCnt,
gasusage: blockGasusage,
preblockNumber: preBlockNum,
preminer: preBlockMiner,
prebasefee: preBlockBasefee,
pretxcount: preBlockTxcount,
pregasusage: preBlockGasusage
}));
}
preBlockNum = i;
preBlockMiner = blockMiner;
preBlockBasefee = blockBasefee;
preBlockTxcount = blockTxCnt;
preBlockGasusage = blockGasusage;
}
}
// Part Main Function
(async function main(){
moment.locale('zh-cn');
await connectDB();
await connectWeb3();
await scanTheChain();
})();
nohup node EmptyPreInfo.js >>EmptyPreInfo.log 2>&1 &
use eth_blockminer;
db.emptypreinfos.count()
json
mongoexport --forceTableScan -d eth_blockminer -c emptypreinfos -o ./emptypreinfos.json
json–csv
cat emptypreinfos.json | jsoncsv | mkexcel > emptypreinfos.csv