Skip to content

Commit

Permalink
Merge branch 'test' into feature/380
Browse files Browse the repository at this point in the history
  • Loading branch information
jdutchak authored Jul 12, 2024
2 parents afbf7f2 + 4744de3 commit 2225b51
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 178 deletions.
29 changes: 29 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
**Description**

This PR fixes #

**Notes for Reviewers**


**[Signed commits](../CONTRIBUTING.md#signing-off-on-commits-developer-certificate-of-origin)**
- [ ] Yes, I signed my commits.

<!--
Thank you for contributing to Masa!
Contributing Conventions
-------------------------
The draft above helps to give a quick overview of your PR.
Remember to remove this comment and to at least:
1. Include descriptive PR titles with [<component-name>] prepended. We use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).
2. Build and test your changes before submitting a PR.
3. Sign your commits
4. **Tag maintainer:** for a quicker response, tag the relevant maintainer (see below).
By following the community's contribution conventions upfront, the review process will
be accelerated and your PR merged more quickly.
-->
13 changes: 0 additions & 13 deletions .github/workflows/commitlint.yaml

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/prlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check PR style

on:
pull_request_target:
types:
- opened
- reopened
- edited
- synchronize

jobs:
title-lint:
runs-on: ubuntu-latest
permissions:
statuses: write
steps:
- uses: aslafy-z/conventional-pr-title-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
check-pr-description:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: jadrol/[email protected]
id: description-checker
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
exempt-labels: no qa
31 changes: 31 additions & 0 deletions .github/workflows/secscan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Security Scan"

# Run workflow each time code is pushed to your repository and on a schedule.
# The scheduled workflow runs every at 00:00 on Sunday UTC time.
on:
push:
pull_request:
schedule:
- cron: '0 0 * * 0'

jobs:
tests:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- name: Checkout Source
uses: actions/checkout@v4
if: ${{ github.actor != 'dependabot[bot]' }}
- name: Run Gosec Security Scanner
if: ${{ github.actor != 'dependabot[bot]' }}
uses: securego/gosec@master
with:
# we let the report trigger content trigger a failure using the GitHub Security features.
args: '-no-fail -fmt sarif -out results.sarif ./...'
- name: Upload SARIF file
if: ${{ github.actor != 'dependabot[bot]' }}
uses: github/codeql-action/upload-sarif@v3
with:
# Path to SARIF file relative to the root of the repository
sarif_file: results.sarif
132 changes: 72 additions & 60 deletions pkg/api/handlers_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,38 @@ func (api *API) GetFromDHT() gin.HandlerFunc {
})
return
}
sharedData := db.SharedData{}
nv := db.ReadData(api.Node, keyStr)
err := json.Unmarshal(nv, &sharedData)

nv, err := db.ReadData(api.Node, keyStr)
if err != nil {
if IsBase64(string(nv)) {
decodedString, _ := base64.StdEncoding.DecodeString(string(nv))
_ = json.Unmarshal(decodedString, &sharedData)
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": sharedData,
})
return
} else {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": string(nv),
})
return
logrus.WithFields(logrus.Fields{
"key": keyStr,
"error": err,
}).Error("Failed to read data from DHT")
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "failed to read data",
})
return
}

var sharedData db.SharedData
if err := json.Unmarshal(nv, &sharedData); err != nil {
if decodedString, decodeErr := base64.StdEncoding.DecodeString(string(nv)); decodeErr == nil {
if json.Unmarshal(decodedString, &sharedData) == nil {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": sharedData,
})
return
}
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": string(nv),
})
return
}

