Closed ninabreznik closed 4 years ago
const chainAPI = { registerHoster, registerEncoder, publishData }
function registerHoster (payload) {
const hosterID = saveHoster(payload)
const hostingEvents = calculateAsHoster(hosterID, payload)
emitHostingEvents(hostingEvents)
}
function publishData (payload) {
const feedID = saveHoster(payload)
const hostingEvents = calculateAsPublisher(feedID, payload)
emitHostingEvents(hostingEvents)
}
function registerEncoder (payload) {
const encoderID = saveEncoder(payload)
const hostingEvents = calculateAsEncoder(encoderID, payload)
emitHostingEvents(hostingEvents)
}
/************************************************************************/
// INTERNAL
const underemployed_hosters = []
const unemployed_encoders = []
const waiting_feeds = []
function emitHostingEvents (hostingEvents) {
if (!hostingEvents.length) return
for (const [feedID, encoderID, hosterID] in hostingEvents) {
emit(['NewPin', feedID, encoderID, hosterID])
}
}
function calculateAsEncoder(encoderID, payload) {
const events = []
const len = Math.min(waiting_feeds.length, underemployed_hosters.length)
for (var i = 0; i < len; i++) {
events.push([waiting_feeds[i], encoderID, underemployed_hosters[i]])
}
return events
}
function calculateAsHoster(hosterID, payload) {
const events = []
const len = Math.min(waiting_feeds.length, unemployed_encoders.length)
for (var i = 0; i < len; i++) {
events.push([waiting_feeds[i], , unemployed_encoders[i], hosterID])
}
return events
}
function calculateAsPublisher(feedID, payload) {
const events = []
const len = Math.min(unemployed_encoders.length, underemployed_hosters.length)
for (var i = 0; i < len; i++) {
events.push([feedID, unemployed_encoders[i], underemployed_hosters[i]])
}
return events
}
my goal is to simplify the storage of challenges held by hosters from multiple storage values, to:
(hoster, archive) => HostedArchive
(or, use DoubleMap, so we can iterate over exclusively hosters to find all archives they host)
hoster, archive => HostedArchive
where
struct HostedArchive {
encoded: SortedVec<(ChunkIndex, EncoderId)>,
state: State,
}
enum State {
Encoding,
Ready(Vec<Challenge>),
}
enum Challenge {
Active(blockNumber, chunk),
Attesting(ChallengeAttestations),
}
the goal of which would be that we can drastically reduce the storage lookups when challenging a hoster
publishData
Problem:
we should instead emit event (selected encoders & selected hosters)
Solution?
newHostingRequest emits 3 selected hosters and 3 selected encoders