Allow joining channels without querying API

This commit is contained in:
DataHoarder 2023-04-26 06:18:17 +02:00
parent 88ec29ecb2
commit 74ff0d870f
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 47 additions and 23 deletions

30
bot.go
View file

@ -2,13 +2,11 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
utils2 "git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
"git.gammaspectra.live/P2Pool/p2pool-observer/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
hbot "github.com/whyrusleeping/hellabot"
@ -140,33 +138,19 @@ func main() {
log.Printf("Creating entry for %s", split[2])
basePoolInfo := getPoolInfo(split[2])
consensusData, _ := json.Marshal(basePoolInfo["sidechain"].(map[string]any)["consensus"].(map[string]any))
consensus, err := sidechain.NewConsensusFromJSON(consensusData)
if err != nil {
log.Panic(err)
}
entry := &channelEntry{
ApiEndpoint: split[2],
Channel: split[0],
Name: split[1],
Consensus: consensus,
PreviousBlocks: getPreviousBlocks(split[2]),
Window: make(map[types.Hash]*index.SideBlock),
}
for _, b := range getSideBlocksFromAPI(entry.ApiEndpoint, fmt.Sprintf("/api/side_blocks_in_window?window=%d&noMainStatus", entry.DesiredPruneDistance())) {
entry.Window[b.TemplateId] = b
if entry.Tip == nil || entry.Tip.SideHeight < b.SideHeight {
entry.Tip = b
}
ApiEndpoint: split[2],
Channel: split[0],
Name: split[1],
Window: make(map[types.Hash]*index.SideBlock),
}
//websocket needs to come after
entry.getConsensus()
entry.getPreviousBlocks()
entry.getPreviousWindow()
entry.openWebSocket()
channelEntries = append(channelEntries, entry)
}
var onlineUsersLock sync.RWMutex

View file

@ -2,11 +2,13 @@ package main
import (
"context"
"encoding/json"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"golang.org/x/exp/slices"
"log"
"net/url"
"nhooyr.io/websocket"
"sync"
@ -72,6 +74,44 @@ func (c *channelEntry) DifficultyFromHeight(height uint64) types.Difficulty {
return types.ZeroDifficulty
}
func (c *channelEntry) getPreviousWindow() {
if c.ApiEndpoint == "" {
return
}
for _, b := range getSideBlocksFromAPI(c.ApiEndpoint, fmt.Sprintf("/api/side_blocks_in_window?window=%d&noMainStatus", c.DesiredPruneDistance())) {
c.Window[b.TemplateId] = b
if c.Tip == nil || c.Tip.SideHeight < b.SideHeight {
c.Tip = b
}
}
}
func (c *channelEntry) getConsensus() {
if c.ApiEndpoint == "" {
return
}
basePoolInfo := getPoolInfo(c.ApiEndpoint)
consensusData, _ := json.Marshal(basePoolInfo["sidechain"].(map[string]any)["consensus"].(map[string]any))
consensus, err := sidechain.NewConsensusFromJSON(consensusData)
if err != nil {
log.Panic(err)
}
c.Consensus = consensus
}
func (c *channelEntry) getPreviousBlocks() {
if c.ApiEndpoint == "" {
return
}
result := getSliceFromAPI[*index.FoundBlock](c.ApiEndpoint, fmt.Sprintf("/api/found_blocks?limit=%d", numberOfBlockHistoryToKeep))
//sort from oldest to newest
slices.SortFunc(result, func(a, b *index.FoundBlock) bool {
return a.MainBlock.Height < b.MainBlock.Height
})
c.PreviousBlocks = result
}
func (c *channelEntry) openWebSocket() {
if c.ApiEndpoint == "" {
c.Ws = nil