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

Updated Average Block Time #548

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -2,6 +2,7 @@

## [UNRELEASED]
* [#523] Implemented readable messages for IBC messages
* [#542] Removed `startHeight` value from average block time and updated calculations to get the average block time within `averageBlockTimeWindow` instead of calculating it from genesis time.

## [v0.41.x-14.2]
* Fixes Ledger WebUSB + Chrome 91.x issue (https://github.com/LedgerHQ/ledgerjs/issues/607)
Expand Down
1 change: 1 addition & 0 deletions both/i18n/en-us.i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ chainStatus:
outOfValidators: 'out of {$totalValidators} validators'
onlineVotingPower: 'Online Voting Power'
fromTotalStakes: '{$percent} from {$totalStakes} {$denomPlural}'
averageBlockTimeWindow: '{$avgBlockTimeWindow} Blocks'
analytics:
blockTimeHistory: 'Block Time History'
averageBlockTime: 'Average Block Time'
Expand Down
19 changes: 14 additions & 5 deletions imports/api/blocks/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,29 @@ Meteor.methods({
let chainStatus = Chain.findOne({chainId:block.block.header.chain_id});
let lastSyncedTime = chainStatus?chainStatus.lastSyncedTime:0;
let timeDiff;
let blockTime = Meteor.settings.params.defaultBlockTime;
let blockTime = 0;
let res;
if (lastSyncedTime){
let blockDiff = Meteor.settings.public.averageBlockTimeWindow;
let pastBlockHeight = (height - blockDiff > 0) ? (height - blockDiff) : (height - 1);
let getPastBlock = `${API}/blocks/${pastBlockHeight}`;
try{
res = HTTP.get(getPastBlock);
}
catch(e){
console.log(e)
}
let pastBlockTime = new Date(JSON.parse(res?.content)?.block?.header?.time);
let dateLatest = new Date(blockData.time);
let dateLast = new Date(lastSyncedTime);
let genesisTime = new Date(Meteor.settings.public.genesisTime);
timeDiff = Math.abs(dateLatest.getTime() - dateLast.getTime());
// blockTime = (chainStatus.blockTime * (blockData.height - 1) + timeDiff) / blockData.height;
blockTime = (dateLatest.getTime() - genesisTime.getTime()) / blockData.height;
blockTime = (height - blockDiff > 0) ? ((dateLatest.getTime() - pastBlockTime.getTime()) / blockDiff) : (dateLatest.getTime() - pastBlockTime.getTime());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MonikaCat can you explain the whole logic here?

In the original implementation, averageBlcokTime = lastBlockTime - genesisTime / blockHeight. This is always true for the idea of average block time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the genesis time value had been removed from the settings.json file, we cannot calculate the average block time by using averageBlockTime = lastBlockTime - genesisTime / blockHeight. To handle the calculations, I initially added averageBlockTimeWindow in settings.json file to calculate the average block time of last n blocks...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I changed the approach and I've just updated the code to calculate average block time starting from the startHeight height set in params in settings.json file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... I have missed the genesisTime in the default_settings.json. It should be there as this is how the average block time is being calculated. Querying from chain is not feasible as the genesis could be huge. The size on cosmoshub-4 can't even give a full response if we query from the api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Alright I will update the code to include genesisTime calculations again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, how does it do now?

}

let endGetValidatorsTime = new Date();
console.log("Get height validators time: "+((endGetValidatorsTime-startGetValidatorsTime)/1000)+"seconds.");

Chain.update({chainId:block.block.header.chainId}, {$set:{lastSyncedTime:blockData.time, blockTime:blockTime}});
Chain.update({ chainId: block.block.header.chain_id}, {$set:{lastSyncedTime:blockData.time, blockTime:blockTime}});

analyticsData.averageBlockTime = blockTime;
analyticsData.timeDiff = timeDiff;
Expand Down
2 changes: 1 addition & 1 deletion imports/ui/home/ChainStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ChainStatus extends React.Component {
totalNumValidators: 0,
avgBlockTimeType: "",
avgVotingPowerType: "",
blockTimeText: <T>chainStatus.all</T>,
blockTimeText: <T _purify={false} avgBlockTimeWindow={numbro(Meteor.settings.public.averageBlockTimeWindow).format({ thousandSeparated: true })}>chainStatus.averageBlockTimeWindow</T>,
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
votingPowerText: <T>chainStatus.now</T>
}
}
Expand Down