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

Fetch keybase with timer settings #501

Merged
merged 10 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [#487] Fixed typo in the query which make validator power change tx lookup failed.
* Fixed an issue on displaying individual transaction.
* [#497] Fetch keybase with timer settings
## v0.41.x-7 (Stargate compatible)

* [#472] Fix missing transactions
Expand Down
42 changes: 7 additions & 35 deletions imports/api/blocks/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ getRemovedValidators = (prevValidators, validators) => {
return prevValidators;
}


getValidatorFromConsensusKey = (validators, consensusKey) => {
for (v in validators){
try {
Expand All @@ -42,13 +43,14 @@ getValidatorFromConsensusKey = (validators, consensusKey) => {
return null;
}

getValidatorProfileUrl = (identity) => {

export const getValidatorProfileUrl = (identity) => {
console.log("Get validator avatar.")
if (identity.length == 16){
let response = HTTP.get(`https://keybase.io/_/api/1.0/user/lookup.json?key_suffix=${identity}&fields=pictures`)
if (response.statusCode == 200) {
let them = response.data.them
return them && them.length && them[0].pictures && them[0].pictures.primary && them[0].pictures.primary.url;
let them = response?.data?.them
return them && them.length && them[0]?.pictures && them[0]?.pictures?.primary && them[0]?.pictures?.primary?.url;
} else {
console.log(JSON.stringify(response))
}
Expand All @@ -63,6 +65,7 @@ getValidatorProfileUrl = (identity) => {
}
}


getValidatorUptime = async (validatorSet) => {

// get validator uptime
Expand Down Expand Up @@ -500,7 +503,7 @@ Meteor.methods({

if (valData.description && valData.description.identity){
try{
valData.profile_url = getValidatorProfileUrl(valData.description.identity)
valData.profile_url = getValidatorProfileUrl(valData.description.identity)
}
catch (e){
console.log("Error fetching keybase: %o", e)
Expand Down Expand Up @@ -628,38 +631,7 @@ Meteor.methods({
getValidatorUptime(validatorSet)
}

// fetching keybase every base on keybaseFetchingInterval settings
// default to every 5 hours

if (height == curr+1){

// check the last fetching time

let now = Date.now();
let lastKeybaseFetchTime = Date.parse(chainStatus.lastKeybaseFetchTime) || 0
console.log("Now: %o", now)
console.log("Last fetch time: %o", lastKeybaseFetchTime)

if (!lastKeybaseFetchTime || (now - lastKeybaseFetchTime) > Meteor.settings.params.keybaseFetchingInterval ){
console.log('Fetching keybase...')
// eslint-disable-next-line no-loop-func
Validators.find({}).forEach(async (validator) => {
try {
if (validator.description && validator.description.identity){
let profileUrl = getValidatorProfileUrl(validator.description.identity)
if (profileUrl) {
bulkValidators.find({address: validator.address}).upsert().updateOne({$set:{'profile_url':profileUrl}});
}
}
} catch (e) {
console.log("Error fetching Keybase for %o: %o", validator.address, e)
}
})

Chain.update({chainId:block.block.header.chainId}, {$set:{lastKeybaseFetchTime:new Date().toUTCString()}});
}

}

let endFindValidatorsNameTime = new Date();
console.log("Get validators name time: "+((endFindValidatorsNameTime-startFindValidatorsNameTime)/1000)+"seconds.");
Expand Down
61 changes: 60 additions & 1 deletion imports/api/validators/server/methods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Transactions } from '../../transactions/transactions.js';
import { Blockscon } from '../../blocks/blocks.js';
import { Validators } from '../../validators/validators.js';
import { Chain } from '../../chain/chain.js';
import { getValidatorProfileUrl } from '../../blocks/server/methods.js';


Meteor.methods({
'Validators.findCreateValidatorTime': function(address){
Expand Down Expand Up @@ -44,5 +48,60 @@ Meteor.methods({
console.log(url);
console.log("Getting error: %o when fetching from %o", e, url);
}
}
},

'Validators.fetchKeybase'(address) {
this.unblock();
// fetching keybase every base on keybaseFetchingInterval settings
// default to every 5 hours

let url = RPC + '/status';
let chainId;
try {
let response = HTTP.get(url);
let status = JSON.parse(response?.content);
chainId = (status?.result?.node_info?.network);
}
catch (e) {
console.log("Error getting chainId for keybase fetching")
}
let chainStatus = Chain.findOne({ chainId});
const bulkValidators = Validators.rawCollection().initializeUnorderedBulkOp();

let lastKeybaseFetchTime = Date.parse(chainStatus?.lastKeybaseFetchTime) ?? 0
console.log("Last fetch time: %o", lastKeybaseFetchTime)

console.log('Fetching keybase...')
// eslint-disable-next-line no-loop-func
Validators.find({}).forEach(async (validator) => {
try {
if (validator?.description && validator?.description?.identity) {
let profileUrl = getValidatorProfileUrl(validator?.description?.identity)
if (profileUrl) {
bulkValidators.find({ address: validator?.address }).upsert().updateOne({ $set: { 'profile_url': profileUrl } });
if (bulkValidators.length > 0) {
bulkValidators.execute((err, result) => {
if (err) {
console.log(`Error when updating validator profile_url ${err}`);
}
if (result) {
console.log('Validator profile_url has been updated!');
}
});
}
}
}
} catch (e) {
console.log("Error fetching Keybase for %o: %o", validator?.address, e)
}
})
try{
Chain.update({ chainId }, { $set: { lastKeybaseFetchTime: new Date().toUTCString() } });
}
catch(e){
console.log("Error when updating lastKeybaseFetchTime")
}

}

});
22 changes: 19 additions & 3 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ timerProposalsResults = 0;
timerMissedBlock = 0;
timerDelegation = 0;
timerAggregate = 0;
timerFetchKeybase = 0;

const DEFAULTSETTINGS = '/default_settings.json';

Expand Down Expand Up @@ -98,6 +99,17 @@ updateMissedBlocks = () => {
});
}

fetchKeybase = () => {
Meteor.call('Validators.fetchKeybase', (error, result) => {
if (error) {
console.log("Error when fetching Keybase" + error)
}
if (result) {
console.log("Keybase profile_url updated ", result);
}
});
}

getDelegations = () => {
Meteor.call('delegations.getDelegations', (error, result) => {
if (error){
Expand Down Expand Up @@ -200,12 +212,12 @@ Meteor.startup(async function(){
updateChainStatus();
}, Meteor.settings.params.statusInterval);

if (Meteor.settings.public.modules.gov) {
timerProposal = Meteor.setInterval(function () {
if (Meteor.settings.public.modules.gov){
timerProposal = Meteor.setInterval(function (){
getProposals();
}, Meteor.settings.params.proposalInterval);

timerProposalsResults = Meteor.setInterval(function () {
timerProposalsResults = Meteor.setInterval(function (){
getProposalsResults();
}, Meteor.settings.params.proposalInterval);
}
Expand All @@ -214,6 +226,10 @@ Meteor.startup(async function(){
updateMissedBlocks();
}, Meteor.settings.params.missedBlocksInterval);

timerFetchKeybase = Meteor.setInterval(function (){
fetchKeybase();
}, Meteor.settings.params.keybaseFetchingInterval);

// timerDelegation = Meteor.setInterval(function(){
// getDelegations();
// }, Meteor.settings.params.delegationInterval);
Expand Down