Moved orchestrator info to global state, smarter caching when getting it

This commit is contained in:
Marco van Dijk 2022-04-17 11:44:37 +02:00
parent 6572385906
commit 362660d690
9 changed files with 162 additions and 23 deletions

View File

@ -1251,6 +1251,25 @@ apiRouter.get("/getAllOrchScores", async (req, res) => {
}
});
// Returns entire orch info cache
apiRouter.get("/getAllOrchInfo", async (req, res) => {
try {
res.send(orchestratorCache);
} catch (err) {
res.status(400).send(err);
}
});
// Returns entire delegator info cache
apiRouter.get("/getAllDelInfo", async (req, res) => {
try {
res.send(delegatorCache);
} catch (err) {
res.status(400).send(err);
}
});
export default apiRouter;

View File

@ -21,6 +21,7 @@ export const RECEIVE_QUOTES = "RECEIVE_QUOTES";
export const RECEIVE_BLOCKCHAIN_DATA = "RECEIVE_BLOCKCHAIN_DATA";
export const RECEIVE_EVENTS = "RECEIVE_EVENTS";
export const RECEIVE_CURRENT_ORCHESTRATOR = "RECEIVE_CURRENT_ORCHESTRATOR";
export const CACHE_ORCHESTRATOR = "CACHE_ORCHESTRATOR";
export const RECEIVE_ORCHESTRATOR = "RECEIVE_ORCHESTRATOR";
export const CLEAR_ORCHESTRATOR = "CLEAR_ORCHESTRATOR";
export const RECEIVE_TICKETS = "RECEIVE_TICKETS";
@ -29,6 +30,8 @@ export const SET_ALL_ENS_INFO = "SET_ALL_ENS_INFO";
export const SET_ALL_ENS_DOMAINS = "SET_ALL_ENS_DOMAINS";
export const SET_ALL_THREEBOX_INFO = "SET_ALL_THREEBOX_INFO";
export const SET_ALL_ORCH_SCORES = "SET_ALL_ORCH_SCORES";
export const SET_ALL_ORCH_INFO = "SET_ALL_ORCH_INFO";
export const SET_ALL_DEL_INFO = "SET_ALL_DEL_INFO";
const setQuotes = message => ({
type: RECEIVE_QUOTES, message
@ -42,6 +45,9 @@ const setEvents = message => ({
const setCurrentOrchestratorInfo = message => ({
type: RECEIVE_CURRENT_ORCHESTRATOR, message
});
const cacheNewOrch = message => ({
type: CACHE_ORCHESTRATOR, message
});
const setOrchestratorInfo = message => ({
type: RECEIVE_ORCHESTRATOR, message
});
@ -66,6 +72,14 @@ const setAllThreeBoxInfo = message => ({
const setAllOrchScores = message => ({
type: SET_ALL_ORCH_SCORES, message
});
const setAllOrchInfo = message => ({
type: SET_ALL_ORCH_INFO, message
});
const setAllDelInfo = message => ({
type: SET_ALL_DEL_INFO, message
});
export const getQuotes = () => async dispatch => {
@ -145,13 +159,13 @@ export const getEvents = () => async dispatch => {
tmpAmount.toFixed(2) + " LPT stake",
"round " + tmpWhen
]
eventDescription = <Ticket icon={"🚀"} subtext={subtext} descriptions={descriptions} />
eventDescription = <Ticket seed={currentTx+descriptions} icon={"🚀"} subtext={subtext} descriptions={descriptions} />
} else {
const subtext = "reactivated";
const descriptions = [
"round " + tmpWhen
]
eventDescription = <Ticket icon={"🚀"} subtext={subtext} descriptions={descriptions} />
eventDescription = <Ticket seed={currentTx+descriptions} icon={"🚀"} subtext={subtext} descriptions={descriptions} />
}
}
// Lone Unbond => Unbond Event
@ -164,7 +178,7 @@ export const getEvents = () => async dispatch => {
"round " + tmpWhen
]
eventDescription =
<Ticket icon={"❌"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"❌"} subtext={subtext} descriptions={descriptions} />
}
// Lone Bond => Stake Event
else if (eventContainsBond) {
@ -180,7 +194,7 @@ export const getEvents = () => async dispatch => {
tmpAmount.toFixed(2) + " LPT"
]
eventDescription =
<Ticket icon={"⌛"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"⌛"} subtext={subtext} descriptions={descriptions} />
}
// Fill description of Stake Event if it wasn't set yet
@ -191,7 +205,7 @@ export const getEvents = () => async dispatch => {
tmpAmount.toFixed(2) + " LPT"
]
eventDescription =
<Ticket icon={"⌛"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"⌛"} subtext={subtext} descriptions={descriptions} />
} else if (eventFrom === eventTo) {
eventFrom = "";
const subtext = "changed stake";
@ -199,14 +213,14 @@ export const getEvents = () => async dispatch => {
tmpAmount.toFixed(2) + " LPT"
]
eventDescription =
<Ticket icon={"⌛"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"⌛"} subtext={subtext} descriptions={descriptions} />
} else {
const subtext = "moved stake";
const descriptions = [
tmpAmount.toFixed(2) + " LPT"
]
eventDescription =
<Ticket icon={"⌛"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"⌛"} subtext={subtext} descriptions={descriptions} />
}
}
@ -263,7 +277,7 @@ export const getEvents = () => async dispatch => {
"round " + eventObj.data.withdrawRound
]
const txt =
<Ticket icon={"🏦"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"🏦"} subtext={subtext} descriptions={descriptions} />
finalEventList.push({
eventType: "Withdraw",
eventDescription: txt,
@ -287,7 +301,7 @@ export const getEvents = () => async dispatch => {
amount.toFixed(4) + " Eth"
]
const txt =
<Ticket icon={"🏦"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"🏦"} subtext={subtext} descriptions={descriptions} />
finalEventList.push({
eventType: "Withdraw",
eventDescription: txt,
@ -313,7 +327,7 @@ export const getEvents = () => async dispatch => {
amount2.toFixed(2) + "% on transcoding fees"
]
const txt =
<Ticket icon={"🔄"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"🔄"} subtext={subtext} descriptions={descriptions} />
finalEventList.push({
eventType: "Update",
eventDescription: txt,
@ -343,7 +357,7 @@ export const getEvents = () => async dispatch => {
"+" + amount2.toFixed(4) + " Eth fees"
]
let txt =
<Ticket icon={"💰"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"💰"} subtext={subtext} descriptions={descriptions} />
finalEventList.push({
eventType: "Claim",
eventDescription: txt,
@ -367,7 +381,7 @@ export const getEvents = () => async dispatch => {
"+" + amount1.toFixed(2) + " LPT" + (Math.floor(amount1) == 69 ? "... Nice!" : "")
]
const txt =
<Ticket icon={"💸"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"💸"} subtext={subtext} descriptions={descriptions} />
finalEventList.push({
eventType: "Reward",
eventDescription: txt,
@ -469,7 +483,7 @@ export const getTickets = () => async dispatch => {
"+" + amount.toFixed(4) + " Eth"
]
const txt =
<Ticket icon={"🎟️"} subtext={subtext} descriptions={descriptions} />
<Ticket seed={currentTx+descriptions} icon={"🎟️"} subtext={subtext} descriptions={descriptions} />
finalTicketList.push({
eventType: "RedeemTicket",
eventDescription: txt,
@ -526,11 +540,14 @@ export const getOrchestratorInfo = (orchAddr) => async dispatch => {
const data = await response.json();
if (response.ok) {
if (data && data.id) {
console.log(data);
dispatch(cacheNewOrch(data));
return dispatch(setOrchestratorInfo(data));
} else {
const response = await apiUtil.getOrchestratorByDelegator(orchAddr);
const data = await response.json();
if (response.ok) {
dispatch(cacheNewOrch(data));
return dispatch(setOrchestratorInfo(data));
}
}
@ -538,6 +555,10 @@ export const getOrchestratorInfo = (orchAddr) => async dispatch => {
return dispatch(receiveErrors(data));
};
export const setCachedOrch = (orchObj) => async dispatch => {
return dispatch(setOrchestratorInfo(orchObj));
};
export const clearOrchestrator = () => async dispatch => {
return dispatch(clearOrchestratorInfo({}));
};
@ -587,9 +608,26 @@ export const getOrchestratorScores = (year, month) => async dispatch => {
export const getAllOrchScores = () => async dispatch => {
const response = await apiUtil.getAllOrchScores();
const data = await response.json();
console.log(data);
if (response.ok) {
return dispatch(setAllOrchScores(data));
}
return dispatch(receiveErrors(data));
};
export const getAllOrchInfo = () => async dispatch => {
const response = await apiUtil.getAllOrchInfo();
const data = await response.json();
if (response.ok) {
return dispatch(setAllOrchInfo(data));
}
return dispatch(receiveErrors(data));
};
export const getAllDelInfo = () => async dispatch => {
const response = await apiUtil.getAllDelInfo();
const data = await response.json();
if (response.ok) {
return dispatch(setAllDelInfo(data));
}
return dispatch(receiveErrors(data));
};

View File

@ -17,7 +17,7 @@ const Ticket = (obj) => {
<div className="stroke" style={{ flex: 2 }}>
{obj.descriptions.map(function (thisTextItem, i) {
return (
<p className="smallTxt">
<p className="smallTxt" key={obj.seed}>
{thisTextItem}
</p>
)

View File

@ -1,6 +1,6 @@
import React, { useState } from "react";
import {
getOrchestratorInfo, getEnsInfo, getThreeBoxInfo
getOrchestratorInfo, getEnsInfo, getThreeBoxInfo, setCachedOrch
} from "../actions/livepeer";
import { useDispatch, useSelector } from 'react-redux';
@ -126,7 +126,22 @@ const EventButtonAddress = (obj) => {
</button>
<span>{obj.name}</span>
{thisIcon}
<button className="selectOrch" style={{ padding: '0.5em', cursor: 'help' }} onClick={() => { dispatch(getOrchestratorInfo(obj.address)) }} >
<button className="selectOrch" style={{ padding: '0.5em', cursor: 'help' }} onClick={() => {
// Check if cached as an orchestrator
if (livepeer.orchInfo) {
for (const thisOrch of livepeer.orchInfo) {
if (thisOrch.id === obj.address) {
const now = new Date().getTime();
if (now - thisOrch.lastGet < 120000) {
dispatch(setCachedOrch(thisOrch));
return;
}
break;
}
}
}
dispatch(getOrchestratorInfo(obj.address));
}} >
{thisName}
</button>
</div>

View File

@ -23,9 +23,10 @@ const Livepeer = (obj) => {
console.log("Rendering Livepeer");
useEffect(() => {
if (prefill.get('orchAddr') && prefill.get('orchAddr') !== "") {
dispatch(getOrchestratorInfo(prefill.get('orchAddr')));
setSearchTerm(prefill.get('orchAddr'));
const searchOrch = prefill.get('orchAddr');
if (searchOrch && searchOrch !== "") {
dispatch(getOrchestratorInfo(searchOrch));
setSearchTerm(searchOrch);
}
}, [prefill]);

View File

@ -5,7 +5,8 @@ import {
} from "../actions/user";
import {
getQuotes, getBlockchainData, getEvents, getCurrentOrchestratorInfo, getTickets,
getAllEnsDomains, getAllEnsInfo, getAllThreeBoxInfo, getAllOrchScores
getAllEnsDomains, getAllEnsInfo, getAllThreeBoxInfo, getAllOrchScores, getAllOrchInfo,
getAllDelInfo
} from "../actions/livepeer";
import { login } from "../actions/session";
@ -43,6 +44,14 @@ const Startup = (obj) => {
dispatch(getAllThreeBoxInfo());
dispatch(getAllEnsDomains());
dispatch(getAllEnsInfo());
});
}
const refreshStaticProps = () => {
console.log("Refreshing global data...");
batch(() => {
dispatch(getAllOrchInfo());
dispatch(getAllDelInfo());
dispatch(getAllOrchScores());
});
}
@ -51,6 +60,7 @@ const Startup = (obj) => {
refreshLogin();
refreshAllZeData();
refreshENS();
refreshStaticProps();
setIsLoaded(true);
if (refreshInterval) {
const interval = setInterval(refreshAllZeData, refreshInterval);

View File

@ -202,7 +202,7 @@ const Tickets = (obj) => {
return (
<Accordion.Item
label={data.year + "-" + thisMonth + ": " + data.orchestrators.length + " orchestrators earned " + data.total.toFixed(4) + " Eth"}
label={data.year + "-" + thisMonth + ": " + data.orchestrators.length + " orchestrators earned " + data.total.toFixed(2) + " Eth"}
className="stroke"
key={data.year + "-" + data.month + "-" + data.total}>
<WinnerMonth

View File

@ -10,7 +10,10 @@ import {
SET_ALL_ENS_INFO,
SET_ALL_ENS_DOMAINS,
SET_ALL_THREEBOX_INFO,
SET_ALL_ORCH_SCORES
SET_ALL_ORCH_SCORES,
SET_ALL_ORCH_INFO,
SET_ALL_DEL_INFO,
CACHE_ORCHESTRATOR
} from "../../actions/livepeer";
export default (state = {
@ -51,6 +54,41 @@ export default (state = {
return { ...state, threeBoxInfo: message };
case SET_ALL_ORCH_SCORES:
return { ...state, orchScores: message };
case SET_ALL_ORCH_INFO:
return { ...state, orchInfo: message };
case CACHE_ORCHESTRATOR:
let isCached = false;
// Check to see if it is already cached
if (state.orchInfo) {
for (const thisOrch of state.orchInfo) {
if (thisOrch.id === message.id) {
isCached = true;
break;
}
}
}
// If cached, lookup and modify existing entry
if (isCached) {
return {
...state,
contents: state.orchInfo.map(
(content) => {
if (content.id == message.id) {
return message;
} else {
return content;
}
}
)
}
} else {
return {
...state,
orchInfo: [...state.orchInfo, message]
};
}
case SET_ALL_DEL_INFO:
return { ...state, delInfo: message };
default:
return { ...state };
}

View File

@ -125,4 +125,22 @@ export const getAllOrchScores = () => (
"Content-Type": "application/json"
}
})
);
export const getAllOrchInfo = () => (
fetch("api/livepeer/getAllOrchInfo", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
);
export const getAllDelInfo = () => (
fetch("api/livepeer/getAllDelInfo", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
);