Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit 568e5ee

Browse files
authored
Turn on track signers (#47)
* return validators downtime blocks * get validator validated blocks * make validatorGroup in validator schema not mandate
1 parent 6acd45c commit 568e5ee

File tree

5 files changed

+108
-29
lines changed

5 files changed

+108
-29
lines changed

CHANGLOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## [Unreleased]
44

55
* [#45] Fixed accounts list resolver
6+
* Return validator downtime blocks in resolver
7+
* Return validator validated blocks in resolver
68

79
## First Release v0.1.0
810

imports/api/blocks/server/methods.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,31 @@ Meteor.methods({
6868
chainState.latestHeight = block.number
6969

7070
// get block singer records
71-
// try{
72-
// const epochNumber = await kit.getEpochNumberOfBlock(block.number)
73-
// const election = await kit.contracts.getElection()
74-
// const validatorSet = await election.getElectedValidators(epochNumber)
75-
// const validators = await kit.contracts.getValidators()
76-
// const epochSize = await validators.getEpochSize()
77-
// // console.log(validatorSet)
78-
79-
// const electionRC = new ElectionResultsCache(election, epochSize.toNumber())
80-
// for (let v in validatorSet){
81-
// let record:any = {
82-
// blockNumber: block.number,
83-
// signer: validatorSet[v].signer,
84-
// exist: await electionRC.signedParent(validatorSet[v].signer, block)
85-
// }
86-
// ValidatorRecords.insert(record);
87-
// }
88-
// // const blockExtraData = parseBlockExtraData(block.extraData);
89-
// // console.log(blockExtraData);
90-
// }
91-
// catch(e){
92-
// console.log(e)
93-
// }
71+
try{
72+
const epochNumber = await kit.getEpochNumberOfBlock(block.number)
73+
const election = await kit.contracts.getElection()
74+
const validatorSet = await election.getElectedValidators(epochNumber)
75+
const validators = await kit.contracts.getValidators()
76+
const epochSize = await validators.getEpochSize()
77+
// console.log(validatorSet)
78+
79+
const electionRC = new ElectionResultsCache(election, epochSize.toNumber())
80+
for (let v in validatorSet){
81+
let record:any = {
82+
blockNumber: block.number,
83+
signer: validatorSet[v].signer,
84+
exist: await electionRC.signedParent(validatorSet[v].signer, block)
85+
}
86+
ValidatorRecords.insert(record);
87+
}
88+
block.hasSingers = true
89+
// const blockExtraData = parseBlockExtraData(block.extraData);
90+
// console.log(blockExtraData);
91+
}
92+
catch(e){
93+
console.log(e)
94+
}
95+
9496
// get transactions hash
9597
if (block.transactions.length > 0) {
9698
for (let j = 0; j < block.transactions.length; j++) {

imports/api/graphql/resolvers.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,67 @@ export default {
142142
});
143143

144144
return validatorSet
145-
}
145+
},
146+
async downtime(_, { address, pageSize = 20, page = 1 }, context, info){
147+
const totalCounts = ValidatorRecords.find({signer:address}).count();
148+
const pipeline = [
149+
{
150+
'$match': {
151+
'signer': address,
152+
'exist': false
153+
}
154+
}, {
155+
'$lookup': {
156+
'from': 'blocks',
157+
'localField': 'blockNumber',
158+
'foreignField': 'number',
159+
'as': 'block'
160+
}
161+
}, {
162+
'$sort': {
163+
'blockNumber': -1
164+
}
165+
}, {
166+
'$skip': (page - 1) * pageSize
167+
}, {
168+
'$limit': pageSize
169+
}, {
170+
'$unwind': {
171+
'path': '$block'
172+
}
173+
}
174+
]
175+
const records = await ValidatorRecords.rawCollection().aggregate(pipeline).toArray()
176+
177+
let blocks = new Array()
178+
records.forEach(record => {
179+
blocks.push(record.block)
180+
});
181+
182+
return {
183+
pageSize: pageSize,
184+
page: page,
185+
blocks,
186+
totalCounts: totalCounts,
187+
cursor: blocks.length ? blocks[blocks.length - 1].number : null,
188+
hasMore: blocks.length ? blocks[blocks.length - 1].number != 1 : false,
189+
};
190+
},
191+
validatedBlocks(_, { address, pageSize = 20, page = 1 }, context, info){
192+
const totalCounts = Blocks.find({miner:address}).count()
193+
const blocks = Blocks.find(
194+
{miner:address},
195+
{ sort: { number: -1 }, limit: pageSize, skip: (page - 1) * pageSize }
196+
).fetch()
197+
return {
198+
pageSize: pageSize,
199+
page: page,
200+
blocks,
201+
totalCounts: totalCounts,
202+
cursor: blocks.length ? blocks[blocks.length - 1].number : null,
203+
hasMore: blocks.length ? blocks[blocks.length - 1].number != 1 : false,
204+
};
205+
},
146206
},
147207
Block: {
148208
transactions(parent) {
@@ -162,7 +222,7 @@ export default {
162222
}
163223
return miner
164224
},
165-
signers(parent) {
225+
async signers(parent) {
166226
const pipeline = [
167227
{
168228
'$match': {
@@ -181,8 +241,8 @@ export default {
181241
}
182242
}
183243
]
184-
const signerRecords = ValidatorRecords.rawCollection().aggregate(pipeline).toArray()
185-
console.log(signerRecords);
244+
const signerRecords = await ValidatorRecords.rawCollection().aggregate(pipeline).toArray()
245+
// console.log(signerRecords);
186246
return signerRecords
187247
}
188248
},

imports/api/graphql/schema.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const typeDefs = gql`
164164
score: Float!
165165
signerAccount: Account
166166
signer: String!
167-
validatorGroup: ValidatorGroup!
167+
validatorGroup: ValidatorGroup
168168
}
169169
170170
type Query {
@@ -190,6 +190,16 @@ const typeDefs = gql`
190190
pageSize: Int,
191191
page: Int
192192
): TransactionList
193+
validatedBlocks(
194+
address: String
195+
pageSize: Int
196+
page: Int
197+
): BlockList!
198+
downtime(
199+
address: String
200+
pageSize: Int
201+
page: Int
202+
): BlockList!
193203
account(address: String!): Account
194204
validatorGroup(address:String!): ValidatorGroup
195205
validator(address:String!): Validator

imports/startup/server/create-indexes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import { Validators, ValidatorRecords } from "../../api/validators/validators";
77
import { Contracts } from "../../api/contracts/contracts";
88

99
Blocks.rawCollection().createIndex({ number: -1 }, { unique: true });
10+
Blocks.rawCollection().createIndex({ miner: 1 });
11+
Blocks.rawCollection().createIndex({ number: -1, miner: 1 });
1012
Accounts.rawCollection().createIndex({ address: 1 }, { unique: true });
1113
Transactions.rawCollection().createIndex({ hash: 1 }, { unique: true });
1214
Transactions.rawCollection().createIndex({ blockNumber: -1 });
1315
Transactions.rawCollection().createIndex({ to: 1 });
1416
Transactions.rawCollection().createIndex({ from: 1 });
1517
ValidatorGroups.rawCollection().createIndex({ address: 1 }, { unique: true });
1618
Validators.rawCollection().createIndex({ address: 1 }, { unique: true });
17-
ValidatorRecords.rawCollection().createIndex({blockNumber:1, signer: 1}, {unique: true});
19+
ValidatorRecords.rawCollection().createIndex({blockNumber:-1, signer: 1}, {unique: true});
20+
ValidatorRecords.rawCollection().createIndex({blockNumber:-1});
21+
ValidatorRecords.rawCollection().createIndex({signer: 1});
22+
ValidatorRecords.rawCollection().createIndex({exist:1});
1823
Contracts.rawCollection().createIndex({ address: 1 }, { unique: true });

0 commit comments

Comments
 (0)