Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
feat: organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnygleason committed Oct 10, 2019
1 parent f96095f commit dac18a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 109 deletions.
170 changes: 62 additions & 108 deletions api/views/tourdesol/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {filter, reduce, orderBy} from 'lodash/fp';
import _ from 'lodash';

import {LAMPORT_SOL_RATIO} from '../../util';

Expand All @@ -12,16 +11,16 @@ const stages = [
id: 1,
title: 'Stage 1',
isTbd: true,
startDate: '2019-11-09T17:00:00.0Z',
endDate: '2019-11-13T17:00:00.0Z',
startDate: '2019-09-09T17:00:00.0Z',
endDate: '2019-09-13T17:00:00.0Z',
duration: TDS_DEFAULT_STAGE_LENGTH_BLOCKS,
},
{
id: 2,
title: 'Stage 2',
isTbd: true,
startDate: '2019-12-07T17:00:00.0Z',
endDate: '2019-12-11T17:00:00.0Z',
startDate: '2019-10-07T17:00:00.0Z',
endDate: '2019-10-11T17:00:00.0Z',
duration: TDS_DEFAULT_STAGE_LENGTH_BLOCKS,
},
{
Expand Down Expand Up @@ -77,36 +76,13 @@ export class TourDeSolIndexView {
inactiveValidators: inactiveValidatorsRaw.length,
};

///////////
let firstStake =
(activeValidatorsRaw &&
activeValidatorsRaw.length &&
activeValidatorsRaw[0].activatedStake) ||
0;

const [minVal, maxVal] = _(activeValidatorsRaw).reduce(
(a, x) => {
let stake = x.activatedStake;
return [Math.min(a[0], stake), Math.max(a[1], stake)];
},
[firstStake, firstStake],
const scoreParams = this.computeScoreParams(
activeValidatorsRaw,
isDemo,
lastSlot,
currentStage,
);

const maxValF = maxVal * 1.0; // the maximum observed stake
const minValF = minVal * 1.0; // the minimum observed stake
const spreadF = maxValF - minValF; // the spread between min and max

const currentBlock = !isDemo ? lastSlot : new Date().getTime() % 60000;
const maxBlock = !isDemo ? currentStage && currentStage.duration : 60000;

// the node's position based on block count only
const nonWeightedPosition = (currentBlock * 1.0) / (maxBlock * 1.0);
// maximum weight factor
const maxFactor = spreadF !== 0 ? 0.1 : 0.05;
// synthetic function for 'closeness' : sin(), which is max at middle of race
const weightFactor = Math.sin(nonWeightedPosition * Math.PI) * maxFactor;
///////////

const activeValidatorsPre = reduce((a, x) => {
const pubkey = x.nodePubkey;
const slot = x.currentSlot;
Expand All @@ -117,19 +93,7 @@ export class TourDeSolIndexView {
x.uptime.uptime &&
x.uptime.uptime.length &&
100.0 * parseFloat(x.uptime.uptime[0].percentage);

//////
// node position adjustment is based on stake, or random if no spread
let nodePosF =
spreadF !== 0
? (x.activatedStake * 1.0 - minValF) / spreadF
: Math.random();
const weightShare = nodePosF * weightFactor;
let weightedPosition = (nonWeightedPosition + weightShare) * 100.0;
weightedPosition = Math.max(Math.floor(weightedPosition), 0);
weightedPosition = Math.min(Math.ceil(weightedPosition), 100);
let score = weightedPosition;
//////
const score = this.computeNodeScore(x, scoreParams);

const validator = {
name,
Expand Down Expand Up @@ -160,66 +124,6 @@ export class TourDeSolIndexView {
return {lastScore: score, lastRank: rank, accum};
})({lastScore: 101, lastRank: 0, accum: []}, activeValidatorsRank);

// const timelineData = rawData.timelinePage;
// const timelineInfo = rawData.timelineInfo;
//
// const results = _.map(timelineData.results, result => {
// let [k, x] = result;
//
// const id = x.id;
// const slot = x.s;
// const tick_height = x.h;
// const entry_id = x.entry_id;
// const block_id = x.block_id;
// const leader = x.l;
// const timestamp = x.dt;
//
// const instructions = _.map(x.instructions, i => {
// const program_id = i.program_id;
// const keys = i.keys;
// const data = i.data;
//
// return {
// program_id,
// keys,
// data,
// };
// });
//
// return [
// k,
// {
// id,
// slot,
// tick_height,
// entry_id,
// block_id,
// leader,
// instructions,
// timestamp,
// },
// ];
// });
//
// const pageData = {
// timeline: timelineData.timeline,
// start: timelineData.start,
// results,
// length: timelineData.length,
// count: timelineData.count,
// next: timelineData.next,
// prev: timelineData.prev,
// timestamp: timelineData.dt,
// };
//
// const pageInfo = {
// timeline: timelineInfo.timeline,
// last: timelineInfo.last,
// first: timelineInfo.first,
// count: timelineInfo.count,
// timestamp: timelineInfo.dt,
// };

if (version === 'TourDeSolIndexView@latest' || version === __VERSION__) {
return {
__VERSION__,
Expand All @@ -229,8 +133,6 @@ export class TourDeSolIndexView {
stages,
activeStage,
slotsPerDay,
// pageData,
// pageInfo,
};
}

Expand All @@ -240,4 +142,56 @@ export class TourDeSolIndexView {
desiredVersion: version,
};
}

computeNodeScore(x, scoreParams) {
const {minValF, spreadF, maxBlock, maxFactor} = scoreParams;
const currentBlock = x.currentSlot;

// the node's position based on block count only
const nonWeightedPosition = (currentBlock * 1.0) / (maxBlock * 1.0);

// synthetic function for 'closeness' : sin(), which is max at middle of race
const weightFactor = Math.sin(nonWeightedPosition * Math.PI) * maxFactor;

const nodePosF =
spreadF !== 0
? (x.activatedStake * 1.0 - minValF) / spreadF
: Math.random();

const weightShare = nodePosF * weightFactor;
let weightedPosition = (nonWeightedPosition + weightShare) * 100.0;
weightedPosition = Math.max(Math.floor(weightedPosition), 0);
weightedPosition = Math.min(Math.ceil(weightedPosition), 100);

return weightedPosition;
}

computeScoreParams(validators, isDemo, lastSlot, currentStage) {
const firstStake =
(validators && validators.length && validators[0].activatedStake) || 0;

const [minVal, maxVal] = reduce((a, x) => {
let stake = x.activatedStake;
return [Math.min(a[0], stake), Math.max(a[1], stake)];
})([firstStake, firstStake], validators);

const maxValF = maxVal * 1.0; // the maximum observed stake
const minValF = minVal * 1.0; // the minimum observed stake
const spreadF = maxValF - minValF; // the spread between min and max

const currentBlock = !isDemo ? lastSlot : new Date().getTime() % 60000;
const maxBlock = !isDemo ? currentStage && currentStage.duration : 60000;

// maximum weight factor
const maxFactor = spreadF !== 0 ? 0.1 : 0.05;

return {
maxValF,
minValF,
spreadF,
currentBlock,
maxBlock,
maxFactor,
};
}
}
2 changes: 1 addition & 1 deletion src/v2/components/TourDeSol/Ranking/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Ranking = ({activeValidators}: {activeValidators: Array}) => {

const renderNode = node => {
const {name, pubkey, avatarUrl, slot, activatedStake, score} = node;
const title = `SLOT: ${slot} | STAKE: ${activatedStake} | SCORE: ${score}`;
const title = `SLOT: ${slot} | STAKE: ${activatedStake.toFixed(8)} | SCORE: ${score}`;

return (
<li key={pubkey} className={classes.item}>
Expand Down

0 comments on commit dac18a3

Please sign in to comment.