Skip to content

Commit

Permalink
Merge pull request #2 from openrelayxyz/feature/isSynced-plugin
Browse files Browse the repository at this point in the history
Better issynced plugin and updated mod and sum
  • Loading branch information
AusIV authored Oct 11, 2021
2 parents cd458cb + c3ec8ad commit ec0fbf0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.16

require (
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/openrelayxyz/plugeth-utils v0.0.7
github.com/openrelayxyz/plugeth-utils v0.0.8
gopkg.in/urfave/cli.v1 v1.20.0
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/openrelayxyz/plugeth-utils v0.0.6 h1:bnoUyRBrxbkfd5Zn89X1D6zEQicpPY3qW81iBrq+6e4=
github.com/openrelayxyz/plugeth-utils v0.0.6/go.mod h1:Lv47unyKJ3b/PVbVAt9Uk+RQmpdrzDOsjSCPhAMQAps=
github.com/openrelayxyz/plugeth-utils v0.0.7 h1:rPJJYSncqwWo/v57Aghl2LL4bcNg5lHfIthrSeeQE8M=
github.com/openrelayxyz/plugeth-utils v0.0.7/go.mod h1:Lv47unyKJ3b/PVbVAt9Uk+RQmpdrzDOsjSCPhAMQAps=
github.com/openrelayxyz/plugeth-utils v0.0.8 h1:HpXGnWTQD0/zOmva/YLdY53HET70h/LQLSuVCJl1gdY=
github.com/openrelayxyz/plugeth-utils v0.0.8/go.mod h1:Lv47unyKJ3b/PVbVAt9Uk+RQmpdrzDOsjSCPhAMQAps=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
81 changes: 81 additions & 0 deletions packages/isSynced/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"context"
"fmt"
"math/big"

"github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted/crypto"
"gopkg.in/urfave/cli.v1"
)

type IsSyncedService struct {
backend core.Backend
stack core.Node
}

type peerinfo struct {
Protocols struct {
Eth struct {
Difficulty *big.Int `json:"difficulty"`
} `json:"eth"`
} `json:"protocols"`
}

var log core.Logger

var httpApiFlagName = "http.api"

func Initialize(ctx *cli.Context, loader core.PluginLoader, logger core.Logger) {
log = logger
v := ctx.GlobalString(httpApiFlagName)
if v != "" {
ctx.GlobalSet(httpApiFlagName, v+",plugeth")
} else {
ctx.GlobalSet(httpApiFlagName, "eth,net,web3,plugeth")
log.Info("Loaded isSynced plugin")
}
}

func GetAPIs(stack core.Node, backend core.Backend) []core.API {
return []core.API{
{
Namespace: "plugeth",
Version: "1.0",
Service: &IsSyncedService{backend, stack},
Public: true,
},
}
}

func (service *IsSyncedService) IsSynced(ctx context.Context) (interface{}, error) {
client, err := service.stack.Attach()
if err != nil {
return nil, err
}
var x []peerinfo
err = client.Call(&x, "admin_peers")
peers := false
hash := crypto.Keccak256Hash(service.backend.CurrentHeader())
totalDifficulty := service.backend.GetTd(ctx, hash)
y := len(x)
if y > 0 {
for i := range x {
if totalDifficulty.Cmp(x[i].Protocols.Eth.Difficulty) <= 0 {
peers = true
break
}
}
}
progress := service.backend.Downloader().Progress()
return map[string]interface{}{
"startingBlock": fmt.Sprintf("%#x", (progress.StartingBlock())),
"currentBlock": fmt.Sprintf("%#x", (progress.CurrentBlock())),
"highestBlock": fmt.Sprintf("%#x", (progress.HighestBlock())),
"pulledStates": fmt.Sprintf("%#x", (progress.PulledStates())),
"knownStates": fmt.Sprintf("%#x", (progress.KnownStates())),
"activePeers": peers,
"nodeIsSynced": peers && progress.CurrentBlock() >= progress.HighestBlock(),
}, err
}

0 comments on commit ec0fbf0

Please sign in to comment.