Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 3af17e8

Browse files
MonikaCatkwunyeung
authored andcommitted
Fetch keybase with timer settings (#501)
* Update methods.js * Add timerFetchKeybase * Updated methofs.js * Updated methods.js * Update message * Update CHANGELOG.md * Add fetching keybase when validators are first indexed * Moved getValidatorProfileUrl method * . * Updated method
1 parent dd79875 commit 3af17e8

File tree

4 files changed

+88
-39
lines changed

4 files changed

+88
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [UNRELEASED]
44
* [#484] Replaced delegation list displayed under validator page with total number of delegations.
5+
* [#497] Fetch keybase with timer settings
56

67
## v0.41.x-12
78
* [#387] Added Bluetooth Ledger support
@@ -22,6 +23,7 @@
2223
* Fixed an issue on displaying individual transaction.
2324
* Updated Ledger app name checking so that it will follows the value defined in settings.
2425
* [#213] Updated `@ledgerhq/hw-transport-webusb` pkg to v5.49.0 to fix Ledger errors on Windows10
26+
2527
## v0.41.x-7 (Stargate compatible)
2628

2729
* [#472] Fix missing transactions

imports/api/blocks/server/methods.js

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ getRemovedValidators = (prevValidators, validators) => {
2626
return prevValidators;
2727
}
2828

29+
2930
getValidatorFromConsensusKey = (validators, consensusKey) => {
3031
for (v in validators){
3132
try {
@@ -42,13 +43,14 @@ getValidatorFromConsensusKey = (validators, consensusKey) => {
4243
return null;
4344
}
4445

45-
getValidatorProfileUrl = (identity) => {
46+
47+
export const getValidatorProfileUrl = (identity) => {
4648
console.log("Get validator avatar.")
4749
if (identity.length == 16){
4850
let response = HTTP.get(`https://keybase.io/_/api/1.0/user/lookup.json?key_suffix=${identity}&fields=pictures`)
4951
if (response.statusCode == 200) {
50-
let them = response.data.them
51-
return them && them.length && them[0].pictures && them[0].pictures.primary && them[0].pictures.primary.url;
52+
let them = response?.data?.them
53+
return them && them.length && them[0]?.pictures && them[0]?.pictures?.primary && them[0]?.pictures?.primary?.url;
5254
} else {
5355
console.log(JSON.stringify(response))
5456
}
@@ -63,6 +65,7 @@ getValidatorProfileUrl = (identity) => {
6365
}
6466
}
6567

68+
6669
getValidatorUptime = async (validatorSet) => {
6770

6871
// get validator uptime
@@ -500,7 +503,7 @@ Meteor.methods({
500503

501504
if (valData.description && valData.description.identity){
502505
try{
503-
valData.profile_url = getValidatorProfileUrl(valData.description.identity)
506+
valData.profile_url = getValidatorProfileUrl(valData.description.identity)
504507
}
505508
catch (e){
506509
console.log("Error fetching keybase: %o", e)
@@ -628,38 +631,7 @@ Meteor.methods({
628631
getValidatorUptime(validatorSet)
629632
}
630633

631-
// fetching keybase every base on keybaseFetchingInterval settings
632-
// default to every 5 hours
633-
634-
if (height == curr+1){
635634

636-
// check the last fetching time
637-
638-
let now = Date.now();
639-
let lastKeybaseFetchTime = Date.parse(chainStatus.lastKeybaseFetchTime) || 0
640-
console.log("Now: %o", now)
641-
console.log("Last fetch time: %o", lastKeybaseFetchTime)
642-
643-
if (!lastKeybaseFetchTime || (now - lastKeybaseFetchTime) > Meteor.settings.params.keybaseFetchingInterval ){
644-
console.log('Fetching keybase...')
645-
// eslint-disable-next-line no-loop-func
646-
Validators.find({}).forEach(async (validator) => {
647-
try {
648-
if (validator.description && validator.description.identity){
649-
let profileUrl = getValidatorProfileUrl(validator.description.identity)
650-
if (profileUrl) {
651-
bulkValidators.find({address: validator.address}).upsert().updateOne({$set:{'profile_url':profileUrl}});
652-
}
653-
}
654-
} catch (e) {
655-
console.log("Error fetching Keybase for %o: %o", validator.address, e)
656-
}
657-
})
658-
659-
Chain.update({chainId:block.block.header.chainId}, {$set:{lastKeybaseFetchTime:new Date().toUTCString()}});
660-
}
661-
662-
}
663635

664636
let endFindValidatorsNameTime = new Date();
665637
console.log("Get validators name time: "+((endFindValidatorsNameTime-startFindValidatorsNameTime)/1000)+"seconds.");

imports/api/validators/server/methods.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Meteor } from 'meteor/meteor';
22
import { Transactions } from '../../transactions/transactions.js';
33
import { Blockscon } from '../../blocks/blocks.js';
4+
import { Validators } from '../../validators/validators.js';
5+
import { Chain } from '../../chain/chain.js';
6+
import { getValidatorProfileUrl } from '../../blocks/server/methods.js';
7+
48

59
Meteor.methods({
610
'Validators.findCreateValidatorTime': function(address){
@@ -38,5 +42,60 @@ Meteor.methods({
3842
console.log(url);
3943
console.log("Getting error: %o when getting delegations count from %o", e, url);
4044
}
41-
}
45+
},
46+
47+
'Validators.fetchKeybase'(address) {
48+
this.unblock();
49+
// fetching keybase every base on keybaseFetchingInterval settings
50+
// default to every 5 hours
51+
52+
let url = RPC + '/status';
53+
let chainId;
54+
try {
55+
let response = HTTP.get(url);
56+
let status = JSON.parse(response?.content);
57+
chainId = (status?.result?.node_info?.network);
58+
}
59+
catch (e) {
60+
console.log("Error getting chainId for keybase fetching")
61+
}
62+
let chainStatus = Chain.findOne({ chainId});
63+
const bulkValidators = Validators.rawCollection().initializeUnorderedBulkOp();
64+
65+
let lastKeybaseFetchTime = Date.parse(chainStatus?.lastKeybaseFetchTime) ?? 0
66+
console.log("Last fetch time: %o", lastKeybaseFetchTime)
67+
68+
console.log('Fetching keybase...')
69+
// eslint-disable-next-line no-loop-func
70+
Validators.find({}).forEach(async (validator) => {
71+
try {
72+
if (validator?.description && validator?.description?.identity) {
73+
let profileUrl = getValidatorProfileUrl(validator?.description?.identity)
74+
if (profileUrl) {
75+
bulkValidators.find({ address: validator?.address }).upsert().updateOne({ $set: { 'profile_url': profileUrl } });
76+
if (bulkValidators.length > 0) {
77+
bulkValidators.execute((err, result) => {
78+
if (err) {
79+
console.log(`Error when updating validator profile_url ${err}`);
80+
}
81+
if (result) {
82+
console.log('Validator profile_url has been updated!');
83+
}
84+
});
85+
}
86+
}
87+
}
88+
} catch (e) {
89+
console.log("Error fetching Keybase for %o: %o", validator?.address, e)
90+
}
91+
})
92+
try{
93+
Chain.update({ chainId }, { $set: { lastKeybaseFetchTime: new Date().toUTCString() } });
94+
}
95+
catch(e){
96+
console.log("Error when updating lastKeybaseFetchTime")
97+
}
98+
99+
}
100+
42101
});

server/main.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ timerProposalsResults = 0;
2121
timerMissedBlock = 0;
2222
timerDelegation = 0;
2323
timerAggregate = 0;
24+
timerFetchKeybase = 0;
2425

2526
const DEFAULTSETTINGS = '/default_settings.json';
2627

@@ -98,6 +99,17 @@ updateMissedBlocks = () => {
9899
});
99100
}
100101

102+
fetchKeybase = () => {
103+
Meteor.call('Validators.fetchKeybase', (error, result) => {
104+
if (error) {
105+
console.log("Error when fetching Keybase" + error)
106+
}
107+
if (result) {
108+
console.log("Keybase profile_url updated ", result);
109+
}
110+
});
111+
}
112+
101113
getDelegations = () => {
102114
Meteor.call('delegations.getDelegations', (error, result) => {
103115
if (error){
@@ -200,12 +212,12 @@ Meteor.startup(async function(){
200212
updateChainStatus();
201213
}, Meteor.settings.params.statusInterval);
202214

203-
if (Meteor.settings.public.modules.gov) {
204-
timerProposal = Meteor.setInterval(function () {
215+
if (Meteor.settings.public.modules.gov){
216+
timerProposal = Meteor.setInterval(function (){
205217
getProposals();
206218
}, Meteor.settings.params.proposalInterval);
207219

208-
timerProposalsResults = Meteor.setInterval(function () {
220+
timerProposalsResults = Meteor.setInterval(function (){
209221
getProposalsResults();
210222
}, Meteor.settings.params.proposalInterval);
211223
}
@@ -214,6 +226,10 @@ Meteor.startup(async function(){
214226
updateMissedBlocks();
215227
}, Meteor.settings.params.missedBlocksInterval);
216228

229+
timerFetchKeybase = Meteor.setInterval(function (){
230+
fetchKeybase();
231+
}, Meteor.settings.params.keybaseFetchingInterval);
232+
217233
// timerDelegation = Meteor.setInterval(function(){
218234
// getDelegations();
219235
// }, Meteor.settings.params.delegationInterval);

0 commit comments

Comments
 (0)