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

Commit 1db4b64

Browse files
kwunyeungMonikaCat
andauthored
Fetch keybase with timer settings (#501) (#516)
* 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 * fix lint Co-authored-by: Magic Cat <[email protected]>
1 parent dd79875 commit 1db4b64

File tree

5 files changed

+91
-39
lines changed

5 files changed

+91
-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: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/* eslint-disable camelcase */
2+
13
import { Meteor } from 'meteor/meteor';
24
import { Transactions } from '../../transactions/transactions.js';
35
import { Blockscon } from '../../blocks/blocks.js';
6+
import { Validators } from '../../validators/validators.js';
7+
import { Chain } from '../../chain/chain.js';
8+
import { getValidatorProfileUrl } from '../../blocks/server/methods.js';
9+
410

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

imports/ui/transactions/TransactionRow.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable camelcase */
12

23
import React, { Component } from 'react';
34
import { Link } from 'react-router-dom';

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)