-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fda85a
commit e635627
Showing
5 changed files
with
1,009 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
bin | ||
cp.out | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const express = require('express') | ||
const cors = require('cors') | ||
const app = express() | ||
|
||
app.use(cors()) | ||
|
||
const port = 3000 | ||
|
||
|
||
const MongoClient = require('mongodb').MongoClient; | ||
// TODO env | ||
const url = "mongodb://localhost:27017/"; | ||
|
||
let dbo | ||
|
||
MongoClient.connect(url, function (err, db) { | ||
dbo = db.db("local"); | ||
}) | ||
|
||
function isNormalInteger(str) { | ||
const n = Math.floor(Number(str)); | ||
return n !== Infinity && String(n) === str && n >= 0; | ||
} | ||
|
||
const handler = async (req, res) => { | ||
const selector = req.params.selector; | ||
|
||
const perPage = isNormalInteger(req.query.per_page) ? parseInt(req.query.per_page) : 10 | ||
const page = isNormalInteger(req.query.page) ? parseInt(req.query.page) : 1 | ||
const skip = perPage * (page - 1) | ||
|
||
let query = {}; | ||
if (selector) { | ||
if (isNormalInteger(selector)) { | ||
const dealID = parseInt(selector) | ||
query = {"_id": dealID} | ||
} else { | ||
query = {"$or": [{"proposal.piececid": selector}, {"proposal.provider": selector}]} | ||
} | ||
} | ||
|
||
let deals = await dbo.collection("deals").find(query).limit(perPage).skip(skip).sort({"_id": -1}).toArray() | ||
|
||
deals = await Promise.all(deals.map(async deal => { | ||
|
||
const dealID = deal.dealid | ||
const sector = await dbo.collection("sectors").findOne({"info.info.dealids": dealID}) | ||
|
||
let Status = ""; | ||
let sectorID = ""; | ||
|
||
if (sector) { | ||
if (sector.Recovery) { | ||
Status = "Recovery" | ||
} else if (sector.Fault) { | ||
Status = "Fault" | ||
} else { | ||
Status = "Active" | ||
} | ||
|
||
sectorID = sector.ID | ||
} | ||
|
||
return { | ||
"DealInfo": deal, | ||
"DealID": dealID, | ||
"SectorID": sectorID, | ||
"SectorInfo": sector, | ||
"Status": Status | ||
} | ||
})) | ||
|
||
const response = { | ||
"Pagination": { | ||
"Page": page, | ||
"PerPage": perPage | ||
}, | ||
"Deals": deals | ||
} | ||
|
||
res.send(response); | ||
} | ||
|
||
app.get('/deals', handler) | ||
app.get('/deals/:selector', handler) | ||
|
||
app.listen(port, () => console.log(`CID checker app listening at http://localhost:${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
const cbor = require('cbor'); | ||
const got = require('got'); | ||
|
||
const MongoClient = require('mongodb').MongoClient; | ||
// TODO env | ||
const url = "mongodb://localhost:27017/"; | ||
|
||
let dbo | ||
|
||
MongoClient.connect(url, function (err, db) { | ||
dbo = db.db("local"); | ||
}) | ||
|
||
const rpcUrl = 'http://3.9.46.104:1234/rpc/v0'; | ||
|
||
const requestChainHead = { | ||
"jsonrpc": "2.0", | ||
"method": "Filecoin.ChainHead", | ||
"id": 1, | ||
"params": [] | ||
} | ||
|
||
const requestStateMarketDeals = { | ||
"jsonrpc": "2.0", | ||
"method": "Filecoin.StateMarketDeals", | ||
"id": 1, | ||
"params": [[]] | ||
} | ||
|
||
const requestStateMinerSectors = minerId => { | ||
return { | ||
"jsonrpc": "2.0", | ||
"method": "Filecoin.StateMinerSectors", | ||
"id": 1, | ||
"params": [minerId, null, null, null] | ||
} | ||
} | ||
|
||
const requestStateGetActor = minerId => { | ||
return { | ||
"jsonrpc": "2.0", | ||
"method": "Filecoin.StateGetActor", | ||
"id": 1, | ||
"params": [minerId, null] | ||
} | ||
} | ||
|
||
const requestChainReadObj = cid => { | ||
return { | ||
"jsonrpc": "2.0", | ||
"method": "Filecoin.ChainReadObj", | ||
"id": 1, | ||
"params": [{"/": cid}] | ||
} | ||
} | ||
|
||
const DealsProcessor = async () => { | ||
console.log("Fetching deals from Lotus node") | ||
|
||
const {body} = await got.post(rpcUrl, { | ||
json: requestStateMarketDeals, | ||
responseType: 'json' | ||
}); | ||
|
||
let writeOps = [] | ||
|
||
Object.keys(body.result).forEach(function (key) { | ||
|
||
writeOps.push({ | ||
"replaceOne": { | ||
"filter": {"_id": key}, | ||
"replacement": body.result[key], | ||
"upsert": true | ||
} | ||
}); | ||
|
||
}); | ||
|
||
await dbo.collection("deals").bulkWrite(writeOps) | ||
} | ||
|
||
const SectorsProcessor = async () => { | ||
console.log("Fetching sectors from Lotus node") | ||
|
||
let minersList = await dbo.collection("deals1").distinct("Proposal.Provider") | ||
|
||
let writeOps = [] | ||
|
||
for (const minerId of minersList) { | ||
const {body} = await got.post(rpcUrl, { | ||
json: requestStateMinerSectors(minerId), | ||
responseType: 'json' | ||
}); | ||
|
||
if (body.result != null) { | ||
for (const sector of body.result) { | ||
writeOps.push({ | ||
"updateOne": { | ||
"filter": {"_id": sector.ID}, | ||
"update": {"$set": {"Info": sector.Info}}, | ||
"upsert": true | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
await dbo.collection("sectors").bulkWrite(writeOps) | ||
} | ||
|
||
const MinersProcessor = async () => { | ||
console.log("Fetching miners from Lotus node") | ||
|
||
let minersList = await dbo.collection("deals").distinct("Proposal.Provider") | ||
|
||
for (const minerId of minersList) { | ||
let response = await got.post(rpcUrl, { | ||
json: requestStateGetActor(minerId), | ||
responseType: 'json' | ||
}); | ||
|
||
let minerActorHead = response.body.result.Head['/'] | ||
|
||
response = await got.post(rpcUrl, { | ||
json: requestChainReadObj(minerActorHead), | ||
responseType: 'json' | ||
}); | ||
|
||
// TODO: UnmarshalCBOR response.body.result (minerStateBytes) | ||
console.log(response.body.result) | ||
|
||
// TODO: collect faults and recoveries | ||
} | ||
|
||
// TODO: save faults and recoveries | ||
} | ||
|
||
|
||
let height = 0; | ||
|
||
loopHandler = async () => { | ||
const {body} = await got.post(rpcUrl, { | ||
json: requestChainHead, | ||
responseType: 'json' | ||
}); | ||
|
||
if (body.result.Height > height) { | ||
height = body.result.Height; | ||
|
||
await DealsProcessor() | ||
await SectorsProcessor() | ||
await MinersProcessor() | ||
} | ||
|
||
} | ||
|
||
(async () => { | ||
while (true) { | ||
await loopHandler() | ||
} | ||
})(); |
Oops, something went wrong.