c.JSON(http.StatusOK, gin.H{
"success": true,
"message": sharedData,
Expand Down Expand Up @@ -376,50 +388,50 @@ func (api *API) ChatPageHandler() gin.HandlerFunc {
// an HTML page displaying the node's status and uptime info.
func (api *API) NodeStatusPageHandler() gin.HandlerFunc {
return func(c *gin.Context) {
peers := api.Node.Host.Network().Peers()
nodeData := api.Node.NodeTracker.GetNodeData(api.Node.Host.ID().String())
if nodeData == nil {
c.HTML(http.StatusOK, "index.html", gin.H{
"TotalPeers": 0,
"Name": "Masa Status Page",
"PeerID": api.Node.Host.ID().String(),
"IsStaked": false,
"IsTwitterScraper": false,
"IsDiscordScraper": false,
"IsWebScraper": false,
"FirstJoined": api.Node.FromUnixTime(time.Now().Unix()),
"LastJoined": api.Node.FromUnixTime(time.Now().Unix()),
"CurrentUptime": "0",
"Rewards": "Coming Soon!",
"BytesScraped": 0,
})
return
} else {
nd := *nodeData
nd.CurrentUptime = nodeData.GetCurrentUptime()
nd.AccumulatedUptime = nodeData.GetAccumulatedUptime()
nd.CurrentUptimeStr = pubsub.PrettyDuration(nd.CurrentUptime)
nd.AccumulatedUptimeStr = pubsub.PrettyDuration(nd.AccumulatedUptime)

sharedData := db.SharedData{}
nv := db.ReadData(api.Node, api.Node.Host.ID().String())
_ = json.Unmarshal(nv, &sharedData)
bytesScraped, _ := strconv.Atoi(fmt.Sprintf("%v", sharedData["bytesScraped"]))
c.HTML(http.StatusOK, "index.html", gin.H{
"TotalPeers": len(peers),
"Name": "Masa Status Page",
"PeerID": api.Node.Host.ID().String(),
"IsStaked": nd.IsStaked,
"IsTwitterScraper": nd.IsTwitterScraper,
"IsDiscordScraper": nd.IsDiscordScraper,
"IsWebScraper": nd.IsWebScraper,
"FirstJoined": api.Node.FromUnixTime(nd.FirstJoinedUnix),
"LastJoined": api.Node.FromUnixTime(nd.LastJoinedUnix),
"CurrentUptime": nd.CurrentUptimeStr,
"TotalUptime": nd.AccumulatedUptimeStr,
"BytesScraped": fmt.Sprintf("%.4f MB", float64(bytesScraped)/(1024*1024)),
})
// Initialize default values for the template data
templateData := gin.H{
"TotalPeers": 0,
"Name": "Masa Status Page",
"PeerID": api.Node.Host.ID().String(),
"IsStaked": false,
"IsTwitterScraper": false,
"IsDiscordScraper": false,
"IsWebScraper": false,
"FirstJoined": api.Node.FromUnixTime(time.Now().Unix()),
"LastJoined": api.Node.FromUnixTime(time.Now().Unix()),
"CurrentUptime": "0",
"TotalUptime": "0",
"Rewards": "Coming Soon!",
"BytesScraped": "0 MB",
}

if api.Node != nil && api.Node.Host != nil {
peers := api.Node.Host.Network().Peers()
templateData["TotalPeers"] = len(peers)

if nodeData := api.Node.NodeTracker.GetNodeData(api.Node.Host.ID().String()); nodeData != nil {
nd := *nodeData
templateData["IsStaked"] = nd.IsStaked
templateData["IsTwitterScraper"] = nd.IsTwitterScraper
templateData["IsDiscordScraper"] = nd.IsDiscordScraper
templateData["IsWebScraper"] = nd.IsWebScraper
templateData["FirstJoined"] = api.Node.FromUnixTime(nd.FirstJoinedUnix)
templateData["LastJoined"] = api.Node.FromUnixTime(nd.LastJoinedUnix)
templateData["CurrentUptime"] = pubsub.PrettyDuration(nd.GetCurrentUptime())
templateData["TotalUptime"] = pubsub.PrettyDuration(nd.GetAccumulatedUptime())

if nv, err := db.ReadData(api.Node, api.Node.Host.ID().String()); err == nil {
var sharedData db.SharedData
if json.Unmarshal(nv, &sharedData) == nil {
if bytesScraped, err := strconv.Atoi(fmt.Sprintf("%v", sharedData["bytesScraped"])); err == nil {
templateData["BytesScraped"] = fmt.Sprintf("%.4f MB", float64(bytesScraped)/(1024*1024))
}
}
}
}
}

c.HTML(http.StatusOK, "index.html", templateData)
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/db/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func WriteData(node *masa.OracleNode, key string, value []byte) error {

// ReadData reads the value for the given key from the database.
// It requires the host for access control verification before reading.
func ReadData(node *masa.OracleNode, key string) []byte {
func ReadData(node *masa.OracleNode, key string) ([]byte, error) {
logrus.WithFields(logrus.Fields{
"nodeID": node.Host.ID().String(),
"isAuthorized": true,
"ReadData": true,
})
}).Info("Attempting to read data")

ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
Expand All @@ -95,11 +95,11 @@ func ReadData(node *masa.OracleNode, key string) []byte {
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Debug("Failed to read from the database")
return val
}).Error("Failed to read from the database")
return nil, err
}

return val
return val, nil
}

func SendToS3(uid string, payload map[string]string) error {
Expand Down
Loading

0 comments on commit 2225b51

Please sign in to comment.